Не могу получить версию babel 7 @ babel / зарегистрироваться на работу - PullRequest
0 голосов
/ 09 декабря 2018

Я пробую babel @ 7's @ babel / register, но не могу заставить его работать.Мои package.json следующие:

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

и файл es6, который требует ('@ babel / register'):

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()

Обратите внимание, что я намеренно использую версию 6 узла для проверкиесли babel действительно перенесет мой сценарий es6.

и я получу:

$ nvm use 6
Now using node v6.9.1

$ npm run start

> trying-out-babel-register-v7@1.0.0 start /Users/apollotang/Desktop/trying-out-babel-register-v7
> node index.js

/Users/apollotang/Desktop/trying-out-babel-register-v7/index.js:21
   ...a
   ^^^
SyntaxError: Unexpected token ...
   at Object.exports.runInThisContext (vm.js:76:16)
   at Module._compile (module.js:542:28)
   at Object.Module._extensions..js (module.js:579:10)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.runMain (module.js:604:10)
   at run (bootstrap_node.js:394:7)
   at startup (bootstrap_node.js:149:9)
   at bootstrap_node.js:509:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! trying-out-babel-register-v7@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the trying-out-babel-register-v7@1.0.0 start script 'node index.js'.

ссылку на репо: https://github.com/ApolloTang/trying-out-babel-register-v7

1 Ответ

0 голосов
/ 10 декабря 2018

Я понял: по какой-то причине передаваемый код должен быть в требуемом файле (см. Комментарий ниже):

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

...