Skip to content

Commit

Permalink
fix(ssr): (backport #18150) fix source map remapping with multiple so…
Browse files Browse the repository at this point in the history
…urces (#18204)

Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
  • Loading branch information
hi-ogawa and AriPerkkio authored Sep 30, 2024
1 parent 0474550 commit 262a879
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Nested file will trigger edge case that used to break sourcemaps'
25 changes: 25 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 5 additions & 10 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,20 @@ 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 &&
'sources' in inMap &&
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 {
Expand Down

0 comments on commit 262a879

Please sign in to comment.