UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Minify global objects

Open keganlance opened this issue 2 years ago • 1 comments

Currently UglifyJS doesn't make the the code shorter by replacing globals with a variable reference. I wonder why this is the case.

Take something like:

(function () {
  let button = document.createElement('button');
  let span = document.createElement('span');
  span.textContent = 'something';
  button.appendChild(span);
  document.body.appendChild(button);
})();

This could easily be minified more when replacing the document references with a variable:

(function (document_) {
  let button = document_.createElement('button');
  let span = document_.createElement('span');
  span.textContent = 'something';
  button.appendChild(span);
  document_.body.appendChild(button);
})(document);

It could apply to more globals like window, navigator, and others. Is there a reason this is not included in minifiers?

keganlance avatar Jul 19 '22 16:07 keganlance

If you desire such a feature, please file a Pull Request so that your implementation can be benchmarked against both minified and gzipped sizes.

alexlamsl avatar Jul 19 '22 17:07 alexlamsl