google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.35k stars 1.14k forks source link

Object.freeze/seal prevent removal #3102

Open jplaisted opened 5 years ago

jplaisted commented 5 years ago

Closure Library's new goog.js file has the following:

export const module = Object.seal({
  get: goog.module.get,
  // TODO(johnplaisted): Delete this once people have migrated to
  // declareModuleId.
  declareNamespace: goog.module.declareNamespace,
});

In compiled output this won't get removed, even when it isn't referenced (and RewriteGoogJsImports should ensure it isn't referenced after it runs...).

var b = this;
Object.seal({get:function() {
  return null;
}, b:function(c) {
  var a = b.$jscomp;
  if (!a || "function" != typeof a.a) {
    throw Error('Module with namespace "' + c + '" has been loaded incorrectly.');
  }
  a.c(a.a());
}});
karikera commented 4 years ago

Could I avoid this situation?

concavelenz commented 4 years ago

@karikera What is your use case?

karikera commented 4 years ago

@concavelenz I just use Object.freeze for optimizing. but it keeps unused variables.