Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
d8c6cfff97 Disable RuntimeTranspilerCache when --define flag is used
When user-provided defines are passed via --define, the transpiler cache
is now disabled. This is necessary because defines affect transpilation
output, and the cache doesn't account for different define values.

Implementation:
- Added has_user_defines boolean to BundlerOptions in options.zig
- Added has_user_defines boolean to Runtime.Features
- Added has_user_defines boolean to Cli.BundlerOptions
- Added has_user_defines boolean to Transpiler.ParseOptions
- Set has_user_defines in loadDefines() when transform_options.define has keys
- Pass has_user_defines through ParseOptions to Runtime.Features in transpiler.zig
- Modified RuntimeTranspilerStore to:
  - Set has_user_defines in ParseOptions
  - Disable cache when has_user_defines is true
- Added test to verify cache is disabled with --define flag

The has_user_defines boolean flows through the chain:
BundlerOptions -> ParseOptions -> Runtime.Features -> Parser

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 23:10:44 +00:00
6 changed files with 56 additions and 1 deletions

View File

@@ -392,7 +392,8 @@ pub const RuntimeTranspilerStore = struct {
vm.debugger.?.set_breakpoint_on_first_line and
is_main and
setBreakPointOnFirstLine(),
.runtime_transpiler_cache = if (!jsc.RuntimeTranspilerCache.is_disabled) &cache else null,
.has_user_defines = transpiler.options.has_user_defines,
.runtime_transpiler_cache = if (!jsc.RuntimeTranspilerCache.is_disabled and !transpiler.options.has_user_defines) &cache else null,
.remove_cjs_module_wrapper = is_main and vm.module_loader.eval_source != null,
.module_type = module_type,
.allow_bytecode_cache = true,

View File

@@ -459,6 +459,10 @@ pub const Command = struct {
compile_target: Cli.CompileTarget = .{},
compile_exec_argv: ?[]const u8 = null,
windows: options.WindowsOptions = .{},
/// Set to true when user-provided defines (via --define) are present.
/// Used to disable RuntimeTranspilerCache since defines affect transpilation output.
has_user_defines: bool = false,
};
pub fn create(allocator: std.mem.Allocator, log: *logger.Log, comptime command: Command.Tag) anyerror!Context {

View File

@@ -1724,6 +1724,10 @@ pub const BundleOptions = struct {
auto_import_jsx: bool = true,
allow_runtime: bool = true,
/// Set to true when user-provided defines (via --define) are present.
/// Disables RuntimeTranspilerCache since defines affect transpilation output.
has_user_defines: bool = false,
trim_unused_imports: ?bool = null,
mark_builtins_as_external: bool = false,
server_components: bool = false,
@@ -1879,6 +1883,12 @@ pub const BundleOptions = struct {
if (this.defines_loaded) {
return;
}
// Check if user provided defines via --define flag
if (this.transform_options.define) |user_defines| {
this.has_user_defines = user_defines.keys.len > 0;
}
this.define = try definesFromTransformOptions(
allocator,
this.log,

View File

@@ -211,6 +211,10 @@ pub const Runtime = struct {
// TODO: make this a bitset of all unsupported features
lower_using: bool = true,
/// Set to true when user-provided defines (via --define) are present.
/// Disables RuntimeTranspilerCache since defines affect transpilation output.
has_user_defines: bool = false,
const hash_fields_for_runtime_transpiler = .{
.top_level_await,
.auto_import_jsx,

View File

@@ -960,6 +960,10 @@ pub const Transpiler = struct {
keep_json_and_toml_as_one_statement: bool = false,
allow_bytecode_cache: bool = false,
/// Set to true when user-provided defines (via --define) are present.
/// Disables RuntimeTranspilerCache since defines affect transpilation output.
has_user_defines: bool = false,
};
pub fn parse(
@@ -1112,6 +1116,7 @@ pub const Transpiler = struct {
opts.features.minify_identifiers = transpiler.options.minify_identifiers;
opts.features.dead_code_elimination = transpiler.options.dead_code_elimination;
opts.features.remove_cjs_module_wrapper = this_parse.remove_cjs_module_wrapper;
opts.features.has_user_defines = this_parse.has_user_defines;
if (transpiler.macro_context == null) {
transpiler.macro_context = js_ast.Macro.MacroContext.init(transpiler);

View File

@@ -186,4 +186,35 @@ describe("transpiler cache", () => {
expect(b.stdout == "production 5");
expect(newCacheCount()).toBe(0);
});
test("is disabled when --define is used", () => {
writeFileSync(join(temp_dir, "a.js"), dummyFile((50 * 1024 * 1.5) | 0, "1", { code: "MY_DEFINE" }));
// First run with --define should work but not cache
const proc1 = Bun.spawnSync({
cmd: [bunExe(), "--define", "MY_DEFINE=123", join(temp_dir, "a.js")],
env,
cwd: temp_dir,
});
expect(proc1.stdout.toString().trim()).toBe("123");
// Cache should not be created when --define is used
expect(!existsSync(cache_dir) || newCacheCount() === 0).toBeTrue();
// Second run with same --define should also not use cache
const proc2 = Bun.spawnSync({
cmd: [bunExe(), "--define", "MY_DEFINE=123", join(temp_dir, "a.js")],
env,
cwd: temp_dir,
});
expect(proc2.stdout.toString().trim()).toBe("123");
expect(!existsSync(cache_dir) || newCacheCount() === 0).toBeTrue();
// Run with different --define value should give different output
const proc3 = Bun.spawnSync({
cmd: [bunExe(), "--define", "MY_DEFINE=456", join(temp_dir, "a.js")],
env,
cwd: temp_dir,
});
expect(proc3.stdout.toString().trim()).toBe("456");
expect(!existsSync(cache_dir) || newCacheCount() === 0).toBeTrue();
});
});