webpack: LimitChunkCountPlugin max chunks not working for dynamic import

Bug report

What is the current behavior? When use LimitChunkCountPlugin with dynamic import for multi entry, webpack will generate chunks even set maxChunks = 1.

If the current behavior is a bug, please provide the steps to reproduce. The repo can show the current behavior, just as the image shown below. image

What is the expected behavior? I think webpack should only generate the entry chunk with LimitChunkCountPlugin for each entry and prevent any additional chunks.

Other relevant information: webpack version: 4.44.1 Node.js version: v12.18.2 Operating System: MacBook-Pro

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 26 (13 by maintainers)

Most upvoted comments

Still valid

@mohsinulhaq you can set this option to eager if you just want to get rid of dynamic import chunks.

@hoang-dao try this:

const path = require('path')
const webpack = require('webpack')

module.exports = {
  target: 'node',
  devtool: false,
  entry: { a: './a.js', b: './b.js' },
  output: {
    path: path.resolve(__dirname, './build'),
    chunkFilename: 'chunk.js'
  },
  module: {
    parser: {
      javascript: {
        dynamicImportMode: "eager"
      }
    }
  },
  plugins: [
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  ]
}

@vankop the issue can still be reproducible following the posted repo (https://github.com/LLGZONE/limitchunkplugin-demo) even with updated dependencies

"dependencies": {
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0"
}
  • webpack: 5.74.0
  • webpack-cli: 4.10.0
  • node: v16.13.1
  • OS: MacOS Monterey

In my case, my project is a monorepo which contains many typescript serverless functions, I follow the entry descriptor and set chunkLoading: false as a workaround to build a single bundle for every entry point https://webpack.js.org/configuration/entry-context/#entry-descriptor.

Refer from the posted repo, this is my updated config: // webpack.config.js

const path = require('path')
const webpack = require('webpack')

module.exports = {
  target: 'node',
  entry: { a: './a.js', b: './b.js' },
  output: {
    path: path.resolve(__dirname, './build'),
    chunkLoading: false,
  },
}

Hope it can help!