JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Webpack packages ES6 and CommonJs mixed React

Author:JIYIK Last Updated:2025/03/02 Views:

When learning React, I have always used Webpack (ebpack) for packaging. At the beginning, I had no problem using the basic configuration for the basic CommonJs ReactES6 syntax. However, when I recently learned about props in React...other, which is a feature of React ES7, the original basic configuration can no longer meet the requirements. The existing configuration must be upgraded.

Next, I will talk about the upgrade process of my React environment.

When we first use CommonJs syntax, we only need to use the most basic configuration:

$ npm install babel-core babel-loader --save-dev

After installing babel-core and babel-loader, let's write the Webpack configuration file. The default file name is webpack.config.js. Of course, you can use other file names (such as wpconfig.js), just add the --config parameter when using the webpack command:

$ webpack --config wpconfig.js

The configuration file content is as follows

module.exports = {
    entry: {
        module: './src',
    } ,
    output:{
        path: './builds',
        filename: 'onmpw.js',
    },
    module:{
        loaders:[
            {
                test: /\.jsx?$/,
                loader: 'babel',
             },
        ],
    }
};

If you want to use jsx in react code, you must install babel-preset-react and then modify the webpack configuration file

$ npm install babel-preset-react --save-dev

The following is part of the configuration file

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    query:{
        presets:['react']
    }
}

Next, if you want to use ES6 syntax in React, you need to make your environment support ES6 or ES2015

$ npm install babel-preset-es2015 --save-dev

The following is part of the configuration file

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    query:{
        presets:['react', 'es2015']
    }
}

Finally, if you want to make your environment support some experimental features of ES7, you can continue to upgrade as follows

$# npm install babel-preset-stage-0 --save-dev

The content of the configuration file is as follows:

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    query:{
        presets:['react', 'es2015', 'stage-0']
    }
}

In response to the above situations, here is a code snippet that covers these situations.

var FancyCheckbox = React.createClass({
    render: function() {
        var { checked, ...other } = this.props;
        var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
        // `other` 包含 { onClick: console.log } 但 checked 属性除外
        return (
            <div {...other} className={fancyClass} />
        );
    }
});
ReactDOM.render(
  <FancyCheckbox checked={true} onClick={console.log.bind(console)}>
    Hello world!
  </FancyCheckbox>,
  document.getElementById('example')
);

Finally, we need to pay attention. Babel cannot support the separate compilation of all ES6 codes. It needs the support of some runtime environment to achieve it. More typically, the new ES6 built-in methods, such as Set, Map and Promise, must be supported by polyfilled. And the implementation of Babel's parser also requires a series of auxiliary runtime environments. The method of installing polyfilled is as follows

$ npm install babel-polyfilled --save

At the same time, Babel compiles these small helper code directly into your code. This is fine for a single small file. But if you bundle this with Webpack, the duplicate code will result in a very large file. In view of this situation, it is more appropriate to use the babel-runtime package with the transform-runtime plugin instead of these helper tools.

$ npm install babel-runtime --save
$ npm install babel-plugin-transform-runtime --save-dev

Of course, after installing these, we also need to upgrade our Webpack configuration file. The code is as follows:

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    query:{
        plugins: ['transform-runtime'],
        presets: [ 'react', 'es2015', 'stage-0']
    }
}

Well, all of the above is the configuration of the environment required for several situations of packaging React with Webpack. I hope it will be helpful for everyone to learn React in the future.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React component refs detailed explanation

Publish Date:2025/03/02 Views:118 Category:React

React component reference ref can be said to be a component identifier. Ref is commonly used to make input get focus. Because render returns only virtual DOM, ref is used here.

Common terms used in React Virtual DOM

Publish Date:2025/03/02 Views:174 Category:React

In all the terms of React, there are five core types that we need to remember. These five types are ReactElement / ReactElement Factory ReactNode ReactComponent / ReactComponent Class

React Getting Started createClass Usage Instructions

Publish Date:2025/03/02 Views:50 Category:React

Before using React.createClass, lets look at the official explanation ReactClass createClass(object specification) Create a component class with a given specification (the specification is the object specification). This component implement

React.createElement method usage details

Publish Date:2025/03/02 Views:193 Category:React

The React.createElement method creates and returns a ReactElement element of a given type. The type parameter can be an html tag name string or a ReactClasss. This type parameter is required for createElement.

React Factory Method - Detailed Explanation of createFactory

Publish Date:2025/03/02 Views:88 Category:React

React.createFactory returns a function for generating ReactElement of a given type, similar to React.createElement. The type parameter can be an html tag name (for example: "div", "li", etc.) or a ReactClass object.

React Project Management

Publish Date:2025/03/02 Views:123 Category:React

In the article "Beginner's Guide to React - Building a React Runtime Environment", we only introduced how to build a React runtime environment, which is actually how to reference the React library. But I think it is still a bit confusing for

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial