feat: new bun_build and bun_compile, extend bun_install
This commit is contained in:
@@ -3,7 +3,10 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
exports_files([
|
||||
"bun_build_support.bzl",
|
||||
"bun_binary.bzl",
|
||||
"bun_command.bzl",
|
||||
"bun_compile.bzl",
|
||||
"bun_bundle.bzl",
|
||||
"bun_dev.bzl",
|
||||
"bun_install.bzl",
|
||||
@@ -15,20 +18,53 @@ exports_files([
|
||||
"workspace.bzl",
|
||||
])
|
||||
|
||||
bzl_library(
|
||||
name = "bun_command_bzl",
|
||||
srcs = ["bun_command.bzl"],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_build_support_bzl",
|
||||
srcs = ["bun_build_support.bzl"],
|
||||
deps = [
|
||||
":bun_command_bzl",
|
||||
":js_library_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_binary_bzl",
|
||||
srcs = ["bun_binary.bzl"],
|
||||
deps = [
|
||||
":bun_command_bzl",
|
||||
":js_library_bzl",
|
||||
":workspace_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_compile_bzl",
|
||||
srcs = ["bun_compile.bzl"],
|
||||
deps = [
|
||||
":bun_build_support_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_bundle_bzl",
|
||||
srcs = ["bun_bundle.bzl"],
|
||||
deps = [":js_library_bzl"],
|
||||
deps = [
|
||||
":bun_build_support_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_dev_bzl",
|
||||
srcs = ["bun_dev.bzl"],
|
||||
deps = [
|
||||
":bun_command_bzl",
|
||||
":workspace_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
@@ -39,12 +75,20 @@ bzl_library(
|
||||
bzl_library(
|
||||
name = "bun_script_bzl",
|
||||
srcs = ["bun_script.bzl"],
|
||||
deps = [
|
||||
":bun_command_bzl",
|
||||
":workspace_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_test_bzl",
|
||||
srcs = ["bun_test.bzl"],
|
||||
deps = [":js_library_bzl"],
|
||||
deps = [
|
||||
":bun_command_bzl",
|
||||
":js_library_bzl",
|
||||
":workspace_bzl",
|
||||
],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
"""Rule for running JS/TS scripts with Bun."""
|
||||
|
||||
load("//internal:bun_command.bzl", "append_shell_flag", "append_shell_flag_files", "append_shell_flag_values", "append_shell_install_mode", "append_shell_raw_flags", "render_shell_array", "shell_quote")
|
||||
load("//internal:js_library.bzl", "collect_js_runfiles")
|
||||
load("//internal:workspace.bzl", "create_bun_workspace_info", "render_workspace_setup", "workspace_runfiles")
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
def _bun_binary_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
@@ -13,21 +11,28 @@ def _bun_binary_impl(ctx):
|
||||
dep_runfiles = [collect_js_runfiles(dep) for dep in ctx.attr.deps]
|
||||
workspace_info = create_bun_workspace_info(
|
||||
ctx,
|
||||
extra_files = ctx.files.data + [bun_bin],
|
||||
extra_files = ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin],
|
||||
primary_file = entry_point,
|
||||
)
|
||||
|
||||
launcher_lines = [render_shell_array("bun_args", ["--bun", "run"])]
|
||||
append_shell_install_mode(launcher_lines, "bun_args", ctx.attr.install_mode)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--preload", ctx.files.preload)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--env-file", ctx.files.env_files)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-env-file", ctx.attr.no_env_file)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--smol", ctx.attr.smol)
|
||||
append_shell_flag_values(launcher_lines, "bun_args", "--conditions", ctx.attr.conditions)
|
||||
append_shell_raw_flags(launcher_lines, "bun_args", ctx.attr.run_flags)
|
||||
launcher_lines.append('bun_args+=("${primary_source}")')
|
||||
for arg in ctx.attr.args:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(arg))
|
||||
|
||||
command = """
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_exec_dir}"
|
||||
exec "${bun_bin}" --bun run "${primary_source}" "$@"
|
||||
"""
|
||||
if ctx.attr.args:
|
||||
command = """
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_exec_dir}"
|
||||
exec "${bun_bin}" --bun run "${primary_source}" __DEFAULT_ARGS__ "$@"
|
||||
""".replace("__DEFAULT_ARGS__", " ".join([_shell_quote(arg) for arg in ctx.attr.args]))
|
||||
__BUN_ARGS__
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
""".replace("__BUN_ARGS__", "\n".join(launcher_lines))
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(
|
||||
@@ -76,6 +81,33 @@ Use this rule for non-test scripts and CLIs that should run via `bazel run`.
|
||||
"deps": attr.label_list(
|
||||
doc = "Library dependencies required by the program.",
|
||||
),
|
||||
"preload": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Modules to preload with `--preload` before running the entry point.",
|
||||
),
|
||||
"env_files": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional environment files loaded with `--env-file`.",
|
||||
),
|
||||
"no_env_file": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables Bun's automatic `.env` loading.",
|
||||
),
|
||||
"smol": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's lower-memory runtime mode.",
|
||||
),
|
||||
"conditions": attr.string_list(
|
||||
doc = "Custom package resolve conditions passed to Bun.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages at runtime.",
|
||||
),
|
||||
"run_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun run` before the entry point.",
|
||||
),
|
||||
"working_dir": attr.string(
|
||||
default = "workspace",
|
||||
values = ["workspace", "entry_point"],
|
||||
|
||||
90
internal/bun_build_support.bzl
Normal file
90
internal/bun_build_support.bzl
Normal file
@@ -0,0 +1,90 @@
|
||||
"""Shared helpers for Bun build- and compile-style rules."""
|
||||
|
||||
load("//internal:bun_command.bzl", "add_flag", "add_flag_value", "add_flag_values", "add_install_mode", "add_raw_flags")
|
||||
load("//internal:js_library.bzl", "collect_js_sources")
|
||||
|
||||
def bun_build_transitive_inputs(ctx):
|
||||
transitive_inputs = []
|
||||
if getattr(ctx.attr, "node_modules", None):
|
||||
transitive_inputs.append(ctx.attr.node_modules[DefaultInfo].files)
|
||||
for dep in getattr(ctx.attr, "deps", []):
|
||||
transitive_inputs.append(collect_js_sources(dep))
|
||||
return transitive_inputs
|
||||
|
||||
def add_bun_build_common_flags(args, attr, metafile = None, metafile_md = None):
|
||||
add_install_mode(args, getattr(attr, "install_mode", "disable"))
|
||||
add_flag_value(args, "--target", getattr(attr, "target", None))
|
||||
add_flag_value(args, "--format", getattr(attr, "format", None))
|
||||
add_flag(args, "--production", getattr(attr, "production", False))
|
||||
add_flag(args, "--splitting", getattr(attr, "splitting", False))
|
||||
add_flag_value(args, "--root", getattr(attr, "root", None))
|
||||
|
||||
sourcemap = getattr(attr, "sourcemap", None)
|
||||
if sourcemap == True:
|
||||
args.add("--sourcemap")
|
||||
elif sourcemap and sourcemap != "none":
|
||||
add_flag_value(args, "--sourcemap", sourcemap)
|
||||
|
||||
add_flag_value(args, "--banner", getattr(attr, "banner", None))
|
||||
add_flag_value(args, "--footer", getattr(attr, "footer", None))
|
||||
add_flag_value(args, "--public-path", getattr(attr, "public_path", None))
|
||||
add_flag_value(args, "--packages", getattr(attr, "packages", None))
|
||||
add_flag_values(args, "--external", getattr(attr, "external", []))
|
||||
add_flag_value(args, "--entry-naming", getattr(attr, "entry_naming", None))
|
||||
add_flag_value(args, "--chunk-naming", getattr(attr, "chunk_naming", None))
|
||||
add_flag_value(args, "--asset-naming", getattr(attr, "asset_naming", None))
|
||||
add_flag(args, "--minify", getattr(attr, "minify", False))
|
||||
add_flag(args, "--minify-syntax", getattr(attr, "minify_syntax", False))
|
||||
add_flag(args, "--minify-whitespace", getattr(attr, "minify_whitespace", False))
|
||||
add_flag(args, "--minify-identifiers", getattr(attr, "minify_identifiers", False))
|
||||
add_flag(args, "--keep-names", getattr(attr, "keep_names", False))
|
||||
add_flag(args, "--css-chunking", getattr(attr, "css_chunking", False))
|
||||
add_flag_values(args, "--conditions", getattr(attr, "conditions", []))
|
||||
add_flag_value(args, "--env", getattr(attr, "env", None))
|
||||
add_flag_values(args, "--define", getattr(attr, "define", []))
|
||||
add_flag_values(args, "--drop", getattr(attr, "drop", []))
|
||||
add_flag_values(args, "--feature", getattr(attr, "feature", []))
|
||||
add_flag_values(args, "--loader", getattr(attr, "loader", []))
|
||||
add_flag_value(args, "--jsx-factory", getattr(attr, "jsx_factory", None))
|
||||
add_flag_value(args, "--jsx-fragment", getattr(attr, "jsx_fragment", None))
|
||||
add_flag_value(args, "--jsx-import-source", getattr(attr, "jsx_import_source", None))
|
||||
add_flag_value(args, "--jsx-runtime", getattr(attr, "jsx_runtime", None))
|
||||
add_flag(args, "--jsx-side-effects", getattr(attr, "jsx_side_effects", False))
|
||||
add_flag(args, "--react-fast-refresh", getattr(attr, "react_fast_refresh", False))
|
||||
add_flag(args, "--emit-dce-annotations", getattr(attr, "emit_dce_annotations", False))
|
||||
add_flag(args, "--no-bundle", getattr(attr, "no_bundle", False))
|
||||
if metafile:
|
||||
args.add("--metafile=%s" % metafile.path)
|
||||
if metafile_md:
|
||||
args.add("--metafile-md=%s" % metafile_md.path)
|
||||
add_raw_flags(args, getattr(attr, "build_flags", []))
|
||||
|
||||
def add_bun_compile_flags(args, attr, compile_executable = None):
|
||||
add_flag(args, "--compile", True)
|
||||
add_flag(args, "--bytecode", getattr(attr, "bytecode", False))
|
||||
add_flag_values(args, "--compile-exec-argv", getattr(attr, "compile_exec_argv", []))
|
||||
if getattr(attr, "compile_autoload_dotenv", True):
|
||||
args.add("--compile-autoload-dotenv")
|
||||
else:
|
||||
args.add("--no-compile-autoload-dotenv")
|
||||
if getattr(attr, "compile_autoload_bunfig", True):
|
||||
args.add("--compile-autoload-bunfig")
|
||||
else:
|
||||
args.add("--no-compile-autoload-bunfig")
|
||||
if getattr(attr, "compile_autoload_tsconfig", False):
|
||||
args.add("--compile-autoload-tsconfig")
|
||||
else:
|
||||
args.add("--no-compile-autoload-tsconfig")
|
||||
if getattr(attr, "compile_autoload_package_json", False):
|
||||
args.add("--compile-autoload-package-json")
|
||||
else:
|
||||
args.add("--no-compile-autoload-package-json")
|
||||
if compile_executable:
|
||||
add_flag_value(args, "--compile-executable-path", compile_executable.path)
|
||||
add_flag(args, "--windows-hide-console", getattr(attr, "windows_hide_console", False))
|
||||
add_flag_value(args, "--windows-icon", getattr(attr, "windows_icon", None))
|
||||
add_flag_value(args, "--windows-title", getattr(attr, "windows_title", None))
|
||||
add_flag_value(args, "--windows-publisher", getattr(attr, "windows_publisher", None))
|
||||
add_flag_value(args, "--windows-version", getattr(attr, "windows_version", None))
|
||||
add_flag_value(args, "--windows-description", getattr(attr, "windows_description", None))
|
||||
add_flag_value(args, "--windows-copyright", getattr(attr, "windows_copyright", None))
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Rule for bundling JS/TS sources with Bun."""
|
||||
|
||||
load("//internal:js_library.bzl", "collect_js_sources")
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "bun_build_transitive_inputs")
|
||||
|
||||
|
||||
def _output_name(target_name, entry):
|
||||
@@ -12,11 +12,7 @@ def _bun_bundle_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
|
||||
transitive_inputs = []
|
||||
if ctx.attr.node_modules:
|
||||
transitive_inputs.append(ctx.attr.node_modules[DefaultInfo].files)
|
||||
for dep in ctx.attr.deps:
|
||||
transitive_inputs.append(collect_js_sources(dep))
|
||||
transitive_inputs = bun_build_transitive_inputs(ctx)
|
||||
|
||||
outputs = []
|
||||
for entry in ctx.files.entry_points:
|
||||
@@ -26,20 +22,12 @@ def _bun_bundle_impl(ctx):
|
||||
args = ctx.actions.args()
|
||||
args.add("--bun")
|
||||
args.add("build")
|
||||
args.add(entry.path)
|
||||
add_bun_build_common_flags(args, ctx.attr)
|
||||
args.add("--outfile")
|
||||
args.add(output.path)
|
||||
args.add("--target")
|
||||
args.add(ctx.attr.target)
|
||||
args.add("--format")
|
||||
args.add(ctx.attr.format)
|
||||
if ctx.attr.minify:
|
||||
args.add("--minify")
|
||||
if ctx.attr.sourcemap:
|
||||
args.add("--sourcemap")
|
||||
for package in ctx.attr.external:
|
||||
args.add("--external")
|
||||
args.add(package)
|
||||
args.add(entry.path)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = bun_bin,
|
||||
@@ -78,6 +66,11 @@ Each entry point produces one output JavaScript artifact.
|
||||
allow_files = True,
|
||||
doc = "Additional non-source files needed during bundling.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages during bundling.",
|
||||
),
|
||||
"target": attr.string(
|
||||
default = "browser",
|
||||
values = ["browser", "node", "bun"],
|
||||
@@ -99,6 +92,9 @@ Each entry point produces one output JavaScript artifact.
|
||||
"external": attr.string_list(
|
||||
doc = "Package names to treat as externals (not bundled).",
|
||||
),
|
||||
"build_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun build`.",
|
||||
),
|
||||
},
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
)
|
||||
|
||||
84
internal/bun_command.bzl
Normal file
84
internal/bun_command.bzl
Normal file
@@ -0,0 +1,84 @@
|
||||
"""Shared Bun CLI flag builders for rules and launchers."""
|
||||
|
||||
def shell_quote(value):
|
||||
return "'" + str(value).replace("'", "'\"'\"'") + "'"
|
||||
|
||||
def _runfiles_workspace(file):
|
||||
workspace_name = file.owner.workspace_name
|
||||
if workspace_name:
|
||||
return workspace_name
|
||||
return "_main"
|
||||
|
||||
def runfiles_path_expr(file):
|
||||
return '"${runfiles_dir}/%s/%s"' % (_runfiles_workspace(file), file.short_path)
|
||||
|
||||
def render_shell_array(name, values):
|
||||
rendered = [shell_quote(value) for value in values]
|
||||
return "%s=(%s)" % (name, " ".join(rendered))
|
||||
|
||||
def append_shell_arg(lines, name, value):
|
||||
lines.append("%s+=(%s)" % (name, shell_quote(value)))
|
||||
|
||||
def append_shell_expr(lines, name, expr):
|
||||
lines.append("%s+=(%s)" % (name, expr))
|
||||
|
||||
def append_shell_flag(lines, name, flag, enabled):
|
||||
if enabled:
|
||||
append_shell_arg(lines, name, flag)
|
||||
|
||||
def append_shell_flag_value(lines, name, flag, value):
|
||||
if value == None:
|
||||
return
|
||||
if type(value) == type("") and not value:
|
||||
return
|
||||
append_shell_arg(lines, name, flag)
|
||||
append_shell_arg(lines, name, value)
|
||||
|
||||
def append_shell_flag_values(lines, name, flag, values):
|
||||
for value in values:
|
||||
append_shell_flag_value(lines, name, flag, value)
|
||||
|
||||
def append_shell_flag_files(lines, name, flag, files):
|
||||
for file in files:
|
||||
append_shell_arg(lines, name, flag)
|
||||
append_shell_expr(lines, name, runfiles_path_expr(file))
|
||||
|
||||
def append_shell_raw_flags(lines, name, values):
|
||||
for value in values:
|
||||
append_shell_arg(lines, name, value)
|
||||
|
||||
def append_shell_install_mode(lines, name, install_mode):
|
||||
if install_mode == "disable":
|
||||
append_shell_arg(lines, name, "--no-install")
|
||||
elif install_mode in ["fallback", "force"]:
|
||||
append_shell_flag_value(lines, name, "--install", install_mode)
|
||||
|
||||
def add_flag(args, flag, enabled):
|
||||
if enabled:
|
||||
args.add(flag)
|
||||
|
||||
def add_flag_value(args, flag, value):
|
||||
if value == None:
|
||||
return
|
||||
if type(value) == type("") and not value:
|
||||
return
|
||||
args.add(flag)
|
||||
args.add(value)
|
||||
|
||||
def add_flag_values(args, flag, values):
|
||||
for value in values:
|
||||
add_flag_value(args, flag, value)
|
||||
|
||||
def add_flag_files(args, flag, files):
|
||||
for file in files:
|
||||
args.add(flag)
|
||||
args.add(file.path)
|
||||
|
||||
def add_raw_flags(args, values):
|
||||
args.add_all(values)
|
||||
|
||||
def add_install_mode(args, install_mode):
|
||||
if install_mode == "disable":
|
||||
args.add("--no-install")
|
||||
elif install_mode in ["fallback", "force"]:
|
||||
add_flag_value(args, "--install", install_mode)
|
||||
315
internal/bun_compile.bzl
Normal file
315
internal/bun_compile.bzl
Normal file
@@ -0,0 +1,315 @@
|
||||
"""Rules for Bun build outputs and standalone executables."""
|
||||
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "add_bun_compile_flags", "bun_build_transitive_inputs")
|
||||
|
||||
def _bun_build_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
output_dir = ctx.actions.declare_directory(ctx.label.name)
|
||||
metafile = ctx.actions.declare_file(ctx.label.name + ".meta.json") if ctx.attr.metafile else None
|
||||
metafile_md = ctx.actions.declare_file(ctx.label.name + ".meta.md") if ctx.attr.metafile_md else None
|
||||
|
||||
args = ctx.actions.args()
|
||||
args.add("--bun")
|
||||
args.add("build")
|
||||
add_bun_build_common_flags(args, ctx.attr, metafile = metafile, metafile_md = metafile_md)
|
||||
args.add("--outdir")
|
||||
args.add(output_dir.path)
|
||||
args.add_all(ctx.files.entry_points)
|
||||
|
||||
outputs = [output_dir]
|
||||
if metafile:
|
||||
outputs.append(metafile)
|
||||
if metafile_md:
|
||||
outputs.append(metafile_md)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = bun_bin,
|
||||
arguments = [args],
|
||||
inputs = depset(
|
||||
direct = ctx.files.entry_points + ctx.files.data,
|
||||
transitive = bun_build_transitive_inputs(ctx),
|
||||
),
|
||||
outputs = outputs,
|
||||
mnemonic = "BunBuild",
|
||||
progress_message = "Building {} with Bun".format(ctx.label.name),
|
||||
)
|
||||
|
||||
return [DefaultInfo(files = depset(outputs))]
|
||||
|
||||
def _bun_compile_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
output = ctx.actions.declare_file(ctx.label.name)
|
||||
compile_executable = ctx.file.compile_executable
|
||||
|
||||
args = ctx.actions.args()
|
||||
args.add("--bun")
|
||||
args.add("build")
|
||||
add_bun_build_common_flags(args, ctx.attr)
|
||||
add_bun_compile_flags(args, ctx.attr, compile_executable = compile_executable)
|
||||
args.add("--outfile")
|
||||
args.add(output.path)
|
||||
args.add(ctx.file.entry_point.path)
|
||||
|
||||
direct_inputs = [ctx.file.entry_point] + ctx.files.data
|
||||
if compile_executable:
|
||||
direct_inputs.append(compile_executable)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = bun_bin,
|
||||
arguments = [args],
|
||||
inputs = depset(
|
||||
direct = direct_inputs,
|
||||
transitive = bun_build_transitive_inputs(ctx),
|
||||
),
|
||||
outputs = [output],
|
||||
mnemonic = "BunCompile",
|
||||
progress_message = "Compiling {} with Bun".format(ctx.file.entry_point.short_path),
|
||||
)
|
||||
|
||||
return [
|
||||
DefaultInfo(
|
||||
executable = output,
|
||||
files = depset([output]),
|
||||
),
|
||||
]
|
||||
|
||||
_COMMON_BUILD_ATTRS = {
|
||||
"node_modules": attr.label(
|
||||
doc = "Optional label providing package files from a `node_modules` tree, typically produced by `bun_install`, for package resolution.",
|
||||
),
|
||||
"deps": attr.label_list(
|
||||
doc = "Source/library dependencies that provide transitive inputs.",
|
||||
),
|
||||
"data": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional non-source files needed during building.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages while executing the build.",
|
||||
),
|
||||
"target": attr.string(
|
||||
default = "browser",
|
||||
values = ["browser", "node", "bun"],
|
||||
doc = "Bun build target environment.",
|
||||
),
|
||||
"format": attr.string(
|
||||
default = "esm",
|
||||
values = ["esm", "cjs", "iife"],
|
||||
doc = "Output module format.",
|
||||
),
|
||||
"production": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, sets `NODE_ENV=production` and enables Bun production mode.",
|
||||
),
|
||||
"splitting": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables code splitting.",
|
||||
),
|
||||
"root": attr.string(
|
||||
doc = "Optional root directory for multiple entry points.",
|
||||
),
|
||||
"sourcemap": attr.string(
|
||||
default = "none",
|
||||
values = ["none", "linked", "inline", "external"],
|
||||
doc = "Sourcemap emission mode.",
|
||||
),
|
||||
"banner": attr.string(
|
||||
doc = "Optional bundle banner text.",
|
||||
),
|
||||
"footer": attr.string(
|
||||
doc = "Optional bundle footer text.",
|
||||
),
|
||||
"public_path": attr.string(
|
||||
doc = "Optional public path prefix for emitted imports.",
|
||||
),
|
||||
"packages": attr.string(
|
||||
default = "bundle",
|
||||
values = ["bundle", "external"],
|
||||
doc = "Whether packages stay bundled or are treated as external.",
|
||||
),
|
||||
"external": attr.string_list(
|
||||
doc = "Modules treated as externals (not bundled).",
|
||||
),
|
||||
"entry_naming": attr.string(
|
||||
doc = "Optional entry naming template.",
|
||||
),
|
||||
"chunk_naming": attr.string(
|
||||
doc = "Optional chunk naming template.",
|
||||
),
|
||||
"asset_naming": attr.string(
|
||||
doc = "Optional asset naming template.",
|
||||
),
|
||||
"minify": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables all Bun minification passes.",
|
||||
),
|
||||
"minify_syntax": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, minifies syntax only.",
|
||||
),
|
||||
"minify_whitespace": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, minifies whitespace only.",
|
||||
),
|
||||
"minify_identifiers": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, minifies identifiers only.",
|
||||
),
|
||||
"keep_names": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, preserves function and class names when minifying.",
|
||||
),
|
||||
"css_chunking": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, Bun chunks CSS across multiple entry points.",
|
||||
),
|
||||
"conditions": attr.string_list(
|
||||
doc = "Custom resolve conditions passed to Bun.",
|
||||
),
|
||||
"env": attr.string(
|
||||
doc = "Inline environment variable behavior passed to `--env`.",
|
||||
),
|
||||
"define": attr.string_list(
|
||||
doc = "Repeated `--define` values such as `process.env.NODE_ENV:\"production\"`.",
|
||||
),
|
||||
"drop": attr.string_list(
|
||||
doc = "Repeated `--drop` values, for example `console`.",
|
||||
),
|
||||
"feature": attr.string_list(
|
||||
doc = "Repeated `--feature` values for dead-code elimination.",
|
||||
),
|
||||
"loader": attr.string_list(
|
||||
doc = "Repeated `--loader` values such as `.svg:file`.",
|
||||
),
|
||||
"jsx_factory": attr.string(
|
||||
doc = "Optional JSX factory override.",
|
||||
),
|
||||
"jsx_fragment": attr.string(
|
||||
doc = "Optional JSX fragment override.",
|
||||
),
|
||||
"jsx_import_source": attr.string(
|
||||
doc = "Optional JSX import source override.",
|
||||
),
|
||||
"jsx_runtime": attr.string(
|
||||
values = ["", "automatic", "classic"],
|
||||
default = "",
|
||||
doc = "Optional JSX runtime override.",
|
||||
),
|
||||
"jsx_side_effects": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, treats JSX as having side effects.",
|
||||
),
|
||||
"react_fast_refresh": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's React fast refresh transform.",
|
||||
),
|
||||
"emit_dce_annotations": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, re-emits DCE annotations in the bundle.",
|
||||
),
|
||||
"no_bundle": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, transpiles without bundling.",
|
||||
),
|
||||
"build_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun build`.",
|
||||
),
|
||||
}
|
||||
|
||||
bun_build = rule(
|
||||
implementation = _bun_build_impl,
|
||||
doc = """Builds one or more entry points with `bun build`.
|
||||
|
||||
The rule emits a directory artifact so Bun can materialize multi-file output
|
||||
graphs such as HTML, CSS, assets, and split chunks. Optional metafile outputs
|
||||
may be requested with `metafile` and `metafile_md`.
|
||||
""",
|
||||
attrs = dict(_COMMON_BUILD_ATTRS, **{
|
||||
"entry_points": attr.label_list(
|
||||
mandatory = True,
|
||||
allow_files = True,
|
||||
doc = "Entry files to build, including JS/TS or HTML entry points.",
|
||||
),
|
||||
"metafile": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, emits Bun's JSON metafile alongside the output directory.",
|
||||
),
|
||||
"metafile_md": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, emits Bun's markdown metafile alongside the output directory.",
|
||||
),
|
||||
}),
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
)
|
||||
|
||||
bun_compile = rule(
|
||||
implementation = _bun_compile_impl,
|
||||
doc = """Compiles a Bun program into a standalone executable with `bun build --compile`.""",
|
||||
attrs = dict(_COMMON_BUILD_ATTRS, **{
|
||||
"target": attr.string(
|
||||
default = "bun",
|
||||
values = ["browser", "node", "bun"],
|
||||
doc = "Bun build target environment for the compiled executable.",
|
||||
),
|
||||
"entry_point": attr.label(
|
||||
mandatory = True,
|
||||
allow_single_file = True,
|
||||
doc = "Entry file to compile into an executable.",
|
||||
),
|
||||
"bytecode": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun bytecode caching in the compiled executable.",
|
||||
),
|
||||
"compile_exec_argv": attr.string_list(
|
||||
doc = "Repeated `--compile-exec-argv` values prepended to the executable's `execArgv`.",
|
||||
),
|
||||
"compile_executable": attr.label(
|
||||
allow_single_file = True,
|
||||
doc = "Optional Bun executable used for cross-compilation via `--compile-executable-path`.",
|
||||
),
|
||||
"compile_autoload_dotenv": attr.bool(
|
||||
default = True,
|
||||
doc = "Whether the compiled executable auto-loads `.env` files at runtime.",
|
||||
),
|
||||
"compile_autoload_bunfig": attr.bool(
|
||||
default = True,
|
||||
doc = "Whether the compiled executable auto-loads `bunfig.toml` at runtime.",
|
||||
),
|
||||
"compile_autoload_tsconfig": attr.bool(
|
||||
default = False,
|
||||
doc = "Whether the compiled executable auto-loads `tsconfig.json` at runtime.",
|
||||
),
|
||||
"compile_autoload_package_json": attr.bool(
|
||||
default = False,
|
||||
doc = "Whether the compiled executable auto-loads `package.json` at runtime.",
|
||||
),
|
||||
"windows_hide_console": attr.bool(
|
||||
default = False,
|
||||
doc = "When targeting Windows, hides the console window for GUI-style executables.",
|
||||
),
|
||||
"windows_icon": attr.string(
|
||||
doc = "Optional Windows icon path passed directly to Bun.",
|
||||
),
|
||||
"windows_title": attr.string(
|
||||
doc = "Optional Windows executable title.",
|
||||
),
|
||||
"windows_publisher": attr.string(
|
||||
doc = "Optional Windows publisher metadata.",
|
||||
),
|
||||
"windows_version": attr.string(
|
||||
doc = "Optional Windows version metadata.",
|
||||
),
|
||||
"windows_description": attr.string(
|
||||
doc = "Optional Windows description metadata.",
|
||||
),
|
||||
"windows_copyright": attr.string(
|
||||
doc = "Optional Windows copyright metadata.",
|
||||
),
|
||||
}),
|
||||
executable = True,
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Rule for running JS/TS scripts with Bun in watch mode for development."""
|
||||
|
||||
load("//internal:bun_command.bzl", "append_shell_flag", "append_shell_flag_files", "append_shell_flag_values", "append_shell_install_mode", "append_shell_raw_flags", "render_shell_array", "shell_quote")
|
||||
load("//internal:workspace.bzl", "create_bun_workspace_info", "render_workspace_setup", "workspace_runfiles")
|
||||
|
||||
def _bun_dev_impl(ctx):
|
||||
@@ -8,23 +9,37 @@ def _bun_dev_impl(ctx):
|
||||
entry_point = ctx.file.entry_point
|
||||
workspace_info = create_bun_workspace_info(
|
||||
ctx,
|
||||
extra_files = ctx.files.data + ctx.files.restart_on + [bun_bin],
|
||||
extra_files = ctx.files.data + ctx.files.restart_on + ctx.files.preload + ctx.files.env_files + [bun_bin],
|
||||
primary_file = entry_point,
|
||||
)
|
||||
|
||||
restart_watch_paths = "\n".join([path.short_path for path in ctx.files.restart_on])
|
||||
launcher_lines = [render_shell_array("bun_args", ["--bun", "run"])]
|
||||
append_shell_install_mode(launcher_lines, "bun_args", ctx.attr.install_mode)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--preload", ctx.files.preload)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--env-file", ctx.files.env_files)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-env-file", ctx.attr.no_env_file)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--smol", ctx.attr.smol)
|
||||
append_shell_flag_values(launcher_lines, "bun_args", "--conditions", ctx.attr.conditions)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-clear-screen", ctx.attr.no_clear_screen)
|
||||
append_shell_raw_flags(launcher_lines, "bun_args", ctx.attr.run_flags)
|
||||
launcher_lines.append('bun_args+=("${primary_source}")')
|
||||
for arg in ctx.attr.args:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(arg))
|
||||
|
||||
command = """
|
||||
__BUN_ARGS__
|
||||
watch_mode="__WATCH_MODE__"
|
||||
if [[ "${watch_mode}" == "hot" ]]; then
|
||||
dev_flag="--hot"
|
||||
bun_args+=("--hot")
|
||||
else
|
||||
dev_flag="--watch"
|
||||
bun_args+=("--watch")
|
||||
fi
|
||||
|
||||
if [[ __RESTART_COUNT__ -eq 0 ]]; then
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_exec_dir}"
|
||||
exec "${bun_bin}" --bun "${dev_flag}" run "${primary_source}" "$@"
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
fi
|
||||
|
||||
readarray -t restart_paths <<'EOF_RESTART_PATHS'
|
||||
@@ -59,7 +74,7 @@ restart_child() {
|
||||
|
||||
(
|
||||
cd "${runtime_exec_dir}"
|
||||
exec "${bun_bin}" --bun "${dev_flag}" run "${primary_source}" "$@"
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
) &
|
||||
child_pid=$!
|
||||
}
|
||||
@@ -101,6 +116,9 @@ done
|
||||
).replace(
|
||||
"__RESTART_PATHS__",
|
||||
restart_watch_paths,
|
||||
).replace(
|
||||
"__BUN_ARGS__",
|
||||
"\n".join(launcher_lines),
|
||||
)
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
@@ -151,6 +169,37 @@ watch/HMR plus optional full restarts on selected file changes.
|
||||
allow_files = True,
|
||||
doc = "Additional runtime files required by the dev process.",
|
||||
),
|
||||
"preload": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Modules to preload with `--preload` before running the entry point.",
|
||||
),
|
||||
"env_files": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional environment files loaded with `--env-file`.",
|
||||
),
|
||||
"no_env_file": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables Bun's automatic `.env` loading.",
|
||||
),
|
||||
"smol": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's lower-memory runtime mode.",
|
||||
),
|
||||
"conditions": attr.string_list(
|
||||
doc = "Custom package resolve conditions passed to Bun.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages in dev mode.",
|
||||
),
|
||||
"no_clear_screen": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables terminal clearing on Bun reloads.",
|
||||
),
|
||||
"run_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun run` before the entry point.",
|
||||
),
|
||||
"working_dir": attr.string(
|
||||
default = "workspace",
|
||||
values = ["workspace", "entry_point"],
|
||||
|
||||
@@ -344,6 +344,17 @@ def _bun_install_repository_impl(repository_ctx):
|
||||
workspace_packages = _materialize_workspace_packages(repository_ctx, package_json)
|
||||
|
||||
install_args = [str(bun_bin), "--bun", "install", "--frozen-lockfile", "--no-progress"]
|
||||
if repository_ctx.attr.production:
|
||||
install_args.append("--production")
|
||||
for omit in repository_ctx.attr.omit:
|
||||
install_args.extend(["--omit", omit])
|
||||
if repository_ctx.attr.linker:
|
||||
install_args.extend(["--linker", repository_ctx.attr.linker])
|
||||
if repository_ctx.attr.backend:
|
||||
install_args.extend(["--backend", repository_ctx.attr.backend])
|
||||
if repository_ctx.attr.ignore_scripts:
|
||||
install_args.append("--ignore-scripts")
|
||||
install_args.extend(repository_ctx.attr.install_flags)
|
||||
if repository_ctx.attr.isolated_home:
|
||||
result = repository_ctx.execute(
|
||||
install_args,
|
||||
@@ -394,6 +405,12 @@ bun_install_repository = repository_rule(
|
||||
"bun_lockfile": attr.label(mandatory = True, allow_single_file = True),
|
||||
"install_inputs": attr.label_list(allow_files = True),
|
||||
"isolated_home": attr.bool(default = True),
|
||||
"production": attr.bool(default = False),
|
||||
"omit": attr.string_list(),
|
||||
"linker": attr.string(),
|
||||
"backend": attr.string(),
|
||||
"ignore_scripts": attr.bool(default = False),
|
||||
"install_flags": attr.string_list(),
|
||||
"visible_repo_name": attr.string(),
|
||||
"bun_linux_x64": attr.label(default = "@bun_linux_x64//:bun-linux-x64/bun", allow_single_file = True),
|
||||
"bun_linux_aarch64": attr.label(default = "@bun_linux_aarch64//:bun-linux-aarch64/bun", allow_single_file = True),
|
||||
@@ -403,7 +420,18 @@ bun_install_repository = repository_rule(
|
||||
},
|
||||
)
|
||||
|
||||
def bun_install(name, package_json, bun_lockfile, install_inputs = [], isolated_home = True):
|
||||
def bun_install(
|
||||
name,
|
||||
package_json,
|
||||
bun_lockfile,
|
||||
install_inputs = [],
|
||||
isolated_home = True,
|
||||
production = False,
|
||||
omit = [],
|
||||
linker = "",
|
||||
backend = "",
|
||||
ignore_scripts = False,
|
||||
install_flags = []):
|
||||
"""Create an external repository containing installed node_modules.
|
||||
|
||||
Args:
|
||||
@@ -414,6 +442,12 @@ def bun_install(name, package_json, bun_lockfile, install_inputs = [], isolated_
|
||||
into the install context, such as patch files or auth/config files.
|
||||
isolated_home: Whether to run Bun with HOME set to the generated
|
||||
repository root for a more isolated install context.
|
||||
production: Whether to omit devDependencies during install.
|
||||
omit: Optional Bun dependency groups to omit, such as `dev` or `peer`.
|
||||
linker: Optional Bun linker strategy, such as `isolated` or `hoisted`.
|
||||
backend: Optional Bun install backend, such as `hardlink` or `copyfile`.
|
||||
ignore_scripts: Whether to skip lifecycle scripts in the project manifest.
|
||||
install_flags: Additional raw flags forwarded to `bun install`.
|
||||
|
||||
Usage (WORKSPACE):
|
||||
bun_install(
|
||||
@@ -429,5 +463,11 @@ def bun_install(name, package_json, bun_lockfile, install_inputs = [], isolated_
|
||||
bun_lockfile = bun_lockfile,
|
||||
install_inputs = install_inputs,
|
||||
isolated_home = isolated_home,
|
||||
production = production,
|
||||
omit = omit,
|
||||
linker = linker,
|
||||
backend = backend,
|
||||
ignore_scripts = ignore_scripts,
|
||||
install_flags = install_flags,
|
||||
visible_repo_name = name,
|
||||
)
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
"""Rule for running package.json scripts with Bun."""
|
||||
|
||||
load("//internal:bun_command.bzl", "append_shell_flag", "append_shell_flag_files", "append_shell_flag_value", "append_shell_flag_values", "append_shell_install_mode", "append_shell_raw_flags", "render_shell_array", "shell_quote")
|
||||
load("//internal:workspace.bzl", "create_bun_workspace_info", "render_workspace_setup", "workspace_runfiles")
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
|
||||
def _bun_script_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
@@ -12,16 +10,39 @@ def _bun_script_impl(ctx):
|
||||
package_json = ctx.file.package_json
|
||||
workspace_info = create_bun_workspace_info(
|
||||
ctx,
|
||||
extra_files = ctx.files.data + [bun_bin],
|
||||
extra_files = ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin],
|
||||
package_dir_hint = package_json.dirname or ".",
|
||||
package_json = package_json,
|
||||
primary_file = package_json,
|
||||
)
|
||||
|
||||
launcher_lines = [render_shell_array("bun_args", ["--bun", "run"])]
|
||||
append_shell_install_mode(launcher_lines, "bun_args", ctx.attr.install_mode)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--preload", ctx.files.preload)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--env-file", ctx.files.env_files)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-env-file", ctx.attr.no_env_file)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--smol", ctx.attr.smol)
|
||||
append_shell_flag_values(launcher_lines, "bun_args", "--conditions", ctx.attr.conditions)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--workspaces", ctx.attr.workspaces)
|
||||
append_shell_flag_values(launcher_lines, "bun_args", "--filter", ctx.attr.filters)
|
||||
if ctx.attr.execution_mode == "parallel":
|
||||
append_shell_flag(launcher_lines, "bun_args", "--parallel", True)
|
||||
elif ctx.attr.execution_mode == "sequential":
|
||||
append_shell_flag(launcher_lines, "bun_args", "--sequential", True)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-exit-on-error", ctx.attr.no_exit_on_error)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--shell", ctx.attr.shell)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--silent", ctx.attr.silent)
|
||||
append_shell_raw_flags(launcher_lines, "bun_args", ctx.attr.run_flags)
|
||||
launcher_lines.append('bun_args+=(%s)' % shell_quote(ctx.attr.script))
|
||||
for arg in ctx.attr.args:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(arg))
|
||||
|
||||
command = """
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_exec_dir}"
|
||||
exec "${bun_bin}" --bun run __SCRIPT__ "$@"
|
||||
""".replace("__SCRIPT__", _shell_quote(ctx.attr.script))
|
||||
__BUN_ARGS__
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
""".replace("__BUN_ARGS__", "\n".join(launcher_lines))
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(
|
||||
@@ -72,6 +93,58 @@ declared in `package.json` and expect to run from the package directory with
|
||||
allow_files = True,
|
||||
doc = "Additional runtime files required by the script.",
|
||||
),
|
||||
"preload": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Modules to preload with `--preload` before running the script.",
|
||||
),
|
||||
"env_files": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional environment files loaded with `--env-file`.",
|
||||
),
|
||||
"no_env_file": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables Bun's automatic `.env` loading.",
|
||||
),
|
||||
"smol": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's lower-memory runtime mode.",
|
||||
),
|
||||
"conditions": attr.string_list(
|
||||
doc = "Custom package resolve conditions passed to Bun.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages while running the script.",
|
||||
),
|
||||
"filters": attr.string_list(
|
||||
doc = "Workspace package filters passed via repeated `--filter` flags.",
|
||||
),
|
||||
"workspaces": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, runs the script in all workspace packages.",
|
||||
),
|
||||
"execution_mode": attr.string(
|
||||
default = "single",
|
||||
values = ["single", "parallel", "sequential"],
|
||||
doc = "How Bun should execute matching workspace scripts.",
|
||||
),
|
||||
"no_exit_on_error": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, Bun keeps running other workspace scripts when one fails.",
|
||||
),
|
||||
"shell": attr.string(
|
||||
default = "",
|
||||
values = ["", "bun", "system"],
|
||||
doc = "Optional shell implementation for package scripts.",
|
||||
),
|
||||
"silent": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, suppresses Bun's command echo for package scripts.",
|
||||
),
|
||||
"run_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun run` before the script name.",
|
||||
),
|
||||
"working_dir": attr.string(
|
||||
default = "package",
|
||||
values = ["workspace", "package"],
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
"""Rule for running test suites with Bun."""
|
||||
|
||||
load("//internal:bun_command.bzl", "append_shell_flag", "append_shell_flag_files", "append_shell_flag_value", "append_shell_flag_values", "append_shell_install_mode", "append_shell_raw_flags", "render_shell_array", "shell_quote")
|
||||
load("//internal:js_library.bzl", "collect_js_runfiles")
|
||||
load("//internal:workspace.bzl", "create_bun_workspace_info", "render_workspace_setup", "workspace_runfiles")
|
||||
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
|
||||
def _bun_test_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
@@ -15,33 +12,68 @@ def _bun_test_impl(ctx):
|
||||
dep_runfiles = [collect_js_runfiles(dep) for dep in ctx.attr.deps]
|
||||
workspace_info = create_bun_workspace_info(
|
||||
ctx,
|
||||
extra_files = ctx.files.srcs + ctx.files.data + [bun_bin],
|
||||
extra_files = ctx.files.srcs + ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin],
|
||||
primary_file = primary_file,
|
||||
)
|
||||
|
||||
src_args = " ".join([_shell_quote(src.short_path) for src in ctx.files.srcs])
|
||||
launcher_lines = [render_shell_array("bun_args", ["--bun", "test"])]
|
||||
append_shell_install_mode(launcher_lines, "bun_args", ctx.attr.install_mode)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--preload", ctx.files.preload)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--env-file", ctx.files.env_files)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-env-file", ctx.attr.no_env_file)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--smol", ctx.attr.smol)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--timeout", str(ctx.attr.timeout_ms) if ctx.attr.timeout_ms > 0 else None)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--update-snapshots", ctx.attr.update_snapshots)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--rerun-each", str(ctx.attr.rerun_each) if ctx.attr.rerun_each > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--retry", str(ctx.attr.retry) if ctx.attr.retry > 0 else None)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--todo", ctx.attr.todo)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--only", ctx.attr.only)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--pass-with-no-tests", ctx.attr.pass_with_no_tests)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--concurrent", ctx.attr.concurrent)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--randomize", ctx.attr.randomize)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--seed", str(ctx.attr.seed) if ctx.attr.seed > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--bail", str(ctx.attr.bail) if ctx.attr.bail > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--max-concurrency", str(ctx.attr.max_concurrency) if ctx.attr.max_concurrency > 0 else None)
|
||||
append_shell_raw_flags(launcher_lines, "bun_args", ctx.attr.test_flags)
|
||||
launcher_lines.append('coverage_requested="0"')
|
||||
launcher_lines.append('coverage_dir=""')
|
||||
launcher_lines.append('if [[ "${COVERAGE_DIR:-}" != "" ]]; then')
|
||||
launcher_lines.append(' coverage_requested="1"')
|
||||
launcher_lines.append(' coverage_dir="${COVERAGE_DIR}"')
|
||||
launcher_lines.append('elif [[ "%s" == "1" ]]; then' % ("1" if ctx.attr.coverage else "0"))
|
||||
launcher_lines.append(' coverage_requested="1"')
|
||||
launcher_lines.append(' coverage_dir="${TEST_UNDECLARED_OUTPUTS_DIR:-${runtime_workspace}/coverage}"')
|
||||
launcher_lines.append('fi')
|
||||
launcher_lines.append('if [[ "${coverage_requested}" == "1" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--coverage")')
|
||||
launcher_lines.append(' bun_args+=("--coverage-dir" "${coverage_dir}")')
|
||||
if ctx.attr.coverage_reporters:
|
||||
for reporter in ctx.attr.coverage_reporters:
|
||||
launcher_lines.append(' bun_args+=("--coverage-reporter" %s)' % shell_quote(reporter))
|
||||
else:
|
||||
launcher_lines.append(' if [[ "${COVERAGE_DIR:-}" != "" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--coverage-reporter" "lcov")')
|
||||
launcher_lines.append(' fi')
|
||||
launcher_lines.append('fi')
|
||||
launcher_lines.append('if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--test-name-pattern" "${TESTBRIDGE_TEST_ONLY}")')
|
||||
launcher_lines.append('fi')
|
||||
if ctx.attr.reporter == "junit":
|
||||
launcher_lines.append('reporter_out="${XML_OUTPUT_FILE:-${runtime_workspace}/junit.xml}"')
|
||||
launcher_lines.append('bun_args+=("--reporter" "junit" "--reporter-outfile" "${reporter_out}")')
|
||||
elif ctx.attr.reporter == "dots":
|
||||
launcher_lines.append('bun_args+=("--reporter" "dots")')
|
||||
for src in ctx.files.srcs:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(src.short_path))
|
||||
for arg in ctx.attr.args:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(arg))
|
||||
|
||||
command = """
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_workspace}"
|
||||
test_args=(__SRC_ARGS__)
|
||||
|
||||
if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" && -n "${COVERAGE_DIR:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --test-name-pattern "${TESTBRIDGE_TEST_ONLY}" --coverage "$@"
|
||||
fi
|
||||
if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --test-name-pattern "${TESTBRIDGE_TEST_ONLY}" "$@"
|
||||
fi
|
||||
if [[ -n "${COVERAGE_DIR:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --coverage "$@"
|
||||
fi
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" "$@"
|
||||
""".replace("__SRC_ARGS__", src_args)
|
||||
if ctx.attr.args:
|
||||
default_args = "\n".join(['test_args+=({})'.format(_shell_quote(arg)) for arg in ctx.attr.args])
|
||||
command = command.replace(
|
||||
'test_args=(__SRC_ARGS__)',
|
||||
'test_args=(__SRC_ARGS__)\n' + default_args,
|
||||
)
|
||||
__BUN_ARGS__
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
""".replace("__BUN_ARGS__", "\n".join(launcher_lines))
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(
|
||||
@@ -89,6 +121,90 @@ Supports Bazel test filtering (`--test_filter`) and coverage integration.
|
||||
allow_files = True,
|
||||
doc = "Additional runtime files needed by tests.",
|
||||
),
|
||||
"preload": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Modules to preload with `--preload` before running tests.",
|
||||
),
|
||||
"env_files": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional environment files loaded with `--env-file`.",
|
||||
),
|
||||
"no_env_file": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables Bun's automatic `.env` loading.",
|
||||
),
|
||||
"smol": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's lower-memory runtime mode.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages while testing.",
|
||||
),
|
||||
"timeout_ms": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional per-test timeout in milliseconds.",
|
||||
),
|
||||
"update_snapshots": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, updates Bun snapshot files.",
|
||||
),
|
||||
"rerun_each": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional number of times to rerun each test file.",
|
||||
),
|
||||
"retry": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional default retry count for all tests.",
|
||||
),
|
||||
"todo": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, includes tests marked with `test.todo()`.",
|
||||
),
|
||||
"only": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, runs only tests marked with `test.only()` or `describe.only()`.",
|
||||
),
|
||||
"pass_with_no_tests": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, exits successfully when no tests are found.",
|
||||
),
|
||||
"concurrent": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, treats all tests as concurrent tests.",
|
||||
),
|
||||
"randomize": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, runs tests in random order.",
|
||||
),
|
||||
"seed": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional randomization seed.",
|
||||
),
|
||||
"bail": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional failure count after which Bun exits the test run.",
|
||||
),
|
||||
"reporter": attr.string(
|
||||
default = "console",
|
||||
values = ["console", "dots", "junit"],
|
||||
doc = "Test reporter format.",
|
||||
),
|
||||
"max_concurrency": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional maximum number of concurrent tests.",
|
||||
),
|
||||
"coverage": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, always enables Bun coverage output.",
|
||||
),
|
||||
"coverage_reporters": attr.string_list(
|
||||
doc = "Repeated Bun coverage reporters such as `text` or `lcov`.",
|
||||
),
|
||||
"test_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun test` before the test source list.",
|
||||
),
|
||||
},
|
||||
test = True,
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
|
||||
Reference in New Issue
Block a user