How to compile a file with Babel CLI

I am trying to compile a simple file es6with babel CLI

Given the details below: what is going wrong?

$ node --version
v5.0.0

$ npm --version
3.3.6

$ npm init
$ npm install --save-dev babel-cli

$ echo -e 'import url from "url"\nconsole.log(`2+2=${2+2}`)' > script.js

$ ./node_modules/.bin/babel  script.js 
import url from "url";
console.log(`2+2=${ 2 + 2 }`);

In other words: I put ES6 and I exit ES6 (albeit with a slightly different spacing, and with a comma). I expect the import to be converted to the required one and that my back ticks disappear.

That is: I want ES5 out .

What do I need to do differently?

+4
source share
2 answers

Babylonian version 6 sends "without any default conversions." You can learn more about the changes to this blog post.

es6 es5, :

  • run npm i --save-dev babel-preset-es2015

  • .babelrc :

    {

       "presets": ["es2015"]
    

    }

, , .

+9

: --presets "es2015" babel.

:

$ ./node_modules/.bin/babel  --presets "es2015" script.js 
"use strict";

var _url = require("url");

var _url2 = _interopRequireDefault(_url);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log("2+2=" + (2 + 2));
+1

Source: https://habr.com/ru/post/1615495/


All Articles