20

while setup a new project for development, the app is not hot reloading. The app consists of multiple apps separated in its different folders.Currently, there are 2 apps html_nodes_prototype and second_app in src/apps folder. Each app consist of index.html, style.css and main.js when I visit the link http://localhost:8080/second_app/second_app.html, the app shows but If I change something in src/apps/second_app/main.js. It doesn't hot reload, I've to manually reload to get changes. I've set up a webpack-dev-server. Error prompts in the console. I couldn't figure out what's wrong with the config file.

Error in console

[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"] }],
  },
};
DecPK
  • 24,537
  • 6
  • 26
  • 42

1 Answers1

40

Adding runtimeChunk: 'single' to webpack.dev.js works for me. Found the answer here

module.exports = {
    ...,
    optimization: {
        runtimeChunk: 'single'
    },
    ...
}
Sameer
  • 4,758
  • 3
  • 20
  • 41
  • it`s work but when finish hot reload the component state is 'reloaded' and state is cleared :/ – LuanLuanLuan Nov 12 '21 at 20:29
  • Sweet upvote for @Sameer . It seems that HtmlWebpackPlugin kinda force compile twice least for me that's running e.g. npx webpack serve using webpack-dev-server . Once I included suggest snippet by Sameer , all files started reloading properly . Life saver , thanks ! – projektorius96 Jan 24 '22 at 15:47
  • 2
    @LuanLuanLuan you must use `devServer: {devMiddleware: {writeToDisk : true}}` to stay persisted for your data . It's achievable since Webpack v5 . For more please refer to [this](https://webpack.js.org/configuration/dev-server/#devserverdevmiddleware) – projektorius96 Jan 24 '22 at 15:56
  • @Sameer Thanks for this solution and the accompanying issue link. – anand Feb 10 '22 at 08:48