forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication-code-bundle.ts
501 lines (448 loc) · 16.4 KB
/
application-code-bundle.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* @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 type { BuildOptions } from 'esbuild';
import assert from 'node:assert';
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { extname, join } from 'node:path';
import type { NormalizedApplicationBuildOptions } from '../../builders/application/options';
import { allowMangle } from '../../utils/environment-options';
import { createCompilerPlugin } from './angular/compiler-plugin';
import { SourceFileCache } from './angular/source-file-cache';
import { BundlerOptionsFactory } from './bundler-context';
import { createCompilerPluginOptions } from './compiler-plugin-options';
import { createExternalPackagesPlugin } from './external-packages-plugin';
import { createAngularLocaleDataPlugin } from './i18n-locale-plugin';
import { createRxjsEsmResolutionPlugin } from './rxjs-esm-resolution-plugin';
import { createSourcemapIgnorelistPlugin } from './sourcemap-ignorelist-plugin';
import { getFeatureSupport } from './utils';
import { createVirtualModulePlugin } from './virtual-module-plugin';
export function createBrowserCodeBundleOptions(
options: NormalizedApplicationBuildOptions,
target: string[],
sourceFileCache?: SourceFileCache,
): BuildOptions {
const { entryPoints, outputNames } = options;
const { pluginOptions, styleOptions } = createCompilerPluginOptions(
options,
target,
sourceFileCache,
);
const buildOptions: BuildOptions = {
...getEsBuildCommonOptions(options),
platform: 'browser',
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
// match and the ES5 distribution would be bundled and ends up breaking at
// runtime with the RxJS testing library.
// More details: https://github.com/angular/angular-cli/issues/25405.
mainFields: ['es2020', 'es2015', 'browser', 'module', 'main'],
entryNames: outputNames.bundles,
entryPoints,
target,
supported: getFeatureSupport(target),
plugins: [
createSourcemapIgnorelistPlugin(),
createCompilerPlugin(
// JS/TS options
pluginOptions,
// Component stylesheet options
styleOptions,
),
],
};
if (options.plugins) {
buildOptions.plugins?.push(...options.plugins);
}
if (options.externalPackages) {
// Package files affected by a customized plugin should not be implicitly marked as external
if (
options.plugins ||
typeof options.externalPackages === 'object'
) {
// Plugin must be added after custom plugins to ensure any added loader options are considered
buildOptions.plugins?.push(
createExternalPackagesPlugin(
options.externalPackages !== true ? options.externalPackages : undefined,
),
);
} else {
// Safe to use the packages external option directly
buildOptions.packages = 'external';
}
}
return buildOptions;
}
export function createBrowserPolyfillBundleOptions(
options: NormalizedApplicationBuildOptions,
target: string[],
sourceFileCache?: SourceFileCache,
): BuildOptions | BundlerOptionsFactory | undefined {
const namespace = 'angular:polyfills';
const polyfillBundleOptions = getEsBuildCommonPolyfillsOptions(
options,
namespace,
true,
sourceFileCache,
);
if (!polyfillBundleOptions) {
return;
}
const { outputNames, polyfills } = options;
const hasTypeScriptEntries = polyfills?.some((entry) => /\.[cm]?tsx?$/.test(entry));
const buildOptions: BuildOptions = {
...polyfillBundleOptions,
platform: 'browser',
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
// match and the ES5 distribution would be bundled and ends up breaking at
// runtime with the RxJS testing library.
// More details: https://github.com/angular/angular-cli/issues/25405.
mainFields: ['es2020', 'es2015', 'browser', 'module', 'main'],
entryNames: outputNames.bundles,
target,
entryPoints: {
'polyfills': namespace,
},
};
// Only add the Angular TypeScript compiler if TypeScript files are provided in the polyfills
if (hasTypeScriptEntries) {
buildOptions.plugins ??= [];
const { pluginOptions, styleOptions } = createCompilerPluginOptions(
options,
target,
sourceFileCache,
);
buildOptions.plugins.push(
createCompilerPlugin(
// JS/TS options
{ ...pluginOptions, noopTypeScriptCompilation: true },
// Component stylesheet options are unused for polyfills but required by the plugin
styleOptions,
),
);
}
// Use an options factory to allow fully incremental bundling when no TypeScript files are present.
// The TypeScript compilation is not currently integrated into the bundler invalidation so
// cannot be used with fully incremental bundling yet.
return hasTypeScriptEntries ? buildOptions : () => buildOptions;
}
/**
* Create an esbuild 'build' options object for the server bundle.
* @param options The builder's user-provider normalized options.
* @returns An esbuild BuildOptions object.
*/
export function createServerCodeBundleOptions(
options: NormalizedApplicationBuildOptions,
target: string[],
sourceFileCache: SourceFileCache,
): BuildOptions {
const {
jit,
serverEntryPoint,
workspaceRoot,
ssrOptions,
watch,
externalPackages,
prerenderOptions,
} = options;
assert(
serverEntryPoint,
'createServerCodeBundleOptions should not be called without a defined serverEntryPoint.',
);
const { pluginOptions, styleOptions } = createCompilerPluginOptions(
options,
target,
sourceFileCache,
);
const mainServerNamespace = 'angular:server-render-utils';
const entryPoints: Record<string, string> = {
'render-utils.server': mainServerNamespace,
'main.server': serverEntryPoint,
};
const ssrEntryPoint = ssrOptions?.entry;
if (ssrEntryPoint) {
entryPoints['server'] = ssrEntryPoint;
}
const buildOptions: BuildOptions = {
...getEsBuildCommonOptions(options),
platform: 'node',
splitting: true,
outExtension: { '.js': '.mjs' },
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
// match and the ES5 distribution would be bundled and ends up breaking at
// runtime with the RxJS testing library.
// More details: https://github.com/angular/angular-cli/issues/25405.
mainFields: ['es2020', 'es2015', 'module', 'main'],
entryNames: '[name]',
target,
banner: {
js: `import './polyfills.server.mjs';`,
},
entryPoints,
supported: getFeatureSupport(target),
plugins: [
createSourcemapIgnorelistPlugin(),
createCompilerPlugin(
// JS/TS options
{ ...pluginOptions, noopTypeScriptCompilation: true },
// Component stylesheet options
styleOptions,
),
],
};
buildOptions.plugins ??= [];
if (externalPackages) {
buildOptions.packages = 'external';
} else {
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
}
buildOptions.plugins.push(
createVirtualModulePlugin({
namespace: mainServerNamespace,
cache: sourceFileCache?.loadResultCache,
loadContent: async () => {
const contents: string[] = [
`export { ɵConsole } from '@angular/core';`,
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
];
if (watch) {
contents.push(`export { ɵresetCompiledComponents } from '@angular/core';`);
}
if (prerenderOptions?.discoverRoutes) {
// We do not import it directly so that node.js modules are resolved using the correct context.
const routesExtractorCode = await readFile(
join(__dirname, '../../utils/routes-extractor/extractor.js'),
'utf-8',
);
// Remove source map URL comments from the code if a sourcemap is present as this will not match the file.
contents.push(routesExtractorCode.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''));
}
return {
contents: contents.join('\n'),
loader: 'js',
resolveDir: workspaceRoot,
};
},
}),
);
if (options.plugins) {
buildOptions.plugins.push(...options.plugins);
}
return buildOptions;
}
export function createServerPolyfillBundleOptions(
options: NormalizedApplicationBuildOptions,
target: string[],
sourceFileCache?: SourceFileCache,
): BundlerOptionsFactory | undefined {
const polyfills: string[] = [];
const polyfillsFromConfig = new Set(options.polyfills);
if (polyfillsFromConfig.has('zone.js')) {
polyfills.push('zone.js/node');
}
if (
polyfillsFromConfig.has('@angular/localize') ||
polyfillsFromConfig.has('@angular/localize/init')
) {
polyfills.push('@angular/localize/init');
}
polyfills.push('@angular/platform-server/init');
const namespace = 'angular:polyfills-server';
const polyfillBundleOptions = getEsBuildCommonPolyfillsOptions(
{
...options,
polyfills,
},
namespace,
false,
sourceFileCache,
);
if (!polyfillBundleOptions) {
return;
}
const buildOptions: BuildOptions = {
...polyfillBundleOptions,
platform: 'node',
outExtension: { '.js': '.mjs' },
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
// match and the ES5 distribution would be bundled and ends up breaking at
// runtime with the RxJS testing library.
// More details: https://github.com/angular/angular-cli/issues/25405.
mainFields: ['es2020', 'es2015', 'module', 'main'],
entryNames: '[name]',
banner: {
js: [
// Note: Needed as esbuild does not provide require shims / proxy from ESModules.
// See: https://github.com/evanw/esbuild/issues/1921.
`import { createRequire } from 'node:module';`,
`globalThis['require'] ??= createRequire(import.meta.url);`,
].join('\n'),
},
target,
entryPoints: {
'polyfills.server': namespace,
},
};
return () => buildOptions;
}
function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): BuildOptions {
const {
workspaceRoot,
outExtension,
optimizationOptions,
sourcemapOptions,
tsconfig,
externalDependencies,
outputNames,
preserveSymlinks,
jit,
loaderExtensions,
jsonLogs,
} = options;
// Ensure unique hashes for i18n translation changes when using post-process inlining.
// This hash value is added as a footer to each file and ensures that the output file names (with hashes)
// change when translation files have changed. If this is not done the post processed files may have
// different content but would retain identical production file names which would lead to browser caching problems.
let footer;
if (options.i18nOptions.shouldInline) {
// Update file hashes to include translation file content
const i18nHash = Object.values(options.i18nOptions.locales).reduce(
(data, locale) => data + locale.files.map((file) => file.integrity || '').join('|'),
'',
);
footer = { js: `/**i18n:${createHash('sha256').update(i18nHash).digest('hex')}*/` };
}
return {
absWorkingDir: workspaceRoot,
bundle: true,
format: 'esm',
assetNames: outputNames.media,
conditions: ['es2020', 'es2015', 'module'],
resolveExtensions: ['.ts', '.tsx', '.mjs', '.js'],
metafile: true,
legalComments: options.extractLicenses ? 'none' : 'eof',
logLevel: options.verbose && !jsonLogs ? 'debug' : 'silent',
minifyIdentifiers: optimizationOptions.scripts && allowMangle,
minifySyntax: optimizationOptions.scripts,
minifyWhitespace: optimizationOptions.scripts,
pure: ['forwardRef'],
outdir: workspaceRoot,
outExtension: outExtension ? { '.js': `.${outExtension}` } : undefined,
sourcemap: sourcemapOptions.scripts && (sourcemapOptions.hidden ? 'external' : true),
splitting: true,
chunkNames: options.namedChunks ? '[name]-[hash]' : 'chunk-[hash]',
tsconfig,
external: externalDependencies,
write: false,
preserveSymlinks,
define: {
...options.define,
// Only set to false when script optimizations are enabled. It should not be set to true because
// Angular turns `ngDevMode` into an object for development debugging purposes when not defined
// which a constant true value would break.
...(optimizationOptions.scripts ? { 'ngDevMode': 'false' } : undefined),
'ngJitMode': jit ? 'true' : 'false',
},
loader: loaderExtensions,
footer,
publicPath: options.publicPath,
};
}
function getEsBuildCommonPolyfillsOptions(
options: NormalizedApplicationBuildOptions,
namespace: string,
tryToResolvePolyfillsAsRelative: boolean,
sourceFileCache: SourceFileCache | undefined,
): BuildOptions | undefined {
const { jit, workspaceRoot, i18nOptions } = options;
const buildOptions: BuildOptions = {
...getEsBuildCommonOptions(options),
splitting: false,
plugins: [createSourcemapIgnorelistPlugin()],
};
const polyfills = options.polyfills ? [...options.polyfills] : [];
// Angular JIT mode requires the runtime compiler
if (jit) {
polyfills.unshift('@angular/compiler');
}
// Add Angular's global locale data if i18n options are present.
// Locale data should go first so that project provided polyfill code can augment if needed.
let needLocaleDataPlugin = false;
if (i18nOptions.shouldInline) {
// Add locale data for all active locales
// TODO: Inject each individually within the inlining process itself
for (const locale of i18nOptions.inlineLocales) {
polyfills.unshift(`angular:locale/data:${locale}`);
}
needLocaleDataPlugin = true;
} else if (i18nOptions.hasDefinedSourceLocale) {
// When not inlining and a source local is present, use the source locale data directly
polyfills.unshift(`angular:locale/data:${i18nOptions.sourceLocale}`);
needLocaleDataPlugin = true;
}
if (needLocaleDataPlugin) {
buildOptions.plugins?.push(createAngularLocaleDataPlugin());
}
if (polyfills.length === 0) {
return;
}
buildOptions.plugins?.push(
createVirtualModulePlugin({
namespace,
cache: sourceFileCache?.loadResultCache,
loadContent: async (_, build) => {
let hasLocalizePolyfill = false;
let polyfillPaths = polyfills;
if (tryToResolvePolyfillsAsRelative) {
polyfillPaths = await Promise.all(
polyfills.map(async (path) => {
hasLocalizePolyfill ||= path.startsWith('@angular/localize');
if (path.startsWith('zone.js') || !extname(path)) {
return path;
}
const potentialPathRelative = './' + path;
const result = await build.resolve(potentialPathRelative, {
kind: 'import-statement',
resolveDir: workspaceRoot,
});
return result.path ? potentialPathRelative : path;
}),
);
} else {
hasLocalizePolyfill = polyfills.some((p) => p.startsWith('@angular/localize'));
}
if (!i18nOptions.shouldInline && !hasLocalizePolyfill) {
const result = await build.resolve('@angular/localize', {
kind: 'import-statement',
resolveDir: workspaceRoot,
});
if (result.path) {
polyfillPaths.push('@angular/localize/init');
}
}
// Generate module contents with an import statement per defined polyfill
let contents = polyfillPaths
.map((file) => `import '${file.replace(/\\/g, '/')}';`)
.join('\n');
// The below should be done after loading `$localize` as otherwise the locale will be overridden.
if (i18nOptions.shouldInline) {
// When inlining, a placeholder is used to allow the post-processing step to inject the $localize locale identifier.
contents += '(globalThis.$localize ??= {}).locale = "___NG_LOCALE_INSERT___";\n';
} else if (i18nOptions.hasDefinedSourceLocale) {
// If not inlining translations and source locale is defined, inject the locale specifier.
contents += `(globalThis.$localize ??= {}).locale = "${i18nOptions.sourceLocale}";\n`;
}
return {
contents,
loader: 'js',
resolveDir: workspaceRoot,
};
},
}),
);
return buildOptions;
}