forked from vercel/next.js
/
lib.rs
239 lines (209 loc) · 7.25 KB
/
lib.rs
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
/*
Copyright (c) 2017 The swc Project Developers
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#![recursion_limit = "2048"]
#![deny(clippy::all)]
use auto_cjs::contains_cjs;
use either::Either;
use fxhash::FxHashSet;
use serde::Deserialize;
use std::cell::RefCell;
use std::rc::Rc;
use std::{path::PathBuf, sync::Arc};
use swc_core::{
base::config::ModuleConfig,
common::{chain, comments::Comments, pass::Optional, FileName, SourceFile, SourceMap},
ecma::ast::EsVersion,
ecma::parser::parse_file_as_module,
ecma::transforms::base::pass::noop,
ecma::visit::Fold,
};
pub mod amp_attributes;
mod auto_cjs;
pub mod disallow_re_export_all_in_page;
pub mod hook_optimizer;
pub mod next_dynamic;
pub mod next_ssg;
pub mod page_config;
pub mod react_remove_properties;
pub mod react_server_components;
#[cfg(not(target_arch = "wasm32"))]
pub mod relay;
pub mod remove_console;
pub mod shake_exports;
mod top_level_binding_collector;
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransformOptions {
#[serde(flatten)]
pub swc: swc_core::base::config::Options,
#[serde(default)]
pub disable_next_ssg: bool,
#[serde(default)]
pub disable_page_config: bool,
#[serde(default)]
pub pages_dir: Option<PathBuf>,
#[serde(default)]
pub is_page_file: bool,
#[serde(default)]
pub is_development: bool,
#[serde(default)]
pub is_server: bool,
#[serde(default)]
pub server_components: Option<react_server_components::Config>,
#[serde(default)]
pub styled_components: Option<styled_components::Config>,
#[serde(default)]
pub remove_console: Option<remove_console::Config>,
#[serde(default)]
pub react_remove_properties: Option<react_remove_properties::Config>,
#[serde(default)]
#[cfg(not(target_arch = "wasm32"))]
pub relay: Option<relay::Config>,
#[serde(default)]
pub shake_exports: Option<shake_exports::Config>,
#[serde(default)]
pub emotion: Option<swc_emotion::EmotionOptions>,
#[serde(default)]
pub modularize_imports: Option<modularize_imports::Config>,
}
pub fn custom_before_pass<'a, C: Comments + 'a>(
cm: Arc<SourceMap>,
file: Arc<SourceFile>,
opts: &'a TransformOptions,
comments: C,
eliminated_packages: Rc<RefCell<FxHashSet<String>>>,
) -> impl Fold + 'a
where
C: Clone,
{
#[cfg(target_arch = "wasm32")]
let relay_plugin = noop();
#[cfg(not(target_arch = "wasm32"))]
let relay_plugin = {
if let Some(config) = &opts.relay {
Either::Left(relay::relay(
config,
file.name.clone(),
opts.pages_dir.clone(),
))
} else {
Either::Right(noop())
}
};
chain!(
disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file),
match &opts.server_components {
Some(config) if config.truthy() =>
Either::Left(react_server_components::server_components(
file.name.clone(),
config.clone(),
comments.clone(),
)),
_ => Either::Right(noop()),
},
styled_jsx::styled_jsx(cm.clone(), file.name.clone()),
hook_optimizer::hook_optimizer(),
match &opts.styled_components {
Some(config) => Either::Left(styled_components::styled_components(
file.name.clone(),
file.src_hash,
config.clone(),
)),
None => Either::Right(noop()),
},
Optional::new(
next_ssg::next_ssg(eliminated_packages),
!opts.disable_next_ssg
),
amp_attributes::amp_attributes(),
next_dynamic::next_dynamic(
opts.is_development,
opts.is_server,
file.name.clone(),
opts.pages_dir.clone()
),
Optional::new(
page_config::page_config(opts.is_development, opts.is_page_file),
!opts.disable_page_config
),
relay_plugin,
match &opts.remove_console {
Some(config) if config.truthy() =>
Either::Left(remove_console::remove_console(config.clone())),
_ => Either::Right(noop()),
},
match &opts.react_remove_properties {
Some(config) if config.truthy() =>
Either::Left(react_remove_properties::remove_properties(config.clone())),
_ => Either::Right(noop()),
},
match &opts.shake_exports {
Some(config) => Either::Left(shake_exports::shake_exports(config.clone())),
None => Either::Right(noop()),
},
opts.emotion
.as_ref()
.and_then(|config| {
if !config.enabled.unwrap_or(false) {
return None;
}
if let FileName::Real(path) = &file.name {
path.to_str().map(|_| {
Either::Left(swc_emotion::EmotionTransformer::new(
config.clone(),
path,
cm,
comments,
))
})
} else {
None
}
})
.unwrap_or_else(|| Either::Right(noop())),
match &opts.modularize_imports {
Some(config) => Either::Left(modularize_imports::modularize_imports(config.clone())),
None => Either::Right(noop()),
}
)
}
impl TransformOptions {
pub fn patch(mut self, fm: &SourceFile) -> Self {
self.swc.swcrc = false;
let should_enable_commonjs = self.swc.config.module.is_none()
&& (fm.src.contains("module.exports") || fm.src.contains("__esModule"))
&& {
let syntax = self.swc.config.jsc.syntax.unwrap_or_default();
let target = self.swc.config.jsc.target.unwrap_or_else(EsVersion::latest);
parse_file_as_module(fm, syntax, target, None, &mut vec![])
.map(|m| contains_cjs(&m))
.unwrap_or_default()
};
if should_enable_commonjs {
self.swc.config.module = Some(ModuleConfig::CommonJs(Default::default()));
}
self
}
}