-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Proposal: add --hoistImports to compiler options #22178
Comments
|
Thanks Wesley. Good catch. Updating |
It's also probably worth noting that is is the behavior for all module formats other than cjs. |
Sorry, now that I am rereading what I wrote. When using webpack with |
Right, but your point is that our behavior differs from spec when |
If TypeScript is a strict superset of es6, then the code generated when module: “cjs” is not to spec. Whether or not that’s what TypeScript is, I would like to ensure that my fellow engineers do not accidentally introduce bugs due to side effects when testing via node vs runtime environments. |
Wait, is this #16166? |
Yeah I guess it’s very similar. That’s more about having an error for out of order and this is an option to force hoisted imports even in cjs |
The resolution on the other issue was to commit to changing our cjs emit to hoist imports (not to issue some warning), which would is what we'd do here, too. As I said, it is already how it behaves for every other target. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
In ES6, imports are hoisted and evaluated before code within the same file. Webpack definitely already does this and I think rollup might too (or certainly it would be legal for them to do so).
Example:
In ES6, your code will be evaluated as
and cause an unexpected error (polyfills aren't available for
Foo
) since in an ES5 engine it still behaves as the example.Unfortunately, this allows us to write code that doesn't behave the same depending on the output module type (if TS hoisted imports when
module: "es6"
) nor when running ES6 style code through other tools (like webpack or rollup).This flag would instruct TS to hoist imports in generated code, regardless of the output module format. Thus one can write code that can be sure to behave the same in node 6, webpack, and engines that natively support es6 modules.
Thus, with
hostImports: true
andmodule: "cjs"
, your output code would be:I think that ideally this flag would default to true when
module: "es6"
however that would likely be considered a breaking change. So perhaps that change (or always defaulting it to true, since TS looks like ES6) could happen in TS3.Thanks
CC @mhegazy
The text was updated successfully, but these errors were encountered: