blob: 04323048d7e2b17971b95acd1dc3f200f486c34d [file] [log] [blame]
"""This module provides a wrapper around the ts_library rule from the rules_nodejs repository."""
load("@npm//@bazel/typescript:index.bzl", _ts_library = "ts_library")
def ts_library(name, srcs, deps = [], **kwargs):
"""Wraps rules_nodejs's ts_library rule to include ambient types declared in //tsconfig.json.
The ts_library[1] rule from the rules_nodejs repository ignores[2] any ambient types[3] declared
via the `{"compilerOptions": {"types": [...]}}` field[4] in //tsconfig.json. This causes
TypeScript files using said types to fail to compile (e.g. "TS2304: Cannot find name 'XXX'").
Code editors, however, would not report any such errors because they correctly interpret the
"types" field in //tsconfig.json.
The solution is to explicitly include the ambient types in the deps argument of the ts_library
rule, e.g.:
// tsconfig.json
"compilerOptions": {
"types": ["mocha", "node"], // Ambient type declarations.
// BUILD.bazel
name = "example",
srcs = ["example.ts"],
deps = [
"@types/mocha", # Ambient type declared in //tsconfig.json.
"@types/node", # Ambient type declared in //tsconfig.json.
... # Any other deps.
This wrapper around the ts_library rule eliminates said compilation errors by automatically
including as dependencies any ambient types declared in the //tsconfig.json file.
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 = [
name = name,
srcs = srcs,
deps = deps + [dep for dep in ambient_types if dep not in deps],