Skip to content

Commit

Permalink
feat: add custom node_toolchain support (#196)
Browse files Browse the repository at this point in the history
* feat: add custom node_toolchain support

Close #195

* Update BUILD.bazel

---------

Co-authored-by: Alex Eagle <[email protected]>
  • Loading branch information
jbedard and alexeagle authored Mar 22, 2024
1 parent 5de804f commit ee39d23
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ nodejs_register_toolchains(
node_version = "16.9.0",
)

nodejs_register_toolchains(
name = "node18",
node_version = "18.14.2",
)

load("//esbuild:repositories.bzl", "esbuild_register_toolchains")

esbuild_register_toolchains(
Expand Down
5 changes: 3 additions & 2 deletions docs/esbuild.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ See https://esbuild.github.io/api/#target for more details
Log levels: {}""".format(", ".join(js_lib_constants.LOG_LEVELS.keys())),
values = js_lib_constants.LOG_LEVELS.keys(),
),
"node_toolchain": attr.label(
doc = """The Node.js toolchain to use for this target.
See https://bazelbuild.github.io/rules_nodejs/Toolchains.html
Typically this is left unset so that Bazel automatically selects the right Node.js toolchain
for the target platform. See https://bazel.build/extending/toolchains#toolchain-resolution
for more information.
""",
),
}

def _bin_relative_path(ctx, file):
Expand All @@ -212,7 +222,12 @@ def _bin_relative_path(ctx, file):
return up + "/" + file.path

def _esbuild_impl(ctx):
node_toolinfo = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo
if ctx.attr.node_toolchain:
node_toolchain = ctx.attr.node_toolchain[platform_common.ToolchainInfo]
else:
node_toolchain = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"]

node_toolinfo = node_toolchain.nodeinfo
esbuild_toolinfo = ctx.toolchains["@aspect_rules_esbuild//esbuild:toolchain_type"].esbuildinfo

entry_points = desugar_entry_point_names(ctx.file.entry_point, ctx.files.entry_points)
Expand Down
7 changes: 7 additions & 0 deletions examples/plugins/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ esbuild(
],
config = "esbuild.config.mjs",
entry_point = "main.js",
# using the select statement will download toolchains for all three platforms in an unconfigured build (e.g. query rather than cquery)
# you can also just provide an individual toolchain if you don't want to download them all
node_toolchain = select({
"@bazel_tools//src/conditions:linux_x86_64": "@node18_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node18_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node18_windows_amd64//:node_toolchain",
}),
)

0 comments on commit ee39d23

Please sign in to comment.