Skip to content

Commit 9e36186

Browse files
committed
feat(napi/minify): expose drop_labels option (#14635)
Exposed the `drop_labels` option from JS API.
1 parent d09c7ee commit 9e36186

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

napi/minify/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ oxc_span = { workspace = true }
3434

3535
napi = { workspace = true }
3636
napi-derive = { workspace = true }
37-
rustc-hash = { workspace = true }
3837

3938
[target.'cfg(not(any(target_os = "linux", target_os = "freebsd", target_arch = "arm", target_family = "wasm")))'.dependencies]
4039
mimalloc-safe = { workspace = true, optional = true, features = ["skip_collect_on_exit"] }

napi/minify/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ export interface CompressOptions {
6060
* @default true
6161
*/
6262
sequences?: boolean
63+
/**
64+
* Set of label names to drop from the code.
65+
*
66+
* Labeled statements matching these names will be removed during minification.
67+
*
68+
* @default []
69+
*/
70+
dropLabels?: Array<string>
6371
/** Limit the maximum number of iterations for debugging purpose. */
6472
maxIterations?: number
6573
}

napi/minify/src/options.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use napi_derive::napi;
33

44
use oxc_compat::EngineTargets;
55
use oxc_minifier::TreeShakeOptions;
6-
use rustc_hash::FxHashSet;
76

87
#[napi(object)]
98
pub struct CompressOptions {
@@ -53,6 +52,13 @@ pub struct CompressOptions {
5352
/// @default true
5453
pub sequences: Option<bool>,
5554

55+
/// Set of label names to drop from the code.
56+
///
57+
/// Labeled statements matching these names will be removed during minification.
58+
///
59+
/// @default []
60+
pub drop_labels: Option<Vec<String>>,
61+
5662
/// Limit the maximum number of iterations for debugging purpose.
5763
pub max_iterations: Option<u8>,
5864
}
@@ -82,7 +88,11 @@ impl TryFrom<&CompressOptions> for oxc_minifier::CompressOptions {
8288
},
8389
keep_names: o.keep_names.as_ref().map(Into::into).unwrap_or_default(),
8490
treeshake: TreeShakeOptions::default(),
85-
drop_labels: FxHashSet::default(),
91+
drop_labels: o
92+
.drop_labels
93+
.as_ref()
94+
.map(|labels| labels.iter().cloned().collect())
95+
.unwrap_or_default(),
8696
max_iterations: o.max_iterations,
8797
})
8898
}

napi/minify/test/minify.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ describe('simple', () => {
4343
expect(ret.code).toBe('');
4444
expect(ret.errors.length).toBe(1);
4545
});
46+
47+
it('supports drop_labels option', () => {
48+
const code = 'PURE: { foo(); bar(); } OTHER: { baz(); }';
49+
const ret = minify('test.js', code, {
50+
compress: { dropLabels: ['PURE'] },
51+
});
52+
expect(ret.code).toBe('OTHER:baz();');
53+
expect(ret.errors.length).toBe(0);
54+
});
4655
});
4756

4857
describe('worker', () => {

0 commit comments

Comments
 (0)