Skip to content

fix(@angular-devkit/build-angular): don't add hash to lazy styles #11491

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

Merged
merged 1 commit into from
Jul 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { findUp } from '../../utilities/find-up';
import { RawCssLoader } from '../../plugins/webpack';
import { ExtraEntryPoint } from '../../../browser/schema';
import { normalizeExtraEntryPoints } from './utils';
import { RemoveHashPlugin } from '../../plugins/remove-hash-plugin';

const postcssUrl = require('postcss-url');
const autoprefixer = require('autoprefixer');
Expand Down Expand Up @@ -158,7 +159,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {

// use includePaths from appConfig
const includePaths: string[] = [];
let lessPathOptions: { paths?: string[] } = { };
let lessPathOptions: { paths?: string[] } = {};

if (buildOptions.stylePreprocessorOptions
&& buildOptions.stylePreprocessorOptions.includePaths
Expand All @@ -173,6 +174,8 @@ export function getStylesConfig(wco: WebpackConfigOptions) {

// Process global styles.
if (buildOptions.styles.length > 0) {
const chunkIds: string[] = [];

normalizeExtraEntryPoints(buildOptions.styles, 'styles').forEach(style => {
const resolvedPath = path.resolve(root, style.input);

Expand All @@ -183,9 +186,19 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
entryPoints[style.bundleName] = [resolvedPath]
}

// Add lazy styles to the list.
if (style.lazy) {
chunkIds.push(style.bundleName);
}

// Add global css paths.
globalStylePaths.push(resolvedPath);
});

if (chunkIds.length > 0) {
// Add plugin to remove hashes from lazy styles.
extraPlugins.push(new RemoveHashPlugin({ chunkIds, hashFormat}));
}
}

// set base rules to derive final rules from
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Compiler, compilation } from 'webpack';
import { HashFormat } from '../models/webpack-configs/utils';


export interface RemoveHashPluginOptions {
chunkIds: string[];
hashFormat: HashFormat;
}

export class RemoveHashPlugin {

constructor(private options: RemoveHashPluginOptions) { }

apply(compiler: Compiler): void {
compiler.hooks.compilation.tap('remove-hash-plugin', compilation => {
const mainTemplate = compilation.mainTemplate as compilation.MainTemplate & {
hooks: compilation.CompilationHooks;
};

mainTemplate.hooks.assetPath.tap('remove-hash-plugin',
(path: string, data: { chunk?: { id: string } }) => {
const chunkId = data.chunk && data.chunk.id;

if (chunkId && this.options.chunkIds.includes(chunkId)) {
// Replace hash formats with empty strings.
return path
.replace(this.options.hashFormat.chunk, '')
.replace(this.options.hashFormat.extract, '');
}

return path;
},
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,21 @@ describe('Browser Builder output hashing', () => {
}),
).toPromise().then(done, done.fail);
});

it('does not hash lazy styles', (done) => {
const overrides = {
outputHashing: 'all',
extractCss: true,
styles: [{ input: 'src/styles.css', lazy: true }],
};

runTargetSpec(host, browserTargetSpec, overrides, DefaultTimeout).pipe(
tap(() => {
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js.map/)).toBeFalsy();
expect(host.scopedSync().exists(normalize('dist/styles.css'))).toBe(true);
expect(host.scopedSync().exists(normalize('dist/styles.css.map'))).toBe(true);
}),
).toPromise().then(done, done.fail);
});
});