-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathutils.ts
474 lines (416 loc) · 14.3 KB
/
utils.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
/**
* @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.io/license
*/
import { logging } from '@angular-devkit/core';
import { BuildOptions, Metafile, OutputFile, formatMessages } from 'esbuild';
import { createHash } from 'node:crypto';
import { constants as fsConstants } from 'node:fs';
import fs from 'node:fs/promises';
import { basename, dirname, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { brotliCompress } from 'node:zlib';
import { coerce } from 'semver';
import {
NormalizedApplicationBuildOptions,
NormalizedOutputOptions,
} from '../../builders/application/options';
import { BudgetCalculatorResult } from '../../utils/bundle-calculator';
import { Spinner } from '../../utils/spinner';
import { BundleStats, generateEsbuildBuildStatsTable } from '../webpack/utils/stats';
import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundler-context';
import { BuildOutputAsset, ExecutionResult } from './bundler-execution-result';
export function logBuildStats(
metafile: Metafile,
initial: Map<string, InitialFileRecord>,
budgetFailures: BudgetCalculatorResult[] | undefined,
colors: boolean,
changedFiles?: Set<string>,
estimatedTransferSizes?: Map<string, number>,
ssrOutputEnabled?: boolean,
verbose?: boolean,
): string {
const browserStats: BundleStats[] = [];
const serverStats: BundleStats[] = [];
let unchangedCount = 0;
for (const [file, output] of Object.entries(metafile.outputs)) {
// Only display JavaScript and CSS files
if (!/\.(?:css|m?js)$/.test(file)) {
continue;
}
// Skip internal component resources
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((output as any)['ng-component']) {
continue;
}
// Show only changed files if a changed list is provided
if (changedFiles && !changedFiles.has(file)) {
++unchangedCount;
continue;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isPlatformServer = (output as any)['ng-platform-server'];
if (isPlatformServer && !ssrOutputEnabled) {
// Only log server build stats when SSR is enabled.
continue;
}
let name = initial.get(file)?.name;
if (name === undefined && output.entryPoint) {
name = basename(output.entryPoint)
.replace(/\.[cm]?[jt]s$/, '')
.replace(/[\\/.]/g, '-');
}
const stat: BundleStats = {
initial: initial.has(file),
stats: [file, name ?? '-', output.bytes, estimatedTransferSizes?.get(file) ?? '-'],
};
if (isPlatformServer) {
serverStats.push(stat);
} else {
browserStats.push(stat);
}
}
if (browserStats.length > 0 || serverStats.length > 0) {
const tableText = generateEsbuildBuildStatsTable(
[browserStats, serverStats],
colors,
unchangedCount === 0,
!!estimatedTransferSizes,
budgetFailures,
verbose,
);
return tableText + '\n';
} else if (changedFiles !== undefined) {
return '\nNo output file changes.\n';
}
if (unchangedCount > 0) {
return `Unchanged output files: ${unchangedCount}`;
}
return '';
}
export async function calculateEstimatedTransferSizes(
outputFiles: OutputFile[],
): Promise<Map<string, number>> {
const sizes = new Map<string, number>();
if (outputFiles.length <= 0) {
return sizes;
}
return new Promise((resolve, reject) => {
let completeCount = 0;
for (const outputFile of outputFiles) {
// Only calculate JavaScript and CSS files
if (!outputFile.path.endsWith('.js') && !outputFile.path.endsWith('.css')) {
++completeCount;
continue;
}
// Skip compressing small files which may end being larger once compressed and will most likely not be
// compressed in actual transit.
if (outputFile.contents.byteLength < 1024) {
sizes.set(outputFile.path, outputFile.contents.byteLength);
++completeCount;
continue;
}
// Directly use the async callback function to minimize the number of Promises that need to be created.
brotliCompress(outputFile.contents, (error, result) => {
if (error) {
reject(error);
return;
}
sizes.set(outputFile.path, result.byteLength);
if (++completeCount >= outputFiles.length) {
resolve(sizes);
}
});
}
// Covers the case where no files need to be compressed
if (completeCount >= outputFiles.length) {
resolve(sizes);
}
});
}
export async function withSpinner<T>(text: string, action: () => T | Promise<T>): Promise<T> {
const spinner = new Spinner(text);
spinner.start();
try {
return await action();
} finally {
spinner.stop();
}
}
export async function withNoProgress<T>(text: string, action: () => T | Promise<T>): Promise<T> {
return action();
}
/**
* Generates a syntax feature object map for Angular applications based on a list of targets.
* A full set of feature names can be found here: https://esbuild.github.io/api/#supported
* @param target An array of browser/engine targets in the format accepted by the esbuild `target` option.
* @returns An object that can be used with the esbuild build `supported` option.
*/
export function getFeatureSupport(target: string[]): BuildOptions['supported'] {
const supported: Record<string, boolean> = {
// Native async/await is not supported with Zone.js. Disabling support here will cause
// esbuild to downlevel async/await, async generators, and for await...of to a Zone.js supported form.
'async-await': false,
// V8 currently has a performance defect involving object spread operations that can cause signficant
// degradation in runtime performance. By not supporting the language feature here, a downlevel form
// will be used instead which provides a workaround for the performance issue.
// For more details: https://bugs.chromium.org/p/v8/issues/detail?id=11536
'object-rest-spread': false,
};
// Detect Safari browser versions that have a class field behavior bug
// See: https://github.com/angular/angular-cli/issues/24355#issuecomment-1333477033
// See: https://github.com/WebKit/WebKit/commit/e8788a34b3d5f5b4edd7ff6450b80936bff396f2
let safariClassFieldScopeBug = false;
for (const browser of target) {
let majorVersion;
if (browser.startsWith('ios')) {
majorVersion = Number(browser.slice(3, 5));
} else if (browser.startsWith('safari')) {
majorVersion = Number(browser.slice(6, 8));
} else {
continue;
}
// Technically, 14.0 is not broken but rather does not have support. However, the behavior
// is identical since it would be set to false by esbuild if present as a target.
if (majorVersion === 14 || majorVersion === 15) {
safariClassFieldScopeBug = true;
break;
}
}
// If class field support cannot be used set to false; otherwise leave undefined to allow
// esbuild to use `target` to determine support.
if (safariClassFieldScopeBug) {
supported['class-field'] = false;
supported['class-static-field'] = false;
}
return supported;
}
export async function writeResultFiles(
outputFiles: BuildOutputFile[],
assetFiles: BuildOutputAsset[] | undefined,
{ base, browser, media, server }: NormalizedOutputOptions,
) {
const directoryExists = new Set<string>();
const ensureDirectoryExists = async (destPath: string) => {
const basePath = dirname(destPath);
if (!directoryExists.has(basePath)) {
await fs.mkdir(join(base, basePath), { recursive: true });
directoryExists.add(basePath);
}
};
// Writes the output file to disk and ensures the containing directories are present
await emitFilesToDisk(outputFiles, async (file: BuildOutputFile) => {
let outputDir: string;
switch (file.type) {
case BuildOutputFileType.Browser:
case BuildOutputFileType.Media:
outputDir = browser;
break;
case BuildOutputFileType.Server:
outputDir = server;
break;
case BuildOutputFileType.Root:
outputDir = '';
break;
default:
throw new Error(
`Unhandled write for file "${file.path}" with type "${BuildOutputFileType[file.type]}".`,
);
}
const destPath = join(outputDir, file.path);
// Ensure output subdirectories exist
await ensureDirectoryExists(destPath);
// Write file contents
await fs.writeFile(join(base, destPath), file.contents);
});
if (assetFiles?.length) {
await emitFilesToDisk(assetFiles, async ({ source, destination }) => {
const destPath = join(browser, destination);
// Ensure output subdirectories exist
await ensureDirectoryExists(destPath);
// Copy file contents
await fs.copyFile(source, join(base, destPath), fsConstants.COPYFILE_FICLONE);
});
}
}
const MAX_CONCURRENT_WRITES = 64;
export async function emitFilesToDisk<T = BuildOutputAsset | BuildOutputFile>(
files: T[],
writeFileCallback: (file: T) => Promise<void>,
): Promise<void> {
// Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
for (let fileIndex = 0; fileIndex < files.length; ) {
const groupMax = Math.min(fileIndex + MAX_CONCURRENT_WRITES, files.length);
const actions = [];
while (fileIndex < groupMax) {
actions.push(writeFileCallback(files[fileIndex++]));
}
await Promise.all(actions);
}
}
export function createOutputFileFromText(
path: string,
text: string,
type: BuildOutputFileType,
): BuildOutputFile {
return {
path,
text,
type,
get hash() {
return createHash('sha256').update(this.text).digest('hex');
},
get contents() {
return Buffer.from(this.text, 'utf-8');
},
clone(): BuildOutputFile {
return createOutputFileFromText(this.path, this.text, this.type);
},
};
}
export function createOutputFileFromData(
path: string,
data: Uint8Array,
type: BuildOutputFileType,
): BuildOutputFile {
return {
path,
type,
get text() {
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString('utf-8');
},
get hash() {
return createHash('sha256').update(this.text).digest('hex');
},
get contents() {
return data;
},
clone(): BuildOutputFile {
return createOutputFileFromData(this.path, this.contents, this.type);
},
};
}
export function convertOutputFile(file: OutputFile, type: BuildOutputFileType): BuildOutputFile {
const { path, contents, hash } = file;
return {
contents,
hash,
path,
type,
get text() {
return Buffer.from(
this.contents.buffer,
this.contents.byteOffset,
this.contents.byteLength,
).toString('utf-8');
},
clone(): BuildOutputFile {
return convertOutputFile(this, this.type);
},
};
}
/**
* Transform browserlists result to esbuild target.
* @see https://esbuild.github.io/api/#target
*/
export function transformSupportedBrowsersToTargets(supportedBrowsers: string[]): string[] {
const transformed: string[] = [];
// https://esbuild.github.io/api/#target
const esBuildSupportedBrowsers = new Set([
'chrome',
'edge',
'firefox',
'ie',
'ios',
'node',
'opera',
'safari',
]);
for (const browser of supportedBrowsers) {
let [browserName, version] = browser.toLowerCase().split(' ');
// browserslist uses the name `ios_saf` for iOS Safari whereas esbuild uses `ios`
if (browserName === 'ios_saf') {
browserName = 'ios';
}
// browserslist uses ranges `15.2-15.3` versions but only the lowest is required
// to perform minimum supported feature checks. esbuild also expects a single version.
[version] = version.split('-');
if (esBuildSupportedBrowsers.has(browserName)) {
if (browserName === 'safari' && version === 'tp') {
// esbuild only supports numeric versions so `TP` is converted to a high number (999) since
// a Technology Preview (TP) of Safari is assumed to support all currently known features.
version = '999';
} else if (!version.includes('.')) {
// A lone major version is considered by esbuild to include all minor versions. However,
// browserslist does not and is also inconsistent in its `.0` version naming. For example,
// Safari 15.0 is named `safari 15` but Safari 16.0 is named `safari 16.0`.
version += '.0';
}
transformed.push(browserName + version);
}
}
return transformed;
}
const SUPPORTED_NODE_VERSIONS = '0.0.0-ENGINES-NODE';
/**
* Transform supported Node.js versions to esbuild target.
* @see https://esbuild.github.io/api/#target
*/
export function getSupportedNodeTargets(): string[] {
if (SUPPORTED_NODE_VERSIONS.charAt(0) === '0') {
// Unlike `pkg_npm`, `ts_library` which is used to run unit tests does not support substitutions.
return [];
}
return SUPPORTED_NODE_VERSIONS.split('||').map((v) => 'node' + coerce(v)?.version);
}
interface BuildManifest {
errors: string[];
warnings: string[];
outputPaths: {
root: URL;
server?: URL | undefined;
browser: URL;
};
prerenderedRoutes?: string[];
}
export async function logMessages(
logger: logging.LoggerApi,
executionResult: ExecutionResult,
options: NormalizedApplicationBuildOptions,
): Promise<void> {
const {
outputOptions: { base, server, browser },
ssrOptions,
jsonLogs,
colors: color,
} = options;
const { warnings, errors, prerenderedRoutes } = executionResult;
const warningMessages = warnings.length
? await formatMessages(warnings, { kind: 'warning', color })
: [];
const errorMessages = errors.length ? await formatMessages(errors, { kind: 'error', color }) : [];
if (jsonLogs) {
// JSON format output
const manifest: BuildManifest = {
errors: errorMessages,
warnings: warningMessages,
outputPaths: {
root: pathToFileURL(base),
browser: pathToFileURL(join(base, browser)),
server: ssrOptions ? pathToFileURL(join(base, server)) : undefined,
},
prerenderedRoutes,
};
logger.info(JSON.stringify(manifest, undefined, 2));
return;
}
if (warningMessages.length) {
logger.warn(warningMessages.join('\n'));
}
if (errorMessages.length) {
logger.error(errorMessages.join('\n'));
}
}