Babel 不编译打字稿,看起来它忽略了 babel.config.js



我正在尝试用 Babel 编译 Typescript。我的项目根文件夹中有babel.config.js

module.exports = function (api) {
// https://babeljs.io/docs/en/config-files#apicache
// api.cache(true);
return {
presets: [
'@babel/preset-env',
'@babel/preset-typescript',
'@babel/preset-react'
],
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-optional-chaining', { loose: true }],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-syntax-dynamic-import',
["babel-plugin-rewire"]
['inline-react-svg', { ignorePattern: /^(.(?!.Component.svg$))+$/ }]
]
};
};

我运行以下命令

npx babel --config-file babel.config.js --out-dir build-test --extensions '.ts,.tsx' src

我在解析打字稿时出错:

SyntaxError: src/api/CarInsurance.Client.ts: Unexpected token, expected { (4:7)
2 | 
3 | 
> 4 | export enum InsurancePlanType {

看起来babel.config.js文件没有被拾取,因为即使我在它的第一行抛出错误,也没有任何变化。

好的,问题是由以前安装的 Babel 6.x 版本引起的,即使在安装后也不知何故会优先。

解决方案:确保已安装本地版本的 @babel/core 和 @babel/cli,并显式调用它们。

npm install --save-dev @babel/core @babel/cli
./node_modules/.bin/babel src --config-file ./babel.config.js --out-dir build-test --extensions '.ts,.tsx'

最新更新