diff --git a/build/wd_api_bundle.bzl b/build/wd_js_bundle.bzl similarity index 98% rename from build/wd_api_bundle.bzl rename to build/wd_js_bundle.bzl index 2b21b374fff7..9dd7147657ac 100644 --- a/build/wd_api_bundle.bzl +++ b/build/wd_js_bundle.bzl @@ -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. diff --git a/build/wd_ts_bundle.bzl b/build/wd_ts_bundle.bzl new file mode 100644 index 000000000000..9b22d89ba665 --- /dev/null +++ b/build/wd_ts_bundle.bzl @@ -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 + ":" and internal modules under + "-internal:". + 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 ":" 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 "-internal:" 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": [], + }), + ) diff --git a/src/cloudflare/BUILD.bazel b/src/cloudflare/BUILD.bazel index 0831bc12178b..62bdaa9cc383 100644 --- a/src/cloudflare/BUILD.bazel +++ b/src/cloudflare/BUILD.bazel @@ -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:" 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:" 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", ) diff --git a/src/node/BUILD.bazel b/src/node/BUILD.bazel index 94a12de4d49a..38de0c62589b 100644 --- a/src/node/BUILD.bazel +++ b/src/node/BUILD.bazel @@ -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:" 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:" 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", ) diff --git a/src/workerd/api/node/node.h b/src/workerd/api/node/node.h index 8600d0dbf8c2..a95459be44aa 100644 --- a/src/workerd/api/node/node.h +++ b/src/workerd/api/node/node.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include namespace workerd::api::node { diff --git a/src/workerd/io/BUILD.bazel b/src/workerd/io/BUILD.bazel index d12dc907f0dc..eaecae061dee 100644 --- a/src/workerd/io/BUILD.bazel +++ b/src/workerd/io/BUILD.bazel @@ -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",