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);
babel es6.js -o compiled.js
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": [] }
npm i --save-dev babel-preset-es2015
{ "presets": ["es2015"], "plugins": [] }
"use strict"; var num = [1, 2, 3, 4]; var plusDouble = num.map(function (item) { return item * 2; }); console.log(plusDouble);
object-rest-spread, similarly, we Use the command to install
npm i babel-plugin-transform-object-rest-spread --save-dev
{ "presets": ["es2015"], "plugins": ["transform-object-rest-spread"] }
let courses = { name: 'english', score: 90}; courses = { ...courses, comment: 'A'}; console.log(courses);
'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);
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!