-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bazel: extract wd_ts_bundle rule to reduce boilerplate (#497)
- Loading branch information
Showing
6 changed files
with
115 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project") | ||
load("@workerd//:build/wd_js_bundle.bzl", "wd_js_bundle") | ||
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin") | ||
|
||
def _to_js(file_name): | ||
if file_name.endswith(".ts"): | ||
return file_name.removesuffix(".ts") + ".js" | ||
return file_name | ||
|
||
def _to_name(file_name): | ||
return file_name.removesuffix(".ts").removesuffix(".js") | ||
|
||
def wd_ts_bundle( | ||
name, | ||
import_name, | ||
schema_id, | ||
modules, | ||
internal_modules, | ||
tsconfig_json, | ||
eslintrc_json, | ||
lint = True): | ||
"""Compiles typescript modules and generates api bundle with the result. | ||
Args: | ||
name: bundle target name | ||
import_name: bundle import name. Modules will be accessible under | ||
"<import_name>:<module_name>" and internal modules under | ||
"<import_name>-internal:<module_name>". | ||
schema_id: bundle capnp schema id, | ||
modules: list of js and ts source files for builtin modules | ||
internal_modules: list of js and ts source files for internal modules | ||
tsconfig_json: tsconfig.json label | ||
eslintrc_json: eslintrc.json label | ||
lint: enables/disables source linting | ||
""" | ||
ts_config( | ||
name = name + "@tsconfig", | ||
src = tsconfig_json, | ||
) | ||
|
||
srcs = modules + internal_modules | ||
ts_srcs = [src for src in srcs if src.endswith(".ts")] | ||
|
||
ts_project( | ||
name = name + "@tsproject", | ||
srcs = ts_srcs, | ||
allow_js = True, | ||
tsconfig = name + "@tsconfig", | ||
) | ||
|
||
wd_js_bundle( | ||
name = name, | ||
# builtin modules are accessible under "<import_name>:<module_name>" name | ||
builtin_modules = dict([(_to_js(m), import_name + ":" + _to_name(m)) for m in modules]), | ||
const_name = import_name + "Bundle", | ||
include_prefix = import_name, | ||
# internal modules are accessible under "<import_name>-internal:<module_name>" name | ||
# without "internal/" folder prefix. | ||
internal_modules = dict([( | ||
_to_js(m), | ||
import_name + "-internal:" + _to_name(m.removeprefix("internal/")), | ||
) for m in internal_modules if not m.endswith(".d.ts")]), | ||
schema_id = schema_id, | ||
) | ||
|
||
if lint: | ||
# todo: lint js_srcs too, not just ts_srcs | ||
eslint_bin.eslint_test( | ||
name = name + "@eslint", | ||
args = [ | ||
"--config $(location {})".format(eslintrc_json), | ||
"-f stylish", | ||
"--report-unused-disable-directives", | ||
] + ["$(location " + src + ")" for src in ts_srcs], | ||
data = srcs + [ | ||
eslintrc_json, | ||
tsconfig_json, | ||
"//:node_modules/@typescript-eslint/eslint-plugin", | ||
], | ||
tags = ["lint"], | ||
target_compatible_with = select({ | ||
"@platforms//os:windows": ["@platforms//:incompatible"], | ||
"//conditions:default": [], | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,17 @@ | ||
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project") | ||
load("@workerd//:build/wd_api_bundle.bzl", "wd_api_bundle") | ||
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin") | ||
load("@workerd//:build/wd_ts_bundle.bzl", "wd_ts_bundle") | ||
|
||
modules = glob(["*.ts"]) | ||
js_modules = glob(["*.js"]) | ||
|
||
internal_modules = glob(["internal/*.ts"]) | ||
internal_js_modules = glob(["internal/*.js"]) | ||
|
||
srcs = modules + internal_modules | ||
|
||
eslintrc_json = ":.eslintrc.json" | ||
|
||
tsconfig_json = ":tsconfig.json" | ||
|
||
ts_config( | ||
name = "tsconfig", | ||
src = tsconfig_json, | ||
) | ||
|
||
ts_project( | ||
wd_ts_bundle( | ||
name = "cloudflare", | ||
srcs = srcs, | ||
allow_js = True, | ||
tsconfig = ":tsconfig", | ||
) | ||
|
||
wd_api_bundle( | ||
name = "cfbundle", | ||
# builtin modules are accessible under "cloudflare:<module_name>" name | ||
builtin_modules = dict([( | ||
m.removesuffix(".ts") + ".js", | ||
"cloudflare:" + m.removesuffix(".ts"), | ||
) for m in modules] + [( | ||
m, | ||
"cloudflare:" + m.removesuffix(".js"), | ||
) for m in js_modules]), | ||
const_name = "cloudflareBundle", | ||
include_prefix = "cloudflare", | ||
# internal modules are accessible under "cloudflare-internal:<module_name>" name without "internal/" | ||
# folder prefix. | ||
internal_modules = dict([( | ||
m.removesuffix(".ts") + ".js", | ||
"cloudflare-internal:" + m.removeprefix("internal/").removesuffix(".ts"), | ||
) for m in internal_modules if not m.endswith(".d.ts")] + [( | ||
m, "cloudflare-internal:" + m.removeprefix("internal/").removesuffix(".js"), | ||
) for m in internal_js_modules]), | ||
eslintrc_json = ".eslintrc.json", | ||
import_name = "cloudflare", | ||
internal_modules = glob([ | ||
"internal/*.ts", | ||
"internal/*.js", | ||
]), | ||
modules = glob([ | ||
"*.ts", | ||
"*.js", | ||
]), | ||
schema_id = "0xbcc8f57c63814006", | ||
) | ||
|
||
eslint_bin.eslint_test( | ||
name = "eslint", | ||
args = [ | ||
"--config $(location {})".format(eslintrc_json), | ||
"-f stylish", | ||
"--report-unused-disable-directives" | ||
] + ["$(location " + src + ")" for src in srcs], | ||
data = srcs + [ | ||
eslintrc_json, | ||
tsconfig_json, | ||
"//:node_modules/@typescript-eslint/eslint-plugin", | ||
], | ||
tags = ["lint"], | ||
target_compatible_with = select({ | ||
"@platforms//os:windows": ["@platforms//:incompatible"], | ||
"//conditions:default": [], | ||
}), | ||
tsconfig_json = "tsconfig.json", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,17 @@ | ||
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project") | ||
load("@workerd//:build/wd_api_bundle.bzl", "wd_api_bundle") | ||
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin") | ||
load("@workerd//:build/wd_ts_bundle.bzl", "wd_ts_bundle") | ||
|
||
modules = glob(["*.ts"]) | ||
js_modules = glob(["*.js"]) | ||
|
||
internal_modules = glob(["internal/*.ts"]) | ||
internal_js_modules = glob(["internal/*.js"]) | ||
|
||
srcs = modules + internal_modules | ||
|
||
eslintrc_json = ":.eslintrc.json" | ||
|
||
tsconfig_json = ":tsconfig.json" | ||
|
||
ts_config( | ||
name = "tsconfig", | ||
src = tsconfig_json, | ||
) | ||
|
||
ts_project( | ||
wd_ts_bundle( | ||
name = "node", | ||
srcs = srcs, | ||
allow_js = True, | ||
tsconfig = ":tsconfig", | ||
) | ||
|
||
wd_api_bundle( | ||
name = "bundle", | ||
# builtin modules are accessible under "node:<module_name>" name | ||
builtin_modules = dict([( | ||
m.removesuffix(".ts") + ".js", | ||
"node:" + m.removesuffix(".ts"), | ||
) for m in modules] + [( | ||
m, | ||
"node:" + m.removesuffix(".js"), | ||
) for m in js_modules]), | ||
const_name = "nodeBundle", | ||
include_prefix = "node", | ||
# internal modules are accessible under "node-internal:<module_name>" name without "internal/" | ||
# folder prefix. | ||
internal_modules = dict([( | ||
m.removesuffix(".ts") + ".js", | ||
"node-internal:" + m.removeprefix("internal/").removesuffix(".ts"), | ||
) for m in internal_modules if not m.endswith(".d.ts")] + [( | ||
m, "node-internal:" + m.removeprefix("internal/").removesuffix(".js"), | ||
) for m in internal_js_modules]), | ||
eslintrc_json = ".eslintrc.json", | ||
import_name = "node", | ||
internal_modules = glob([ | ||
"internal/*.ts", | ||
"internal/*.js", | ||
]), | ||
modules = glob([ | ||
"*.ts", | ||
"*.js", | ||
]), | ||
schema_id = "0xbcc8f57c63814005", | ||
) | ||
|
||
eslint_bin.eslint_test( | ||
name = "eslint", | ||
args = [ | ||
"--config $(location {})".format(eslintrc_json), | ||
"-f stylish", | ||
"--report-unused-disable-directives" | ||
] + ["$(location " + src + ")" for src in srcs], | ||
data = srcs + [ | ||
eslintrc_json, | ||
tsconfig_json, | ||
"//:node_modules/@typescript-eslint/eslint-plugin", | ||
], | ||
tags = ["lint"], | ||
target_compatible_with = select({ | ||
"@platforms//os:windows": ["@platforms//:incompatible"], | ||
"//conditions:default": [], | ||
}), | ||
tsconfig_json = "tsconfig.json", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters