Mercurial
diff gui_ze/gui_ze.bzl @ 190:a2725419f988 hg-web
Updated so that bun builds will with already existing js files.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sat, 24 Jan 2026 21:06:42 -0800 |
| parents | 8cf4ec5e2191 |
| children | 9f4429c49733 |
line wrap: on
line diff
--- a/gui_ze/gui_ze.bzl Fri Jan 23 22:50:28 2026 -0800 +++ b/gui_ze/gui_ze.bzl Sat Jan 24 21:06:42 2026 -0800 @@ -91,6 +91,87 @@ executable = True, ) +def _bun_bundle_impl(ctx): + """ + Bundle TypeScript/JavaScript with Bun, resolving imports from bazel root. + + Copies all dependencies (dereferencing symlinks) to create a flat structure + where imports can resolve correctly relative to the bazel workspace root. + """ + out = ctx.actions.declare_file(ctx.label.name + ".js") + + inputs = depset( + direct = [ctx.file.src], + transitive = [dep[DefaultInfo].files for dep in ctx.attr.deps] + ) + + # Get the source file's package directory (e.g., "hg-web" from "hg-web/src/main.tsx") + src_package = ctx.file.src.path.split("/")[0] + + # Collect unique root directories to copy (deduped), excluding src_package (handled separately) + dirs_to_copy = {} + for f in ctx.files.deps: + # Find the root directory path by locating where short_path starts in full path + short_path_suffix = "/".join(f.short_path.split("/")[1:]) + pos = f.path.find(short_path_suffix) + if pos > 0: + root_dir = f.path[:pos].rstrip("/") + # Skip src_package - it's a symlink that needs special handling + if not root_dir.endswith(src_package): + dirs_to_copy[root_dir] = True + + # Build copy commands for each unique directory + copy_commands = ["cp -rL {dir} .".format(dir = d) for d in dirs_to_copy.keys()] + + ctx.actions.run_shell( + inputs = inputs, + outputs = [out], + tools = [ctx.executable._bun] + [ctx.file.src] + ctx.files.deps + ctx.files.node_modules, + command = """ +cp -rL {src_package} {src_package}_tmp && rm -rf {src_package} && mv {src_package}_tmp {src_package} +{copy_commands} +export NODE_PATH=./third_party/bun/node_modules +cp ./third_party/bun/tsconfig.json . +{bun} build {entry} --outfile {output} --target browser +""".format( + copy_commands = "\n".join(copy_commands), + src_package = src_package, + bun = ctx.executable._bun.path, + entry = ctx.file.src.path, + output = out.path, + ), + progress_message = "Bundling %s with Bun" % ctx.file.src.short_path, + ) + + return [DefaultInfo(files = depset([out]))] + +bun_bundle = rule( + implementation = _bun_bundle_impl, + attrs = { + "src": attr.label( + allow_single_file = [".ts", ".tsx", ".js", ".jsx"], + doc = "Entry point file to bundle", + ), + "deps": attr.label_list( + allow_files = True, + doc = "Source files and other dependencies to include", + ), + "node_modules": attr.label_list( + allow_files = True, + default = [Label("//third_party/bun:bun_files")], + doc = "Node modules for bundling (defaults to //third_party/bun:bun_files)", + ), + "_bun": attr.label( + default = Label("//third_party/bun:bun"), + executable = True, + cfg = "exec", + ), + }, + doc = "Bundle TypeScript/JavaScript using Bun with bazel root imports", +) + + + def _bun_build_impl(ctx): """ Run bun build on the folder @@ -113,6 +194,7 @@ && cp -r {src_folder}/** . \ && cp -r ./bazel-out/k8-fastbuild/bin/hg-web/src/** src \ && ls src \ + && pwd \ && export NODE_PATH=./node_modules && {bun_path} build {input_path} --outfile {output_path} --target browser """.format( bun_path = ctx.executable._bun.path, @@ -215,7 +297,7 @@ arguments = [src.path, out.path], ) outs.append(out) - return [DefaultInfo(files = depset(outs))] + return [DefaultInfo(files = depset(outs), runfiles = ctx.runfiles(files = outs))] move_files_into_dir = rule( implementation = _move_files_into_dir_impl,