Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix typescript performance #3030

Merged
merged 2 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Vitest {
}

getSerializableConfig() {
return deepMerge<ResolvedConfig>({
return deepMerge({
...this.config,
reporters: [],
deps: {
Expand All @@ -154,7 +154,7 @@ export class Vitest {
benchmark: {
...this.config.benchmark,
reporters: [],
} as ResolvedConfig['benchmark'],
},
},
this.configOverride || {} as any,
) as ResolvedConfig
Expand Down
11 changes: 6 additions & 5 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ?? {}),
Expand Down Expand Up @@ -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 ??= {}
Expand Down Expand Up @@ -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'),
}
Expand Down
15 changes: 0 additions & 15 deletions packages/vitest/src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ export type Nullable<T> = T | null | undefined
export type Arrayable<T> = T | Array<T>
export type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never

export type MergeInsertions<T> =
T extends object
? { [K in keyof T]: MergeInsertions<T[K]> }
: T

export type DeepMerge<F, S> = MergeInsertions<{
[K in keyof F | keyof S]: K extends keyof S & keyof F
? DeepMerge<F[K], S[K]>
: K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never;
}>

export type MutableArray<T extends readonly any[]> = { -readonly [k in keyof T]: T[k] }

export interface Constructable {
Expand Down
8 changes: 5 additions & 3 deletions packages/vitest/src/utils/base.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -88,8 +88,10 @@ 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<T extends object = object, S extends object = T>(target: T, ...sources: S[]): DeepMerge<T, S> {
export function deepMerge<T extends object = object>(target: T, ...sources: any[]): T {
if (!sources.length)
return target as any

Expand All @@ -98,7 +100,7 @@ export function deepMerge<T extends object = object, S extends object = T>(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
Expand Down
2 changes: 1 addition & 1 deletion packages/web-worker/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down