Webpack packages ES6 and CommonJs mixed React
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.
Related Articles
Two ways to implement React's ref callback function
Publish Date:2025/03/02 Views:89 Category:React
-
Two ways to implement ref callback function in React
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.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:197 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during
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
React Getting Started - Building a React Operating Environment
Publish Date:2025/03/02 Views:62 Category:React
-
React, a front-end framework developed by Facebook, is very popular. There are two ways to run React, one is to use npm, and the other is not to use npm. These involve react-dom, babel, webpack, etc.