Skip to content

Commit

Permalink
refactor: use "fixStackTrace" option in vitenode-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 1, 2022
1 parent 6901f46 commit 04b3a59
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
6 changes: 1 addition & 5 deletions packages/vite-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,13 @@ const node = new ViteNodeServer(server)
const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fixStackTrace: true,
// when having the server and runner in a different context,
// you will need to handle the communication between them
// and pass to this function
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
4 changes: 1 addition & 3 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ async function run(files: string[], options: CliOptions = {}) {
const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fixStackTrace: true,
fetchModule(id) {
return node.fetchModule(id)
},
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
19 changes: 17 additions & 2 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ 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)
this.options.fixStackTrace ??= true

if (options.getSourceMap) {
if (this.options.fixStackTrace) {
installSourcemapsSupport({
getSourceMap: options.getSourceMap,
getSourceMap: this.options.getSourceMap ?? (id => this.getSourceMap(id)),
})
}
}
Expand All @@ -143,6 +144,20 @@ export class ViteNodeRunner {
return await this.cachedRequest(id, [])
}

getSourceMap(id: string) {
const fsPath = this.moduleCache.normalizePath(id)
const cache = this.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
}

/** @internal */
async cachedRequest(rawId: string, callstack: string[]) {
const id = normalizeRequestId(rawId, this.options.base)
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface ViteNodeRunnerOptions {
root: string
fetchModule: FetchFunction
resolveId?: ResolveIdFunction
fixStackTrace?: boolean
getSourceMap?: (id: string) => RawSourceMap | null | undefined
createHotContext?: CreateHotContextFunction
base?: string
Expand Down
14 changes: 1 addition & 13 deletions packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,9 @@ 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,
fixStackTrace: true,
interopDefault: config.deps.interopDefault ?? true,
root: config.root,
base: config.base,
Expand Down

0 comments on commit 04b3a59

Please sign in to comment.