Skip to content

Commit 07e776e

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): fail build when importing CSS files as an ECMA modules
BREAKING CHANGE: We now issue a build time error since importing a CSS file as an ECMA module is non standard Webpack specific feature, which is not supported by the Angular CLI. This feature was never truly supported by the Angular CLI, but has as such for visibility.
1 parent 7e7de68 commit 07e776e

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { logging } from '@angular-devkit/core';
10+
import { buildWebpackBrowser } from '../../index';
11+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
12+
13+
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
14+
describe('Behavior: "Style Unsupported"', () => {
15+
it('errors when importing a css file as an ECMA module (Webpack specific behaviour)', async () => {
16+
await harness.writeFiles({
17+
'src/test-style.css': '.test-a {color: red}',
18+
'src/main.ts': `import './test-style.css'`,
19+
});
20+
21+
harness.useTarget('build', {
22+
...BASE_OPTIONS,
23+
styles: [],
24+
});
25+
26+
const { result, logs } = await harness.executeOnce();
27+
28+
expect(result?.success).toBeFalse();
29+
expect(logs).toContain(
30+
jasmine.objectContaining<logging.LogEntry>({
31+
message: jasmine.stringMatching('./src/test-style.css:1:0 - Error'),
32+
}),
33+
);
34+
});
35+
});
36+
});

packages/angular_devkit/build_angular/src/builders/browser/tests/options/allowed-common-js-dependencies_spec.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
1414
describe('Option: "allowedCommonJsDependencies"', () => {
1515
describe('given option is not set', () => {
1616
for (const aot of [true, false]) {
17-
it(`should not show warning for styles import in ${aot ? 'AOT' : 'JIT'} Mode`, async () => {
18-
await harness.writeFile('./test.css', `body { color: red; };`);
19-
await harness.appendToFile('src/app/app.component.ts', `import '../../test.css';`);
20-
21-
harness.useTarget('build', {
22-
...BASE_OPTIONS,
23-
allowedCommonJsDependencies: [],
24-
aot,
25-
});
26-
27-
const { result, logs } = await harness.executeOnce();
28-
29-
expect(result?.success).toBe(true);
30-
expect(logs).not.toContain(
31-
jasmine.objectContaining<logging.LogEntry>({
32-
message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
33-
}),
34-
);
35-
});
36-
3717
it(`should show warning when depending on a Common JS bundle in ${
3818
aot ? 'AOT' : 'JIT'
3919
} Mode`, async () => {

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
384384
{
385385
use: componentStyleLoaders,
386386
type: 'asset/source',
387+
resourceQuery: /\?ngResource/,
387388
},
388389
],
389390
},

0 commit comments

Comments
 (0)