From e49c13fa2d092b76503c73ec8f271eef4eb83c40 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 27 Jan 2023 12:19:56 +0100 Subject: [PATCH] perf: don't import vite in worker (#2759) --- packages/vitest/src/runtime/execute.ts | 6 +++--- packages/vitest/src/runtime/mocker.ts | 5 +++-- packages/vitest/src/runtime/worker.ts | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/runtime/execute.ts b/packages/vitest/src/runtime/execute.ts index de2cf8f8e8c8..c300ccf14d54 100644 --- a/packages/vitest/src/runtime/execute.ts +++ b/packages/vitest/src/runtime/execute.ts @@ -1,10 +1,10 @@ import { ViteNodeRunner } from 'vite-node/client' import { isInternalRequest } from 'vite-node/utils' import type { ViteNodeRunnerOptions } from 'vite-node' -import { normalizePath } from 'vite' +import { normalize } from 'pathe' import { isNodeBuiltin } from 'mlly' import type { MockMap } from '../types/mocker' -import { getCurrentEnvironment, getWorkerState } from '../utils' +import { getCurrentEnvironment, getWorkerState } from '../utils/global' import { VitestMocker } from './mocker' export interface ExecuteOptions extends ViteNodeRunnerOptions { @@ -68,7 +68,7 @@ export class VitestRunner extends ViteNodeRunner { const workerState = getWorkerState() // support `import.meta.vitest` for test entry - if (workerState.filepath && normalizePath(workerState.filepath) === normalizePath(context.__filename)) { + if (workerState.filepath && normalize(workerState.filepath) === normalize(context.__filename)) { // @ts-expect-error injected untyped global Object.defineProperty(context.__vite_ssr_import_meta__, 'vitest', { get: () => globalThis.__vitest_index__ }) } diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index 747707242b35..6c6cc60a46b3 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -1,8 +1,9 @@ import { existsSync, readdirSync } from 'node:fs' import { isNodeBuiltin } from 'mlly' import { basename, dirname, extname, isAbsolute, join, resolve } from 'pathe' -import { getColors } from '@vitest/utils' -import { getAllMockableProperties, getType, getWorkerState } from '../utils' +import { getColors, getType } from '@vitest/utils' +import { getWorkerState } from '../utils/global' +import { getAllMockableProperties } from '../utils/base' import { distDir } from '../constants' import type { MockFactory, PendingSuiteMock } from '../types/mocker' import type { VitestRunner } from './execute' diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index c21c2b5545c8..8590fa55e26d 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -6,7 +6,7 @@ import { ModuleCacheMap } from 'vite-node/client' import { isPrimitive } from 'vite-node/utils' import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types' import { distDir } from '../constants' -import { getWorkerState } from '../utils' +import { getWorkerState } from '../utils/global' import type { MockMap } from '../types/mocker' import { executeInViteNode } from './execute' import { rpc } from './rpc'