Skip to content

Commit f45236c

Browse files
wojteko22mgechev
authored andcommitted
fix(@angular-devkit/build-angular): add missing styles extensions in anyComponentStyle budget
Make anyComponentStyle budged work with all style extensions. Previously it was working only with css styles. Files with other extensions were ignored.
1 parent 1a9cbd3 commit f45236c

File tree

2 files changed

+83
-55
lines changed

2 files changed

+83
-55
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/any-component-style-budget-checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import * as path from 'path';
910
import { Compiler, Plugin } from 'webpack';
1011
import { Budget, Type } from '../../../src/browser/schema';
1112
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../utilities/bundle-calculator';
@@ -32,8 +33,16 @@ export class AnyComponentStyleBudgetChecker implements Plugin {
3233
return;
3334
}
3435

36+
const cssExtensions = [
37+
'.css',
38+
'.scss',
39+
'.less',
40+
'.styl',
41+
'.sass',
42+
];
43+
3544
const componentStyles = Object.keys(compilation.assets)
36-
.filter((name) => name.endsWith('.css'))
45+
.filter((name) => cssExtensions.includes(path.extname(name)))
3746
.map((name) => ({
3847
size: compilation.assets[name].size(),
3948
label: name,

packages/angular_devkit/build_angular/test/browser/bundle-budgets_spec_large.ts

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createArchitect, host } from '../utils';
1111

1212

1313
describe('Browser Builder bundle budgets', () => {
14+
const cssExtensions = ['css', 'scss', 'less', 'styl'];
1415
const targetSpec = { project: 'app', target: 'build' };
1516
let architect: Architect;
1617

@@ -64,66 +65,84 @@ describe('Browser Builder bundle budgets', () => {
6465
await run.stop();
6566
});
6667

67-
it(`shows warnings for large component css when using 'anyComponentStyle' when AOT`, async () => {
68-
const overrides = {
69-
aot: true,
70-
optimization: true,
71-
budgets: [{ type: 'anyComponentStyle', maximumWarning: '1b' }],
72-
};
73-
74-
const cssContent = `
75-
.foo { color: white; padding: 1px; }
76-
.buz { color: white; padding: 2px; }
77-
.bar { color: white; padding: 3px; }
78-
`;
68+
cssExtensions.forEach(ext => {
69+
it(`shows warnings for large component ${ext} when using 'anyComponentStyle' when AOT`, async () => {
70+
const overrides = {
71+
aot: true,
72+
optimization: true,
73+
budgets: [{ type: 'anyComponentStyle', maximumWarning: '1b' }],
74+
styles: [`src/styles.${ext}`],
75+
};
7976

80-
host.writeMultipleFiles({
81-
'src/app/app.component.css': cssContent,
82-
'src/assets/foo.css': cssContent,
83-
'src/styles.css': cssContent,
77+
const cssContent = `
78+
.foo { color: white; padding: 1px; }
79+
.buz { color: white; padding: 2px; }
80+
.bar { color: white; padding: 3px; }
81+
`;
82+
83+
host.writeMultipleFiles({
84+
[`src/app/app.component.${ext}`]: cssContent,
85+
[`src/assets/foo.${ext}`]: cssContent,
86+
[`src/styles.${ext}`]: cssContent,
87+
});
88+
89+
host.replaceInFile(
90+
'src/app/app.component.ts',
91+
'./app.component.css',
92+
`./app.component.${ext}`,
93+
);
94+
95+
const logger = new logging.Logger('');
96+
const logs: string[] = [];
97+
logger.subscribe(e => logs.push(e.message));
98+
99+
const run = await architect.scheduleTarget(targetSpec, overrides, { logger });
100+
const output = await run.result;
101+
expect(output.success).toBe(true);
102+
expect(logs.length).toBe(2);
103+
expect(logs.join()).toMatch(`WARNING.+app\.component\.${ext}`);
104+
await run.stop();
84105
});
85-
86-
const logger = new logging.Logger('');
87-
const logs: string[] = [];
88-
logger.subscribe(e => logs.push(e.message));
89-
90-
const run = await architect.scheduleTarget(targetSpec, overrides, { logger });
91-
const output = await run.result;
92-
expect(output.success).toBe(true);
93-
expect(logs.length).toBe(2);
94-
expect(logs.join()).toMatch(/WARNING.+app\.component\.css/);
95-
await run.stop();
96106
});
97107

98-
it(`shows error for large component css when using 'anyComponentStyle' when AOT`, async () => {
99-
const overrides = {
100-
aot: true,
101-
optimization: true,
102-
budgets: [{ type: 'anyComponentStyle', maximumError: '1b' }],
103-
};
104-
105-
const cssContent = `
106-
.foo { color: white; padding: 1px; }
107-
.buz { color: white; padding: 2px; }
108-
.bar { color: white; padding: 3px; }
109-
`;
108+
cssExtensions.forEach(ext => {
109+
it(`shows error for large component ${ext} when using 'anyComponentStyle' when AOT`, async () => {
110+
const overrides = {
111+
aot: true,
112+
optimization: true,
113+
budgets: [{ type: 'anyComponentStyle', maximumError: '1b' }],
114+
styles: [`src/styles.${ext}`],
115+
};
110116

111-
host.writeMultipleFiles({
112-
'src/app/app.component.css': cssContent,
113-
'src/assets/foo.css': cssContent,
114-
'src/styles.css': cssContent,
117+
const cssContent = `
118+
.foo { color: white; padding: 1px; }
119+
.buz { color: white; padding: 2px; }
120+
.bar { color: white; padding: 3px; }
121+
`;
122+
123+
host.writeMultipleFiles({
124+
[`src/app/app.component.${ext}`]: cssContent,
125+
[`src/assets/foo.${ext}`]: cssContent,
126+
[`src/styles.${ext}`]: cssContent,
127+
});
128+
129+
host.replaceInFile(
130+
'src/app/app.component.ts',
131+
'./app.component.css',
132+
`./app.component.${ext}`,
133+
);
134+
135+
const logger = new logging.Logger('');
136+
const logs: string[] = [];
137+
logger.subscribe(e => logs.push(e.message));
138+
139+
const run = await architect.scheduleTarget(targetSpec, overrides, { logger });
140+
const output = await run.result;
141+
expect(output.success).toBe(false);
142+
expect(logs.length).toBe(2);
143+
expect(logs.join()).toMatch(`ERROR.+app\.component\.${ext}`);
144+
await run.stop();
115145
});
116-
117-
const logger = new logging.Logger('');
118-
const logs: string[] = [];
119-
logger.subscribe(e => logs.push(e.message));
120-
121-
const run = await architect.scheduleTarget(targetSpec, overrides, { logger });
122-
const output = await run.result;
123-
expect(output.success).toBe(false);
124-
expect(logs.length).toBe(2);
125-
expect(logs.join()).toMatch(/ERROR.+app\.component\.css/);
126-
await run.stop();
127146
});
128147

129148
describe(`should ignore '.map' files`, () => {

0 commit comments

Comments
 (0)