Skip to content

Commit

Permalink
fix(ssr): combine empty source mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 19, 2025
1 parent 4f5845a commit 3395c19
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
74 changes: 74 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SourceMap } from 'rollup'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import { transformWithEsbuild } from '../../plugins/esbuild'
import { ssrTransform } from '../ssrTransform'
import { createServer } from '../..'

const ssrTransformSimple = async (code: string, url = '') =>
ssrTransform(code, null, url, code)
Expand Down Expand Up @@ -1385,3 +1386,76 @@ const c = () => {
`,
)
})

test('combine mappings', async () => {
const server = await createServer({
configFile: false,
envFile: false,
logLevel: 'error',
plugins: [
{
name: 'test-mappings',
resolveId(source) {
if (source.startsWith('virtual:test-mappings')) {
return '\0' + source
}
},
load(id) {
if (id.startsWith('\0virtual:test-mappings')) {
const code = `export default "test";\n`
if (id === '\0virtual:test-mappings:empty') {
return { code, map: { mappings: '' } }
}
if (id === '\0virtual:test-mappings:null') {
return { code, map: null }
}
}
},
},
],
})

{
const result = await server.environments.ssr.transformRequest(
'virtual:test-mappings:empty',
)
expect(result?.map).toMatchInlineSnapshot(`
{
"mappings": "",
}
`)
const mod = await server.ssrLoadModule('virtual:test-mappings:empty')
expect(mod).toMatchInlineSnapshot(`
{
"default": "test",
}
`)
}

{
const result = await server.environments.ssr.transformRequest(
'virtual:test-mappings:null',
)
expect(result?.map).toMatchInlineSnapshot(`
SourceMap {
"file": undefined,
"mappings": "AAAA,8BAAc,CAAC,CAAC,IAAI,CAAC;",
"names": [],
"sources": [
"virtual:test-mappings:null",
],
"sourcesContent": [
"export default "test";
",
],
"version": 3,
}
`)
const mod = await server.ssrLoadModule('virtual:test-mappings:null')
expect(mod).toMatchInlineSnapshot(`
{
"default": "test",
}
`)
}
})
35 changes: 20 additions & 15 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,26 @@ 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 as RawSourceMap,
inMap as RawSourceMap,
]) as SourceMap
let map: TransformResult['map']
if (inMap?.mappings === '') {
map = inMap
} else {
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 as RawSourceMap,
inMap as RawSourceMap,
]) as SourceMap
}
}

return {
Expand Down

0 comments on commit 3395c19

Please sign in to comment.