Skip to content

Commit cc9bc21

Browse files
authored
fix(optimizer): detect cache dir precisely (#23912)
1 parent df36558 commit cc9bc21

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import path from 'node:path'
2+
import { describe, expect, test } from 'vitest'
3+
import type { Environment } from '../../environment'
4+
import {
5+
createIsOptimizedDepFile,
6+
createIsOptimizedDepUrl,
7+
} from '../../optimizer'
8+
import { normalizePath, removeLeadingSlash } from '../../utils'
9+
10+
const root = normalizePath(path.resolve(import.meta.dirname, '/project'))
11+
const cacheDir = normalizePath(path.resolve(import.meta.dirname, '/cache'))
12+
const environment = {
13+
name: 'client',
14+
config: { root, cacheDir },
15+
} as Environment
16+
const cacheDirNames = [
17+
'deps',
18+
'deps_ssr',
19+
'deps_ssr_worker',
20+
'deps_temp_123',
21+
'deps-extra',
22+
]
23+
24+
describe('createIsOptimizedDepFile', () => {
25+
const isFile = createIsOptimizedDepFile(environment)
26+
27+
for (const dir of cacheDirNames) {
28+
test(`classifies files in ${dir}`, () => {
29+
expect(isFile(`${cacheDir}/${dir}/dep.js?v=abc`)).toBe(dir === 'deps')
30+
})
31+
}
32+
})
33+
34+
describe('createIsOptimizedDepUrl', () => {
35+
const isUrl = createIsOptimizedDepUrl(environment)
36+
37+
for (const dir of cacheDirNames) {
38+
test(`classifies URLs in ${dir}`, () => {
39+
expect(
40+
isUrl(`/@fs/${removeLeadingSlash(cacheDir)}/${dir}/dep.js.map`),
41+
).toBe(dir === 'deps')
42+
})
43+
}
44+
45+
test('matches root-relative URLs when the cache is inside root', () => {
46+
const isUrlInsideRoot = createIsOptimizedDepUrl({
47+
name: 'client',
48+
config: { root, cacheDir: `${root}/cache` },
49+
} as Environment)
50+
51+
expect(isUrlInsideRoot('/cache/deps/dep.js')).toBe(true)
52+
expect(isUrlInsideRoot('/cache/deps_other/dep.js')).toBe(false)
53+
})
54+
})

‎packages/vite/src/node/optimizer/index.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from 'rolldown'
1515
import { isDynamicPattern } from 'tinyglobby'
1616
import type { DepsOptimizerEsbuildOptions } from '#types/internal/esbuildOptions'
17-
import { isWindows } from '../../shared/utils'
17+
import { isWindows, withTrailingSlash } from '../../shared/utils'
1818
import type { ResolvedConfig } from '../config'
1919
import {
2020
ESBUILD_BASELINE_WIDELY_AVAILABLE_TARGET,
@@ -988,16 +988,18 @@ function getDepsCacheDirPrefix(environment: Environment): string {
988988
export function createIsOptimizedDepFile(
989989
environment: Environment,
990990
): (id: string) => boolean {
991-
const depsCacheDirPrefix = getDepsCacheDirPrefix(environment)
991+
const depsCacheDirPrefix = withTrailingSlash(getDepsCacheDir(environment))
992992
return (id) => id.startsWith(depsCacheDirPrefix)
993993
}
994994

995995
export function createIsOptimizedDepUrl(
996996
environment: Environment,
997997
): (url: string) => boolean {
998998
const { root } = environment.config
999-
const depsCacheDir = getDepsCacheDirPrefix(environment)
1000-
const depsCacheDirPrefix = fileToModuleUrl(depsCacheDir, root)
999+
const depsCacheDir = getDepsCacheDir(environment)
1000+
const depsCacheDirPrefix = withTrailingSlash(
1001+
fileToModuleUrl(depsCacheDir, root),
1002+
)
10011003
return function isOptimizedDepUrl(url: string): boolean {
10021004
return url.startsWith(depsCacheDirPrefix)
10031005
}

0 commit comments

Comments
 (0)