fix: properly parse globalName in JS bundler and validate CLI args

- 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 <noreply@anthropic.com>
This commit is contained in:
Claude Bot
2025-08-30 10:01:49 +00:00
parent dd439a925b
commit 76d3175da6
2 changed files with 21 additions and 0 deletions

View File

@@ -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();
}

View File

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