Babel is a JavaScript compilation and tools platform, using Babel we can: use the new version of JavaScript (es6/es2015,es7/es2016), although some browsers do not fully support the new features ; Use JavaScript language extensions, such as react JSX. Babel is a standalone tool that can be used with WEBPACK. The Babel has been modularized and distributed in different NPM modules, where the core functions are available in the Babel-core Module. Other parts are downloaded according to the needs of the user: if you want to use with webpack, you need to install the Babel-loader module, if you want to use the ES6 feature, you need to install babel-preset-2015, if you want to use react JSX, Then you need to install Babel-preset-react. There are, of course, some other configuration modules, which are not all listed Here. Next we look at a simple react example (source download).
1. Install the necessary NPM module
Install babel-core babel-loader babel-preset-es2015 babel-preset-install react react- Install Json-loader
2. Code files
// greeter.jsImport React, {Component} from ' React './config.json 'class Greeter extends Component { render () { return ( <div>{config.greetText}</div> ); default Greeter // main.jsimport React from ' React 'react-dom './greeter '; render (< Greeter/>, document.getElementById (' Root ');
3. Set Webpack file
Module.exports ={devtool:' Eval-source-map ', entry: __dirname+ "/app/main.js", output: {path: __dirname+ "/public", Filename:"bundle.js"}, module: {loaders: [{test: /\.json$/, loader: "json" }, {test: /\.js$/ , exclude: /node_modules/, loader: ' Babel ', query: { presets: [' es2015 ', ' react ']}} ]}, devserver: {contentbase:"./public", Colors:true, Historyapifallback:true, Inline:true }};
Run the Webpack command, and then open the index.html file. SOURCE Download.
Webpack using tutorial Five (Babel)