Skip to content

Commit

Permalink
Implement a Bazel extension for compiling Closure Template files to J…
Browse files Browse the repository at this point in the history
…avaScript.
  • Loading branch information
mknichel committed Jul 26, 2015
1 parent a962cbc commit 9abf40a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bazel/
bazel-bin/
bazel*
Empty file added BUILD
Empty file.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,40 @@

This project provides an extension to [Bazel](http://bazel.io) to compile
Google [Closure Templates](https://developers.google.com/closure/templates/)
into JavaScript files.
files into JavaScript files.

## Usage

This extension is used in existing BUILD files in your project. For directories
that contain `.soy` files, add the following rule:

```soy_library(
```
load("//path/to/closure_templates", "soy_library")
soy_library(
name = 'my_soy_library',
srcs = glob(["*.soy"]),
)```
)
```

In a terminal, then run the command:

`bazel build //path/to/src:my_soy_library`

This will generate a `.soy.js` file for every Soy source file in that directory.

## Setting up a project to use this Bazel extension

1. Set up Bazel by following the instructions on the [Getting Started](http://bazel.io/docs/getting-started.html) page
2. Put a copy of the SoyToJsSrcCompiler.jar in a tools/ directory in your project.
3. Put the closure_templates.bzl file somewhere in your project. Every BUILD file that uses Soy will need to load this .bzl file at that path.
4. Follow Usage instructions.

## Running the demo in this project

To test out this extension using this project, follow these instructions:

1. Run `git clone https://github.com/mknichel/closure-templates-bazel.git`
2. Set up Bazel by following the instructions on the [Getting Started](http://bazel.io/docs/getting-started.html) page
3. Run `bazel build //demo:demo_soy`
4. Examine the output in bazel-genfiles/demo/demo.soy.js
Empty file added WORKSPACE
Empty file.
25 changes: 25 additions & 0 deletions closure_templates.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def soy_library(
name,
srcs,
should_provide_require_soy_namespaces = True,
visibility=None):

additional_compiler_args = []
if should_provide_require_soy_namespaces:
additional_compiler_args += [
'--shouldProvideRequireSoyNamespaces']

native.genrule(
name = name,
srcs = srcs,
outs = [src + '.js' for src in srcs],
cmd = '$(JAVA) -jar $(location //tools:soy_js_compiler) ' +
'--srcs $(SRCS) ' +
'--outputPathFormat $(@D)/{INPUT_FILE_NAME}.js ' +
'--shouldGenerateJsdoc ' +
'--isUsingIjData ' +
' '.join(additional_compiler_args),
message = 'Compiling templates to JavaScript',
tools = ["//tools:soy_js_compiler"],
visibility = visibility,
)
6 changes: 6 additions & 0 deletions demo/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("/closure_templates", "soy_library")

soy_library(
name = "demo_soy",
srcs = glob(["*.soy"]),
)
5 changes: 5 additions & 0 deletions demo/demo.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{namespace demo}

/** Example template. */
{template .template}
{/template}
5 changes: 5 additions & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "soy_js_compiler",
srcs = ["SoyToJsSrcCompiler.jar"],
visibility = ["//visibility:public"],
)
Binary file added tools/SoyToJsSrcCompiler.jar
Binary file not shown.

0 comments on commit 9abf40a

Please sign in to comment.