diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 226e0ef176278e..2e0768ef0a2f94 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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) @@ -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", + } + `) + } +}) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 8c44859bc49129..25735fd33b0756 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -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 {