欢迎访问 如意编程网!

如意编程网

当前位置: 首页 > 前端技术 > HTML >内容正文

HTML

webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中

发布时间:2022/11/16 HTML 18 老码农
如意编程网 收集整理的这篇文章主要介绍了 webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

问题描述:

 有2个页面index.html和product.html,用html-webpack-plugin和optimization.splitChunks的基本配置如下

{
      template: 'src/html/' + name + '.html',
      filename:  (devMode ? '' : '../') + 'html/' + name + '.html',
      chunks: ['common','jquery','index']
 }
{
      template: 'src/html/' + name + '.html',
      filename:  (devMode ? '' : '../') + 'html/' + name + '.html',
      chunks: ['common','jquery','product']
 }
optimization: {
      splitChunks: {
         chunks: 'all',
         minSize: 30000,
         minChunks: 1,
         cacheGroups: {
            vendor: {
               test: /node_modules/, // 用于规定缓存组匹配的文件位置
               name: 'vendor',
               minSize: 30000,
               priority: -10,//优先级
            }
         }
      }
   }

build后:index.html 页面并未引入index和product页面的公共块(index~product-795ac51aef2e85e2ec28.js?),导致页面不能正常加载

<script src="../js/common-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>
<script src="../js/jquery-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>
<script src="../js/index-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>

解决办法:在html-webpack-plugin的bata版已经修复,请重新安装

npm install --save-dev html-webpack-plugin@next

 

然后,重新build,查看index.html 页面

<script src="../js/common-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>
<script src="../js/jquery-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>
<script src="../js/index~product-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>
<script src="../js/index-795ac51aef2e85e2ec28.js?795ac51aef2e85e2ec28"></script>

页面已经自动引入公共模板,这个坑解决,我只想说:继续采坑

 

总结

以上是如意编程网为你收集整理的webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得如意编程网网站内容还不错,欢迎将如意编程网推荐给好友。