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
use std::path::PathBuf;
use oxc_data_structures::code_buffer::{DEFAULT_INDENT_WIDTH, IndentChar};
/// Codegen Options.
#[derive(Debug, Clone)]
pub struct CodegenOptions {
/// Use single quotes instead of double quotes.
///
/// Default is `false`.
pub single_quote: bool,
/// Remove whitespace.
///
/// Default is `false`.
pub minify: bool,
/// Escape non-ASCII characters in string literals, untagged template literals, regular
/// expression literals and identifier names.
///
/// Uses `\uXXXX` for characters up to U+FFFF and `\u{...}` for higher code points.
/// Regular expressions use escaped UTF-16 surrogate pairs for higher code points instead;
/// escaping changes the observable `RegExp.prototype.source` value.
///
/// Code point escapes (`\u{...}`) require ES2015 or later; this option does not provide
/// ES5-compatible output.
///
/// Non-ASCII characters are left unescaped in tagged template quasis (whose raw text is
/// observable), JSX names and text, JSX attribute strings, hashbangs and preserved comments.
/// JavaScript expressions inside tagged templates and JSX are escaped normally.
///
/// Default is `false`.
pub ascii_only: bool,
/// Print comments?
///
/// At present, only some leading comments are preserved.
///
/// Default is [CommentOptions::default].
pub comments: CommentOptions,
/// Enable sourcemap.
///
/// The provided path sets the `source` field in the returned sourcemap.
///
/// Default is `None` - no sourcemap is produced.
pub source_map_path: Option<PathBuf>,
/// Indentation character.
///
/// Default is [`IndentChar::Tab`].
pub indent_char: IndentChar,
/// Number of characters per indentation level.
///
/// Default is `1`.
pub indent_width: usize,
/// Initial indentation level for generated code.
///
/// Default is `0`.
pub initial_indent: u32,
}
impl Default for CodegenOptions {
fn default() -> Self {
Self {
single_quote: false,
minify: false,
ascii_only: false,
comments: CommentOptions::default(),
source_map_path: None,
indent_char: IndentChar::default(),
indent_width: DEFAULT_INDENT_WIDTH,
initial_indent: 0,
}
}
}
impl CodegenOptions {
/// Minify whitespace and remove comments.
pub fn minify() -> Self {
Self {
single_quote: false,
minify: true,
ascii_only: false,
comments: CommentOptions::disabled(),
source_map_path: None,
indent_char: IndentChar::default(),
indent_width: DEFAULT_INDENT_WIDTH,
initial_indent: 0,
}
}
#[inline]
pub(crate) fn print_normal_comment(&self) -> bool {
self.comments.normal
}
#[inline]
pub(crate) fn print_legal_comment(&self) -> bool {
self.comments.legal.is_inline()
}
#[inline]
pub(crate) fn print_jsdoc_comment(&self) -> bool {
self.comments.jsdoc
}
#[inline]
pub(crate) fn print_annotation_comment(&self) -> bool {
self.comments.annotation
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Comment Options
pub struct CommentOptions {
/// Print normal comments that do not have special meanings.
///
/// At present only statement level comments are printed.
///
/// Default is `true`.
pub normal: bool,
/// Print jsdoc comments.
///
/// * jsdoc: `/** jsdoc */`
///
/// Default is `true`.
pub jsdoc: bool,
/// Print annotation comments.
///
/// * pure: `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`
/// * webpack: `/* webpackChunkName */`
/// * vite: `/* @vite-ignore */`
/// * property key: `/* @__KEY__ */` and `/* #__KEY__ */`
/// * coverage: `v8 ignore`, `c8 ignore`, `node:coverage`, `istanbul ignore`
///
/// Default is `true`.
pub annotation: bool,
/// Print legal comments.
///
/// * starts with `//!` or `/*!`.
/// * contains `/* @license */` or `/* @preserve */`
///
/// Default is [`LegalComment::Inline`].
pub legal: LegalComment,
}
impl Default for CommentOptions {
fn default() -> Self {
Self { normal: true, jsdoc: true, annotation: true, legal: LegalComment::default() }
}
}
impl CommentOptions {
/// Disable Comments.
pub fn disabled() -> Self {
Self { normal: false, jsdoc: false, annotation: false, legal: LegalComment::None }
}
}
/// Legal comment
///
/// <https://esbuild.github.io/api/#legal-comments>
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub enum LegalComment {
/// Do not preserve any legal comments.
None,
/// Preserve all legal comments (default).
#[default]
Inline,
/// Move all legal comments to the end of the file.
Eof,
/// Return all legal comments and link then to them with a comment to the provided string.
Linked(String),
/// Move all legal comments to a .LEGAL.txt file but to not link to them.
External,
}
impl LegalComment {
/// Is None.
pub fn is_none(&self) -> bool {
*self == Self::None
}
/// Is inline mode.
pub fn is_inline(&self) -> bool {
*self == Self::Inline
}
/// Is EOF mode.
pub fn is_eof(&self) -> bool {
*self == Self::Eof
}
}