oxc_minify_napi 0.146.0

A collection of JavaScript tools written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use napi::Either;
use napi_derive::napi;
use rustc_hash::FxHashMap;

use oxc_compat::EngineTargets;
pub use oxc_napi::JsRegExp;
use oxc_str::CompactStr;

#[napi(object)]
pub struct TreeShakeOptions {
    /// Whether to respect the pure annotations.
    ///
    /// Pure annotations are comments that mark an expression as pure.
    /// For example: @__PURE__ or #__NO_SIDE_EFFECTS__.
    ///
    /// @default true
    pub annotations: Option<bool>,

    /// Whether to treat this function call as pure.
    ///
    /// This function is called for normal function calls, new calls, and
    /// tagged template calls.
    pub manual_pure_functions: Option<Vec<String>>,

    /// Whether property read accesses have side effects.
    ///
    /// @default 'always'
    #[napi(ts_type = "boolean | 'always'")]
    pub property_read_side_effects: Option<Either<bool, String>>,

    /// Whether property write accesses (assignments to member expressions) have side effects.
    ///
    /// When false, assignments like `obj.prop = value` are considered side-effect-free
    /// (assuming the object and value expressions themselves are side-effect-free).
    ///
    /// @default true
    pub property_write_side_effects: Option<bool>,

    /// Whether accessing a global variable has side effects.
    ///
    /// Accessing a non-existing global variable will throw an error.
    /// Global variable may be a getter that has side effects.
    ///
    /// @default true
    pub unknown_global_side_effects: Option<bool>,

    /// Whether invalid import statements have side effects.
    ///
    /// Accessing a non-existing import name will throw an error.
    /// Also import statements that cannot be resolved will throw an error.
    ///
    /// @default true
    pub invalid_import_side_effects: Option<bool>,
}

impl TryFrom<&TreeShakeOptions> for oxc_minifier::TreeShakeOptions {
    type Error = String;

    fn try_from(o: &TreeShakeOptions) -> Result<Self, Self::Error> {
        let default = oxc_minifier::TreeShakeOptions::default();
        Ok(oxc_minifier::TreeShakeOptions {
            annotations: o.annotations.unwrap_or(default.annotations),
            manual_pure_functions: o
                .manual_pure_functions
                .clone()
                .unwrap_or(default.manual_pure_functions),
            property_read_side_effects: match &o.property_read_side_effects {
                Some(Either::A(false)) => oxc_minifier::PropertyReadSideEffects::None,
                Some(Either::A(true)) => oxc_minifier::PropertyReadSideEffects::All,
                Some(Either::B(s)) if s == "always" => oxc_minifier::PropertyReadSideEffects::All,
                Some(Either::B(s)) => {
                    return Err(format!(
                        "Invalid propertyReadSideEffects value: '{s}'. Expected 'always'."
                    ));
                }
                None => default.property_read_side_effects,
            },
            property_write_side_effects: o
                .property_write_side_effects
                .unwrap_or(default.property_write_side_effects),
            unknown_global_side_effects: o
                .unknown_global_side_effects
                .unwrap_or(default.unknown_global_side_effects),
            invalid_import_side_effects: o
                .invalid_import_side_effects
                .unwrap_or(default.invalid_import_side_effects),
        })
    }
}

#[napi(object)]
pub struct CompressOptions {
    /// Set desired EcmaScript standard version for output.
    ///
    /// Set `esnext` to enable all target highering.
    ///
    /// Example:
    ///
    /// * `'es2015'`
    /// * `['es2020', 'chrome58', 'edge16', 'firefox57', 'node12', 'safari11']`
    ///
    /// @default 'esnext'
    ///
    /// @see [oxc#target](https://oxc.rs/docs/guide/usage/transformer/lowering#target)
    pub target: Option<Either<String, Vec<String>>>,

    /// Pass true to discard calls to `console.*`.
    ///
    /// @default false
    pub drop_console: Option<bool>,

    /// Remove `debugger;` statements.
    ///
    /// @default true
    pub drop_debugger: Option<bool>,

    /// Pass `true` to drop unreferenced functions and variables.
    ///
    /// Simple direct variable assignments do not count as references unless set to `keep_assign`.
    /// @default true
    #[napi(ts_type = "boolean | 'keep_assign'")]
    pub unused: Option<Either<bool, String>>,

    /// Keep function / class names.
    pub keep_names: Option<CompressOptionsKeepNames>,

    /// Join consecutive var, let and const statements.
    ///
    /// @default true
    pub join_vars: Option<bool>,

    /// Join consecutive simple statements using the comma operator.
    ///
    /// `a; b` -> `a, b`
    ///
    /// @default true
    pub sequences: Option<bool>,

    /// Set of label names to drop from the code.
    ///
    /// Labeled statements matching these names will be removed during minification.
    ///
    /// @default []
    pub drop_labels: Option<Vec<String>>,

    /// Limit the maximum number of iterations for debugging purpose.
    pub max_iterations: Option<u8>,

    /// Treeshake options.
    pub treeshake: Option<TreeShakeOptions>,
}

impl TryFrom<&CompressOptions> for oxc_minifier::CompressOptions {
    type Error = String;
    fn try_from(o: &CompressOptions) -> Result<Self, Self::Error> {
        let default = oxc_minifier::CompressOptions::default();
        Ok(oxc_minifier::CompressOptions {
            target: match &o.target {
                Some(Either::A(s)) => EngineTargets::from_target(s)?,
                Some(Either::B(list)) => EngineTargets::from_target_list(list)?,
                _ => default.target,
            },
            drop_console: o.drop_console.unwrap_or(default.drop_console),
            drop_debugger: o.drop_debugger.unwrap_or(default.drop_debugger),
            join_vars: o.join_vars.unwrap_or(true),
            sequences: o.sequences.unwrap_or(true),
            unused: match &o.unused {
                Some(Either::A(true)) => oxc_minifier::CompressOptionsUnused::Remove,
                Some(Either::A(false)) => oxc_minifier::CompressOptionsUnused::Keep,
                Some(Either::B(s)) => match s.as_str() {
                    "keep_assign" => oxc_minifier::CompressOptionsUnused::KeepAssign,
                    _ => return Err(format!("Invalid unused option: `{s}`.")),
                },
                None => default.unused,
            },
            keep_names: o.keep_names.as_ref().map(Into::into).unwrap_or_default(),
            treeshake: match &o.treeshake {
                Some(ts) => oxc_minifier::TreeShakeOptions::try_from(ts)?,
                None => oxc_minifier::TreeShakeOptions::default(),
            },
            drop_labels: o
                .drop_labels
                .as_ref()
                .map(|labels| labels.iter().cloned().collect())
                .unwrap_or_default(),
            max_iterations: o.max_iterations,
        })
    }
}

#[napi(object)]
pub struct CompressOptionsKeepNames {
    /// Keep function names so that `Function.prototype.name` is preserved.
    ///
    /// This does not guarantee that the `undefined` name is preserved.
    ///
    /// @default false
    pub function: bool,

    /// Keep class names so that `Class.prototype.name` is preserved.
    ///
    /// This does not guarantee that the `undefined` name is preserved.
    ///
    /// @default false
    pub class: bool,
}

impl From<&CompressOptionsKeepNames> for oxc_minifier::CompressOptionsKeepNames {
    fn from(o: &CompressOptionsKeepNames) -> Self {
        oxc_minifier::CompressOptionsKeepNames { function: o.function, class: o.class }
    }
}

#[napi(object)]
#[derive(Default)]
pub struct MangleOptions {
    /// Pass `true` to mangle names declared in the top level scope.
    ///
    /// @default true for modules and commonjs, otherwise false
    pub toplevel: Option<bool>,

    /// Preserve `name` property for functions and classes.
    ///
    /// @default false
    pub keep_names: Option<Either<bool, MangleOptionsKeepNames>>,

    /// Names that bindings must not be renamed to, and that bindings already
    /// carrying them keep. Equivalent to terser's `mangle.reserved`.
    ///
    /// Pass `['exports', 'module']` when minifying prebuilt CommonJS / UMD files
    /// that Node consumers `import` directly, so Node's cjs-module-lexer can still
    /// detect the mangled module's named exports.
    ///
    /// @default []
    pub reserved: Option<Vec<String>>,

    /// Debug mangled names.
    pub debug: Option<bool>,
}

impl From<&MangleOptions> for oxc_minifier::MangleOptions {
    fn from(o: &MangleOptions) -> Self {
        let default = oxc_minifier::MangleOptions::default();
        Self {
            top_level: o.toplevel,
            keep_names: match &o.keep_names {
                Some(Either::A(false)) => oxc_minifier::MangleOptionsKeepNames::all_false(),
                Some(Either::A(true)) => oxc_minifier::MangleOptionsKeepNames::all_true(),
                Some(Either::B(o)) => oxc_minifier::MangleOptionsKeepNames::from(o),
                None => default.keep_names,
            },
            reserved: o.reserved.as_ref().map_or(default.reserved, |names| {
                names.iter().map(|name| CompactStr::from(name.as_str())).collect()
            }),
            debug: o.debug.unwrap_or(default.debug),
        }
    }
}

#[napi(object)]
pub struct MangleOptionsKeepNames {
    /// Preserve `name` property for functions.
    ///
    /// @default false
    pub function: bool,

    /// Preserve `name` property for classes.
    ///
    /// @default false
    pub class: bool,
}

impl From<&MangleOptionsKeepNames> for oxc_minifier::MangleOptionsKeepNames {
    fn from(o: &MangleOptionsKeepNames) -> Self {
        oxc_minifier::MangleOptionsKeepNames { function: o.function, class: o.class }
    }
}

fn compile_js_reg_exp(regex: &JsRegExp, option_name: &str) -> Result<lazy_regex::Regex, String> {
    let error = |error| format!("Invalid mangleProps.{option_name} regex: {error}");
    if regex.flags.is_empty() {
        lazy_regex::Regex::new(&regex.source).map_err(error)
    } else {
        lazy_regex::Regex::new(&format!("(?{}){}", regex.flags, regex.source)).map_err(error)
    }
}

#[napi(object)]
pub struct ManglePropertiesOptions {
    /// JavaScript `RegExp` selecting property names to mangle. The source and flags are compiled
    /// with Rust's regex engine. Flags `i`, `m`, `s`, and `u` are supported.
    #[napi(ts_type = "RegExp")]
    pub include: JsRegExp,

    /// JavaScript `RegExp` excluding property names selected by `include`.
    #[napi(ts_type = "RegExp")]
    pub exclude: Option<JsRegExp>,

    /// Exact names that are neither mangled nor emitted as automatic output names.
    pub reserved: Option<Vec<String>>,

    /// Mangle quoted property occurrences in addition to unquoted occurrences.
    ///
    /// @default false
    pub quoted: Option<bool>,

    /// Generate readable `_$name$_`-style output names.
    ///
    /// @default false
    pub debug: Option<bool>,

    /// Stable mappings from original names to output names. `false` reserves an original name.
    /// Entries that do not match `include`, or that match `exclude`, remain inert but are
    /// preserved in the returned `mangleCache`. String targets must be `IdentifierName` values
    /// other than `__proto__`, `constructor`, or `prototype`. The original name `__proto__` is
    /// always reserved and cannot be used as a cache key.
    #[napi(ts_type = "Record<string, string | false>")]
    pub cache: Option<FxHashMap<String, Either<String, bool>>>,
}

impl TryFrom<&ManglePropertiesOptions> for oxc_minifier::ManglePropertiesOptions {
    type Error = String;

    fn try_from(options: &ManglePropertiesOptions) -> Result<Self, Self::Error> {
        let include = compile_js_reg_exp(&options.include, "include")?;
        let exclude = options
            .exclude
            .as_ref()
            .map(|regex| compile_js_reg_exp(regex, "exclude"))
            .transpose()?;
        let mut cache = oxc_minifier::ManglePropertyCache::default();
        if let Some(entries) = &options.cache {
            for (original, value) in entries {
                if original == "__proto__" {
                    return Err(
                        "Invalid mangleProps.cache key '__proto__': this original name cannot be used as a cache key"
                            .to_string(),
                    );
                }
                let target = match value {
                    Either::A(target) => Some(CompactStr::from(target.as_str())),
                    Either::B(false) => None,
                    Either::B(true) => {
                        return Err(format!(
                            "Invalid mangleProps.cache value for '{original}': expected a string or false"
                        ));
                    }
                };
                cache
                    .insert(CompactStr::from(original.as_str()), target)
                    .map_err(|error| format!("Invalid mangleProps.cache: {error}"))?;
            }
        }

        Ok(Self {
            include,
            exclude,
            reserved: options.reserved.as_ref().map_or_else(Default::default, |names| {
                names.iter().map(|name| CompactStr::from(name.as_str())).collect()
            }),
            mangle_quoted: options.quoted.unwrap_or(false),
            debug: options.debug.unwrap_or(false),
            cache,
        })
    }
}

#[napi(string_enum = "lowercase")]
pub enum LegalCommentsMode {
    /// Do not preserve any legal comments.
    None,
    /// Preserve all legal comments inline.
    Inline,
    /// Move all legal comments to the end of the file.
    Eof,
    /// Extract legal comments without linking.
    External,
}

#[napi(object)]
pub struct LegalCommentsLinked {
    /// Extract legal comments and write them to the given path, with a link
    /// comment appended to the generated code.
    pub linked: String,
}

#[napi(object)]
pub struct CodegenOptions {
    /// Remove whitespace.
    ///
    /// @default true
    pub remove_whitespace: Option<bool>,

    /// How to handle legal comments (comments containing `@license`, `@preserve`, or starting with `//!`/`/*!`).
    ///
    /// * `"none"` - Do not preserve any legal comments.
    /// * `"inline"` - Preserve all legal comments inline.
    /// * `"eof"` - Move all legal comments to the end of the file.
    /// * `"external"` - Extract legal comments without linking.
    /// * `{ linked: "path/to/legal.txt" }` - Extract legal comments and add a link comment to the given path.
    ///
    /// @default "none" (when minifying)
    #[napi(ts_type = "'none' | 'inline' | 'eof' | 'external' | { linked: string }")]
    pub legal_comments: Option<Either<LegalCommentsMode, LegalCommentsLinked>>,
}

impl Default for CodegenOptions {
    fn default() -> Self {
        Self { remove_whitespace: Some(true), legal_comments: None }
    }
}

impl CodegenOptions {
    /// Convert N-API codegen options into codegen options.
    ///
    /// # Errors
    ///
    /// Returns an error if the `linked` variant is given an empty path.
    pub fn to_codegen_options(&self) -> Result<oxc_codegen::CodegenOptions, String> {
        let mut opts = if self.remove_whitespace.unwrap_or(true) {
            oxc_codegen::CodegenOptions::minify()
        } else {
            // Need to remove all comments.
            oxc_codegen::CodegenOptions { minify: false, ..oxc_codegen::CodegenOptions::minify() }
        };

        if let Some(legal) = &self.legal_comments {
            opts.comments.legal = match legal {
                Either::A(mode) => match mode {
                    LegalCommentsMode::None => oxc_codegen::LegalComment::None,
                    LegalCommentsMode::Inline => oxc_codegen::LegalComment::Inline,
                    LegalCommentsMode::Eof => oxc_codegen::LegalComment::Eof,
                    LegalCommentsMode::External => oxc_codegen::LegalComment::External,
                },
                Either::B(linked) => {
                    if linked.linked.is_empty() {
                        return Err("legalComments.linked must be a non-empty path.".into());
                    }
                    oxc_codegen::LegalComment::Linked(linked.linked.clone())
                }
            };
        }

        Ok(opts)
    }
}

#[napi(object)]
#[derive(Default)]
pub struct MinifyOptions {
    /// Use when minifying an ES module.
    pub module: Option<bool>,

    pub compress: Option<Either<bool, CompressOptions>>,

    pub mangle: Option<Either<bool, MangleOptions>>,

    /// Mangle matching property names independently of identifier mangling. Properties owned by
    /// unminified code, imported module namespaces, globals, or host APIs must be excluded or
    /// reserved.
    pub mangle_props: Option<ManglePropertiesOptions>,

    pub codegen: Option<Either<bool, CodegenOptions>>,

    pub sourcemap: Option<bool>,
}

impl TryFrom<&MinifyOptions> for oxc_minifier::MinifierOptions {
    type Error = String;

    fn try_from(o: &MinifyOptions) -> Result<Self, Self::Error> {
        let compress = match &o.compress {
            Some(Either::A(false)) => None,
            None | Some(Either::A(true)) => Some(oxc_minifier::CompressOptions::default()),
            Some(Either::B(o)) => Some(oxc_minifier::CompressOptions::try_from(o)?),
        };
        let mangle = match &o.mangle {
            Some(Either::A(false)) => None,
            None | Some(Either::A(true)) => Some(oxc_minifier::MangleOptions::default()),
            Some(Either::B(o)) => Some(oxc_minifier::MangleOptions::from(o)),
        };
        let mangle_properties = o
            .mangle_props
            .as_ref()
            .map(oxc_minifier::ManglePropertiesOptions::try_from)
            .transpose()?;
        Ok(oxc_minifier::MinifierOptions { compress, mangle, mangle_properties })
    }
}