webpack5配置打包后就无法实现hot功能

参照webpack5官方文档配置的webpack.config.js,如下:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    mode: 'production',
    entry: {
        index: './src/js/index.js',
        home: './src/js/home.js'
    },
    output: {
        filename: '[name].[hash].bundle.js',
        path: path.resolve(__dirname,"assets"),
        clean: true,
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../',
                        }
                    },
                     'css-loader',
                ],
            },   
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/i,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            outputPath: 'images',
                            name: '[name].[hash].[ext]'
                        }
                    }
                ],
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/i,
                type: 'asset/resource',
            },
        ]
    },
    devServer: {
        contentBase: path.join(__dirname, '/'),
        port: 8888,
        hot: true,
        open: true,
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: '../index.html',
            template: './src/index.html',
            inject: 'body',
            chunks: ['index'],
            showErrors: true
        }),
        new HtmlWebpackPlugin({
            filename: '../home.html',
            template: './src/home.html',
            inject: 'body',
            chunks: ['home'],
            showErrors: true 
        }),
        new MiniCssExtractPlugin({
            filename: 'css/[name].min.css',
        }),
        new webpack.HotModuleReplacementPlugin(),

    ],
};

这么做就将html、css、img、js全部打包了,如果html不打包倒是可以热更,html打包后就无法实现热更功能了,start启动后只会打开打包后的页面,内容变更如果不build自然就无法热更

package.json如下:

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "main": "index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "scripts": {
    "start": "webpack serve --config webpack.config.js",
    "build": "webpack"
  },
  "devDependencies": {
    "babel-loader": "^8.2.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-latest": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^5.2.5",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.3.1",
    "mini-css-extract-plugin": "^1.6.0",
    "style-loader": "^2.0.0",
    "webpack": "^5.37.1",
    "webpack-cli": "^4.7.0",
    "webpack-dev-server": "^3.11.2",
    "yarn": "^1.22.10"
  },
  "dependencies": {}
}
阅读 1.9k
1 个回答

根据你提供的 demo 发现,filename 路径有问题

  new HtmlWebpackPlugin({
      filename: "index.html", 
      // 也可以这样 filename: "./index.html", 
      template: "./src/index.html",
      inject: "body",
      chunks: ["index"],
      showErrors: true,
    }),
    new HtmlWebpackPlugin({
      filename: "home.html",
      template: "./src/home.html",
      inject: "body",
      chunks: ["home"],
      showErrors: true,
    })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题