Skip to content
File

Blob: build/wd_ts_bundle.bzl

3.3 KB
1load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
2load("@workerd//:build/lint_test.bzl", "lint_test")
3load("@workerd//:build/wd_js_bundle.bzl", "wd_js_bundle")
4 
5def to_js(filenames):
6 return [_to_js(f) for f in filenames]
7 
8def _to_js(file_name):
9 if file_name.endswith(".ts"):
10 return file_name.removesuffix(".ts") + ".js"
11 return file_name
12 
13def _to_d_ts(file_name):
14 return file_name.removesuffix(".ts") + ".d.ts"
15 
16def wd_ts_bundle(
17 name,
18 import_name,
19 schema_id,
20 modules,
21 internal_modules,
22 tsconfig_json,
23 eslintrc_json,
24 internal_wasm_modules = [],
25 internal_data_modules = [],
26 internal_json_modules = [],
27 lint = True,
28 deps = [],
29 js_deps = [],
30 gen_compile_cache = False,
31 out_dir = ""):
32 """Compiles typescript modules and generates api bundle with the result.
33 
34 Args:
35 name: bundle target name
36 import_name: bundle import name. Modules will be accessible under
37 "<import_name>:<module_name>" and internal modules under
38 "<import_name>-internal:<module_name>".
39 schema_id: bundle capnp schema id,
40 modules: list of js and ts source files for builtin modules
41 internal_modules: list of js, ts, and d.ts source files for internal modules
42 tsconfig_json: tsconfig.json label
43 eslintrc_json: eslintrc.json label
44 internal_wasm_modules: list of wasm source files
45 internal_data_modules: list of data source files
46 internal_json_modules: list of json source files
47 lint: enables/disables source linting
48 deps: additional typescript dependencies
49 gen_compile_cache: generate compilation cache of every file and include into the bundle
50 js_deps: javascript dependencies
51 """
52 ts_config(
53 name = name + "@tsconfig",
54 src = tsconfig_json,
55 deps = ["@workerd//tools:base-tsconfig"],
56 )
57 
58 srcs = modules + internal_modules
59 ts_srcs = [src for src in srcs if src.endswith(".ts")]
60 declarations = [_to_d_ts(src) for src in ts_srcs if not src.endswith(".d.ts")]
61 
62 ts_project(
63 name = name + "@tsproject",
64 srcs = ts_srcs,
65 allow_js = True,
66 declaration = True,
67 tsconfig = ":" + name + "@tsconfig",
68 deps = deps,
69 out_dir = out_dir.removesuffix("/"),
70 visibility = ["//visibility:public"],
71 )
72 
73 wd_js_bundle(
74 name = name,
75 import_name = import_name,
76 # builtin modules are accessible under "<import_name>:<module_name>" name
77 builtin_modules = [out_dir + _to_js(m) for m in modules],
78 # internal modules are accessible under "<import_name>-internal:<module_name>" name
79 # without "internal/" folder prefix.
80 internal_modules = [out_dir + _to_js(m) for m in internal_modules if not m.endswith(".d.ts")],
81 internal_wasm_modules = internal_wasm_modules,
82 internal_data_modules = internal_data_modules,
83 internal_json_modules = internal_json_modules,
84 declarations = declarations,
85 schema_id = schema_id,
86 deps = deps + js_deps,
87 gen_compile_cache = gen_compile_cache,
88 out_dir = out_dir,
89 )
90 
91 if lint:
92 lint_test(
93 name = name,
94 eslintrc_json = eslintrc_json,
95 tsconfig_json = tsconfig_json,
96 srcs = srcs,
97 )