Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-angular): ensure to use content hash as filenames hashing mechanism #22612

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IndexUnion,
InlineStyleLanguage,
Localize,
OutputHashing,
ScriptElement,
SourceMapClass,
StyleElement,
Expand Down Expand Up @@ -43,7 +44,7 @@ export interface BuildOptions {
bundleDependencies?: boolean;
externalDependencies?: string[];
watch?: boolean;
outputHashing?: string;
outputHashing?: OutputHashing;
poll?: number;
index?: IndexUnion;
deleteOutputPath?: boolean;
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
60 changes: 40 additions & 20 deletions packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import glob from 'glob';
import * as path from 'path';
import { ScriptTarget } from 'typescript';
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
import { AssetPatternClass, ScriptElement, StyleElement } from '../../builders/browser/schema';
import {
AssetPatternClass,
OutputHashing,
ScriptElement,
StyleElement,
} from '../../builders/browser/schema';
import { WebpackConfigOptions } from '../../utils/build-options';
import { VERSION } from '../../utils/package-version';

Expand All @@ -24,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<Exclude<ScriptElement | StyleElement, string>>;
Expand Down