Skip to content

Commit eed5794

Browse files
clydinBrocco
authored andcommitted
fix(@angular/cli): ensure external component styles are optimized
1 parent 9a593dd commit eed5794

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

packages/@angular/cli/models/webpack-configs/styles.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
276276
}
277277

278278
if (minimizeCss) {
279-
extraPlugins.push(new CleanCssWebpackPlugin({ sourceMap: cssSourceMap }));
279+
extraPlugins.push(new CleanCssWebpackPlugin({
280+
sourceMap: cssSourceMap,
281+
// component styles retain their original file name
282+
test: (file) => /\.(?:css|scss|sass|less|styl)$/.test(file),
283+
}));
280284
}
281285

282286
return {

packages/@angular/cli/plugins/cleancss-webpack-plugin.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ interface Chunk {
1616

1717
export interface CleanCssWebpackPluginOptions {
1818
sourceMap: boolean;
19+
test: (file: string) => boolean;
1920
}
2021

2122
export class CleanCssWebpackPlugin {
22-
23-
constructor(private options: Partial<CleanCssWebpackPluginOptions> = {}) {}
23+
private readonly _options: CleanCssWebpackPluginOptions;
24+
25+
constructor(options: Partial<CleanCssWebpackPluginOptions>) {
26+
this._options = {
27+
sourceMap: false,
28+
test: (file) => file.endsWith('.css'),
29+
...options,
30+
};
31+
}
2432

2533
apply(compiler: Compiler): void {
2634
compiler.plugin('compilation', (compilation: any) => {
@@ -32,7 +40,7 @@ export class CleanCssWebpackPlugin {
3240
level: 2,
3341
inline: false,
3442
returnPromise: true,
35-
sourceMap: this.options.sourceMap,
43+
sourceMap: this._options.sourceMap,
3644
});
3745

3846
const files: string[] = [...compilation.additionalChunkAssets];
@@ -44,7 +52,7 @@ export class CleanCssWebpackPlugin {
4452
});
4553

4654
const actions = files
47-
.filter(file => file.endsWith('.css'))
55+
.filter(file => this._options.test(file))
4856
.map(file => {
4957
const asset = compilation.assets[file];
5058
if (!asset) {

0 commit comments

Comments
 (0)