Description
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
import {foo} from '@xxx/foobar`
import type {Foo} from '@xxx/foobar`
where the before-transform replaces '@xxx/foobar
with './foobar`.
That's fine in "commonjs" and the final results looks like
const foo_1 = require("./foobar");
In "esm", still using the before-transform, I try replacing '@xxx/foobar
with './foobar.js`,
but the result looks like
import {foo} from './foobar.js'
import type {Foo} from 'foobar.js'
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 the module
value. Although they won't appear as source files unless module
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 about package.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.