ES6 Babel is correctly installed in JavaScript, es6babel

Source: Internet
Author: User

ES6 Babel is correctly installed in JavaScript, es6babel

This document describes how to install Babel6.x ~ First, you can use Babel online conversion https://babeljs.io/repl/

Then go to the topic: Install Babel (command line environment, for Babel6.x)

1. First install babel-cli (used to use babel on the terminal)

Npm install-g babel-cli

2. then install the babel-preset-es2015 plug-in

Npm install -- save babel-preset-es2015

Note: Babel5 contains various conversion plug-ins by default, but Babel6.x related conversion plug-ins need to be downloaded by themselves, such as transform-es2015-arrow-functions, transform-es2015-classes, etc., and ES2015 preset contains all the plug-ins. If no plug-ins are installed, the command line conversion will not work!

The -- save parameter automatically updates the package. json file and writes it to the dependency.

3. Enter the following in the command line:

Babel es6.js -- presets es2015

Output:

 "use strict";  [1, 2, 3].map(function (x) {   return x * x;  });

Note: The following parameter -- presets es2015 indicates that this plug-in is used for compilation. If you do not write the conversion, it will be ineffective.

4. Plug-in configuration

It is very troublesome to write this parameter every time. You can create a configuration file. babelrc in the current directory.

However, in windows, you cannot right-click a file without a file name. You can use the cmd command line to create a file: Open cmd in the current folder and Type A command

Type nul>. babelrc

You can create a file. babelrc in the current directory, and then write the file:

 {   "presets": ['es2015'] } 

Then you can directly use babel es6.js in the command line for conversion without adding parameters indicating the plug-in used

In addition to creating a. babelrc file, you can also configure it in package. json by adding the following attributes:

"babel": {  "presets": ["es2015"]}

Appendix Common Babel commands:

1. Convert the es6.js file and output it in the program window of the current named row.

Babel es6.js

2. Convert es6.js and output it to the es5.js file (use-o or -- out-file)

Babel es6.js-o es5.js

Babel es6.js -- out-file es5.js

3. Monitor es6.js in real time and re-compile it as soon as it changes (use-w or -- watch)

Babel es6.js-w -- out-file es5.js
Babel es6.js -- watch -- out-file es5.js

4. Compile the entire src folder and output it to the lib folder (use-d or -- out-dir)

Babel src-d lib
Babel src -- out-dir lib

5. Compile the entire src folder and output it to a file.

Babel src -- out-file es5.js

6. directly enter the babel-node command to run ES6 Code directly in the command line.

Babel-node

The above section describes the correct installation process of ES6 Babel in JavaScript. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.