switch esbuild to a toolchain

This commit is contained in:
Damien Elmes 2021-04-07 15:19:23 +10:00
parent 4975f47ea3
commit c1c14419a2
5 changed files with 58 additions and 13 deletions

View File

@ -3,11 +3,6 @@ load("//ts/esbuild:upstream.bzl", _esbuild = "esbuild_macro")
def esbuild(name, **kwargs):
_esbuild(
name = name,
tool = select({
"@bazel_tools//src/conditions:darwin": "@esbuild_darwin//:bin/esbuild",
"@bazel_tools//src/conditions:windows": "@esbuild_windows//:esbuild.exe",
"@bazel_tools//src/conditions:linux_x86_64": "@esbuild_linux//:bin/esbuild",
}),
minify = select({
"//:release": True,
"//conditions:default": False,

View File

@ -0,0 +1,8 @@
load(":toolchain.bzl", "define_default_toolchains", "esbuild_toolchain")
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
define_default_toolchains()

View File

@ -5,6 +5,7 @@ Helper macro for fetching esbuild versions for internal tests and examples in ru
"""
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(":toolchain.bzl", "register_default_toolchains")
_VERSION = "0.11.5"
@ -40,3 +41,5 @@ def esbuild_dependencies():
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "113c2e84895f4422a3676db4e15d9f01b2b4fac041edab25284fdb9574ba58a0",
)
register_default_toolchains()

42
ts/esbuild/toolchain.bzl Normal file
View File

@ -0,0 +1,42 @@
def _esbuild_toolchain_impl(ctx):
return [platform_common.ToolchainInfo(
binary = ctx.executable.binary,
)]
esbuild_toolchain = rule(
implementation = _esbuild_toolchain_impl,
attrs = {
"binary": attr.label(allow_single_file = True, executable = True, cfg = "exec"),
},
)
_package_path = "@net_ankiweb_anki//ts/esbuild"
TOOLCHAIN = _package_path + ":toolchain_type"
_default_toolchains = [
["@esbuild_darwin//:bin/esbuild", "macos"],
["@esbuild_linux//:bin/esbuild", "linux"],
["@esbuild_windows//:esbuild.exe", "windows"],
]
def define_default_toolchains():
for repo_path, platform in _default_toolchains:
esbuild_toolchain(
name = "esbuild_" + platform,
binary = repo_path,
)
native.toolchain(
name = "esbuild_{}_toolchain".format(platform),
exec_compatible_with = [
"@platforms//os:" + platform,
"@platforms//cpu:x86_64",
],
toolchain = ":esbuild_" + platform,
toolchain_type = ":toolchain_type",
)
def register_default_toolchains():
for _, platform in _default_toolchains:
native.register_toolchains(_package_path + ":esbuild_{}_toolchain".format(platform))

View File

@ -5,6 +5,7 @@ esbuild rule
load("@build_bazel_rules_nodejs//:providers.bzl", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NpmPackageInfo", "node_modules_aspect")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect")
load(":helpers.bzl", "filter_files", "generate_path_mapping", "resolve_js_input", "write_jsconfig_file")
load(":toolchain.bzl", "TOOLCHAIN")
def _esbuild_impl(ctx):
# For each dep, JSEcmaScriptModuleInfo is used if found, then JSModuleInfo and finally
@ -128,7 +129,7 @@ def _esbuild_impl(ctx):
ctx.actions.run(
inputs = inputs,
outputs = outputs,
executable = ctx.executable.tool,
executable = ctx.toolchains[TOOLCHAIN].binary,
arguments = [args],
progress_message = "%s Javascript %s [esbuild]" % ("Bundling" if not ctx.attr.output_dir else "Splitting", entry_point.short_path),
execution_requirements = execution_requirements,
@ -268,19 +269,15 @@ edge16, node10, default esnext)
See https://esbuild.github.io/api/#target for more details
""",
),
"tool": attr.label(
allow_single_file = True,
mandatory = True,
executable = True,
cfg = "exec",
doc = "An executable for the esbuild binary",
),
},
implementation = _esbuild_impl,
doc = """Runs the esbuild bundler under Bazel
For further information about esbuild, see https://esbuild.github.io/
""",
toolchains = [
TOOLCHAIN,
],
)
def esbuild_macro(name, output_dir = False, output_css = False, **kwargs):