-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Rethinking module
for the present and the future
#55221
Comments
I 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 Secondly, @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.
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 |
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 FWIW, I myself considered module type detection to be an inherent part of resolution when I implemented neoSphere's current module loader: |
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. |
What does
--module
actually 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
module
were limited toamd
,umd
,commonjs
,system
, andes2015
, the former definition was perfectly fine. Whenes2020
andes2022
were added, which added syntax features likeimport.meta
and top-levelawait
that couldn’t be transformed into other module emit targets besidessystem
, it started to feel likemodule
described not just an output format, but the intrinsic capabilities of some external system. Withnode16
andnodenext
, the scope of themodule
flag 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, oldermodule
values makes them feel kind of incoherent.All the values except
node16
/nodenext
are kind of weirdSome of the important characteristics of the module system described by
--module nodenext
are:If we try to infer from existing what the other
module
values say about these characteristics, the result is confusing. For example, you might expect that--module esnext
means 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
module
modes 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.ts
file 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, sinceesModuleInterop
is 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
module
modes 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
/nodenext
prior art, themodule
flag 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
/.js
extension, 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 oldermodule
modes 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.mjs
or.cjs
files that would be invalid in Node.js. (#50647, #54573)Options
Decisions I think are on the table:
module
value for bundlers?module
values 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?module
values to fix #54752, #50647, and #54573, or deprecate them in favor of new ones?module
values, 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 bundler
and--module bundler-node-compatible
, or--module bundler
and another flag enabling Node.js-compatible interop. Ignore everything else.Why I’d rather rethink
module
as a whole than do the minimal change:module
means for documentation purposes..mjs
and.cjs
files is a pretty bad behavior, and we should fix or deprecate every mode that does it.module
mode to represent the browser or another future runtime, and it’s annoying that--module esnext
is 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. Makemodule
controlimpliedNodeFormat
andmoduleResolution
control just module resolution #54788 swaps the trigger, and that change can go unnoticed since we already mademoduleResolution: nodenext
andmodule: nodenext
inseparable at Require module/moduleResolution to match when either is node16/nodenext #54567. ↩The text was updated successfully, but these errors were encountered: