Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 12, 2023
1 parent 3c7aa09 commit 5ba4119
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
12 changes: 8 additions & 4 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ You will not be able to edit your `node_modules` code for debugging, since the c
#### deps.external

- **Type:** `(string | RegExp)[]`
- **Default:** `['**/node_modules/**']`
- **Default:** `[]`

Externalize means that Vite will bypass the package to native Node. Externalized dependencies will not be applied Vite's transformers and resolvers, so they do not support HMR on reload. By default, all packages inside `node_modules` are externalized.

This options support package names as they are written in `node_modules` or specified inside [`deps.moduleDirectories`](#deps-moduledirectories). For example, package `@company/some-name` located inside `packages/some-name` should be specified as `some-name` and `packages` should be included in `deps.moduleDirectories`. Basically, Vitest always checks the file path, not the actual package name.

Externalize means that Vite will bypass the package to native Node. Externalized dependencies will not be applied Vite's transformers and resolvers, so they do not support HMR on reload. Typically, packages under `node_modules` are externalized.
If regexp is used, Vitest calls it on the _file path_, not the package name.

#### deps.inline

Expand Down Expand Up @@ -502,7 +506,7 @@ By providing an object instead of a string you can define individual outputs whe

- **Type:** `boolean`
- **CLI:** `--experimentalVmThreads`, `--experimental-vm-threads`
- **Version:** Since Vitets 0.34.0
- **Version:** Since Vitest 0.34.0

Run tests using [VM context](https://nodejs.org/api/vm.html) (inside a sandboxed environment) in a worker pool.

Expand Down Expand Up @@ -533,7 +537,7 @@ Please, be aware of these issues when using this option. Vitest team cannot fix
- **Type:** `string | number`
- **CLI:** `--experimentalVmWorkerMemoryLimit`, `--experimental-vm-worker-memory-limit`
- **Default:** `1 / CPU Cores`
- **Version:** Since Vitets 0.34.0
- **Version:** Since Vitest 0.34.0

Specifies the memory limit for workers before they are recycled. This value heavily depends on your environment, so it's better to specify it manually instead of relying on default.

Expand Down
3 changes: 0 additions & 3 deletions examples/mocks/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export default defineConfig({
test: {
globals: true,
environment: 'node',
poolMatchGlobs: [
['**/*', 'experimentalVmThreads'],
],
deps: {
external: [/src\/external/],
interopDefault: true,
Expand Down
3 changes: 1 addition & 2 deletions packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { performance } from 'node:perf_hooks'
import { existsSync } from 'node:fs'
import { join, relative, resolve } from 'pathe'
import { type PackageCache, type TransformResult, type ViteDevServer } from 'vite'
import { type TransformResult, type ViteDevServer } from 'vite'
import createDebug from 'debug'
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
import type { DebuggerOptions, FetchResult, ViteNodeResolveId, ViteNodeServerOptions } from './types'
Expand All @@ -27,7 +27,6 @@ export class ViteNodeServer {
}>()

externalizeCache = new Map<string, Promise<string | false>>()
packageCache = new Map<string, PackageCache>()

debugger?: Debugger

Expand Down
3 changes: 0 additions & 3 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Context } from 'node:vm'
import type { ViteHotContext } from 'vite/types/hot'
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
import type { ModuleCacheMap, ViteNodeRunner } from './client'
Expand Down Expand Up @@ -77,11 +76,9 @@ export interface ViteNodeRunnerOptions {
createHotContext?: CreateHotContextFunction
base?: string
moduleCache?: ModuleCacheMap
externalCache?: ModuleCacheMap
interopDefault?: boolean
requestStubs?: Record<string, any>
debug?: boolean
context?: Context
}

export interface ViteNodeResolveId {
Expand Down
12 changes: 5 additions & 7 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
}

function catchError(err: unknown, type: string) {
const worker = options.state
const worker = state()
const error = processError(err)
if (!isPrimitive(error)) {
error.VITEST_TEST_NAME = worker.current?.name
Expand Down Expand Up @@ -176,16 +176,16 @@ export class VitestExecutor extends ViteNodeRunner {
}

get state() {
return this.options.state
return getWorkerState() || this.options.state
}

shouldResolveId(id: string, _importee?: string | undefined): boolean {
if (isInternalRequest(id) || id.startsWith('data:'))
return false
const environment = this.options.state.environment.name
const transformMode = this.state.environment?.transformMode ?? 'ssr'
// do not try and resolve node builtins in Node
// import('url') returns Node internal even if 'url' package is installed
return environment === 'node' ? !isNodeBuiltin(id) : !id.startsWith('node:')
return transformMode === 'ssr' ? !isNodeBuiltin(id) : !id.startsWith('node:')
}

async originalResolveUrl(id: string, importer?: string) {
Expand Down Expand Up @@ -253,10 +253,8 @@ export class VitestExecutor extends ViteNodeRunner {
}

prepareContext(context: Record<string, any>) {
const workerState = this.state

// support `import.meta.vitest` for test entry
if (workerState.filepath && normalize(workerState.filepath) === normalize(context.__filename)) {
if (this.state.filepath && normalize(this.state.filepath) === normalize(context.__filename)) {
const globalNamespace = this.options.context || globalThis
// @ts-expect-error injected untyped global
Object.defineProperty(context.__vite_ssr_import_meta__, 'vitest', { get: () => globalNamespace.__vitest_index__ })
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class VitestMocker {
) {
const context = this.executor.options.context
if (context)
this.primitives = vm.runInContext('({ Object, Symbol, Error, Function, RegExp, Array, Map })', context)
this.primitives = vm.runInContext('({ Object, Error, Function, RegExp, Symbol, Array, Map })', context)
else
this.primitives = { Object, Error, Function, RegExp, Symbol: globalThis.Symbol, Array, Map }

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function run(ctx: WorkerContext) {
getSourceMap: source => moduleCache.getSourceMap(source),
})

const vm = await environment.setupVM!(ctx.environment.options || ctx.config.environmentOptions || {})
const vm = await environment.setupVM(ctx.environment.options || ctx.config.environmentOptions || {})

state.durations.environment = performance.now() - state.durations.environment

Expand Down

0 comments on commit 5ba4119

Please sign in to comment.