1

Using a custom Angular Builder, I'm trying to exclude an entire module from minification/optimization.

The Webpack .md file says

exclude
Type: String|RegExp|Array Default: undefined

Files to exclude.

Can I use this setting to exclude an entire directory (which is the node module)?


The current code is

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;
  }
}
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
LppEdd
  • 20,274
  • 11
  • 84
  • 139

1 Answers1

5

Maybe you are looking for this

   module.exports = {
      optimization: {
        minimizer: [
          new TerserPlugin({
            exclude: /node_modules/,
          }),
        ],
      },
    };
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • So this is a Regex that will match the entire `node_modules` folder? – LppEdd Jun 12 '19 at 08:53
  • @LppEdd I have similar config [here](https://github.com/SaiGonSoftware/Awesome-CMS-Core/blob/master/src/AwesomeCMSCore/AwesomeCMSCore/webpack.config.js#L105) – Tony Ngo Jun 12 '19 at 08:54
  • Tried but it still minifies and removes the wrong functions. I think it is because I'm using an Angular Builder to modify the default TerserPlugin configuration. – LppEdd Jun 12 '19 at 09:19
  • Infact files still have the build-optimizer extensions (e.g. `Viewer.js.pre-build-optimizer.js`) – LppEdd Jun 12 '19 at 09:20
  • 1
    Try this [one](https://stackoverflow.com/questions/33915826/exclude-module-from-webpack-minification) I think it might help you – Tony Ngo Jun 12 '19 at 09:22
  • The problem is I'm not using Webpack directly. I'm passing by Angular CLI (Devkit Builder) – LppEdd Jun 12 '19 at 09:30
  • Okay, I think I'll have to `ng eject`. Thus I'll have the standard `webpack.config.json` file. – LppEdd Jun 12 '19 at 09:48
  • If I use `/.js/` it works fine. That means the `node_modules` isn't included in the file name? – LppEdd Jun 12 '19 at 10:13
  • Where is .js thing you said ? – Tony Ngo Jun 12 '19 at 10:28
  • What I need to ignore are `node_modules/bpmn-js/lib/Viewer.js` and `node_modules/bpmn-js/lib/Modeler.js` – LppEdd Jun 12 '19 at 10:35
  • In the end there is no solution to this problem for Angular. Thanks anyway – LppEdd Jun 12 '19 at 19:32
  • I was able to override the `sideEffects` key, using `module.rules.sideEffects`. So now the Angular build optimizer will simply skip that module. – LppEdd Aug 22 '19 at 16:55