0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

Fix sourcemap warning in server islands plugin (#12877)

This commit is contained in:
Bjorn Lu 2025-01-03 22:43:22 +08:00 committed by GitHub
parent 78bcad952e
commit 73a078835e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes sourcemap warning generated by the `astro:server-islands` Vite plugin

View file

@ -1,3 +1,4 @@
import MagicString from 'magic-string';
import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite'; import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite';
import type { AstroPluginOptions } from '../../types/astro.js'; import type { AstroPluginOptions } from '../../types/astro.js';
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js'; import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js';
@ -82,6 +83,15 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
}, },
renderChunk(code) { renderChunk(code) {
if (code.includes(serverIslandPlaceholder)) { if (code.includes(serverIslandPlaceholder)) {
// If there's no reference, we can fast-path to an empty map replacement
// without sourcemaps as it doesn't shift rows
if (referenceIdMap.size === 0) {
return {
code: code.replace(serverIslandPlaceholder, 'new Map();'),
map: null,
};
}
let mapSource = 'new Map(['; let mapSource = 'new Map([';
for (let [resolvedPath, referenceId] of referenceIdMap) { for (let [resolvedPath, referenceId] of referenceIdMap) {
const fileName = this.getFileName(referenceId); const fileName = this.getFileName(referenceId);
@ -90,7 +100,13 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
} }
mapSource += '\n]);'; mapSource += '\n]);';
referenceIdMap.clear(); referenceIdMap.clear();
return code.replace(serverIslandPlaceholder, mapSource);
const ms = new MagicString(code);
ms.replace(serverIslandPlaceholder, mapSource);
return {
code: ms.toString(),
map: ms.generateMap({ hires: 'boundary' }),
};
} }
}, },
}; };