Skip to content

Commit

Permalink
Fix sourcemap warning in server islands plugin (#12877)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 3, 2025
1 parent 78bcad9 commit 73a0788
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/big-mugs-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes sourcemap warning generated by the `astro:server-islands` Vite plugin
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import MagicString from 'magic-string';
import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite';
import type { AstroPluginOptions } from '../../types/astro.js';
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js';
Expand Down Expand Up @@ -82,6 +83,15 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
},
renderChunk(code) {
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([';
for (let [resolvedPath, referenceId] of referenceIdMap) {
const fileName = this.getFileName(referenceId);
Expand All @@ -90,7 +100,13 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
}
mapSource += '\n]);';
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' }),
};
}
},
};
Expand Down

0 comments on commit 73a0788

Please sign in to comment.