Skip to content

Commit 26f6698

Browse files
authored
feat(typescript): add ts_project rule (#1710)
This is a very thin layer on top of vanilla tsc
1 parent 7afaa48 commit 26f6698

33 files changed

Lines changed: 559 additions & 50 deletions

examples/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ example_integration_test(
6464
name = "examples_react_webpack",
6565
# TODO: add some tests in the example
6666
bazel_commands = ["build ..."],
67+
npm_packages = {
68+
"//packages/typescript:npm_package": "@bazel/typescript",
69+
},
6770
# TODO(alexeagle): somehow this is broken by the new node-patches based node_patches script
6871
# ERROR: D:/temp/tmp-6900sejcsrcttpdb/BUILD.bazel:37:1: output 'app.bundle.js' was not created
6972
tags = ["no-bazelci-windows"],

examples/react_webpack/BUILD.bazel

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@npm//http-server:index.bzl", "http_server")
22
load("@npm//sass:index.bzl", "sass")
3-
load("@npm//typescript:index.bzl", "tsc")
43
load("@npm//webpack-cli:index.bzl", webpack = "webpack_cli")
4+
load("@npm_bazel_typescript//:index.bzl", "ts_project")
55

66
sass(
77
name = "styles",
@@ -13,22 +13,8 @@ sass(
1313
data = ["styles.scss"],
1414
)
1515

16-
tsc(
17-
name = "compile",
18-
outs = ["index.js"],
19-
args = [
20-
"$(execpath index.tsx)",
21-
"$(execpath types.d.ts)",
22-
"--outDir",
23-
"$(RULEDIR)",
24-
"--lib",
25-
"es2015,dom",
26-
"--jsx",
27-
"react",
28-
],
29-
data = [
30-
"index.tsx",
31-
"types.d.ts",
16+
ts_project(
17+
deps = [
3218
"@npm//@types",
3319
"@npm//csstype",
3420
],

examples/react_webpack/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ yarn_install(
1919
package_json = "//:package.json",
2020
yarn_lock = "//:yarn.lock",
2121
)
22+
23+
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
24+
25+
install_bazel_dependencies()

examples/react_webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"@bazel/bazelisk": "^1.3.0",
55
"@bazel/buildifier": "^0.29.0",
66
"@bazel/ibazel": "^0.12.2",
7+
"@bazel/typescript": "^1.4.1",
78
"@types/react": "^16.9.5",
89
"@types/react-dom": "^16.9.1",
910
"css-loader": "^3.2.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"lib": ["ES2015", "DOM"]
5+
}
6+
}

packages/typescript/docs/install.md

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,33 @@
22

33
The TypeScript rules integrate the TypeScript compiler with Bazel.
44

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-
85
## Alternatives
96

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.
168

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.
1811

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.
2015

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.
2219
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:
2524

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.
2827

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)).
4632

4733
## Installation
4834

packages/typescript/src/index.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ Users should not load files under "/internal"
1919

2020
load("//internal:build_defs.bzl", _ts_library = "ts_library_macro")
2121
load("//internal:ts_config.bzl", _ts_config = "ts_config")
22+
load("//internal:ts_project.bzl", _ts_project = "ts_project_macro")
2223
load("//internal:ts_repositories.bzl", _ts_setup_workspace = "ts_setup_workspace")
2324
load("//internal/devserver:ts_devserver.bzl", _ts_devserver = "ts_devserver_macro")
2425

2526
ts_setup_workspace = _ts_setup_workspace
2627
ts_library = _ts_library
2728
ts_config = _ts_config
2829
ts_devserver = _ts_devserver
30+
ts_project = _ts_project
2931
# If adding rules here also add to index.docs.bzl

packages/typescript/src/index.docs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ So this is a copy of index.bzl with macro indirection removed.
2020

2121
load("//internal:build_defs.bzl", _ts_library = "ts_library")
2222
load("//internal:ts_config.bzl", _ts_config = "ts_config")
23+
load("//internal:ts_project.bzl", _ts_project = "ts_project_macro")
2324
load("//internal:ts_repositories.bzl", _ts_setup_workspace = "ts_setup_workspace")
2425
load("//internal/devserver:ts_devserver.bzl", _ts_devserver = "ts_devserver")
2526

2627
ts_setup_workspace = _ts_setup_workspace
2728
ts_library = _ts_library
2829
ts_config = _ts_config
30+
ts_project = _ts_project
2931
ts_devserver = _ts_devserver
3032
# DO NOT ADD MORE rules here unless they appear in the generated docsite.
3133
# Run yarn stardoc to re-generate the docsite.

packages/typescript/src/internal/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ filegroup(
4747
srcs = [
4848
"build_defs.bzl",
4949
"ts_config.bzl",
50+
"ts_project.bzl",
5051
"ts_repositories.bzl",
5152
"//internal/devserver:package_contents",
5253
],

0 commit comments

Comments
 (0)