Repository navigation
Design Meeting Notes, 6/4/2021 #44442
Copy link
Copy link
Closed
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Description
ES Modules in Node
https://gist.github.com/weswigham/22a064ffa961d5921077132ae2f8da78
- Last time we left off talking about
.tsxsyntax and how it affected JS module support..cjsand.mjsneed a corresponding input format for them..ctsand.mts.jsx->.tsx.mjsx->.mtsx?- Can potentially get away with just putting JSX in
.mts. - Is
.mjsxa thing? - No, probably an assumption that
.mjscan contain JSX. - JSX is a superset of JS, right?
- Yes but not a superset of TS.
- In
.jsfiles we always parse JSX anyway.- We were trying to be "descriptive", not "prescriptive" - not something we're in love with.
- Can potentially get away with just putting JSX in
.cjsand.mjs- Have to start recognizing these as inputs under
allowJs - Idea: in the new Node mode, we "do the right thing" - otherwise, we just assume they should be handled as regular
.jsfiles. - No
.cjsx,.mjsx. - Do we conditionally parse JSX on whether the JSX flag is specified?
- No, wouldn't start now either.
- We just complain in grammar checks if you use JSX syntax without the flag.
- Have to start recognizing these as inputs under
- What is the distinction between
.jsand.jsxin today's implementation?- None.
.ctsand.mtsextensions- These include TypeScript syntax and correspond exactly to
.cjsand.mjsrespectively. - That way we don't have to add a flag to say "emit as
.cjs" and "emit as.mjs" from.tsinput.
- These include TypeScript syntax and correspond exactly to
- How do we avoid
.ctsxand.mtsx?- Can just assume
.ctsincludes JSX,.mtsincludes JSX. - Would have to get rid of
- Angle bracket type assertion syntax (
<Type>expression)- Workaround:
expression as Type
- Workaround:
- Arrow functions with single type parameters (
<T>(x: T) => x)- Workaround:
<T,>(x: T) => xor<T extends unknown>(x: T) => x
- Workaround:
- Infinite lookahead?
- Already do infinite look-ahead in other places.
- Could try parsing an arrow function following specific JSX opening tags.
- Then error if you can successfully parse.
- Feels like we can do the disambiguation.
- But the
>in the arrow is not illegal.- "Could've sworn that JSX rejected certain characters."
- "Both major compilers just allowed them." 😕
- What if you unsuccessfully parse?
- You can reject the perfectly-written arrow functions, but imperfectly-written ones will get bad error messages.
- Angle bracket type assertion syntax (
- Feels weird that you might try to rename your file and then get errors with no explanation.
- Makes migration harder.
- Would you use
.cjsfor React? It sounds like a Node-ism.- Electron?
- Maybe worth checking with stewards of JSX (e.g. reach out to React team).
- Can just assume
- Can always support
.ctsand.mtswithout JSX support and add it later.- But that means you have to reject the ambiguous syntax as well.
- Otherwise you end up with the same issue of
.tssyntax conflicting with JSX syntax.
- Otherwise you end up with the same issue of
let x = <Foo>yadda;- have to disallow thislet f = <T>(x: T) => x;- have to disallow this- But can always revisit and relax this!
- But that means you have to reject the ambiguous syntax as well.
<T>3</T>- JSX Open, 3, LessThan, RegExp, Oops End-of-File
- Could consider a prologue directive like
// @jsx: true - Are we overthinking this based on arrow functions?
- Prologue directives affecting parsing is not something our parser is wired up for.
- We do these things post-parse.
- TmLanguage grammars too!
- Can't say "switch to TypeScript-React" right after.
- Start-line headers? Something we can talk about with partner teams.
- Issue an error on and generic arrow functions?
- Has the same downsides of the JSX variant from the start?
- But future-proofed.
- Gives us more direction, but doesn't fix the issue of
.tsx->.mtsbeing weird...but works better for.ts->.mts. - Editor support and command line tooling for helping the migration?
- Sounds cool!
- Conclusion:
.mtsand.ctsparsed as.ts, additional grammar error to inform people of what you meant if you wrote specific constructs. - Declaration for
.cjs,.mjs.d.cts,.d.mts- We used to be scared of this because of the new resolution rules, but now we read directory contents ahead of time to see whether a file read will succeed.
- We would have to have separate shapes for different modules depending on how it's imported.
- You only need to look for
.d.mtsif you import with an.mjsextension, right?- Yes.
- Something worth thinking as a follow-up:
- JSON:
.d.tson? - Extension priorities will add a lot of complexity and work.
- JSON:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Activity
Airkro commentedon Jun 25, 2021
mjs/cjsis fornode.jsonly, not for browsers ordenocommonjsnever be a part ofECMAScript, not supported in browsersmjsinnode.jshave some behavior not support in browsers, it's notpure ECMAScriptJSXis forhtml in browserSo, I think there is no need to support
JSXinmjs/cjsI'm not a team member, I guess this issue is comment allowed, let me know if it's not, I'll delete this.
fatcerberus commentedon Jul 27, 2021
Can you clarify on this point? Browsers don't care about file extensions (only mimetypes), and IIRC module specifier resolution is implementation-defined per ECMAScript. I don't think there's anything non-standard there?
Airkro commentedon Jul 27, 2021
Bruce Pascoe (@fatcerberus)
Try to say it clear, I mean the code might be an ECMAScript-module, but it doesn't mean they have the same implementation:
For a long time, we don't know
*.jsis ready for who, browser or node.jsBut now we know
*.mjs/*.cjsis for node.js only.Airkro commentedon Jul 27, 2021
I thought
JSXis a sugar designed to make HTML binding easier, it's for browser only.But now I realize
JSXis used in many SSR projects. run them innode.jsis possible, I should change my mind.BenBeattieHood commentedon Oct 18, 2022
Has there been any further decision on the adoption of an
.mtsxextension?GoToLoop commentedon Jan 12, 2023
I'd love for TS to recognize both ".mtsx" & ".ctsx" file extensions!
Possibly emit them as ".mjsx" & ".cjsx" respectively.
docs(changeset): Unhandle .cjsx and .mjsx which are unsupported exten…
yinzara commentedon Jun 30, 2023
There is still no way now more than 2 years after this discussion to indicate in a CommonJS React project that a specific file is a TypeScript MJS module making it impossible in a TypeScript CJS application to use any ESM only dependency which now means
react-dnd(the primary drag and drop utility) is no longer accessible to the majority of ReactJS apps as it has gone ESM only (ARGGGGGGGGG WHY!!!!!).I am forced to use a
.mjsfile and completely remove all typing from my file and then use a combination of Suspense and Lazy to load it as a component in a CJS file.EvanLovely commentedon Sep 12, 2024
Wish there was a solution here; anybody have one?