question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`entry.chunkLoading = false` is overridden by `SplitChunksPlugin`

See original GitHub issue

Bug report

Closely related: https://github.com/webpack/webpack/issues/4819.

What is the current behavior?

Here is an example Webpack config, with two entry points app and serviceWorker. For app, the config uses SplitChunksPlugin to put node_modules in a “vendor” chunk to improve long-term caching. Trying to dynamically load chunks from a service worker has failed each time I have tested it, so I want the serviceWorker entry to be compiled to one big JS file.

const path = require("path");

module.exports = {
  mode: "development",
  entry: {
    app: path.resolve(__dirname, "src/app.js"),
    serviceWorker: {
      import: path.resolve(__dirname, "src/serviceWorker.js"),
      chunkLoading: false, // This _should_ put everything in one JS file, but it doesn't
    },
  },

  output: {
    clean: true,
  },
  devtool: false,

  // Enable long-term caching of node_modules. See https://webpack.js.org/guides/caching/
  optimization: {
    moduleIds: "deterministic",
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: "vendor",

          // Include all node_modules that are required for the initial load
          // in one big chunk. Dynamically-imported node_modules will still be
          // put in their own chunks.
          chunks: "initial",
        },
      },
    },
  },
};

The Webpack docs state here that you can pass chunkLoading: false when defining an entry to put everything in the main chunk. It seems like SplitChunksPlugin ignores this option.

If the current behavior is a bug, please provide the steps to reproduce.

  1. Clone my repo: https://github.com/srmagura/webpack-split-chunks-repro
  2. yarn install
  3. yarn build
  4. Check dist/serviceWorker.js and observe that lodash is not in the file. Lodash is loaded dynamically from the vendor chunk.

What is the expected behavior?

chunkLoading: false should cause serviceWorker.js to include all chunks required for the service worker to run. This means Lodash should be in serviceWorker.js.

Other relevant information:

As a workaround, I could compile my service worker through a separate Webpack config, but I wanted to check if there was a better option before doing so.

webpack version: 5.74.0 Node.js version: 16.17.0 Operating System: Ubuntu 22.04

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Oct 7, 2022

@srmagura what about this:

const path = require("path");

module.exports = {
  mode: "development",
  entry: {
    app: {
      layer: 'app',
      import: path.resolve(__dirname, "src/app.js")
    },
    serviceWorker: {
      layer: 'worker',
      import: path.resolve(__dirname, "src/serviceWorker.js"),
    },
  },

  output: {
    clean: true,
  },

  devtool: false,

  experiments: {
    layers: true
  },

  // Enable long-term caching of node_modules. See https://webpack.js.org/guides/caching/
  optimization: {
    moduleIds: "deterministic",
    splitChunks: {
      cacheGroups: {
        vendor: {
          layer: /^app$/,
          test: /[\\/]node_modules[\\/]/,
          name: "vendor",

          // Include all node_modules that are required for the initial load
          // in one big chunk. Dynamically-imported node_modules will still be
          // put in their own chunks.
          chunks: "all",
        },
      },
    },
  },
};
0reactions
alexander-akaitcommented, Oct 24, 2022

@srmagura Yeah, will be great to see the minimum reproducible example, so I will answer why it happens

Read more comments on GitHub >

github_iconTop Results From Across the Web

SplitChunksPlugin - webpack
The name of the split chunk. Providing false will keep the same name of the chunks so it doesn't change names unnecessarily. It...
Read more >
Webpack CommonsChunkPlugin assumes output files will ...
... in favor https://webpack.js.org/plugins/split-chunks-plugin/. ... entry.chunkLoading = false is overridden by SplitChunksPlugin #16320.
Read more >
reactjs - Webpack 4 changes - Stack Overflow
I am currently using webpack 4.32 from reactcreateapp and I wanted to rename the format of the output file. Current it using Tilde(~)...
Read more >
Webpack: An in-depth introduction to SplitChunksPlugin
This article will give you a good introduction to one of the most used plugins in webpack's ecosystem - SplitChunksPlugin.
Read more >
Webpack - The Most Powerful JavaScript Module Bundler
You can config entry , use SplitChunksPlugin or using dynamic imports. ... code by adding "sideEffects": false property in package.json .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found