mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 12:29:07 +00:00
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:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user