Skip to content

Commit

Permalink
feat: add resolveId to vite-node
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 17, 2022
1 parent e475d2d commit 5894122
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TransformResult, ViteDevServer } from 'vite'
import { shouldExternalize } from './externalize'
import type { ViteNodeServerOptions } from './types'
import type { ViteNodeResolveId, ViteNodeServerOptions } from './types'
import { toFilePath } from './utils'

export * from './externalize'
Expand Down Expand Up @@ -28,6 +28,10 @@ export class ViteNodeServer {
return { code: r?.code }
}

async resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null> {
return this.server.pluginContainer.resolveId(id, importer, { ssr: true })
}

async transformRequest(id: string) {
// reuse transform for concurrent requests
if (!this.promiseMap.has(id)) {
Expand Down
10 changes: 10 additions & 0 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface ExternalizeOptions {
}

export type FetchFunction = (id: string) => Promise<{ code?: string; externalize?: string }>
export type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>

export interface ModuleCache {
promise?: Promise<any>
Expand All @@ -14,13 +15,22 @@ export interface ModuleCache {

export interface ViteNodeRunnerOptions {
fetchModule: FetchFunction
resolveId: ResolveIdFunction
root: string
base?: string
moduleCache?: Map<string, ModuleCache>
interpretDefault?: boolean
requestStubs?: Record<string, any>
}

export interface ViteNodeResolveId {
external?: boolean | 'absolute' | 'relative'
id: string
meta?: Record<string, any> | null
moduleSideEffects?: boolean | 'no-treeshake' | null
syntheticNamedExports?: boolean | string | null
}

export interface ViteNodeServerOptions {
deps?: ExternalizeOptions
transformMode?: {
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ function createChannel(ctx: Vitest) {
fetch(id) {
return ctx.vitenode.fetchModule(id)
},
resolveId(id, importer) {
return ctx.vitenode.resolveId(id, importer)
},
onCollected(files) {
ctx.state.collectFiles(files)
ctx.report('onCollected', files)
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ async function startViteNode(ctx: WorkerContext) {
fetchModule(id) {
return rpc().fetch(id)
},
resolveId(id, importer) {
return rpc().resolveId(id, importer)
},
moduleCache,
mockMap,
interpretDefault: config.deps.interpretDefault ?? true,
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/types/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MessagePort } from 'worker_threads'
import type { RawSourceMap } from 'source-map-js'
import type { ViteNodeResolveId } from 'vite-node'
import type { ResolvedConfig } from './config'
import type { File, TaskResultPack } from './tasks'
import type { SnapshotResult } from './snapshot'
Expand All @@ -13,9 +14,11 @@ export interface WorkerContext {
}

export type FetchFunction = (id: string) => Promise<{ code?: string; externalize?: string }>
export type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>

export interface WorkerRPC {
fetch: FetchFunction
resolveId: ResolveIdFunction
getSourceMap: (id: string, force?: boolean) => Promise<RawSourceMap | undefined>

onWorkerExit: (code?: number) => void
Expand Down

0 comments on commit 5894122

Please sign in to comment.