Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@angular/cli): allow component css imports #4667

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need a comment with require('exports-loader') at the top of the file with the others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

...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.