Skip to content
Merged
20 changes: 20 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,26 @@ declare module "bun" {
execArgv?: string[];
executablePath?: string;
outfile?: string;
/**
* Whether to autoload .env files when the standalone executable runs
*
* Standalone-only: applies only when building/running the standalone executable.
*
* Equivalent CLI flags: `--compile-autoload-dotenv`, `--no-compile-autoload-dotenv`
*
* @default true
*/
autoloadDotenv?: boolean;
/**
* Whether to autoload bunfig.toml when the standalone executable runs
*
* Standalone-only: applies only when building/running the standalone executable.
*
* Equivalent CLI flags: `--compile-autoload-bunfig`, `--no-compile-autoload-bunfig`
*
* @default true
*/
autoloadBunfig?: boolean;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
windows?: {
hideConsole?: boolean;
icon?: string;
Expand Down
15 changes: 13 additions & 2 deletions src/StandaloneModuleGraph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const StandaloneModuleGraph = struct {
files: bun.StringArrayHashMap(File),
entry_point_id: u32 = 0,
compile_exec_argv: []const u8 = "",
flags: Flags = .{},

// We never want to hit the filesystem for these files
// We use the `/$bunfs/` prefix to indicate that it's a virtual path
Expand Down Expand Up @@ -289,6 +290,13 @@ pub const StandaloneModuleGraph = struct {
modules_ptr: bun.StringPointer = .{},
entry_point_id: u32 = 0,
compile_exec_argv_ptr: bun.StringPointer = .{},
flags: Flags = .{},
};

pub const Flags = packed struct(u32) {
disable_default_env_files: bool = false,
disable_autoload_bunfig: bool = false,
_padding: u30 = 0,
};

const trailer = "\n---- Bun! ----\n";
Expand Down Expand Up @@ -334,6 +342,7 @@ pub const StandaloneModuleGraph = struct {
.files = modules,
.entry_point_id = offsets.entry_point_id,
.compile_exec_argv = sliceToZ(raw_bytes, offsets.compile_exec_argv_ptr),
.flags = offsets.flags,
};
}

Expand All @@ -349,7 +358,7 @@ pub const StandaloneModuleGraph = struct {
return bytes[ptr.offset..][0..ptr.length :0];
}

pub fn toBytes(allocator: std.mem.Allocator, prefix: []const u8, output_files: []const bun.options.OutputFile, output_format: bun.options.Format, compile_exec_argv: []const u8) ![]u8 {
pub fn toBytes(allocator: std.mem.Allocator, prefix: []const u8, output_files: []const bun.options.OutputFile, output_format: bun.options.Format, compile_exec_argv: []const u8, flags: Flags) ![]u8 {
var serialize_trace = bun.perf.trace("StandaloneModuleGraph.serialize");
defer serialize_trace.end();

Expand Down Expand Up @@ -498,6 +507,7 @@ pub const StandaloneModuleGraph = struct {
.modules_ptr = string_builder.appendCount(std.mem.sliceAsBytes(modules.items)),
.compile_exec_argv_ptr = string_builder.appendCountZ(compile_exec_argv),
.byte_count = string_builder.len,
.flags = flags,
};

_ = string_builder.append(std.mem.asBytes(&offsets));
Expand Down Expand Up @@ -979,8 +989,9 @@ pub const StandaloneModuleGraph = struct {
windows_options: bun.options.WindowsOptions,
compile_exec_argv: []const u8,
self_exe_path: ?[]const u8,
flags: Flags,
) !CompileResult {
const bytes = toBytes(allocator, module_prefix, output_files, output_format, compile_exec_argv) catch |err| {
const bytes = toBytes(allocator, module_prefix, output_files, output_format, compile_exec_argv, flags) catch |err| {
return CompileResult.failFmt("failed to generate module graph bytes: {s}", .{@errorName(err)});
};
if (bytes.len == 0) return CompileResult.fail(.no_output_files);
Expand Down
11 changes: 9 additions & 2 deletions src/bun.js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub const Run = struct {
js_ast.Stmt.Data.Store.create();
const arena = Arena.init();

if (!ctx.debug.loaded_bunfig) {
// Load bunfig.toml unless disabled by compile flags
if (!ctx.debug.loaded_bunfig and !graph.flags.disable_autoload_bunfig) {
try bun.cli.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand);
}

Expand Down Expand Up @@ -81,7 +82,13 @@ pub const Run = struct {
.unspecified => {},
}

b.options.env.behavior = .load_all_without_inlining;
// If .env loading is disabled, only load process env vars
// Otherwise, load all .env files
if (graph.flags.disable_default_env_files) {
b.options.env.behavior = .disable;
} else {
b.options.env.behavior = .load_all_without_inlining;
}

b.configureDefines() catch {
failWithBuildError(vm);
Expand Down
10 changes: 10 additions & 0 deletions src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub const JSBundler = struct {
windows_description: OwnedString = OwnedString.initEmpty(bun.default_allocator),
windows_copyright: OwnedString = OwnedString.initEmpty(bun.default_allocator),
outfile: OwnedString = OwnedString.initEmpty(bun.default_allocator),
autoload_dotenv: bool = true,
autoload_bunfig: bool = true,

pub fn fromJS(globalThis: *jsc.JSGlobalObject, config: jsc.JSValue, allocator: std.mem.Allocator, compile_target: ?CompileTarget) JSError!?CompileOptions {
var this = CompileOptions{
Expand Down Expand Up @@ -177,6 +179,14 @@ pub const JSBundler = struct {
try this.outfile.appendSliceExact(slice.slice());
}

if (try object.getBooleanLoose(globalThis, "autoloadDotenv")) |autoload_dotenv| {
this.autoload_dotenv = autoload_dotenv;
}

if (try object.getBooleanLoose(globalThis, "autoloadBunfig")) |autoload_bunfig| {
this.autoload_bunfig = autoload_bunfig;
}

return this;
}

Expand Down
4 changes: 4 additions & 0 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,10 @@ pub const BundleV2 = struct {
compile_options.executable_path.slice()
else
null,
.{
.disable_default_env_files = !compile_options.autoload_dotenv,
.disable_autoload_bunfig = !compile_options.autoload_bunfig,
},
) catch |err| {
return bun.StandaloneModuleGraph.CompileResult.failFmt("{s}", .{@errorName(err)});
};
Expand Down
2 changes: 2 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ pub const Command = struct {
compile: bool = false,
compile_target: Cli.CompileTarget = .{},
compile_exec_argv: ?[]const u8 = null,
compile_autoload_dotenv: bool = true,
compile_autoload_bunfig: bool = true,
windows: options.WindowsOptions = .{},
};

Expand Down
40 changes: 40 additions & 0 deletions src/cli/Arguments.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ pub const build_only_params = [_]ParamType{
clap.parseParam("--production Set NODE_ENV=production and enable minification") catch unreachable,
clap.parseParam("--compile Generate a standalone Bun executable containing your bundled code. Implies --production") catch unreachable,
clap.parseParam("--compile-exec-argv <STR> Prepend arguments to the standalone executable's execArgv") catch unreachable,
clap.parseParam("--compile-autoload-dotenv Enable autoloading of .env files in standalone executable (default: true)") catch unreachable,
clap.parseParam("--no-compile-autoload-dotenv Disable autoloading of .env files in standalone executable") catch unreachable,
clap.parseParam("--compile-autoload-bunfig Enable autoloading of bunfig.toml in standalone executable (default: true)") catch unreachable,
clap.parseParam("--no-compile-autoload-bunfig Disable autoloading of bunfig.toml in standalone executable") catch unreachable,
clap.parseParam("--bytecode Use a bytecode cache") catch unreachable,
clap.parseParam("--watch Automatically restart the process on file change") catch unreachable,
clap.parseParam("--no-clear-screen Disable clearing the terminal screen on reload when --watch is enabled") catch unreachable,
Expand Down Expand Up @@ -1018,6 +1022,42 @@ pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: C
ctx.bundler_options.compile_exec_argv = compile_exec_argv;
}

// Handle --compile-autoload-dotenv flags
{
const has_positive = args.flag("--compile-autoload-dotenv");
const has_negative = args.flag("--no-compile-autoload-dotenv");

if (has_positive or has_negative) {
if (!ctx.bundler_options.compile) {
Output.errGeneric("--compile-autoload-dotenv requires --compile", .{});
Global.crash();
}
if (has_positive and has_negative) {
Output.errGeneric("Cannot use both --compile-autoload-dotenv and --no-compile-autoload-dotenv", .{});
Global.crash();
}
ctx.bundler_options.compile_autoload_dotenv = has_positive;
}
}

// Handle --compile-autoload-bunfig flags
{
const has_positive = args.flag("--compile-autoload-bunfig");
const has_negative = args.flag("--no-compile-autoload-bunfig");

if (has_positive or has_negative) {
if (!ctx.bundler_options.compile) {
Output.errGeneric("--compile-autoload-bunfig requires --compile", .{});
Global.crash();
}
if (has_positive and has_negative) {
Output.errGeneric("Cannot use both --compile-autoload-bunfig and --no-compile-autoload-bunfig", .{});
Global.crash();
}
ctx.bundler_options.compile_autoload_bunfig = has_positive;
}
}

if (args.flag("--windows-hide-console")) {
// --windows-hide-console technically doesnt depend on WinAPI, but since since --windows-icon
// does, all of these customization options have been gated to windows-only
Expand Down
4 changes: 4 additions & 0 deletions src/cli/build_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ pub const BuildCommand = struct {
ctx.bundler_options.windows,
ctx.bundler_options.compile_exec_argv orelse "",
null,
.{
.disable_default_env_files = !ctx.bundler_options.compile_autoload_dotenv,
.disable_autoload_bunfig = !ctx.bundler_options.compile_autoload_bunfig,
},
) catch |err| {
Output.printErrorln("failed to create executable: {s}", .{@errorName(err)});
Global.exit(1);
Expand Down
Loading