Shopify / sprockets-commoner

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

Can't handle circular reference #47

Open mockdeep opened 7 years ago

mockdeep commented 7 years ago

So I have a weird situation with a circular reference between two files. A vastly over-simplified version looks something like this:

// petrify.js
import StoneObject from './object';

export default function petrify(value) {
  If Array.isArray(value) {
    return new SomethingElse(value);
  } else {
    return new StoneObject(value);
  }
}

// object.js
import petrify from './petrify';

export default function StoneObject(source) {
  const that = this;
  Object.keys(source).forEach(function assignKey(key) {
    that[key] = petrify(source[key]);
  });
}

When I was using require instead of import, I had to do some weird stuff where I did the require later in the file, but switching to import seemed to clean this up, at least when I was using browserify-rails. When I try to switch to sprockets-commoner, however, I see an error in my javascript console:

Uncaught TypeError: (0 , _petrify2.default) is not a function

It points to a line in the compiled javascript that looks like:

Object.keys(source).forEach(function assignKey(key) {
  that[key] = (0, _petrify2.default)(source[key]);
});
mockdeep commented 7 years ago

When I console.log(petrify) it is undefined.

bouk commented 7 years ago

This doesn't work in commoner currently, unfortunately.

danthedaniel commented 7 years ago

@bouk is there any plan to handle this soon?