Skip to content

Commit 9bacba3

Browse files
committedDec 8, 2021
fix(@angular-devkit/build-angular): differentiate components and global styles using file query instead of filename
Previously, we introduced the `ngResource` query to Angular component resources we now use it with `resourceQuery` to differentiate between global and components styles, since in some cases while unlikely a file can be used as a component and global style. Closes #7245
1 parent f4cd684 commit 9bacba3

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -655,4 +655,26 @@ describe('Browser Builder styles', () => {
655655
result = await browserBuild(architect, host, target, { optimization: true });
656656
expect(await result.files['styles.css']).toContain('rgba(0,0,0,.15)');
657657
});
658+
659+
it('works when using the same css file in `styles` and `stylesUrl`', async () => {
660+
host.writeMultipleFiles({
661+
'src/styles.css': `
662+
div { color: red }
663+
`,
664+
'./src/app/app.component.ts': `
665+
import { Component } from '@angular/core';
666+
667+
@Component({
668+
selector: 'app-root',
669+
templateUrl: './app.component.html',
670+
styleUrls: ['../styles.css']
671+
})
672+
export class AppComponent {
673+
title = 'app';
674+
}
675+
`,
676+
});
677+
678+
await browserBuild(architect, host, target, { styles: ['src/styles.css'] });
679+
});
658680
});

‎packages/angular_devkit/build_angular/src/builders/browser/tests/options/named-chunks_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
1111

1212
const MAIN_OUTPUT = 'dist/main.js';
1313
const NAMED_LAZY_OUTPUT = 'dist/src_lazy-module_ts.js';
14-
const UNNAMED_LAZY_OUTPUT = 'dist/8.js';
14+
const UNNAMED_LAZY_OUTPUT = 'dist/459.js';
1515

1616
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
1717
describe('Option: "namedChunks"', () => {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
379379
oneOf: [
380380
// Component styles are all styles except defined global styles
381381
{
382-
exclude: globalStylePaths,
383382
use: componentStyleLoaders,
383+
resourceQuery: /\?ngResource/,
384384
type: 'asset/source',
385385
},
386386
// Global styles are only defined global styles
387387
{
388-
include: globalStylePaths,
389388
use: globalStyleLoaders,
389+
resourceQuery: { not: [/\?ngResource/] },
390390
},
391391
],
392392
},

‎tests/legacy-cli/e2e/tests/build/worker.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { join } from 'path';
10-
import {
11-
appendToFile,
12-
expectFileToExist,
13-
expectFileToMatch,
14-
replaceInFile,
15-
writeFile,
16-
} from '../../utils/fs';
9+
import { expectFileToExist, expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs';
1710
import { ng } from '../../utils/process';
1811

1912
export default async function () {
20-
const workerPath = join('src', 'app', 'app.worker.ts');
21-
const snippetPath = join('src', 'app', 'app.component.ts');
13+
const workerPath = 'src/app/app.worker.ts';
14+
const snippetPath = 'src/app/app.component.ts';
2215
const projectTsConfig = 'tsconfig.json';
2316
const workerTsConfig = 'tsconfig.worker.json';
2417

@@ -33,7 +26,7 @@ export default async function () {
3326
await expectFileToMatch('dist/test-project/main.js', 'src_app_app_worker_ts');
3427

3528
await ng('build', '--output-hashing=none');
36-
const chunkId = '310';
29+
const chunkId = '151';
3730
await expectFileToExist(`dist/test-project/${chunkId}.js`);
3831
await expectFileToMatch('dist/test-project/main.js', chunkId);
3932

0 commit comments

Comments
 (0)