Skip to content

Commit a0c8473

Browse files
committed
Adds node_library and examples.
- Node_library dynamically packages and installs your js files as a local npm repository. - Adds 0.3.2rc2 to build matrix.
1 parent b8c31c8 commit a0c8473

File tree

18 files changed

+526
-45
lines changed

18 files changed

+526
-45
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ language: node_js
44

55
env:
66
- V=HEAD URL=http://ci.bazel.io/job/Bazel/JAVA_VERSION=1.8,PLATFORM_NAME=linux-x86_64/lastSuccessfulBuild/artifact/output/ci/bazel--installer.sh FLAGS='--worker_verbose --strategy=Javac=worker --strategy=JsChecker=worker'
7+
- V=0.3.2rc2 URL=https://storage.googleapis.com/bazel/0.3.2/rc2/bazel-0.3.2rc2-installer-linux-x86_64.sh FLAGS=''
78
- V=0.3.1 URL=https://github.com/bazelbuild/bazel/releases/download/0.3.1/bazel-0.3.1-installer-linux-x86_64.sh FLAGS=''
89

910
before_install:

README.md

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
# `rules_node` [![Build Status](https://travis-ci.org/pubref/rules_node.svg?branch=master)](https://travis-ci.org/pubref/rules_node)
1+
<table><tr>
2+
<td><img src="https://github.com/pubref/rules_protobuf/blob/master/images/bazel.png" width="120"/></td>
3+
<td><img src="https://node-os.com/images/nodejs.png" width="120"/></td>
4+
<td><img src="https://www.npmjs.com/static/images/npm-logo.svg" width="120"/></td>
5+
</tr><tr>
6+
<td>Bazel</td>
7+
<td>NodeJs</td>
8+
<td>npm</td>
9+
</tr></table>
210

3-
# Installation
11+
# `rules_node` [![Build Status](https://travis-ci.org/pubref/rules_node.svg?branch=master)](https://travis-ci.org/pubref/rules_node)
412

513
Put `rules_node` in your `WORKSPACE` and load the main repository
614
dependencies. This will download the nodejs toolchain including
7-
`node` and `npm`.
15+
`node` (6.6.x) and `npm`.
816

917
```python
1018
git_repository(
@@ -23,7 +31,8 @@ node_repositories()
2331
| Rule | Description |
2432
| ---: | :---------- |
2533
| [node_repositories](#node_repositories) | Install node toolchain. |
26-
| [npm_library](#npm_library) | Declare an npm dependency. |
34+
| [npm_library](#npm_library) | Declare an external npm dependency. |
35+
| [node_library](#node_library) | Define a local npm module. |
2736
| [node_binary](#node_binary) | Build or execute a nodejs script. |
2837

2938
# Example
@@ -56,9 +65,8 @@ WORKSPACE rule. No current options.
5665

5766
## npm_library
5867

59-
BUILD rule. Declares a set of npm dependencies. Functionally
60-
equivalent to `npm install --global` (global being relative to the npm
61-
toolchain installed in your WORKSPACE.
68+
Declares a set of npm dependencies. Functionally equivalent to `npm
69+
install ...`.
6270

6371
Takes two forms:
6472

@@ -67,14 +75,70 @@ Takes two forms:
6775
1. **Multiple import**: uses a string_dict declaring the
6876
`name@version` dependency. (see `react-stack` above).
6977

78+
## node_library
79+
80+
This rule accepts a list of `srcs` (`*.js`) and other configuration
81+
attributes. When depended upon, it generates a `package.json` file
82+
describing the module and the `npm install`'s it in a local
83+
`node_modules` folder. The name of the module is the package label,
84+
substituting `/` (slash) with `-` (dash). For example:
85+
86+
```python
87+
load("//node:rules.bzl", "node_library")
88+
89+
node_library(
90+
name = "baz",
91+
main_script = "index.js",
92+
srcs = [
93+
"qux.js"
94+
],
95+
npm_deps = ["glob"],
96+
use_prefix = False,
97+
)
98+
```
99+
100+
Is installed as:
101+
102+
```sh
103+
INFO: From NpmInstallLocal examples/baz/lib/node_modules/examples-baz/package.json:
104+
/private/var/tmp/_bazel_user/178d7438552046b1be3cba61fe7b75a8/execroot/rules_node/bazel-out/local-fastbuild/bin/examples/baz/lib
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
```
118+
119+
And can be `require()`'d in another module as follows:
120+
121+
```js
122+
var baz = require("examples-baz");
123+
console.log('Hello, ' + baz());
124+
```
125+
126+
This packaging/install cycle occurs on demand and is a nicer way to
127+
develop nodejs applications with clear dependency requirements. Bazel
128+
makes this very clean and convenient.
129+
70130
## node_binary
71131

72-
BUILD rule. Create an executable script that will run the file named
73-
in the `main_script` attribute.
132+
Creates an executable script that will run the file named in the
133+
`main_script` attribute. Paths to dependent `node_library` and
134+
`npm_library` rules (each one having a `node_modules` subdirectory)
135+
are used to construct a `NODE_PATH` environment variable that the
136+
`node` executable will use to fulfill `require` dependencies.
74137

138+
---
75139

76-
> **WARNING**: these rules are not hermetic or secure! It trusts that
77-
> the `npm install` command does what is it supposed to do. There is
78-
> no current support for valdating that a particular npm package(s)
79-
> matches a sha256 (this is the the norm for npm, but it is
80-
> sub-standard for bazel).
140+
> **WARNING**: these rules are not hermetic (or by that measure,
141+
> secure)! It trusts that the `npm install` command does what is it
142+
> supposed to do, and there is no current support for validating that
143+
> a particular npm package(s) matches a sha256 (this is the the norm
144+
> for npm, but it's sub-standard for bazel).

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
workspace(name = "org_pubref_rules_node")
22

3-
load("//node:rules.bzl", "node_repositories", "npm_repository")
3+
load("//node:rules.bzl", "node_repositories")
44
node_repositories()

examples/bar/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("//node:rules.bzl", "node_binary", "npm_library", "node_library")
2+
3+
npm_library(
4+
name = "glob",
5+
)
6+
7+
node_binary(
8+
name = "bar",
9+
main_script = "bar.js",
10+
npm_deps = ["glob"],
11+
)
File renamed without changes.

examples/baz/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//node:rules.bzl", "node_binary", "npm_library", "node_library")
4+
5+
npm_library(name = "glob")
6+
7+
node_library(
8+
name = "baz",
9+
main_script = "index.js",
10+
srcs = [
11+
"qux.js"
12+
],
13+
npm_deps = ["glob"],
14+
use_prefix = False,
15+
)

examples/baz/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var qux = require("./qux.js");
2+
3+
module.exports = function() {
4+
return "Baz! (and " + qux() + ")";
5+
};

examples/baz/qux.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var glob = require("glob");
2+
3+
module.exports = function() {
4+
return "Qux!!";
5+
};

examples/foo.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/BUILD renamed to examples/foo/BUILD

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
load("//node:rules.bzl", "node_binary", "npm_library")
1+
load("//node:rules.bzl", "node_binary", "npm_library", "npm_registry")
2+
3+
npm_registry(
4+
name = "private",
5+
url = "http://localhost:3000",
6+
)
27

38
npm_library(
49
name = "glob",
@@ -16,4 +21,7 @@ node_binary(
1621
name = "foo",
1722
main_script = "foo.js",
1823
npm_deps = ["glob", "react-stack"],
24+
deps = [
25+
"//examples/baz",
26+
]
1927
)

0 commit comments

Comments
 (0)