Skip to content

Commit 89a05c1

Browse files
committed
refactor(@angular/build): move Angular compilation creation to build execution
The creation of the internal Angular compilation used by the internal esbuild plugin is now created within the main execution phase of the build process. This is causes no behavior changes but provides the infrastructure for several addition refactoring improvements including performance improvements of the HMR process. The private API for the `createCompilerPlugin` has kept its function signature and currently is not affected by this change. (cherry picked from commit 61d98fd)
1 parent 4405579 commit 89a05c1

File tree

6 files changed

+38
-25
lines changed

6 files changed

+38
-25
lines changed

packages/angular/build/src/builders/application/execute-build.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { BuilderContext } from '@angular-devkit/architect';
10+
import { createAngularCompilation } from '../../tools/angular/compilation';
1011
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
1112
import { generateBudgetStats } from '../../tools/esbuild/budget-stats';
1213
import {
@@ -108,6 +109,8 @@ export async function executeBuild(
108109
target,
109110
codeBundleCache,
110111
componentStyleBundler,
112+
// Create new reusable compilation for the appropriate mode based on the `jit` plugin option
113+
await createAngularCompilation(!!options.jit, !options.serverEntryPoint),
111114
templateUpdates,
112115
);
113116

packages/angular/build/src/builders/application/setup-bundling.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { AngularCompilation } from '../../tools/angular/compilation';
910
import { ComponentStylesheetBundler } from '../../tools/esbuild/angular/component-stylesheets';
1011
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
1112
import {
@@ -34,6 +35,7 @@ export function setupBundlerContexts(
3435
target: string[],
3536
codeBundleCache: SourceFileCache,
3637
stylesheetBundler: ComponentStylesheetBundler,
38+
angularCompilation: AngularCompilation,
3739
templateUpdates: Map<string, string> | undefined,
3840
): {
3941
typescriptContexts: BundlerContext[];
@@ -61,6 +63,7 @@ export function setupBundlerContexts(
6163
target,
6264
codeBundleCache,
6365
stylesheetBundler,
66+
angularCompilation,
6467
templateUpdates,
6568
),
6669
),

packages/angular/build/src/private.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* their existence may change in any future version.
1414
*/
1515

16+
import { NoopCompilation, createAngularCompilation } from './tools/angular/compilation';
1617
import {
1718
CompilerPluginOptions,
1819
createCompilerPlugin as internalCreateCompilerPlugin,
@@ -38,11 +39,17 @@ export { createJitResourceTransformer } from './tools/angular/transformers/jit-r
3839
export { JavaScriptTransformer } from './tools/esbuild/javascript-transformer';
3940

4041
export function createCompilerPlugin(
41-
pluginOptions: CompilerPluginOptions,
42+
pluginOptions: CompilerPluginOptions & {
43+
browserOnlyBuild?: boolean;
44+
noopTypeScriptCompilation?: boolean;
45+
},
4246
styleOptions: BundleStylesheetOptions & { inlineStyleLanguage: string },
4347
): import('esbuild').Plugin {
4448
return internalCreateCompilerPlugin(
4549
pluginOptions,
50+
pluginOptions.noopTypeScriptCompilation
51+
? new NoopCompilation()
52+
: () => createAngularCompilation(!!pluginOptions.jit, !!pluginOptions.browserOnlyBuild),
4653
new ComponentStylesheetBundler(
4754
styleOptions,
4855
styleOptions.inlineStyleLanguage,

packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ import { createHash } from 'node:crypto';
2121
import * as path from 'node:path';
2222
import { maxWorkers, useTypeChecking } from '../../../utils/environment-options';
2323
import { AngularHostOptions } from '../../angular/angular-host';
24-
import {
25-
AngularCompilation,
26-
DiagnosticModes,
27-
NoopCompilation,
28-
createAngularCompilation,
29-
} from '../../angular/compilation';
24+
import { AngularCompilation, DiagnosticModes, NoopCompilation } from '../../angular/compilation';
3025
import { JavaScriptTransformer } from '../javascript-transformer';
3126
import { LoadResultCache, createCachedLoad } from '../load-result-cache';
3227
import { logCumulativeDurations, profileAsync, resetCumulativeDurations } from '../profiling';
@@ -40,10 +35,7 @@ export interface CompilerPluginOptions {
4035
sourcemap: boolean | 'external';
4136
tsconfig: string;
4237
jit?: boolean;
43-
browserOnlyBuild?: boolean;
4438

45-
/** Skip TypeScript compilation setup. This is useful to re-use the TypeScript compilation from another plugin. */
46-
noopTypeScriptCompilation?: boolean;
4739
advancedOptimizations?: boolean;
4840
thirdPartySourcemaps?: boolean;
4941
fileReplacements?: Record<string, string>;
@@ -58,6 +50,7 @@ export interface CompilerPluginOptions {
5850
// eslint-disable-next-line max-lines-per-function
5951
export function createCompilerPlugin(
6052
pluginOptions: CompilerPluginOptions,
53+
compilationOrFactory: AngularCompilation | (() => Promise<AngularCompilation>),
6154
stylesheetBundler: ComponentStylesheetBundler,
6255
): Plugin {
6356
return {
@@ -105,6 +98,13 @@ export function createCompilerPlugin(
10598
build.initialOptions.define ??= {};
10699
build.initialOptions.define['ngI18nClosureMode'] ??= 'false';
107100

101+
// The factory is only relevant for compatibility purposes with the private API.
102+
// TODO: Update private API in the next major to allow compilation function factory removal here.
103+
const compilation =
104+
typeof compilationOrFactory === 'function'
105+
? await compilationOrFactory()
106+
: compilationOrFactory;
107+
108108
// The in-memory cache of TypeScript file outputs will be used during the build in `onLoad` callbacks for TS files.
109109
// A string value indicates direct TS/NG output and a Uint8Array indicates fully transformed code.
110110
const typeScriptFileCache =
@@ -117,10 +117,6 @@ export function createCompilerPlugin(
117117
{ outputFiles?: OutputFile[]; metafile?: Metafile; errors?: PartialMessage[] }
118118
>();
119119

120-
// Create new reusable compilation for the appropriate mode based on the `jit` plugin option
121-
const compilation: AngularCompilation = pluginOptions.noopTypeScriptCompilation
122-
? new NoopCompilation()
123-
: await createAngularCompilation(!!pluginOptions.jit, !!pluginOptions.browserOnlyBuild);
124120
// Compilation is initially assumed to have errors until emitted
125121
let hasCompilationErrors = true;
126122

@@ -153,8 +149,8 @@ export function createCompilerPlugin(
153149
// dependencies or web worker processing.
154150
let modifiedFiles;
155151
if (
156-
pluginOptions.sourceFileCache?.modifiedFiles.size &&
157-
!pluginOptions.noopTypeScriptCompilation
152+
!(compilation instanceof NoopCompilation) &&
153+
pluginOptions.sourceFileCache?.modifiedFiles.size
158154
) {
159155
// TODO: Differentiate between changed input files and stale output files
160156
modifiedFiles = referencedFileTracker.update(pluginOptions.sourceFileCache.modifiedFiles);
@@ -168,11 +164,7 @@ export function createCompilerPlugin(
168164
modifiedFiles.forEach((file) => additionalResults.delete(file));
169165
}
170166

171-
if (
172-
!pluginOptions.noopTypeScriptCompilation &&
173-
compilation.update &&
174-
pluginOptions.sourceFileCache?.modifiedFiles.size
175-
) {
167+
if (compilation.update && pluginOptions.sourceFileCache?.modifiedFiles.size) {
176168
await compilation.update(modifiedFiles ?? pluginOptions.sourceFileCache.modifiedFiles);
177169
}
178170

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
SERVER_APP_ENGINE_MANIFEST_FILENAME,
1818
SERVER_APP_MANIFEST_FILENAME,
1919
} from '../../utils/server-rendering/manifest';
20+
import { AngularCompilation, NoopCompilation } from '../angular/compilation';
2021
import { createCompilerPlugin } from './angular/compiler-plugin';
2122
import { ComponentStylesheetBundler } from './angular/component-stylesheets';
2223
import { SourceFileCache } from './angular/source-file-cache';
@@ -39,6 +40,7 @@ export function createBrowserCodeBundleOptions(
3940
target: string[],
4041
sourceFileCache: SourceFileCache,
4142
stylesheetBundler: ComponentStylesheetBundler,
43+
angularCompilation: AngularCompilation,
4244
templateUpdates: Map<string, string> | undefined,
4345
): BundlerOptionsFactory {
4446
return (loadCache) => {
@@ -73,6 +75,7 @@ export function createBrowserCodeBundleOptions(
7375
createCompilerPlugin(
7476
// JS/TS options
7577
pluginOptions,
78+
angularCompilation,
7679
// Component stylesheet bundler
7780
stylesheetBundler,
7881
),
@@ -135,7 +138,9 @@ export function createBrowserPolyfillBundleOptions(
135138
buildOptions.plugins.push(
136139
createCompilerPlugin(
137140
// JS/TS options
138-
{ ...pluginOptions, noopTypeScriptCompilation: true },
141+
pluginOptions,
142+
// Browser compilation handles the actual Angular code compilation
143+
new NoopCompilation(),
139144
// Component stylesheet options are unused for polyfills but required by the plugin
140145
stylesheetBundler,
141146
),
@@ -276,7 +281,9 @@ export function createServerMainCodeBundleOptions(
276281
createAngularLocalizeInitWarningPlugin(),
277282
createCompilerPlugin(
278283
// JS/TS options
279-
{ ...pluginOptions, noopTypeScriptCompilation: true },
284+
pluginOptions,
285+
// Browser compilation handles the actual Angular code compilation
286+
new NoopCompilation(),
280287
// Component stylesheet bundler
281288
stylesheetBundler,
282289
),
@@ -416,7 +423,9 @@ export function createSsrEntryCodeBundleOptions(
416423
createAngularLocalizeInitWarningPlugin(),
417424
createCompilerPlugin(
418425
// JS/TS options
419-
{ ...pluginOptions, noopTypeScriptCompilation: true },
426+
pluginOptions,
427+
// Browser compilation handles the actual Angular code compilation
428+
new NoopCompilation(),
420429
// Component stylesheet bundler
421430
stylesheetBundler,
422431
),

packages/angular/build/src/tools/esbuild/compiler-plugin-options.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export function createCompilerPluginOptions(
3131
const incremental = !!options.watch;
3232

3333
return {
34-
browserOnlyBuild: !options.serverEntryPoint,
3534
sourcemap: !!sourcemapOptions.scripts && (sourcemapOptions.hidden ? 'external' : true),
3635
thirdPartySourcemaps: sourcemapOptions.vendor,
3736
tsconfig,

0 commit comments

Comments
 (0)