I am setting up a basic Webpack installation and want to have automatic browser reloading of preprocessed CSS in @imported files using PostCSS and the PreCSS plugin. At present if I modify and save an @imported file the browser does not refresh (body.css in below example). If I then save the root referenced CSS file (styles.css) then the browser refreshes and also reflects any changes made to the @imported file.
I have tried with using the configurable webpack-dev-server, and using a server.js. I have tried without and with hot model reloading (HMR) installed.
Is there a way to have webpack watch @imported CSS files, or am I fundamentally missing something here?
package.json
"dependencies": {
"react": "^0.14.0",
"react-dom": "^0.14.0"
},
"devDependencies": {
"autoprefixer": "^6.3.1",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"postcss-loader": "^0.8.0",
"postcss-scss": "^0.1.3",
"precss": "^1.4.0",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.0",
"webpack": "^1.12.13",
"webpack-dev-server": "^1.14.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"start-dev-server": "webpack-dev-server 'webpack-dev-server/client?/' --host 0.0.0.0 --port 9090 --progress --colors",
"build": "echo \"Build hasn't been specified yet\" && exit 1"
},
webpack.config.js
/*global require module __dirname*/
var path = require('path'),
webpack = require('webpack'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
autoprefixer = require('autoprefixer'),
precss = require('precss');
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:9090',
'webpack/hot/only-dev-server',
'./entry.js'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
devtool: 'source-map',
module: {
loaders: [
{
test: /\.css$/i,
loaders: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap&modules&importLoaders=1!postcss-loader')
}
]
},
postcss: function() {
return [precss, autoprefixer];
},
plugins: [
// Set the name of the single CSS file here.
new ExtractTextPlugin('main.css', { allChunks: true }),
new webpack.HotModuleReplacementPlugin()
]
};
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/static/main.css" />
</head>
<body>
<script src="http://localhost:9090/webpack-dev-server.js"></script>
<script type="text/javascript" src="/static/bundle.js" charset="utf-8"></script>
</body>
</html>
entry.js
require("./styles.css");
document.write(require("./content.js"));
styles.css
@import "body.css";
body {
/*background: yellow; */
font-size: 30px;
}
div {
display: flex;
}
body.css
$color: yellow;
body {
background: $color;
}
div {
color: white;
a {
color: green;
}
}
div {
display: flex;
}
After much searching on the internet the answer can be found in these two threads:
Thanks to @zzq889 for the following example using postcss-import which gets around the limitation in postcss-partial-import:
var postcssImport = require('postcss-import');
...
postcss: function (webpack) {
return [
postcssImport({
addDependencyTo: webpack
})
];
}
This will hopefully in future be working with just precss and its dependent library postcss-partial-import with this pull request or a solution based on it.