Minify: Can I tell babel-minify to mangle top level variables?

Created on 11 Nov 2017  路  1Comment  路  Source: babel/minify

I have a library that sets some variables inside my module files.

const isBrowser =
  typeof window !== 'undefined' && typeof window.document !== 'undefined';

export default class MyLib {
  constructor() {
    return isBorwser;
  }
}

When babel-minify minifies the code, it becomes:

// minify test.js | prettier
const isBrowser =
  "undefined" != typeof window && "undefined" != typeof window.document;
export default class MyLib {
  constructor() {
    return isBorwser;
  }
}

As you can see, the isBrowser variable name is not mangled.

Can I tell babel-minify to mangle this variable name? It is defined inside a module, so it's not going to get exposed to the global scope, and even if, I would anyway like to mangle it because I know it's not going to be used by anyone else.

Thanks!

Most helpful comment

Replying to myself, I can pass { mangle: { topLevel: true }} to the babel-minify options.

The docs are here:
https://github.com/babel/minify/blob/master/packages/babel-preset-minify/README.md#options

>All comments

Replying to myself, I can pass { mangle: { topLevel: true }} to the babel-minify options.

The docs are here:
https://github.com/babel/minify/blob/master/packages/babel-preset-minify/README.md#options

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gustorn picture Gustorn  路  4Comments

marvinhagemeister picture marvinhagemeister  路  5Comments

boopathi picture boopathi  路  3Comments

zamb3zi picture zamb3zi  路  3Comments

bosens-China picture bosens-China  路  4Comments