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

babel 下循环引用 导致的import值为 undefined #1

Open
JoyMoch opened this issue Jun 29, 2020 · 1 comment
Open

babel 下循环引用 导致的import值为 undefined #1

JoyMoch opened this issue Jun 29, 2020 · 1 comment

Comments

@JoyMoch
Copy link
Owner

JoyMoch commented Jun 29, 2020

babel/babel#4094

That is to be expected, you have a circular dependency in your files

entry.js -> actions.js -> store.js -> actions.js

When store.js loads action.js, it is already in the queue, but hasn't executed yet because it is waiting for store.js to finish. Now because store.js depends on actions.js, but it is already loading, it will run store.js. The imported value actionTypes is undefined because actions.js hasn't executed yet.

In a real ES6 environment, this would throw a temporal deadzone error, but Babel does not support them in this case.

Your best bet would be to instead make store.js be:

export {actionTypes as default} from './actions';
so that the value is re-exported directly. This will take advantage of live bindings and avoids actually accessing the value of actionTypes until after actions has executed.

@JoyMoch
Copy link
Owner Author

JoyMoch commented Nov 22, 2020

debug 循环引用可以通过断点后的callstack往前看

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

1 participant