如何在打字稿中等待最高级别(从commonjs切换到esnext),而无需更改所有导入以使.js结束

发布于 2025-02-07 14:07:44 字数 1076 浏览 3 评论 0原文

我想在我的打字稿nodejs项目中等待最高水平。

我的tsconfig曾经看起来像这样:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

我现在将其切换到

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

我还添加了“ type”:“ module”到我的软件包。实际上,我现在有能力进行顶级等待,但是

  1. 我需要更改所有导入,以添加
  2. 文件夹的.js文件扩展名,其中添加了index.ts.ts来导出我以前只能导入文件夹名称的所有模块。现在,我需要导入foldername/index.js时
  3. ,当我自动添加与vscode添加导入时,它会添加它而没有.js

的方式是如此优雅 - 我可以使用Esnext具有相同的行为,或者在某种程度上保持其他方式获得顶级等待?

I would like to have top level await in my typescript nodejs project.

My tsconfig used to look like this:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

And I now switched it to

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

I have also added "type": "module" to my package.json. Indeed I now have the ability to do top level awaits however

  1. I need to change every import to add the .js file extension
  2. For folders where I added an index.ts to export all the modules I could previously just import the folder name. Now I need to import foldername/index.js
  3. When I auto add an import with vscode it adds it without the .js

The way it is with commonjs is so elegant - can I have the same behaviour with esnext or keep it some other way while gaining top-level await?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不交电费瞎发啥光 2025-02-14 14:07:44

这是您的tsconfig.json文件的外观示例:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true
    },
  }

Here's an example of what your tsconfig.json file might look like:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true
    },
  }
乞讨 2025-02-14 14:07:44

我不知道这是否是一个选项,但是我认为最简单的方法是将应用程序的入口包装到某种main()函数中,该功能是异步并执行该功能的。在此功能中,您可以像想要的那样使用等待。

I don't know if that is an option, but i think the easiest approach would be, to wrap the entrance of your application into some kind of main() function that is async and execute that function. Inside this function, you can use await like you want.

只涨不跌 2025-02-14 14:07:44

要在Node.js中使用ES模块的顶级等待,您必须在导入中包括.js扩展。但是,可以通过使用webpack bundler来避免这种情况,以处理无需扩展的导入,并将其添加到webpack.config.js文件中:

  resolve: {
    extensions: ['.ts', '.js'],
  },

希望它会有所帮助!

To use top-level await with ES modules in Node.js, you must include .js extensions in imports. But this can be avoided by using a webpack bundler to handle imports without extensions and add this in webpack.config.js file:

  resolve: {
    extensions: ['.ts', '.js'],
  },

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文