Skip to content

Commit

Permalink
bazel: extract wd_ts_bundle rule to reduce boilerplate (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea authored and kentonv committed Apr 8, 2023
1 parent fc20fcf commit 7c20dd5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 134 deletions.
4 changes: 2 additions & 2 deletions build/wd_api_bundle.bzl → build/wd_js_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def _copy_modules(modules):
)
return dict([(modules[m].replace(":", "_"), modules[m]) for m in modules])

def wd_api_bundle(
def wd_js_bundle(
name,
schema_id,
const_name,
builtin_modules = {},
internal_modules = {},
**kwargs):
"""Generate cc capnp library with api bundle.
"""Generate cc capnp library with js api bundle.
NOTE: Due to capnpc embed limitation all modules must be in the same or sub directory of the
actual rule usage.
Expand Down
85 changes: 85 additions & 0 deletions build/wd_ts_bundle.bzl
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": [],
}),
)
78 changes: 13 additions & 65 deletions src/cloudflare/BUILD.bazel
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",
)
78 changes: 13 additions & 65 deletions src/node/BUILD.bazel
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",
)
2 changes: 1 addition & 1 deletion src/workerd/api/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <workerd/jsg/jsg.h>
#include <workerd/jsg/modules.h>
#include <capnp/dynamic.h>
#include <node/bundle.capnp.h>
#include <node/node.capnp.h>

namespace workerd::api::node {

Expand Down
2 changes: 1 addition & 1 deletion src/workerd/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ wd_cc_library(
deps = [
":capnp",
":trace",
"//src/node:bundle",
"//src/node",
"//src/workerd/api:analytics-engine_capnp",
"//src/workerd/api:r2-api_capnp",
"//src/workerd/jsg",
Expand Down

0 comments on commit 7c20dd5

Please # to comment.