Skip to content

Commit

Permalink
perf: idOnly in resolve plugin
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
patak-dev and bluwy committed Apr 7, 2023
1 parent 7ddb277 commit 34b9886
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/node/config.ts
Expand Up @@ -594,6 +594,7 @@ export async function resolveConfig(
preferRelative: false,
tryIndex: true,
...options,
idOnly: true,
}),
],
}))
Expand Down
38 changes: 26 additions & 12 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -115,6 +115,13 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
// Resolve using esbuild deps optimization
getDepsOptimizer?: (ssr: boolean) => DepsOptimizer | undefined
shouldExternalize?: (id: string) => boolean | undefined

/**
* Set by createResolver, we only care about the resolved id. moduleSideEffects
* and other fields are discarded so we can avoid computing them.
* @internal
*/
idOnly?: boolean
}

export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
Expand Down Expand Up @@ -266,7 +273,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
// If this isn't a script imported from a .html file, include side effects
// hints so the non-used code is properly tree-shaken during build time.
if (
!options.scan &&
!options.idOnly &&
options.isBuild &&
!importer?.endsWith('.html')
) {
Expand Down Expand Up @@ -306,10 +313,12 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

// external
if (isExternalUrl(id)) {
return {
id,
external: true,
}
return options.idOnly
? id
: {
id,
external: true,
}
}

// data uri: pass through (this only happens during build and will be
Expand Down Expand Up @@ -381,10 +390,12 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
this.error(message)
}

return {
id,
external: true,
}
return options.idOnly
? id
: {
id,
external: true,
}
} else {
if (!asSrc) {
debug?.(
Expand Down Expand Up @@ -777,7 +788,7 @@ export function tryNodeResolve(
return { ...resolved, id: resolvedId, external: true }
}

if ((!options.scan && isBuild && !depsOptimizer) || externalize) {
if (!options.idOnly && ((isBuild && !depsOptimizer) || externalize)) {
// Resolve package side effects for build so that rollup can better
// perform tree-shaking
return processResult({
Expand Down Expand Up @@ -857,7 +868,7 @@ export function tryNodeResolve(
resolved = depsOptimizer!.getOptimizedDepId(optimizedInfo)
}

if (!options.scan && isBuild) {
if (!options.idOnly && isBuild) {
// Resolve package side effects for build so that rollup can better
// perform tree-shaking
return {
Expand Down Expand Up @@ -1215,7 +1226,10 @@ function tryResolveBrowserMapping(
) {
debug?.(`[browser mapped] ${colors.cyan(id)} -> ${colors.dim(res)}`)
let result: PartialResolvedId = { id: res }
if (!options.scan && options.isBuild) {
if (options.idOnly) {
return result
}
if (options.isBuild) {
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
Expand Down

0 comments on commit 34b9886

Please sign in to comment.