I would like to use webpack-hot-middleware to work(hot reload) on extracted css files when using webpack-dev-middle with express server.
It works fine with webpack-dev-server. But I want to use it with express.
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()
]
}
Everything else works, when I build I get the css file from the scss. I believe style-loader
is what makes this work for when it is not being extracted but doesn't seem to do anything when I try it with it.
I have found a lot of answers using extract-text-webpack-plugin but this does not work with webpack 4 as far as I can tell... So I am using extract-loader with file-loader.