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
513Put ` rules_node ` in your ` WORKSPACE ` and load the main repository
614dependencies. This will download the nodejs toolchain including
7- ` node ` and ` npm ` .
15+ ` node ` (6.6.x) and ` npm ` .
816
917``` python
1018git_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
6371Takes two forms:
6472
@@ -67,14 +75,70 @@ Takes two forms:
67751 . ** 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).
0 commit comments