Skip to content

SWC TypeScript transpiling is moving imports to the top of the file #2729

Answered by kwonoj
joshden asked this question in Q&A
Discussion options

You must be logged in to vote

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joshden
Comment options

Answer selected by joshden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants