Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): combine empty source mappings #19226

Merged
merged 1 commit into from
Jan 23, 2025
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
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
Loading