1

bundle-analyzer开发人员运行yarn analyze已设置package.json"analyze": "craco build --analyze-only".

我确实阅读了craco手册,但没有找到任何解决方案。我当前的配置如下:

const progressBar = require('./progressBar.webpack.config');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
  .BundleAnalyzerPlugin;

module.exports = {
  reactScriptsVersion: 'react-scripts' /* (default value) */,
  webpack: {
    alias: {},
    plugins: {
      add: [progressBar()] /* An array of plugins */
    },
    configure: (webpackConfig, { env, paths }) => {
      if (env === 'production') {
        webpackConfig.plugins.push(new BundleAnalyzerPlugin());
      }
      return webpackConfig;
    }
  }
};

我们如何将参数传递给它?

4

1 回答 1

1

您可以使用process.argv来检查参数,例如:

// craco.config.js
//...
module.exports = {
  // ...
  webpack: {
  //...
    configure: (webpackConfig, { env, paths }) => {
      if (process.argv.includes('--analyze-only'))
        webpackConfig.plugins.push(new BundleAnalyzerPlugin());

      return webpackConfig;
    }
  }
};

于 2021-05-10T08:05:10.143 回答