Skip to content

Commit

Permalink
Support disabling minification in Bun.serve with development: false (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Jan 27, 2025
1 parent 68ee830 commit ce53290
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/api/schema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,9 @@ pub const Api = struct {
/// [serve.static]
/// plugins = ["tailwindcss"]
serve_plugins: ?[]const []const u8 = null,
serve_minify_syntax: ?bool = null,
serve_minify_whitespace: ?bool = null,
serve_minify_identifiers: ?bool = null,
bunfig_path: []const u8,

pub fn decode(reader: anytype) anyerror!TransformOptions {
Expand Down
30 changes: 26 additions & 4 deletions src/bun.js/api/server/HTMLBundle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ bunfig_dir: []const u8,
/// `plugins` is array of serve plugins defined in the bunfig.toml file. They will be resolved and loaded.
/// `bunfig_path` is the path to the bunfig.toml configuration file. It used to resolve the plugins relative
/// to the bunfig.toml file.
pub fn init(globalObject: *JSGlobalObject, path: []const u8, bunfig_path: []const u8, plugins: ?[]const []const u8) !*HTMLBundle {
pub fn init(
globalObject: *JSGlobalObject,
path: []const u8,
bunfig_path: []const u8,
plugins: ?[]const []const u8,
) !*HTMLBundle {
var config = bun.JSC.API.JSBundler.Config{};
try config.entry_points.insert(path);
config.target = .browser;
Expand Down Expand Up @@ -261,12 +266,29 @@ pub const HTMLBundleRoute = struct {
config.entry_points = config.entry_points.clone() catch bun.outOfMemory();
config.public_path = config.public_path.clone() catch bun.outOfMemory();
config.define = config.define.clone() catch bun.outOfMemory();
if (!server.config().development) {
config.minify.syntax = true;
config.minify.whitespace = true;

if (bun.CLI.Command.get().args.serve_minify_identifiers) |minify_identifiers| {
config.minify.identifiers = minify_identifiers;
} else if (!server.config().development) {
config.minify.identifiers = true;
}

if (bun.CLI.Command.get().args.serve_minify_whitespace) |minify_whitespace| {
config.minify.whitespace = minify_whitespace;
} else if (!server.config().development) {
config.minify.whitespace = true;
}

if (bun.CLI.Command.get().args.serve_minify_syntax) |minify_syntax| {
config.minify.syntax = minify_syntax;
} else if (!server.config().development) {
config.minify.syntax = true;
}

if (!server.config().development) {
config.define.put("process.env.NODE_ENV", "\"production\"") catch bun.outOfMemory();
}

config.source_map = .linked;

const completion_task = bun.BundleV2.createAndScheduleCompletionTask(
Expand Down
22 changes: 22 additions & 0 deletions src/bunfig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,29 @@ pub const Bunfig = struct {
}
};

// TODO: accept entire config object.
this.bunfig.serve_plugins = plugins;
if (serve_obj.get("minify")) |minify| {
if (minify.asBool()) |value| {
this.bunfig.serve_minify_syntax = value;
this.bunfig.serve_minify_whitespace = value;
this.bunfig.serve_minify_identifiers = value;
} else if (minify.isObject()) {
if (minify.get("syntax")) |syntax| {
this.bunfig.serve_minify_syntax = syntax.asBool() orelse false;
}

if (minify.get("whitespace")) |whitespace| {
this.bunfig.serve_minify_whitespace = whitespace.asBool() orelse false;
}

if (minify.get("identifiers")) |identifiers| {
this.bunfig.serve_minify_identifiers = identifiers.asBool() orelse false;
}
} else {
try this.addError(minify.loc, "Expected minify to be boolean or object");
}
}
this.bunfig.bunfig_path = bun.default_allocator.dupe(u8, this.source.path.text) catch bun.outOfMemory();
}
}
Expand Down

0 comments on commit ce53290

Please sign in to comment.