WebPack 热重载不适用于 HTML 文件

ewc*_*der 6 javascript webpack webpack-dev-server webpack-hmr

我可以在 webpack 中对 JS 和 SCSS 文件使用热模块替换,并且工作正常,但在尝试热重新加载 html 文件时遇到问题。对我有用的另一个选项是将“watchContentBase:true”属性添加到我的开发服务器属性中,但这会重新加载浏览器。我想要拥有它,这样当我保存文件时,浏览器会更新其内容,而无需完全重新加载页面。

<pre>
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    devServer: {
        port: 8080,
        contentBase: path.resolve(__dirname, './src'),
        hot: true,
        open: true,
    },
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.s[ac]ss$/i,
                use: [
                    // Creates `style` nodes from JS strings
                    'style-loader',
                    // Translates CSS into CommonJS
                    'css-loader',
                    // Compiles Sass to CSS
                    'sass-loader',
                ],
            },
            {
                test: /\.html$/i,
                loader: 'html-loader',
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html',
        }),
    ],
};
</pre>

======== Index Js entry point ========

import tripleMe from './tripleMe';
import './sass/main.scss';

if (module.hot) {
    module.hot.accept();
}
Run Code Online (Sandbox Code Playgroud)

Mak*_*dyk 0

在 Webpack 5 中contentBase,您使用的属性已切换为static

devServer: {
      port: 8080,
      hot: "only",
      static: {
        directory: path.join(__dirname, './'),
        serveIndex: true,
      },
    },
Run Code Online (Sandbox Code Playgroud)