Skip to content

Commit c77fc5d

Browse files
Implement --drop (#14492)
Co-authored-by: dave caruso <me@paperdave.net>
1 parent bbb41be commit c77fc5d

26 files changed

Lines changed: 362 additions & 143 deletions

docs/bundler/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,26 @@ $ bun build ./index.tsx --outdir ./out --footer="// built with love in SF"
11301130

11311131
{% /codetabs %}
11321132

1133+
### `drop`
1134+
1135+
Remove function calls from a bundle. For example, `--drop=console` will remove all calls to `console.log`. Arguments to calls will also be removed, regardless of if those arguments may have side effects. Dropping `debugger` will remove all `debugger` statements.
1136+
1137+
{% codetabs %}
1138+
1139+
```ts#JavaScript
1140+
await Bun.build({
1141+
entrypoints: ['./index.tsx'],
1142+
outdir: './out',
1143+
drop: ["console", "debugger", "anyIdentifier.or.propertyAccess"],
1144+
})
1145+
```
1146+
1147+
```bash#CLI
1148+
$ bun build ./index.tsx --outdir ./out --drop=console --drop=debugger --drop=anyIdentifier.or.propertyAccess
1149+
```
1150+
1151+
{% /codetabs %}
1152+
11331153
### `experimentalCss`
11341154

11351155
Whether to enable _experimental_ support for bundling CSS files. Defaults to `false`.

docs/bundler/vs-esbuild.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
190190
---
191191

192192
- `--drop`
193-
- n/a
194-
- Not supported
193+
- `--drop`
195194

196195
---
197196

packages/bun-types/bun.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,11 @@ declare module "bun" {
16121612
* Enable CSS support.
16131613
*/
16141614
experimentalCss?: boolean;
1615+
1616+
/**
1617+
* Drop function calls to matching property accesses.
1618+
*/
1619+
drop?: string[];
16151620
}
16161621

16171622
namespace Password {

src/api/schema.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,8 @@ pub const Api = struct {
16351635
/// define
16361636
define: ?StringMap = null,
16371637

1638+
drop: []const []const u8 = &.{},
1639+
16381640
/// preserve_symlinks
16391641
preserve_symlinks: ?bool = null,
16401642

src/bun.js/api/BunObject.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub fn inspect(
590590

591591
// we are going to always clone to keep things simple for now
592592
// the common case here will be stack-allocated, so it should be fine
593-
var out = ZigString.init(array.toOwnedSliceLeaky()).withEncoding();
593+
var out = ZigString.init(array.slice()).withEncoding();
594594
const ret = out.toJS(globalThis);
595595
array.deinit();
596596
return ret;
@@ -3932,7 +3932,7 @@ const TOMLObject = struct {
39323932
return .zero;
39333933
};
39343934

3935-
const slice = writer.ctx.buffer.toOwnedSliceLeaky();
3935+
const slice = writer.ctx.buffer.slice();
39363936
var out = bun.String.fromUTF8(slice);
39373937
defer out.deref();
39383938

src/bun.js/api/JSBundler.zig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub const JSBundler = struct {
7575
banner: OwnedString = OwnedString.initEmpty(bun.default_allocator),
7676
footer: OwnedString = OwnedString.initEmpty(bun.default_allocator),
7777
experimental_css: bool = false,
78+
drop: bun.StringSet = bun.StringSet.init(bun.default_allocator),
7879

7980
pub const List = bun.StringArrayHashMapUnmanaged(Config);
8081

@@ -191,7 +192,6 @@ pub const JSBundler = struct {
191192
try this.banner.appendSliceExact(slice.slice());
192193
}
193194

194-
195195
if (try config.getOptional(globalThis, "footer", ZigString.Slice)) |slice| {
196196
defer slice.deinit();
197197
try this.footer.appendSliceExact(slice.slice());
@@ -351,6 +351,18 @@ pub const JSBundler = struct {
351351
}
352352
}
353353

354+
if (try config.getOwnArray(globalThis, "drop")) |drops| {
355+
var iter = drops.arrayIterator(globalThis);
356+
while (iter.next()) |entry| {
357+
var slice = entry.toSliceOrNull(globalThis) orelse {
358+
globalThis.throwInvalidArguments("Expected drop to be an array of strings", .{});
359+
return error.JSError;
360+
};
361+
defer slice.deinit();
362+
try this.drop.insert(slice.slice());
363+
}
364+
}
365+
354366
// if (try config.getOptional(globalThis, "dir", ZigString.Slice)) |slice| {
355367
// defer slice.deinit();
356368
// this.appendSliceExact(slice.slice()) catch unreachable;
@@ -544,6 +556,9 @@ pub const JSBundler = struct {
544556
self.rootdir.deinit();
545557
self.public_path.deinit();
546558
self.conditions.deinit();
559+
self.drop.deinit();
560+
self.banner.deinit();
561+
self.footer.deinit();
547562
}
548563
};
549564

src/bun.js/api/html_rewriter.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ pub const HTMLRewriter = struct {
710710

711711
// pub fn done(this: *StreamOutputSink) void {
712712
// var prev_value = this.response.body.value;
713-
// var bytes = this.bytes.toOwnedSliceLeaky();
713+
// var bytes = this.bytes.slice();
714714
// this.response.body.value = .{
715715
// .Blob = JSC.WebCore.Blob.init(bytes, this.bytes.allocator, this.global),
716716
// };

src/bun.js/bindings/bindings.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ pub const JSGlobalObject = opaque {
31293129
return ZigString.static(fmt).toErrorInstance(this);
31303130

31313131
// Ensure we clone it.
3132-
var str = ZigString.initUTF8(buf.toOwnedSliceLeaky());
3132+
var str = ZigString.initUTF8(buf.slice());
31333133

31343134
return str.toErrorInstance(this);
31353135
} else {
@@ -3148,7 +3148,7 @@ pub const JSGlobalObject = opaque {
31483148
defer buf.deinit();
31493149
var writer = buf.writer();
31503150
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
3151-
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
3151+
var str = ZigString.fromUTF8(buf.slice());
31523152
return str.toTypeErrorInstance(this);
31533153
} else {
31543154
return ZigString.static(fmt).toTypeErrorInstance(this);
@@ -3162,7 +3162,7 @@ pub const JSGlobalObject = opaque {
31623162
defer buf.deinit();
31633163
var writer = buf.writer();
31643164
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
3165-
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
3165+
var str = ZigString.fromUTF8(buf.slice());
31663166
return str.toSyntaxErrorInstance(this);
31673167
} else {
31683168
return ZigString.static(fmt).toSyntaxErrorInstance(this);
@@ -3176,7 +3176,7 @@ pub const JSGlobalObject = opaque {
31763176
defer buf.deinit();
31773177
var writer = buf.writer();
31783178
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
3179-
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
3179+
var str = ZigString.fromUTF8(buf.slice());
31803180
return str.toRangeErrorInstance(this);
31813181
} else {
31823182
return ZigString.static(fmt).toRangeErrorInstance(this);
@@ -4619,7 +4619,7 @@ pub const JSValue = enum(JSValueReprInt) {
46194619

46204620
var writer = buf.writer();
46214621
try writer.print(fmt, args);
4622-
return String.init(buf.toOwnedSliceLeaky()).toJS(globalThis);
4622+
return String.init(buf.slice()).toJS(globalThis);
46234623
}
46244624

46254625
/// Create a JSValue string from a zig format-print (fmt + args), with pretty format
@@ -4633,7 +4633,7 @@ pub const JSValue = enum(JSValueReprInt) {
46334633
switch (Output.enable_ansi_colors) {
46344634
inline else => |enabled| try writer.print(Output.prettyFmt(fmt, enabled), args),
46354635
}
4636-
return String.init(buf.toOwnedSliceLeaky()).toJS(globalThis);
4636+
return String.init(buf.slice()).toJS(globalThis);
46374637
}
46384638

46394639
pub fn fromEntries(globalThis: *JSGlobalObject, keys_array: [*c]ZigString, values_array: [*c]ZigString, strings_count: usize, clone: bool) JSValue {

src/bun.js/module_loader.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,7 @@ pub const ModuleLoader = struct {
21432143
writer.writeAll(";\n") catch bun.outOfMemory();
21442144
}
21452145

2146-
const public_url = bun.String.createUTF8(buf.toOwnedSliceLeaky());
2146+
const public_url = bun.String.createUTF8(buf.slice());
21472147
return ResolvedSource{
21482148
.allocator = &jsc_vm.allocator,
21492149
.source_code = public_url,

src/bun.js/test/diff_format.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ pub const DiffFormatter = struct {
129129
buffered_writer.flush() catch unreachable;
130130
}
131131

132-
const received_slice = received_buf.toOwnedSliceLeaky();
133-
const expected_slice = expected_buf.toOwnedSliceLeaky();
132+
const received_slice = received_buf.slice();
133+
const expected_slice = expected_buf.slice();
134134

135135
if (this.not) {
136136
const not_fmt = "Expected: not <green>{s}<r>";

0 commit comments

Comments
 (0)