From 25d9d226840bbc72c0e048bd70da4d778d1e3445 Mon Sep 17 00:00:00 2001 From: DaZuiZui Date: Fri, 4 Sep 2026 02:25:02 +0800 Subject: [PATCH] fix: resolve relative imports for optimized source files (fix #23430) --- packages/vite/src/node/plugins/resolve.ts | 12 ++++++++++++ .../__tests__/optimize-deps.spec.ts | 18 ++++++++++++++++++ playground/optimize-deps/index.html | 4 ++++ .../optimize-deps/source-include-entry.js | 3 +++ playground/optimize-deps/source-include.js | 1 + playground/optimize-deps/vite.config.js | 1 + 6 files changed, 39 insertions(+) create mode 100644 playground/optimize-deps/source-include-entry.js create mode 100644 playground/optimize-deps/source-include.js diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 7bd7282aed712b..b4ec18d4157192 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -450,6 +450,18 @@ function optimizerResolvePlugin( const normalizedFsPath = normalizePath(fsPath) + if (asSrc && !options.scan && !SPECIAL_QUERY_RE.test(id)) { + const resolvedFsPath = tryFsResolve(fsPath, options) + const optimizedDep = depsOptimizer.metadata.depInfoList.find( + (depInfo) => + depInfo.src === + normalizePath(resolvedFsPath || normalizedFsPath), + ) + if (optimizedDep) { + return depsOptimizer.getOptimizedDepId(optimizedDep) + } + } + if (depsOptimizer.isOptimizedDepFile(normalizedFsPath)) { // Optimized files could not yet exist in disk, resolve to the full path // Inject the current browserHash version if the path doesn't have one diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index d6febaafad2c17..5b16169b449072 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -126,6 +126,24 @@ test('forced include', async () => { .toMatch(`[success]`) }) +test.runIf(isServe)( + 'optimizes a project source file imported relatively', + async () => { + await expect + .poll(() => page.textContent('.source-include')) + .toBe('source include') + + const metadata = readDepOptimizationMetadata() + const optimizedFile = metadata.optimized['./source-include.js'].file + const response = await fetch( + new URL('/source-include-entry.js', viteTestUrl), + ) + const source = await response.text() + + expect(source).toContain(`/node_modules/.vite/deps/${optimizedFile}`) + }, +) + test('import * from optimized dep', async () => { await expect.poll(() => page.textContent('.import-star')).toMatch(`[success]`) }) diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 2e3b6607dabff7..dfeab677720d70 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -58,6 +58,10 @@

Detecting linked src package and optimizing its deps (lodash-es)

Optimizing force included dep even when it's linked

+

Optimizing a project source file through a relative import

+
+ +

Dep with CSS

This should be red
diff --git a/playground/optimize-deps/source-include-entry.js b/playground/optimize-deps/source-include-entry.js new file mode 100644 index 00000000000000..b6131c9c2d0289 --- /dev/null +++ b/playground/optimize-deps/source-include-entry.js @@ -0,0 +1,3 @@ +import { message } from './source-include' + +document.querySelector('.source-include').textContent = message diff --git a/playground/optimize-deps/source-include.js b/playground/optimize-deps/source-include.js new file mode 100644 index 00000000000000..8049e851090730 --- /dev/null +++ b/playground/optimize-deps/source-include.js @@ -0,0 +1 @@ +export const message = 'source include' diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index 3a1abe05c6a50e..d082c7bd59ed7f 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -30,6 +30,7 @@ export default defineConfig({ // only referenced by the import that injectImportIntoOptimizedDep() // adds at serve time, so the scanner can never discover it '@vitejs/test-dep-cjs-for-injected-import', + './source-include.js', ], exclude: [ '@vitejs/test-nested-exclude',