Skip to content

Commit

Permalink
perf: avoid side effects resolving in dev and in the optimizer/scanner (
Browse files Browse the repository at this point in the history
#12789)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
patak-dev and bluwy committed Apr 8, 2023
1 parent 09350c6 commit fb904f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 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
51 changes: 33 additions & 18 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 @@ -265,7 +272,12 @@ 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 (!importer?.endsWith('.html')) {
if (
!options.idOnly &&
!options.scan &&
options.isBuild &&
!importer?.endsWith('.html')
) {
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
Expand Down Expand Up @@ -302,10 +314,7 @@ 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 @@ -377,10 +386,7 @@ 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 @@ -773,7 +779,10 @@ export function tryNodeResolve(
return { ...resolved, id: resolvedId, external: true }
}

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

if (isBuild) {
if (!options.idOnly && !options.scan && isBuild) {
// Resolve package side effects for build so that rollup can better
// perform tree-shaking
return {
Expand Down Expand Up @@ -1210,16 +1219,22 @@ function tryResolveBrowserMapping(
: tryFsResolve(path.join(pkg.dir, browserMappedPath), options))
) {
debug?.(`[browser mapped] ${colors.cyan(id)} -> ${colors.dim(res)}`)
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
)
const result = resPkg
? {
let result: PartialResolvedId = { id: res }
if (options.idOnly) {
return result
}
if (!options.scan && options.isBuild) {
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
)
if (resPkg) {
result = {
id: res,
moduleSideEffects: resPkg.hasSideEffects(res),
}
: { id: res }
}
}
return externalize ? { ...result, external: true } : result
}
} else if (browserMappedPath === false) {
Expand Down

0 comments on commit fb904f9

Please sign in to comment.