feat: new bun_build and bun_compile, extend bun_install
This commit is contained in:
@@ -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"],
|
||||
|
||||
Reference in New Issue
Block a user