How can I configure nodejs rollup in bazel a workspace with multiple npm packages?

594 views Asked by At

I am trying to sort out how to correctly use rollup_bundle() in bazel in a workspace using multiple languages, including JavaScript, and defining multiple npm packages in different directories.

It appears that the rollup bazel rules assume a more centralized setup than I would like: in the context of this workspace I would like each npm package to manage its own node_modules (see WORKSPACE.bazel in reproducible).

Small reproducible (toy example):

https://github.com/mdittmer/bazel-nodejs-rollup-multi-package/commit/a14408c083836ae024719e540ee1115e61eabc97

$ bazel build //js/alpha
Starting local Bazel server and connecting to it...
ERROR: /path/to/bazel-nodejs-rollup-multi-package/js/alpha/BUILD.bazel:6:14: every rule of type rollup_bundle implicitly depends upon the target '//@bazel/rollup/bin:rollup-worker', but this target could not be found because of: no such package '@bazel/rollup/bin': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 - /path/to/bazel-nodejs-rollup-multi-package/@bazel/rollup/bin
Documentation for implicit attribute rollup_worker_bin of rules of type rollup_bundle:
Internal use only
ERROR: Analysis of target '//js/alpha:alpha' failed; build aborted: Analysis failed
INFO: Elapsed time: 2.363s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (4 packages loaded, 5 targets configured)
    currently loading: @rules_nodejs//nodejs/stamp

This error seems to suggest that I may need to specify rollup_bin and/or rollup_worker_bin, but I have had no luck attempting to refer to sources in //js/alpha/node_modules/@bazel/... or //js/alpha/node_modules/rollup/.... The npm documentation for @bazel/rollup suggests that because I am using npm_install() to set up my in-tree npm packages I should not need to set rollup_bin (because related instructions begin with "If you didn't use the yarn_install or npm_install rule, [...]").

How do I keep my multiple-npm-packages-in-same-workspace setup while using bazel rules to run rollup?

1

There are 1 answers

1
mdittmer On

I posted a viable solution here:

https://github.com/mdittmer/bazel-nodejs-rollup-multi-package/commit/07c0810a7e2d4185311f4bab4f36555648585b97

The issue appears to stem from the @build_bazel_rules_nodejs rules hard-coding @npm//... dependencies, i.e., they assume that the correct repository for loading npm module dependencies must be named @npm. To get multiple notions of @npm (theoretically--I only bothered to remap one such repository), I declared a local repository in the root //WORKSPACE.bazel file:

//WORKSPACE.bazel:

npm_install(
  name = "npm_alpha",
  package_json = "@alpha//:package.json",
  package_lock_json = "@alpha//:package-lock.json",
)

local_repository(
  name = "alpha",
  path = "js/alpha",
  repo_mapping = {
    "@npm": "@npm_alpha",
  },
)

The repo_mapping maps my choice of repository to the name @npm in the @alpha repository. Presumably, repeating this with similarly configured @beta and @npm_beta repositories will achieve the desired configuration across multiple npm modules. In this setup the root //WORKSPACE.bazel does most of the "heavy lifting" and //js/alpha/WORKSPACE.bazel is practically empty.

Aside: It appears that re-defining @npm with the configuration up to and including the npm_install() with appropriately renamed name, package_json, and package_lock_json in //js/alpha/WORKSPACE.bazel does not interfere with the root repository working as intended and has the side benefit of the //js/alpha repository working correctly as a root repository if desired.

One unresolved issue I was surprised to encounter was this:

https://github.com/mdittmer/bazel-nodejs-rollup-multi-package/commit/c2bde0fc02bce8f4b1b42dc5881a4f9bc26186db

switching from npm_install() to yarn_install() did not work (see commit message in linked commit for details).