fix(ssr): (backport #18150) fix source map remapping with multiple sources (#18204)

Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
This commit is contained in:
Hiroshi Ogawa 2024-09-30 16:30:12 +09:00 committed by GitHub
parent 0474550c9f
commit 262a8796d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 10 deletions

View File

@ -0,0 +1,11 @@
// nested-directory/nested-file.js
var nested_file_default =
'Nested file will trigger edge case that used to break sourcemaps'
// entrypoint.js
function entrypoint() {
console.log(nested_file_default)
throw new Error('Hello world')
}
export { entrypoint }
//# sourceMappingURL=dist.js.map

View File

@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["nested-directory/nested-file.js", "entrypoint.js"],
"sourcesContent": ["export default 'Nested file will trigger edge case that used to break sourcemaps'\n", "/*\n * You can rebuild this with:\n * - rm -f ./dist.js ./dist.js.map\n * - npx esbuild --bundle entrypoint.js --outfile=dist.js --sourcemap --format=esm\n */\n\nimport nested from './nested-directory/nested-file'\n\nexport function entrypoint() {\n console.log(nested)\n throw new Error('Hello world')\n}\n"],
"mappings": ";AAAA,IAAO,sBAAQ;;;ACQR,SAAS,aAAa;AAC3B,UAAQ,IAAI,mBAAM;AAClB,QAAM,IAAI,MAAM,aAAa;AAC/B;",
"names": []
}

View File

@ -0,0 +1,12 @@
/*
* You can rebuild this with:
* - rm -f ./dist.js ./dist.js.map
* - npx esbuild --bundle entrypoint.js --outfile=dist.js --sourcemap --format=esm
*/
import nested from './nested-directory/nested-file'
export function entrypoint() {
console.log(nested)
throw new Error('Hello world')
}

View File

@ -0,0 +1 @@
export default 'Nested file will trigger edge case that used to break sourcemaps'

View File

@ -441,6 +441,31 @@ test('sourcemap with multiple sources', async () => {
}
})
test('sourcemap with multiple sources and nested paths', async () => {
const code = readFixture('dist.js')
const map = readFixture('dist.js.map')
const result = await ssrTransform(code, JSON.parse(map), '', code)
assert(result?.map)
const { sources } = result.map as SourceMap
expect(sources).toMatchInlineSnapshot(`
[
"nested-directory/nested-file.js",
"entrypoint.js",
]
`)
function readFixture(filename: string) {
const url = new URL(
`./fixtures/multi-source-sourcemaps/${filename}`,
import.meta.url,
)
return readFileSync(fileURLToPath(url), 'utf8')
}
})
test('overwrite bindings', async () => {
expect(
await ssrTransformSimpleCode(

View File

@ -346,6 +346,10 @@ async function ssrTransformScript(
})
let map = s.generateMap({ hires: 'boundary' })
map.sources = [path.basename(url)]
// needs to use originalCode instead of code
// because code might be already transformed even if map is null
map.sourcesContent = [originalCode]
if (
inMap &&
inMap.mappings &&
@ -353,18 +357,9 @@ async function ssrTransformScript(
inMap.sources.length > 0
) {
map = combineSourcemaps(url, [
{
...map,
sources: inMap.sources,
sourcesContent: inMap.sourcesContent,
} as RawSourceMap,
map as RawSourceMap,
inMap as RawSourceMap,
]) as SourceMap
} else {
map.sources = [path.basename(url)]
// needs to use originalCode instead of code
// because code might be already transformed even if map is null
map.sourcesContent = [originalCode]
}
return {