Skip to content

Commit 433aef9

Browse files
clydindgp1130
authored andcommitted
refactor(@angular-devkit/build-angular): move bundler context setup into separate file
To reduce the amount of code within the main `application` builder execution function, the bundler context setup has been moved into a separate file. This also reduces the amount of imports within the main execution function's module. (cherry picked from commit 2d260ea)
1 parent 2a02b13 commit 433aef9

File tree

2 files changed

+133
-102
lines changed

2 files changed

+133
-102
lines changed

packages/angular_devkit/build_angular/src/builders/application/execute-build.ts

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@
88

99
import { BuilderContext } from '@angular-devkit/architect';
1010
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
11-
import {
12-
createBrowserCodeBundleOptions,
13-
createBrowserPolyfillBundleOptions,
14-
createServerCodeBundleOptions,
15-
createServerPolyfillBundleOptions,
16-
} from '../../tools/esbuild/application-code-bundle';
1711
import { generateBudgetStats } from '../../tools/esbuild/budget-stats';
1812
import { BuildOutputFileType, BundlerContext } from '../../tools/esbuild/bundler-context';
1913
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
2014
import { checkCommonJSModules } from '../../tools/esbuild/commonjs-checker';
21-
import { createGlobalScriptsBundleOptions } from '../../tools/esbuild/global-scripts';
22-
import { createGlobalStylesBundleOptions } from '../../tools/esbuild/global-styles';
2315
import { extractLicenses } from '../../tools/esbuild/license-extractor';
2416
import {
2517
calculateEstimatedTransferSizes,
26-
getSupportedNodeTargets,
2718
logBuildStats,
2819
logMessages,
29-
transformSupportedBrowsersToTargets,
3020
} from '../../tools/esbuild/utils';
3121
import { BudgetCalculatorResult, checkBudgets } from '../../utils/bundle-calculator';
3222
import { colors } from '../../utils/color';
@@ -35,8 +25,8 @@ import { getSupportedBrowsers } from '../../utils/supported-browsers';
3525
import { executePostBundleSteps } from './execute-post-bundle';
3626
import { inlineI18n, loadActiveTranslations } from './i18n';
3727
import { NormalizedApplicationBuildOptions } from './options';
28+
import { setupBundlerContexts } from './setup-bundling';
3829

39-
// eslint-disable-next-line max-lines-per-function
4030
export async function executeBuild(
4131
options: NormalizedApplicationBuildOptions,
4232
context: BuilderContext,
@@ -47,16 +37,13 @@ export async function executeBuild(
4737
workspaceRoot,
4838
i18nOptions,
4939
optimizationOptions,
50-
serverEntryPoint,
5140
assets,
5241
cacheOptions,
5342
prerenderOptions,
54-
appShellOptions,
55-
ssrOptions,
5643
} = options;
5744

45+
// TODO: Consider integrating into watch mode. Would require full rebuild on target changes.
5846
const browsers = getSupportedBrowsers(projectRoot, context.logger);
59-
const target = transformSupportedBrowsersToTargets(browsers);
6047

6148
// Load active translations if inlining
6249
// TODO: Integrate into watch mode and only load changed translations
@@ -70,93 +57,7 @@ export async function executeBuild(
7057
rebuildState?.codeBundleCache ??
7158
new SourceFileCache(cacheOptions.enabled ? cacheOptions.path : undefined);
7259
if (bundlerContexts === undefined) {
73-
bundlerContexts = [];
74-
75-
// Browser application code
76-
bundlerContexts.push(
77-
new BundlerContext(
78-
workspaceRoot,
79-
!!options.watch,
80-
createBrowserCodeBundleOptions(options, target, codeBundleCache),
81-
),
82-
);
83-
84-
// Browser polyfills code
85-
const browserPolyfillBundleOptions = createBrowserPolyfillBundleOptions(
86-
options,
87-
target,
88-
codeBundleCache,
89-
);
90-
if (browserPolyfillBundleOptions) {
91-
bundlerContexts.push(
92-
new BundlerContext(workspaceRoot, !!options.watch, browserPolyfillBundleOptions),
93-
);
94-
}
95-
96-
// Global Stylesheets
97-
if (options.globalStyles.length > 0) {
98-
for (const initial of [true, false]) {
99-
const bundleOptions = createGlobalStylesBundleOptions(options, target, initial);
100-
if (bundleOptions) {
101-
bundlerContexts.push(
102-
new BundlerContext(workspaceRoot, !!options.watch, bundleOptions, () => initial),
103-
);
104-
}
105-
}
106-
}
107-
108-
// Global Scripts
109-
if (options.globalScripts.length > 0) {
110-
for (const initial of [true, false]) {
111-
const bundleOptions = createGlobalScriptsBundleOptions(options, target, initial);
112-
if (bundleOptions) {
113-
bundlerContexts.push(
114-
new BundlerContext(workspaceRoot, !!options.watch, bundleOptions, () => initial),
115-
);
116-
}
117-
}
118-
}
119-
120-
// Skip server build when none of the features are enabled.
121-
if (serverEntryPoint && (prerenderOptions || appShellOptions || ssrOptions)) {
122-
const nodeTargets = [...target, ...getSupportedNodeTargets()];
123-
// Server application code
124-
bundlerContexts.push(
125-
new BundlerContext(
126-
workspaceRoot,
127-
!!options.watch,
128-
createServerCodeBundleOptions(
129-
{
130-
...options,
131-
// Disable external deps for server bundles.
132-
// This is because it breaks Vite 'optimizeDeps' for SSR.
133-
externalPackages: false,
134-
},
135-
nodeTargets,
136-
codeBundleCache,
137-
),
138-
() => false,
139-
),
140-
);
141-
142-
// Server polyfills code
143-
const serverPolyfillBundleOptions = createServerPolyfillBundleOptions(
144-
options,
145-
nodeTargets,
146-
codeBundleCache,
147-
);
148-
149-
if (serverPolyfillBundleOptions) {
150-
bundlerContexts.push(
151-
new BundlerContext(
152-
workspaceRoot,
153-
!!options.watch,
154-
serverPolyfillBundleOptions,
155-
() => false,
156-
),
157-
);
158-
}
159-
}
60+
bundlerContexts = setupBundlerContexts(options, browsers, codeBundleCache);
16061
}
16162

16263
const bundlingResult = await BundlerContext.bundleAll(
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
10+
import {
11+
createBrowserCodeBundleOptions,
12+
createBrowserPolyfillBundleOptions,
13+
createServerCodeBundleOptions,
14+
createServerPolyfillBundleOptions,
15+
} from '../../tools/esbuild/application-code-bundle';
16+
import { BundlerContext } from '../../tools/esbuild/bundler-context';
17+
import { createGlobalScriptsBundleOptions } from '../../tools/esbuild/global-scripts';
18+
import { createGlobalStylesBundleOptions } from '../../tools/esbuild/global-styles';
19+
import {
20+
getSupportedNodeTargets,
21+
transformSupportedBrowsersToTargets,
22+
} from '../../tools/esbuild/utils';
23+
import { NormalizedApplicationBuildOptions } from './options';
24+
25+
/**
26+
* Generates one or more BundlerContext instances based on the builder provided
27+
* configuration.
28+
* @param options The normalized application builder options to use.
29+
* @param browsers An string array of browserslist browsers to support.
30+
* @param codeBundleCache An instance of the TypeScript source file cache.
31+
* @returns An array of BundlerContext objects.
32+
*/
33+
export function setupBundlerContexts(
34+
options: NormalizedApplicationBuildOptions,
35+
browsers: string[],
36+
codeBundleCache: SourceFileCache,
37+
): BundlerContext[] {
38+
const { appShellOptions, prerenderOptions, serverEntryPoint, ssrOptions, workspaceRoot } =
39+
options;
40+
const target = transformSupportedBrowsersToTargets(browsers);
41+
const bundlerContexts = [];
42+
43+
// Browser application code
44+
bundlerContexts.push(
45+
new BundlerContext(
46+
workspaceRoot,
47+
!!options.watch,
48+
createBrowserCodeBundleOptions(options, target, codeBundleCache),
49+
),
50+
);
51+
52+
// Browser polyfills code
53+
const browserPolyfillBundleOptions = createBrowserPolyfillBundleOptions(
54+
options,
55+
target,
56+
codeBundleCache,
57+
);
58+
if (browserPolyfillBundleOptions) {
59+
bundlerContexts.push(
60+
new BundlerContext(workspaceRoot, !!options.watch, browserPolyfillBundleOptions),
61+
);
62+
}
63+
64+
// Global Stylesheets
65+
if (options.globalStyles.length > 0) {
66+
for (const initial of [true, false]) {
67+
const bundleOptions = createGlobalStylesBundleOptions(options, target, initial);
68+
if (bundleOptions) {
69+
bundlerContexts.push(
70+
new BundlerContext(workspaceRoot, !!options.watch, bundleOptions, () => initial),
71+
);
72+
}
73+
}
74+
}
75+
76+
// Global Scripts
77+
if (options.globalScripts.length > 0) {
78+
for (const initial of [true, false]) {
79+
const bundleOptions = createGlobalScriptsBundleOptions(options, target, initial);
80+
if (bundleOptions) {
81+
bundlerContexts.push(
82+
new BundlerContext(workspaceRoot, !!options.watch, bundleOptions, () => initial),
83+
);
84+
}
85+
}
86+
}
87+
88+
// Skip server build when none of the features are enabled.
89+
if (serverEntryPoint && (prerenderOptions || appShellOptions || ssrOptions)) {
90+
const nodeTargets = [...target, ...getSupportedNodeTargets()];
91+
// Server application code
92+
bundlerContexts.push(
93+
new BundlerContext(
94+
workspaceRoot,
95+
!!options.watch,
96+
createServerCodeBundleOptions(
97+
{
98+
...options,
99+
// Disable external deps for server bundles.
100+
// This is because it breaks Vite 'optimizeDeps' for SSR.
101+
externalPackages: false,
102+
},
103+
nodeTargets,
104+
codeBundleCache,
105+
),
106+
() => false,
107+
),
108+
);
109+
110+
// Server polyfills code
111+
const serverPolyfillBundleOptions = createServerPolyfillBundleOptions(
112+
options,
113+
nodeTargets,
114+
codeBundleCache,
115+
);
116+
117+
if (serverPolyfillBundleOptions) {
118+
bundlerContexts.push(
119+
new BundlerContext(
120+
workspaceRoot,
121+
!!options.watch,
122+
serverPolyfillBundleOptions,
123+
() => false,
124+
),
125+
);
126+
}
127+
}
128+
129+
return bundlerContexts;
130+
}

0 commit comments

Comments
 (0)