From 7ddb27725ec8f61fff49f8d2a32722cc871b7c28 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 7 Apr 2023 20:05:58 +0200 Subject: [PATCH 1/4] perf: avoid side effects resolving in dev and in the optimizer/scanner --- packages/vite/src/node/plugins/resolve.ts | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 856c8aa37e9033..c67342af6293cf 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -265,7 +265,11 @@ 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.scan && + options.isBuild && + !importer?.endsWith('.html') + ) { const resPkg = findNearestPackageData( path.dirname(res), options.packageCache, @@ -773,7 +777,7 @@ export function tryNodeResolve( return { ...resolved, id: resolvedId, external: true } } - if ((isBuild && !depsOptimizer) || externalize) { + if ((!options.scan && isBuild && !depsOptimizer) || externalize) { // Resolve package side effects for build so that rollup can better // perform tree-shaking return processResult({ @@ -853,7 +857,7 @@ export function tryNodeResolve( resolved = depsOptimizer!.getOptimizedDepId(optimizedInfo) } - if (isBuild) { + if (!options.scan && isBuild) { // Resolve package side effects for build so that rollup can better // perform tree-shaking return { @@ -1210,16 +1214,19 @@ 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.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) { From 34b988693d39beeee19be2d70ae796aaf0b3acfb Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 7 Apr 2023 20:42:33 +0200 Subject: [PATCH 2/4] perf: idOnly in resolve plugin Co-authored-by: Bjorn Lu --- packages/vite/src/node/config.ts | 1 + packages/vite/src/node/plugins/resolve.ts | 38 ++++++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index f4816167fa05e7..7e91c1775a680d 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -594,6 +594,7 @@ export async function resolveConfig( preferRelative: false, tryIndex: true, ...options, + idOnly: true, }), ], })) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c67342af6293cf..ca313cd81dfb68 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -115,6 +115,13 @@ export interface InternalResolveOptions extends Required { // 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 { @@ -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') ) { @@ -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 @@ -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?.( @@ -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({ @@ -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 { @@ -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, From 5ac3a4d6a5b49066ea6469000a073da26bbd3dfd Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 7 Apr 2023 20:51:20 +0200 Subject: [PATCH 3/4] perf: add back scan check, as we don't need side effects in the scanner --- packages/vite/src/node/plugins/resolve.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index ca313cd81dfb68..f45f11bd73fef6 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -274,6 +274,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { // hints so the non-used code is properly tree-shaken during build time. if ( !options.idOnly && + !options.scan && options.isBuild && !importer?.endsWith('.html') ) { @@ -788,7 +789,10 @@ export function tryNodeResolve( return { ...resolved, id: resolvedId, external: true } } - if (!options.idOnly && ((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({ @@ -868,7 +872,7 @@ export function tryNodeResolve( resolved = depsOptimizer!.getOptimizedDepId(optimizedInfo) } - if (!options.idOnly && isBuild) { + if (!options.idOnly && !options.scan && isBuild) { // Resolve package side effects for build so that rollup can better // perform tree-shaking return { @@ -1229,7 +1233,7 @@ function tryResolveBrowserMapping( if (options.idOnly) { return result } - if (options.isBuild) { + if (!options.scan && options.isBuild) { const resPkg = findNearestPackageData( path.dirname(res), options.packageCache, From ea242574c0891ae8e2df47a6d514267b11ec2665 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 8 Apr 2023 07:36:12 +0200 Subject: [PATCH 4/4] chore: format in single line --- packages/vite/src/node/plugins/resolve.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index f45f11bd73fef6..52a01652d4cd5d 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -314,12 +314,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { // external if (isExternalUrl(id)) { - return options.idOnly - ? id - : { - id, - external: true, - } + return options.idOnly ? id : { id, external: true } } // data uri: pass through (this only happens during build and will be @@ -391,12 +386,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { this.error(message) } - return options.idOnly - ? id - : { - id, - external: true, - } + return options.idOnly ? id : { id, external: true } } else { if (!asSrc) { debug?.(