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

Fixing circular case #13

Closed
guybedford opened this issue Nov 15, 2018 · 0 comments
Closed

Fixing circular case #13

guybedford opened this issue Nov 15, 2018 · 0 comments

Comments

@guybedford
Copy link
Contributor

Here is a code example of what is breaking with cycles:

a.js

exports.run = function () {
  console.log('parent');
}
require('./b.js');

b.js

require('./a.js').run();

The issue is that this gets converted into an ES module tree looking like:

a.js

import './b.js';
import 'commonjs-proxy:./b.js';

var fn = function () {
  console.log('parent');
}

var a = {
        fn: fn
};

export default a;
export { a as __moduleExports };
export { fn };

b.js

import './a.js';
import require$$0 from 'commonjs-proxy:./a.js';

require$$0.fn();

var b = {

};

export default b;
export { b as __moduleExports };

And so "fn" isn't defined as b.js executes before a.js entirely, no exports are permitted before the require statement.

Now one naive fix for this particular case would be to use a function binding in the output, as these hoist through circular references in ES modules. That would only fix exported functions though, so executed binding values would still throw. This may possibly be adequate though as most test cases are functions as classes, as it seems from most of the bug reports. So it could be an interesting first step to try, although would be invalidated by a single exports.x = 'evaluated value'; require('./circular') bug.

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