首页 > 解决方案 > 使用 TypeScript 和 Babel(没有任何捆绑程序)为 Node 和现代浏览器构建 JavaScript,可能吗?

问题描述

我想要完成的是:

所以,这是我的注释tsconfig.json

{
  "compilerOptions": {
    "target": "ES2018", /* for Node (Babel will transpile for modern browser) */
    "module": "es6", /* for modern browsers (build script change this to cjs for Node) */
    "lib":
      "DOM",
      "ESNext",
    ],
    "declaration": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
  }
}

构建脚本package.json

"scripts": {
  "build:cjs": "tsc --m commonjs",
  "build:esm": "babel ./src --out-dir dist/esm --extensions .ts,.tsx"
},

脚本build:cjs似乎工作正常:require已使用并且声明正确输出。

相反,build:esm不工作......怀疑:

.babelrc

{
  "presets": [
    "@babel/env",
    "@babel/preset-typescript"
  ],
  "plugins": [
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread"
  ]
}

标签: javascriptnode.jstypescriptbabeljs

解决方案


推荐阅读