Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]`)
})
Expand Down
4 changes: 4 additions & 0 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ <h2>Detecting linked src package and optimizing its deps (lodash-es)</h2>
<h2>Optimizing force included dep even when it's linked</h2>
<div class="force-include"></div>

<h2>Optimizing a project source file through a relative import</h2>
<div class="source-include"></div>
<script type="module" src="./source-include-entry.js"></script>

<h2>Dep with CSS</h2>
<div class="dep-linked-include">This should be red</div>

Expand Down
3 changes: 3 additions & 0 deletions playground/optimize-deps/source-include-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { message } from './source-include'

document.querySelector('.source-include').textContent = message
1 change: 1 addition & 0 deletions playground/optimize-deps/source-include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = 'source include'
1 change: 1 addition & 0 deletions playground/optimize-deps/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading