forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.rs
224 lines (192 loc) · 6.2 KB
/
transform.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
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use anyhow::Context as _;
use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Env, JsBuffer, JsBufferValue, Ref, Task,
};
use path_clean::clean;
use swc_core::{
base::{config::Options, Compiler, TransformOutput},
common::FileName,
ecma::ast::Program,
node::{deserialize_json, get_deserialized, MapErr},
};
use tracing::instrument;
use crate::{get_compiler, util::try_with};
/// Input to transform
#[derive(Debug)]
pub enum Input {
/// json string
Program(String),
/// Raw source code.
Source { src: String },
/// File
File(PathBuf),
}
pub struct TransformTask {
pub c: Arc<Compiler>,
pub input: Input,
pub options: Ref<JsBufferValue>,
}
#[napi]
impl Task for TransformTask {
type JsValue = TransformOutput;
type Output = TransformOutput;
#[instrument(level = "trace", skip_all)]
fn compute(&mut self) -> napi::Result<Self::Output> {
let mut options: Options = serde_json::from_slice(self.options.as_ref())?;
if !options.filename.is_empty() {
options.config.adjust(Path::new(&options.filename));
}
let error_format = options.experimental.error_format.unwrap_or_default();
try_with(
self.c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
self.c.run(|| match &self.input {
Input::Program(ref s) => {
let program: Program =
deserialize_json(s).expect("failed to deserialize Program");
// TODO: Source map
self.c.process_js(handler, program, &options)
}
Input::File(ref path) => {
let fm = self.c.cm.load_file(path).context("failed to load file")?;
self.c.process_js_file(fm, handler, &options)
}
Input::Source { src } => {
let fm = self.c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
src.to_string(),
);
self.c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}
fn resolve(&mut self, _env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
Ok(result)
}
fn finally(&mut self, env: Env) -> napi::Result<()> {
self.options.unref(env)?;
Ok(())
}
}
#[napi]
#[instrument(level = "trace", skip_all)]
pub fn transform(
src: String,
is_module: bool,
options: JsBuffer,
signal: Option<AbortSignal>,
) -> napi::Result<AsyncTask<TransformTask>> {
crate::util::init_default_trace_subscriber();
let c = get_compiler();
let input = if is_module {
Input::Program(src)
} else {
Input::Source { src }
};
let task = TransformTask {
c,
input,
options: options.into_ref()?,
};
Ok(AsyncTask::with_optional_signal(task, signal))
}
#[napi]
#[instrument(level = "trace", skip_all)]
pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result<TransformOutput> {
crate::util::init_default_trace_subscriber();
let c = get_compiler();
let mut options: Options = get_deserialized(&opts)?;
if !options.filename.is_empty() {
options.config.adjust(Path::new(&options.filename));
}
let error_format = options.experimental.error_format.unwrap_or_default();
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program =
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
} else {
let fm = c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
s,
);
c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}
#[napi]
#[instrument(level = "trace", skip_all)]
pub fn transform_file(
src: String,
_is_module: bool,
options: JsBuffer,
signal: Option<AbortSignal>,
) -> napi::Result<AsyncTask<TransformTask>> {
crate::util::init_default_trace_subscriber();
let c = get_compiler();
let path = clean(&src);
let task = TransformTask {
c,
input: Input::File(path.into()),
options: options.into_ref()?,
};
Ok(AsyncTask::with_optional_signal(task, signal))
}
#[napi]
pub fn transform_file_sync(
s: String,
is_module: bool,
opts: Buffer,
) -> napi::Result<TransformOutput> {
crate::util::init_default_trace_subscriber();
let c = get_compiler();
let mut options: Options = get_deserialized(&opts)?;
if !options.filename.is_empty() {
options.config.adjust(Path::new(&options.filename));
}
let error_format = options.experimental.error_format.unwrap_or_default();
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program =
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
} else {
let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file");
c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}