javascript - 使用 TypeScript 和 Babel(没有任何捆绑程序)为 Node 和现代浏览器构建 JavaScript,可能吗?
问题描述
我想要完成的是:
- 使用具有现代语法 (
DOM
,ESNext
aslib
)的 TypeScript - 目标节点 10.x 及更高版本(ES2018 + CommonJS 模块)
- 针对现代浏览器(Babel 预设 env + ES6 模块)
- 只使用 TypeScript 和 Babel(不使用 Rollup、Webpack 或任何其他打包工具)
所以,这是我的注释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
不工作......怀疑:
- 使用
require
而不是import
? - 没有声明,为什么?
.babelrc
:
{
"presets": [
"@babel/env",
"@babel/preset-typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
解决方案
推荐阅读
- swift - 您的应用包含 NSUserTrackingUsageDescription,表示您将请求跟踪用户的权限
- javascript - js设置选中的单选框问题,用户选择了另一个
- javascript - 在 Angular 5 中使用多次子组件
- shell - 批量转换图片文件为webp并清理旧文件
- php - 递归推入数组
- sdk - 无法确定任务“:compileReleaseAidl”的依赖关系
- javascript - 如何根据另一个值获取对象中的值
- elasticsearch - fluentd-elasticsearch 无法挂载 hostPath 卷错误
- javascript - 检查购物车中的重复项并更新数量(ReactJS)
- git - 在另一个存储库中更改丢失的 git 存储库