Skip to content

Commit f321fa8

Browse files
authoredJul 17, 2024··
fix(ssrTransform): sourcemaps with multiple sources (#17677)
1 parent 63fabfc commit f321fa8

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed
 

‎packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js

+30
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/bundled-with-sourcemaps/bundle.js.map

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

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { expect, test } from 'vitest'
1+
import { readFileSync } from 'node:fs'
2+
import { fileURLToPath } from 'node:url'
3+
import { assert, expect, test } from 'vitest'
4+
import type { SourceMap } from 'rollup'
25
import { transformWithEsbuild } from '../../plugins/esbuild'
36
import { ssrTransform } from '../ssrTransform'
47

@@ -411,11 +414,33 @@ test('sourcemap source', async () => {
411414
'input.js',
412415
'export const a = 1 /* */',
413416
)
414-
)?.map
417+
)?.map as SourceMap
418+
415419
expect(map?.sources).toStrictEqual(['input.js'])
416420
expect(map?.sourcesContent).toStrictEqual(['export const a = 1 /* */'])
417421
})
418422

423+
test('sourcemap with multiple sources', async () => {
424+
const code = readFixture('bundle.js')
425+
const map = readFixture('bundle.js.map')
426+
427+
const result = await ssrTransform(code, JSON.parse(map), '', code)
428+
assert(result?.map)
429+
430+
const { sources } = result.map as SourceMap
431+
expect(sources).toContain('./first.ts')
432+
expect(sources).toContain('./second.ts')
433+
434+
function readFixture(filename: string) {
435+
const url = new URL(
436+
`./fixtures/bundled-with-sourcemaps/${filename}`,
437+
import.meta.url,
438+
)
439+
440+
return readFileSync(fileURLToPath(url), 'utf8')
441+
}
442+
})
443+
419444
test('overwrite bindings', async () => {
420445
expect(
421446
await ssrTransformSimpleCode(

‎packages/vite/src/node/utils.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,6 @@ export function combineSourcemaps(
852852
}
853853
return newSourcemaps
854854
})
855-
const escapedFilename = escapeToLinuxLikePath(filename)
856855

857856
// We don't declare type here so we can convert/fake/map as RawSourceMap
858857
let map //: SourceMap
@@ -863,11 +862,15 @@ export function combineSourcemaps(
863862
map = remapping(sourcemapList, () => null)
864863
} else {
865864
map = remapping(sourcemapList[0], function loader(sourcefile) {
866-
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
867-
return sourcemapList[mapIndex++]
868-
} else {
869-
return null
865+
const mapForSources = sourcemapList
866+
.slice(mapIndex)
867+
.find((s) => s.sources.includes(sourcefile))
868+
869+
if (mapForSources) {
870+
mapIndex++
871+
return mapForSources
870872
}
873+
return null
871874
})
872875
}
873876
if (!map.file) {

0 commit comments

Comments
 (0)
Please sign in to comment.