Skip to content

Commit 4e89c3c

Browse files
cexbrayatalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): use a dash in bundle names
This updates the esbuild based builder to use a dash in bundles and media instead of a dot to be consistent with the chunks files `chunk-xxx.js`.
1 parent f9fdd09 commit 4e89c3c

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

Diff for: packages/angular_devkit/build_angular/src/builders/application/options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ export async function normalizeOptions(
100100
const outputNames = {
101101
bundles:
102102
options.outputHashing === OutputHashing.All || options.outputHashing === OutputHashing.Bundles
103-
? '[name].[hash]'
103+
? '[name]-[hash]'
104104
: '[name]',
105105
media:
106106
'media/' +
107107
(options.outputHashing === OutputHashing.All || options.outputHashing === OutputHashing.Media
108-
? '[name].[hash]'
108+
? '[name]-[hash]'
109109
: '[name]'),
110110
};
111111

Diff for: packages/angular_devkit/build_angular/src/builders/application/tests/options/output-hashing_spec.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
3131
const { result } = await harness.executeOnce();
3232
expect(result?.success).toBe(true);
3333

34-
expect(harness.hasFileMatch('dist', /main\.[0-9A-Z]{8}\.js$/)).toBeTrue();
35-
expect(harness.hasFileMatch('dist', /polyfills\.[0-9A-Z]{8}\.js$/)).toBeTrue();
36-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeTrue();
37-
expect(harness.hasFileMatch('dist/media', /spectrum\.[0-9A-Z]{8}\.png$/)).toBeTrue();
34+
expect(harness.hasFileMatch('dist', /main-[0-9A-Z]{8}\.js$/)).toBeTrue();
35+
expect(harness.hasFileMatch('dist', /polyfills-[0-9A-Z]{8}\.js$/)).toBeTrue();
36+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeTrue();
37+
expect(harness.hasFileMatch('dist/media', /spectrum-[0-9A-Z]{8}\.png$/)).toBeTrue();
3838
});
3939

4040
it(`doesn't hash any filenames when not set`, async () => {
@@ -49,10 +49,10 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
4949
const { result } = await harness.executeOnce();
5050
expect(result?.success).toBe(true);
5151

52-
expect(harness.hasFileMatch('dist', /main\.[0-9A-Z]{8}\.js$/)).toBeFalse();
53-
expect(harness.hasFileMatch('dist', /polyfills\.[0-9A-Z]{8}\.js$/)).toBeFalse();
54-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeFalse();
55-
expect(harness.hasFileMatch('dist/media', /spectrum\.[0-9A-Z]{8}\.png$/)).toBeFalse();
52+
expect(harness.hasFileMatch('dist', /main-[0-9A-Z]{8}\.js$/)).toBeFalse();
53+
expect(harness.hasFileMatch('dist', /polyfills-[0-9A-Z]{8}\.js$/)).toBeFalse();
54+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeFalse();
55+
expect(harness.hasFileMatch('dist/media', /spectrum-[0-9A-Z]{8}\.png$/)).toBeFalse();
5656
});
5757

5858
it(`doesn't hash any filenames when set to "none"`, async () => {
@@ -68,10 +68,10 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
6868
const { result } = await harness.executeOnce();
6969
expect(result?.success).toBe(true);
7070

71-
expect(harness.hasFileMatch('dist', /main\.[0-9A-Z]{8}\.js$/)).toBeFalse();
72-
expect(harness.hasFileMatch('dist', /polyfills\.[0-9A-Z]{8}\.js$/)).toBeFalse();
73-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeFalse();
74-
expect(harness.hasFileMatch('dist/media', /spectrum\.[0-9A-Z]{8}\.png$/)).toBeFalse();
71+
expect(harness.hasFileMatch('dist', /main-[0-9A-Z]{8}\.js$/)).toBeFalse();
72+
expect(harness.hasFileMatch('dist', /polyfills-[0-9A-Z]{8}\.js$/)).toBeFalse();
73+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeFalse();
74+
expect(harness.hasFileMatch('dist/media', /spectrum-[0-9A-Z]{8}\.png$/)).toBeFalse();
7575
});
7676

7777
it(`hashes CSS resources filenames only when set to "media"`, async () => {
@@ -87,10 +87,10 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
8787
const { result } = await harness.executeOnce();
8888
expect(result?.success).toBe(true);
8989

90-
expect(harness.hasFileMatch('dist', /main\.[0-9A-Z]{8}\.js$/)).toBeFalse();
91-
expect(harness.hasFileMatch('dist', /polyfills\.[0-9A-Z]{8}\.js$/)).toBeFalse();
92-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeFalse();
93-
expect(harness.hasFileMatch('dist/media', /spectrum\.[0-9A-Z]{8}\.png$/)).toBeTrue();
90+
expect(harness.hasFileMatch('dist', /main-[0-9A-Z]{8}\.js$/)).toBeFalse();
91+
expect(harness.hasFileMatch('dist', /polyfills-[0-9A-Z]{8}\.js$/)).toBeFalse();
92+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeFalse();
93+
expect(harness.hasFileMatch('dist/media', /spectrum-[0-9A-Z]{8}\.png$/)).toBeTrue();
9494
});
9595

9696
it(`hashes bundles filenames only when set to "bundles"`, async () => {
@@ -106,10 +106,10 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
106106
const { result } = await harness.executeOnce();
107107
expect(result?.success).toBe(true);
108108

109-
expect(harness.hasFileMatch('dist', /main\.[0-9A-Z]{8}\.js$/)).toBeTrue();
110-
expect(harness.hasFileMatch('dist', /polyfills\.[0-9A-Z]{8}\.js$/)).toBeTrue();
111-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeTrue();
112-
expect(harness.hasFileMatch('dist/media', /spectrum\.[0-9A-Z]{8}\.png$/)).toBeFalse();
109+
expect(harness.hasFileMatch('dist', /main-[0-9A-Z]{8}\.js$/)).toBeTrue();
110+
expect(harness.hasFileMatch('dist', /polyfills-[0-9A-Z]{8}\.js$/)).toBeTrue();
111+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeTrue();
112+
expect(harness.hasFileMatch('dist/media', /spectrum-[0-9A-Z]{8}\.png$/)).toBeFalse();
113113
});
114114

115115
it('does not hash non injected styles', async () => {
@@ -128,8 +128,8 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
128128
const { result } = await harness.executeOnce();
129129
expect(result?.success).toBe(true);
130130

131-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css$/)).toBeFalse();
132-
expect(harness.hasFileMatch('dist', /styles\.[0-9A-Z]{8}\.css.map$/)).toBeFalse();
131+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css$/)).toBeFalse();
132+
expect(harness.hasFileMatch('dist', /styles-[0-9A-Z]{8}\.css.map$/)).toBeFalse();
133133
harness.expectFile('dist/styles.css').toExist();
134134
harness.expectFile('dist/styles.css.map').toExist();
135135
});

Diff for: packages/angular_devkit/build_angular/src/tools/esbuild/bundler-context.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export class BundlerContext {
169169
outputFile.path = relativeFilePath;
170170

171171
if (entryPoint) {
172-
// The first part of the filename is the name of file (e.g., "polyfills" for "polyfills.7S5G3MDY.js")
173-
const name = basename(relativeFilePath).split('.', 1)[0];
172+
// The first part of the filename is the name of file (e.g., "polyfills" for "polyfills-7S5G3MDY.js")
173+
const name = basename(relativeFilePath).replace(/(?:-[\dA-Z]{8})?\.[a-z]{2,3}$/, '');
174174
// Entry points are only styles or scripts
175175
const type = extname(relativeFilePath) === '.css' ? 'style' : 'script';
176176

Diff for: tests/legacy-cli/e2e/tests/basic/build.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default async function () {
1616
// Production build
1717
const { stdout: stdout2 } = await ng('build');
1818
if (getGlobalVariable('argv')['esbuild']) {
19-
// esbuild uses an 8 character hash
20-
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{8}\.js/);
19+
// esbuild uses an 8 character hash and a dash as separator
20+
await expectFileToMatch('dist/test-project/index.html', /main-[a-zA-Z0-9]{8}\.js/);
2121
} else {
2222
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{16}\.js/);
2323
}

Diff for: tests/legacy-cli/e2e/tests/basic/run.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export default async function () {
1010
// Production build
1111
await silentNg('run', 'test-project:build');
1212
if (getGlobalVariable('argv')['esbuild']) {
13-
// esbuild uses an 8 character hash
14-
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{8}\.js/);
13+
// esbuild uses an 8 character hash and a dash as separator
14+
await expectFileToMatch('dist/test-project/index.html', /main-[a-zA-Z0-9]{8}\.js/);
1515
} else {
1616
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{16}\.js/);
1717
}

Diff for: tests/legacy-cli/e2e/tests/build/prod-build.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { statSync } from 'fs';
22
import { join } from 'path';
3+
import { getGlobalVariable } from '../../utils/env';
34
import { expectFileToExist, expectFileToMatch, readFile } from '../../utils/fs';
45
import { noSilentNg } from '../../utils/process';
56

@@ -30,12 +31,21 @@ export default async function () {
3031
await noSilentNg('build');
3132
await expectFileToExist(join(process.cwd(), 'dist'));
3233
// Check for cache busting hash script src
33-
await expectFileToMatch('dist/test-project/index.html', /main\.[0-9a-zA-Z]{8,16}\.js/);
34-
await expectFileToMatch('dist/test-project/index.html', /styles\.[0-9a-zA-Z]{8,16}\.css/);
34+
if (getGlobalVariable('argv')['esbuild']) {
35+
// esbuild uses an 8 character hash and a dash as separator
36+
await expectFileToMatch('dist/test-project/index.html', /main-[0-9a-zA-Z]{8}\.js/);
37+
await expectFileToMatch('dist/test-project/index.html', /styles-[0-9a-zA-Z]{8}\.css/);
38+
} else {
39+
await expectFileToMatch('dist/test-project/index.html', /main\.[0-9a-zA-Z]{16}\.js/);
40+
await expectFileToMatch('dist/test-project/index.html', /styles\.[0-9a-zA-Z]{16}\.css/);
41+
}
3542
await expectFileToMatch('dist/test-project/3rdpartylicenses.txt', /MIT/);
3643

3744
const indexContent = await readFile('dist/test-project/index.html');
38-
const mainPath = indexContent.match(/src="(main\.[0-9a-zA-Z]{0,32}\.js)"/)![1];
45+
const mainSrcRegExp = getGlobalVariable('argv')['esbuild']
46+
? /src="(main-[0-9a-zA-Z]{8}\.js)"/
47+
: /src="(main\.[0-9a-zA-Z]{16}\.js)"/;
48+
const mainPath = indexContent.match(mainSrcRegExp)![1];
3949

4050
// Content checks
4151
await expectFileToMatch(`dist/test-project/${mainPath}`, bootstrapRegExp);

0 commit comments

Comments
 (0)