Skip to content

Commit 8f6225b

Browse files
authoredDec 28, 2023
fix: don't fail if inline: true is set (#4815)
1 parent ee8b46d commit 8f6225b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed
 

‎packages/vite-node/src/client.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ export class ViteNodeRunner {
303303
enumerable: false,
304304
configurable: false,
305305
})
306+
const SYMBOL_NOT_DEFINED = Symbol('not defined')
307+
let moduleExports: unknown = SYMBOL_NOT_DEFINED
306308
// this proxy is triggered only on exports.{name} and module.exports access
307309
// inside the module itself. imported module is always "exports"
308310
const cjsExports = new Proxy(exports, {
@@ -326,12 +328,14 @@ export class ViteNodeRunner {
326328

327329
// returns undefined, when accessing named exports, if default is not an object
328330
// but is still present inside hasOwnKeys, this is Node behaviour for CJS
329-
if (isPrimitive(exports.default)) {
331+
if (moduleExports !== SYMBOL_NOT_DEFINED && isPrimitive(moduleExports)) {
330332
defineExport(exports, p, () => undefined)
331333
return true
332334
}
333335

334-
exports.default[p] = value
336+
if (!isPrimitive(exports.default))
337+
exports.default[p] = value
338+
335339
if (p !== 'default')
336340
defineExport(exports, p, () => value)
337341

@@ -345,6 +349,7 @@ export class ViteNodeRunner {
345349
set exports(value) {
346350
exportAll(cjsExports, value)
347351
exports.default = value
352+
moduleExports = value
348353
},
349354
get exports() {
350355
return cjsExports

‎packages/vitest/src/runtime/execute.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pathToFileURL } from 'node:url'
22
import vm from 'node:vm'
33
import { DEFAULT_REQUEST_STUBS, ModuleCacheMap, ViteNodeRunner } from 'vite-node/client'
4-
import { isInternalRequest, isNodeBuiltin, isPrimitive } from 'vite-node/utils'
4+
import { isInternalRequest, isNodeBuiltin, isPrimitive, toFilePath } from 'vite-node/utils'
55
import type { ViteNodeRunnerOptions } from 'vite-node'
66
import { normalize, relative, resolve } from 'pathe'
77
import { processError } from '@vitest/utils/error'
@@ -41,6 +41,7 @@ export const packageCache = new Map<string, any>()
4141
export const moduleCache = new ModuleCacheMap()
4242
export const mockMap: MockMap = new Map()
4343
export const fileMap = new FileMap()
44+
const externalizeMap = new Map<string, string>()
4445

4546
export async function startViteNode(options: ContextExecutorOptions) {
4647
if (_viteNode)
@@ -64,7 +65,7 @@ export interface ContextExecutorOptions {
6465

6566
export async function startVitestExecutor(options: ContextExecutorOptions) {
6667
// @ts-expect-error injected untyped global
67-
const state = () => globalThis.__vitest_worker__ || options.state
68+
const state = (): WorkerGlobalState => globalThis.__vitest_worker__ || options.state
6869
const rpc = () => state().rpc
6970

7071
const processExit = process.exit
@@ -97,7 +98,18 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
9798
}
9899

99100
return await createVitestExecutor({
100-
fetchModule(id) {
101+
async fetchModule(id) {
102+
if (externalizeMap.has(id))
103+
return { externalize: externalizeMap.get(id)! }
104+
// always externalize Vitest because we import from there before running tests
105+
// so we already have it cached by Node.js
106+
if (id.includes(distDir)) {
107+
const { path } = toFilePath(id, state().config.root)
108+
const externalize = pathToFileURL(path).toString()
109+
externalizeMap.set(id, externalize)
110+
return { externalize }
111+
}
112+
101113
return rpc().fetch(id, getTransformMode())
102114
},
103115
resolveId(id, importer) {

0 commit comments

Comments
 (0)
Please sign in to comment.