TypeScript ESM TSCONFIG-从TSCONFIG内部读取哪些属性(IES)/值肯定会告诉输出是否是ESM?

发布于 2025-01-22 16:38:12 字数 1348 浏览 2 评论 0原文

我正在编写一个打字稿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 

node12nodeNext几乎可以肯定是“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-01-29 16:38:12

我打开了一个问题关于如何进行进程确定sourcefile是否被输出为“ concomjs”或“ concomjs”或“ concomjs” “ ESM”。 #48794 关于打字稿问题并获得了解决方案。

从打字稿代码文档

(属性)sourcefile.impliednodeformat?:ts.modulekind.commonjs | ts.modulekind.esnext |未定义

当模块为node12或nodeNext时,此字段控制所讨论的源文件是esnext-of-unput-format文件还是commonjs-outjs-output-unput-format模块。这是由模块解析器在查找文件时得出的,因为它是从模块的文件扩展名或包含的package.json上下文中得出的,并且会影响检查和发射。

是公开
转换。一般来说,如果尚不确定,则将该领域视为Modulekind.commonjs。

API客户端试图确定按照程序,每源文件是否会转换为绝对命令,绝对 - esm或其他内容,可以使用此编码逻辑:

if SourceFile.impliedNodeFormat===ModuleKind.CommonJS, then "commonjs"
else if SourceFile.impliedNodeFormat===ModuleKind.ESNext, then "module"
else if compilerOptions.module===ts.ModuleKind.CommonJS, then "commonjs"
else if compilerOptions.module>=ts.ModuleKind.ES2015, then "module"
else not definitely "commonjs" nor definitely "module"

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

(property) SourceFile.impliedNodeFormat?: ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined

When module is Node12 or NodeNext, this field controls whether the source file in question is an ESNext-output-format file, or a CommonJS-output-format module. This is derived by the module resolver as it looks up the file, since it is derived from either the file extension of the module, or the containing package.json context, and affects both checking and emit.

It is public so that (pre)transformers can set this field, since it switches the builtin node module
transform. Generally speaking, if unset, the field is treated as though it is ModuleKind.CommonJS.

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:

if SourceFile.impliedNodeFormat===ModuleKind.CommonJS, then "commonjs"
else if SourceFile.impliedNodeFormat===ModuleKind.ESNext, then "module"
else if compilerOptions.module===ts.ModuleKind.CommonJS, then "commonjs"
else if compilerOptions.module>=ts.ModuleKind.ES2015, then "module"
else not definitely "commonjs" nor definitely "module"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文