Skip to content

Commit ff54b49

Browse files
alan-agius4dgp1130
authored andcommittedFeb 2, 2022
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 (cherry picked from commit dc1817a)
1 parent 32b3064 commit ff54b49

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed
 

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
IndexUnion,
1818
InlineStyleLanguage,
1919
Localize,
20+
OutputHashing,
2021
SourceMapClass,
2122
} from '../builders/browser/schema';
2223
import { Schema as DevServerSchema } from '../builders/dev-server/schema';
@@ -42,7 +43,7 @@ export interface BuildOptions {
4243
bundleDependencies?: boolean;
4344
externalDependencies?: string[];
4445
watch?: boolean;
45-
outputHashing?: string;
46+
outputHashing?: OutputHashing;
4647
poll?: number;
4748
index?: IndexUnion;
4849
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

+35-19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
AssetPatternClass,
1818
ExtraEntryPoint,
1919
ExtraEntryPointClass,
20+
OutputHashing,
2021
} from '../../builders/browser/schema';
2122
import { WebpackConfigOptions } from '../../utils/build-options';
2223
import { VERSION } from '../../utils/package-version';
@@ -28,25 +29,40 @@ export interface HashFormat {
2829
script: string;
2930
}
3031

31-
export function getOutputHashFormat(option: string, length = 20): HashFormat {
32-
const hashFormats: { [option: string]: HashFormat } = {
33-
none: { chunk: '', extract: '', file: '', script: '' },
34-
media: { chunk: '', extract: '', file: `.[hash:${length}]`, script: '' },
35-
bundles: {
36-
chunk: `.[contenthash:${length}]`,
37-
extract: `.[contenthash:${length}]`,
38-
file: '',
39-
script: `.[hash:${length}]`,
40-
},
41-
all: {
42-
chunk: `.[contenthash:${length}]`,
43-
extract: `.[contenthash:${length}]`,
44-
file: `.[hash:${length}]`,
45-
script: `.[hash:${length}]`,
46-
},
47-
};
48-
49-
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+
}
5066
}
5167

5268
export type NormalizedEntryPoint = Required<ExtraEntryPointClass>;

0 commit comments

Comments
 (0)
Please sign in to comment.