mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
refactor(vite-node): sourcemap handling
This commit is contained in:
parent
76a30f0258
commit
b4a546800a
@ -1,7 +1,6 @@
|
||||
import type { TransformResult, ViteDevServer } from 'vite'
|
||||
import type { FetchResult } from '..'
|
||||
import type { FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
|
||||
import { shouldExternalize } from './externalize'
|
||||
import type { ViteNodeResolveId, ViteNodeServerOptions } from './types'
|
||||
import { toFilePath, withInlineSourcemap } from './utils'
|
||||
|
||||
export * from './externalize'
|
||||
@ -9,7 +8,8 @@ export * from './externalize'
|
||||
export class ViteNodeServer {
|
||||
private fetchPromiseMap = new Map<string, Promise<FetchResult>>()
|
||||
private transformPromiseMap = new Map<string, Promise<TransformResult | null | undefined>>()
|
||||
private fetchCache = new Map<string, {
|
||||
|
||||
fetchCache = new Map<string, {
|
||||
timestamp: number
|
||||
result: FetchResult
|
||||
}>()
|
||||
@ -32,6 +32,9 @@ export class ViteNodeServer {
|
||||
if (!this.fetchPromiseMap.has(id)) {
|
||||
this.fetchPromiseMap.set(id,
|
||||
this._fetchModule(id)
|
||||
.then((r) => {
|
||||
return this.options.sourcemap !== true ? { ...r, map: undefined } : r
|
||||
})
|
||||
.finally(() => {
|
||||
this.fetchPromiseMap.delete(id)
|
||||
}),
|
||||
@ -69,7 +72,7 @@ export class ViteNodeServer {
|
||||
private async _fetchModule(id: string): Promise<FetchResult> {
|
||||
let result: FetchResult
|
||||
|
||||
const timestamp = this.server.moduleGraph.getModuleById(id)?.lastHMRTimestamp
|
||||
const timestamp = this.server.moduleGraph.getModuleById(id)?.lastHMRTimestamp || Date.now()
|
||||
const cache = this.fetchCache.get(id)
|
||||
if (timestamp && cache && cache.timestamp >= timestamp)
|
||||
return cache.result
|
||||
@ -80,15 +83,13 @@ export class ViteNodeServer {
|
||||
}
|
||||
else {
|
||||
const r = await this._transformRequest(id)
|
||||
result = { code: r?.code }
|
||||
result = { code: r?.code, map: r?.map as unknown as RawSourceMap }
|
||||
}
|
||||
|
||||
if (timestamp) {
|
||||
this.fetchCache.set(id, {
|
||||
timestamp,
|
||||
result,
|
||||
})
|
||||
}
|
||||
this.fetchCache.set(id, {
|
||||
timestamp,
|
||||
result,
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
@ -107,7 +108,8 @@ export class ViteNodeServer {
|
||||
result = await this.server.transformRequest(id, { ssr: true })
|
||||
}
|
||||
|
||||
if (this.options.sourcemap !== false && result && !id.includes('node_modules'))
|
||||
const sourcemap = this.options.sourcemap ?? 'inline'
|
||||
if (sourcemap === 'inline' && result && !id.includes('node_modules'))
|
||||
withInlineSourcemap(result)
|
||||
|
||||
return result
|
||||
|
||||
@ -8,9 +8,23 @@ export interface DepsHandlingOptions {
|
||||
fallbackCJS?: boolean
|
||||
}
|
||||
|
||||
export interface StartOfSourceMap {
|
||||
file?: string
|
||||
sourceRoot?: string
|
||||
}
|
||||
|
||||
export interface RawSourceMap extends StartOfSourceMap {
|
||||
version: string
|
||||
sources: string[]
|
||||
names: string[]
|
||||
sourcesContent?: string[]
|
||||
mappings: string
|
||||
}
|
||||
|
||||
export interface FetchResult {
|
||||
code?: string
|
||||
externalize?: string
|
||||
map?: RawSourceMap
|
||||
}
|
||||
|
||||
export type FetchFunction = (id: string) => Promise<FetchResult>
|
||||
@ -43,9 +57,9 @@ export interface ViteNodeResolveId {
|
||||
export interface ViteNodeServerOptions {
|
||||
/**
|
||||
* Inject inline sourcemap to modules
|
||||
* @default true
|
||||
* @default 'inline'
|
||||
*/
|
||||
sourcemap?: boolean
|
||||
sourcemap?: 'inline' | boolean
|
||||
/**
|
||||
* Deps handling
|
||||
*/
|
||||
|
||||
@ -61,9 +61,12 @@ export async function reportCoverage(ctx: Vitest) {
|
||||
|
||||
// add source maps
|
||||
Array
|
||||
.from(ctx.visitedFilesMap.entries())
|
||||
.from(ctx.vitenode.fetchCache.entries())
|
||||
.filter(i => !i[0].includes('/node_modules/'))
|
||||
.forEach(([file, map]) => {
|
||||
.forEach(([file, { result }]) => {
|
||||
const map = result.map
|
||||
if (!map)
|
||||
return
|
||||
const url = pathToFileURL(file).href
|
||||
const sources = map.sources.length
|
||||
? map.sources.map(i => pathToFileURL(i).href)
|
||||
|
||||
@ -5,7 +5,7 @@ import fg from 'fast-glob'
|
||||
import mm from 'micromatch'
|
||||
import c from 'picocolors'
|
||||
import { ViteNodeServer } from 'vite-node/server'
|
||||
import type { ArgumentsType, RawSourceMap, Reporter, ResolvedConfig, UserConfig } from '../types'
|
||||
import type { ArgumentsType, Reporter, ResolvedConfig, UserConfig } from '../types'
|
||||
import { SnapshotManager } from '../integrations/snapshot/manager'
|
||||
import { deepMerge, hasFailed, noop, slash, toArray } from '../utils'
|
||||
import { cleanCoverage, reportCoverage } from '../integrations/coverage'
|
||||
@ -37,7 +37,6 @@ export class Vitest {
|
||||
|
||||
invalidates: Set<string> = new Set()
|
||||
changedTests: Set<string> = new Set()
|
||||
visitedFilesMap: Map<string, RawSourceMap> = new Map()
|
||||
runningPromise?: Promise<void>
|
||||
closingPromise?: Promise<void>
|
||||
|
||||
|
||||
@ -4,7 +4,8 @@ import { resolve } from 'pathe'
|
||||
import type { Options as TinypoolOptions } from 'tinypool'
|
||||
import { Tinypool } from 'tinypool'
|
||||
import { createBirpc } from 'birpc'
|
||||
import type { RawSourceMap, WorkerContext, WorkerRPC } from '../types'
|
||||
import type { RawSourceMap } from 'vite-node'
|
||||
import type { WorkerContext, WorkerRPC } from '../types'
|
||||
import { distDir } from '../constants'
|
||||
import type { Vitest } from './core'
|
||||
|
||||
|
||||
@ -74,16 +74,3 @@ export interface ModuleGraphData {
|
||||
externalized: string[]
|
||||
inlined: string[]
|
||||
}
|
||||
|
||||
export interface StartOfSourceMap {
|
||||
file?: string
|
||||
sourceRoot?: string
|
||||
}
|
||||
|
||||
export interface RawSourceMap extends StartOfSourceMap {
|
||||
version: string
|
||||
sources: string[]
|
||||
names: string[]
|
||||
sourcesContent?: string[]
|
||||
mappings: string
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import type { Profiler } from 'inspector'
|
||||
import type { MessagePort } from 'worker_threads'
|
||||
import type { FetchFunction, ViteNodeResolveId } from 'vite-node'
|
||||
import type { RawSourceMap } from '../types'
|
||||
import type { FetchFunction, RawSourceMap, ViteNodeResolveId } from 'vite-node'
|
||||
import type { ResolvedConfig } from './config'
|
||||
import type { File, TaskResultPack } from './tasks'
|
||||
import type { SnapshotResult } from './snapshot'
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { SourceMapConsumer } from 'source-map-js'
|
||||
import type { ErrorWithDiff, ParsedStack, Position, RawSourceMap } from '../types'
|
||||
import type { RawSourceMap } from 'vite-node'
|
||||
import type { ErrorWithDiff, ParsedStack, Position } from '../types'
|
||||
import type { Vitest } from '../node'
|
||||
import { notNullish } from './base'
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user