Compare commits

...

3 Commits

Author SHA1 Message Date
Claude Bot
93339929db Improve process.isBun DCE test to properly verify code elimination
- Use KEEP/REMOVE/DROP keywords to verify DCE actually removes dead code
- Test now fails on stable Bun (verified), passes with the new feature
- Follows the established DCE test conventions from expectBundled.md

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-14 18:00:36 +00:00
Claude Bot
89ae6487cf Remove comment and add test for process.isBun DCE
- Removed the explanatory comment as requested
- Added bundler test to ensure process.isBun DCE works when target=bun
- Test verifies that the Node.js branch is eliminated and only Bun code runs

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-14 17:55:27 +00:00
Claude Bot
f2b344dfd1 feat: enable DCE for process.isBun when target is bun
When bundling with --target=bun, process.isBun is now replaced with the literal value `true`, enabling dead code elimination for conditional code paths. This provides an easy way to write Bun-specific code that gets optimized away during bundling.

Example:
```js
if (process.isBun) {
  // This code is kept when --target=bun
  bunSpecificLogic();
} else {
  // This code is eliminated when --target=bun
  nodeSpecificLogic();
}
```

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-14 17:40:35 +00:00
2 changed files with 33 additions and 0 deletions

View File

@@ -1510,6 +1510,14 @@ pub fn definesFromTransformOptions(
.value = .{ .e_undefined = .{} },
}));
}
if (!user_defines.contains("process.isBun")) {
_ = try environment_defines.getOrPutValue("process.isBun", .init(.{
.valueless = false,
.original_name = "process.isBun",
.value = .{ .e_boolean = .{ .value = true } },
}));
}
}
const resolved_defines = try defines.DefineData.fromInput(user_defines, drop, log, allocator);

View File

@@ -3401,4 +3401,29 @@ describe("bundler", () => {
// "/project/node_modules/pkg/styles.css": `button { color: red }`,
// },
// });
itBundled("dce/ProcessIsBunTargetBun", {
files: {
"/entry.js": /* js */ `
if (process.isBun) {
console.log("KEEP_BUN_CODE");
function bunOnlyFunction() {
return "KEEP_THIS";
}
console.log(bunOnlyFunction());
} else {
console.log("REMOVE_NODE_CODE");
function nodeOnlyFunction() {
return "DROP_THIS";
}
console.log(nodeOnlyFunction());
}
`,
},
target: "bun",
dce: true,
run: {
stdout: "KEEP_BUN_CODE\nKEEP_THIS",
},
});
});