我可以使css模塊工作,但不是在熱重載。Webpack開發服務器和css模塊
當第一次加載,作風出現的方式應該:
但在更改CSS文件,它打破了,並且需要進行完整的重新加載後:
我使用的是如下所示的css模塊:
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const pack = {
entry: [
path.join(__dirname, 'src/app/index.js'),
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/',
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/app/index.html',
inject: 'body',
filename: 'index.html',
}),
],
module: {
loaders: [
{
test: /\.css$/,
include: /src\/app/,
loaders: [
'style?sourceMap',
'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
],
},
{
test: /\.js?$/,
include: /src\/app/,
loader: 'babel',
},
{
test: /\.woff(2)?(\?[a-z0-9#=&.]+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff',
},
{
test: /\.(ttf|eot|svg)(\?[a-z0-9#=&.]+)?$/,
loader: 'file',
},
],
},
};
module.exports = pack;
webpack.development.config.js
const webpack = require('webpack');
const pack = require('./webpack.config');
pack.entry.unshift('react-hot-loader/patch');
pack.entry.unshift('webpack/hot/only-dev-server');
pack.entry.unshift('webpack-dev-server/client?http://localhost:3000');
pack.plugins.push(new webpack.HotModuleReplacementPlugin());
pack.plugins.push(new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
}));
module.exports = pack;
正如我已經注意到,CSS類,它試圖在組件中獲取仍然是相同的,不應該爲每個文件更改/重新生成一個新的哈希?
您是否在源代碼中添加了'module.accept()'語句? –
不......我解決了它改變'pack.entry.unshift('webpack/hot/only-dev-server');'到'pack.entry.unshift('webpack/hot/dev-server');'' 。它刷新了一切,但按預期更新了CSS。 –
這也許可以解釋爲什麼你會得到全面更新。 –