For details, please visit Babel’s Chinese website to describe the environment installation and basic use. Install Node.js 1, open the terminal, CD into an empty folder, create a Babel project, and enter the project information on the terminal if necessary

npm init
Copy the code

Then enter the information and press Enter. If you do not need to enter the project information, go to the following command:

npm init -y
Copy the code

2, then install Babel development environment (drop dev to production)

npm install --save-dev babel-cli
Copy the code

Install a dependency terminal for Babel:

npm i -D babel-preset-env
Copy the code

Install a property plug-in for the transform class

npm i -D babel-plugin-transform-class-properties
Copy the code

Create a configuration file on the terminal whose name cannot be changed

touch .babelrc
Copy the code

Babelrc fills in the configuration compatible with the latest browsers and plug-ins

{
    "presets": [["env",
                {
                    "targets":      {
                    "browsers": ["last 1 version"]}}]],"plugins": ["transform-class-properties"]}Copy the code

The devDependencies in package.json file describes the dependencies installed in the development environment. We need to add an entry to scripts in package.json:

"build": "babel entry.js -o index.js -w"
Copy the code

Output entry. Js code to index.js. -w is shorthand for automatic compilation. Create three files in the project:

Entry. js is used to write ES6 codes, and index.js is used to import the output code index.html entry file into the index.js file with script tags

Write some ES6 code in entry.js

class Car{
	static total_car = 0;
	color = '# 000';
	constructor(color){
		Car.total_car += 1;
		this.color = color;
	}
}

new Car();
new Car();
new Car();

console.log(Car.total_car);
Copy the code

And then it runs on the terminal

npm run build
Copy the code

Then you can see the converted code in index.js and then you can open the index.html in your browser and you can see the printed result