[英]Transpiling a Node.js + TypeScript app with Babel: why it's not using optional chaining?
I am experimenting with Babel to understand better how it works.我正在试验 Babel 以更好地了解它是如何工作的。 I created a simple API with Node.js and TypeScript.
我用 Node.js 和 TypeScript 创建了一个简单的 API。 Everything works fine when I transpile code and the server runs properly.
当我转译代码并且服务器正常运行时,一切正常。 I have a question though: in node
v14.15.3
optional chaining is supported, so why when I build with:不过我有一个问题:在节点
v14.15.3
中支持可选链接,那么为什么当我构建时:
rm -rf build/ && babel src --source-maps --extensions '.js,.ts,.tsx' --ignore '**/*.test.ts' -d build
no matter what version of node I'm running in the shell (by using nvm - v12.18.3
or v14.15.3
) the following line无论我在 shell 中运行什么版本的节点(通过使用 nvm -
v12.18.3
或v14.15.3
)以下行
console.log(x?.ciao ?? 10)
it's always transpiled to它总是被转换成
console.log((_x$ciao = x === null || x === void 0 ? void 0 : x.ciao) !== null && _x$ciao !== void 0 ? _x$ciao : 10);
I would expect it to use .?
我希望它使用
.?
and ??
和
??
in the transpiled code since they're supported.在转译的代码中,因为它们受支持。 What am I missing?
我错过了什么?
I also tried to remove the @babel/preset-typescript
preset and did a test with a single file, but yet the code is transpiled as above.我还尝试删除
@babel/preset-typescript
预设并使用单个文件进行测试,但代码已按上述方式进行转换。
My .babelrc
file:我的
.babelrc
文件:
{
"presets": ["@babel/preset-typescript", ["@babel/preset-env", { "shippedProposals": true }]],
"plugins": [
[
"module-resolver",
{
"root": "./src",
"alias": {
"@root": ["./"],
"@src": ["./src"]
},
"extensions": [".js", ".ts"]
}
]
],
"sourceMaps": true
}
To make it work use "targets": { "node": 14 }
as in the following .babelrc
example.要使其工作,请使用
"targets": { "node": 14 }
,如下面的.babelrc
示例所示。 This will not transpile .?
这不会转译
.?
or ??
还是
??
since they are supported in node 14.x.因为它们在节点 14.x 中受支持。
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": { "node": 14 }
}
]
],
"plugins": [
[
"module-resolver",
{
"root": "./src",
"alias": {
"@root": ["./"],
"@src": ["./src"]
},
"extensions": [".js", ".ts"]
}
]
],
"sourceMaps": true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.