diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index a5abea866b0c7f..e01bbd5a68d08e 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -2,6 +2,8 @@ import crypto from 'node:crypto' import fs from 'node:fs' import os, { type NetworkInterfaceInfoIPv4 } from 'node:os' import path from 'node:path' +import type { DecodedSourceMap, RawSourceMap } from '@jridgewell/remapping' +import MagicString from 'magic-string' import { fileURLToPath } from 'mlly' import { describe, expect, test, vi, onTestFinished } from 'vitest' import type { CommonServerOptions, ResolvedServerUrls } from '..' @@ -902,6 +904,28 @@ describe('combineSourcemaps', () => { return normalizePath(path.resolve(_dirname, file)) } + test('composes decoded intermediate mappings identically', () => { + const filename = 'assets/index.js' + const s = new MagicString('export const value = 1') + const originalMap = s.generateMap({ + source: filename, + hires: true, + includeContent: true, + }) + const options = { source: filename, hires: 'boundary' as const } + const encoded = combineSourcemaps(filename, [ + s.generateMap(options) as RawSourceMap, + originalMap as RawSourceMap, + ]) + const decoded = combineSourcemaps(filename, [ + s.generateDecodedMap(options) as DecodedSourceMap, + originalMap as RawSourceMap, + ]) + + expect(decoded).toStrictEqual(encoded) + expect(decoded.sourcesContent).toStrictEqual(originalMap.sourcesContent) + }) + test('should combine sourcemaps with single sources', () => { const sourcemaps = [ // processed with magic-string diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index ca10f62ca560b7..2904527ce76494 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -1,5 +1,5 @@ import path from 'node:path' -import type { RawSourceMap } from '@jridgewell/remapping' +import type { DecodedSourceMap, RawSourceMap } from '@jridgewell/remapping' import convertSourceMap from 'convert-source-map' import type { ImportSpecifier } from 'es-module-lexer' import { init, parse as parseImports } from 'es-module-lexer' @@ -604,13 +604,13 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] { if (s.hasChanged()) { chunk.code = s.toString() if (buildSourcemap && chunk.map) { - const nextMap = s.generateMap({ + const nextMap = s.generateDecodedMap({ source: chunk.fileName, hires: 'boundary', }) const originalFile = chunk.map.file const map = combineSourcemaps(chunk.fileName, [ - nextMap as RawSourceMap, + nextMap as DecodedSourceMap, chunk.map as RawSourceMap, ]) as SourceMap map.toUrl = () => genSourceMapUrl(map) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index d53dc833081670..49321f47e7457d 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -1,5 +1,5 @@ import path from 'node:path' -import type { RawSourceMap } from '@jridgewell/remapping' +import type { DecodedSourceMap, RawSourceMap } from '@jridgewell/remapping' import { walk as eswalk } from 'estree-walker' import MagicString from 'magic-string' import { extract_names as extractNames } from 'periscopic' @@ -414,21 +414,27 @@ async function ssrTransformScript( if (inMap?.mappings === '') { map = inMap } else { - map = s.generateMap({ hires: 'boundary' }) as SourceMap - 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 ( + const shouldCombine = !!( inMap && inMap.mappings && 'sources' in inMap && inMap.sources.length > 0 - ) { + ) + const mapOptions = { hires: 'boundary' as const } + const nextMap = shouldCombine + ? s.generateDecodedMap(mapOptions) + : s.generateMap(mapOptions) + nextMap.sources = [path.basename(url)] + // needs to use originalCode instead of code + // because code might be already transformed even if map is null + nextMap.sourcesContent = [originalCode] + if (shouldCombine) { map = combineSourcemaps(url, [ - map as RawSourceMap, + nextMap as DecodedSourceMap, inMap as RawSourceMap, ]) as SourceMap + } else { + map = nextMap as SourceMap } }