当使用带有快速服务器的webpack-dev-middle时,我想使用webpack-hot-middleware对提取的css文件进行工作(热重载)。
它适用于webpack-dev-server。 但是我想用快递来使用它。
server.js
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const app = express();
const config = require('./webpack.config.js');
const compiler = webpack(config);
app.set('view engine', 'pug')
// Tell express to use the webpack-dev-middleware and use the webpack.config.js
// configuration file as a base.
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
// HMR
app.use(require("webpack-hot-middleware")(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}));
// Render/serve HTML
app.get('/', (req, res) => {
res.render('layout')
})
// Serve the files on port 3000.
app.listen(3000, function() {
console.log('Example app listening on port 3000!\n');
});

webpack.config.js
const path = require('path')
const webpack = require('webpack')
const webpackHotMiddleware = require('webpack-hot-middleware')
const hotMidConf = 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'
module.exports = {
mode: 'development',
entry: {
bundle: [hotMidConf, './src/index.js'],
style: [hotMidConf, './src/style.scss']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader'
},
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: 'style.css'
}
},
{
loader: 'extract-loader',
options: {
publicPath: './'
}
},
{loader: 'css-loader'},
{
loader: 'sass-loader',
options: {
includePaths: [path.resolve(__dirname, 'node_modules')]
}
}
]
},
]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
}

其他所有工作,当我构建时,我从scss获取css文件。我相信style-loader
是什么让它在没有被提取时起作用,但当我尝试使用它时似乎没有做任何事情。
我使用extract-text-webpack-plugin找到了很多答案,但据我所知,这对webpack 4不起作用......所以我使用了带文件加载器的extract-loader。