Answer the question
In order to leave comments, you need to log in
Webpack, why doesn't LiveReload work on HTML changes if Hot Module Replacement is enabled?
If you set hot: true in Webpack 's devServer settings , then Hot Module Replacement for CSS works and changes are applied without a full page reload. But for some reason, when changing HTML files , LiveReload does not work , you need to update the page manually for the changes to be applied. If you disable hot: true
in the configuration file in devServer , then when changing HTML files, LiveReload works fine, the page reloads itself, but Hot Module Replacement for CSS does not work
, when you change the CSS , the page reloads completely.
Is this behavior supposed to be? Why is this happening, how can you enable Hot Module Replacement for CSS , but still have LiveReload working when changing HTML files? I use the HtmlWebpackPlugin
plugin
to generate multiple HTML files .
Here are the config files below: webpack.common.js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
module.exports = mode => {
const PRODUCTION = mode === 'production';
return {
entry: {
app: './src/index.js',
},
output: {
filename: 'js/[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: 'img/[path][name].[ext]',
outputPath: 'img',
},
},
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
hash: false,
template: 'src/index.html',
filename: 'index.html',
}),
new webpack.DefinePlugin({
PRODUCTION: PRODUCTION,
}),
new CopyPlugin([
{ from: 'src/img', to: 'img' },
{ from: 'src/fonts', to: 'fonts' },
]),
],
}
};
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
module.exports = (env, argv) => {
return merge(common(argv.mode), {
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
overlay: {
warnings: true,
errors: true
},
port: 8081,
hot: true,
},
watchOptions: {
aggregateTimeout: 100,
},
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
],
}
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
],
});
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question