Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Babel 6 to compile to ES5 javascript?

I've installed the latest version of babel. Currently 6.4.0. I create a file called myclass.js that has the following code.

class MyClass {     constructor(name) {         console.log("I am a MyClass object .. ", name);     } }  var myclass = new MyClass('1234'); 

after creating my class I do the following in the command line.

$> babel ./src/myclass.js --out-file ./out/app.js 

I would expect my app.js file to have es5 compiled javascript but it has the same exact code in it that the myclass.js file has. What am I missing that may be causing this?

like image 737
mattwallace Avatar asked Jan 12 '16 15:01

mattwallace


People also ask

How do you compile with Babel?

Simply add a "scripts" field to your package. json and put the babel command inside there as build . This will run Babel the same way as before and the output will be present in lib directory, only now we are using a local copy. Alternatively, you can reference the babel cli inside of node_modules .

How does Babel work JavaScript?

Babel is a tool that lets you write your Javascript code using all the latest syntax and features, and run it in browsers that may not support those features. Babel is a transpiler that will translate your modern JS code into an older version of Javscript that more browsers are able to understand.

What is Babel ES6?

Babel is a free and open-source JavaScript transcompiler that is mainly used to convert ECMAScript 2015+ (ES6+) code into a backwards compatible version of JavaScript that can be run by older JavaScript engines. Babel is a popular tool for using the newest features of the JavaScript programming language.


2 Answers

You don't tell Babel to target ES5, you choose the necessary presets/plugins that do that for you. For example, if you use the es2015 preset, this will compile ES6 code to ES5-compatible code. You don't specify a "target".

The guide below will take you through using Babel to transform ES6 code into code that can run in an environment that supports ES <= 5.


0. A note on API changes from Babel 5

In the documentation for Babel 6:

The babel package is no more. Previously, it was the entire compiler and all the transforms plus a bunch of CLI tools, but this lead to unnecessarily large downloads and was a bit confusing. Now we’ve split it up into two separate packages: babel-cli and babel-core.

And:

Babel 6 ships without any default transforms, so when you run Babel on a file it will just print it back out to you without changing anything.

______

1. Install babel-cli

First, as in the docs, you need to install babel-cli:

$ npm install babel-cli 

______

2. Define presets in .babelrc

Second, you need to use a .babelrc (docs) local to your files and explicitly define the presets that you want Babel to use. For example, for ES6+ features use the env preset.

...a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s).

Install it:

npm install @babel/preset-env 

And then declare it in your .babelrc:

{   "presets": ["@babel/preset-env"] } 

Note: if you are using Babel 7.x you may prefer to use a "project-wide configuration" (babel.config.js) (docs) which "applies broadly, even allowing plugins and presets to easily apply to files in node_modules or in symlinked packages".

______

3. Run babel

Now running the babel command as in your example should work:

$> babel ./src/myclass.js --out-file ./out/app.js 

Alternatively, use a bundler like webpack, rollup, or browserify, with their respective babel plugin.

like image 181
sdgluck Avatar answered Oct 12 '22 14:10

sdgluck


The exact answer using babel with nodejs (and using CLI):

Install babel and preset

npm install babel-cli babel-preset-es2015 

Execute with npx

npx babel  ./src/myclass.js --out-file ./out/app.js --presets babel-preset-es2015 

Note

When babel-cli is globally installed could be not working. So this is my definitive solution

like image 34
Francesco Capodanno Avatar answered Oct 12 '22 12:10

Francesco Capodanno