1515| Rule | Description |
1616| ---: | :---------- |
1717| [ node_repositories] ( #node_repositories ) | Install node toolchain. |
18- | [ yarn_modules] ( #yarn_modules ) | Install a set node_modules dependencies using yarn. |
19- | [ node_module] ( #node_module ) | Define a node module from a set of source files and a main (or index) source file. |
20- | [ node_binary] ( #node_binary ) | Build a node_modules tree and execute an entrypoint module script. |
21- | [ mocha_test] ( #mocha_test ) | Run a mocha test script. |
18+ | [ yarn_modules] ( #yarn_modules ) | Install a set node module dependencies using yarn. |
19+ | [ node_module] ( #node_module ) | Define a node module from a set of source files (having an optional main (or index) entry point). |
20+ | [ node_binary] ( #node_binary ) | Run a node module. |
21+ | [ node_test] ( #node_test ) | Run a node binary as a bazel test. |
22+ | [ mocha_test] ( #mocha_test ) | Run a mocha test script. |
2223
2324<table ><tr >
2425<td ><img src =" https://www.kernel.org/theme/images/logos/tux.png " height =" 48 " /></td >
@@ -84,13 +85,14 @@ populate it with the necessary dependencies.
84853 . Read the generated ` yarn.lock ` file, parse it, and write out a
8586 ` @yarn_modules//:BUILD ` file. This file contains a ` node_module `
8687 rule foreach entry in the ` yarn.lock ` file, a ` node_module ` rule
87- with the special name ` _all_ ` , and an ` sh_binary ` rule foreach
88+ with the special name ` _all_ ` , and a ` node_binary ` rule foreach
8889 executable script in the ` node_modules/.bin ` folder.
8990
9091> Note 1: You can inspect all the targets by running ` bazel query @yarn_modules//:* ` .
9192
9293> Note 2: The workspace name ` yarn_modules ` is arbitrary, choose
93- whatever you like * other than* ` node_modules ` (that one doesn't work).
94+ whatever you like (* other than* ` node_modules ` itself, that one
95+ doesn't work).
9496
9597At this point you can use these rule targets as ` deps ` for your
9698` node_module ` rules. * Example* :
@@ -152,7 +154,7 @@ node_modules/fs-extra
152154When used by other `node_module` rules, you can import the module as :
153155
154156```javascript
155- const myModule = require(" my-module " );
157+ const myModule = require(" my_module " );
156158```
157159
158160There are three basic ways to create a `node_module` rule:
@@ -249,6 +251,7 @@ These are only relevant if you don't explicitly name a `package.json` file.
249251| optional | `string` | `url` | `None ` | Url where the module tgz archive was resolved
250252| optional | `string` | `sha1` | `None ` | Sha1 hash of of the resolved tgz archive
251253| optional | `string` | `description` | `None ` | Module description
254+ | optional | `string_dict` | `executables` | `None ` | A mapping from binary name to internal node module path. Example `executables = { ' foo' : ' bin/foo' }` .
252255
253256# ## node_module attributes that affect the relative path of files included in the module
254257
@@ -267,9 +270,7 @@ the workspace, which needs to be preserved in the generated module.
267270
268271# # node_binary
269272
270- The `node_binary` rule builds a `node_modules/ ` tree based on its
271- `node_module` dependencies and writes a script to execute a module
272- entrypoint.
273+ The `node_binary` rule writes a script to execute a module entrypoint.
273274
274275```python
275276load(" @org_pubref_rules_node//node:rules.bzl" , " node_binary" )
@@ -299,38 +300,55 @@ the entrypoint (under the hood, it will just build a `node_module`
299300becoming equivalent to the first example).
300301
301302
303+ ```python
304+ node_binary(
305+ name = " foo" ,
306+ entrypoint = " :my_module_2" ,
307+ executable = " baz" ,
308+ )
309+ ```
310+
311+ In this third example (above), we' re specifying the name of the node
312+ module to start with (`my_module_2` ) and the name of the executable
313+ within `my_module_2` to run (`baz` ). In this case the `node_module`
314+ rule definition for `my_module_2` must have a `string_dict` with an
315+ entry for `baz` (like `executables = { ' baz' : ' bin/baz' }` .
316+
302317# ## Output structure of files generated for a `node_binary` rule
303318
304- A `node_binary` rule named `foo` will create a folder having exactly two entries:
319+ A `node_binary` rule named `foo` will create a folder having exactly
320+ two entries:
305321
3063221 . An executable shell script named `foo` .
307- 1 . A folder which bundles up all the needed files in `foo_bundle / ` .
323+ 1 . A folder which bundles up all the needed files in `foo_files / ` .
308324
309- Within `foo_bundle / ` , there will also be exactly two entries:
325+ Within `foo_files / ` , there will also be exactly two entries:
310326
3113271 . The `node` executable itself.
312- 1 . The `node_modules/ ` folder with all the built/ copied modules.
313-
314- The bash shell script `foo` performs the following:
315-
316- `cd $ (dirname $ 0 )/ foo_bundle && exec node node_modules/ entrypoint`
328+ 1 . The `node_modules/ ` folder with all the built/ copied modules
329+ (including the entrypoint module).
317330
318331
319332# ## Building a deployable bundle
320333
321- To generate a tarred gzipped archive of the above example that you can
322- ship as a single ' executable' file , invoke `$ bazel build
323- :{target}_bundle.tgz `. This is similar in intent to the java
334+ To generate a tarred/ gzipped archive of the above example that you can
335+ ship as a single ' executable' self - contained package , invoke `$ bazel
336+ build :{target}_deploy.tar.gz `. This is similar in intent to the java
324337`{target}_deploy.jar` implicit build rule.
325338
326339```sh
327- $ bazel build :foo_bundle.tgz
328- Target // :foo_bundle.tgz up- to- date:
340+ $ bazel build :foo_deploy
341+ Target // :foo_deploy.tar.gz up- to- date:
329342 bazel- bin / foo_bundle.tgz
330343$ du - h bazel- bin / foo_bundle.tgz
33134433M bazel- bin / foo_bundle.tgz
332345```
333346
347+ # # node_test
348+
349+ The `node_test` rule is identical to node_binary, but sets the `test =
350+ True ` flag such that it can be used as a bazel test.
351+
334352# # mocha_test
335353
336354Runs a mocha test identified by the start script given in `main` or
@@ -353,13 +371,9 @@ mocha_test(
353371 name = " test" ,
354372 main = " test.js" ,
355373)
356-
357- mocha_test(
358- name = " test" ,
359- entrypoint = " :my_module" ,
360- )
361374```
362375
363376# # Conclusion
364377
365- That' s it! Please refer to the various workspaces in `tests/` and the source for more detail.
378+ That' s it! Please refer to the various workspaces in `tests/` and the
379+ source for more detail.
0 commit comments