Skip to content

Commit

Permalink
Improve documentation for the ts_library#tsconfig attribute now tha…
Browse files Browse the repository at this point in the history
…t it has a default.

PiperOrigin-RevId: 205877360
  • Loading branch information
alexeagle committed Jul 24, 2018
1 parent f9f71cb commit a97c724
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ ts_library(
name = "my_code",
srcs = glob(["*.ts"]),
deps = ["//path/to/other:library"],
tsconfig = "//:tsconfig.json",
)
```

Expand All @@ -123,10 +122,17 @@ Then build it:
The resulting `.d.ts` file paths will be printed. Additionally, the `.js`
outputs from TypeScript will be written to disk, next to the `.d.ts` files <sup>1</sup>.

Note that the `tsconfig.json` file used for compilation should be the same one
your editor references, to keep consistent settings for the TypeScript compiler.
By default, `ts_library` uses the `tsconfig.json` file in the workspace root
directory. See the notes about the `tsconfig` attribute in the [ts_library API docs].

> <sup>1</sup> The
> [declarationDir](https://www.typescriptlang.org/docs/handbook/compiler-options.html)
> compiler option will be silently overwritten if present.
[ts_library API docs]: http://tsetse.info/api/build_defs.html#ts_library

### Serving TypeScript for development

There are two choices for development mode:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _ts_library_impl(ctx):
)
return ts_providers_dict_to_struct(ts_providers)

_ts_library = rule(
ts_library = rule(
_ts_library_impl,
attrs = dict(COMMON_ATTRIBUTES, **{
"srcs": attr.label_list(
Expand All @@ -177,7 +177,16 @@ _ts_library = rule(
"tsconfig": attr.label(
doc = """A tsconfig.json file containing settings for TypeScript compilation.
Note that some properties in the tsconfig are governed by Bazel and will be
overridden, such as `target` and `module`.""",
overridden, such as `target` and `module`.
The default value is set to `//:tsconfig.json` by a macro. This means you must
either:
- Have your `tsconfig.json` file in the workspace root directory
- Use an alias in the root BUILD.bazel file to point to the location of tsconfig:
`alias(name="tsconfig.json", actual="//path/to:tsconfig-something.json")`
- Give an explicit `tsconfig` attribute to all `ts_library` targets
""",
allow_files = True,
single_file = True,
),
Expand Down Expand Up @@ -219,7 +228,19 @@ TypeScript targets and JavaScript for the browser and Closure compiler.
"""

def ts_library_macro(tsconfig = None, **kwargs):
"""Wraps `ts_library` to set the default for the `tsconfig` attribute.
This must be a macro so that the string is converted to a label in the context of the
workspace that declares the `ts_library` target, rather than the workspace that defines
`ts_library`, or the workspace where the build is taking place.
This macro is re-exported as `ts_library` in the public API.
Args:
tsconfig: the label pointing to a tsconfig.json file
**kwargs: remaining args to pass to the ts_library rule
"""
if not tsconfig:
tsconfig = "//:tsconfig.json"

_ts_library(tsconfig = tsconfig, **kwargs)
ts_library(tsconfig = tsconfig, **kwargs)

0 comments on commit a97c724

Please sign in to comment.