Skip to content

Commit 262a879

Browse files
hi-ogawaAriPerkkio
andauthored
fix(ssr): (backport #18150) fix source map remapping with multiple sources (#18204)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 0474550 commit 262a879

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

Diff for: packages/vite/src/node/ssr/__tests__/fixtures/multi-source-sourcemaps/dist.js

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/vite/src/node/ssr/__tests__/fixtures/multi-source-sourcemaps/dist.js.map

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* You can rebuild this with:
3+
* - rm -f ./dist.js ./dist.js.map
4+
* - npx esbuild --bundle entrypoint.js --outfile=dist.js --sourcemap --format=esm
5+
*/
6+
7+
import nested from './nested-directory/nested-file'
8+
9+
export function entrypoint() {
10+
console.log(nested)
11+
throw new Error('Hello world')
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'Nested file will trigger edge case that used to break sourcemaps'

Diff for: packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,31 @@ test('sourcemap with multiple sources', async () => {
441441
}
442442
})
443443

444+
test('sourcemap with multiple sources and nested paths', async () => {
445+
const code = readFixture('dist.js')
446+
const map = readFixture('dist.js.map')
447+
448+
const result = await ssrTransform(code, JSON.parse(map), '', code)
449+
assert(result?.map)
450+
451+
const { sources } = result.map as SourceMap
452+
expect(sources).toMatchInlineSnapshot(`
453+
[
454+
"nested-directory/nested-file.js",
455+
"entrypoint.js",
456+
]
457+
`)
458+
459+
function readFixture(filename: string) {
460+
const url = new URL(
461+
`./fixtures/multi-source-sourcemaps/${filename}`,
462+
import.meta.url,
463+
)
464+
465+
return readFileSync(fileURLToPath(url), 'utf8')
466+
}
467+
})
468+
444469
test('overwrite bindings', async () => {
445470
expect(
446471
await ssrTransformSimpleCode(

Diff for: packages/vite/src/node/ssr/ssrTransform.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -346,25 +346,20 @@ async function ssrTransformScript(
346346
})
347347

348348
let map = s.generateMap({ hires: 'boundary' })
349+
map.sources = [path.basename(url)]
350+
// needs to use originalCode instead of code
351+
// because code might be already transformed even if map is null
352+
map.sourcesContent = [originalCode]
349353
if (
350354
inMap &&
351355
inMap.mappings &&
352356
'sources' in inMap &&
353357
inMap.sources.length > 0
354358
) {
355359
map = combineSourcemaps(url, [
356-
{
357-
...map,
358-
sources: inMap.sources,
359-
sourcesContent: inMap.sourcesContent,
360-
} as RawSourceMap,
360+
map as RawSourceMap,
361361
inMap as RawSourceMap,
362362
]) as SourceMap
363-
} else {
364-
map.sources = [path.basename(url)]
365-
// needs to use originalCode instead of code
366-
// because code might be already transformed even if map is null
367-
map.sourcesContent = [originalCode]
368363
}
369364

370365
return {

0 commit comments

Comments
 (0)