Skip to content

Commit ffe4410

Browse files
committed
feat(intro): 重写Traceur的介绍
1 parent 8ad3c20 commit ffe4410

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

docs/intro.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Node.js是JavaScript语言的服务器运行环境,对ES6的支持度比浏览
4747
安装nvm需要打开命令行窗口,运行下面的命令。
4848

4949
```bash
50-
$ curl -o https://raw.githubusercontent.com/creationix/nvm/<version number>/install.sh | bash
50+
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/<version number>/install.sh | bash
5151
```
5252

5353
上面命令的`version number`处,需要用版本号替换。本节写作时的版本号是`v0.29.0`。该命令运行后,`nvm`会默认安装在用户主目录的`.nvm`子目录。
@@ -468,20 +468,20 @@ Google公司的[Traceur](https://github.com/google/traceur-compiler)转码器,
468468

469469
Traceur允许将ES6代码直接插入网页。首先,必须在网页头部加载Traceur库文件。
470470

471-
```javascript
472-
<!-- 加载Traceur编译器 -->
473-
<script src="http://google.github.io/traceur-compiler/bin/traceur.js"
474-
type="text/javascript"></script>
475-
<!-- 将Traceur编译器用于网页 -->
476-
<script src="http://google.github.io/traceur-compiler/src/bootstrap.js"
477-
type="text/javascript"></script>
478-
<!-- 打开实验选项,否则有些特性可能编译不成功 -->
479-
<script>
480-
traceur.options.experimental = true;
471+
```html
472+
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
473+
<script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
474+
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
475+
<script type="module">
476+
import './Greeter.js';
481477
</script>
482478
```
483479

484-
接下来,就可以把ES6代码放入上面这些代码的下方。
480+
上面代码中,一共有4个`script`标签。第一个是加载Traceur的库文件,第二个和第三个是将这个库文件用于浏览器环境,第四个则是加载用户脚本,这个脚本里面可以使用ES6代码。
481+
482+
注意,第四个`script`标签的`type`属性的值是`module`,而不是`text/javascript`。这是Traceur编译器识别ES6代码的标志,编译器会自动将所有`type=module`的代码编译为ES5,然后再交给浏览器执行。
483+
484+
除了引用外部ES6脚本,也可以直接在网页中放置ES6代码。
485485

486486
```javascript
487487
<script type="module">
@@ -501,29 +501,44 @@ Traceur允许将ES6代码直接插入网页。首先,必须在网页头部加
501501

502502
正常情况下,上面代码会在控制台打印出9。
503503

504-
注意,`script`标签的`type`属性的值是`module`,而不是`text/javascript`。这是Traceur编译器识别ES6代码的标识,编译器会自动将所有`type=module`的代码编译为ES5,然后再交给浏览器执行。
505-
506-
如果ES6代码是一个外部文件,也可以用`script`标签插入网页。
504+
如果想对Traceur的行为有精确控制,可以采用下面参数配置的写法。
507505

508506
```javascript
509-
<script type="module" src="calc.js" >
507+
<script>
508+
// Create the System object
509+
window.System = new traceur.runtime.BrowserTraceurLoader();
510+
// Set some experimental options
511+
var metadata = {
512+
traceurOptions: {
513+
experimental: true,
514+
properTailCalls: true,
515+
symbols: true,
516+
arrayComprehension: true,
517+
asyncFunctions: true,
518+
asyncGenerators: exponentiation,
519+
forOn: true,
520+
generatorComprehension: true
521+
}
522+
};
523+
// Load your module
524+
System.import('./myModule.js', {metadata: metadata}).catch(function(ex) {
525+
console.error('Import failed', ex.stack || ex);
526+
});
510527
</script>
511528
```
512529

530+
上面代码中,首先生成Traceur的全局对象`window.System`,然后`System.import`方法可以用来加载ES6模块。加载的时候,需要传入一个配置对象`metadata`,该对象的`traceurOptions`属性可以配置支持ES6功能。如果设为`experimental: true`,就表示除了ES6以外,还支持一些实验性的新功能。
531+
513532
### 在线转换
514533

515534
Traceur也提供一个[在线编译器](http://google.github.io/traceur-compiler/demo/repl.html),可以在线将ES6代码转为ES5代码。转换后的代码,可以直接作为ES5代码插入网页运行。
516535

517536
上面的例子转为ES5代码运行,就是下面这个样子。
518537

519538
```javascript
520-
<script src="http://google.github.io/traceur-compiler/bin/traceur.js"
521-
type="text/javascript"></script>
522-
<script src="http://google.github.io/traceur-compiler/src/bootstrap.js"
523-
type="text/javascript"></script>
524-
<script>
525-
traceur.options.experimental = true;
526-
</script>
539+
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
540+
<script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
541+
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
527542
<script>
528543
$traceurRuntime.ModuleStore.getAnonymousModule(function() {
529544
"use strict";
@@ -545,23 +560,23 @@ $traceurRuntime.ModuleStore.getAnonymousModule(function() {
545560

546561
### 命令行转换
547562

548-
作为命令行工具使用时,Traceur是一个Node.js的模块,首先需要用npm安装
563+
作为命令行工具使用时,Traceur是一个Node的模块,首先需要用Npm安装
549564

550565
```bash
551566
$ npm install -g traceur
552567
```
553568

554-
安装成功后,就可以在命令行下使用traceur了
569+
安装成功后,就可以在命令行下使用Traceur了
555570

556-
traceur直接运行es6脚本文件,会在标准输出显示运行结果,以前面的`calc.js`为例。
571+
Traceur直接运行es6脚本文件,会在标准输出显示运行结果,以前面的`calc.js`为例。
557572

558573
```bash
559574
$ traceur calc.js
560575
Calc constructor
561576
9
562577
```
563578

564-
如果要将ES6脚本转为ES5保存,要采用下面的写法
579+
如果要将ES6脚本转为ES5保存,要采用下面的写法
565580

566581
```bash
567582
$ traceur --script calc.es6.js --out calc.es5.js
@@ -575,7 +590,7 @@ $ traceur --script calc.es6.js --out calc.es5.js
575590
$ traceur --script calc.es6.js --out calc.es5.js --experimental
576591
```
577592

578-
命令行下转换的文件,就可以放到浏览器中运行
593+
命令行下转换生成的文件,就可以直接放到浏览器中运行
579594

580595
### Node.js环境的用法
581596

0 commit comments

Comments
 (0)