From c2375d55c04bc05c0921efd25090fbe3122b824b Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Mon, 27 Feb 2023 19:14:39 -0800 Subject: [PATCH] WIP: Fix rules. --- common/paths.bzl | 24 +++++++++++++++++++ .../rules_prerender/prerender_component.bzl | 15 ++++++------ .../prerender_pages_unbundled.bzl | 7 ++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/common/paths.bzl b/common/paths.bzl index e032e909..7deaa138 100644 --- a/common/paths.bzl +++ b/common/paths.bzl @@ -5,3 +5,27 @@ visibility("public") def is_js_file(src): """Returns whether or not the given src path is a JavaScript file.""" return src.endswith(".js") or src.endswith(".mjs") or src.endswith(".cjs") + +def is_ts_file(src): + return src.endswith(".ts") or src.endswith(".mts") or src.endswith(".cts") + +def is_ts_declaration_file(src): + return src.endswith(".d.ts") or src.endswith(".d.mts") or src.endswith(".d.cts") + +def js_output(src): + parts = src.split(".") + rest = parts[:-1] + ext = parts[-1] + js_ext = _to_js_extension(ext) + + return ".".join(rest) + "." + js_ext + +def _to_js_extension(ext): + if ext == "ts": + return "js" + elif ext == "mts": + return "mjs" + elif ext == "cts": + return "cjs" + else: + fail("Expected a `[mc]?ts` extension, but got `%s`." % ext) diff --git a/packages/rules_prerender/prerender_component.bzl b/packages/rules_prerender/prerender_component.bzl index 9d8e58e0..7f1de45a 100644 --- a/packages/rules_prerender/prerender_component.bzl +++ b/packages/rules_prerender/prerender_component.bzl @@ -4,7 +4,7 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") load("@aspect_rules_js//js:providers.bzl", "JsInfo", "js_info") load("@aspect_rules_ts//ts:defs.bzl", "ts_project") load("//common:label.bzl", "absolute") -load("//common:paths.bzl", "is_js_file") +load("//common:paths.bzl", "is_js_file", "is_ts_file", "is_ts_declaration_file") load("//packages/rules_prerender/css:css_binaries.bzl", "css_binaries") load("//packages/rules_prerender/css:css_group.bzl", "css_group") load(":web_resources.bzl", "web_resources") @@ -39,10 +39,11 @@ def prerender_component( Args: name: The name of this rule. - srcs: The source files for use in prerendering. May be `*.ts` files or - `*.js`/`*.mjs`/`*.cjs`. All source files must be JavaScript, or all - source files must be TypeScript. However, mixing the two is not - allowed. `*.d.ts` files are also allowed in either case. + srcs: The source files for use in prerendering. May be + `*.ts`/`*.mts`/`*.cts` or `*.js`/`*.mjs`/`*.cjs` files. All source + files must be JavaScript, or all source files must be TypeScript. + However, mixing the two is not allowed. `*.d.ts` files are also + allowed in either case. tsconfig: A label referencing a tsconfig.json file or `ts_config()` target. Will be used to compile `*.ts` files in `srcs`. source_map: A boolean indicating whether or not to generate source maps for @@ -79,7 +80,7 @@ def prerender_component( """ prerender_lib = "%s_prerender" % name - if all([src.endswith(".ts") or src.endswith(".d.ts") for src in srcs]): + if all([is_ts_file(src) for src in srcs]): ts_project( name = prerender_lib, srcs = srcs, @@ -93,7 +94,7 @@ def prerender_component( testonly = testonly, visibility = visibility, ) - elif all([is_js_file(src) or src.endswith(".d.ts") for src in srcs]): + elif all([is_js_file(src) or is_ts_declaration_file(src) for src in srcs]): if tsconfig: fail("Cannot set a `tsconfig.json` file for a JS-based" + " `prerender_component()`, don't set the `tsconfig` attribute.") diff --git a/packages/rules_prerender/prerender_pages_unbundled.bzl b/packages/rules_prerender/prerender_pages_unbundled.bzl index 975d0890..658d3f7c 100644 --- a/packages/rules_prerender/prerender_pages_unbundled.bzl +++ b/packages/rules_prerender/prerender_pages_unbundled.bzl @@ -2,6 +2,7 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") load("//common:label.bzl", "absolute", "file_path_of", "rel_path") +load("//common:paths.bzl", "js_output") load(":prerender_component.bzl", "prerender_component") load(":prerender_resources.bzl", "prerender_resources_internal") load(":script_entry_point.bzl", "script_entry_points") @@ -121,11 +122,7 @@ def prerender_pages_unbundled( # Execute the runner to generate annotated resources. annotated = "%s_annotated" % name - js_src = ( - ".ts".join(src.split(".ts")[:-1]) + ".js" - if src.endswith(".ts") - else src - ) + js_src = js_output(src) prerender_resources_internal( name = annotated, entry_point = rel_path(file_path_of(absolute(js_src))),