Skip to content

Commit 4b9199d

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): ensure to use content hash as filenames hashing mechanism
Previously we used hash which resulted in a unique hash generated for every build even when the contents of the files didn't differ. More info: https://webpack.js.org/guides/caching/#output-filenames
1 parent a6bbd60 commit 4b9199d

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

packages/angular_devkit/build_angular/src/utils/build-options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
IndexUnion,
1717
InlineStyleLanguage,
1818
Localize,
19+
OutputHashing,
1920
ScriptElement,
2021
SourceMapClass,
2122
StyleElement,
@@ -43,7 +44,7 @@ export interface BuildOptions {
4344
bundleDependencies?: boolean;
4445
externalDependencies?: string[];
4546
watch?: boolean;
46-
outputHashing?: string;
47+
outputHashing?: OutputHashing;
4748
poll?: number;
4849
index?: IndexUnion;
4950
deleteOutputPath?: boolean;

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
9898
} = await loadEsmModule<typeof import('@angular/compiler-cli')>('@angular/compiler-cli');
9999

100100
// determine hashing format
101-
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
101+
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);
102102

103103
if (buildOptions.progress) {
104104
extraPlugins.push(new ProgressPlugin(platform));

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
8383
const cssSourceMap = buildOptions.sourceMap.styles;
8484

8585
// Determine hashing format.
86-
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as string);
86+
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);
8787

8888
// use includePaths from appConfig
8989
const includePaths =

packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts

+40-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import glob from 'glob';
1313
import * as path from 'path';
1414
import { ScriptTarget } from 'typescript';
1515
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
16-
import { AssetPatternClass, ScriptElement, StyleElement } from '../../builders/browser/schema';
16+
import {
17+
AssetPatternClass,
18+
OutputHashing,
19+
ScriptElement,
20+
StyleElement,
21+
} from '../../builders/browser/schema';
1722
import { WebpackConfigOptions } from '../../utils/build-options';
1823
import { VERSION } from '../../utils/package-version';
1924

@@ -24,25 +29,40 @@ export interface HashFormat {
2429
script: string;
2530
}
2631

27-
export function getOutputHashFormat(option: string, length = 20): HashFormat {
28-
const hashFormats: { [option: string]: HashFormat } = {
29-
none: { chunk: '', extract: '', file: '', script: '' },
30-
media: { chunk: '', extract: '', file: `.[hash:${length}]`, script: '' },
31-
bundles: {
32-
chunk: `.[contenthash:${length}]`,
33-
extract: `.[contenthash:${length}]`,
34-
file: '',
35-
script: `.[hash:${length}]`,
36-
},
37-
all: {
38-
chunk: `.[contenthash:${length}]`,
39-
extract: `.[contenthash:${length}]`,
40-
file: `.[hash:${length}]`,
41-
script: `.[hash:${length}]`,
42-
},
43-
};
44-
45-
return hashFormats[option] || hashFormats['none'];
32+
export function getOutputHashFormat(outputHashing = OutputHashing.None, length = 20): HashFormat {
33+
const hashTemplate = `.[contenthash:${length}]`;
34+
35+
switch (outputHashing) {
36+
case 'media':
37+
return {
38+
chunk: '',
39+
extract: '',
40+
file: hashTemplate,
41+
script: '',
42+
};
43+
case 'bundles':
44+
return {
45+
chunk: hashTemplate,
46+
extract: hashTemplate,
47+
file: '',
48+
script: hashTemplate,
49+
};
50+
case 'all':
51+
return {
52+
chunk: hashTemplate,
53+
extract: hashTemplate,
54+
file: hashTemplate,
55+
script: hashTemplate,
56+
};
57+
case 'none':
58+
default:
59+
return {
60+
chunk: '',
61+
extract: '',
62+
file: '',
63+
script: '',
64+
};
65+
}
4666
}
4767

4868
export type NormalizedEntryPoint = Required<Exclude<ScriptElement | StyleElement, string>>;

0 commit comments

Comments
 (0)