Skip to content

Commit cbf91ba

Browse files
authoredDec 5, 2022
fix: always apply vite ssr source maps (#2433)
* fix: always apply vite ssr source maps * chore: remove non-vite-node source maps from code * test: source map test * chore: force esbuild to not generate inline source map * chore: fix failing text
1 parent 5098b21 commit cbf91ba

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed
 

‎packages/vite-node/src/client.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isNodeBuiltin } from 'mlly'
66
import createDebug from 'debug'
77
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
88
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
9+
import { extractSourceMap } from './source-map'
910

1011
const debugExecute = createDebug('vite-node:client:execute')
1112
const debugNative = createDebug('vite-node:client:native')
@@ -114,13 +115,11 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
114115
* Return parsed source map based on inlined source map of the module
115116
*/
116117
getSourceMap(id: string) {
117-
const fsPath = this.normalizePath(id)
118-
const cache = this.get(fsPath)
118+
const cache = this.get(id)
119119
if (cache.map)
120120
return cache.map
121-
const mapString = cache?.code?.match(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/)?.[1]
122-
if (mapString) {
123-
const map = JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
121+
const map = cache.code && extractSourceMap(cache.code)
122+
if (map) {
124123
cache.map = map
125124
return map
126125
}

‎packages/vite-node/src/server.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import type { TransformResult, ViteDevServer } from 'vite'
44
import createDebug from 'debug'
55
import type { DebuggerOptions, FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
66
import { shouldExternalize } from './externalize'
7-
import { toArray, toFilePath, withInlineSourcemap } from './utils'
7+
import { toArray, toFilePath } from './utils'
88
import { Debugger } from './debug'
9+
import { withInlineSourcemap } from './source-map'
910

1011
export * from './externalize'
1112

‎packages/vite-node/src/source-map.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
import { install } from 'source-map-support'
2+
import type { TransformResult } from 'vite'
23
import type { RawSourceMap } from './types'
34

45
interface InstallSourceMapSupportOptions {
56
getSourceMap: (source: string) => RawSourceMap | null | undefined
67
}
78

9+
let SOURCEMAPPING_URL = 'sourceMa'
10+
SOURCEMAPPING_URL += 'ppingURL'
11+
12+
const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;source=vite-node`
13+
const VITE_NODE_SOURCEMAPPING_REGEXP = new RegExp(`//# ${VITE_NODE_SOURCEMAPPING_URL};base64,(.+)`)
14+
const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,(.+)`)
15+
16+
export async function withInlineSourcemap(result: TransformResult) {
17+
const { code, map } = result
18+
19+
if (!map || code.includes(VITE_NODE_SOURCEMAPPING_URL))
20+
return result
21+
22+
// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
23+
if (OTHER_SOURCE_MAP_REGEXP.test(code))
24+
result.code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')
25+
26+
result.code = `${code}\n\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')}\n`
27+
28+
return result
29+
}
30+
31+
export function extractSourceMap(code: string): RawSourceMap | null {
32+
const mapString = code.match(VITE_NODE_SOURCEMAPPING_REGEXP)?.[1]
33+
if (mapString)
34+
return JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
35+
return null
36+
}
37+
838
export function installSourcemapsSupport(options: InstallSourceMapSupportOptions) {
939
install({
1040
environment: 'node',

‎packages/vite-node/src/utils.ts

-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { fileURLToPath, pathToFileURL } from 'url'
22
import { existsSync } from 'fs'
33
import { relative, resolve } from 'pathe'
4-
import type { TransformResult } from 'vite'
54
import { isNodeBuiltin } from 'mlly'
65
import type { Arrayable, Nullable } from './types'
76

@@ -88,20 +87,6 @@ export function toFilePath(id: string, root: string): string {
8887
: absolute
8988
}
9089

91-
let SOURCEMAPPING_URL = 'sourceMa'
92-
SOURCEMAPPING_URL += 'ppingURL'
93-
94-
export async function withInlineSourcemap(result: TransformResult) {
95-
const { code, map } = result
96-
97-
if (code.includes(`${SOURCEMAPPING_URL}=`))
98-
return result
99-
if (map)
100-
result.code = `${code}\n\n//# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')}\n`
101-
102-
return result
103-
}
104-
10590
/**
10691
* Convert `Arrayable<T>` to `Array<T>`
10792
*

‎packages/vitest/src/node/plugins/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
9090
open = '/'
9191

9292
const config: ViteConfig = {
93+
esbuild: {
94+
sourcemap: 'external',
95+
},
9396
resolve: {
9497
// by default Vite resolves `module` field, which not always a native ESM module
9598
// setting this option can bypass that and fallback to cjs version

‎packages/vitest/src/utils/source-map.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export const lineSplitRE = /\r?\n/
66
const stackIgnorePatterns = [
77
'node:internal',
88
'/vitest/dist/',
9+
'/vite-node/dist',
10+
'/vite-node/src',
911
'/vitest/src/',
1012
'/node_modules/chai/',
1113
'/node_modules/tinypool/',

‎test/stacktraces/vite.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { defineConfig } from 'vite'
22

33
export default defineConfig({
4+
esbuild: {
5+
sourcemap: 'both',
6+
},
47
test: {
58
include: ['test/*.test.ts'],
69
},

0 commit comments

Comments
 (0)
Please sign in to comment.