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; }