简体   繁体   English

为什么Webpack为每个动态import()生成2个块?

[英]Why is Webpack generating 2 chunks per dynamic import()?

I've started using some dynamic import() statements like so 我开始像这样使用一些动态import()语句

import(/* webpackChunkName: "chart" */'./Chart')

the problem is that Webpack generates 2 new chunks for this: chart.js (which is almost empty) and vendors~chart.js (which actually has everything that I expected to be in one new chunk). 问题是Webpack为此生成了2个新块: chart.js (几乎为空)和vendors~chart.js (实际上具有我希望在新块中包含的所有内容)。

My config has nothing fancy in it. 我的配置没有花哨的东西。 I have only one named entry called client and that was my only bundle before using the dynamic require. 在使用动态需求之前,我只有一个名为client命名条目,这是我唯一的捆绑包。 This happens for both development and production mode. 开发和生产模式都会发生这种情况。 I'm using Webpack ver. 我正在使用Webpack版本。 4.8.1 4.8.1

Any ideas how to achieve just one new chunk? 有什么想法只实现一个新的块吗? I don't want the client to make 2 requests instead of one. 我不希望客户提出2个请求,而不是一个。

I found two (imperfect) ways to avoid that. 我发现了两种(不完美的)方法可以避免这种情况。

  1. If your app have a single entry, you can remove the vendors cache group altogether since this vendor group is designed for multi-entry apps. 如果您的应用程序只有一个条目,则可以完全删除vendors缓存组,因为该供应商组是为多条目应用程序设计的。

     optimization: { splitChunks: { cacheGroups: { vendors: false, // not needed for a single entry app }, }, }, 
  2. you can use webpack.optimize.MinChunkSizePlugin that will bundle you small chunk to another one, albeit not necessarily with the best optimized option. 您可以使用webpack.optimize.MinChunkSizePlugin将您的小块内容捆绑到另一个,尽管不一定具有最佳优化选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM