Skip to content

Commit 5898f72

Browse files
cexbrayatclydin
authored andcommitted
feat(@angular-devkit/build-angular): support namedChunks option in application builder
This adds the support of `namedChunks` for the new `application` builder. It generates output like the following: ``` Initial Chunk Files | Names | Raw Size | Estimated Transfer Size chunk-ACXUMF56.js | - | 94.14 kB | 28.25 kB main-3WP5KDHR.js | main | 71.95 kB | 18.31 kB polyfills-4UVFGIFL.js | polyfills | 32.85 kB | 10.68 kB chunk-2XJVAMHT.js | - | 449 bytes | 449 bytes styles-5INURTSO.css | styles | 0 bytes | 0 bytes | Initial Total | 199.38 kB | 57.68 kB Lazy Chunk Files | Names | Raw Size | Estimated Transfer Size about.component-2PJOS5PM.js | - | 401 bytes | 401 bytes home.component-25UHFOEY.js | - | 398 bytes | 398 bytes ``` This is really handy to get a glimpse at what a chunk is referring to and be able to analyze it (especially in applications with dozens of chunks).
1 parent 76da084 commit 5898f72

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

packages/angular_devkit/build_angular/src/builders/application/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export async function normalizeOptions(
237237
progress = true,
238238
externalPackages,
239239
deleteOutputPath,
240+
namedChunks,
240241
} = options;
241242

242243
// Return all the normalized options
@@ -284,6 +285,7 @@ export async function normalizeOptions(
284285
indexHtmlOptions,
285286
tailwindConfiguration,
286287
i18nOptions,
288+
namedChunks,
287289
};
288290
}
289291

packages/angular_devkit/build_angular/src/builders/application/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@
325325
"description": "Extract all licenses in a separate file.",
326326
"default": true
327327
},
328+
"namedChunks": {
329+
"type": "boolean",
330+
"description": "Use file name for lazy loaded chunks.",
331+
"default": false
332+
},
328333
"subresourceIntegrity": {
329334
"type": "boolean",
330335
"description": "Enables the use of subresource integrity validation.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
const MAIN_OUTPUT = 'dist/main.js';
13+
const NAMED_LAZY_OUTPUT = 'dist/lazy-module-7QZXF7K7.js';
14+
const UNNAMED_LAZY_OUTPUT = 'dist/chunk-OW5RYMPM.js';
15+
16+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
17+
describe('Option: "namedChunks"', () => {
18+
beforeEach(async () => {
19+
// Setup a lazy loaded chunk
20+
await harness.writeFiles({
21+
'src/lazy-module.ts': 'export const value = 42;',
22+
'src/main.ts': `import('./lazy-module');`,
23+
});
24+
});
25+
26+
it('generates named files in output when true', async () => {
27+
harness.useTarget('build', {
28+
...BASE_OPTIONS,
29+
namedChunks: true,
30+
});
31+
32+
const { result } = await harness.executeOnce();
33+
34+
expect(result?.success).toBe(true);
35+
36+
harness.expectFile(MAIN_OUTPUT).toExist();
37+
harness.expectFile(NAMED_LAZY_OUTPUT).toExist();
38+
harness.expectFile(UNNAMED_LAZY_OUTPUT).toNotExist();
39+
});
40+
41+
it('does not generate named files in output when false', async () => {
42+
harness.useTarget('build', {
43+
...BASE_OPTIONS,
44+
namedChunks: false,
45+
});
46+
47+
const { result } = await harness.executeOnce();
48+
49+
expect(result?.success).toBe(true);
50+
51+
harness.expectFile(MAIN_OUTPUT).toExist();
52+
harness.expectFile(NAMED_LAZY_OUTPUT).toNotExist();
53+
harness.expectFile(UNNAMED_LAZY_OUTPUT).toExist();
54+
});
55+
56+
it('does not generates named files in output when not present', async () => {
57+
harness.useTarget('build', {
58+
...BASE_OPTIONS,
59+
});
60+
61+
const { result } = await harness.executeOnce();
62+
63+
expect(result?.success).toBe(true);
64+
65+
harness.expectFile(MAIN_OUTPUT).toExist();
66+
harness.expectFile(NAMED_LAZY_OUTPUT).toNotExist();
67+
harness.expectFile(UNNAMED_LAZY_OUTPUT).toExist();
68+
});
69+
});
70+
});

packages/angular_devkit/build_angular/src/builders/browser-esbuild/builder-status-warnings.ts

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const UNSUPPORTED_OPTIONS: Array<keyof BrowserBuilderOptions> = [
1919
// 'commonChunk',
2020

2121
// * Unused by builder and will be removed in a future release
22-
'namedChunks',
2322
'vendorChunk',
2423
'resourcesOutputPath',
2524

@@ -43,7 +42,6 @@ export function logBuilderStatusWarnings(options: BrowserBuilderOptions, context
4342
}
4443

4544
if (
46-
unsupportedOption === 'namedChunks' ||
4745
unsupportedOption === 'vendorChunk' ||
4846
unsupportedOption === 'resourcesOutputPath' ||
4947
unsupportedOption === 'deployUrl'

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
327327
outExtension: outExtension ? { '.js': `.${outExtension}` } : undefined,
328328
sourcemap: sourcemapOptions.scripts && (sourcemapOptions.hidden ? 'external' : true),
329329
splitting: true,
330-
chunkNames: 'chunk-[hash]',
330+
chunkNames: options.namedChunks ? '[name]-[hash]' : 'chunk-[hash]',
331331
tsconfig,
332332
external: externalDependencies,
333333
write: false,

0 commit comments

Comments
 (0)