Skip to content

Commit 73a0788

Browse files
authored
Fix sourcemap warning in server islands plugin (#12877)
1 parent 78bcad9 commit 73a0788

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

.changeset/big-mugs-approve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes sourcemap warning generated by the `astro:server-islands` Vite plugin

packages/astro/src/core/server-islands/vite-plugin-server-islands.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import MagicString from 'magic-string';
12
import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite';
23
import type { AstroPluginOptions } from '../../types/astro.js';
34
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js';
@@ -82,6 +83,15 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
8283
},
8384
renderChunk(code) {
8485
if (code.includes(serverIslandPlaceholder)) {
86+
// If there's no reference, we can fast-path to an empty map replacement
87+
// without sourcemaps as it doesn't shift rows
88+
if (referenceIdMap.size === 0) {
89+
return {
90+
code: code.replace(serverIslandPlaceholder, 'new Map();'),
91+
map: null,
92+
};
93+
}
94+
8595
let mapSource = 'new Map([';
8696
for (let [resolvedPath, referenceId] of referenceIdMap) {
8797
const fileName = this.getFileName(referenceId);
@@ -90,7 +100,13 @@ export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions
90100
}
91101
mapSource += '\n]);';
92102
referenceIdMap.clear();
93-
return code.replace(serverIslandPlaceholder, mapSource);
103+
104+
const ms = new MagicString(code);
105+
ms.replace(serverIslandPlaceholder, mapSource);
106+
return {
107+
code: ms.toString(),
108+
map: ms.generateMap({ hires: 'boundary' }),
109+
};
94110
}
95111
},
96112
};

0 commit comments

Comments
 (0)