Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compressor options #335

Merged
merged 5 commits into from
Mar 4, 2019
Merged

Add compressor options #335

merged 5 commits into from
Mar 4, 2019

Conversation

developit
Copy link
Owner

@developit developit commented Mar 4, 2019

This allows specifying Terser options in mangle.json or package.json. The old "mangle" field is still supported for legacy reasons alongside the new "minify" option.

// in `mangle.json` or `package.json`:
{
  "minify": {
    // old/simple variant:
    "regex": "^_",
    // new: customize any terser options:
    "compress": {
      "hoist_vars": true,
      "reduce_funcs": false
    }
  }
}

This is useful for projects that are already structured with functions aimed at gzip compression optimization.

This allows specifying Terser options in mangle.json or package.json's "mangle" field.
@developit
Copy link
Owner Author

developit commented Mar 4, 2019

I've updated the PR with a new config format.

The previous package.json "mangle" field is still supported for compatibility purposes.

Now, in either mangle.json or package.json, both can specify a "minify" field containing configuration for Terser:

{
  // "minify" for Terser (https://github.com/terser-js/terser#minify-options-structure)
  // Note: works as a key in both package.json or mangle.json
  "minify": {
    // Legacy/convenient way to set mangle.properties.{regex,reserved}:
    "properties": "^_",
    "reserved": ["_dom", "_constructor"],

    // Optionally specify Terser compress configuration.
    // Supported values: https://github.com/terser-js/terser#compress-options
    "compress": {
      "hoist_vars": true,
      "reduce_funcs": false
    },

    // Optionally specify Terser mangle configuration
    // Supported values: https://github.com/terser-js/terser#mangle-options
    "mangle": {}
  }
}

Copy link
Collaborator

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉👍 I always wondered why we didn't just expose the whole terser config.

@developit
Copy link
Owner Author

@marvinhagemeister for now I've left out a couple top-level configuration items like module and ecma because Microbundle is going to automate those soon and we don't want folks to tweak them thinking it's necessary.

Copy link

@hassanbazzi hassanbazzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was gonna ask the same as Marvin but makes sense I guess. Can't you just spread the rest of the config onto Terser instead? If someone passes something specific (without it being documented), that means they want something very specific and they know (I think) what they're doing.

So we document and expose exactly what we want people to actually change, and spread the rest just in case.

@developit
Copy link
Owner Author

@hassanbazzi most of the Terser options folks would want to change are on mangle and compress, so for those that's what this does - sets some defaults then allows arbitrary overrides.

For Terser's root options, we really can't allow arbitrary customization. Doing so would break output
in the very near future, when Microbundle begins outputting ES2017 alongside the current downlevelled ES5 (see #304). When we do this, we'll be running Terser in ES5 mode for the 3 classic script bundles, and {ecma:8,module:true} for modern bundles. Since ecma and module are the two main root options we're not exposing here, I think that with the modern bundling setup in place there won't really be a reason to customize any root options. We can always add them manually once we have a better idea, but for now I don't want folks to try customizing ecma only to intentionally break it in 1.0.

@developit developit merged commit f6ebebb into master Mar 4, 2019
@bors bors bot deleted the compressor-options branch March 4, 2019 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants