Skip to content

Commit

Permalink
fix(vitest): fix optimizeDeps.disabled warnings on Vite 5.1 (#5215)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 16, 2024
1 parent b3182e0 commit 1aecd65
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 27 deletions.
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
66 changes: 40 additions & 26 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:*"
}
}
12 changes: 12 additions & 0 deletions test/optimize-deps/test/ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @vitest-environment node

import { expect, test } from 'vitest'

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

// TODO: flaky on Windows
// https://github.com/vitest-dev/vitest/pull/5215#discussion_r1492066033
test.skipIf(process.platform === 'win32')('import.meta.url', () => {
expect(importMetaUrl).toContain('/node_modules/.vitest/deps_ssr/')
})
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'

export default defineConfig({
optimizeDeps: {
include: ['@vitest/test-dep-url'],
},
ssr: {
optimizeDeps: {
include: ['@vitest/test-dep-url'],
},
},
test: {
chaiConfig: {
truncateThreshold: 1000,
},
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')
}
},
},
],
})

0 comments on commit 1aecd65

Please sign in to comment.