diff gui_ze/gui_ze.bzl @ 195:f8f5004a920a

Merging back hg-web-tip
author MrJuneJune <me@mrjunejune.com>
date Tue, 27 Jan 2026 06:51:44 -0800
parents 9f4429c49733
children
line wrap: on
line diff
--- a/gui_ze/gui_ze.bzl	Sat Jan 24 06:37:43 2026 -0800
+++ b/gui_ze/gui_ze.bzl	Tue Jan 27 06:51:44 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
@@ -111,11 +192,15 @@
     command = """
       cp -r third_party/bun/** . \
       && cp -r {src_folder}/** .  \
-      && export NODE_PATH=./node_modules && {bun_path} build {input_path} --outfile {output_path}
+      && 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,
       src_folder = ctx.attr.src_folder,
-      input_path = ctx.file.src.path.split("/")[-1],
+      # Fix this lol
+      input_path = "/".join(ctx.file.src.path.split("/")[-2:]),
       output_path = out.path,
     ),
     progress_message = "Bundling {} with Bun!\n\n".format(ctx.file.src.path),
@@ -208,11 +293,11 @@
     ctx.actions.run_shell(
       inputs = [src],
       outputs = [out],
-      command = "cp \"$1\" \"$2\"",
+      command = "cp -r \"$1\" \"$2\"",
       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,