Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure to use content hash as fil…
Browse files Browse the repository at this point in the history
…enames 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)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Feb 2, 2022
1 parent 32b3064 commit ff54b49
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
Expand Up @@ -17,6 +17,7 @@ import {
IndexUnion,
InlineStyleLanguage,
Localize,
OutputHashing,
SourceMapClass,
} from '../builders/browser/schema';
import { Schema as DevServerSchema } from '../builders/dev-server/schema';
Expand All @@ -42,7 +43,7 @@ export interface BuildOptions {
bundleDependencies?: boolean;
externalDependencies?: string[];
watch?: boolean;
outputHashing?: string;
outputHashing?: OutputHashing;
poll?: number;
index?: IndexUnion;
deleteOutputPath?: boolean;
Expand Down
Expand Up @@ -98,7 +98,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
} = await loadEsmModule<typeof import('@angular/compiler-cli')>('@angular/compiler-cli');

// determine hashing format
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);

if (buildOptions.progress) {
extraPlugins.push(new ProgressPlugin(platform));
Expand Down
Expand Up @@ -83,7 +83,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
const cssSourceMap = buildOptions.sourceMap.styles;

// Determine hashing format.
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as string);
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);

// use includePaths from appConfig
const includePaths =
Expand Down
54 changes: 35 additions & 19 deletions packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts
Expand Up @@ -17,6 +17,7 @@ import {
AssetPatternClass,
ExtraEntryPoint,
ExtraEntryPointClass,
OutputHashing,
} from '../../builders/browser/schema';
import { WebpackConfigOptions } from '../../utils/build-options';
import { VERSION } from '../../utils/package-version';
Expand All @@ -28,25 +29,40 @@ export interface HashFormat {
script: string;
}

export function getOutputHashFormat(option: string, length = 20): HashFormat {
const hashFormats: { [option: string]: HashFormat } = {
none: { chunk: '', extract: '', file: '', script: '' },
media: { chunk: '', extract: '', file: `.[hash:${length}]`, script: '' },
bundles: {
chunk: `.[contenthash:${length}]`,
extract: `.[contenthash:${length}]`,
file: '',
script: `.[hash:${length}]`,
},
all: {
chunk: `.[contenthash:${length}]`,
extract: `.[contenthash:${length}]`,
file: `.[hash:${length}]`,
script: `.[hash:${length}]`,
},
};

return hashFormats[option] || hashFormats['none'];
export function getOutputHashFormat(outputHashing = OutputHashing.None, length = 20): HashFormat {
const hashTemplate = `.[contenthash:${length}]`;

switch (outputHashing) {
case 'media':
return {
chunk: '',
extract: '',
file: hashTemplate,
script: '',
};
case 'bundles':
return {
chunk: hashTemplate,
extract: hashTemplate,
file: '',
script: hashTemplate,
};
case 'all':
return {
chunk: hashTemplate,
extract: hashTemplate,
file: hashTemplate,
script: hashTemplate,
};
case 'none':
default:
return {
chunk: '',
extract: '',
file: '',
script: '',
};
}
}

export type NormalizedEntryPoint = Required<ExtraEntryPointClass>;
Expand Down

0 comments on commit ff54b49

Please sign in to comment.