Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling aggressive optimizations breaks modules with define("foo", foo) #81

Open
gustavderdrache opened this issue Feb 23, 2015 · 2 comments

Comments

@gustavderdrache
Copy link

Given a module like D3, which has the following UMD stanza:

if (typeof define === "function" && define.amd) define(d3);
else if (typeof module === "object" && module.exports) module.exports = d3;
this.d3 = d3;

amdclean will, if aggressive optimizations are enabled and two modules refer to it, break the d3 identifier by overwriting it:

  if (true)
    _d3_ = d3 = function () {
      return typeof d3 === 'function' ? d3() : d3;
    }();

You can see the issue by entering the following example into the amdclean page:

// d3 UMD stanza
(function () {
  var d3 = {};

  if (typeof define === "function" && define.amd) define('d3', d3);
  else if (typeof module === "object" && module.exports) module.exports = d3;
  this.d3 = d3;
}());

// modules become candidates for hoisting
define('foo', ['d3'], function (d3) {
  reference(d3);
});

define('bar', ['d3'], function (d3) {
  reference(d3);
});

I've disabled aggressive optimizations to avoid the issue, but it seems a shame to lose the benefits over an edge case like this.

@gfranko
Copy link
Owner

gfranko commented Apr 2, 2015

Can you test to see if this is still an issue with AMDclean 2.6.0?

@gustavderdrache
Copy link
Author

Unfortunately, it is. If it helps any, I was able to narrow down the test case:

(function () {
  var foo;

  define('foo', {});
})()

The rewritten output clobbers the local foo instead of the module variable it probably intended to:

;(function() {
var foo;
(function () {
  var foo;
  foo = {};
}());
}());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants