0

我有一个使用babel-preset-react-app. 我想通过运行带有@babel/node. 不幸的是,我收到以下错误:

trpgb21@development:~/develop/ts-demo-new$ DEBUG=* NODE_ENV=production npx babel-node --extensions '.ts,.tsx'
> var test = require('./src')
  babel:config:loading:files:configuration Found configuration '/home/trpgb21/develop/ts-demo-new/package.json' from '/home/trpgb21/develop/ts-demo-new'. +0ms
  babel:config:loading:files:plugins Loaded preset 'react-app' from '/home/trpgb21/develop/ts-demo-new'. +0ms
  babel program.body[0] VariableDeclaration: enter +0ms
  babel program.body[0] VariableDeclaration: Recursing into... +1ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: enter +0ms
  babel program.body[0].declarations[0].id Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +1ms
  babel program.body[0].declarations[0].init.callee Identifier: enter +0ms
  babel program.body[0].declarations[0].init.callee Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].init.callee Identifier: exit +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: enter +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: Recursing into... +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program Program: enter +2ms
  babel program.body[2] ExpressionStatement: Inserted. +1ms
  babel program.body[0] VariableDeclaration: enter +0ms
  babel program.body[0] VariableDeclaration: Recursing into... +1ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel program.body[0] VariableDeclaration: enter +1ms
  babel program.body[0] VariableDeclaration: Recursing into... +0ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1] ExpressionStatement: exit +1ms
  babel program.body[0] VariableDeclaration: enter +0ms
  babel program.body[0] VariableDeclaration: Recursing into... +0ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: enter +0ms
  babel program.body[0].declarations[0].id Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +0ms
  babel program.body[0].declarations[0].init.callee Identifier: enter +0ms
  babel program.body[0].declarations[0].init.callee Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].init.callee Identifier: exit +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: enter +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: Recursing into... +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1].expression Identifier: enter +0ms
  babel program.body[1].expression Identifier: Recursing into... +1ms
  babel program.body[1].expression Identifier: exit +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel program.body[0] VariableDeclaration: enter +0ms
  babel program.body[0] VariableDeclaration: Recursing into... +0ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel program.body[0] VariableDeclaration: enter +1ms
  babel program.body[0] VariableDeclaration: Recursing into... +0ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel program Program: Recursing into... +0ms
  babel program.body[0] VariableDeclaration: enter +0ms
  babel program.body[0] VariableDeclaration: Recursing into... +1ms
  babel program.body[0].declarations[0] VariableDeclarator: enter +0ms
  babel program.body[0].declarations[0] VariableDeclarator: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: enter +0ms
  babel program.body[0].declarations[0].id Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].id Identifier: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: enter +0ms
  babel program.body[0].declarations[0].init CallExpression: Recursing into... +1ms
  babel program.body[0].declarations[0].init.callee Identifier: enter +0ms
  babel program.body[0].declarations[0].init.callee Identifier: Recursing into... +0ms
  babel program.body[0].declarations[0].init.callee Identifier: exit +0ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: enter +4ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: Recursing into... +1ms
  babel program.body[0].declarations[0].init.arguments[0] StringLiteral: exit +0ms
  babel program.body[0].declarations[0].init CallExpression: exit +0ms
  babel program.body[0].declarations[0] VariableDeclarator: exit +0ms
  babel program.body[0] VariableDeclaration: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +0ms
  babel program.body[1].expression Identifier: enter +0ms
  babel program.body[1].expression Identifier: Recursing into... +0ms
  babel program.body[1].expression Identifier: exit +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel program Program: exit +0ms
  babel program.body[1] ExpressionStatement: enter +0ms
  babel program.body[1] ExpressionStatement: Recursing into... +1ms
  babel program.body[1].expression Identifier: enter +0ms
  babel program.body[1].expression Identifier: Recursing into... +0ms
  babel program.body[1].expression Identifier: exit +0ms
  babel program.body[1] ExpressionStatement: exit +0ms
  babel:config:loading:files:configuration Found configuration '/home/trpgb21/develop/ts-demo-new/package.json' from '/home/trpgb21/develop/ts-demo-new/src'. +305ms
Thrown:
/home/trpgb21/develop/ts-demo-new/src/index.tsx:1
import * as React from "react";
       ^

SyntaxError: Unexpected token *

为什么 babel 不使用预设转换代码???

这是我的package.jsonbabel 块供参考:

  "babel": {
    "presets": [
      "react-app"
    ]
  },
4

1 回答 1

0

通过"debug": true@babel/preset-env我解决这个问题,以防其他人遇到带有 babel 的递归 program.body 循环:

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "loose": true,
                "modules": false,
                "debug": true
            }
        ]
    ],
    "env": {
        "commonjs": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "loose": true,
                        "modules": "commonjs"
                    }
                ]
            ]
        },
        "test": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "loose": true
                    }
                ]
            ]
        }
    }
}
于 2020-11-19T15:59:02.337 回答