Skip to content

Commit 6b796ce

Browse files
committed
chore: fix import from chunk
1 parent 2fc2e33 commit 6b796ce

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

packages/vite/src/node/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,10 @@ export const wildcardHosts = new Set([
151151
export const DEFAULT_DEV_PORT = 5173
152152

153153
export const DEFAULT_PREVIEW_PORT = 4173
154+
155+
export let SOURCEMAPPING_URL = 'sourceMa'
156+
SOURCEMAPPING_URL += 'ppingURL'
157+
158+
export const VITE_SOURCEMAPPING_SOURCE =
159+
'//# sourceMappingSource=vite-runtime-client'
160+
export const VITE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`

packages/vite/src/node/ssr/runtimeClient.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// runtime client should be lightweight so we import only the bare minimum
2+
// for this we also have a separate utils file for shared utils
3+
14
import { fileURLToPath, pathToFileURL } from 'node:url'
25
import { createRequire } from 'node:module'
36
import { dirname, resolve } from 'node:path'
@@ -8,6 +11,7 @@ import {
811
CLIENT_PUBLIC_PATH,
912
ENV_PUBLIC_PATH,
1013
VALID_ID_PREFIX,
14+
VITE_SOURCEMAPPING_URL,
1115
} from '../constants'
1216
import {
1317
cleanUrl,
@@ -18,7 +22,17 @@ import {
1822
toFilePath,
1923
} from '../utils/shared'
2024
import type { FetchModuleResult } from './ssrFetchModule'
21-
import { extractSourceMap } from './ssrSourceMap'
25+
26+
const VITE_SOURCEMAPPING_REGEXP = new RegExp(
27+
`//# ${VITE_SOURCEMAPPING_URL};base64,(.+)`,
28+
)
29+
30+
export function extractSourceMap(code: string): SourceMap | null {
31+
const mapString = code.match(VITE_SOURCEMAPPING_REGEXP)?.[1]
32+
if (mapString)
33+
return JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
34+
return null
35+
}
2236

2337
// const debugExecute = createDebug('vite-runtime:client:execute')
2438
// const debugNative = createDebug('vite-runtime:client:native')
@@ -562,7 +576,12 @@ function exportAll(exports: any, sourceModule: any) {
562576

563577
// we don't want to have "string" to be made into {0:"s",1:"t",2:"r",3:"i",4:"n",5:"g"}
564578
// the same goes for arrays: [1,2,3] -> {0:1,1:2,2:3}
565-
if (isPrimitive(sourceModule) || Array.isArray(sourceModule)) return
579+
if (
580+
isPrimitive(sourceModule) ||
581+
Array.isArray(sourceModule) ||
582+
sourceModule instanceof Promise
583+
)
584+
return
566585

567586
for (const key in sourceModule) {
568587
if (key !== 'default') {

packages/vite/src/node/ssr/ssrSourceMap.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
2-
import type { SourceMap } from 'rollup'
2+
import {
3+
SOURCEMAPPING_URL,
4+
VITE_SOURCEMAPPING_SOURCE,
5+
VITE_SOURCEMAPPING_URL,
6+
} from '../constants'
37
import { installSourceMapHandler } from './sourceMapHandler'
48
import type { FetchModuleResult } from './ssrFetchModule'
59

610
interface InstallSourceMapSupportOptions {
711
getSourceMap: (source: string) => EncodedSourceMap | null | undefined
812
}
913

10-
let SOURCEMAPPING_URL = 'sourceMa'
11-
SOURCEMAPPING_URL += 'ppingURL'
12-
13-
const VITE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime-client'
14-
const VITE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
15-
const VITE_SOURCEMAPPING_REGEXP = new RegExp(
16-
`//# ${VITE_SOURCEMAPPING_URL};base64,(.+)`,
17-
)
18-
1914
export function withInlineSourcemap(
2015
result: FetchModuleResult,
2116
): FetchModuleResult {
@@ -38,13 +33,6 @@ export function withInlineSourcemap(
3833
return result
3934
}
4035

41-
export function extractSourceMap(code: string): SourceMap | null {
42-
const mapString = code.match(VITE_SOURCEMAPPING_REGEXP)?.[1]
43-
if (mapString)
44-
return JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
45-
return null
46-
}
47-
4836
let sourceMapHanlderInstalled = true
4937

5038
export function installSourcemapsSupport(

0 commit comments

Comments
 (0)