-
I am looking into moving our transpiling with ts-node to use SWC instead of the default in order to have much better performance. I found a difference though with SWC that is breaking our current code base. Given these two TypeScript files:
TSCWhen I run
SWCHowever, when I run
Notice how with SWC, in the a.js file, the import line and first console.log line are swapped. It seems to be transpiling so all the imports are done before any of the code in the module is run. I have a case where I run some code before the first import to setup some global state. I realize this code isn't very clean in its present form, but I was surprised to see SWC move the import to the top as compared to TSC. Is this expected? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Which module output target you're aiming, esm as is (preserving afaik, this shouldn't matter if it's for esm since esm spec specifies any import evalution should be completed before code (https://hacks.mozilla.org/2015/08/es6-in-depth-modules/) so even placing statement before import syntax, it won't run before import completes. tsc leaves as-is since syntaxwise it's not prevented (microsoft/TypeScript#16166 (comment)) but import is always hoisted, while babel seems to do explicit hoisting babel/babel#1057. I can't comment with gaurantee, but looks like swc follows babel's model. Answer may different for the q |
Beta Was this translation helpful? Give feedback.
Which module output target you're aiming, esm as is (preserving
import
) or using other targets like cjs?afaik, this shouldn't matter if it's for esm since esm spec specifies any import evalution should be completed before code (https://hacks.mozilla.org/2015/08/es6-in-depth-modules/) so even placing statement before import syntax, it won't run before import completes. tsc leaves as-is since syntaxwise it's not prevented (microsoft/TypeScript#16166 (comment)) but import is always hoisted, while babel seems to do explicit hoisting babel/babel#1057. I can't comment with gaurantee, but looks like swc follows babel's model.
Answer may different for the q
have a case where I run some code bef…