ES6 Babel Compilation

Source: Internet
Author: User

This article mainly refers to Nanyi's ES6 introduction, in order to improve their own writing an essay.

The original address please poke here ECMAScript 6 Getting Started

ECMAScript 6 is the next generation of JavaScript language standards. Since the current version of ES6 was released in 2015, it is also called ECMAScript 2015.

However, the browser is not fully compatible with ES6 and needs to be Babel compiled.

Babel is a widely used ES6 transcoding device that converts ES6 code into ES5 code, which can be executed in an existing environment.

Node installation Babel;

--save-dev babel-preset-es2015
The . BABELRC file is in the directory.

This file is used to set up transcoding rules and plugins, the basic format is as follows.

{  "presets": [], "plugins": []}

presetsThe field sets the transcoding rules, which are officially provided by the following rule set, which you can install as needed.

# ES2015 transcoding rules $ NPM Install--save-dev Babel-preset-es2015# react transcoding rules $ NPM Install--save-dev Babel-preset-react# ES7 transcoding rules for different stages of grammar proposals (total 4 stages), choose a $ NPM install --save-dev Babel-preset-stage-0$ npm install --save-dev Babel-preset-stage-1$ npm install --save-dev babel< Span class= "token operator" >-preset-stage-2$ NPM install Span class= "token operator" >--save-dev Babel-preset< Span class= "token operator" >-stage-3         

Then, add these rules .babelrc .

  {    "presets": [ "es2015", "react", "stage-2" ], "plugins": [] }
Command line transcoding BABEL-CLI

The Babel provides babel-cli tools for command line transcoding.

$ npm install --global Babel-cli //Global installation  

$ npm install --save-dev Babel-cli //Install
and then initialize the project
$NPM init
Configure some basic parameters.
{  // ...  " Devdependencies ": {  // development dependent    " babel-cli ":" ^6.0.0 "  },  " Scripts ": {    "build": "Babel src-d Lib"  // src below the js file, transcode to lib file.   },}

然后执行  $ npm run build
< Span class= "token operator" >  
or execute a commonly used command line.
 # transcoding results output to standard output $ babel example.js# transcoding results write to a file #  -- out-file or-o parameter specifies the output file  $ babel example.js --  out-file compiled.js  # or Babel Example.js  - o compiled.js# entire Directory transcoding #  -- out-dir or-d parameter specifies output directory  $ babel src -- out-dir Lib  # or $ babel src  - d lib#  - s parameter generates a source map file $ babel src  - D lib - s 
Babel-register

babel-registerThe module require overrides the command and adds a hook to it. Thereafter, whenever a require file .js with the .es load, .jsx , and .es6 suffix names is used, the Babel is first used for transcoding.

--save-dev babel-register

使用时,必须首先加载babel-register

Require ("Babel-register"); require ("./index.js");
Then, you don't need to  index.js  manually transcode the code.

It is babel-register important to note that only require the file that is loaded by the command is transcoded, not the current file. In addition, because it is real-time transcoding, it is only suitable for use in the development environment.












ES6 Babel Compilation

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.