@ babel / node为什么不转换我的代码

时间:2019-12-14 18:45:30

标签: typescript babeljs babel create-react-app babel-node

我有一个使用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 *

为什么不通行使用预设来转换代码?

这是我的package.json通天塔块供参考:

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

1 个答案:

答案 0 :(得分:0)

"debug": true传递给@babel/preset-env为我解决了此问题,以防其他人遇到递归程序。

{
    "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
                    }
                ]
            ]
        }
    }
}