`import` and hoisting
# Kyle Simpson (10 years ago)
bump.
# caridy (10 years ago)
yes, they will work. "hoisting of function initialization happens across the whole module linkage graph before any module initialization code starts executing." quoting @dherman.
there is one thing to keep in mind though: many edge cases cannot be transpile into CJS, AMD, etc., the only format that preserves all the semantics of the module system is the system
format, which is an experimental format we came up with a while ago. those two examples might fail in babeljs, but they definitely work on esnext module transpiler (which is already deprecated), but at least will give you an idea of how things work internally: bit.ly/1ETGVDA
As far as I can tell,
import
is hoisted (due toModuleDeclarationInstantiation
). Is the following code OK, then? No temporal dead zone?bar(); import {foo} from 'mymodule'; function bar() { // hoisted! foo(); // already initialized? }
How about this code?
foo(); import {foo} from 'mymodule';
Thanks!
Axel