TypeScript ESM TSCONFIG-从TSCONFIG内部读取哪些属性(IES)/值肯定会告诉输出是否是ESM?
我正在编写一个打字稿API转换,该变换在发射阶段处理源文件。我可以通过compileOptions
的内部值访问program.getCompilerOptions()
。
我需要知道输出是否将是“ commonjs”,ESM脚本或其他内容。 我想我可以从模块
字段中分辨出来,但我不确定。 Typescript定义 -
// export enum ModuleKind {
// None = 0,
// CommonJS = 1,
// AMD = 2,
// UMD = 3,
// System = 4,
// ES2015 = 5,
// ES2020 = 6,
// ...
// ESNext = 99
// }
我认为我可以使用此逻辑 -
const moduleKind: ts.ModuleKind | undefined =
program.getCompilerOptions().module;
if (!moduleKind) {
throw new Error(
`compilerOptions.module is undefined (should have default value)`
);
}
if (
moduleKind < ts.ModuleKind.ES2015 &&
moduleKind !== ts.ModuleKind.CommonJS
) {
// not esm and not commonjs
}
else if (moduleKind === ts.ModuleKind.CommonJS) {
// commonjs
} else {
// esm
}
但是,在Typescript 模块属性 它在侧边栏中列出了这些允许的值
none
commonjs
amd
umd
system
es6/es2015
es2020
es2022
esnext
node12
nodenext
,node12
和nodeNext
几乎可以肯定是“ commonjs”。 这让我认为某些值&gt; = es6/es2015
可能是(如果不是今天,则有一天)。我正在寻找这个问题的确切答案, 因为手册没有明确回答该问题。
I am writing a typescript API transform that processes source files during the emit phase. I have access in the transform to the internal value of the compileOptions
, via program.getCompilerOptions()
.
I need to know if the output will be 'CommonJS', esm script, or something else.
I think I can tell from the module
field, but I'm not certain.
Typescript defines -
// export enum ModuleKind {
// None = 0,
// CommonJS = 1,
// AMD = 2,
// UMD = 3,
// System = 4,
// ES2015 = 5,
// ES2020 = 6,
// ...
// ESNext = 99
// }
and I think I can use this logic -
const moduleKind: ts.ModuleKind | undefined =
program.getCompilerOptions().module;
if (!moduleKind) {
throw new Error(
`compilerOptions.module is undefined (should have default value)`
);
}
if (
moduleKind < ts.ModuleKind.ES2015 &&
moduleKind !== ts.ModuleKind.CommonJS
) {
// not esm and not commonjs
}
else if (moduleKind === ts.ModuleKind.CommonJS) {
// commonjs
} else {
// esm
}
However, on the typescript tsconfig man page for the module
property
it lists in the sidebar these allowed values
none
commonjs
amd
umd
system
es6/es2015
es2020
es2022
esnext
node12
nodenext
from which node12
and nodenext
are almost certainly "commonjs".
Which makes me think maybe some values >=es6/es2015
might be (and if not today, then someday) non-esm values. I'm looking for a definitive answer to this question,
because the manual doesn't answer that question explicitly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我打开了一个问题关于如何进行进程确定sourcefile是否被输出为“ concomjs”或“ concomjs”或“ concomjs” “ ESM”。 #48794 关于打字稿问题并获得了解决方案。
从打字稿代码文档
API客户端试图确定按照程序,每源文件是否会转换为绝对命令,绝对 - esm或其他内容,可以使用此编码逻辑:
I opened an issue Clarity on how to determine progmatically whether a sourceFile is to be output as "commonjs" or "esm". #48794 on typescript issues and obtained a solution.
From Typescript code documentation
An API client trying to determine per-Program, per-SourceFile whether it will be transformed to definitely-CommonJs, definitely-Esm, or something else, can use this coding logic: