Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache depsCacheDirPrefix value for isOptimizedDepFile #12601

Merged
merged 1 commit into from
Mar 26, 2023
Merged
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
10 changes: 5 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,15 +930,15 @@ export function getDepsCacheDir(config: ResolvedConfig, ssr: boolean): string {
return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(config, ssr)
}

export function getDepsCacheDirPrefix(config: ResolvedConfig): string {
function getDepsCacheDirPrefix(config: ResolvedConfig): string {
return normalizePath(path.resolve(config.cacheDir, 'deps'))
}

export function isOptimizedDepFile(
id: string,
export function createIsOptimizedDepFile(
config: ResolvedConfig,
): boolean {
return id.startsWith(getDepsCacheDirPrefix(config))
): (id: string) => boolean {
const depsCacheDirPrefix = getDepsCacheDirPrefix(config)
return (id) => id.startsWith(depsCacheDirPrefix)
}

export function createIsOptimizedDepUrl(
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ResolvedConfig, ViteDevServer } from '..'
import {
addManuallyIncludedOptimizeDeps,
addOptimizedDepInfo,
createIsOptimizedDepFile,
createIsOptimizedDepUrl,
debuggerViteDeps as debug,
depsFromOptimizedDepInfo,
Expand All @@ -14,7 +15,6 @@ import {
extractExportsData,
getOptimizedDepPath,
initDepsOptimizerMetadata,
isOptimizedDepFile,
loadCachedDepOptimizationMetadata,
newDepOptimizationProcessing,
optimizeServerSsrDeps,
Expand Down Expand Up @@ -112,7 +112,7 @@ async function createDepsOptimizer(
metadata,
registerMissingImport,
run: () => debouncedProcessing(0),
isOptimizedDepFile: (id: string) => isOptimizedDepFile(id, config),
isOptimizedDepFile: createIsOptimizedDepFile(config),
isOptimizedDepUrl: createIsOptimizedDepUrl(config),
getOptimizedDepId: (depInfo: OptimizedDepInfo) =>
isBuild ? depInfo.file : `${depInfo.file}?v=${depInfo.browserHash}`,
Expand Down Expand Up @@ -754,7 +754,7 @@ async function createDevSsrDepsOptimizer(

const depsOptimizer = {
metadata,
isOptimizedDepFile: (id: string) => isOptimizedDepFile(id, config),
isOptimizedDepFile: createIsOptimizedDepFile(config),
isOptimizedDepUrl: createIsOptimizedDepUrl(config),
getOptimizedDepId: (depInfo: OptimizedDepInfo) =>
`${depInfo.file}?v=${depInfo.browserHash}`,
Expand Down
8 changes: 2 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ import {
shouldExternalizeForSSR,
} from '../ssr/ssrExternal'
import { transformRequest } from '../server/transformRequest'
import {
getDepsCacheDirPrefix,
getDepsOptimizer,
optimizedDepNeedsInterop,
} from '../optimizer'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import { checkPublicFile } from './asset'
import {
ERR_OUTDATED_OPTIMIZED_DEP,
Expand Down Expand Up @@ -348,7 +344,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// in root: infer short absolute path from root
url = resolved.id.slice(root.length)
} else if (
resolved.id.startsWith(getDepsCacheDirPrefix(config)) ||
depsOptimizer?.isOptimizedDepFile(resolved.id) ||
fs.existsSync(cleanUrl(resolved.id))
) {
// an optimized deps may not yet exists in the filesystem, or
Expand Down