Skip to content

Commit 3395c19

Browse files
committed
fix(ssr): combine empty source mappings
1 parent 4f5845a commit 3395c19

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

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

+74
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { SourceMap } from 'rollup'
55
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
66
import { transformWithEsbuild } from '../../plugins/esbuild'
77
import { ssrTransform } from '../ssrTransform'
8+
import { createServer } from '../..'
89

910
const ssrTransformSimple = async (code: string, url = '') =>
1011
ssrTransform(code, null, url, code)
@@ -1385,3 +1386,76 @@ const c = () => {
13851386
`,
13861387
)
13871388
})
1389+
1390+
test('combine mappings', async () => {
1391+
const server = await createServer({
1392+
configFile: false,
1393+
envFile: false,
1394+
logLevel: 'error',
1395+
plugins: [
1396+
{
1397+
name: 'test-mappings',
1398+
resolveId(source) {
1399+
if (source.startsWith('virtual:test-mappings')) {
1400+
return '\0' + source
1401+
}
1402+
},
1403+
load(id) {
1404+
if (id.startsWith('\0virtual:test-mappings')) {
1405+
const code = `export default "test";\n`
1406+
if (id === '\0virtual:test-mappings:empty') {
1407+
return { code, map: { mappings: '' } }
1408+
}
1409+
if (id === '\0virtual:test-mappings:null') {
1410+
return { code, map: null }
1411+
}
1412+
}
1413+
},
1414+
},
1415+
],
1416+
})
1417+
1418+
{
1419+
const result = await server.environments.ssr.transformRequest(
1420+
'virtual:test-mappings:empty',
1421+
)
1422+
expect(result?.map).toMatchInlineSnapshot(`
1423+
{
1424+
"mappings": "",
1425+
}
1426+
`)
1427+
const mod = await server.ssrLoadModule('virtual:test-mappings:empty')
1428+
expect(mod).toMatchInlineSnapshot(`
1429+
{
1430+
"default": "test",
1431+
}
1432+
`)
1433+
}
1434+
1435+
{
1436+
const result = await server.environments.ssr.transformRequest(
1437+
'virtual:test-mappings:null',
1438+
)
1439+
expect(result?.map).toMatchInlineSnapshot(`
1440+
SourceMap {
1441+
"file": undefined,
1442+
"mappings": "AAAA,8BAAc,CAAC,CAAC,IAAI,CAAC;",
1443+
"names": [],
1444+
"sources": [
1445+
"virtual:test-mappings:null",
1446+
],
1447+
"sourcesContent": [
1448+
"export default "test";
1449+
",
1450+
],
1451+
"version": 3,
1452+
}
1453+
`)
1454+
const mod = await server.ssrLoadModule('virtual:test-mappings:null')
1455+
expect(mod).toMatchInlineSnapshot(`
1456+
{
1457+
"default": "test",
1458+
}
1459+
`)
1460+
}
1461+
})

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

+20-15
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,26 @@ async function ssrTransformScript(
419419
},
420420
})
421421

422-
let map = s.generateMap({ hires: 'boundary' })
423-
map.sources = [path.basename(url)]
424-
// needs to use originalCode instead of code
425-
// because code might be already transformed even if map is null
426-
map.sourcesContent = [originalCode]
427-
if (
428-
inMap &&
429-
inMap.mappings &&
430-
'sources' in inMap &&
431-
inMap.sources.length > 0
432-
) {
433-
map = combineSourcemaps(url, [
434-
map as RawSourceMap,
435-
inMap as RawSourceMap,
436-
]) as SourceMap
422+
let map: TransformResult['map']
423+
if (inMap?.mappings === '') {
424+
map = inMap
425+
} else {
426+
map = s.generateMap({ hires: 'boundary' })
427+
map.sources = [path.basename(url)]
428+
// needs to use originalCode instead of code
429+
// because code might be already transformed even if map is null
430+
map.sourcesContent = [originalCode]
431+
if (
432+
inMap &&
433+
inMap.mappings &&
434+
'sources' in inMap &&
435+
inMap.sources.length > 0
436+
) {
437+
map = combineSourcemaps(url, [
438+
map as RawSourceMap,
439+
inMap as RawSourceMap,
440+
]) as SourceMap
441+
}
437442
}
438443

439444
return {

0 commit comments

Comments
 (0)