Skip to content

Commit

Permalink
Updates rules_prerender macros to support *.cjs, *.mjs, *.cts
Browse files Browse the repository at this point in the history
…, `*.mts`, `*.d.cts`, and `*.d.mts`.
  • Loading branch information
dgp1130 committed Mar 4, 2023
1 parent d5908b0 commit 094ea55
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
30 changes: 30 additions & 0 deletions common/paths.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,33 @@ 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):
"""Returns whether or not the given src path is a TypeScript source file.
Does _not_ include declaration files like `*.d.ts`.
"""
return src.endswith(".ts") or src.endswith(".mts") or src.endswith(".cts")

def is_ts_declaration_file(src):
"""Returns whether or not the given src path is a TypeScript declaration file."""
return src.endswith(".d.ts") or src.endswith(".d.mts") or src.endswith(".d.cts")

def js_output(src):
"""Returns the JS file which will be output by the given TypeScript file."""
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)
15 changes: 8 additions & 7 deletions packages/rules_prerender/prerender_component.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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.")
Expand Down
7 changes: 2 additions & 5 deletions packages/rules_prerender/prerender_pages_unbundled.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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", "is_js_file")
load(":prerender_component.bzl", "prerender_component")
load(":prerender_resources.bzl", "prerender_resources_internal")
load(":script_entry_point.bzl", "script_entry_points")
Expand Down Expand Up @@ -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 = src if is_js_file(src) else js_output(src)
prerender_resources_internal(
name = annotated,
entry_point = rel_path(file_path_of(absolute(js_src))),
Expand Down
3 changes: 2 additions & 1 deletion tools/jasmine/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("@aspect_rules_jasmine//jasmine:defs.bzl", "jasmine_test")
load("//common:paths.bzl", "is_js_file")

visibility("private")

Expand Down Expand Up @@ -39,7 +40,7 @@ def _jasmine_config_impl(ctx):
ctx.actions.write(config, json.encode_indent({
"spec_dir": "",
"spec_files": [file.short_path for file in ctx.files.deps
if file.extension == "js"],
if is_js_file(file.path)],
"failSpecWithNoExpectations": True,
}))

Expand Down

0 comments on commit 094ea55

Please sign in to comment.