Skip to content

Commit e003a2c

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

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 1 addition & 0 deletions
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'

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,31 @@ test('sourcemap with multiple sources', async () => {
463463
}
464464
})
465465

466+
test('sourcemap with multiple sources and nested paths', async () => {
467+
const code = readFixture('dist.js')
468+
const map = readFixture('dist.js.map')
469+
470+
const result = await ssrTransform(code, JSON.parse(map), '', code)
471+
assert(result?.map)
472+
473+
const { sources } = result.map as SourceMap
474+
expect(sources).toMatchInlineSnapshot(`
475+
[
476+
"nested-directory/nested-file.js",
477+
"entrypoint.js",
478+
]
479+
`)
480+
481+
function readFixture(filename: string) {
482+
const url = new URL(
483+
`./fixtures/multi-source-sourcemaps/${filename}`,
484+
import.meta.url,
485+
)
486+
487+
return readFileSync(fileURLToPath(url), 'utf8')
488+
}
489+
})
490+
466491
test('overwrite bindings', async () => {
467492
expect(
468493
await ssrTransformSimpleCode(

packages/vite/src/node/ssr/ssrTransform.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,25 +351,20 @@ async function ssrTransformScript(
351351
})
352352

353353
let map = s.generateMap({ hires: 'boundary' })
354+
map.sources = [path.basename(url)]
355+
// needs to use originalCode instead of code
356+
// because code might be already transformed even if map is null
357+
map.sourcesContent = [originalCode]
354358
if (
355359
inMap &&
356360
inMap.mappings &&
357361
'sources' in inMap &&
358362
inMap.sources.length > 0
359363
) {
360364
map = combineSourcemaps(url, [
361-
{
362-
...map,
363-
sources: inMap.sources,
364-
sourcesContent: inMap.sourcesContent,
365-
} as RawSourceMap,
365+
map as RawSourceMap,
366366
inMap as RawSourceMap,
367367
]) as SourceMap
368-
} else {
369-
map.sources = [path.basename(url)]
370-
// needs to use originalCode instead of code
371-
// because code might be already transformed even if map is null
372-
map.sourcesContent = [originalCode]
373368
}
374369

375370
return {

0 commit comments

Comments
 (0)