Is --drop intended to only work with builtins? --drop does not remove a function I created myself. #14960
Replies: 3 comments 1 reply
|
i too would like to know. i've tested this thoroughly and haven't found a way to get bun to drop anything that is actually defined, which seems to make the feature only marginally useful |
|
Digging this up with some recent findings, though no solutions as of yet. the --drop command seems to only target things defined by source: function test() {
throw new Error("test");
}
const _x = {
test
};
globalThis.x = _x;
test();
_x.test();
x.test();command: bun build test.ts --drop="x" --drop="_x" --drop="test"output: // test.ts
function test() {
throw new Error("test");
}
var _x = {
test
};
globalThis.x = _x;
test();
_x.test();Notice there's no |
|
Based on the current docs and the behavior in your example, That explains this result: test() // local binding, kept
_x.test() // local object binding, kept
x.test() // global/property access, dropped when --drop=xThe docs example is For custom debug-only code, I would use a compile-time flag instead: if (DEBUG) {
test()
}and build with: bun build index.ts --define DEBUG=false --minifyor put debug helpers behind an object you only use for that purpose: globalThis.__debug?.test()then drop that property path if it matches your bundle shape. If the desired behavior is “remove calls to this local function and then tree-shake the function declaration,” that is more of a feature request for Bun's bundler than something |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Regarding --drop paramater. Documentation:
I was experimenting with --drop trying to remove a "debug-only" function, but noticed that "custom" functions will not be removed.
Is --drop intended to work only with/for builtins?
index.ts:
cmd:
bun build index.ts --drop="console" --drop="test" --drop="Bun" --drop="window"output:
All reactions