File tree 2 files changed +107
-2
lines changed
2 files changed +107
-2
lines changed Original file line number Diff line number Diff line change 10
10
* [ Nginx代理] ( ./Nginx/Nginx代理.md )
11
11
* [ Linux安装Nginx] ( ./Nginx/linux安装Nginx.md )
12
12
* [ ssl证书结合Nginx搭建https站点(免费证书)] ( ./Nginx/ssl证书结合Nginx搭建https站点(免费证书).md )
13
-
13
+ # JS
14
+ * [ 使用babel把es6转成es5] ( ./js/使用babel把es6转成es5.md )
14
15
15
16
# git
16
17
* [ git基础配置] ( ./Git/git配置ssh.md )
17
18
# node
18
19
* [ nvm安装node] ( ./Node/nvm安装node.md )
19
- # python
20
+ # python
20
21
* [ python控制台输出颜色字体] ( ./python控制台输出颜色.md )
21
22
22
23
Original file line number Diff line number Diff line change
1
+
2
+ # Babel 是什么
3
+
4
+ Babel 是一个工具链,主要用于将 ECMAScript 2015+ 版本的代码转换为向后兼容的 JavaScript 语法,以便能够运行在当前和旧版本的浏览器或其他环境中。
5
+
6
+ # Babel能用在哪里
7
+
8
+ ## NodeJs
9
+
10
+ ### 使用 ` npm ` 初始化项目
11
+ ``` shell script
12
+ $ mkdir es6-to-es5
13
+ $ npm init -y
14
+
15
+ ```
16
+
17
+ ### 安装Babel
18
+ ``` shell script
19
+ $ npm install --save-dev @babel/core @babel/cli
20
+ ```
21
+
22
+ 安装完成之后, 你的 ` package.json ` 会加入这些代码
23
+
24
+
25
+ ``` json5
26
+ " devDependencies" : {
27
+ + " @babel/cli" : " ^7.12.8" ,
28
+ + " @babel/core" : " ^7.12.9"
29
+ }
30
+ ```
31
+
32
+ 现在把 ` Babel ` 的运行命令放入到 ` npm ` 脚本中, 也是在 ` package.json ` 中
33
+
34
+
35
+ ``` json5
36
+ " scripts" : {
37
+ + " build" : " babel src -d lib"
38
+
39
+ }
40
+ ```
41
+
42
+ ### 创建.babelrc配置文件
43
+
44
+ ``` shell script
45
+ $ npm install @babel/preset-env --save-dev
46
+
47
+ ```
48
+ 创建` babel.config.json ` 文件,写入以下内容
49
+ ``` json
50
+ {
51
+ "presets" : [" @babel/preset-env" ]
52
+ }
53
+ ```
54
+
55
+ 环境都配置完成, 下面开始正式写代码了
56
+
57
+ ### 写一段ES6代码
58
+
59
+ 创建 ` /src/index.js `
60
+
61
+ ``` shell script
62
+ $ mkdir src
63
+ $ touch src/index.js
64
+
65
+ ```
66
+ 写入一个简单的箭头函数
67
+ ``` javascript
68
+ let sayHello = () => {
69
+ console .log (' hello xiaotaideng' )
70
+ }
71
+
72
+ sayHello ()
73
+
74
+ ```
75
+
76
+ 现在运行刚刚写好的启动脚本
77
+
78
+ ``` shell script
79
+ $ npm run build
80
+
81
+ ```
82
+ 完成之后可以看到目录中新增了一个` build ` 文件夹,打开里面的` index.js ` 它的内容是这样的
83
+
84
+ ``` javascript
85
+ " use strict" ;
86
+
87
+ var sayHello = function sayHello () {
88
+ console .log (' hello xiaotaideng' );
89
+ };
90
+
91
+ sayHello ();
92
+ ```
93
+
94
+ 现在执行下面的命令
95
+
96
+ ``` shell
97
+ $ node build/index.js
98
+ ```
99
+
100
+ 可以正常的输出,到现在好像已经可以正常的使用了
101
+
102
+ [ 点击] ( https://github.com/lizeze/es6-to-es5 ) 获取源码
103
+
104
+
You can’t perform that action at this time.
0 commit comments