-
Notifications
You must be signed in to change notification settings - Fork 12.8k
.mts file extension does not work as expected (tsc emits commonJS code instead) #51990
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
Comments
Essentially a duplicate of #50647. In that issue, the CJS/ESM is flipped from yours, but the point is that these extensions are only meaningful in one configuration mode, which is not the default. I agree with your expectation FWIW, but it gets a lot more complicated if you follow the principle all the way through. There is some related discussion in #49270 as well. |
I'm sorry for the duplicate issue, I wasn't exactly sure how to find an existing issue with the terms I had in mind (I only thought of Uhm, I can see why this might be difficult to make The reason I want to do it that way is to avoid configuring anything. I'm not a fan of configurations and try to make it work by using as little config as possible. Speaking of, I think you understand what I wanna achieve: I want to use ESModules with Types: essentially I want I tried to configure {
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "es2015", /* Specify what module code is generated. */
"moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */
"outDir": "./dist/", /* Specify an output folder for all emitted files. */
"esModuleInterop": false, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": false, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["./src/*.ts"],
} The problem here is that import path from "path" To work. Otherwise
But I don't think setting this flag to Node running the file on its own does not complain and it works just fine. Leading me to assume that Can you tell me what configs I need to set to get the behaviour I'm looking for? |
|
Without checking I'll take your word for it. Thank you for your help! |
Yeah, this isn't really practical for TypeScript. There are just too many variables w.r.t. where the code is going to be run, what ES version is supported by the target environment(s), etc. Sane defaults are great, but there's only so far that goes before you can no longer make everyone happy and configuration is required. That said, TS's defaults really aren't sane these days - there's a lot of legacy baggage there (default |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
The TypeScript compiler is fundamentally broken in its handling of Let's say you want to create a CJS library, but also have some tooling scripts that are written in ESM, for whatever reason (and there are reasons), that should likewise be part of the build output. For the most part your project uses Moreover, if you were wrapping This article on ECMAScript Modules in Node.js should be updated to warn about what's described here. Particularly this part under New File Extensions:
I would suggest this diff: - In turn, TypeScript supports two new source file extensions: .mts and .cts.
+ In turn, TypeScript partially supports two new source file extensions: .mts and .cts.
- When TypeScript emits these to JavaScript files, it will emit them to .mjs and .cjs respectively.
+ When TypeScript emits these to JavaScript files, it will emit them to .mjs and .cjs respectively,
+ but the .mjs will incorrectly use the CommonJS module system if you pass --module commonjs to the compiler. It seems the only thing for |
It absolutely does not, in |
Yes, by the decisions of the TypeScript team. There is no way to preserve the module system of This seems like a clear violation of your stated design goals 4 and 7. Can you tell me how to compile other |
|
Ok, so So the only way to convert a given |
That’s right, dual module format emit is not a well-supported scenario. #54593 |
I'm currently working a tool to allow dual builds, and for the most part I think I've got it working but it requires copying files over from one build into the other to account for unexpected module systems emitted during the I'll look at that more closely. Thanks for your time, I appreciate the feedback. |
We already do some mapping logic with respect to
|
You still are making arbitrary decisions, just like Node.js, but in contradiction to the latter. M$. |
My two cents: Dual ESM/CJS packages are essential at the moment due to the convoluted degree of support for ESM in the NodeJS world. ie, bundlers need all ESM code for maximum tree shaking, whereas a lot of tooling still requires all CJS to be parse-able. Simply saying "that's a bad idea" isn't an acceptable solution. None of this is a good idea, it's all just the unfortunate reality of where the ecosystem is right now. |
Not related to this issue at all, but Webpack is complaining about TypeScript syntax. A loader to remove type annotations is not being used. |
@andrewbranch Thanks, I've tried a lot of stuff based on this and a few other issues, and ended up changing |
Bug Report
I have created a post on stackoverflow asking about this behaviour, I haven't gotten a single reaction (yet). I asked on stackoverflow first, because I thought I might used
tsc
wrong.However, getting no reaction in conjunction with the "obvious" expected behaviour I decided to file a bug report here.
The behaviour is easily reproducable by first installing typescript like so:
npm install --save-dev typescript
then creating theexample.mts
file and then running./node_modules/.bin/tsc example.mts
- that's all.I expect
*.mts
files to be convert into VALID*.mjs
files WITHOUT using a configuration, I think that's a reasonable expectation.🔎 Search Terms
.mts
🕗 Version & Regression Information
💻 Code
🙁 Actual behavior
This is not a valid
.mjs
file because ES Modules do not have arequire
function.🙂 Expected behavior
The text was updated successfully, but these errors were encountered: