|
8 | 8 |
|
9 | 9 | import { BuilderContext } from '@angular-devkit/architect'; |
10 | 10 | import { createAngularCompilation } from '../../tools/angular/compilation'; |
| 11 | +import { AngularCompilationContext } from '../../tools/esbuild/angular/compilation-state'; |
11 | 12 | import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache'; |
12 | 13 | import { generateBudgetStats } from '../../tools/esbuild/budget-stats'; |
13 | 14 | import { BundleContextResult, BundlerContext } from '../../tools/esbuild/bundler-context'; |
@@ -71,61 +72,74 @@ export async function executeBuild( |
71 | 72 | let codeBundleCache; |
72 | 73 | let bundlingResult: BundleContextResult; |
73 | 74 | let templateUpdates: Map<string, string> | undefined; |
74 | | - if (rebuildState) { |
75 | | - bundlerContexts = rebuildState.rebuildContexts; |
76 | | - componentStyleBundler = rebuildState.componentStyleBundler; |
77 | | - codeBundleCache = rebuildState.codeBundleCache; |
78 | | - templateUpdates = rebuildState.templateUpdates; |
79 | | - // Reset template updates for new rebuild |
80 | | - templateUpdates?.clear(); |
81 | | - |
82 | | - const allFileChanges = rebuildState.fileChanges.all; |
83 | | - |
84 | | - // Bundle all contexts that do not require TypeScript changed file checks. |
85 | | - // These will automatically use cached results based on the changed files. |
86 | | - bundlingResult = await BundlerContext.bundleAll(bundlerContexts.otherContexts, allFileChanges); |
87 | | - |
88 | | - // Check the TypeScript code bundling cache for changes. If invalid, force a rebundle of |
89 | | - // all TypeScript related contexts. |
90 | | - const forceTypeScriptRebuild = codeBundleCache?.invalidate(allFileChanges); |
91 | | - const typescriptResults: BundleContextResult[] = []; |
92 | | - for (const typescriptContext of bundlerContexts.typescriptContexts) { |
93 | | - typescriptContext.invalidate(allFileChanges); |
94 | | - const result = await typescriptContext.bundle(forceTypeScriptRebuild); |
95 | | - typescriptResults.push(result); |
96 | | - } |
97 | | - bundlingResult = BundlerContext.mergeResults([bundlingResult, ...typescriptResults]); |
98 | | - } else { |
99 | | - const target = transformSupportedBrowsersToTargets(browsers); |
100 | | - codeBundleCache = new SourceFileCache(cacheOptions.enabled ? cacheOptions.path : undefined); |
101 | | - componentStyleBundler = createComponentStyleBundler(options, target); |
102 | | - if (options.templateUpdates) { |
103 | | - templateUpdates = new Map<string, string>(); |
104 | | - } |
105 | | - bundlerContexts = setupBundlerContexts( |
106 | | - options, |
107 | | - target, |
108 | | - codeBundleCache, |
109 | | - componentStyleBundler, |
110 | | - // Create new reusable compilation for the appropriate mode based on the `jit` plugin option |
111 | | - await createAngularCompilation(!!options.jit, !options.serverEntryPoint), |
112 | | - templateUpdates, |
113 | | - ); |
| 75 | + let angularCompilationContext: AngularCompilationContext | undefined; |
| 76 | + try { |
| 77 | + if (rebuildState) { |
| 78 | + bundlerContexts = rebuildState.rebuildContexts; |
| 79 | + componentStyleBundler = rebuildState.componentStyleBundler; |
| 80 | + codeBundleCache = rebuildState.codeBundleCache; |
| 81 | + templateUpdates = rebuildState.templateUpdates; |
| 82 | + // Reset template updates for new rebuild |
| 83 | + templateUpdates?.clear(); |
| 84 | + |
| 85 | + const allFileChanges = rebuildState.fileChanges.all; |
| 86 | + |
| 87 | + // Bundle all contexts that do not require TypeScript changed file checks. |
| 88 | + // These will automatically use cached results based on the changed files. |
| 89 | + bundlingResult = await BundlerContext.bundleAll( |
| 90 | + bundlerContexts.otherContexts, |
| 91 | + allFileChanges, |
| 92 | + ); |
114 | 93 |
|
115 | | - // Bundle everything on initial build |
116 | | - bundlingResult = await BundlerContext.bundleAll([ |
117 | | - ...bundlerContexts.typescriptContexts, |
118 | | - ...bundlerContexts.otherContexts, |
119 | | - ]); |
120 | | - } |
| 94 | + // Check the TypeScript code bundling cache for changes. If invalid, force a rebundle of |
| 95 | + // all TypeScript related contexts. |
| 96 | + const forceTypeScriptRebuild = codeBundleCache?.invalidate(allFileChanges); |
| 97 | + const typescriptResults: BundleContextResult[] = []; |
| 98 | + for (const typescriptContext of bundlerContexts.typescriptContexts) { |
| 99 | + typescriptContext.invalidate(allFileChanges); |
| 100 | + const result = await typescriptContext.bundle(forceTypeScriptRebuild); |
| 101 | + typescriptResults.push(result); |
| 102 | + } |
| 103 | + bundlingResult = BundlerContext.mergeResults([bundlingResult, ...typescriptResults]); |
| 104 | + } else { |
| 105 | + const target = transformSupportedBrowsersToTargets(browsers); |
| 106 | + codeBundleCache = new SourceFileCache(cacheOptions.enabled ? cacheOptions.path : undefined); |
| 107 | + componentStyleBundler = createComponentStyleBundler(options, target); |
| 108 | + if (options.templateUpdates) { |
| 109 | + templateUpdates = new Map<string, string>(); |
| 110 | + } |
| 111 | + const angularCompilation = await createAngularCompilation( |
| 112 | + !!options.jit, |
| 113 | + !options.serverEntryPoint, |
| 114 | + ); |
| 115 | + angularCompilationContext = new AngularCompilationContext(angularCompilation); |
| 116 | + bundlerContexts = setupBundlerContexts( |
| 117 | + options, |
| 118 | + target, |
| 119 | + codeBundleCache, |
| 120 | + componentStyleBundler, |
| 121 | + angularCompilationContext, |
| 122 | + templateUpdates, |
| 123 | + ); |
121 | 124 |
|
122 | | - // Update any external component styles if enabled and rebuilding. |
123 | | - // TODO: Only attempt rebundling of invalidated styles once incremental build results are supported. |
124 | | - if (rebuildState && options.externalRuntimeStyles) { |
125 | | - componentStyleBundler.invalidate(rebuildState.fileChanges.all); |
| 125 | + // Bundle everything on initial build |
| 126 | + bundlingResult = await BundlerContext.bundleAll([ |
| 127 | + ...bundlerContexts.typescriptContexts, |
| 128 | + ...bundlerContexts.otherContexts, |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + // Update any external component styles if enabled and rebuilding. |
| 133 | + // TODO: Only attempt rebundling of invalidated styles once incremental build results are supported. |
| 134 | + if (rebuildState && options.externalRuntimeStyles) { |
| 135 | + componentStyleBundler.invalidate(rebuildState.fileChanges.all); |
126 | 136 |
|
127 | | - const componentResults = await componentStyleBundler.bundleAllFiles(true, true); |
128 | | - bundlingResult = BundlerContext.mergeResults([bundlingResult, ...componentResults]); |
| 137 | + const componentResults = await componentStyleBundler.bundleAllFiles(true, true); |
| 138 | + bundlingResult = BundlerContext.mergeResults([bundlingResult, ...componentResults]); |
| 139 | + } |
| 140 | + } catch (error) { |
| 141 | + await angularCompilationContext?.dispose(); |
| 142 | + throw error; |
129 | 143 | } |
130 | 144 |
|
131 | 145 | const executionResult = new ExecutionResult( |
|
0 commit comments