Skip to content

Commit

Permalink
chore: fix import from chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 3, 2023
1 parent 2fc2e33 commit 6b796ce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
7 changes: 7 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,10 @@ export const wildcardHosts = new Set([
export const DEFAULT_DEV_PORT = 5173

export const DEFAULT_PREVIEW_PORT = 4173

export let SOURCEMAPPING_URL = 'sourceMa'
SOURCEMAPPING_URL += 'ppingURL'

export const VITE_SOURCEMAPPING_SOURCE =
'//# sourceMappingSource=vite-runtime-client'
export const VITE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
23 changes: 21 additions & 2 deletions packages/vite/src/node/ssr/runtimeClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// runtime client should be lightweight so we import only the bare minimum
// for this we also have a separate utils file for shared utils

import { fileURLToPath, pathToFileURL } from 'node:url'
import { createRequire } from 'node:module'
import { dirname, resolve } from 'node:path'
Expand All @@ -8,6 +11,7 @@ import {
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH,
VALID_ID_PREFIX,
VITE_SOURCEMAPPING_URL,
} from '../constants'
import {
cleanUrl,
Expand All @@ -18,7 +22,17 @@ import {
toFilePath,
} from '../utils/shared'
import type { FetchModuleResult } from './ssrFetchModule'
import { extractSourceMap } from './ssrSourceMap'

const VITE_SOURCEMAPPING_REGEXP = new RegExp(
`//# ${VITE_SOURCEMAPPING_URL};base64,(.+)`,
)

export function extractSourceMap(code: string): SourceMap | null {
const mapString = code.match(VITE_SOURCEMAPPING_REGEXP)?.[1]
if (mapString)
return JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
return null
}

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

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

for (const key in sourceModule) {
if (key !== 'default') {
Expand Down
22 changes: 5 additions & 17 deletions packages/vite/src/node/ssr/ssrSourceMap.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
import type { SourceMap } from 'rollup'
import {
SOURCEMAPPING_URL,
VITE_SOURCEMAPPING_SOURCE,
VITE_SOURCEMAPPING_URL,
} from '../constants'
import { installSourceMapHandler } from './sourceMapHandler'
import type { FetchModuleResult } from './ssrFetchModule'

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

let SOURCEMAPPING_URL = 'sourceMa'
SOURCEMAPPING_URL += 'ppingURL'

const VITE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime-client'
const VITE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
const VITE_SOURCEMAPPING_REGEXP = new RegExp(
`//# ${VITE_SOURCEMAPPING_URL};base64,(.+)`,
)

export function withInlineSourcemap(
result: FetchModuleResult,
): FetchModuleResult {
Expand All @@ -38,13 +33,6 @@ export function withInlineSourcemap(
return result
}

export function extractSourceMap(code: string): SourceMap | null {
const mapString = code.match(VITE_SOURCEMAPPING_REGEXP)?.[1]
if (mapString)
return JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
return null
}

let sourceMapHanlderInstalled = true

export function installSourcemapsSupport(
Expand Down

0 comments on commit 6b796ce

Please sign in to comment.