Search code examples
javascriptuglifyjs

Can uglifyjs ignore undefined symbols


The first file that I am passing to uglifyjs declares some namespaces like

window.MyNamespace = {}

when uglifyjs sees this line it complains that window is not defined.

Is there a way to have uglifyjs ignore undefined symbols? I have tried using the --no-dead-code option


Solution

  • You can wrap your global code in a function:

    (function(window) {
      window.whatever = something;
      // ...
    })(this);
    

    You can also do this:

    (function(window) {
      "use strict";
    
      // ...
    
    })(this);
    

    which is probably a good idea anyway. You'll get warnings/errors from stray undefined variables even without uglify.