| """This module provides a wrapper around the ts_project rule from the rules_ts repository.""" |
| |
| load("@aspect_rules_swc//swc:defs.bzl", "swc") |
| load("@aspect_rules_ts//ts:defs.bzl", "ts_project") |
| load("@bazel_skylib//lib:partial.bzl", "partial") |
| |
| def ts_library(name, srcs, deps = [], **kwargs): |
| """Wraps rules_ts's ts_project rule with common settings for our code. |
| |
| This macro ensures all ts_project[1] rules use our common //tsconfig.json file, and prevents |
| errors such as "error TS2688: Cannot find type definition file for 'mocha'", which arise when |
| the tsconfig.json file declares ambient modules[2] via compilerOptions.types[3], but we forget |
| to provide them to the ts_project rule via the deps argument. This macro simply adds those |
| dependencies for us. |
| |
| This macro is called ts_library, as opposed to ts_project, for consistency with our other |
| *_library rules, and also because it used to be a wrapper around the now-deprecated ts_library |
| rule from the rules_nodejs repository[4]. |
| |
| [1] https://docs.aspect.build/rulesets/aspect_rules_ts/docs/rules#ts_project |
| [2] https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules |
| [3] https://www.typescriptlang.org/tsconfig#types |
| [4] https://bazelbuild.github.io/rules_nodejs/TypeScript.html#option-4-ts_library |
| |
| Args: |
| name: The name of the target. |
| srcs: A list of TypeScript source files. |
| deps: A list of TypeScript dependencies. |
| **kwargs: Any other arguments to pass to the ts_library rule. |
| """ |
| |
| # Keep in sync with the "types" field in //tsconfig.json. |
| ambient_types = [ |
| "//:node_modules/@types/mocha", |
| "//:node_modules/@types/node", |
| ] |
| |
| # This produces: |
| # [name] - the default target which can be included in the deps of downstream rules. |
| # It does not do type-checking. |
| # [name]_types - provides typings (.d.ts files) as the default outputs. |
| # [name]_typecheck - provides default outputs asserting type-checking has been run. |
| # Building this target always causes the typechecker to run. |
| # [name]_typecheck_test - a build_test target which simply depends on the [name]_typecheck |
| # target. This ensures that typechecking will be run under bazel test with |
| # --build_tests_only. |
| # See https://github.com/aspect-build/rules_ts/blob/main/docs/transpiler.md#macro-expansion |
| # for more details. |
| ts_project( |
| name = name, |
| srcs = srcs, |
| deps = deps + [dep for dep in ambient_types if dep not in deps], |
| # Pinpoint Web UI provides it's own `tsconfig`. |
| tsconfig = kwargs.pop("tsconfig", "//:ts_config"), |
| declaration = True, |
| transpiler = partial.make(swc, swcrc = "//:.swcrc"), |
| allow_js = True, |
| **kwargs |
| ) |