Add module: hybrid by weswigham · Pull Request #29353 · microsoft/TypeScript (original) (raw)

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 an import statement) or assigning a (potentially callable) export object yourself. We allow both of these thru import= and export= 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= and export= statements, to emit them as their cjs counterparts (since this is an error, the emit change shouldn't matter). I then add a module: hybrid mode which suppresses the errors we would normally emit on those export= or import= statements, but otherwise behaves like the es2015 module target. This new hybrid 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