embroider-build / embroider

Compiling Ember apps into spec-compliant, modern Javascript.
MIT License
337 stars 137 forks source link

Set terser options #1353

Open andrewtimberlake opened 1 year ago

andrewtimberlake commented 1 year ago

Because embroider is not using ember-cli-terser, I’m not sure how to set the drop_console option. Before embroider I had the following in ember-cli-build.js

'ember-cli-terser': {
  terser: {
    compress: {
      drop_console: true,
    },
  },
},

Is it possible to set that option via embroider?

Windvis commented 1 year ago

Embroider uses hardcoded options for terser so I don't think it's possible to configure terser at the moment.

I assume we could make it possible by adding a new terserOptions / minifierOptions object to the available Webpack options which would then be merged with the Embroider terser options before being passed into terser?

Which would then look like this in apps:

return require('@embroider/compat').compatBuild(app, Webpack, {
  packagerOptions: {
    terserOptions: {
      compress: {
        drop_console: true,
      }
    }
  }
});
ef4 commented 1 year ago

That is correct but also it's important to note that our direct use of terser is only for the legacy scripts (so vendor.js).

All the rest of your app and addons goes through webpack's minification, so you can adjust all the minification options by passing them in the webpack config.

andrewtimberlake commented 1 year ago

Thanks, that worked. I did the following:

  const TerserPlugin = require('terser-webpack-plugin');
  //…
  const { Webpack } = require('@embroider/webpack');
  return require('@embroider/compat').compatBuild(app, Webpack, {
    packagerOptions: {
      webpackConfig: {
        optimization: {
          minimize: true,
          minimizer: [
            new TerserPlugin({
              terserOptions: {
                compress: {
                  drop_console: true,
                },
              },
            }),
          ],
        },
      },
    },
  });