简体   繁体   English

Webpack v4在单个页面上有多个条目,不必要地复制了块

[英]Webpack v4 with multiple entries on a single page, chunks are duplicated unnecessarily

I have a multipage website with a subset of pages that is a SPA. 我有一个多页网站,一部分页面是SPA。 We just upgraded to Webpack V4 and have encountered some new issues that we havent seen before. 我们刚刚升级到Webpack V4,并且遇到了一些以前从未见过的新问题。

Our pages always include a script tag for app.build.js . 我们的页面始终包含app.build.js的脚本标签。 This file has global inits, and imports our public path. 该文件具有全局init,并导入我们的公共路径。 It imports things like Modernizr and builds our site navigation. 它导入诸如Modernizr之类的内容并构建我们的网站导航。

Additionally, on a per-page basis we serve a second JS file like account.build.js . 此外,在每页的基础上,我们提供第二个JS文件,例如account.build.js Some pages do not have this second file if they are very simple. 如果某些页面很简单,则它们没有第二个文件。 They still need app.build.js to create the navigation though. 他们仍然需要app.build.js来创建导航。

It appears as though Webpack doesn't know about this combination, so there are redundant chunks in the two output files. Webpack似乎不知道这种组合,因此在两个输出文件中有多余的块。 they both may include a Modernizr test, etc. and all of that gets added to both build files bloating them significantly. 它们都可能包含Modernizr测试等,并且所有这些都添加到了两个构建文件中,从而极大地膨胀了它们。

My question is, how do you tell Webpack that app.build.js is globally included??? 我的问题是,您如何告诉Webpack全局包含app.build.js ??? We do this with SplitChunks for our vendors, but it matches on imports, not on the entry file name. 我们为供应商使用SplitChunks进行此操作,但是它与导入匹配,而不与条目文件名匹配。

We also now need to import './public-path'; 现在,我们还需要import './public-path'; in every entry file, which isn't ideal. 在每个条目文件中,这都不理想。 I would prefer to do it once in app.build.js and have it globally recognized. 我希望在app.build.js中执行一次,并使其得到全球认可。

Bug filed with Webpack here: https://github.com/webpack/webpack/issues/8842 向Webpack提交的错误在这里: https : //github.com/webpack/webpack/issues/8842

If you want to move all duplicates to a single file ( commons.js ) leaving aside all vendor files in vendors.js you can specify multiple cache groups. 如果你想所有重复移动到一个单一的文件( commons.js )撇开所有供应商的文件vendors.js可以指定多个缓存组。 This commons.js will contain all modules duplicated across all of your entry points. 这个commons.js将包含在所有入口点重复的所有模块。

optimization: {
  splitChunks: {
    cacheGroups: {
      vendors: {
        test: /[\\/]node_modules[\\/]/,
        name: 'vendors',
        chunks: 'all'
      },
      commons: {
        name: 'commons',
        chunks: 'all',
        minChunks: 2,
        enforce: true,
      },
    }
  }
}

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

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