Skip to content

Commit 05a50bf

Browse files
committed
refactor(@angular/build): merge common plugins and options into getEsBuildCommonOptions
Consolidates common ESBuild plugins and configuration options into a single utility function
1 parent 2ee9042 commit 05a50bf

File tree

1 file changed

+78
-86
lines changed

1 file changed

+78
-86
lines changed

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

+78-86
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import type { BuildOptions, PartialMessage } from 'esbuild';
9+
import type { BuildOptions, PartialMessage, Plugin } from 'esbuild';
1010
import assert from 'node:assert';
1111
import { createHash } from 'node:crypto';
1212
import { extname, relative } from 'node:path';
@@ -67,27 +67,25 @@ export function createBrowserCodeBundleOptions(
6767
entryPoints,
6868
target,
6969
supported: getFeatureSupport(target, zoneless),
70-
plugins: [
71-
createLoaderImportAttributePlugin(),
72-
createWasmPlugin({ allowAsync: zoneless, cache: loadCache }),
73-
createSourcemapIgnorelistPlugin(),
74-
createAngularLocalizeInitWarningPlugin(),
75-
createCompilerPlugin(
76-
// JS/TS options
77-
pluginOptions,
78-
angularCompilation,
79-
// Component stylesheet bundler
80-
stylesheetBundler,
81-
),
82-
],
8370
};
8471

72+
buildOptions.plugins ??= [];
73+
buildOptions.plugins.push(
74+
createWasmPlugin({ allowAsync: zoneless, cache: loadCache }),
75+
createAngularLocalizeInitWarningPlugin(),
76+
createCompilerPlugin(
77+
// JS/TS options
78+
pluginOptions,
79+
angularCompilation,
80+
// Component stylesheet bundler
81+
stylesheetBundler,
82+
),
83+
);
84+
8585
if (options.plugins) {
86-
buildOptions.plugins?.push(...options.plugins);
86+
buildOptions.plugins.push(...options.plugins);
8787
}
8888

89-
appendOptionsForExternalPackages(options, buildOptions);
90-
9189
return buildOptions;
9290
};
9391
}
@@ -275,22 +273,21 @@ export function createServerMainCodeBundleOptions(
275273
},
276274
entryPoints,
277275
supported: getFeatureSupport(target, zoneless),
278-
plugins: [
279-
createWasmPlugin({ allowAsync: zoneless, cache: loadResultCache }),
280-
createSourcemapIgnorelistPlugin(),
281-
createAngularLocalizeInitWarningPlugin(),
282-
createCompilerPlugin(
283-
// JS/TS options
284-
pluginOptions,
285-
// Browser compilation handles the actual Angular code compilation
286-
new NoopCompilation(),
287-
// Component stylesheet bundler
288-
stylesheetBundler,
289-
),
290-
],
291276
};
292277

293278
buildOptions.plugins ??= [];
279+
buildOptions.plugins.push(
280+
createWasmPlugin({ allowAsync: zoneless, cache: loadResultCache }),
281+
createAngularLocalizeInitWarningPlugin(),
282+
createCompilerPlugin(
283+
// JS/TS options
284+
pluginOptions,
285+
// Browser compilation handles the actual Angular code compilation
286+
new NoopCompilation(),
287+
// Component stylesheet bundler
288+
stylesheetBundler,
289+
),
290+
);
294291

295292
if (!externalPackages) {
296293
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
@@ -369,8 +366,6 @@ export function createServerMainCodeBundleOptions(
369366
buildOptions.plugins.push(...options.plugins);
370367
}
371368

372-
appendOptionsForExternalPackages(options, buildOptions);
373-
374369
return buildOptions;
375370
};
376371
}
@@ -418,21 +413,20 @@ export function createSsrEntryCodeBundleOptions(
418413
'server': ssrEntryNamespace,
419414
},
420415
supported: getFeatureSupport(target, true),
421-
plugins: [
422-
createSourcemapIgnorelistPlugin(),
423-
createAngularLocalizeInitWarningPlugin(),
424-
createCompilerPlugin(
425-
// JS/TS options
426-
pluginOptions,
427-
// Browser compilation handles the actual Angular code compilation
428-
new NoopCompilation(),
429-
// Component stylesheet bundler
430-
stylesheetBundler,
431-
),
432-
],
433416
};
434417

435418
buildOptions.plugins ??= [];
419+
buildOptions.plugins.push(
420+
createAngularLocalizeInitWarningPlugin(),
421+
createCompilerPlugin(
422+
// JS/TS options
423+
pluginOptions,
424+
// Browser compilation handles the actual Angular code compilation
425+
new NoopCompilation(),
426+
// Component stylesheet bundler
427+
stylesheetBundler,
428+
),
429+
);
436430

437431
if (!externalPackages) {
438432
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
@@ -506,21 +500,19 @@ export function createSsrEntryCodeBundleOptions(
506500
buildOptions.plugins.push(...options.plugins);
507501
}
508502

509-
appendOptionsForExternalPackages(options, buildOptions);
510-
511503
return buildOptions;
512504
};
513505
}
514506

515507
function getEsBuildServerCommonOptions(options: NormalizedApplicationBuildOptions): BuildOptions {
516508
const isNodePlatform = options.ssrOptions?.platform !== ExperimentalPlatform.Neutral;
517509

518-
const commonOptons = getEsBuildCommonOptions(options);
519-
commonOptons.define ??= {};
520-
commonOptons.define['ngServerMode'] = 'true';
510+
const commonOptions = getEsBuildCommonOptions(options);
511+
commonOptions.define ??= {};
512+
commonOptions.define['ngServerMode'] = 'true';
521513

522514
return {
523-
...commonOptons,
515+
...commonOptions,
524516
platform: isNodePlatform ? 'node' : 'neutral',
525517
outExtension: { '.js': '.mjs' },
526518
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
@@ -585,15 +577,41 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
585577
conditions.push(...customConditions);
586578
} else {
587579
// Include default conditions
588-
conditions.push('module');
589-
conditions.push(optimizationOptions.scripts ? 'production' : 'development');
580+
conditions.push('module', optimizationOptions.scripts ? 'production' : 'development');
581+
}
582+
583+
const plugins: Plugin[] = [
584+
createLoaderImportAttributePlugin(),
585+
createSourcemapIgnorelistPlugin(),
586+
];
587+
588+
let packages: BuildOptions['packages'] = 'bundle';
589+
if (options.externalPackages) {
590+
// Package files affected by a customized loader should not be implicitly marked as external
591+
if (
592+
options.loaderExtensions ||
593+
options.plugins ||
594+
typeof options.externalPackages === 'object'
595+
) {
596+
// Plugin must be added after custom plugins to ensure any added loader options are considered
597+
plugins.push(
598+
createExternalPackagesPlugin(
599+
options.externalPackages !== true ? options.externalPackages : undefined,
600+
),
601+
);
602+
603+
packages = 'bundle';
604+
} else {
605+
// Safe to use the packages external option directly
606+
packages = 'external';
607+
}
590608
}
591609

592610
return {
593611
absWorkingDir: workspaceRoot,
594612
format: 'esm',
595613
bundle: true,
596-
packages: 'bundle',
614+
packages,
597615
assetNames: outputNames.media,
598616
conditions,
599617
resolveExtensions: ['.ts', '.tsx', '.mjs', '.js', '.cjs'],
@@ -626,6 +644,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
626644
},
627645
loader: loaderExtensions,
628646
footer,
647+
plugins,
629648
};
630649
}
631650

@@ -636,11 +655,10 @@ function getEsBuildCommonPolyfillsOptions(
636655
loadResultCache: LoadResultCache | undefined,
637656
): BuildOptions | undefined {
638657
const { jit, workspaceRoot, i18nOptions } = options;
639-
const buildOptions: BuildOptions = {
640-
...getEsBuildCommonOptions(options),
641-
splitting: false,
642-
plugins: [createSourcemapIgnorelistPlugin()],
643-
};
658+
659+
const buildOptions = getEsBuildCommonOptions(options);
660+
buildOptions.splitting = false;
661+
buildOptions.plugins ??= [];
644662

645663
let polyfills = options.polyfills ? [...options.polyfills] : [];
646664

@@ -668,14 +686,14 @@ function getEsBuildCommonPolyfillsOptions(
668686
needLocaleDataPlugin = true;
669687
}
670688
if (needLocaleDataPlugin) {
671-
buildOptions.plugins?.push(createAngularLocaleDataPlugin());
689+
buildOptions.plugins.push(createAngularLocaleDataPlugin());
672690
}
673691

674692
if (polyfills.length === 0) {
675693
return;
676694
}
677695

678-
buildOptions.plugins?.push(
696+
buildOptions.plugins.push(
679697
createVirtualModulePlugin({
680698
namespace,
681699
cache: loadResultCache,
@@ -736,29 +754,3 @@ function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string):
736754
.replace(/\\/g, '/')
737755
);
738756
}
739-
740-
function appendOptionsForExternalPackages(
741-
options: NormalizedApplicationBuildOptions,
742-
buildOptions: BuildOptions,
743-
): void {
744-
if (!options.externalPackages) {
745-
return;
746-
}
747-
748-
buildOptions.plugins ??= [];
749-
750-
// Package files affected by a customized loader should not be implicitly marked as external
751-
if (options.loaderExtensions || options.plugins || typeof options.externalPackages === 'object') {
752-
// Plugin must be added after custom plugins to ensure any added loader options are considered
753-
buildOptions.plugins.push(
754-
createExternalPackagesPlugin(
755-
options.externalPackages !== true ? options.externalPackages : undefined,
756-
),
757-
);
758-
759-
buildOptions.packages = undefined;
760-
} else {
761-
// Safe to use the packages external option directly
762-
buildOptions.packages = 'external';
763-
}
764-
}

0 commit comments

Comments
 (0)