如何解决Webpack 5中的ChunkLoadError: Loading hot update chunk secondary_app failed?

问题描述 投票:0回答:2

在设置新项目进行开发时,应用程序不支持热重载。该应用程序由多个应用程序组成,分别位于不同的文件夹中。目前,

html_nodes_prototype
文件夹中有2个应用程序
second_app
src/apps
。每个应用程序都由
index.html
style.css
main.js
组成,当我访问链接
http://localhost:8080/second_app/second_app.html
时,应用程序会显示,但如果我更改
src/apps/second_app/main.js
中的某些内容。它不支持热重载,我必须手动重新加载才能获取更改。我已经设置了一个 webpack-dev-server。控制台有错误提示。我无法弄清楚配置文件出了什么问题。

控制台出错

[HMR] Update failed: ChunkLoadError: Loading hot update chunk second_app failed.
(missing: http://localhost:8080/second_app.5b0047c2bf2b48c8a084.hot-update.js)
    at http://localhost:8080/second_app/second_app.bundle.js:987:26
    at new Promise (<anonymous>)
    at loadUpdateChunk (http://localhost:8080/second_app/second_app.bundle.js:982:20)
    at http://localhost:8080/second_app/second_app.bundle.js:1411:29
    at Array.forEach (<anonymous>)
    at Object.__webpack_require__.hmrC.jsonp (http://localhost:8080/second_app/second_app.bundle.js:1406:22)
    at http://localhost:8080/second_app/second_app.bundle.js:819:45
    at Array.reduce (<anonymous>)
    at http://localhost:8080/second_app/second_app.bundle.js:815:53

paths.js

const path = require("path");

module.exports = {
  // Source files
  src: path.resolve(__dirname, "../src"),

  // Production build files
  build: path.resolve(__dirname, "../dist"),

  // public path
  public: path.resolve(__dirname, "../public/"),
};

webpack.dev.js

const common = require("./webpack.common");
var HtmlWebpackPlugin = require("html-webpack-plugin");
const merge = require("webpack-merge").merge;
const paths = require("./paths");

module.exports = merge(common, {
  mode: "development",

  output: {
    filename: "[name]/[name].bundle.js",
    path: paths.build,
  },

  module: {
    rules: [
      { test: /\.scss$/, use: ["style-loader", "css-loader", "sass-loader"] },
      { test: /\.css$/, use: ["style-loader", "css-loader"] },
    ],
  },

  devServer: {
    historyApiFallback: true,
    // contentBase: paths.build,
    open: true,
    compress: true,
    hot: true,
    port: 8080,
  },

  plugins: [
    new HtmlWebpackPlugin({
      filename: "html_nodes_prototype/html_nodes_prototype.html",
      title: "HTML Nodes",
      template: "src/apps/html_nodes_prototype/index.html",
      chunks: ["html_nodes_prototype", "vendor"],
    }),
    new HtmlWebpackPlugin({
      filename: "second_app/second_app.html",
      title: "Second app",
      hot: true,
      template: "src/apps/second_app/index.html",
      chunks: ["second_app", "vendor"],
    }),
  ],
});

webpack.common.js

const paths = require("./paths");

module.exports = {
  mode: "development",

  entry: {
    html_nodes_prototype: paths.src + "/apps/html_nodes_prototype/main.js",
    second_app: paths.src + "/apps/second_app/main.js",
    vendor: paths.src + "/apps/_vendor/vendor.js",
  },

  module: {
    rules: [{ test: /\.m?js$/, use: ["babel-loader"] }],
  },
};
javascript webpack webpack-dev-server
2个回答
51
投票

runtimeChunk: 'single'
添加到 webpack.dev.js 对我有用。找到答案在这里

module.exports = {
    ...,
    optimization: {
        runtimeChunk: 'single'
    },
    ...
}

0
投票

也适用于我,但在主机应用程序中出现以下错误

加载脚本失败。 (缺少:http://localhost:3000/internal/postpo/postPORemote.js) 从 3035 加载“./App”时 脚本外部加载错误 在3035(http://localhost:5000/main.039c8f05349a71d72a27.js:28:25) 在 webpack_require (http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:34:32) 在 initExternal (http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:352:28) 在 webpack_require.I (http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:365:15) 在 http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:873:47 在4914(http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:927:26) 在 http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:961:56 在Array.forEach() 在 webpack_require.f.consumes (http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:942:36) 在http://localhost:5000/runtime.a439e888716b1ad0ac8c.js:146:40

© www.soinside.com 2019 - 2024. All rights reserved.