Repository navigation
Rethinking module for the present and the future #55221
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.In DiscussionNot yet reached consensusNot yet reached consensus
on Jul 31, 2023 andrewbranch commented
on Aug 8, 2023 MemberAuthorMore actionsI want to draw out two things that were discussed in the design meeting #55271.
First, there was broad agreement that it would be worth updating the old
esnext/commonjs/etc. modes to fix issues like #50647 by having them refuse to emit CJS syntax into.mjsfiles or ESM syntax into.cjsfiles, and that we don’t need to wait until 6.0 to at least experiment and see how breaky that kind of change would be. This could be done either by issuing a program error, or updating those modes to take file extension (and perhaps package.json"type") into account and really treat those files as the module format their extension implies, even though it may disagree in name with themodulevalue. I’m leaning toward trying the latter, because as I discussed in the issue body, you can look at the semantics of how imports and exports in declaration files work in these modes and notice that they are not actually intended to limit program files to just ESM or just CJS. For example, in--module esnext, you’re allowed to import a declaration file that usesimport x = require("...")andexport = ..., so it seems to acknowledge that CJS files exist and can be imported, so there doesn’t seem to be a strong reason to refuse to emit CJS syntax into a.cjsfile in that program.Secondly, Wesley Wigham (@weswigham) floated the idea of creating several granular, advanced-usage flags that control individual aspects of the module system, and rolling them up into named presets reflecting real known runtimes, e.g.
esbuild,webpack, etc. The individual controls that might be relevant are:- Module format detection algorithm: There are two algorithms in use today: what node16/nodenext do, and no detection at all. We may need a variation of the node16/nodenext algorithm that leaves files unaffected by file extension or package.json
"type"in an indeterminate format for use with bundlers, since non-mjs files aren’t properly CJS, they just have different interop behavior in Webpack/esbuild. - Supported module formats: We discussed the potential utility of a true ESM-only mode that would error when attempting to import unambiguously CJS dependencies. That said, no existing mode and no planned bundler mode would use this.
- Module format interop controls: This is a big one that is currently just baked into
node16/nodenext. To support bundlers, we need one switch that can control whetherrequire(esm)is allowed, and one switch for controlling the shape of CJS modules when imported. - Emit format for indeterminate/ambiguous format files: I don’t know that this really needs to be configurable for bundler usage, since you usually don’t want to emit at all, and any place where TS needs to reason about the runtime behavior should assume that no module transformations took place (new
--module none?). But this seems like the most plausible way to represent the behavior we might want out of a fixed/improved--module esnextor--module commonjs, where we always emit the “right” syntax into.mjsand.cjsfiles, but we have direct control over the emit format of other files—which ones count as indeterminate would depend on the module format detection algorithm mentioned earlier. This also could be the right option for picking a module format forts.transpileModule, where you truly are just forcing an output syntax. - ESM spec version or feature list: whereas we currently have
--module es2015,es2020,es2022, andesnext, which differ only in whether language features likeimport.metaand top-levelawaitare available, we might want to move this to a separate option that can be applied to any module mode that supports emitting any ESM-format files (potentially all the existing ones, if we update every existing mode to emit ESM into.mjsfiles).
I feel fairly confident that this set of levers would let us model everything we currently have and everything that we’d like to add in the near term. It still makes me a bit uncomfortable to expose all these as public API though, as they would really take the “advanced” section of the tsconfig options to a new level. On the other hand, if we could use these to dramatically lower the barrier to giving users named presets that are a really good fit for their runtime/bundler, that might be a good tradeoff. (That does necessitate another decision about preset versioning—do we need a
bun2023and abunnext? Or are we ok with changing presets as needed in each new TS version?)Reacted by uhyo, Alexander Sorokin and Tolstikov VladimirReacted by Jayden Seric- Module format detection algorithm: There are two algorithms in use today: what node16/nodenext do, and no detection at all. We may need a variation of the node16/nodenext algorithm that leaves files unaffected by file extension or package.json
Today, the module format detection (the setting of impliedNodeFormat) is actually triggered by moduleResolution, not module, but I think this doesn‘t make sense.
I could go either way on this. If you say that "resolution" is only the process of "resolve a module specifier to a file on disk", then that's fair, but I think it could be argued that the process of module resolution also covers what kind of module it resolves to (in particular imagine a world in which you could write
esm:fooorcommonjs:foowhere the module loader sees the exact same resolved filename for both). The other thing is thatmodule, intuitively, is the answer to the question "What environment do you want to emit modules for?", and under that interpretation it doesn't make much sense for that to control what kind of module an import resolves to, as that's a (mostly) orthogonal concern.FWIW, I myself considered module type detection to be an inherent part of resolution when I implemented neoSphere's current module loader:
https://github.com/spheredev/neosphere/blob/main/src/neosphere/module.c#L398
I think I originally tried a solution wherein module type was handled later, during module loading, but that caused more problems than it solved; for example there was no way to say I wanted Node-like semantics forrequirebut not forimport, since all the loader saw was a filename and not, e.g., the contents ofpackage.json.Reacted by uhyo and wh1tecatWe discussed the potential utility of a true ESM-only mode that would error when attempting to import unambiguously CJS dependencies. That said, no existing mode and no planned bundler mode would use this.
I would just like to express my support for this option. I increasingly have (mostly) standards compliant pure ESM codebases and dependencies, and CommonJS interoperability is steadily becoming more of a burden than a boon to productivity.
Reacted by Andrew Branch and Alexander Sorokin
What does
--moduleactually mean?Which of these is a better definition for the flag as it exists today? Which is a better fit for the future?
When the possible values of
modulewere limited toamd,umd,commonjs,system, andes2015, the former definition was perfectly fine. Whenes2020andes2022were added, which added syntax features likeimport.metaand top-levelawaitthat couldn’t be transformed into other module emit targets besidessystem, it started to feel likemoduledescribed not just an output format, but the intrinsic capabilities of some external system. Withnode16andnodenext, the scope of themoduleflag suddenly expanded to include a new module format detection algorithm used by the target module system and special interop rules between module formats, while it stopped directly controlling the output format, since the format of every output file would be fully determined by Node.js’s format detection algorithm.The latter interpretation of
module, one that fully describes the target module system, works well fornode16/nodenext, but trying to project that definition onto the other, oldermodulevalues makes them feel kind of incoherent.All the values except
node16/nodenextare kind of weirdSome of the important characteristics of the module system described by
--module nodenextare:If we try to infer from existing what the other
modulevalues say about these characteristics, the result is confusing. For example, you might expect that--module esnextmeans an ESM-only module system that must reject CommonJS/AMD/System modules—after all, you’re not allowed to writeimport foo = require("./mod")in that mode. But you are allowed to import a dependency that declares CommonJS constructs like that.None of these
modulemodes have any restriction on the kinds of modules that can be imported, nor do they particularly make any effort to detect what kind of module a dependency is. Essentially, type checking between modules proceeds as if everything is CommonJS, even when we’re explicitly emittingesnext. This can be observed direclty by writing a default import of a.d.tsfile that only declares named exports:This behavior is enabled by
esModuleInterop/allowSyntheticDefaultImports, but those settings should only affect how the exports of CommonJS modules appear (and arguably only to imports written other CommonJS modules, sinceesModuleInteropis an emit setting that only emits code into CommonJS outputs). There’s no attempt to distinguish between what happens when two ES modules interact, two CJS modules interact, or an ES module imports a CJS module. This is perhaps, historically, because we had no idea what the actual module format of the JS file described by the declaration file is. (It would have been really nice for declaration emit to have always encoded the output module format, but here we are.)Even if we had perfect information about the module format of every file, the distinction between I want to emit ESM and My module system can only handle ESM is potentially useful, and these old
modulemodes can only describe the former. Essentially, they all describe the same hypothetical module system, where any module format can be loaded interchangeably.Supporting bundlers
Webpack and esbuild vary their handling of ESM→CJS imports based on whether the importing file would be recognized as ESM according to Node.js’s module format detection algorithm. According to the
node16/nodenextprior art, themoduleflag is the trigger that should enable this behavior.1 Unlike in Node.js, files in these bundlers’ module systems are not always unambiguously ESM or CJS. When a file has a.ts/.jsextension, and the ancestor package.json doesn’t have a"type"field at all, they’re not treated as CJS; they just don’t get the aforementioned special Node.js-compatible import behavior.Other bundlers don’t implement this Node.js compatibility behavior (at least by default). They’re already fairly well served by
--module esnext, with the exception of the bug described in the previous section (#54752). It seems like we could improve on all the oldermodulemodes by including file extension and package.json"type"fields as a heuristic for when a default export of should be synthesized, and to avoid emitting syntax into.mjsor.cjsfiles that would be invalid in Node.js. (#50647, #54573)Options
Decisions I think are on the table:
modulevalue for bundlers?modulevalues for bundlers, one for Webpack/esbuild (Node.js-style interop) and one for all others? Or, should the Node.js-style interop behavior be triggered by a separate flag?modulevalues to fix #54752, #50647, and #54573, or deprecate them in favor of new ones?modulevalues, what new ones do we actually need?module, would it be better to have a more granular set of flags describing what formats are supported, how they interoperate, what output format to emit (when ambiguous via detection), and what ECMAScript spec version is supported?My proposed minimal change:
--module bundlerand--module bundler-node-compatible, or--module bundlerand another flag enabling Node.js-compatible interop. Ignore everything else.Why I’d rather rethink
moduleas a whole than do the minimal change:modulemeans for documentation purposes..mjsand.cjsfiles is a pretty bad behavior, and we should fix or deprecate every mode that does it.modulemode to represent the browser or another future runtime, and it’s annoying that--module esnextis a poor fit for that.Footnotes
Today, the module format detection (the setting of
impliedNodeFormat) is actually triggered bymoduleResolution, notmodule, but I think this doesn‘t make sense. MakemodulecontrolimpliedNodeFormatandmoduleResolutioncontrol just module resolution #54788 swaps the trigger, and that change can go unnoticed since we already mademoduleResolution: nodenextandmodule: nodenextinseparable at Require module/moduleResolution to match when either is node16/nodenext #54567. ↩