Skip to content

Commit

Permalink
Modernize JS protobuf rule (bazelbuild#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannic authored and ptmphuong committed Dec 9, 2022
1 parent 30cb6a8 commit 7d865ad
Show file tree
Hide file tree
Showing 21 changed files with 679 additions and 76 deletions.
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ os:
- osx

env:
- V=0.5.3
- V=0.12.0

before_install:
- |
Expand All @@ -33,22 +33,28 @@ before_install:
script:
- |
excludes=()
if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then
# https://github.com/bazelbuild/rules_closure/issues/247
excludes+=('//closure/testing/test:noto_fonts_render_as_expected')
fi
targets=$(bazel query "... except set($excludes)")
bazel \
--output_base=$HOME/.cache/bazel \
--batch \
--host_jvm_args=-Xmx500m \
--host_jvm_args=-Xms500m \
test \
closure/... \
javatests/... \
--worker_verbose \
--verbose_failures \
--test_output=errors \
--spawn_strategy=sandboxed \
--genrule_strategy=sandboxed \
--local_resources=400,2,1.0 \
--strategy=Javac=worker \
--strategy=Closure=worker
--strategy=Closure=worker \
$targets
notifications:
email: false
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ JavaScript | Templating | Stylesheets | Miscellaneous
--- | --- | --- | ---
[closure_js_library] | [closure_js_template_library] | [closure_css_library] | [closure_js_proto_library]
[closure_js_binary] | [closure_java_template_library] | [closure_css_binary] | [phantomjs_test]
[closure_js_deps] | [closure_py_template_library] | |
[closure_js_deps] | [closure_py_template_library] | | [closure_proto_library] \(Experimental\)
[closure_js_test] | | |

## Overview
Expand Down Expand Up @@ -950,6 +950,30 @@ This rule can be referenced as though it were the following:
- `IMPORT_ES6` // import { member } from ''


## closure\_proto\_library

```python
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_proto_library")
closure_proto_library(name, deps)
```

`closure_proto_library` generates JS code from `.proto` files.

`deps` must point to [proto_library] rules.

#### Rule Polymorphism

This rule can be referenced as though it were the following:

- [closure_js_library]: `srcs` will be the generated JS files,
and `deps` will contain necessary libraries.

- **name:** ([Name]; required) A unique name for this rule. Convention states
that such rules be named `*_closure_proto`.

- **deps:** (List of [labels]; required) The list of [proto_library] rules
to generate JS code for.

[Bazel]: http://bazel.io/
[Closure Compiler]: https://developers.google.com/closure/compiler/
[Closure Library]: https://developers.google.com/closure/library/
Expand Down Expand Up @@ -980,6 +1004,7 @@ This rule can be referenced as though it were the following:
[closure_js_proto_library]: #closure_js_proto_library
[closure_js_template_library]: #closure_js_template_library
[closure_js_test]: #closure_js_test
[closure_proto_library]: #closure_proto_library
[closure_py_template_library]: #closure_py_template_library
[coffeescript]: http://coffeescript.org/
[compiler-issue]: https://github.com/google/closure-compiler/issues/new
Expand All @@ -995,6 +1020,7 @@ This rule can be referenced as though it were the following:
[output-wrapper-faq]: https://github.com/google/closure-compiler/wiki/FAQ#when-using-advanced-optimizations-closure-compiler-adds-new-variables-to-the-global-scope-how-do-i-make-sure-my-variables-dont-collide-with-other-scripts-on-the-page
[phantomjs-bug]: https://github.com/ariya/phantomjs/issues/14028
[phantomjs_test]: #phantomjs_test
[proto_library]: https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_library
[protobuf-generator]: https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h
[protobuf-js]: https://github.com/google/protobuf/tree/master/js
[repositories.bzl]: https://github.com/bazelbuild/rules_closure/tree/master/closure/repositories.bzl
Expand Down
12 changes: 8 additions & 4 deletions closure/compiler/closure_js_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def _impl(ctx):
ctx.attr.language, ", ".join(JS_LANGUAGES)))

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

Expand Down Expand Up @@ -100,8 +101,11 @@ def _impl(ctx):
# should be cut off. These are basically the same thing as C++ include dirs,
# except unlike C++ there's no I/O operation penalty to using them since all
# source paths that exist are being passed as flags.
js_module_roots = sort_roots(find_js_module_roots(ctx, [ctx.outputs.bin]) +
js.js_module_roots)
js_module_roots = sort_roots(
find_js_module_roots(
[ctx.outputs.bin], ctx.workspace_name, ctx.label,
getattr(ctx.attr, "includes", [])) +
js.js_module_roots)
for root in js_module_roots:
args.append("--js_module_root")
args.append(root)
Expand Down Expand Up @@ -199,7 +203,7 @@ def _impl(ctx):

# Insert an edge into the build graph that produces the minified version of
# all JavaScript sources in the transitive closure, sans dead code.
argfile = create_argfile(ctx, args)
argfile = create_argfile(ctx.actions, ctx.label.name, args)
inputs.append(argfile)
ctx.action(
inputs=inputs,
Expand Down
2 changes: 1 addition & 1 deletion closure/compiler/closure_js_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load("//closure/private:defs.bzl",

def _impl(ctx):
deps = unfurl(ctx.attr.deps, provider="closure_js_library")
js = collect_js(ctx, deps)
js = collect_js(deps)
closure_root = _dirname(long_path(ctx, ctx.file._closure_library_base))
closure_rel = '/'.join(['..' for _ in range(len(closure_root.split('/')))])
outputs = [ctx.outputs.out]
Expand Down
Loading

0 comments on commit 7d865ad

Please sign in to comment.