Skip to content

HMR with chunk splitting fails to reload the modified chunk if it’s not the last #730

Closed
@andersk

Description

@andersk
  • Operating System: Linux
  • Node Version: 14.16.0
  • NPM Version: 6.14.11
  • webpack Version: 5.27.2
  • mini-css-extract-plugin Version: 1.3.9

Expected Behavior

Hot module reload should work with all CSS chunks.

Actual Behavior

When there are multiple CSS chunks due to optimization.splitChunks, hot module reload only works with the last CSS chunk.

Code

https://gist.github.com/andersk/2d45d4363478b22e998e177836ebce12

// webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  mode: "development",
  entry: {
    foo: ["./common.css", "./foo.css"],
    bar: ["./common.css", "./bar.css"],
  },
  module: {
    rules: [
      { test: /\.css$/, use: [MiniCssExtractPlugin.loader, "css-loader"] },
    ],
  },
  optimization: {
    splitChunks: { chunks: "all", minSize: 1 },
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: "foo.html",
      template: "foo.html",
      chunks: ["foo"],
    }),
    new HtmlWebpackPlugin({
      filename: "bar.html",
      template: "bar.html",
      chunks: ["bar"],
    }),
    new MiniCssExtractPlugin(),
  ],
};

How Do We Reproduce?

  1. git clone https://gist.github.com/andersk/2d45d4363478b22e998e177836ebce12 css-hmr-test
    cd css-hmr-test
    npm install
    npx webpack serve --hot
  2. Open http://localhost:8080/foo.html.
  3. Edit foo.css, e.g. by changing green to blue, and see that the change is applied in the browser immediately.
  4. Edit common.css, e.g. by changing green to blue, and see that the change is not applied.

In the browser console, you can see that the wrong chunk has been reloaded:

[WDS] App updated. Recompiling...
[WDS] App hot update...
[HMR] Checking for updates on the server...
[HMR] Updated modules:
[HMR]  - ./common.css
[HMR] App is up to date.
[HMR] css reload http://localhost:8080/foo.css

This may be the same as one of the issues reported in #444. If so, consider this to be a minimal reproducible test case as requested there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions