Skip to content

Commit

Permalink
fix(browser): specify entries for correct deps optimization (#5839)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 4, 2024
1 parent 23d580a commit c79b3f1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
17 changes: 16 additions & 1 deletion packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import sirv from 'sirv'
import type { ViteDevServer } from 'vite'
import type { ResolvedConfig } from 'vitest'
import type { BrowserScript, WorkspaceProject } from 'vitest/node'
import { getFilePoolName, distDir as vitestDist } from 'vitest/node'
import { type Plugin, coverageConfigDefaults } from 'vitest/config'
import { slash } from '@vitest/utils'
import { slash, toArray } from '@vitest/utils'
import BrowserContext from './plugins/pluginContext'
import BrowserMocker from './plugins/pluginMocker'
import DynamicImport from './plugins/pluginDynamicImport'
Expand Down Expand Up @@ -155,8 +156,22 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
name: 'vitest:browser:tests',
enforce: 'pre',
async config() {
const allTestFiles = await project.globTestFiles()
const browserTestFiles = allTestFiles.filter(file => getFilePoolName(project, file) === 'browser')
const setupFiles = toArray(project.config.setupFiles)
const vitestPaths = [
resolve(vitestDist, 'index.js'),
resolve(vitestDist, 'browser.js'),
resolve(vitestDist, 'runners.js'),
resolve(vitestDist, 'utils.js'),
]
return {
optimizeDeps: {
entries: [
...browserTestFiles,
...setupFiles,
...vitestPaths,
],
exclude: [
'vitest',
'vitest/utils',
Expand Down
5 changes: 1 addition & 4 deletions packages/vitest/src/integrations/browser/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function createBrowserServer(project: WorkspaceProject, configFile:
server: {
hmr: false,
watch: null,
preTransformRequests: false,
},
plugins: [
...project.options?.plugins || [],
Expand Down Expand Up @@ -58,10 +59,6 @@ export async function createBrowserServer(project: WorkspaceProject, configFile:
resolve: {
alias: config.test?.alias,
},
server: {
watch: null,
preTransformRequests: false,
},
}
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export { registerConsoleShortcuts } from './stdin'
export type { GlobalSetupContext } from './globalSetup'
export type { WorkspaceSpec, ProcessPool } from './pool'
export { createMethodsRPC } from './pools/rpc'
export { getFilePoolName } from './pool'
export { VitestPackageInstaller } from './packageInstaller'

export { distDir, rootDir } from '../paths'

export type { TestSequencer, TestSequencerConstructor } from './sequencers/types'
export { BaseSequencer } from './sequencers/BaseSequencer'

Expand Down
46 changes: 23 additions & 23 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ export interface PoolProcessOptions {

export const builtinPools: BuiltinPool[] = ['forks', 'threads', 'browser', 'vmThreads', 'vmForks', 'typescript']

function getDefaultPoolName(project: WorkspaceProject, file: string): Pool {
if (project.config.typecheck.enabled) {
for (const glob of project.config.typecheck.include) {
if (mm.isMatch(file, glob, { cwd: project.config.root }))
return 'typescript'
}
}
if (project.config.browser.enabled)
return 'browser'
return project.config.pool
}

export function getFilePoolName(project: WorkspaceProject, file: string) {
for (const [glob, pool] of project.config.poolMatchGlobs) {
if ((pool as Pool) === 'browser')
throw new Error('Since Vitest 0.31.0 "browser" pool is not supported in "poolMatchGlobs". You can create a workspace to run some of your tests in browser in parallel. Read more: https://vitest.dev/guide/workspace')
if (mm.isMatch(file, glob, { cwd: project.config.root }))
return pool as Pool
}
return getDefaultPoolName(project, file)
}

export function createPool(ctx: Vitest): ProcessPool {
const pools: Record<Pool, ProcessPool | null> = {
forks: null,
Expand All @@ -37,28 +59,6 @@ export function createPool(ctx: Vitest): ProcessPool {
typescript: null,
}

function getDefaultPoolName(project: WorkspaceProject, file: string): Pool {
if (project.config.typecheck.enabled) {
for (const glob of project.config.typecheck.include) {
if (mm.isMatch(file, glob, { cwd: project.config.root }))
return 'typescript'
}
}
if (project.config.browser.enabled)
return 'browser'
return project.config.pool
}

function getPoolName([project, file]: WorkspaceSpec) {
for (const [glob, pool] of project.config.poolMatchGlobs) {
if ((pool as Pool) === 'browser')
throw new Error('Since Vitest 0.31.0 "browser" pool is not supported in "poolMatchGlobs". You can create a workspace to run some of your tests in browser in parallel. Read more: https://vitest.dev/guide/workspace')
if (mm.isMatch(file, glob, { cwd: project.config.root }))
return pool as Pool
}
return getDefaultPoolName(project, file)
}

// in addition to resolve.conditions Vite also adds production/development,
// see: https://github.com/vitejs/vite/blob/af2aa09575229462635b7cbb6d248ca853057ba2/packages/vite/src/node/plugins/resolve.ts#L1056-L1080
const potentialConditions = new Set(['production', 'development', ...ctx.server.config.resolve.conditions])
Expand Down Expand Up @@ -137,7 +137,7 @@ export function createPool(ctx: Vitest): ProcessPool {
}

for (const spec of files) {
const pool = getPoolName(spec)
const pool = getFilePoolName(spec[0], spec[1])
filesByPool[pool] ??= []
filesByPool[pool].push(spec)
}
Expand Down

0 comments on commit c79b3f1

Please sign in to comment.