Skip to content
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
24 changes: 24 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '..'
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}
}

Expand Down
Loading