From 76d3175da67e4ef87dd1ce84dea7564fa4681e26 Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Sat, 30 Aug 2025 10:01:49 +0000 Subject: [PATCH] fix: properly parse globalName in JS bundler and validate CLI args MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add parsing for globalName (camelCase) and global_name (snake_case) properties in JSBundler Config.fromJS - Fix memory leak by adding global_name.deinit() call in Config.deinit - Restrict --global-name CLI flag to only work with --format=iife - Add JavaScript identifier validation for --global-name using js_lexer.isIdentifier - Return clear error messages for invalid usage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/bun.js/api/JSBundler.zig | 9 +++++++++ src/cli/Arguments.zig | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index 4ae467d3f5..34f7df0c9d 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -332,6 +332,14 @@ pub const JSBundler = struct { try this.footer.appendSliceExact(slice.slice()); } + if (try config.getOptional(globalThis, "globalName", ZigString.Slice)) |slice| { + defer slice.deinit(); + try this.global_name.appendSliceExact(slice.slice()); + } else if (try config.getOptional(globalThis, "global_name", ZigString.Slice)) |slice| { + defer slice.deinit(); + try this.global_name.appendSliceExact(slice.slice()); + } + if (try config.getTruthy(globalThis, "sourcemap")) |source_map_js| { if (source_map_js.isBoolean()) { if (source_map_js == .true) { @@ -722,6 +730,7 @@ pub const JSBundler = struct { self.conditions.deinit(); self.drop.deinit(); self.banner.deinit(); + self.global_name.deinit(); if (self.compile) |*compile| { compile.deinit(); } diff --git a/src/cli/Arguments.zig b/src/cli/Arguments.zig index bcfdec2d3b..ad1ea7e326 100644 --- a/src/cli/Arguments.zig +++ b/src/cli/Arguments.zig @@ -1024,6 +1024,18 @@ pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: C } if (args.option("--global-name")) |global_name| { + // --global-name is only valid with --format=iife + if (ctx.bundler_options.output_format != .iife) { + Output.errGeneric("--global-name can only be used with --format=iife", .{}); + Global.exit(1); + } + + // Validate that the provided name is a valid JavaScript identifier + if (!bun.js_lexer.isIdentifier(global_name)) { + Output.errGeneric("--global-name must be a valid JavaScript identifier, got: {s}", .{global_name}); + Global.exit(1); + } + ctx.bundler_options.global_name = global_name; }