forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundler-context.ts
480 lines (420 loc) · 14.8 KB
/
bundler-context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
BuildContext,
BuildFailure,
BuildOptions,
BuildResult,
Message,
Metafile,
OutputFile,
build,
context,
} from 'esbuild';
import assert from 'node:assert';
import { basename, extname, join, relative } from 'node:path';
import { LoadResultCache, MemoryLoadResultCache } from './load-result-cache';
import { SERVER_GENERATED_EXTERNALS, convertOutputFile } from './utils';
export type BundleContextResult =
| { errors: Message[]; warnings: Message[] }
| {
errors: undefined;
warnings: Message[];
metafile: Metafile;
outputFiles: BuildOutputFile[];
initialFiles: Map<string, InitialFileRecord>;
externalImports: {
server?: Set<string>;
browser?: Set<string>;
};
externalConfiguration?: string[];
};
export interface InitialFileRecord {
entrypoint: boolean;
name?: string;
type: 'script' | 'style';
external?: boolean;
serverFile: boolean;
depth: number;
}
export enum BuildOutputFileType {
Browser,
Media,
ServerApplication,
ServerRoot,
Root,
}
export interface BuildOutputFile extends OutputFile {
type: BuildOutputFileType;
readonly size: number;
clone: () => BuildOutputFile;
}
export type BundlerOptionsFactory<T extends BuildOptions = BuildOptions> = (
loadCache: LoadResultCache | undefined,
) => T;
/**
* Determines if an unknown value is an esbuild BuildFailure error object thrown by esbuild.
* @param value A potential esbuild BuildFailure error object.
* @returns `true` if the object is determined to be a BuildFailure object; otherwise, `false`.
*/
function isEsBuildFailure(value: unknown): value is BuildFailure {
return !!value && typeof value === 'object' && 'errors' in value && 'warnings' in value;
}
export class BundlerContext {
#esbuildContext?: BuildContext<{ metafile: true; write: false }>;
#esbuildOptions?: BuildOptions & { metafile: true; write: false };
#esbuildResult?: BundleContextResult;
#optionsFactory: BundlerOptionsFactory<BuildOptions & { metafile: true; write: false }>;
#shouldCacheResult: boolean;
#loadCache?: MemoryLoadResultCache;
readonly watchFiles = new Set<string>();
constructor(
private workspaceRoot: string,
private incremental: boolean,
options: BuildOptions | BundlerOptionsFactory,
private initialFilter?: (initial: Readonly<InitialFileRecord>) => boolean,
) {
// To cache the results an option factory is needed to capture the full set of dependencies
this.#shouldCacheResult = incremental && typeof options === 'function';
this.#optionsFactory = (...args) => {
const baseOptions = typeof options === 'function' ? options(...args) : options;
return {
...baseOptions,
metafile: true,
write: false,
};
};
}
static async bundleAll(
contexts: Iterable<BundlerContext>,
changedFiles?: Iterable<string>,
): Promise<BundleContextResult> {
const individualResults = await Promise.all(
[...contexts].map((context) => {
if (changedFiles) {
context.invalidate(changedFiles);
}
return context.bundle();
}),
);
return BundlerContext.mergeResults(individualResults);
}
static mergeResults(results: BundleContextResult[]): BundleContextResult {
// Return directly if only one result
if (results.length === 1) {
return results[0];
}
let errors: Message[] | undefined;
const warnings: Message[] = [];
const metafile: Metafile = { inputs: {}, outputs: {} };
const initialFiles = new Map<string, InitialFileRecord>();
const externalImportsBrowser = new Set<string>();
const externalImportsServer = new Set<string>();
const outputFiles = [];
let externalConfiguration;
for (const result of results) {
warnings.push(...result.warnings);
if (result.errors) {
errors ??= [];
errors.push(...result.errors);
continue;
}
// Combine metafiles used for the stats option as well as bundle budgets and console output
if (result.metafile) {
Object.assign(metafile.inputs, result.metafile.inputs);
Object.assign(metafile.outputs, result.metafile.outputs);
}
result.initialFiles.forEach((value, key) => initialFiles.set(key, value));
outputFiles.push(...result.outputFiles);
result.externalImports.browser?.forEach((value) => externalImportsBrowser.add(value));
result.externalImports.server?.forEach((value) => externalImportsServer.add(value));
if (result.externalConfiguration) {
externalConfiguration ??= new Set<string>();
for (const value of result.externalConfiguration) {
externalConfiguration.add(value);
}
}
}
if (errors !== undefined) {
return { errors, warnings };
}
return {
errors,
warnings,
metafile,
initialFiles,
outputFiles,
externalImports: {
browser: externalImportsBrowser,
server: externalImportsServer,
},
externalConfiguration: externalConfiguration ? [...externalConfiguration] : undefined,
};
}
/**
* Executes the esbuild build function and normalizes the build result in the event of a
* build failure that results in no output being generated.
* All builds use the `write` option with a value of `false` to allow for the output files
* build result array to be populated.
*
* @param force If true, always rebundle.
* @returns If output files are generated, the full esbuild BuildResult; if not, the
* warnings and errors for the attempted build.
*/
async bundle(force?: boolean): Promise<BundleContextResult> {
// Return existing result if present
if (!force && this.#esbuildResult) {
return this.#esbuildResult;
}
const result = await this.#performBundle();
if (this.#shouldCacheResult) {
this.#esbuildResult = result;
}
return result;
}
// eslint-disable-next-line max-lines-per-function
async #performBundle(): Promise<BundleContextResult> {
// Create esbuild options if not present
if (this.#esbuildOptions === undefined) {
if (this.incremental) {
this.#loadCache = new MemoryLoadResultCache();
}
this.#esbuildOptions = this.#optionsFactory(this.#loadCache);
}
if (this.incremental) {
this.watchFiles.clear();
}
let result: BuildResult<{ metafile: true; write: false }>;
try {
if (this.#esbuildContext) {
// Rebuild using the existing incremental build context
result = await this.#esbuildContext.rebuild();
} else if (this.incremental) {
// Create an incremental build context and perform the first build.
// Context creation does not perform a build.
this.#esbuildContext = await context(this.#esbuildOptions);
result = await this.#esbuildContext.rebuild();
} else {
// For non-incremental builds, perform a single build
result = await build(this.#esbuildOptions);
}
} catch (failure) {
// Build failures will throw an exception which contains errors/warnings
if (isEsBuildFailure(failure)) {
this.#addErrorsToWatch(failure);
return failure;
} else {
throw failure;
}
} finally {
if (this.incremental) {
// When incremental always add any files from the load result cache
if (this.#loadCache) {
for (const file of this.#loadCache.watchFiles) {
if (!isInternalAngularFile(file)) {
// watch files are fully resolved paths
this.watchFiles.add(file);
}
}
}
}
}
// Update files that should be watched.
// While this should technically not be linked to incremental mode, incremental is only
// currently enabled with watch mode where watch files are needed.
if (this.incremental) {
// Add input files except virtual angular files which do not exist on disk
for (const input of Object.keys(result.metafile.inputs)) {
if (!isInternalAngularFile(input)) {
// input file paths are always relative to the workspace root
this.watchFiles.add(join(this.workspaceRoot, input));
}
}
}
// Return if the build encountered any errors
if (result.errors.length) {
this.#addErrorsToWatch(result);
return {
errors: result.errors,
warnings: result.warnings,
};
}
const {
'ng-platform-server': isPlatformServer = false,
'ng-ssr-entry-bundle': isSsrEntryBundle = false,
} = result.metafile as Metafile & {
'ng-platform-server'?: boolean;
'ng-ssr-entry-bundle'?: boolean;
};
// Find all initial files
const initialFiles = new Map<string, InitialFileRecord>();
for (const outputFile of result.outputFiles) {
// Entries in the metafile are relative to the `absWorkingDir` option which is set to the workspaceRoot
const relativeFilePath = relative(this.workspaceRoot, outputFile.path);
const entryPoint = result.metafile.outputs[relativeFilePath]?.entryPoint;
outputFile.path = relativeFilePath;
if (entryPoint) {
// The first part of the filename is the name of file (e.g., "polyfills" for "polyfills-7S5G3MDY.js")
const name = basename(relativeFilePath).replace(/(?:-[\dA-Z]{8})?\.[a-z]{2,3}$/, '');
// Entry points are only styles or scripts
const type = extname(relativeFilePath) === '.css' ? 'style' : 'script';
// Only entrypoints with an entry in the options are initial files.
// Dynamic imports also have an entryPoint value in the meta file.
if ((this.#esbuildOptions.entryPoints as Record<string, string>)?.[name]) {
// An entryPoint value indicates an initial file
const record: InitialFileRecord = {
name,
type,
entrypoint: true,
serverFile: isPlatformServer,
depth: 0,
};
if (!this.initialFilter || this.initialFilter(record)) {
initialFiles.set(relativeFilePath, record);
}
}
}
}
// Analyze for transitive initial files
const entriesToAnalyze = [...initialFiles];
let currentEntry;
while ((currentEntry = entriesToAnalyze.pop())) {
const [entryPath, entryRecord] = currentEntry;
for (const initialImport of result.metafile.outputs[entryPath].imports) {
const existingRecord = initialFiles.get(initialImport.path);
if (existingRecord) {
// Store the smallest value depth
if (existingRecord.depth > entryRecord.depth + 1) {
existingRecord.depth = entryRecord.depth + 1;
}
continue;
}
if (initialImport.kind === 'import-statement' || initialImport.kind === 'import-rule') {
const record: InitialFileRecord = {
type: initialImport.kind === 'import-rule' ? 'style' : 'script',
entrypoint: false,
external: initialImport.external,
serverFile: isPlatformServer,
depth: entryRecord.depth + 1,
};
if (!this.initialFilter || this.initialFilter(record)) {
initialFiles.set(initialImport.path, record);
}
if (!initialImport.external) {
entriesToAnalyze.push([initialImport.path, record]);
}
}
}
}
// Collect all external package names
const externalImports = new Set<string>();
for (const { imports } of Object.values(result.metafile.outputs)) {
for (const { external, kind, path } of imports) {
if (
!external ||
SERVER_GENERATED_EXTERNALS.has(path) ||
(kind !== 'import-statement' && kind !== 'dynamic-import' && kind !== 'require-call')
) {
continue;
}
externalImports.add(path);
}
}
assert(this.#esbuildOptions, 'esbuild options cannot be undefined.');
const outputFiles = result.outputFiles.map((file) => {
let fileType: BuildOutputFileType;
// All files that are not JS, CSS, WASM, or sourcemaps for them are considered media
if (!/\.([cm]?js|css|wasm)(\.map)?$/i.test(file.path)) {
fileType = BuildOutputFileType.Media;
} else if (isPlatformServer) {
fileType = isSsrEntryBundle
? BuildOutputFileType.ServerRoot
: BuildOutputFileType.ServerApplication;
} else {
fileType = BuildOutputFileType.Browser;
}
return convertOutputFile(file, fileType);
});
let externalConfiguration = this.#esbuildOptions.external;
if (isPlatformServer && externalConfiguration) {
externalConfiguration = externalConfiguration.filter(
(dep) => !SERVER_GENERATED_EXTERNALS.has(dep),
);
if (!externalConfiguration.length) {
externalConfiguration = undefined;
}
}
// Return the successful build results
return {
...result,
outputFiles,
initialFiles,
externalImports: {
[isPlatformServer ? 'server' : 'browser']: externalImports,
},
externalConfiguration,
errors: undefined,
};
}
#addErrorsToWatch(result: BuildFailure | BuildResult): void {
for (const error of result.errors) {
let file = error.location?.file;
if (file && !isInternalAngularFile(file)) {
this.watchFiles.add(join(this.workspaceRoot, file));
}
for (const note of error.notes) {
file = note.location?.file;
if (file && !isInternalAngularFile(file)) {
this.watchFiles.add(join(this.workspaceRoot, file));
}
}
}
}
/**
* Invalidate a stored bundler result based on the previous watch files
* and a list of changed files.
* The context must be created with incremental mode enabled for results
* to be stored.
* @returns True, if the result was invalidated; False, otherwise.
*/
invalidate(files: Iterable<string>): boolean {
if (!this.incremental) {
return false;
}
let invalid = false;
for (const file of files) {
if (this.#loadCache?.invalidate(file)) {
invalid = true;
continue;
}
invalid ||= this.watchFiles.has(file);
}
if (invalid) {
this.#esbuildResult = undefined;
}
return invalid;
}
/**
* Disposes incremental build resources present in the context.
*
* @returns A promise that resolves when disposal is complete.
*/
async dispose(): Promise<void> {
try {
this.#esbuildOptions = undefined;
this.#esbuildResult = undefined;
this.#loadCache = undefined;
await this.#esbuildContext?.dispose();
} finally {
this.#esbuildContext = undefined;
}
}
}
function isInternalAngularFile(file: string) {
return file.startsWith('angular:');
}