无法获得Babel版本7 @ babel /注册工作

时间:2018-12-09 10:07:45

标签: babel babel-register

我正在尝试babel @ 7的@ babel / register,但似乎无法正常工作。我的package.json如下:

#: import  FadeTransition kivy.uix.screenmanager.FadeTransition

<Information>
    transition: FadeTransition()
    SlothFacts:
    KiwiFacts:

<SlothFacts>:
    name: "sloth"
    Label:
        text:"im sloth"

<KiwiFacts>:
    name: "kiwi"
    Label:
        text:"im kiwi"

和需要('@ babel / register')的es6文件是:

{
  "name": "trying-out-babel-register-v7",
  "version": "1.0.0",
  "engines": {
    "node": "~6.0.0"
   },
  "license": "MIT",
  "dependencies": {
    "@babel/core": "^7.2.0",
    "@babel/register": "^7.0.0"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.2.0"
  },
  "scripts": {
    "start": "node index.js"
  }
}

请注意,我有意使用节点版本6来检查babel是否确实在编译我的es6脚本。

我得到:

require('@babel/register')({
  presets: [
    [
       "@babel/env",
       {
         module: false,
         targets: { "node": process.versions.node },
         useBuiltIns: "usage"
       }
    ]
  ]
});

const f = () =>{ console.log('arrow function work')}
f()

const a = {'a': 'a'};

const b = {
  'b':'b',
  ...a
};
console.log(b)

class A {
  constructor() {
    console.log('hello class')
  }
}
const k = new A()

回购链接:https://github.com/ApolloTang/trying-out-babel-register-v7

1 个答案:

答案 0 :(得分:0)

我知道了: 由于某种原因,要转译的代码必须在所需的文件中(请参见下面的注释):

require('@babel/register')({
  presets: [
    [
       "@babel/env",
       {
         // module: false,      // <--- typo, not module
         modules: "commonjs",   // <--- must transpile to commonjs module
         targets: { "node": process.versions.node },
         useBuiltIns: "usage"   // <--- not sure if this work
       }
    ]
  ]
});

const f = () =>{ console.log('arrow function work')}
f()

 /*
  * does not work here, but works in the
  * required file script-1.js
  *
  * const a = {'a': 'a'};
  *
  * const b = {
  *   'b':'b',
  *   ...a
  * };
  * console.log(b)
  */

class A {
  constructor() {
    console.log('hello class')
  }
}
const k = new A()

// import someScript from  './script-1.js'; // <-- import does not work here but will work in ./script-1.js
require( './script-1.js')

我还在回购中包含了工作代码,链接: https://github.com/ApolloTang/trying-out-babel-register-v7/tree/solved