Skip to content

Commit

Permalink
Merge pull request #16 from yext/upgrade
Browse files Browse the repository at this point in the history
Upgrade to latest rules_closure
  • Loading branch information
benmcginnis authored Sep 24, 2024
2 parents 4d54729 + 3f6023c commit 9b3ee49
Show file tree
Hide file tree
Showing 30 changed files with 470 additions and 364 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
common --noenable_bzlmod
test --proto_compiler=@com_google_protobuf//:protoc
build --proto_compiler=@com_google_protobuf//:protoc
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Closure Rules for Bazel for Yext internal use

To build/test locally you must pass the --proto_compiler flag whihc we
use to speed up builds.

The .bazelrc file adds `--proto_compiler=@com_google_protobuf//:protoc`
automatically for you, so the following should "just work".

```
bazel build //...
bazel test //...
```

# Closure Rules for Bazel (αlpha) [![Bazel CI build status](https://badge.buildkite.com/7569410e2a2661076591897283051b6d137f35102167253fed.svg)](https://buildkite.com/bazel/closure-compiler-rules-closure-postsubmit)

JavaScript | Templating | Stylesheets | Miscellaneous
Expand Down
7 changes: 0 additions & 7 deletions closure/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ lines_sorted_test(
file = "repositories.bzl",
)

lines_sorted_test(
name = "defs_loads_sorted_test",
cmd = "grep -e '^load' $< >$@",
error_message = "The load list in defs.bzl must be sorted",
file = "defs.bzl",
)

bzl_library(
name = "web_library_bzl",
srcs = ["webfiles/web_library.bzl"],
Expand Down
15 changes: 4 additions & 11 deletions closure/compiler/closure_js_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@
load(
"//closure/private:defs.bzl",
"CLOSURE_WORKER_ATTR",
"JS_FILE_TYPE",
"collect_js",
"collect_runfiles",
"convert_path_to_es6_module_name",
"find_js_module_roots",
"get_jsfile_path",
"make_jschecker_progress_message",
"unfurl",
"ClosureJsLibraryInfo",
)

def _closure_js_aspect_impl(target, ctx):
if hasattr(target, "closure_js_library"):
return struct()
if ClosureJsLibraryInfo in target:
return []

# This aspect is currently a no-op in the open source world. We intend to add
# content to it in the future. It is still provided to ensure the Skylark API
# is well defined.
return struct()
return []

closure_js_aspect = aspect(
implementation = _closure_js_aspect_impl,
Expand Down
53 changes: 26 additions & 27 deletions closure/compiler/closure_js_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@

"""Rule for building JavaScript binaries with Closure Compiler."""

load(
"//closure/compiler:closure_js_aspect.bzl",
"closure_js_aspect",
)
load(
"//closure/private:defs.bzl",
"CLOSURE_JS_TOOLCHAIN_ATTRS",
"ClosureCssBinaryInfo",
"ClosureJsBinaryInfo",
"ClosureJsLibraryInfo",
"JS_LANGUAGES",
"JS_LANGUAGE_IN",
"JS_LANGUAGE_OUT_DEFAULT",
"collect_js",
"collect_runfiles",
"difference",
"extract_providers",
"find_js_module_roots",
"get_jsfile_path",
"sort_roots",
"unfurl",
)
load(
"//closure/compiler:closure_js_aspect.bzl",
"closure_js_aspect",
)

_dependency_mode_warning = '\n'.join([
"{target}: dependency_mode={old_mode} is deprecated and will be " +
"removed soon; prefer to use its equivalent {new_mode}.",
"",
" ** You can use the following buildozer command:",
"buildozer 'set dependency_mode \"{new_mode}\"' {target}",
_dependency_mode_warning = "\n".join([
"{target}: dependency_mode={old_mode} is deprecated and will be " +
"removed soon; prefer to use its equivalent {new_mode}.",
"",
" ** You can use the following buildozer command:",
"buildozer 'set dependency_mode \"{new_mode}\"' {target}",
])

def _get_dependency_mode_flag(target, attr):
Expand Down Expand Up @@ -78,8 +82,8 @@ def _impl(ctx):
", ".join(JS_LANGUAGES.to_list()),
))

deps = unfurl(ctx.attr.deps, provider = "closure_js_library")
js = collect_js(deps, ctx.attr._closure_library_base, css = ctx.attr.css)
deps = extract_providers(ctx.attr.deps, ClosureJsLibraryInfo)
js = collect_js(unfurl(deps), css = ctx.attr.css)
if not js.srcs:
fail("There are no JS source files in the transitive closure")

Expand Down Expand Up @@ -281,27 +285,22 @@ def _impl(ctx):
# promise to compile. Its fulfillment is the prerogative of ancestors which
# are free to ignore the binary in favor of the raw sauces propagated by the
# closure_js_library provider, in which case, no compilation is performed.
return struct(
files = depset(files),
closure_js_library = js,
closure_js_binary = struct(
return [
ClosureJsBinaryInfo(
bin = ctx.outputs.bin,
map = ctx.outputs.map,
language = ctx.attr.language,
),
runfiles = ctx.runfiles(
files = files + ctx.files.data,
transitive_files = depset(transitive = [
collect_runfiles(deps),
collect_runfiles([ctx.attr.css]),
collect_runfiles(ctx.attr.data),
]),
js,
DefaultInfo(
files = depset(files),
runfiles = collect_runfiles(ctx, files, extra_runfiles_attrs = ["css"]),
),
)
]

def _validate_css_graph(ctx, js):
if ctx.attr.css:
missing = difference(js.stylesheets, ctx.attr.css.closure_css_binary.labels)
missing = difference(js.stylesheets, ctx.attr.css[ClosureCssBinaryInfo].labels)
if missing:
fail("Dependent JS libraries depend on CSS libraries that weren't " +
"compiled into the referenced CSS binary: " +
Expand All @@ -315,7 +314,7 @@ closure_js_binary = rule(
implementation = _impl,
attrs = dict({
"compilation_level": attr.string(default = "ADVANCED"),
"css": attr.label(providers = ["closure_css_binary"]),
"css": attr.label(providers = [ClosureCssBinaryInfo]),
"debug": attr.bool(default = False),
"defs": attr.string_list(),
# TODO(tjgq): Remove the deprecated STRICT/LOOSE in favor of PRUNE/PRUNE_LEGACY.
Expand All @@ -332,7 +331,7 @@ closure_js_binary = rule(
),
"deps": attr.label_list(
aspects = [closure_js_aspect],
providers = ["closure_js_library"],
providers = [ClosureJsLibraryInfo],
),
"entry_points": attr.string_list(),
"formatting": attr.string(),
Expand Down
Loading

0 comments on commit 9b3ee49

Please sign in to comment.