1""" 2Rules for building typescript flatbuffers with Bazel. 3""" 4 5load("@build_bazel_rules_nodejs//:index.bzl", "js_library") 6load("@npm//@bazel/typescript:index.bzl", "ts_project") 7load(":build_defs.bzl", "DEFAULT_INCLUDE_PATHS", "flatbuffer_library_public") 8 9DEFAULT_FLATC_TS_ARGS = [ 10 "--gen-object-api", 11 "--gen-mutable", 12 "--reflect-names", 13 "--gen-name-strings", 14 "--ts-flat-files", 15 "--keep-prefix", 16] 17 18def flatbuffer_ts_library( 19 name, 20 srcs, 21 compatible_with = None, 22 target_compatible_with = None, 23 deps = [], 24 include_paths = DEFAULT_INCLUDE_PATHS, 25 flatc_args = DEFAULT_FLATC_TS_ARGS, 26 visibility = None, 27 restricted_to = None, 28 include_reflection = True, 29 gen_reflections = False, 30 package_name = None): 31 """Generates a ts_library rule for a given flatbuffer definition. 32 33 Args: 34 name: Name of the generated ts_library rule. 35 srcs: Source .fbs file(s). 36 deps: Other flatbuffer_ts_library's to depend on. Note that currently 37 you must specify all your transitive dependencies manually. 38 include_paths: Optional, list of paths the includes files can be found in. 39 flatc_args: Optional list of additional arguments to pass to flatc 40 (e.g. --gen-mutable). 41 visibility: The visibility of the generated cc_library. By default, use the 42 default visibility of the project. 43 compatible_with: Optional, The list of environments this rule can be built 44 for, in addition to default-supported environments. 45 restricted_to: Optional, The list of environments this rule can be built 46 for, instead of default-supported environments. 47 target_compatible_with: Optional, The list of target platform constraints 48 to use. 49 include_reflection: Optional, Whether to depend on the flatbuffer 50 reflection library automatically. Only really relevant for the 51 target that builds the reflection library itself. 52 gen_reflections: Optional, if true this will generate the flatbuffer 53 reflection binaries for the schemas. 54 package_name: Optional, Package name to use for the generated code. 55 """ 56 srcs_lib = "%s_srcs" % (name) 57 out_base = [s.replace(".fbs", "").split("/")[-1].split(":")[-1] for s in srcs] 58 59 # Because of how we have to manage the bazel rules for typescript, 60 # reflection has to get special-cased to get imported when 61 # run within bazel. As such, generate the code using the _pregenerate 62 # suffix; then do a find/replace to fix-up all the reflection imports. 63 pre_outs = ["%s_pregenerated.ts" % s for s in out_base] 64 outs = ["%s_generated.ts" % s for s in out_base] 65 includes = [d + "_includes" for d in deps] 66 reflection_name = "%s_reflection" % name if gen_reflections else "" 67 flatbuffer_library_public( 68 name = srcs_lib, 69 srcs = srcs, 70 outs = pre_outs, 71 language_flag = "--ts", 72 includes = includes, 73 include_paths = include_paths, 74 flatc_args = flatc_args + ["--filename-suffix _pregenerated"], 75 compatible_with = compatible_with, 76 restricted_to = restricted_to, 77 reflection_name = reflection_name, 78 reflection_visibility = visibility, 79 target_compatible_with = target_compatible_with, 80 ) 81 fix_import_cmd = " ".join([ 82 "SRCS=($(SRCS));", 83 "OUTS=($(OUTS));", 84 "for i in $${!SRCS[@]}; do", 85 "sed \"s/'.*reflection\\/reflection_pregenerated/'flatbuffers_reflection\\/reflection_generated/; s/_pregenerated/_generated/\" $${SRCS[i]} > $${OUTS[i]};", 86 "done", 87 ]) 88 native.genrule( 89 name = name + "_reimporter", 90 srcs = pre_outs, 91 outs = outs, 92 cmd = fix_import_cmd, 93 ) 94 ts_project( 95 name = name + "_ts", 96 srcs = outs, 97 declaration = True, 98 visibility = visibility, 99 compatible_with = compatible_with, 100 restricted_to = restricted_to, 101 target_compatible_with = target_compatible_with, 102 tsconfig = { 103 "compilerOptions": { 104 "declaration": True, 105 "lib": [ 106 "ES2015", 107 "ES2020.BigInt", 108 "DOM", 109 ], 110 "module": "commonjs", 111 "moduleResolution": "node", 112 "noUnusedLocals": True, 113 "strict": True, 114 "types": ["node"], 115 }, 116 }, 117 deps = deps + ["@com_github_google_flatbuffers//ts:flatbuffers"] + (["@com_github_google_flatbuffers//reflection/ts:reflection_ts_fbs"] if include_reflection else []), 118 ) 119 js_library( 120 name = name, 121 visibility = visibility, 122 compatible_with = compatible_with, 123 restricted_to = restricted_to, 124 target_compatible_with = target_compatible_with, 125 deps = [name + "_ts"], 126 package_name = package_name, 127 ) 128 native.filegroup( 129 name = "%s_includes" % (name), 130 srcs = srcs + includes, 131 compatible_with = compatible_with, 132 restricted_to = restricted_to, 133 visibility = visibility, 134 ) 135