Skip to content

Commit

Permalink
feat(@angular/cli): allow component css imports (#4667)
Browse files Browse the repository at this point in the history
Fix #4285
  • Loading branch information
filipesilva authored and hansl committed Feb 14, 2017
1 parent 61fc099 commit e55cb82
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 164 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"ember-cli-normalize-entity-name": "^1.0.0",
"ember-cli-string-utils": "^1.0.0",
"enhanced-resolve": "^3.1.0",
"exports-loader": "^0.6.3",
"extract-text-webpack-plugin": "^2.0.0-rc.3",
"file-loader": "^0.10.0",
"findup": "0.1.5",
Expand Down
18 changes: 12 additions & 6 deletions packages/@angular/cli/models/webpack-configs/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
* Enumerate loaders and their dependencies from this file to let the dependency validator
* know they are used.
*
* require('raw-loader')
* require('exports-loader')
* require('style-loader')
* require('postcss-loader')
* require('css-loader')
Expand Down Expand Up @@ -86,21 +86,27 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
}
];

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

// load component css as raw strings
let rules: any = baseRules.map(({test, loaders}) => ({
exclude: globalStylePaths, test, loaders: ['raw-loader', ...commonLoaders, ...loaders]
exclude: globalStylePaths, test, loaders: [
'exports-loader?module.exports.toString()',
...commonLoaders,
...loaders
]
}));

// load global css as css files
if (globalStylePaths.length > 0) {
rules.push(...baseRules.map(({test, loaders}) => ({
include: globalStylePaths, test, loaders: ExtractTextPlugin.extract({
use: [
// css-loader doesn't support webpack.LoaderOptionsPlugin properly,
// so we need to add options in its query
`css-loader?${JSON.stringify({ sourceMap: cssSourceMap })}`,
...commonLoaders,
...loaders
],
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"diff": "^3.1.0",
"ember-cli-normalize-entity-name": "^1.0.0",
"ember-cli-string-utils": "^1.0.0",
"exports-loader": "^0.6.3",
"extract-text-webpack-plugin": "^2.0.0-rc.3",
"file-loader": "^0.10.0",
"findup": "0.1.5",
Expand Down
32 changes: 0 additions & 32 deletions tests/e2e/tests/build/styles/css.ts

This file was deleted.

70 changes: 70 additions & 0 deletions tests/e2e/tests/build/styles/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
writeMultipleFiles,
deleteFile,
expectFileToMatch,
replaceInFile
} from '../../../utils/fs';
import { expectToFail } from '../../../utils/utils';
import { ng } from '../../../utils/process';
import { stripIndents } from 'common-tags';
import { updateJsonFile } from '../../../utils/project';

export default function () {
const extensions = ['css', 'scss', 'less', 'styl'];
let promise = Promise.resolve();

extensions.forEach(ext => {
promise = promise.then(() => {
return writeMultipleFiles({
[`src/styles.${ext}`]: stripIndents`
@import './imported-styles.${ext}';
body { background-color: #00f; }
`,
[`src/imported-styles.${ext}`]: stripIndents`
p { background-color: #f00; }
`,
[`src/app/app.component.${ext}`]: stripIndents`
@import './imported-component-styles.${ext}';
.outer {
.inner {
background: #fff;
}
}
`,
[`src/app/imported-component-styles.${ext}`]: stripIndents`
h1 { background: #000; }
`})
// change files to use preprocessor
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['styles'] = [`styles.${ext}`];
}))
.then(() => replaceInFile('src/app/app.component.ts',
'./app.component.css', `./app.component.${ext}`))
// run build app
.then(() => ng('build', '--extract-css', '--sourcemap'))
// verify global styles
.then(() => expectFileToMatch('dist/styles.bundle.css',
/body\s*{\s*background-color: #00f;\s*}/))
.then(() => expectFileToMatch('dist/styles.bundle.css',
/p\s*{\s*background-color: #f00;\s*}/))
// verify global styles sourcemap
.then(() => expectToFail(() =>
expectFileToMatch('dist/styles.bundle.css', '"mappings":""')))
// verify component styles
.then(() => expectFileToMatch('dist/main.bundle.js',
/.outer.*.inner.*background:\s*#[fF]+/))
.then(() => expectFileToMatch('dist/main.bundle.js',
/h1.*background:\s*#000+/))
// change files back
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['styles'] = ['styles.css'];
}))
.then(() => replaceInFile('src/app/app.component.ts',
`./app.component.${ext}`, './app.component.css'));
});
});

return promise;
}
42 changes: 0 additions & 42 deletions tests/e2e/tests/build/styles/less.ts

This file was deleted.

42 changes: 0 additions & 42 deletions tests/e2e/tests/build/styles/scss.ts

This file was deleted.

42 changes: 0 additions & 42 deletions tests/e2e/tests/build/styles/stylus.ts

This file was deleted.

0 comments on commit e55cb82

Please sign in to comment.