Javascript 如何为热重新加载html代码配置Webpack4?

Javascript 如何为热重新加载html代码配置Webpack4?,javascript,webpack-dev-server,webpack-4,Javascript,Webpack Dev Server,Webpack 4,我只想在index.html上热重新加载html代码,但我不知道怎么做。为css代码启用热重新加载很简单,但当涉及html或js时,它会根据更改不断地重新加载页面。我尝试了许多不同的方法来实现它,包括使用contentBase/watchContentBase、polls,但似乎没有任何效果。基本上,我希望能够运行类似于vue init webpack的东西。默认情况下,vue webpack具有配置,您可以在其中热加载包括js html css在内的所有组件。请帮忙! 这是我的配置: webp

我只想在index.html上热重新加载html代码,但我不知道怎么做。为css代码启用热重新加载很简单,但当涉及html或js时,它会根据更改不断地重新加载页面。我尝试了许多不同的方法来实现它,包括使用contentBase/watchContentBase、polls,但似乎没有任何效果。基本上,我希望能够运行类似于
vue init webpack
的东西。默认情况下,vue webpack具有配置,您可以在其中热加载包括js html css在内的所有组件。请帮忙! 这是我的配置:

webpack.config.js:

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


module.exports = {
  entry: './src/js/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
    filename: 'js/app.js'
  },
  devServer: {
    hot:true,
    inline:true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject:true,
      filename: 'index.html',
      template: 'index.html',
      alwaysWriteToDisk: false
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
  ],
};
import '../sass/app.scss';
import '../../index.html';
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1 class="h1">Hello world!!!!</h1>
<script src="src/js/app.js"></script>
</body>
</html
app.js:

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


module.exports = {
  entry: './src/js/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
    filename: 'js/app.js'
  },
  devServer: {
    hot:true,
    inline:true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject:true,
      filename: 'index.html',
      template: 'index.html',
      alwaysWriteToDisk: false
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
  ],
};
import '../sass/app.scss';
import '../../index.html';
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1 class="h1">Hello world!!!!</h1>
<script src="src/js/app.js"></script>
</body>
</html
index.html:

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


module.exports = {
  entry: './src/js/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
    filename: 'js/app.js'
  },
  devServer: {
    hot:true,
    inline:true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject:true,
      filename: 'index.html',
      template: 'index.html',
      alwaysWriteToDisk: false
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
  ],
};
import '../sass/app.scss';
import '../../index.html';
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1 class="h1">Hello world!!!!</h1>
<script src="src/js/app.js"></script>
</body>
</html
}