-
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
Add module: hybrid #29353
Add module: hybrid #29353
Conversation
…ing es6 modules, emit cjs hybrid output
I actually didn't expect webpack to still allow access to Note, though, that webpack does completely ban access to I also suspect that this
|
The irony there is that if you only use |
@weswigham yep, that's why I clarified that is "if any ES imports remain" 😄 |
Also, looking at the CommonJsPlugin code, it seems the |
(Personally I think it's a bad idea to use this Also, a question: would such a module that only has |
I don't think see why a module loader would be any more capable of tree-shaking a default import over a In general I'm not big on the name "hybrid" because it's so vague as to what it's a hybrid of (i.e. CommonJS/ESM). It's also not 100% clear whether this hybrid mode is going to be compatible with whatever Node.js supports either, which concerns me. I also feel like the user story will now be even more confusing giving users the options between module targets:
and emit/check options:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any checks to set the default option for moduleResolution
- this new mode should set it to Node.
@DanielRosenwasser Thanks for the reminder, done. |
If the goal is to be compatible with Node.js supports (another reason why "hybrid" might not be a good name) then this type of file will actually need two resolutions modes: |
Drive-by comment from the future.
I think it's meant to be compatible with bundlers, not with node. Node doesn't allow using CommonJS APIs in modules. But those bundlers happen to use the node (or rather CommonJS) resolution algorithm. |
Oftentimes TS is used for typechecking and then piped into webpack, babel, @std/esm, or the like. The ES6 module format usually works in these cases, however there are sometimes some idiosyncrasies that necessitate usage of some features that aren't strictly es6 module features - for example, importing a callable cjs module via
require
(rather than thru animport
statement) or assigning a (potentially callable) export object yourself. We allow both of these thruimport=
andexport=
statements when targeting cjs, and most of these targets (webpack, babel, @std/esm) support mixing these cjs idioms into an es module to one degree or another.So, in this PR I have modified the es2015 module transformer to, rather than elide
import=
andexport=
statements, to emit them as their cjs counterparts (since this is an error, the emit change shouldn't matter). I then add amodule: hybrid
mode which suppresses the errors we would normally emit on thoseexport=
orimport=
statements, but otherwise behaves like thees2015
module target. This newhybrid
target is a pretty optimal middleground for people using hybrid pipelines where the ultimate target is usually cjs-like, but the output of the ts compiler is desired to be es2015-y (or at least have no TS-specific syntax) for code-splitting purposes.Ref #22321, #22851
cc @bterlson @DanielRosenwasser