René's URL Explorer Experiment


Title: Rethinking `module` for the present and the future · Issue #55221 · microsoft/TypeScript · GitHub

Open Graph Title: Rethinking `module` for the present and the future · Issue #55221 · microsoft/TypeScript

X Title: Rethinking `module` for the present and the future · Issue #55221 · microsoft/TypeScript

Description: 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? The output module syntax you want to be emitted A declarative description of the module system...

Open Graph Description: 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? The output module syntax you want to be emitted A decl...

X Description: 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? The output module syntax you want to be emitted A decl...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/55221

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Rethinking `module` for the present and the future","articleBody":"# What does `--module` actually mean?\r\n\r\nWhich of these is a better definition for the flag as it exists today? Which is a better fit for the future?\r\n\r\n1. The output module syntax you want to be emitted\r\n2. A declarative description of the module system that will process your emitted code at bundle-time or runtime\r\n\r\nWhen the possible values of `module` were limited to `amd`, `umd`, `commonjs`, `system`, and `es2015`, the former definition was perfectly fine. When `es2020` and `es2022` were added, which added syntax features like `import.meta` and top-level `await` that couldn’t be transformed into other module emit targets besides `system`, it started to feel like `module` described not just an output format, but the intrinsic capabilities of some external system. With `node16` and `nodenext`, the scope of the `module` 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.\r\n\r\nThe latter interpretation of `module`, one that fully describes the target module system, works well for `node16`/`nodenext`, but trying to project that definition onto the other, older `module` values makes them feel kind of incoherent.\r\n\r\n## All the values except `node16`/`nodenext` are kind of weird\r\n\r\nSome of the important characteristics of the module system described by `--module nodenext` are:\r\n\r\n- Multiple module formats are supported (CJS/ESM)\r\n- The module format of each file is determined via some algorithm\r\n- Modules of different formats interact with each other in specific ways\r\n\r\nIf 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 write `import foo = require(\"./mod\")` in that mode. But you _are_ allowed to import a dependency that _declares_ CommonJS constructs like that.\r\n\r\nNone 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 emitting `esnext`. This can be observed direclty by writing a default import of a `.d.ts` file that only declares named exports:\r\n\r\n```ts\r\n// @module: esnext\r\n// @esModuleInterop: true\r\n\r\n// @Filename: /esm.d.ts\r\nexport const x: string;\r\n\r\n// @Filename: /main.ts\r\nimport esm from \"./esm\";\r\nesm.x; // string, no error, what??\r\n```\r\n\r\nThis 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, since `esModuleInterop` 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.)\r\n\r\nEven 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.\r\n\r\n## Supporting bundlers\r\n\r\nWebpack and esbuild [vary their handling of ESM→CJS imports](https://andrewbranch.github.io/interop-test/#synthesizing-default-exports-for-cjs-modules) 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, the `module` 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.\r\n\r\nOther 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](https://github.com/microsoft/TypeScript/issues/54752)). It seems like we could improve on all the older `module` 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](https://github.com/microsoft/TypeScript/issues/50647), [#54573](https://github.com/microsoft/TypeScript/issues/54573))\r\n\r\n## Options\r\n\r\nDecisions I think are on the table:\r\n\r\n- Should we make at least one new `module` value for bundlers?\r\n- Should we make two new `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?\r\n- Should we add module format detection heuristics to the old `module` values to fix [#54752](https://github.com/microsoft/TypeScript/issues/54752), [#50647](https://github.com/microsoft/TypeScript/issues/50647), and [#54573](https://github.com/microsoft/TypeScript/issues/54573), or deprecate them in favor of new ones?\r\n- If we deprecate old `module` values, what new ones do we actually need?\r\n- Instead of encoding everything significant into `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?\r\n\r\nMy proposed minimal change:\r\n\r\n- Add (completely bikesheddable names, I hate them) `--module bundler` and `--module bundler-node-compatible`, or `--module bundler` and another flag enabling Node.js-compatible interop. Ignore everything else.\r\n\r\nWhy I’d rather rethink `module` as a whole than do the minimal change:\r\n\r\n- I want to be able to give a single coherent explanation of what `module` means for documentation purposes.\r\n- Emitting conflicting syntax into `.mjs` and `.cjs` files is a pretty bad behavior, and we should fix or deprecate every mode that does it.\r\n- [#54752](https://github.com/microsoft/TypeScript/issues/54752)\r\n- In the future, we may want to make a true ESM-only `module` mode to represent the browser or another future runtime, and it’s annoying that `--module esnext` is a poor fit for that.\r\n\r\n[^1]: Today, the module format detection (the setting of `impliedNodeFormat`) is actually triggered by `moduleResolution`, not `module`, but I think this doesn‘t make sense. #54788 swaps the trigger, and that change can go unnoticed since we already made `moduleResolution: nodenext` and `module: nodenext` inseparable at #54567.","author":{"url":"https://github.com/andrewbranch","@type":"Person","name":"andrewbranch"},"datePublished":"2023-07-31T22:23:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/55221/TypeScript/issues/55221"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:ba956978-e048-e6c5-961c-b1f1b806ca38
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idEC84:2F3AB6:5EAF1B9:7D82CE7:6A5DCE67
html-safe-nonce4415c98b3424896dbfdd07640e56b4f796903b42a11b29b15d1c228830d973c6
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzg0OjJGM0FCNjo1RUFGMUI5OjdEODJDRTc6NkE1RENFNjciLCJ2aXNpdG9yX2lkIjoiMjUyNTQ0OTg5NDM0MzA2OTI4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb23abc151edf24b09dadeb7eb753d1dd8cf56a4d9f69c063459d12ae7d7706a0
hovercard-subject-tagissue:1830138013
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/55221/issue_layout
twitter:imagehttps://opengraph.githubassets.com/6ffb412d5a3c5b836c5f2f4e7bb2364abcb72a08d0ebb001aa2b1560b19232d6/microsoft/TypeScript/issues/55221
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/6ffb412d5a3c5b836c5f2f4e7bb2364abcb72a08d0ebb001aa2b1560b19232d6/microsoft/TypeScript/issues/55221
og:image:altWhat 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? The output module syntax you want to be emitted A decl...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameandrewbranch
hostnamegithub.com
expected-hostnamegithub.com
None5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release9c975978430e9ad293956f2bbdaf153b1bd84a99
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/55221#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F55221
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F55221
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/55221
Reloadhttps://github.com/microsoft/TypeScript/issues/55221
Reloadhttps://github.com/microsoft/TypeScript/issues/55221
Please reload this pagehttps://github.com/microsoft/TypeScript/issues/55221
microsoft https://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Notifications https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Fork 13.6k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Star 110k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Code https://github.com/microsoft/TypeScript
Issues 5k+ https://github.com/microsoft/TypeScript/issues
Pull requests 31 https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality 0 https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
Code https://github.com/microsoft/TypeScript
Issues https://github.com/microsoft/TypeScript/issues
Pull requests https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
Rethinking module for the present and the futurehttps://github.com/microsoft/TypeScript/issues/55221#top
https://github.com/andrewbranch
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/andrewbranch
andrewbranchhttps://github.com/andrewbranch
on Jul 31, 2023https://github.com/microsoft/TypeScript/issues/55221#issue-1830138013
vary their handling of ESM→CJS importshttps://andrewbranch.github.io/interop-test/#synthesizing-default-exports-for-cjs-modules
1https://github.com/microsoft/TypeScript/issues/55221#user-content-fn-1-e44d38f74875edf2466e58077b671e71
#54752https://github.com/microsoft/TypeScript/issues/54752
#50647https://github.com/microsoft/TypeScript/issues/50647
#54573https://github.com/microsoft/TypeScript/issues/54573
#54752https://github.com/microsoft/TypeScript/issues/54752
#50647https://github.com/microsoft/TypeScript/issues/50647
#54573https://github.com/microsoft/TypeScript/issues/54573
#54752https://github.com/microsoft/TypeScript/issues/54752
Make module control impliedNodeFormat and moduleResolution control just module resolution #54788https://github.com/microsoft/TypeScript/pull/54788
Require module/moduleResolution to match when either is node16/nodenext #54567https://github.com/microsoft/TypeScript/pull/54567
https://github.com/microsoft/TypeScript/issues/55221#user-content-fnref-1-e44d38f74875edf2466e58077b671e71
andrewbranchhttps://github.com/andrewbranch
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.