【npm第6期】通过vue中npm run build --report来介绍npm传参

2023-10-27

最近发现vue中可以通过npm run build --report来调用webpack-bundle-analyzer插件,具体相关代码如下:

// webpack.prod.conf.js
if (config.build.bundleAnalyzerReport) {
  const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
// config/index.js
build: {
  // Run the build command with an extra argument to
  // View the bundle analyzer report after build finishes:
  // `npm run build --report`
  // Set to `true` or `false` to always turn it on or off
  bundleAnalyzerReport: process.env.npm_config_report
}
复制代码

实际上执行npm run build --report时,在process.env对象内添加了一个属性。如下:

{
  npm_config_report: "true"
}
复制代码

所以我们可以通过process.env.npm_config_report获取它的值为true,由于webpack.prod.conf.js调用config/index.js中的bundleAnalyzerReport 所以会调用webpack-bundle-analyzer插件

如有侵权,请发邮箱至wk_daxiangmubu@163.com 或留言,本人会在第一时间与您联系,谢谢!!

长按二维码关注我们,了解最新前端资讯

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【npm第6期】通过vue中npm run build --report来介绍npm传参 的相关文章

随机推荐