-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Import statement hoisting causing code to run out of order #1395
Comments
This is just how
If you have some code that needs to be evaluated after one import but before another, you can move it to a separate file to achieve this. For example, you cannot do this because of the way // This is wrong, console.log happens last
import './a.js'
console.log('after a, before b')
import './b.js' But you can do this: import './a.js'
import './c.js'
import './b.js' where the file console.log('after a, before b') If you don't believe me about import evaluation order, you should try it out for yourself in a real JavaScript environment such as in node or in the browser. |
Your explanation makes a lot of sense. I am not sure why I didn't think about separating it to another module, but it works. P.S.: will this be affected by #399 or does it only break when using code splitting? |
I am writing some Cloud Functions in TypeScript and using ESBuild as a bundler. Here is the relevant part of my code:
index.ts
:webhooks/myFunction.ts
:helpers/firestore.ts
:(code heavily simplified for the issue)
In case you are not familiar with Firebase, the
firebase.initializeApp()
function must be called before any of the Firebase components are used (in this case,firestore()
inhelpers/firestore.ts
).The problem is that the generated code has
firebase.initializeApp()
as the last line of code, even though it needs to be before the import of the re-exported module.I thought this might be related to #399, but I am not using code splitting or multiple entry points, and this is about the order of statements before imports/exports, not the order of imports themselves.
Note that I worked around the issue by replacing the
export { .. as .. } from '..';
syntax withwhich is uglier and repetitive.
Is this intended behavior or is it a bug? If it is intended behavior, is my workaround the actual fix or is it unintended behavior?
esbuild.config.js
The text was updated successfully, but these errors were encountered: