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

fix(vitest): fix optimizeDeps.disabled warnings on Vite 5.1 #5215

Merged
Merged
13 changes: 12 additions & 1 deletion packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
if (!allowed && testOptions?.enabled === true)
console.warn(`Vitest: "deps.optimizer" is only available in Vite >= 4.3.2, current Vite version: ${viteVersion}`)
else
// enable by default
// disabled by default
testOptions.enabled ??= false
if (!allowed || testOptions?.enabled !== true) {
newConfig.cacheDir = undefined
Expand Down Expand Up @@ -50,6 +50,17 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
include,
}
}

// `optimizeDeps.disabled` is deprecated since v5.1.0-beta.1
// https://github.com/vitejs/vite/pull/15184
if (major >= 5 && minor >= 1) {
if (newConfig.optimizeDeps.disabled) {
newConfig.optimizeDeps.noDiscovery = true
newConfig.optimizeDeps.include = []
}
delete newConfig.optimizeDeps.disabled
}

return newConfig
}

Expand Down
68 changes: 44 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/optimize-deps/dep-url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const importMetaUrl = import.meta.url
5 changes: 5 additions & 0 deletions test/optimize-deps/dep-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitest/test-deps-url",
"type": "module",
"exports": "./index.js"
}
12 changes: 12 additions & 0 deletions test/optimize-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@vitest/test-optimize-deps",
"type": "module",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"@vitest/test-dep-url": "file:./dep-url",
"vitest": "workspace:*"
}
}
10 changes: 10 additions & 0 deletions test/optimize-deps/test/ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @vitest-environment node

import { expect, test } from 'vitest'

// @ts-expect-error untyped
import { importMetaUrl } from '@vitest/test-dep-url'

test('import.meta.url', () => {
expect(importMetaUrl).toContain('/node_modules/.vitest/deps_ssr/')

Check failure on line 9 in test/optimize-deps/test/ssr.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

test/ssr.test.ts > import.meta.url

AssertionError: expected 'file:///D:/a/vitest/vitest/node_modul…' to include '/node_modules/.vitest/deps_ssr/' ❯ test/ssr.test.ts:9:25
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
})
10 changes: 10 additions & 0 deletions test/optimize-deps/test/web.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @vitest-environment happy-dom

import { expect, test } from 'vitest'

// @ts-expect-error untyped
import { importMetaUrl } from '@vitest/test-dep-url'

test('import.meta.url', () => {
expect(importMetaUrl).toContain('/node_modules/.vitest/deps/')
})
40 changes: 40 additions & 0 deletions test/optimize-deps/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defineConfig } from 'vitest/config'

// cf.
// https://stackblitz.com/edit/vitest-dev-vitest-8wf26p?file=test%2Fssr.test.ts

export default defineConfig({
optimizeDeps: {
include: ['@vitest/test-dep-url'],
},
ssr: {
optimizeDeps: {
include: ['@vitest/test-dep-url'],
},
},
test: {
deps: {
optimizer: {
web: {
enabled: true,
},
ssr: {
enabled: true,
},
},
},
},
// use dummy ssrLoadModule to trigger ssr.optimizeDeps.
// this will be unnecessary from Vite 5.1
// cf. https://github.com/vitejs/vite/pull/15561
plugins: [
{
name: 'force-ssr-optimize-deps',
configureServer(server) {
return async () => {
await server.ssrLoadModule('/package.json')
}
},
},
],
})
Loading