-
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
Using "allowJs": true
and "module": "commonjs"
to transform .mjs
files should emit .cjs
files
#54573
Comments
I did a bit of digging and I believe this is the piece of code responsible for determining the extension of the output file: TypeScript/src/compiler/emitter.ts Lines 574 to 580 in e49a15f
|
Related, I found these minutes from a design meeting on the feature to support |
I think that #35148 (and #35589) is related; however, I don't recall whether that also did path rewriting - and we didn't have the context of #44442 at the time. As you pointed out, in #44442 we discussed the issue of Is there any reason you don't want to just use |
I agree the behavior is bad, but I think the solution we want is either
|
Yes. Since the package has |
@andrewbranch If using I assume both suggestions would also apply to |
If the code is intended to always target CJS, it should just be a cts file. A naive syntactic transform from ESM to CJS is liable to end up importing semantically different modules from when the code was written, causing unpredictable breaks. This isn't something that you can "just" do in the general case. |
Tagging as backlog for either of the proposed behaviors, not the behavior described in the OP |
Converting This is the correct approach.
Please fix this. |
Why would that be expected? If you specify a file as
Yes, a compiler error, not user error. Any Also, why was this labelled Possible Improvement and not a bug? |
Because the tsconfig has |
This is where you and TypeScript differ with the way the Node.js runtime operates against file extensions. Too bad. Congrats on your newborn. |
Bug Report
🔎 Search Terms
CommonJS, allowJS, ESM, CJS, file extensions
🕗 Version & Regression Information
⏯ Playground Link
Sandbox link with relevant code (I couldn't use the playground, since this involves using an .mjs file).
💻 Code
🙁 Actual behavior
The
javascript.mjs
is transformed to CommonJS (as expected), but the file extension of the emitted file is still.mjs
.This breaks the package, since
.mjs
is supposed to be use exclusively for JavaScript using ESM and the emitted file now uses CommonJS.🙂 Expected behavior
I would expect
tsc
to transform thesrc/javascript.mjs
to CommonJS and either:dist/javascript.cjs
or alternatively"type": "commonjs"
in the package.json and emit the file as.js
as it does with thesrc/typescript.ts
file.The text was updated successfully, but these errors were encountered: