Shopify / sprockets-commoner

Use Babel in Sprockets to compile JavaScript modules for the browser
MIT License
182 stars 22 forks source link

Some references to require do not work #50

Closed danthedaniel closed 7 years ago

danthedaniel commented 7 years ago

I've discovered that in HubSpot/tether's "compiled" .js file, there is an expression which evades Commoner's handling of require.

https://github.com/HubSpot/tether/blob/master/dist/js/tether.js#L7

(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(factory);
  } else if (typeof exports === 'object') {
    // A reference to the require function is made here
    module.exports = factory(require, exports, module);
  } else {
    root.Tether = factory();
  }
}(this, function(require, exports, module) {

You can see that on line 7 require is passed to a function, but it is not used as a top-level function call. According to #11, require() is only resolved when used in the global namespace. It seems that the solution used there does not meet all cases.

This means that I get an exception saying Uncaught ReferenceError: require is not defined(…).

It looks like adding the line:

var require = require;

to the beginning of the file side-steps this issue.

bouk commented 7 years ago

This is the same issue as https://github.com/Shopify/sprockets-commoner/issues/47, because commoner attempts to resolve requires statically, thus this doesn't work