关于webpack的optimize.CommonsChunkPlugin的文件生成问题

发布于 2022-09-04 21:21:18 字数 1255 浏览 7 评论 0

问题是:
1.mainifest和vendor是干嘛?
2.build之后,生成的html的scritpt居然带上了?

build的时候发现build出了一些文件,不知道为什么mainifest和vendor是干嘛的

➜  static ls -1 js
app.5945d04d354b51405f5a.js
manifest.3378dfda55d1bfb8710d.js
vendor.df0961104544908e1df2.js

查看webpack.prod.conf.js发现用了这个插件进行处理,但是没有看懂为啥要这么做

   // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: function (module, count) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      chunks: ['vendor']
    })

查了官网的http://webpack.github.io/docs... 也是不甚了解,所以来咨询大家,谢谢.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

已下线请稍等 2022-09-11 21:21:18

CommonsChunkPlugin是提取公共代码块用的,vendor是一般规则的命名,你可以写成其他的都可以。

new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',  //name是提取公共代码块后js文件的名字。
      chunks: ['vendor'] //只有在vendor中配置的文件才会提取公共代码块至manifest的js文件中
    })

一般在entry中都会配置的
具体的你可以看看这个例子
https://segmentfault.com/a/11...

宁愿没拥抱 2022-09-11 21:21:18

name: 'vendor',

  minChunks: function (module, count) {
    // any required modules inside node_modules are extracted to vendor
    return (
      module.resource &&
      /\.js$/.test(module.resource) &&
      module.resource.indexOf(
        path.join(__dirname, '../node_modules')
      ) === 0
    )
  }
}),
这段代码可以换吗
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文