Skip to content
Advertisement

How to minify/uglify document and window properties and methods

I’m looking for a way to minify a code like this:

JavaScript

To something like this (minus spaces and new lines):

JavaScript

using an automatic tool like UglifyJS or similar. From the documentation it doesn’t seem to be an option to do that.

EDIT: It’s quite common to see code like this:

JavaScript

This is done for performance and to make the code more minifier-friendly, so I’m wondering why this is not done on a deeper level.

Advertisement

Answer

Using uglify-js (tested it with version 3.14.5 but it should also work with version 2), you can use the --enclose option:

JavaScript

Giving the following output:

JavaScript

Unfortunately it cannot replace expressions like document.getElementById.

Advertisement