@aspect_rules_ts//ts:defs.bzl
Public API for TypeScript rules
The most commonly used is the ts_project macro which accepts TypeScript sources as
inputs and produces JavaScript or declaration (.d.ts) outputs.
Functions & Macros
ts_projectCompiles one TypeScript project using tsc --project.
This is a drop-in replacement for the tsc rule automatically generated for the "typescript"
package, typically loaded from @npm//typescript:package_json.bzl.
Unlike bare tsc, this rule understands the Bazel interop mechanism (Providers)
so that this rule works with others that produce or consume TypeScript typings (.d.ts files).
Some TypeScript options affect which files are emitted, and Bazel needs to predict these ahead-of-time.
As a result, several options from the tsconfig file must be mirrored as attributes to ts_project.
A validation action is run to help ensure that these are correctly mirrored.
See https://www.typescriptlang.org/tsconfig for a listing of the TypeScript options.
If you have problems getting your ts_project to work correctly, read the dedicated
troubleshooting guide.
Parameters
*name | a name for this target |
tsconfig | Label of the tsconfig.json file to use for the compilation. By default, if a "tsconfig.json" file is in the same folder with the ts_project rule, it is used. Instead of a label, you can pass a dictionary matching the JSON schema. See docs/tsconfig.md for detailed information. Default: None |
srcs | List of labels of TypeScript source files to be provided to the compiler. If absent, the default is set as follows:
Default: None |
args | List of strings of additional command-line arguments to pass to tsc. Default: [] |
data | Files needed at runtime by binaries or tests that transitively depend on this target. Default: [] |
deps | List of targets that produce TypeScript typings ( If this list contains linked npm packages, npm package store targets or other targets that provide NB: Linked npm package targets that are "dev" dependencies do not forward their underlying Default: [] |
assets | Files which are needed by a downstream build step such as a bundler. These files are not included as inputs to any actions spawned by A typical use is when your TypeScript code has an import that TS itself doesn't understand
and the type-checker allows this because you have an "ambient" global type declaration like
A bundler like webpack will expect to be able to resolve the Note that Default: [] |
extends | Label of the tsconfig file referenced in the Default: None |
allow_js | Whether TypeScript will read .js and .jsx files. Default: False |
isolated_typecheck | Whether to type-check asynchronously as a separate bazel action from transpilation. Any transpiling with Requires https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/#the---nocheck-option6 Default: False |
declaration | Whether the Default: False |
source_map | Whether the Default: False |
declaration_map | Whether the Default: False |
resolve_json_module | Boolean; specifies whether TypeScript will read .json files. Default: None |
preserve_jsx | Whether the Default: False |
composite | Whether the Default: False |
incremental | Whether the Default: False |
no_emit | Whether the Default: False |
emit_declaration_only | Whether the Default: False |
transpiler | A custom transpiler tool to run that produces the JavaScript outputs instead of Under Without It may also be any rule or macro with this signature: If JavaScript outputs are configured to not be emitted the custom transpiler will not be used, such as See docs/transpiler.md for more details. Default: None |
declaration_transpiler | A custom transpiler tool to run that produces the TypeScript declaration outputs instead of It may be any rule or macro with this signature: If TypeScript declaration outputs are configured to not be emitted the custom declaration transpiler will See docs/transpiler.md for more details. Default: None |
ts_build_info_file | The user-specified value of Default: None |
generate_trace | Whether to generate a trace file for TypeScript compiler performance analysis. Default: None |
tsc | Label of the TypeScript compiler binary to run. See examples of use in examples/custom_compiler Default: "@npm_typescript//:tsc" |
tsc_worker | Label of a custom TypeScript compiler binary which understands Bazel's persistent worker protocol. Default: "@npm_typescript//:tsc_worker" |
validate | Whether to check that the dependencies are valid and the tsconfig JSON settings match the attributes on this target. Default: True |
validator | Label of the tsconfig validator to run when Default: "@npm_typescript//:validator" |
declaration_dir | String specifying a subdirectory under the bazel-out folder where generated declaration Default: None |
out_dir | String specifying a subdirectory under the bazel-out folder where outputs are written. Note that Bazel always requires outputs be written under a subdirectory matching the input package, By default the out_dir is the package's folder under bazel-out. Default: None |
root_dir | String specifying a subdirectory under the input package which should be consider the Default: None |
supports_workers | Whether the "Persistent Worker" protocol is enabled. Worker mode can be enabled for all This is a "tri-state" attribute, accepting values
Default: -1 |
kwargs | passed through to underlying |
Rules
ts_configAllows a tsconfig.json file to extend another file.
Normally, you just give a single tsconfig.json file as the tsconfig attribute
of a ts_library or ts_project rule. However, if your tsconfig.json uses the extends
feature from TypeScript, then the Bazel implementation needs to know about that
extended configuration file as well, to pass them both to the TypeScript compiler.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
deps | list of labels | Additional tsconfig.json files referenced via extends Default: [] |
*src | label | The tsconfig.json file passed to the TypeScript compiler |
ts_project_ruleImplementation rule behind the ts_project macro.
Most users should use ts_project instead.
This skips conveniences like validation of the tsconfig attributes, default settings
for srcs and tsconfig, and pre-declaring output files.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
allow_js | boolean | https://www.typescriptlang.org/tsconfig#allowJs Default: False |
composite | boolean | https://www.typescriptlang.org/tsconfig#composite Default: False |
declaration | boolean | https://www.typescriptlang.org/tsconfig#declaration Default: False |
declaration_map | boolean | https://www.typescriptlang.org/tsconfig#declarationMap Default: False |
no_emit | boolean | https://www.typescriptlang.org/tsconfig#noEmit Default: False |
emit_declaration_only | boolean | https://www.typescriptlang.org/tsconfig#emitDeclarationOnly Default: False |
extends | label | https://www.typescriptlang.org/tsconfig#extends Default: None |
incremental | boolean | https://www.typescriptlang.org/tsconfig#incremental Default: False |
preserve_jsx | boolean | https://www.typescriptlang.org/tsconfig#jsx Default: False |
resolve_json_module | boolean | https://www.typescriptlang.org/tsconfig#resolveJsonModule Default: False |
source_map | boolean | https://www.typescriptlang.org/tsconfig#sourceMap Default: False |
ts_build_info_file | string | https://www.typescriptlang.org/tsconfig#tsBuildInfoFile Default: "" |
generate_trace | boolean | https://www.typescriptlang.org/tsconfig/#generateTrace Default: False |
assets | list of labels | Files which are needed by a downstream build step such as a bundler. See more details on the Default: [] |
args | list of strings | https://www.typescriptlang.org/docs/handbook/compiler-options.html Default: [] |
data | list of labels | Runtime dependencies to include in binaries/tests that depend on this target. Follows the same semantics as Default: [] |
declaration_dir | string | https://www.typescriptlang.org/tsconfig#declarationDir Default: "" |
deps | list of labels | List of targets that produce TypeScript typings ( Follows the same runfiles semantics as Default: [] |
out_dir | string | https://www.typescriptlang.org/tsconfig#outDir Default: "" |
root_dir | string | https://www.typescriptlang.org/tsconfig#rootDir Default: "" |
*srcs | list of labels | TypeScript source files |
supports_workers | integer | Whether to use a custom See the docs for Default: 0 |
is_typescript_5_or_greater | boolean | Whether TypeScript version is >= 5.0.0 Default: False |
transpile | integer | Whether tsc should be used to produce .js outputs Values are:
Default: -1 |
pretranspiled_js | label | Externally transpiled .js to be included in output providers Default: None |
pretranspiled_dts | label | Externally transpiled .d.ts to be included in output providers Default: None |
declaration_transpile | boolean | Whether tsc should be used to produce .d.ts outputs Default: False |
*tsc | label | TypeScript compiler binary |
*tsc_worker | label | TypeScript compiler worker binary |
*tsconfig | label | tsconfig.json file, see https://www.typescriptlang.org/tsconfig |
eslintconfig | label | .eslintrc file, or other filenames accepted by ESLint. Default: None |
isolated_typecheck | boolean | Whether type-checking should be a separate action. This allows the transpilation action to run without waiting for typings from dependencies. Requires a minimum version of typescript 5.6 for the noCheck Requires isolatedDeclarations Default: False |
validate | boolean | whether to add a Validation Action to verify the other attributes match Default: True |
build_progress_message | string | Custom progress message for the build action. Default: "Transpiling{emit_part}{type_check_part} TypeScript project {label} [tsc -p {tsconfig_path}]" |
isolated_typecheck_progress_message | string | Custom progress message for the isolated typecheck action. Default: "Type-checking TypeScript project {label} [tsc -p {tsconfig_path}]" |
*validator | label | |
buildinfo_out | label | Location in bazel-out where tsc will write a Default: None |
js_outs | list of labels | Locations in bazel-out where tsc will write Default: [] |
map_outs | list of labels | Locations in bazel-out where tsc will write Default: [] |
typing_maps_outs | list of labels | Locations in bazel-out where tsc will write Default: [] |
typings_outs | list of labels | Locations in bazel-out where tsc will write Default: [] |
resource_set | string | A predefined function used as the resource_set for actions. Used with --experimental_action_resource_set to reserve more RAM/CPU, preventing Bazel overscheduling resource-intensive actions. By default, Bazel allocates 1 CPU and 250M of RAM. Default: "default" |
Providers
TsConfigInfoProvides TypeScript configuration, in the form of a tsconfig.json file
along with any transitively referenced tsconfig.json files chained by the
"extends" feature
Fields
deps | all tsconfig.json files needed to configure TypeScript |
@aspect_rules_ts//ts:extensions.bzl
Define module extensions for using rules_ts with bzlmod.
See https://bazel.build/docs/bzlmod#extension-definition
@aspect_rules_ts//ts:proto.bzl
DEPRECATED: Protocol Buffers and gRPC
[!WARNING]
This API has been replaced by rules_js: https://github.com/aspect-build/rules_js/blob/main/js/proto.bzl
See updated documentation at https://docs.aspect.build/bazel/javascript/aspect_rules_js/js_proto
ts_proto_library uses the Connect library from bufbuild, and supports both Web and Node.js:
This Bazel integration follows the "Local Generation" mechanism described at
https://connectrpc.com/docs/web/generating-code#local-generation,
using packages such as @bufbuild/protoc-gen-es and @connectrpc/protoc-gen-connect-query
as plugins to protoc.
The aspect configure command
auto-generates ts_proto_library rules as of the 5.7.2 release.
It's also possible to compile this library into your Gazelle binary.
Note: this API surface is not included in defs.bzl to avoid eager loads of rules_proto for all rules_ts users.
Installation
If you install rules_ts in WORKSPACE, you'll need to install the deps of rules_proto, like this:
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")
rules_proto_dependencies()
If you use bzlmod/MODULE.bazel then no extra install is required.
Future work
- Allow users to choose other plugins. We intend to wait until http://github.com/bazelbuild/rules_proto supports protoc plugins.
- Allow users to control the output format. Currently it is hard-coded to
js+dts, and the JS output uses ES Modules.
Functions & Macros
ts_proto_libraryA macro to generate JavaScript code and TypeScript typings from .proto files.
Parameters
*name | name of resulting ts_proto_library target |
*node_modules | Label pointing to the linked node_modules target where @bufbuild/protoc-gen-es is linked, e.g. //:node_modules. |
*proto | the |
protoc_gen_options | dict of protoc_gen_es options. Default: {} |
gen_connect_es | [deprecated] whether protoc_gen_connect_es should generate grpc services, and therefore Default: False |
gen_connect_query | whether protoc_gen_connect_query should generate TanStack Query clients, and therefore Default: False |
gen_connect_query_service_mapping | mapping from source proto file to the named RPC services that file contains. For example, given Default: {} |
copy_files | whether to copy the resulting Default: True |
proto_srcs | the .proto files that are being generated. Repeats the Default: None |
files_to_copy | which files from the protoc output to copy. By default, performs a replacement on Default: None |
kwargs | additional named arguments to the ts_proto_library rule |