diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 1d64e1da55b5a3..1976de78f64258 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -182,6 +182,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { const ensureVersionQuery = (resolved: string): string => { if ( !options.isBuild && + !options.scan && depsOptimizer && !( resolved === normalizedClientEntry || diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 2137d068372c6a..bb0841e5b72f65 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -143,6 +143,12 @@ test('import aliased package with colon', async () => { expect(await page.textContent('.url')).toBe('vitejs.dev') }) +test('import aliased package using absolute path', async () => { + expect(await page.textContent('.alias-using-absolute-path')).toBe( + 'From dep-alias-using-absolute-path', + ) +}) + test('variable names are reused in different scripts', async () => { expect(await page.textContent('.reused-variable-names')).toBe('reused') }) diff --git a/playground/optimize-deps/dep-alias-using-absolute-path/index.js b/playground/optimize-deps/dep-alias-using-absolute-path/index.js new file mode 100644 index 00000000000000..82b1f4d1f0874f --- /dev/null +++ b/playground/optimize-deps/dep-alias-using-absolute-path/index.js @@ -0,0 +1,13 @@ +// Importing a shared dependency used by other modules, +// so dependency optimizer creates a common chunk. +// This is used to setup a test scenario, where dep scanner +// could not determine all of the used dependencies on first +// pass, e.g., a dependency that is aliased using an absolute +// path, in which case it used to trigger unnecessary "full +// reloads" invalidating all modules in a module graph. +const cloneDeep = require('lodash/cloneDeep') + +// no-op, using imported module for sake of completeness +module.exports = cloneDeep({ + message: 'From dep-alias-using-absolute-path', +}).message diff --git a/playground/optimize-deps/dep-alias-using-absolute-path/package.json b/playground/optimize-deps/dep-alias-using-absolute-path/package.json new file mode 100644 index 00000000000000..3a43490c40ba48 --- /dev/null +++ b/playground/optimize-deps/dep-alias-using-absolute-path/package.json @@ -0,0 +1,9 @@ +{ + "name": "@vitejs/test-dep-alias-using-absolute-path", + "private": true, + "version": "1.0.0", + "main": "index.js", + "dependencies": { + "lodash": "^4.17.21" + } +} diff --git a/playground/optimize-deps/dynamic-use-dep-alias-using-absolute-path.js b/playground/optimize-deps/dynamic-use-dep-alias-using-absolute-path.js new file mode 100644 index 00000000000000..784123a81ec685 --- /dev/null +++ b/playground/optimize-deps/dynamic-use-dep-alias-using-absolute-path.js @@ -0,0 +1,6 @@ +// This is used to setup a test scenario, where dep scanner +// could not determine all of the used dependencies on first +// pass, e.g., a dependency that is aliased using an absolute +// path, in which case it used to trigger unnecessary "full +// reloads" invalidating all modules in a module graph. +export { default } from '@vitejs/test-dep-alias-using-absolute-path' diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 2e24f421542fe7..921bf244c08d8d 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -83,6 +83,9 @@

Nested include

Alias with colon

URL:
+

Alias using absolute path

+
+

Reused variable names

This should show reused:
@@ -153,6 +156,10 @@

Non Optimized Module isn't duplicated

import { parse } from 'node:url' text('.url', parse('https://vitejs.dev').hostname) + import('./dynamic-use-dep-alias-using-absolute-path.js').then((mod) => + text('.alias-using-absolute-path', mod.default), + ) + import './index.astro' // All these imports should end up resolved to the same URL (same ?v= injected on them) diff --git a/playground/optimize-deps/package.json b/playground/optimize-deps/package.json index 675dd035e494d2..238c442b351310 100644 --- a/playground/optimize-deps/package.json +++ b/playground/optimize-deps/package.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^0.27.2", "clipboard": "^2.0.11", + "@vitejs/test-dep-alias-using-absolute-path": "file:./dep-alias-using-absolute-path", "@vitejs/test-dep-cjs-browser-field-bare": "file:./dep-cjs-browser-field-bare", "@vitejs/test-dep-cjs-compiled-from-cjs": "file:./dep-cjs-compiled-from-cjs", "@vitejs/test-dep-cjs-compiled-from-esm": "file:./dep-cjs-compiled-from-esm", diff --git a/playground/optimize-deps/entry.js b/playground/optimize-deps/unused-split-entry.js similarity index 100% rename from playground/optimize-deps/entry.js rename to playground/optimize-deps/unused-split-entry.js diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index b07f519783762e..24278a449012fc 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -11,6 +11,9 @@ module.exports = { dedupe: ['react'], alias: { 'node:url': 'url', + '@vitejs/test-dep-alias-using-absolute-path': require.resolve( + '@vitejs/test-dep-alias-using-absolute-path', + ), }, }, optimizeDeps: { @@ -38,7 +41,7 @@ module.exports = { }, ], }, - entries: ['entry.js'], + entries: ['index.html', 'unused-split-entry.js'], }, build: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8e48e76538290a..dbd2a52a797c0e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -616,6 +616,7 @@ importers: playground/optimize-deps: specifiers: '@vitejs/test-added-in-entries': file:./added-in-entries + '@vitejs/test-dep-alias-using-absolute-path': file:./dep-alias-using-absolute-path '@vitejs/test-dep-cjs-browser-field-bare': file:./dep-cjs-browser-field-bare '@vitejs/test-dep-cjs-compiled-from-cjs': file:./dep-cjs-compiled-from-cjs '@vitejs/test-dep-cjs-compiled-from-esm': file:./dep-cjs-compiled-from-esm @@ -646,6 +647,7 @@ importers: vuex: ^4.1.0 dependencies: '@vitejs/test-added-in-entries': file:playground/optimize-deps/added-in-entries + '@vitejs/test-dep-alias-using-absolute-path': file:playground/optimize-deps/dep-alias-using-absolute-path '@vitejs/test-dep-cjs-browser-field-bare': file:playground/optimize-deps/dep-cjs-browser-field-bare '@vitejs/test-dep-cjs-compiled-from-cjs': file:playground/optimize-deps/dep-cjs-compiled-from-cjs '@vitejs/test-dep-cjs-compiled-from-esm': file:playground/optimize-deps/dep-cjs-compiled-from-esm @@ -678,6 +680,12 @@ importers: playground/optimize-deps/added-in-entries: specifiers: {} + playground/optimize-deps/dep-alias-using-absolute-path: + specifiers: + lodash: ^4.17.21 + dependencies: + lodash: 4.17.21 + playground/optimize-deps/dep-cjs-browser-field-bare: specifiers: {} @@ -9926,6 +9934,14 @@ packages: version: 1.0.0 dev: false + file:playground/optimize-deps/dep-alias-using-absolute-path: + resolution: {directory: playground/optimize-deps/dep-alias-using-absolute-path, type: directory} + name: '@vitejs/test-dep-alias-using-absolute-path' + version: 1.0.0 + dependencies: + lodash: 4.17.21 + dev: false + file:playground/optimize-deps/dep-cjs-browser-field-bare: resolution: {directory: playground/optimize-deps/dep-cjs-browser-field-bare, type: directory} name: '@vitejs/test-dep-cjs-browser-field-bare'