1"""
2Rules for building typescript flatbuffers with Bazel.
3"""
4
5load("@aspect_rules_js//js:defs.bzl", "js_library")
6load(":build_defs.bzl", "flatbuffer_library_public")
7
8DEFAULT_FLATC_TS_ARGS = [
9 "--gen-object-api",
10 "--gen-mutable",
11 "--reflect-names",
12 "--gen-name-strings",
13 "--ts-flat-files",
14 "--keep-prefix",
15]
16
17def flatbuffer_ts_library(
18 name,
19 srcs,
20 compatible_with = None,
21 target_compatible_with = None,
22 deps = [],
23 include_paths = None,
24 flatc_args = DEFAULT_FLATC_TS_ARGS,
25 visibility = None,
26 restricted_to = None,
27 gen_reflections = False):
28 """Generates a ts_library rule for a given flatbuffer definition.
29
30 Args:
31 name: Name of the generated ts_library rule.
32 srcs: Source .fbs file(s).
33 deps: Other flatbuffer_ts_library's to depend on. Note that currently
34 you must specify all your transitive dependencies manually.
35 include_paths: Optional, list of paths the includes files can be found in.
36 flatc_args: Optional list of additional arguments to pass to flatc
37 (e.g. --gen-mutable).
38 visibility: The visibility of the generated cc_library. By default, use the
39 default visibility of the project.
40 compatible_with: Optional, The list of environments this rule can be built
41 for, in addition to default-supported environments.
42 restricted_to: Optional, The list of environments this rule can be built
43 for, instead of default-supported environments.
44 target_compatible_with: Optional, The list of target platform constraints
45 to use.
46 gen_reflections: Optional, if true this will generate the flatbuffer
47 reflection binaries for the schemas.
48 """
49 srcs_lib = "%s_srcs" % (name)
50 out_base = [s.replace(".fbs", "").split("/")[-1].split(":")[-1] for s in srcs]
51
52 if len(srcs) != 1:
53 fail("flatbuffer_ts_library only supports one .fbs file per target currently.")
54
55 outs = ["%s_generated.cjs" % s for s in out_base]
56 includes = [d + "_includes" for d in deps]
57 reflection_name = "%s_reflection" % name if gen_reflections else ""
58 flatbuffer_library_public(
59 name = srcs_lib,
60 srcs = srcs,
61 outs = outs,
62 language_flag = "--ts",
63 includes = includes,
64 include_paths = include_paths,
65 extra_env = "ESBUILD_BIN=$(ESBUILD_BIN)",
66 flatc_args = flatc_args + ["--filename-suffix _generated"],
67 compatible_with = compatible_with,
68 restricted_to = restricted_to,
69 reflection_name = reflection_name,
70 reflection_visibility = visibility,
71 target_compatible_with = target_compatible_with,
72 flatc_path = "@com_github_google_flatbuffers//ts:compile_flat_file",
73 toolchains = ["@aspect_rules_esbuild//esbuild:resolved_toolchain"],
74 tools = ["@aspect_rules_esbuild//esbuild:resolved_toolchain"],
75 )
76 js_library(
77 name = name,
78 visibility = visibility,
79 compatible_with = compatible_with,
80 restricted_to = restricted_to,
81 target_compatible_with = target_compatible_with,
82 srcs = outs,
83 )
84 native.filegroup(
85 name = "%s_includes" % (name),
86 srcs = srcs + includes,
87 compatible_with = compatible_with,
88 restricted_to = restricted_to,
89 visibility = visibility,
90 )
View as plain text