I am trying to add an external reference to a CSS file in Angular2 using Webpack. My css is defined as
{ test: /\\.css$/, loader: \"style-loader!css-loader\
My current Solution for WebPack 2 with Angular 2 for css or scss Loader;
{
test: /\.css$/,
loader: [
ExtractTextPlugin.extract({ fallbackLoader: "style-loader", loader: 'css-loader' }),
'to-string-loader',
'css-loader'
],
exclude: [helpers.root('Content')],
include: [helpers.root('App')]
},
{
test: /\.scss$/,
loader: [ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: ['css-loader'] }),
'to-string-loader',
'css-loader',
'sass-loader'
],
exclude: [helpers.root('Content')],
include: [helpers.root('App')]
},
Plugins:
new ExtractTextPlugin({ filename: "[name].css" }),
And now you can use the following in your component
@Component({
selector: 'logmonitor',
styleUrls: ['./logmonitor.component.scss', './logmonitor.component.css'],
templateUrl: './logmonitor.component.html'
})
my current DevDependencies are:
"devDependencies": {
"@types/node": "6.0.51",
"angular2-template-loader": "0.6.0",
"awesome-typescript-loader": "2.2.4",
"css-loader": "0.26.1",
"css-to-string-loader": "0.1.2",
"file-loader": "0.9.0",
"url-loader": "0.5.7",
"html-loader": "0.4.4",
"svg-url-loader": "1.1.0",
"less": "2.7.1",
"less-loader": "2.2.3",
"node-sass": "3.13.0",
"sass-loader": "4.0.2",
"style-loader": "0.13.1",
"raw-loader": "0.5.1",
"to-string-loader": "1.1.5",
"clean-webpack-plugin": "0.1.4",
"extract-text-webpack-plugin": "2.0.0-beta.4",
"html-webpack-plugin": "2.21.0",
"webpack-notifier": "1.4.1",
"webpack": "2.1.0-beta.27",
"webpack-dev-middleware": "1.8.4",
"webpack-dev-server": "2.1.0-beta.12",
"webpack-md5-hash": "0.0.5",
"webpack-merge": "0.17.0",
"typescript": "2.0.10",
"typings": "2.0.0"
}
Update for Webpack 2.2.1 and extract-text-webpack-plugin 2.0.0-rc.3 here the solution above is not working any more.
addional devdependencies
"extract-text-webpack-plugin": "2.0.0-rc.3",
"postcss-loader": "1.3.0",
"postcss-import": "9.1.0",
"autoprefixer": "6.7.2",
"webpack": "2.2.1",
you need to add a postcss.config.js in your root of your app with the content
module.exports = {
plugins: [
require('postcss-import')(),
require('autoprefixer')()
]
};
and the new rule for scss is the following
{
test: /\.scss$/,
loader: [
{ loader: 'raw-loader' },
{ loader: 'sass-loader', query: { sourceMaps: true } },
{ loader: 'postcss-loader' }
],
exclude: [helpers.root('Content')],
include: [helpers.root('App')]
}