From e88660fd9f009b5e2953a44c128f6b2095caec99 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sun, 19 Mar 2023 00:36:48 +0100 Subject: [PATCH 1/2] chore: fix typescript performance --- packages/vitest/src/node/core.ts | 4 ++-- packages/vitest/src/node/plugins/index.ts | 11 ++++++----- packages/vitest/src/types/general.ts | 15 --------------- packages/vitest/src/utils/base.ts | 6 +++--- packages/web-worker/src/utils.ts | 2 +- 5 files changed, 12 insertions(+), 26 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 76ee4a1d8cdd..dd1918218d14 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -133,7 +133,7 @@ export class Vitest { } getSerializableConfig() { - return deepMerge({ + return deepMerge({ ...this.config, reporters: [], deps: { @@ -154,7 +154,7 @@ export class Vitest { benchmark: { ...this.config.benchmark, reporters: [], - } as ResolvedConfig['benchmark'], + }, }, this.configOverride || {} as any, ) as ResolvedConfig diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index d21ec1d52061..083af605840d 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -38,7 +38,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t options() { this.meta.watchMode = false }, - async config(viteConfig: any) { + async config(viteConfig) { if (options.watch) { // Earlier runs have overwritten values of the `options`. // Reset it back to initial user config before setting up the server again. @@ -49,7 +49,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t // however to allow vitest plugins to modify vitest config values // this is repeated in configResolved where the config is final const preOptions = deepMerge( - {}, + {} as UserConfig, configDefaults, options, removeUndefinedValues(viteConfig.test ?? {}), @@ -130,7 +130,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t }, } - const classNameStrategy = preOptions.css && preOptions.css?.modules?.classNameStrategy + const classNameStrategy = (typeof preOptions.css !== 'boolean' && preOptions.css?.modules?.classNameStrategy) || 'stable' if (classNameStrategy !== 'scoped') { config.css ??= {} @@ -164,12 +164,13 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t ) entries.push(...setupFiles) } - optimizeConfig.cacheDir = preOptions.cache?.dir ?? 'node_modules/.vitest' + const cacheDir = preOptions.cache !== false ? preOptions.cache?.dir : null + optimizeConfig.cacheDir = cacheDir ?? 'node_modules/.vitest' optimizeConfig.optimizeDeps = { ...viteConfig.optimizeDeps, ...optimizer, disabled: false, - entries: [...(optimizer.entries || viteConfig.optimizeDeps?.entries || []), ...entries], + entries: [...(viteConfig.optimizeDeps?.entries || []), ...entries], exclude: ['vitest', ...builtinModules, ...(optimizer.exclude || viteConfig.optimizeDeps?.exclude || [])], include: (optimizer.include || viteConfig.optimizeDeps?.include || []).filter((n: string) => n !== 'vitest'), } diff --git a/packages/vitest/src/types/general.ts b/packages/vitest/src/types/general.ts index d2eb8c8cc851..e71d3c3ee3a5 100644 --- a/packages/vitest/src/types/general.ts +++ b/packages/vitest/src/types/general.ts @@ -5,21 +5,6 @@ export type Nullable = T | null | undefined export type Arrayable = T | Array export type ArgumentsType = T extends (...args: infer U) => any ? U : never -export type MergeInsertions = - T extends object - ? { [K in keyof T]: MergeInsertions } - : T - -export type DeepMerge = MergeInsertions<{ - [K in keyof F | keyof S]: K extends keyof S & keyof F - ? DeepMerge - : K extends keyof S - ? S[K] - : K extends keyof F - ? F[K] - : never; -}> - export type MutableArray = { -readonly [k in keyof T]: T[k] } export interface Constructable { diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index 9bbe16341b9b..07f6518d79bc 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -1,4 +1,4 @@ -import type { Arrayable, DeepMerge, Nullable, ResolvedConfig, VitestEnvironment } from '../types' +import type { Arrayable, Nullable, ResolvedConfig, VitestEnvironment } from '../types' function isFinalObj(obj: any) { return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype @@ -89,7 +89,7 @@ export function isObject(item: unknown): boolean { * * Will merge objects only if they are plain */ -export function deepMerge(target: T, ...sources: S[]): DeepMerge { +export function deepMerge(target: T, ...sources: any[]): T { if (!sources.length) return target as any @@ -98,7 +98,7 @@ export function deepMerge(targe return target as any if (isMergeableObject(target) && isMergeableObject(source)) { - (Object.keys(source) as (keyof S & keyof T)[]).forEach((key) => { + (Object.keys(source) as (keyof T)[]).forEach((key) => { if (isMergeableObject(source[key])) { if (!target[key]) target[key] = {} as any diff --git a/packages/web-worker/src/utils.ts b/packages/web-worker/src/utils.ts index 9104707e9651..75da02b35469 100644 --- a/packages/web-worker/src/utils.ts +++ b/packages/web-worker/src/utils.ts @@ -61,7 +61,7 @@ export function createMessageEvent(data: any, transferOrOptions: StructuredSeria } } -export function getRunnerOptions() { +export function getRunnerOptions(): any { const { config, ctx, rpc, mockMap, moduleCache } = getWorkerState() return { From 4376cf3794ea368341c061b53af5677bbfacc046 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sun, 19 Mar 2023 00:43:19 +0100 Subject: [PATCH 2/2] chore: cleanup --- packages/vitest/src/utils/base.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index 07f6518d79bc..06b02509d11b 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -88,6 +88,8 @@ export function isObject(item: unknown): boolean { * Deep merge :P * * Will merge objects only if they are plain + * + * Do not merge types - it is very expensive and usually it's better to case a type here */ export function deepMerge(target: T, ...sources: any[]): T { if (!sources.length)