Skip to content

Commit 8a1c62d

Browse files
alan-agius4hansl
authored andcommitted
fix(@angular-devkit/build-angular): build should not hash lazy styles
When having `optimize` enabled the chunk Id doesn't match the bundle name, this we should use the chunk name instead Closes #11772 and Closes #11704
1 parent 1f9e169 commit 8a1c62d

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { getOutputHashFormat } from './utils';
1515
import { WebpackConfigOptions } from '../build-options';
1616
import { findUp } from '../../utilities/find-up';
1717
import { RawCssLoader } from '../../plugins/webpack';
18-
import { ExtraEntryPoint } from '../../../browser/schema';
1918
import { normalizeExtraEntryPoints } from './utils';
2019
import { RemoveHashPlugin } from '../../plugins/remove-hash-plugin';
2120

@@ -174,7 +173,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
174173

175174
// Process global styles.
176175
if (buildOptions.styles.length > 0) {
177-
const chunkIds: string[] = [];
176+
const chunkNames: string[] = [];
178177

179178
normalizeExtraEntryPoints(buildOptions.styles, 'styles').forEach(style => {
180179
const resolvedPath = path.resolve(root, style.input);
@@ -188,16 +187,16 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
188187

189188
// Add lazy styles to the list.
190189
if (style.lazy) {
191-
chunkIds.push(style.bundleName);
190+
chunkNames.push(style.bundleName);
192191
}
193192

194193
// Add global css paths.
195194
globalStylePaths.push(resolvedPath);
196195
});
197196

198-
if (chunkIds.length > 0) {
197+
if (chunkNames.length > 0) {
199198
// Add plugin to remove hashes from lazy styles.
200-
extraPlugins.push(new RemoveHashPlugin({ chunkIds, hashFormat}));
199+
extraPlugins.push(new RemoveHashPlugin({ chunkNames, hashFormat}));
201200
}
202201
}
203202

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/remove-hash-plugin.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { HashFormat } from '../models/webpack-configs/utils';
1010

1111

1212
export interface RemoveHashPluginOptions {
13-
chunkIds: string[];
13+
chunkNames: string[];
1414
hashFormat: HashFormat;
1515
}
1616

@@ -25,14 +25,15 @@ export class RemoveHashPlugin {
2525
};
2626

2727
mainTemplate.hooks.assetPath.tap('remove-hash-plugin',
28-
(path: string, data: { chunk?: { id: string } }) => {
29-
const chunkId = data.chunk && data.chunk.id;
28+
(path: string, data: { chunk?: { name: string } }) => {
29+
const chunkName = data.chunk && data.chunk.name;
30+
const { chunkNames, hashFormat } = this.options;
3031

31-
if (chunkId && this.options.chunkIds.includes(chunkId)) {
32+
if (chunkName && chunkNames.includes(chunkName)) {
3233
// Replace hash formats with empty strings.
3334
return path
34-
.replace(this.options.hashFormat.chunk, '')
35-
.replace(this.options.hashFormat.extract, '');
35+
.replace(hashFormat.chunk, '')
36+
.replace(hashFormat.extract, '');
3637
}
3738

3839
return path;

packages/angular_devkit/build_angular/test/browser/output-hashing_spec_large.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,22 @@ describe('Browser Builder output hashing', () => {
180180
}),
181181
).toPromise().then(done, done.fail);
182182
});
183+
184+
it('does not hash lazy styles when optimization is enabled', (done) => {
185+
const overrides = {
186+
outputHashing: 'all',
187+
extractCss: true,
188+
optimization: true,
189+
styles: [{ input: 'src/styles.css', lazy: true }],
190+
};
191+
192+
runTargetSpec(host, browserTargetSpec, overrides, DefaultTimeout).pipe(
193+
tap(() => {
194+
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js/)).toBeFalsy();
195+
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js.map/)).toBeFalsy();
196+
expect(host.scopedSync().exists(normalize('dist/styles.css'))).toBe(true);
197+
expect(host.scopedSync().exists(normalize('dist/styles.css.map'))).toBe(true);
198+
}),
199+
).toPromise().then(done, done.fail);
200+
});
183201
});

0 commit comments

Comments
 (0)