Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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 @@ -347,25 +347,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