Skip to content

Commit 8d883cf

Browse files
authored
fix(browser): exclude missed packages from optimization, print help message (#6445)
1 parent c321a3f commit 8d883cf

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

packages/browser/src/node/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { WorkspaceProject } from 'vitest/node'
22
import type { Plugin } from 'vitest/config'
3-
import { createServer } from 'vitest/node'
3+
import { createViteLogger, createViteServer } from 'vitest/node'
44
import c from 'tinyrainbow'
55
import { version } from '../../package.json'
66
import { setupBrowserRpc } from './rpc'
@@ -30,10 +30,28 @@ export async function createBrowserServer(
3030

3131
const configPath = typeof configFile === 'string' ? configFile : false
3232

33-
const vite = await createServer({
33+
const logLevel = (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info'
34+
35+
const logger = createViteLogger(logLevel)
36+
37+
const vite = await createViteServer({
3438
...project.options, // spread project config inlined in root workspace config
3539
base: '/',
36-
logLevel: (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info',
40+
logLevel,
41+
customLogger: {
42+
...logger,
43+
info(msg, options) {
44+
logger.info(msg, options)
45+
if (msg.includes('optimized dependencies changed. reloading')) {
46+
logger.warn(
47+
[
48+
c.yellow(`\n${c.bold('[vitest]')} Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.\n`),
49+
c.yellow(`For a stable experience, please add mentioned dependencies to your config\'s ${c.bold('\`optimizeDeps.include\`')} field manually.\n\n`),
50+
].join(''),
51+
)
52+
}
53+
},
54+
},
3755
mode: project.config.mode,
3856
configFile: configPath,
3957
// watch is handled by Vitest

packages/browser/src/node/plugin.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
182182
...(project.config.snapshotSerializers || []),
183183
]
184184

185+
const exclude = [
186+
'vitest',
187+
'vitest/utils',
188+
'vitest/browser',
189+
'vitest/runners',
190+
'@vitest/browser',
191+
'@vitest/browser/client',
192+
'@vitest/utils',
193+
'@vitest/utils/source-map',
194+
'@vitest/runner',
195+
'@vitest/spy',
196+
'@vitest/utils/error',
197+
'@vitest/snapshot',
198+
'@vitest/expect',
199+
'std-env',
200+
'tinybench',
201+
'tinyspy',
202+
'tinyrainbow',
203+
'pathe',
204+
'msw',
205+
'msw/browser',
206+
]
207+
185208
if (project.config.diff) {
186209
entries.push(project.config.diff)
187210
}
@@ -193,12 +216,14 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
193216
const path = tryResolve('@vitest/coverage-v8', [project.ctx.config.root])
194217
if (path) {
195218
entries.push(path)
219+
exclude.push('@vitest/coverage-v8/browser')
196220
}
197221
}
198222
else if (provider === 'istanbul') {
199223
const path = tryResolve('@vitest/coverage-istanbul', [project.ctx.config.root])
200224
if (path) {
201225
entries.push(path)
226+
exclude.push('@vitest/coverage-istanbul')
202227
}
203228
}
204229
else if (provider === 'custom' && coverage.customProviderModule) {
@@ -224,29 +249,6 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
224249
include.push(vue)
225250
}
226251

227-
const exclude = [
228-
'vitest',
229-
'vitest/utils',
230-
'vitest/browser',
231-
'vitest/runners',
232-
'@vitest/browser',
233-
'@vitest/browser/client',
234-
'@vitest/utils',
235-
'@vitest/utils/source-map',
236-
'@vitest/runner',
237-
'@vitest/spy',
238-
'@vitest/utils/error',
239-
'@vitest/snapshot',
240-
'@vitest/expect',
241-
'std-env',
242-
'tinybench',
243-
'tinyspy',
244-
'tinyrainbow',
245-
'pathe',
246-
'msw',
247-
'msw/browser',
248-
]
249-
250252
const svelte = tryResolve('vitest-browser-svelte', [project.ctx.config.root])
251253
if (svelte) {
252254
exclude.push(svelte)

packages/vitest/src/public/node.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createServer as _createServer } from 'vite'
12
import { TestModule as _TestFile } from '../node/reporters/reported-tasks'
23
import type { ModuleDiagnostic as _FileDiagnostic } from '../node/reporters/reported-tasks'
34

@@ -49,7 +50,15 @@ export type { JsonOptions } from '../node/reporters/json'
4950
export type { JUnitOptions } from '../node/reporters/junit'
5051
export type { HTMLOptions } from '../node/reporters/html'
5152

52-
export { isFileServingAllowed, createServer, parseAst, parseAstAsync } from 'vite'
53+
export {
54+
isFileServingAllowed,
55+
parseAst,
56+
parseAstAsync,
57+
createLogger as createViteLogger,
58+
} from 'vite'
59+
/** @deprecated use `createViteServer` instead */
60+
export const createServer = _createServer
61+
export const createViteServer = _createServer
5362
export type * as Vite from 'vite'
5463

5564
export { TestCase, TestModule, TestSuite } from '../node/reporters/reported-tasks'

0 commit comments

Comments
 (0)