Skip to content

Commit

Permalink
chore: misc cleanup (#3729)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Apr 2, 2024
1 parent 6199ad0 commit 7821520
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 40 deletions.
22 changes: 0 additions & 22 deletions docs/Toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ When you call `nodejs_register_toolchains()` in your `WORKSPACE` file it will se

If you have an advanced use-case and want to use a version of node not supported by this repository, you can also register your own toolchains.

## Node.js binary for the target platform

Sometimes your target platform (where your software runs) is different from the host platform (where you run Bazel) or execution platform (where Bazel actions run).
The most common case is developing a docker image on MacOS, which will execute in a Linux container.

Our toolchain support is conditional on the execution platform, as it's meant for running nodejs tools during the build.
It is not needed for this use case. Instead, simply select the nodejs you want to include in the runtime.

For example, rules_docker has a `nodejs_image` rule, which takes a `node_repository_name` attribute indicating
which nodejs binary you want to include in the image. `nodejs_linux_amd64` is the value you'd use.

## Cross-compilation

Bazel Toolchains are intended to support cross-compilation, e.g. building a linux binary from mac or windows.
Most JavaScript use cases produce platform-independent code,
but the exception is native modules which use [node-gyp](https://github.com/nodejs/node-gyp).
Any native modules will still be fetched and built, by npm/yarn, for your host platform,
so they will not work on the target platform.
The workaround is to perform the npm_install inside a docker container so that it produces modules for the target platform.

Follow https://github.com/bazelbuild/rules_nodejs/issues/506 for updates on support for node-gyp cross-compilation.

## Registering a custom toolchain

To run a custom toolchain (i.e., to run a node binary not supported by the built-in toolchains), you'll need four things:
Expand Down
4 changes: 1 addition & 3 deletions e2e/nodejs_host/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ local_repository(
path = "../..",
)

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains", "rules_nodejs_dependencies")

rules_nodejs_dependencies()
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

# Create the default node toolchain
nodejs_register_toolchains()
Expand Down
4 changes: 1 addition & 3 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains", "rules_nodejs_dependencies")

rules_nodejs_dependencies()
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

# The order matters because Bazel will provide the first registered toolchain when a rule asks Bazel to select it
# This applies to the resolved_toolchain
Expand Down
20 changes: 8 additions & 12 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
Fulfills similar role as the package.json file.
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//nodejs:repositories.bzl", "rules_nodejs_dependencies")

def http_archive(**kwargs):
maybe(_http_archive, **kwargs)

def rules_nodejs_dev_dependencies():
"""
Expand All @@ -29,10 +31,7 @@ def rules_nodejs_dev_dependencies():
shorter.
"""

rules_nodejs_dependencies()

maybe(
http_archive,
http_archive(
name = "bazel_skylib",
sha256 = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7",
urls = [
Expand All @@ -41,8 +40,7 @@ def rules_nodejs_dev_dependencies():
],
)

maybe(
http_archive,
http_archive(
name = "io_bazel_stardoc",
sha256 = "d681269c40a368c6eb7e9eccfee44a9919d22f84f80e331e41e74bdf99a3108e",
strip_prefix = "stardoc-8f6d22452d088b49b13ba2c224af69ccc8ccbc90",
Expand All @@ -51,8 +49,7 @@ def rules_nodejs_dev_dependencies():
],
)

maybe(
http_archive,
http_archive(
name = "rules_codeowners",
strip_prefix = "rules_codeowners-27fe3bbe6e5b0df196e360fc9e081835f22a10be",
sha256 = "0aada1d5df72cb13161a78dff965e02575930f3ea9550e778f6fa45f3f4e2537",
Expand All @@ -61,8 +58,7 @@ def rules_nodejs_dev_dependencies():
],
)

maybe(
http_archive,
http_archive(
name = "aspect_bazel_lib",
sha256 = "97fa63d95cc9af006c4c7b2123ddd2a91fb8d273012f17648e6423bae2c69470",
strip_prefix = "bazel-lib-1.30.2",
Expand Down

0 comments on commit 7821520

Please sign in to comment.