Skip to content

Commit cbde8ef

Browse files
filipesilvaZhicheng Wang
authored and
Zhicheng Wang
committed
feat(@angular/cli): allow component css imports (angular#4667)
Fix angular#4285
1 parent 3506bd2 commit cbde8ef

File tree

8 files changed

+79
-163
lines changed

8 files changed

+79
-163
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"ember-cli-normalize-entity-name": "^1.0.0",
5656
"ember-cli-string-utils": "^1.0.0",
5757
"enhanced-resolve": "^3.1.0",
58+
"exports-loader": "^0.6.3",
5859
"extract-text-webpack-plugin": "^2.0.0-rc.3",
5960
"file-loader": "^0.10.0",
6061
"findup": "0.1.5",

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
1414
* Enumerate loaders and their dependencies from this file to let the dependency validator
1515
* know they are used.
1616
*
17-
* require('raw-loader')
17+
* require('exports-loader')
1818
* require('style-loader')
1919
* require('postcss-loader')
2020
* require('css-loader')
@@ -86,7 +86,12 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
8686
}
8787
];
8888

89-
const commonLoaders = ['postcss-loader'];
89+
const commonLoaders = [
90+
// css-loader doesn't support webpack.LoaderOptionsPlugin properly,
91+
// so we need to add options in its query
92+
`css-loader?${JSON.stringify({ sourceMap: cssSourceMap, importLoaders: 1 })}`,
93+
'postcss-loader'
94+
];
9095

9196
// load component css as raw strings
9297
let rules: any = baseRules.map(({test, loaders}) => ({
@@ -103,9 +108,6 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
103108
rules.push(...baseRules.map(({test, loaders}) => ({
104109
include: globalStylePaths, test, loaders: ExtractTextPlugin.extract({
105110
use: [
106-
// css-loader doesn't support webpack.LoaderOptionsPlugin properly,
107-
// so we need to add options in its query
108-
`css-loader?${JSON.stringify({ sourceMap: cssSourceMap })}`,
109111
...commonLoaders,
110112
...loaders
111113
],

packages/@angular/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"diff": "^3.1.0",
4141
"ember-cli-normalize-entity-name": "^1.0.0",
4242
"ember-cli-string-utils": "^1.0.0",
43+
"exports-loader": "^0.6.3",
4344
"extract-text-webpack-plugin": "^2.0.0-rc.3",
4445
"file-loader": "^0.10.0",
4546
"findup": "0.1.5",

tests/e2e/tests/build/styles/css.ts

-32
This file was deleted.
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {
2+
writeMultipleFiles,
3+
deleteFile,
4+
expectFileToMatch,
5+
replaceInFile
6+
} from '../../../utils/fs';
7+
import { expectToFail } from '../../../utils/utils';
8+
import { ng } from '../../../utils/process';
9+
import { stripIndents } from 'common-tags';
10+
import { updateJsonFile } from '../../../utils/project';
11+
12+
export default function () {
13+
const extensions = ['css', 'scss', 'less', 'styl'];
14+
let promise = Promise.resolve();
15+
16+
extensions.forEach(ext => {
17+
promise = promise.then(() => {
18+
return writeMultipleFiles({
19+
[`src/styles.${ext}`]: stripIndents`
20+
@import './imported-styles.${ext}';
21+
body { background-color: #00f; }
22+
`,
23+
[`src/imported-styles.${ext}`]: stripIndents`
24+
p { background-color: #f00; }
25+
`,
26+
[`src/app/app.component.${ext}`]: stripIndents`
27+
@import './imported-component-styles.${ext}';
28+
.outer {
29+
.inner {
30+
background: #fff;
31+
}
32+
}
33+
`,
34+
[`src/app/imported-component-styles.${ext}`]: stripIndents`
35+
h1 { background: #000; }
36+
`})
37+
// change files to use preprocessor
38+
.then(() => updateJsonFile('angular-cli.json', configJson => {
39+
const app = configJson['apps'][0];
40+
app['styles'] = [`styles.${ext}`];
41+
}))
42+
.then(() => replaceInFile('src/app/app.component.ts',
43+
'./app.component.css', `./app.component.${ext}`))
44+
// run build app
45+
.then(() => ng('build', '--extract-css', '--sourcemap'))
46+
// verify global styles
47+
.then(() => expectFileToMatch('dist/styles.bundle.css',
48+
/body\s*{\s*background-color: #00f;\s*}/))
49+
.then(() => expectFileToMatch('dist/styles.bundle.css',
50+
/p\s*{\s*background-color: #f00;\s*}/))
51+
// verify global styles sourcemap
52+
.then(() => expectToFail(() =>
53+
expectFileToMatch('dist/styles.bundle.css', '"mappings":""')))
54+
// verify component styles
55+
.then(() => expectFileToMatch('dist/main.bundle.js',
56+
/.outer.*.inner.*background:\s*#[fF]+/))
57+
.then(() => expectFileToMatch('dist/main.bundle.js',
58+
/h1.*background:\s*#000+/))
59+
// change files back
60+
.then(() => updateJsonFile('angular-cli.json', configJson => {
61+
const app = configJson['apps'][0];
62+
app['styles'] = ['styles.css'];
63+
}))
64+
.then(() => replaceInFile('src/app/app.component.ts',
65+
`./app.component.${ext}`, './app.component.css'));
66+
});
67+
});
68+
69+
return promise;
70+
}

tests/e2e/tests/build/styles/less.ts

-42
This file was deleted.

tests/e2e/tests/build/styles/scss.ts

-42
This file was deleted.

tests/e2e/tests/build/styles/stylus.ts

-42
This file was deleted.

0 commit comments

Comments
 (0)