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

问题描述 投票:-1回答:1

我正在尝试使用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文件没有被拾取,因为即使我在第一行中输入了错误,也没有任何改变。

typescript config babel
1个回答
0
投票

好吧,该问题是由先前安装的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'
© www.soinside.com 2019 - 2024. All rights reserved.