Skip to content

Commit 6ea4be2

Browse files
authoredMar 23, 2023
perf: extract vite:resolve internal functions (#12522)
1 parent 04d14af commit 6ea4be2

File tree

1 file changed

+71
-62
lines changed

1 file changed

+71
-62
lines changed
 

‎packages/vite/src/node/plugins/resolve.ts

+71-62
Original file line numberDiff line numberDiff line change
@@ -170,35 +170,12 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
170170
scan: resolveOpts?.scan ?? resolveOptions.scan,
171171
}
172172

173-
const resolveSubpathImports = (id: string, importer?: string) => {
174-
if (!importer || !id.startsWith(subpathImportsPrefix)) return
175-
const basedir = path.dirname(importer)
176-
const pkgData = findNearestPackageData(basedir, options.packageCache)
177-
if (!pkgData) return
178-
179-
let importsPath = resolveExportsOrImports(
180-
pkgData.data,
181-
id,
182-
options,
183-
targetWeb,
184-
'imports',
185-
)
186-
187-
if (importsPath?.[0] === '.') {
188-
importsPath = path.relative(
189-
basedir,
190-
path.join(pkgData.dir, importsPath),
191-
)
192-
193-
if (importsPath[0] !== '.') {
194-
importsPath = `./${importsPath}`
195-
}
196-
}
197-
198-
return importsPath
199-
}
200-
201-
const resolvedImports = resolveSubpathImports(id, importer)
173+
const resolvedImports = resolveSubpathImports(
174+
id,
175+
importer,
176+
options,
177+
targetWeb,
178+
)
202179
if (resolvedImports) {
203180
id = resolvedImports
204181
}
@@ -230,42 +207,14 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
230207
return optimizedPath
231208
}
232209

233-
const ensureVersionQuery = (resolved: string): string => {
234-
if (
235-
!options.isBuild &&
236-
!options.scan &&
237-
depsOptimizer &&
238-
!(
239-
resolved === normalizedClientEntry ||
240-
resolved === normalizedEnvEntry
241-
)
242-
) {
243-
// Ensure that direct imports of node_modules have the same version query
244-
// as if they would have been imported through a bare import
245-
// Use the original id to do the check as the resolved id may be the real
246-
// file path after symlinks resolution
247-
const isNodeModule =
248-
nodeModulesInPathRE.test(normalizePath(id)) ||
249-
nodeModulesInPathRE.test(normalizePath(resolved))
250-
251-
if (isNodeModule && !resolved.match(DEP_VERSION_RE)) {
252-
const versionHash = depsOptimizer.metadata.browserHash
253-
if (versionHash && isOptimizable(resolved, depsOptimizer.options)) {
254-
resolved = injectQuery(resolved, `v=${versionHash}`)
255-
}
256-
}
257-
}
258-
return resolved
259-
}
260-
261210
// explicit fs paths that starts with /@fs/*
262211
if (asSrc && id.startsWith(FS_PREFIX)) {
263212
const fsPath = fsPathFromId(id)
264213
res = tryFsResolve(fsPath, options)
265214
isDebug && debug(`[@fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
266215
// always return here even if res doesn't exist since /@fs/ is explicit
267216
// if the file doesn't exist it should be a 404
268-
return ensureVersionQuery(res || fsPath)
217+
return ensureVersionQuery(res || fsPath, id, options, depsOptimizer)
269218
}
270219

271220
// URL
@@ -274,7 +223,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
274223
const fsPath = path.resolve(root, id.slice(1))
275224
if ((res = tryFsResolve(fsPath, options))) {
276225
isDebug && debug(`[url] ${colors.cyan(id)} -> ${colors.dim(res)}`)
277-
return ensureVersionQuery(res)
226+
return ensureVersionQuery(res, id, options, depsOptimizer)
278227
}
279228
}
280229

@@ -314,7 +263,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
314263
}
315264

316265
if ((res = tryFsResolve(fsPath, options))) {
317-
res = ensureVersionQuery(res)
266+
res = ensureVersionQuery(res, id, options, depsOptimizer)
318267
isDebug &&
319268
debug(`[relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
320269
const pkg = importer != null && idToPkgMap.get(importer)
@@ -336,7 +285,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
336285
if ((res = tryFsResolve(fsPath, options))) {
337286
isDebug &&
338287
debug(`[drive-relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
339-
return ensureVersionQuery(res)
288+
return ensureVersionQuery(res, id, options, depsOptimizer)
340289
}
341290
}
342291

@@ -346,7 +295,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
346295
(res = tryFsResolve(id, options))
347296
) {
348297
isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
349-
return ensureVersionQuery(res)
298+
return ensureVersionQuery(res, id, options, depsOptimizer)
350299
}
351300

352301
// external
@@ -467,6 +416,66 @@ export default new Proxy({}, {
467416
}
468417
}
469418

419+
function resolveSubpathImports(
420+
id: string,
421+
importer: string | undefined,
422+
options: InternalResolveOptions,
423+
targetWeb: boolean,
424+
) {
425+
if (!importer || !id.startsWith(subpathImportsPrefix)) return
426+
const basedir = path.dirname(importer)
427+
const pkgData = findNearestPackageData(basedir, options.packageCache)
428+
if (!pkgData) return
429+
430+
let importsPath = resolveExportsOrImports(
431+
pkgData.data,
432+
id,
433+
options,
434+
targetWeb,
435+
'imports',
436+
)
437+
438+
if (importsPath?.[0] === '.') {
439+
importsPath = path.relative(basedir, path.join(pkgData.dir, importsPath))
440+
441+
if (importsPath[0] !== '.') {
442+
importsPath = `./${importsPath}`
443+
}
444+
}
445+
446+
return importsPath
447+
}
448+
449+
function ensureVersionQuery(
450+
resolved: string,
451+
id: string,
452+
options: InternalResolveOptions,
453+
depsOptimizer?: DepsOptimizer,
454+
): string {
455+
if (
456+
!options.isBuild &&
457+
!options.scan &&
458+
depsOptimizer &&
459+
!(resolved === normalizedClientEntry || resolved === normalizedEnvEntry)
460+
) {
461+
// Ensure that direct imports of node_modules have the same version query
462+
// as if they would have been imported through a bare import
463+
// Use the original id to do the check as the resolved id may be the real
464+
// file path after symlinks resolution
465+
const isNodeModule =
466+
nodeModulesInPathRE.test(normalizePath(id)) ||
467+
nodeModulesInPathRE.test(normalizePath(resolved))
468+
469+
if (isNodeModule && !resolved.match(DEP_VERSION_RE)) {
470+
const versionHash = depsOptimizer.metadata.browserHash
471+
if (versionHash && isOptimizable(resolved, depsOptimizer.options)) {
472+
resolved = injectQuery(resolved, `v=${versionHash}`)
473+
}
474+
}
475+
}
476+
return resolved
477+
}
478+
470479
function splitFileAndPostfix(path: string) {
471480
let file = path
472481
let postfix = ''

0 commit comments

Comments
 (0)
Please sign in to comment.