Webpack using tutorial Five (Babel)

Source: Internet
Author: User

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)

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.