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: avoid side effects resolving in dev and in the optimizer/scanner #12789

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
61 changes: 43 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,12 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

// external
if (isExternalUrl(id)) {
return {
id,
external: true,
}
return options.idOnly
? id
: {
id,
external: true,
}
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
}

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

return {
id,
external: true,
}
return options.idOnly
? id
: {
id,
external: true,
}
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!asSrc) {
debug?.(
Expand Down Expand Up @@ -773,7 +789,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 +872,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 +1229,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