-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathoptions.rs
More file actions
168 lines (143 loc) · 4.71 KB
/
options.rs
File metadata and controls
168 lines (143 loc) · 4.71 KB
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
use std::str::FromStr;
use napi::Either;
use napi_derive::napi;
use oxc_sourcemap::napi::SourceMap;
use oxc_syntax::es_target::ESTarget;
#[napi(object)]
pub struct CompressOptions {
/// Set desired EcmaScript standard version for output.
///
/// Set `esnext` to enable all target highering.
///
/// e.g.
///
/// * catch optional binding when >= es2019
/// * `??` operator >= es2020
///
/// @default 'esnext'
#[napi(
ts_type = "'esnext' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'es2023' | 'es2024'"
)]
pub target: Option<String>,
/// Keep function / class names.
pub keep_names: Option<CompressOptionsKeepNames>,
/// Pass true to discard calls to `console.*`.
///
/// @default false
pub drop_console: Option<bool>,
/// Remove `debugger;` statements.
///
/// @default true
pub drop_debugger: Option<bool>,
}
impl Default for CompressOptions {
fn default() -> Self {
Self { target: None, keep_names: None, drop_console: None, drop_debugger: Some(true) }
}
}
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: o
.target
.as_ref()
.map(|s| ESTarget::from_str(s))
.transpose()?
.unwrap_or(default.target),
keep_names: o.keep_names.as_ref().map(Into::into).unwrap_or_default(),
drop_console: o.drop_console.unwrap_or(default.drop_console),
drop_debugger: o.drop_debugger.unwrap_or(default.drop_debugger),
})
}
}
#[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 false
pub toplevel: Option<bool>,
/// 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.unwrap_or(default.top_level),
debug: o.debug.unwrap_or(default.debug),
}
}
}
#[napi(object)]
pub struct CodegenOptions {
/// Remove whitespace.
///
/// @default true
pub remove_whitespace: Option<bool>,
}
impl Default for CodegenOptions {
fn default() -> Self {
Self { remove_whitespace: Some(true) }
}
}
impl From<&CodegenOptions> for oxc_codegen::CodegenOptions {
fn from(o: &CodegenOptions) -> Self {
let default = oxc_codegen::CodegenOptions::default();
oxc_codegen::CodegenOptions {
minify: o.remove_whitespace.unwrap_or(default.minify),
..default
}
}
}
#[napi(object)]
#[derive(Default)]
pub struct MinifyOptions {
pub compress: Option<Either<bool, CompressOptions>>,
pub mangle: Option<Either<bool, MangleOptions>>,
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)),
};
Ok(oxc_minifier::MinifierOptions { compress, mangle })
}
}
#[napi(object)]
pub struct MinifyResult {
pub code: String,
pub map: Option<SourceMap>,
}