Skip to content

Commit

Permalink
fix: don't fail if inline: true is set (#4815)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Dec 28, 2023
1 parent ee8b46d commit 8f6225b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ export class ViteNodeRunner {
enumerable: false,
configurable: false,
})
const SYMBOL_NOT_DEFINED = Symbol('not defined')
let moduleExports: unknown = SYMBOL_NOT_DEFINED
// this proxy is triggered only on exports.{name} and module.exports access
// inside the module itself. imported module is always "exports"
const cjsExports = new Proxy(exports, {
Expand All @@ -326,12 +328,14 @@ export class ViteNodeRunner {

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

exports.default[p] = value
if (!isPrimitive(exports.default))
exports.default[p] = value

if (p !== 'default')
defineExport(exports, p, () => value)

Expand All @@ -345,6 +349,7 @@ export class ViteNodeRunner {
set exports(value) {
exportAll(cjsExports, value)
exports.default = value
moduleExports = value
},
get exports() {
return cjsExports
Expand Down
18 changes: 15 additions & 3 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pathToFileURL } from 'node:url'
import vm from 'node:vm'
import { DEFAULT_REQUEST_STUBS, ModuleCacheMap, ViteNodeRunner } from 'vite-node/client'
import { isInternalRequest, isNodeBuiltin, isPrimitive } from 'vite-node/utils'
import { isInternalRequest, isNodeBuiltin, isPrimitive, toFilePath } from 'vite-node/utils'
import type { ViteNodeRunnerOptions } from 'vite-node'
import { normalize, relative, resolve } from 'pathe'
import { processError } from '@vitest/utils/error'
Expand Down Expand Up @@ -41,6 +41,7 @@ export const packageCache = new Map<string, any>()
export const moduleCache = new ModuleCacheMap()
export const mockMap: MockMap = new Map()
export const fileMap = new FileMap()
const externalizeMap = new Map<string, string>()

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

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

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

return await createVitestExecutor({
fetchModule(id) {
async fetchModule(id) {
if (externalizeMap.has(id))
return { externalize: externalizeMap.get(id)! }
// always externalize Vitest because we import from there before running tests
// so we already have it cached by Node.js
if (id.includes(distDir)) {
const { path } = toFilePath(id, state().config.root)
const externalize = pathToFileURL(path).toString()
externalizeMap.set(id, externalize)
return { externalize }
}

return rpc().fetch(id, getTransformMode())
},
resolveId(id, importer) {
Expand Down

0 comments on commit 8f6225b

Please sign in to comment.