I have a React + Webpack + Express setup. The React files are bundled by Webpack and stored in a /dist
directory. In development mode Webpack is running in the background in watch mode. New changes are updated inside the dist directory. The Express server is running separately from the Webpack process. On request localhost:3000/
the Express server sends the client the dist/index.html
file.
I want to enable a page reload everytime the Webpack watcher is done compiling the React files (Frontend). What I have seen so far is people hot reloading the express server via webpack, the moment something changes, but that doesn’t update the client.
Is there a way to hot-reload the client (via Express?) when Webpack re-bundles the frontend code?
webpack.config.js
const path = require('path');
const Dotenv = require('dotenv-webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: 'development',
devtool: 'source-map',
entry: {
main: './src/index.js',
vendor: ['semantic-ui-react'],
},
output:{
path: path.join(__dirname, '/dist'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/i,
use: [{
loader: "style-loader"
}, {
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "sass-loader", options: {
sourceMap: true
}
}]
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
},
{
test: /\.css$/,
include: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: '../',
hmr: process.env.NODE_ENV === 'development',
},
},
'css-loader',
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new Dotenv(),
new MiniCssExtractPlugin('styles.css')
]
};
server.js
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.static('dist'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/dist/index.html');
});
app.listen(port, () => console.log('App listening on port 3000!'));