From 9ef599774c72dc820b1571c4bf751def265bd67d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 19 Jan 2022 04:26:23 +0800 Subject: [PATCH] fix(vite-node): stub `/@vite/client` by default --- packages/vite-node/src/client.ts | 18 ++++++++++++++++-- packages/vitest/src/node/execute.ts | 14 -------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index f1d6ede3a..5268a677c 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -5,6 +5,19 @@ import { dirname, resolve } from 'pathe' import { isPrimitive, normalizeId, slash, toFilePath } from './utils' import type { ModuleCache, ViteNodeRunnerOptions } from './types' +export const DEFAULT_REQUEST_STUBS = { + '/@vite/client': { + injectQuery: (id: string) => id, + createHotContext() { + return { + accept: () => {}, + prune: () => {}, + } + }, + updateStyle() {}, + }, +} + export class ViteNodeRunner { root: string @@ -51,8 +64,9 @@ export class ViteNodeRunner { return this.cachedRequest(dep, callstack) } - if (this.options.requestStubs && id in this.options.requestStubs) - return this.options.requestStubs[id] + const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS + if (id in requestStubs) + return requestStubs[id] const { code: transformed, externalize } = await this.options.fetchModule(id) if (externalize) { diff --git a/packages/vitest/src/node/execute.ts b/packages/vitest/src/node/execute.ts index 6fabc0e7b..1c265a925 100644 --- a/packages/vitest/src/node/execute.ts +++ b/packages/vitest/src/node/execute.ts @@ -24,20 +24,6 @@ export class VitestRunner extends ViteNodeRunner { constructor(public options: ExecuteOptions) { super(options) - - options.requestStubs = options.requestStubs || { - '/@vite/client': { - injectQuery: (id: string) => id, - createHotContext() { - return { - accept: () => {}, - prune: () => {}, - } - }, - updateStyle() {}, - }, - } - this.mocker = createMocker(this.root, options.mockMap) }