Home Web Front-end JS Tutorial Detailed explanation of the case of es6 file compilation by Babel (with code)

Detailed explanation of the case of es6 file compilation by Babel (with code)

May 31, 2018 pm 02:41 PM
babel Case compile

This time I will bring you a detailed explanation of the case of babel compiling es6 files (with code). What are the precautions for babel to compile es6 files. The following is a practical case, let's take a look.

1.babel

babel official website

2. Installation

npm i babel-cli -g
Copy after login

Use the above command to install babel, where i means install, -g means install globally

##3. Use

to create the file es6.js

let num = [1,2,3,4]; 
let plusDouble = num.map(item => item * 2); 
console.log(plusDouble);
Copy after login
and then use the command to compile:

babel es6.js -o compiled.js
Copy after login
Then the compiled file will appear in the current directory , In this way, we have completed the compilation process, but when we run the compiled file, an error will still be reported. In fact, the main reason is that the above compilation did not add constraints, that is, it did not tell Babel how to compile, then the following Let’s configure babel

4. Configuration

(1) Configure through the file

in the project directory Create the file .babelrc and write the following code in the file: Since babel is used in the form of a plug-in, install the plug-in by adding the object preset and plug-in

{ 
 "presets": [], 
 "plugins": [] 
}
Copy after login
in the following code. In the following plug-in Using, you can compile ES6 code into ES5 code:

npm i --save-dev babel-preset-es2015
Copy after login
(--save-dev in the code represents installation in local development dependencies)

Then modify the file in .babelrc to The following content:

{ 
 "presets": ["es2015"], 
 "plugins": [] 
}
Copy after login
At this point, we have completed the configuration. Run the compile command to get the following results:

"use strict";  
var num = [1, 2, 3, 4]; 
var plusDouble = num.map(function (item) { 
 return item * 2; 
}); 
console.log(plusDouble);
Copy after login
The results can be printed normally after running.

Now we can proceed Simple compilation, but there are still some restrictions on some new features in es7. In this way, we use plug-ins for compilation, as shown below. The object spreader plug-in

object-rest-spread, similarly, we Use the command to install

npm i babel-plugin-transform-object-rest-spread --save-dev
Copy after login
Similarly proceed to modify the plug-in

{ 
 "presets": ["es2015"], 
 "plugins": ["transform-object-rest-spread"] 
}
Copy after login
Then test through the code, write the following content in the code (...is a pre-conceived idea in ES7):

let courses = { name: 'english', score: 90}; 
courses = { ...courses, comment: 'A'}; 
console.log(courses);
Copy after login
The result after compilation is:

'use strict';  
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };  
var courses = { name: 'english', score: 90 }; 
courses = _extends({}, courses, { comment: 'A' }); 
console.log(courses);
Copy after login
Convert the object expander by adding the _extends method, and the running code can output the result normally

(2) By adding the _extends method in webpack

Configuration file Load configuration of other attributes

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use js to count the number of page tags

How to develop verification code passwords in WeChat mini-programs Input box function

The above is the detailed content of Detailed explanation of the case of es6 file compilation by Babel (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ compilation error: undeclared identifier, how to solve it? C++ compilation error: undeclared identifier, how to solve it? Aug 22, 2023 pm 03:34 PM

When programming in C++, we often encounter the problem of undeclared identifiers. This usually occurs when undefined variables, functions, or classes are used, causing the compiler to fail to recognize these identifiers, resulting in compilation errors. This article describes common causes of undeclared identifier problems and how to resolve them. Common Causes Undeclared identifier problems usually arise from the following reasons: Variables, functions, or classes are not declared correctly: You should declare variables, functions, or classes before using them. If the variable is not declared or function

Why does Linux need to compile source code? Why does Linux need to compile source code? Mar 17, 2023 am 10:21 AM

Reasons: 1. There are many versions of Linux, but each version uses different software or kernel versions, and the environment that the binary package depends on may not be able to run normally, so most software directly provides source code for compilation and installation. 2. Easy to customize to meet different needs. 3. Convenient for operation and maintenance and developer maintenance; source code can be packaged as binary, but packaging this software will require costly extra work, including maintenance, so if it is source code, the software manufacturer will maintain it directly.

Compilation and decompilation techniques in Java Compilation and decompilation techniques in Java Jun 09, 2023 am 09:43 AM

Java is a very popular programming language that is widely used to develop various types of software. In Java development, compilation and decompilation technology are very important links. Compilation technology is used to convert Java code into executable files, while decompilation technology allows one to convert executable files back into Java code. This article will introduce compilation and decompilation techniques in Java. 1. Compilation technology Compilation is the process of converting high-level language (such as Java) code into machine language. in Java

Why does my Go program take longer to compile? Why does my Go program take longer to compile? Jun 09, 2023 pm 06:00 PM

In recent years, Go language has become the choice of more and more developers. However, compared to other programming languages, the compilation speed of Go language is not fast enough. Many developers will encounter this problem when compiling Go programs: Why does my Go program take longer to compile? This article will explore this issue from several aspects. The compiler architecture of Go language The compiler architecture of Go language adopts a three-stage design, which are front-end, middle layer and back-end. The front-end is responsible for translating the source code into intermediate code in Go language, and the middle layer will

C++ compilation error: The function parameter list is too long, how to solve it? C++ compilation error: The function parameter list is too long, how to solve it? Aug 21, 2023 pm 11:19 PM

C++ compilation error: The function parameter list is too long, how to solve it? When writing programs in C++, you sometimes encounter such a compilation error: the function parameter list is too long. For C++ beginners, this may be a headache. Next, we’ll cover the causes and solutions to this problem. First, let's take a look at the basic rules of C++ function parameters. In C++, function parameters must be declared between the function name and the opening parenthesis. When you pass a function parameter, you tell the function what to do. These parameters can be any

Can go language be compiled? Can go language be compiled? Dec 09, 2022 pm 06:20 PM

The go language can be compiled. Go language is a compiled static language, a programming language that requires compilation to run. There are two commands for compiling Go language programs: 1. "go build" command, which can compile Go language program code into a binary executable file, but the binary file needs to be run manually; 2. "go run" command, The Go language program will be run directly after compilation. A temporary file will be generated during the compilation process, but an executable file will not be generated.

Apache PHP compilation and installation detailed steps Apache PHP compilation and installation detailed steps Mar 08, 2024 pm 01:15 PM

Apache and PHP are two tools commonly used in website development. Compiling and installing them allows us to configure and manage them more flexibly. The following will introduce the compilation and installation steps of Apache and PHP in detail, including specific code examples. Step 1: Download the Apache and PHP source code packages. First, we need to download the latest versions of the Apache and PHP source code packages. You can visit the Apache official website (https://httpd.apache.org) and the PHP official website (https:

How to compile and install Apache PHP in the directory How to compile and install Apache PHP in the directory Mar 09, 2024 am 09:27 AM

How to compile and install ApachePHP in the directory requires specific code examples. Apache and PHP are two important tools often used in web development. Their compilation and installation can help us better customize and manage the server environment. This article will introduce in detail how to compile and install ApachePHP in the directory, covering specific code examples and step instructions. Step 1: Preparation Before starting, make sure you have the necessary compilation tools and dependencies installed on your system. Normally, the following

See all articles