


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
Use the above command to install babel, where i means install, -g means install globally
##3. Use
to create the file es6.jslet num = [1,2,3,4]; let plusDouble = num.map(item => item * 2); console.log(plusDouble);and then use the command to compile:
babel es6.js -o compiled.jsThen 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": [] }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(--save-dev in the code represents installation in local development dependencies)Then modify the file in .babelrc to The following content:
{ "presets": ["es2015"], "plugins": [] }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);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-devSimilarly proceed to modify the plug-in
{ "presets": ["es2015"], "plugins": ["transform-object-rest-spread"] }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);The result after compilation is:
'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i Convert the object expander by adding the _extends method, and the running code can output the result normally<p style="text-align: left;"></p>(2) By adding the _extends method in webpack<p style="text-align: left;">Configuration file<a href="http://www.php.cn/code/10545.html" target="_blank"> Load configuration of other </a> attributes<a href="http://www.php.cn/wiki/169.html" target="_blank"></a></p> 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! <p></p>Recommended reading: <p></p><p>How to use js to count the number of page tags<a href="http://www.php.cn/xiaochengxu-399476.html" target="_blank"></a><br></p><p>How to develop verification code passwords in WeChat mini-programs Input box function<a href="http://www.php.cn/xiaochengxu-399477.html" target="_blank"></a><br></p>
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!

在使用C++进行编程时,经常会遇到未声明的标识符这个问题。这种情况通常发生在使用了未定义的变量、函数或类时,导致编译器无法识别这些标识符,进而产生编译错误。本文将介绍导致未声明的标识符问题的常见原因以及如何解决这个问题。常见原因未声明的标识符问题通常由以下几种原因导致:变量、函数或类未被正确声明:在使用变量、函数或类之前,应该先声明它们。如果变量未被声明或函

近年来,Go语言已经成为了越来越多开发者的选择。但是,相比其他编程语言而言,Go语言的编译速度却不够快。很多开发者在编译Go程序时都会遇到这样的问题:为什么我的Go程序需要更长时间来编译?本文将会从几个方面探讨这个问题。Go语言的编译器架构Go语言的编译器架构采用的是三阶段设计,分别是前端、中间层和后端。前端负责将源代码翻译成Go语言的中间代码,中间层则将中

Java是一种非常流行的编程语言,广泛应用于开发各种类型的软件。在Java开发中,编译和反编译技术是非常重要的环节。编译技术用于将Java代码转换成可执行文件,而反编译技术则允许人们将可执行文件重新转换回Java代码。本文将介绍Java中的编译和反编译技术。一、编译技术编译是将高级语言(如Java)代码转换为机器语言的过程。在Java

原因:1、Linux发型版本众多,但是每个版本采用的软件或者内核版本都不一样,而二进制包所依赖的环境不一定能够正常运行,所以大部分软件直接提供源码进行编译安装。2、方便定制,满足不同的需求。3、方便运维、开发人员维护;源码是可以打包二进制的,但是对于这个软件的打包都会有一份代价不小的额外工作,包括维护,所以如果是源码的话,软件产商会直接维护。

C++编译错误:函数参数列表太长,应该怎么解决?在使用C++编写程序时,有时候会遇到这样的编译错误:函数参数列表太长。对于C++初学者来说,这可能是一个很头疼的问题。接下来,我们将介绍这个问题的原因和解决方法。首先,让我们来看一下C++函数参数的基本规定。在C++中,函数参数必须在函数名和左括号之间声明。当你传递函数参数时,告诉函数要做什么。这些参数可以是任

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

编译go语言程序的方法:1、在命令行工具中执行“go build fileName”命令,会将程序代码编译成二进制的可执行文件;2、在命令行工具中执行“go run fileName”命令,它会在编译后直接运行Go语言程序。

C++编译错误:参数数量错误,该怎么修改?在编写C++程序时,我们经常会遇到各种编译错误。其中一个常见的错误就是参数数量错误。当我们编写函数时,如果函数的参数数量和类型与调用该函数时提供的参数不匹配,就会导致编译错误。在C++中,编译器会在编译时检查函数参数的数量和类型,因此,我们需要确保函数参数的匹配性以避免编译错误。那么,当我们遇到C++编译错误:参数数


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
