Webpack TerserPlugin - 排除节点模块

问题描述 投票:0回答:2

使用自定义 Angular Builder,我尝试从缩小/优化中排除整个模块。

Webpack

.md
文件说

排除
类型:String|RegExp|Array 默认值:undefined

要排除的文件。

我可以使用此设置来排除整个目录(即节点模块)吗?


当前代码是

export default class CustomizeTerserBrowserBuilder extends BrowserBuilder {
  public buildWebpackConfig(root: any, projectRoot: any, host: any, options: any): any {
    const webpackConfig = super.buildWebpackConfig(root, projectRoot, host, options);

    if (
      webpackConfig.optimization &&
      webpackConfig.optimization.minimizer &&
      Array.isArray(webpackConfig.optimization.minimizer)
    ) {
      const terserPlugin = (webpackConfig.optimization.minimizer as any[]).find(
        minimizer => minimizer instanceof TerserPlugin
      );

      if (terserPlugin) {
        terserPlugin.options.exclude = [/node_modules/];
      }
    }

    return webpackConfig;
  }
}
angular typescript webpack ecmascript-6 single-page-application
2个回答
5
投票

也许您正在寻找这个

   module.exports = {
      optimization: {
        minimizer: [
          new TerserPlugin({
            exclude: /node_modules/,
          }),
        ],
      },
    };

0
投票

在使用 Webpack5 进行生产时遇到了完全相同的问题,最终遇到了类似以下问题,它将排除

node_modules
dist
下的特定文件,这会导致 Terser 的“激进”缩小尝试被破坏。

optimization: {
    minimize: NODE_ENV !== 'development', //only minimize on production
    //JS minimizer when "minimize === true",
    minimizer: [
        new TerserPlugin({
            test: /\.js$/,
            exclude: /(?:node_modules|dist)\/(update-notifier|numeral|jackspeak|bulma-extensions)\//, //whatever causes u a problem, either as RegExp or as a string[]
            terserOptions: {
                parse: {
                    bare_returns: true //allow code with returns outside of functions, solved a ton of issues.
                },
            },
        })],
    // rest of config.
    // ...

}

说实话,我不确定排除

/node_modules
下的所有文件是否是一个好主意。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.