From ee39d2354dd1e98e2dae87c748eefa260ea50ae0 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Fri, 22 Mar 2024 10:08:57 -0700 Subject: [PATCH] feat: add custom node_toolchain support (#196) * feat: add custom node_toolchain support Close #195 * Update BUILD.bazel --------- Co-authored-by: Alex Eagle --- WORKSPACE | 5 +++++ docs/esbuild.md | 5 +++-- esbuild/private/esbuild.bzl | 17 ++++++++++++++++- examples/plugins/BUILD.bazel | 7 +++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 4bc070e..77a538c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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( diff --git a/docs/esbuild.md b/docs/esbuild.md index 672f392..50c5508 100644 --- a/docs/esbuild.md +++ b/docs/esbuild.md @@ -9,8 +9,8 @@
 esbuild_bundle(name, deps, srcs, data, args_file, bazel_sandbox_plugin, bundle, config, define,
                entry_point, entry_points, esbuild_log_level, external, format, js_log_level, launcher,
-               max_threads, metafile, minify, output, output_css, output_dir, output_map, platform,
-               sourcemap, sources_content, splitting, target, tsconfig)
+               max_threads, metafile, minify, node_toolchain, output, output_css, output_dir,
+               output_map, platform, sourcemap, sources_content, splitting, target, tsconfig)
 
Runs the esbuild bundler under Bazel @@ -43,6 +43,7 @@ Note: to prevent esbuild from following symlinks and leaving the bazel sandbox, | max_threads | Sets the `GOMAXPROCS` variable to limit the number of threads that esbuild can run with. This can be useful if running many esbuild rule invocations in parallel, which has the potential to cause slowdown. For general use, leave this attribute unset. | Integer | optional | `0` | | metafile | If true, esbuild creates a metafile along with the output | Boolean | optional | `False` | | minify | Minifies the bundle with the built in minification. Removes whitespace, shortens identifieres and uses equivalent but shorter syntax.

Sets all --minify-* flags

See https://esbuild.github.io/api/#minify for more details | Boolean | optional | `False` | +| node_toolchain | 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. | Label | optional | `None` | | output | Name of the output file when bundling | Label | optional | `None` | | output_css | Declare a .css file will be output next to output bundle.

If your JS code contains import statements that import .css files, esbuild will place the content in a file next to the main output file, which you'll need to declare. If your output file is named 'foo.js', you should set this to 'foo.css'. | Label | optional | `None` | | output_dir | If true, esbuild produces an output directory containing all output files | Boolean | optional | `False` | diff --git a/esbuild/private/esbuild.bzl b/esbuild/private/esbuild.bzl index 33a7f4a..10d6616 100644 --- a/esbuild/private/esbuild.bzl +++ b/esbuild/private/esbuild.bzl @@ -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): @@ -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) diff --git a/examples/plugins/BUILD.bazel b/examples/plugins/BUILD.bazel index 73635f6..9adfbe2 100644 --- a/examples/plugins/BUILD.bazel +++ b/examples/plugins/BUILD.bazel @@ -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", + }), )