Skip to content

Commit 1f09371

Browse files
committed
Add documentation for environment variables to configure watching of files and directories. PR792
1 parent 3500a1c commit 1f09371

5 files changed

Lines changed: 43 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ TypeScript是JavaScript的超集并且能够编译输出为纯粹的JavaScript.
5959
* [tsconfig.json](./doc/handbook/tsconfig.json.md)
6060
* [NPM包的类型](./doc/handbook/Typings%20for%20NPM%20Packages.md)
6161
* [编译选项](./doc/handbook/Compiler%20Options.md)
62+
* [配置 Watch](./doc/handbook/Configuring%20Watch.md)
6263
* [在MSBuild里使用编译选项](./doc/handbook/Compiler%20Options%20in%20MSBuild.md)
6364
* [与其它构建工具整合](./doc/handbook/Integrating%20with%20Build%20Tools.md)
6465
* [使用TypeScript的每日构建版本](./doc/handbook/Nightly%20Builds.md)

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [tsconfig.json](./doc/handbook/tsconfig.json.md)
4646
* [NPM包的类型](./doc/handbook/Typings for NPM Packages.md)
4747
* [编译选项](./doc/handbook/Compiler Options.md)
48+
* [配置 Watch](./doc/handbook/Configuring Watch.md)
4849
* [在MSBuild里使用编译选项](./doc/handbook/Compiler Options in MSBuild.md)
4950
* [与其它构建工具整合](./doc/handbook/Integrating with Build Tools.md)
5051
* [使用TypeScript的每日构建版本](./doc/handbook/Nightly Builds.md)

doc/handbook/Compiler Options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
`--types` | `string[]`| | 要包含的类型声明文件名列表。查看[@types,--typeRoots和--types](./tsconfig.json.md#types-typeroots-and-types)章节了解详细信息。
8080
`--typeRoots` | `string[]`| | 要包含的类型声明文件路径列表。查看[@types,--typeRoots和--types](./tsconfig.json.md#types-typeroots-and-types)章节了解详细信息。
8181
`--version`<br/>`-v` | | | 打印编译器版本号。
82-
`--watch`<br/>`-w` | | | 在监视模式下运行编译器。会监视输出文件,在它们改变时重新编译。
82+
`--watch`<br/>`-w` | | | 在监视模式下运行编译器。会监视输出文件,在它们改变时重新编译。监视文件和目录的具体实现可以通过环境变量进行配置。详情请看[配置 Watch](./Configuring%20Watch.md)
8383

8484
* <sup>[1]</sup> 这些选项是试验性的。
8585
* <sup>[2]</sup> 这些选项只能在`tsconfig.json`里使用,不能在命令行使用。

doc/handbook/Configuring Watch.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
编译器支持使用环境变量配置如何监视文件和目录的变化。
2+
3+
## 使用`TSC_WATCHFILE`环境变量来配置文件监视
4+
5+
选项 | 描述
6+
-----------------------------------------------|----------------------------------------------------------------------
7+
`PriorityPollingInterval` | 使用`fs.watchFile`但针对源码文件,配置文件和消失的文件使用不同的轮询间隔
8+
`DynamicPriorityPolling` | 使用动态队列,对经常被修改的文件使用较短的轮询间隔,对未修改的文件使用较长的轮询间隔
9+
`UseFsEvents` | 使用 `fs.watch`,它使用文件系统事件(但在不同的系统上可能不一定准确)来查询文件的修改/创建/删除。注意少数的系统如Linux,对监视者的数量有限制,如果使用`fs.watch`创建监视失败那么将通过`fs.watchFile`来创建监视
10+
`UseFsEventsWithFallbackDynamicPolling` | 此选项与`UseFsEvents`类似,只不过当使用`fs.watch`创建监视失败后,回退到使用动态轮询队列进行监视(如`DynamicPriorityPolling`介绍的那样)
11+
`UseFsEventsOnParentDirectory` | 此选项通过`fs.watch`(使用系统文件事件)监视文件的父目录,因此CPU占用率低但也会降低精度
12+
默认 (无指定值) | 如果环境变量`TSC_NONPOLLING_WATCHER`设置为`true`,监视文件的父目录(如同`UseFsEventsOnParentDirectory`)。否则,使用`fs.watchFile`监视文件,超时时间为`250ms`
13+
14+
## 使用`TSC_WATCHDIRECTORY`环境变量来配置目录监视
15+
16+
在那些Nodejs原生就不支持递归监视目录的平台上,我们会根据`TSC_WATCHDIRECTORY`的不同选项递归地创建对子目录的监视。 注意在那些原生就支持递归监视目录的平台上(如Windows),这个环境变量会被忽略。
17+
18+
选项 | 描述
19+
-----------------------------------------------|----------------------------------------------------------------------
20+
`RecursiveDirectoryUsingFsWatchFile` | 使用`fs.watchFile`监视目录和子目录,它是一个轮询监视(消耗CPU周期)
21+
`RecursiveDirectoryUsingDynamicPriorityPolling`| 使用动态轮询队列来获取目录与其子目录的改变
22+
默认 (无指定值) | 使用`fs.watch`来监视目录及其子目录
23+
24+
## 背景
25+
26+
在编译器中`--watch`的实现依赖于Nodejs提供的`fs.watch``fs.watchFile`,两者各有优缺点。
27+
28+
`fs.watch`使用文件系统事件通知文件及目录的变化。
29+
但是它依赖于操作系统,且事件通知并不完全可靠,在很多操作系统上的行为难以预料。
30+
还可能会有创建监视个数的限制,如Linux系统,在包含大量文件的程序中监视器个数很快被耗尽。
31+
但也正是因为它使用文件系统事件,不需要占用过多的CPU周期。
32+
典型地,编译器使用`fs.watch`来监视目录(比如配置文件里声明的源码目录,无法进行模块解析的目录)。
33+
这样就可以处理改动通知不准确的问题。
34+
但递归地监视仅在Windows和OSX系统上支持。
35+
这就意味着在其它系统上要使用替代方案。
36+
37+
`fs.watchFile`使用轮询,因此涉及到CPU周期。
38+
但是这是最可靠的获取文件/目录状态的机制。
39+
典型地,编译器使用`fs.watchFile`监视源文件,配置文件和消失的文件(失去文件引用),这意味着对CPU的使用依赖于程序里文件的数量。

preface.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ TypeScript目前还在积极的开发完善之中,不断地会有新的特性
6767
* [tsconfig.json](./doc/handbook/tsconfig.json.html)
6868
* [NPM包的类型](./doc/handbook/Typings for NPM Packages.html)
6969
* [编译选项](./doc/handbook/Compiler Options.html)
70+
* [配置 Watch](./doc/handbook/Configuring Watch.html)
7071
* [在MSBuild里使用编译选项](./doc/handbook/Compiler Options in MSBuild.html)
7172
* [与其它构建工具整合](./doc/handbook/Integrating with Build Tools.html)
7273
* [使用TypeScript的每日构建版本](./doc/handbook/Nightly Builds.html)

0 commit comments

Comments
 (0)