|
2 | 2 |
|
3 | 3 | The TypeScript rules integrate the TypeScript compiler with Bazel. |
4 | 4 |
|
5 | | -Looking for Karma rules `ts_web_test` and `karma_web_test`? |
6 | | -These are now documented in the README at https://npmjs.com/package/@bazel/karma |
7 | | - |
8 | 5 | ## Alternatives |
9 | 6 |
|
10 | | -This package provides Bazel wrappers around the TypeScript compiler, and are how we compile TS code at Google. |
11 | | - |
12 | | -These rules are opinionated, for example: |
13 | | - |
14 | | -- Your TS code must compile under the `--declaration` flag so that downstream libraries depend only on types, not implementation. This makes Bazel faster by avoiding cascading rebuilds in cases where the types aren't changed. |
15 | | -- We control the output format and module syntax so that downstream rules can rely on them. |
| 7 | +This package provides Bazel wrappers around the TypeScript compiler. |
16 | 8 |
|
17 | | -They are also fast and optimized: |
| 9 | +At a high level, there are two alternatives provided: `ts_project` and `ts_library`. |
| 10 | +This section describes the trade-offs between these rules. |
18 | 11 |
|
19 | | -- We keep a running TypeScript compile running as a daemon, using Bazel workers. This process avoids re-parse and re-JIT of the >1MB `typescript.js` and keeps cached bound ASTs for input files which saves time. |
| 12 | +`ts_project` simply runs `tsc --project`, with Bazel knowing which outputs to expect based on the TypeScript compiler options, and with interoperability with other TypeScript rules via a Bazel Provider (DeclarationInfo) that transmits the type information. |
| 13 | +It is intended as an easy on-boarding for existing TypeScript code and should be familiar if your background is in frontend ecosystem idioms. |
| 14 | +Any behavior of `ts_project` should be reproducible outside of Bazel, with a couple of caveats noted in the rule documentation below. |
20 | 15 |
|
21 | | -We understand this is a tradeoff. If you want to use the plain TypeScript compiler provided by the TS team at Microsoft, you can do this by calling its CLI directly. For example, |
| 16 | +> We used to recommend using the `tsc` rule directly from the `typescript` project, like |
| 17 | +> `load("@npm//typescript:index.bzl", "tsc")` |
| 18 | +> However `ts_project` is strictly better and should be used instead. |
22 | 19 |
|
23 | | -```python |
24 | | -load("@npm//typescript:index.bzl", "tsc") |
| 20 | +`ts_library` is an open-sourced version of the rule we use to compile TS code at Google. |
| 21 | +It should be familiar if your background is in Bazel idioms. |
| 22 | +It is very complex, involving code generation of the `tsconfig.json` file, a custom compiler binary, and a lot of extra features. |
| 23 | +It is also opinionated, and may not work with existing TypeScript code. For example: |
25 | 24 |
|
26 | | -srcs = glob(["*.ts"]) |
27 | | -deps = ["@npm//@types/node"] |
| 25 | +- Your TS code must compile under the `--declaration` flag so that downstream libraries depend only on types, not implementation. This makes Bazel faster by avoiding cascading rebuilds in cases where the types aren't changed. |
| 26 | +- We control the output format and module syntax so that downstream rules can rely on them. |
28 | 27 |
|
29 | | -tsc( |
30 | | - name = "compile", |
31 | | - data = srcs + deps, |
32 | | - outs = [s.replace(".ts", ext) for ext in [".js", ".d.ts"] for s in srcs], |
33 | | - args = [ |
34 | | - "--outDir", |
35 | | - "$(RULEDIR)", |
36 | | - "--lib", |
37 | | - "es2017,dom", |
38 | | - "--downlevelIteration", |
39 | | - "--declaration", |
40 | | - ] + [ |
41 | | - "$(location %s)" % s |
42 | | - for s in srcs |
43 | | - ], |
44 | | -) |
45 | | -``` |
| 28 | +On the other hand, `ts_library` is also fast and optimized. |
| 29 | +We keep a running TypeScript compile running as a daemon, using Bazel workers. |
| 30 | +This process avoids re-parse and re-JIT of the >1MB `typescript.js` and keeps cached bound ASTs for input files which saves time. |
| 31 | +We also produce JS code which can be loaded faster (using named AMD module format) and which can be consumed by the Closure Compiler (via integration with [tsickle](https://github.com/angular/tsickle)). |
46 | 32 |
|
47 | 33 | ## Installation |
48 | 34 |
|
|
0 commit comments