-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Clarity on how to determine progmatically whether a sourceFile is to be output as "commonjs" or "esm". #48794
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
The ESM doc says
Doesn’t that spell everything out? And yes, at the top you’re supposed to understand that the document is in context of |
@andrewbranch - Thank you very much for taking the time to answer. Let me phrase the problem in the operational terms. During a solution watch build, a piece of software for emit-time custom transformations needs to know, on a per-emit, per-SourceFile basis, whether typescript is going to transform the SourceFile as commonjs or esm. (In the use-case described, it will process as a "before"-transform or "after"-transform accordingly.) If SourceFile["fileName"] has a Does your answer mean that in the case of The reason I feel some dissonance in that answer is because the Also, the What should a client application of the |
Ah, you’re asking for an API to answer the question more than you’re asking how it works. |
We set |
@andrewbranch @weswigham - Wonderful - thank you so much! In ts 4.5.5 I can see the member
Only the last sentence "Generally speaking ..." is ambiguous, from the perspective of an API client application. I think this is a correct future-proof coding for an API client -
Would you be so kind as to confirm whether it is correct? If it is correct, I will close this issue. |
No—the algorithm is
|
I think the code comment could just as well have left off “Generally speaking” |
The |
I mean, non-module (script) source files technically exist. They're just rare. |
Oh yeah sorry, my comment was assuming node12/nodenext but that’s not in your assumptions. As for scripts, you can tell modules from scripts by the source file’s |
In other words, I think this is right if you assume all input files are modules, and ignore some weird things like |
@andrewbranch @weswigham - Great! This has been a huge help! Closing now. |
c.f., #48961 - It should be moduleResolution that is set to "Node12" or "NodeNext", not module, which can be left unset. |
Bug Report
It appears that at least some import path "before"-transform action that works with "commonjs" mode, needs "after"-transform action to work in "esm" mode. That's OK, and not a bug.
For example
where the before-transform replaces
'@xxx/foobar
with './foobar`.That's fine in "commonjs" and the final results looks like
In "esm", still using the before-transform, I try replacing
'@xxx/foobar
with './foobar.js`,but the result looks like
and the
import type
is an error.Fortunately, just changing the before-transform to an after-transform for the "esm" case only, works perfectly.
So that is not a bug.
Now the problem is only determining whether typescript is going to output "commonjs" or "esm" (or something else).
Currently I am using
program.getCompilerOptions().module>=ts.ModuleKind.ES2015
to decide if a ".ts" file will be output as "esm"but I am not absolutely confident that is correct. This is based on the documentation for compilerOptions/module and the assumption that going forward all new values for
module
will imply "esm".Now looking at the experimental doc for esm, it appears that
if a file extension is
.cts
or.mts
then it will be "commonjs" or "esm" respectively, whatever themodule
value. Although they won't appear as source files unlessmodule
is set to 'node12' or 'nodenext', I guess.But what's the difference between 'node12' and 'nodenext'? Is it the default output type of straight
.ts
extension file (commonjs vs esm)? The document doesn't say. The document talks much aboutpackage.json/type
- that is not being used as the source of truth for the default output (commonjs vs esm), is it?.This report is classifying the lack of documentation clarity as a bug,
because without clarity there is a good change of written software crashing.
🔎 Search Terms
import path transform commonjs vs esm modules
🕗 Version & Regression Information
I noticed when reading documentation for compilerOptions/module, experimental doc for esm.
⏯ Playground Link
💻 Code
🙁 Actual behavior
🙂 Expected behavior
The text was updated successfully, but these errors were encountered: