Skip to content

Commit

Permalink
chore: replace installViteNodeSourcamp with getSourceMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 1, 2022
1 parent 8f85da5 commit 6901f46
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 44 deletions.
11 changes: 5 additions & 6 deletions packages/vite-node/README.md
Expand Up @@ -56,7 +56,6 @@ In Vite Node, the server and runner (client) are separated, so you can integrate
import { createServer } from 'vite'
import { ViteNodeServer } from 'vite-node/server'
import { ViteNodeRunner } from 'vite-node/client'
import { installViteNodeSourcemaps } from 'vite-node/source-map'

// create vite server
const server = await createServer({
Expand All @@ -71,11 +70,6 @@ await server.pluginContainer.buildStart({})
// create vite-node server
const node = new ViteNodeServer(server)

// fixes stacktrace in Errors and console.trace calls
installViteNodeSourcemaps({
getSourceMap: source => node.getSourceMap(source)
})

// create vite-node runner
const runner = new ViteNodeRunner({
root: server.config.root,
Expand All @@ -86,6 +80,11 @@ const runner = new ViteNodeRunner({
fetchModule(id) {
return node.fetchModule(id)
},
// fixes stacktrace in Errors and console.trace calls
// has to be syncronouse
getSourceMap(source) {
return node.getSourceMap(source)
},
resolveId(id, importer) {
return node.resolveId(id, importer)
},
Expand Down
5 changes: 0 additions & 5 deletions packages/vite-node/package.json
Expand Up @@ -39,11 +39,6 @@
"types": "./dist/hmr.d.ts",
"require": "./dist/hmr.cjs",
"import": "./dist/hmr.mjs"
},
"./source-map": {
"types": "./dist/source-map.d.ts",
"require": "./dist/source-map.cjs",
"import": "./dist/source-map.mjs"
}
},
"main": "./dist/index.mjs",
Expand Down
15 changes: 7 additions & 8 deletions packages/vite-node/rollup.config.js
Expand Up @@ -9,14 +9,13 @@ import { defineConfig } from 'rollup'
import pkg from './package.json'

const entries = {
'index': 'src/index.ts',
'server': 'src/server.ts',
'types': 'src/types.ts',
'client': 'src/client.ts',
'utils': 'src/utils.ts',
'cli': 'src/cli.ts',
'hmr': 'src/hmr/index.ts',
'source-map': 'src/source-map.ts',
index: 'src/index.ts',
server: 'src/server.ts',
types: 'src/types.ts',
client: 'src/client.ts',
utils: 'src/utils.ts',
cli: 'src/cli.ts',
hmr: 'src/hmr/index.ts',
}

const external = [
Expand Down
8 changes: 3 additions & 5 deletions packages/vite-node/src/cli.ts
Expand Up @@ -7,7 +7,6 @@ import { ViteNodeRunner } from './client'
import type { ViteNodeServerOptions } from './types'
import { toArray } from './utils'
import { createHotContext, handleMessage, viteNodeHmrPlugin } from './hmr'
import { installViteNodeSourcemaps } from './source-map'

const cli = cac('vite-node')

Expand Down Expand Up @@ -59,10 +58,6 @@ async function run(files: string[], options: CliOptions = {}) {

const node = new ViteNodeServer(server, serverOptions)

installViteNodeSourcemaps({
getSourceMap: source => node.getSourceMap(source),
})

const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
Expand All @@ -72,6 +67,9 @@ async function run(files: string[], options: CliOptions = {}) {
resolveId(id, importer) {
return node.resolveId(id, importer)
},
getSourceMap(source) {
return node.getSourceMap(source)
},
createHotContext(runner, url) {
return createHotContext(runner, server.emitter, files, url)
},
Expand Down
7 changes: 7 additions & 0 deletions packages/vite-node/src/client.ts
Expand Up @@ -6,6 +6,7 @@ import { isNodeBuiltin } from 'mlly'
import createDebug from 'debug'
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
import { installSourcemapsSupport } from './source-map'

const debugExecute = createDebug('vite-node:client:execute')
const debugNative = createDebug('vite-node:client:native')
Expand Down Expand Up @@ -126,6 +127,12 @@ export class ViteNodeRunner {
this.root = options.root ?? process.cwd()
this.moduleCache = options.moduleCache ?? new ModuleCacheMap()
this.debug = options.debug ?? (typeof process !== 'undefined' ? !!process.env.VITE_NODE_DEBUG_RUNNER : false)

if (options.getSourceMap) {
installSourcemapsSupport({
getSourceMap: options.getSourceMap,
})
}
}

async executeFile(file: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-node/src/source-map.ts
Expand Up @@ -5,7 +5,7 @@ interface InstallSourceMapSupportOptions {
getSourceMap: (source: string) => RawSourceMap | null | undefined
}

export function installViteNodeSourcemaps(options: InstallSourceMapSupportOptions) {
export function installSourcemapsSupport(options: InstallSourceMapSupportOptions) {
install({
environment: 'node',
handleUncaughtExceptions: false,
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Expand Up @@ -56,6 +56,7 @@ export interface ViteNodeRunnerOptions {
root: string
fetchModule: FetchFunction
resolveId?: ResolveIdFunction
getSourceMap?: (id: string) => RawSourceMap | null | undefined
createHotContext?: CreateHotContextFunction
base?: string
moduleCache?: ModuleCacheMap
Expand Down
19 changes: 0 additions & 19 deletions packages/vitest/src/runtime/setup.ts
@@ -1,4 +1,3 @@
import { installViteNodeSourcemaps } from 'vite-node/source-map'
import { environments } from '../integrations/env'
import type { Environment, ResolvedConfig } from '../types'
import { clearTimeout, getWorkerState, isNode, setTimeout, toArray } from '../utils'
Expand All @@ -23,24 +22,6 @@ export async function setupGlobalEnv(config: ResolvedConfig) {
if (globalSetup)
return

const state = getWorkerState()

installViteNodeSourcemaps({
getSourceMap(source) {
const fsPath = state.moduleCache.normalizePath(source)
const cache = state.moduleCache.get(fsPath)
if (cache.map)
return cache.map
const mapString = cache?.code?.match(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/)?.[1]
if (mapString) {
const map = JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
cache.map = map
return map
}
return null
},
})

globalSetup = true

if (isNode)
Expand Down
13 changes: 13 additions & 0 deletions packages/vitest/src/runtime/worker.ts
Expand Up @@ -48,6 +48,19 @@ async function startViteNode(ctx: WorkerContext) {
resolveId(id, importer) {
return rpc().resolveId(id, importer)
},
getSourceMap(source) {
const fsPath = moduleCache.normalizePath(source)
const cache = moduleCache.get(fsPath)
if (cache.map)
return cache.map
const mapString = cache?.code?.match(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/)?.[1]
if (mapString) {
const map = JSON.parse(Buffer.from(mapString, 'base64').toString('utf-8'))
cache.map = map
return map
}
return null
},
moduleCache,
mockMap,
interopDefault: config.deps.interopDefault ?? true,
Expand Down

0 comments on commit 6901f46

Please sign in to comment.