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

发布于 2022-09-12 23:57:10 字数 3592 浏览 15 评论 0

参照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": {}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2022-09-19 23:57:10

根据你提供的 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,
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文