-
Notifications
You must be signed in to change notification settings - Fork 397
/
bundler.rs
114 lines (95 loc) · 2.96 KB
/
bundler.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
use napi::{tokio::sync::Mutex, Env};
use napi_derive::napi;
use rolldown::Bundler as NativeBundler;
use rolldown_fs::OsFileSystem;
use tracing::instrument;
use crate::{
options::{BindingInputOptions, BindingOutputOptions},
types::binding_outputs::BindingOutputs,
utils::{normalize_binding_options::normalize_binding_options, try_init_custom_trace_subscriber},
};
#[napi]
pub struct Bundler {
inner: Mutex<NativeBundler<OsFileSystem>>,
}
#[napi]
impl Bundler {
#[napi(constructor)]
pub fn new(
env: Env,
input_options: BindingInputOptions,
output_options: BindingOutputOptions,
) -> napi::Result<Self> {
try_init_custom_trace_subscriber(env);
let ret = normalize_binding_options(input_options, output_options)?;
Ok(Self {
inner: Mutex::new(NativeBundler::with_plugins(
ret.input_options,
ret.output_options,
ret.plugins,
)),
})
}
#[napi]
pub async fn write(&self) -> napi::Result<BindingOutputs> {
self.write_impl().await
}
#[napi]
pub async fn generate(&self) -> napi::Result<BindingOutputs> {
self.generate_impl().await
}
#[napi]
pub async fn scan(&self) -> napi::Result<()> {
self.scan_impl().await
}
}
impl Bundler {
#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn scan_impl(&self) -> napi::Result<()> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;
let result = bundler_core.scan().await;
if let Err(err) = result {
// TODO: better handing errors
eprintln!("{err:?}");
return Err(napi::Error::from_reason("Build failed"));
}
Ok(())
}
#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn write_impl(&self) -> napi::Result<BindingOutputs> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;
let maybe_outputs = bundler_core.write().await;
let outputs = match maybe_outputs {
Ok(outputs) => outputs,
Err(err) => {
// TODO: better handing errors
eprintln!("{err:?}");
return Err(napi::Error::from_reason("Build failed"));
}
};
Ok(outputs.assets.into())
}
#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn generate_impl(&self) -> napi::Result<BindingOutputs> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;
let maybe_outputs = bundler_core.generate().await;
let outputs = match maybe_outputs {
Ok(outputs) => outputs,
Err(err) => {
// TODO: better handing errors
eprintln!("{err:?}");
return Err(napi::Error::from_reason("Build failed"));
}
};
Ok(outputs.assets.into())
}
}