Skip to content

Commit c4c6179

Browse files
committed
fix NAPI-RS 3.0 upgrade issues
1 parent ab32e20 commit c4c6179

File tree

14 files changed

+103
-252
lines changed

14 files changed

+103
-252
lines changed

bindings/Cargo.lock

Lines changed: 57 additions & 192 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/Cargo.toml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ resolver = "2"
2424
js-sys = "0.3.77"
2525
lightningcss = "1.0.0-alpha.66"
2626
miette = "7.6.0"
27-
napi = { version = "2", default-features = false }
27+
napi = { version = "3", default-features = false }
2828
napi-build = "2"
29-
napi-derive = { version = "2", default-features = false }
29+
napi-derive = { version = "3", default-features = false }
3030
owo-colors = "4.2.1"
3131
par-core = "2.0.0"
3232
path-clean = "1.0"
@@ -35,25 +35,25 @@ resolver = "2"
3535
serde = "1"
3636
serde-wasm-bindgen = "0.6.5"
3737
serde_json = "1.0.140"
38-
swc_atoms = "7.0.0"
39-
swc_cli_impl = "40.0.0"
40-
swc_common = "14.0.3"
41-
swc_compiler_base = "34.0.0"
42-
swc_config = "3.1.1"
43-
swc_core = "40.0.0"
44-
swc_css_ast = "14.0.0"
45-
swc_css_codegen = "14.0.0"
46-
swc_css_minifier = "14.0.0"
47-
swc_css_parser = "14.0.0"
48-
swc_ecma_react_compiler = "8.0.0"
49-
swc_error_reporters = "16.0.1"
50-
swc_html = "26.0.0"
51-
swc_html_ast = "14.0.0"
52-
swc_html_minifier = "32.0.0"
53-
swc_malloc = "1.2.3"
54-
swc_nodejs_common = "1.0.2"
38+
swc_atoms = { path = "../crates/swc_atoms" }
39+
swc_cli_impl = { path = "../crates/swc_cli_impl" }
40+
swc_common = { path = "../crates/swc_common" }
41+
swc_compiler_base = { path = "../crates/swc_compiler_base" }
42+
swc_config = { path = "../crates/swc_config" }
43+
swc_core = { path = "../crates/swc_core" }
44+
swc_css_ast = { path = "../crates/swc_css_ast" }
45+
swc_css_codegen = { path = "../crates/swc_css_codegen" }
46+
swc_css_minifier = { path = "../crates/swc_css_minifier" }
47+
swc_css_parser = { path = "../crates/swc_css_parser" }
48+
swc_ecma_react_compiler = { path = "../crates/swc_ecma_react_compiler" }
49+
swc_error_reporters = { path = "../crates/swc_error_reporters" }
50+
swc_html = { path = "../crates/swc_html" }
51+
swc_html_ast = { path = "../crates/swc_html_ast" }
52+
swc_html_minifier = { path = "../crates/swc_html_minifier" }
53+
swc_malloc = { path = "../crates/swc_malloc" }
54+
swc_nodejs_common = { path = "../crates/swc_nodejs_common" }
5555
swc_sourcemap = "9.3.4"
56-
swc_ts_fast_strip = "32.0.0"
56+
swc_ts_fast_strip = { path = "../crates/swc_ts_fast_strip" }
5757
tracing = "0.1.41"
5858
tracing-chrome = "0.7.2"
5959
tracing-futures = "0.2.5"

bindings/binding_core_node/src/analyze.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::sync::Arc;
22

33
use napi::{
4-
bindgen_prelude::{AbortSignal, AsyncTask},
5-
Env, JsBuffer, JsBufferValue, Ref, Task,
4+
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
5+
Env, Task,
66
};
77
use swc_core::{
88
base::{wasm_analysis::WasmAnalysisOptions, Compiler},
@@ -16,7 +16,7 @@ use crate::{get_fresh_compiler, util::try_with};
1616
pub struct AnalyzeTask {
1717
pub c: Arc<Compiler>,
1818
pub input: Option<String>,
19-
pub options: Ref<JsBufferValue>,
19+
pub options: Buffer,
2020
}
2121

2222
#[napi]
@@ -48,25 +48,20 @@ impl Task for AnalyzeTask {
4848
fn resolve(&mut self, _env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
4949
Ok(result)
5050
}
51-
52-
fn finally(&mut self, env: Env) -> napi::Result<()> {
53-
self.options.unref(env)?;
54-
Ok(())
55-
}
5651
}
5752

5853
#[napi]
5954
pub fn analyze(
6055
src: String,
61-
options: JsBuffer,
56+
options: Buffer,
6257
signal: Option<AbortSignal>,
6358
) -> napi::Result<AsyncTask<AnalyzeTask>> {
6459
crate::util::init_default_trace_subscriber();
6560

6661
let task = AnalyzeTask {
6762
c: get_fresh_compiler(),
6863
input: Some(src),
69-
options: options.into_ref()?,
64+
options,
7065
};
7166
Ok(AsyncTask::with_optional_signal(task, signal))
7267
}

bindings/binding_core_node/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
2929
Arc::new(Compiler::new(cm))
3030
});
3131

32-
#[napi::module_init]
32+
#[napi_derive::module_init]
3333
fn init() {
3434
if cfg!(debug_assertions) || env::var("SWC_DEBUG").unwrap_or_default() == "1" {
3535
set_hook(Box::new(|panic_info| {

bindings/binding_core_node/src/minify.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use swc_core::{
1717

1818
use crate::{get_fresh_compiler, util::try_with};
1919

20-
#[napi(object)]
20+
#[napi(object, object_to_js = false)]
2121
pub struct NapiMinifyExtra {
2222
#[napi(ts_type = "object")]
23-
pub mangle_name_cache: Option<NameMangleCache>,
23+
pub mangle_name_cache: Option<&'static NameMangleCache>,
2424
}
2525

2626
struct MinifyTask {
@@ -100,7 +100,7 @@ fn minify(
100100
let code = String::from_utf8_lossy(code.as_ref()).to_string();
101101
let options = String::from_utf8_lossy(opts.as_ref()).to_string();
102102
let extras = JsMinifyExtras::default()
103-
.with_mangle_name_cache(extras.mangle_name_cache.as_deref().cloned());
103+
.with_mangle_name_cache(extras.mangle_name_cache.as_deref().map(|s| (*s).clone()));
104104

105105
let c = get_fresh_compiler();
106106

@@ -134,7 +134,7 @@ pub fn minify_sync(
134134
};
135135
let opts = get_deserialized(opts)?;
136136
let extras = JsMinifyExtras::default()
137-
.with_mangle_name_cache(extras.mangle_name_cache.as_deref().cloned());
137+
.with_mangle_name_cache(extras.mangle_name_cache.as_deref().map(|s| (*s).clone()));
138138

139139
let c = get_fresh_compiler();
140140

bindings/binding_core_node/src/transform.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
use anyhow::Context as _;
77
use napi::{
88
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
9-
Env, JsBuffer, JsBufferValue, Ref, Task,
9+
Env, Task,
1010
};
1111
use path_clean::clean;
1212
use swc_core::{
@@ -33,7 +33,7 @@ pub enum Input {
3333
pub struct TransformTask {
3434
pub c: Arc<Compiler>,
3535
pub input: Input,
36-
pub options: Ref<JsBufferValue>,
36+
pub options: Buffer,
3737
}
3838

3939
#[napi]
@@ -89,19 +89,14 @@ impl Task for TransformTask {
8989
fn resolve(&mut self, _env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
9090
Ok(result)
9191
}
92-
93-
fn finally(&mut self, env: Env) -> napi::Result<()> {
94-
self.options.unref(env)?;
95-
Ok(())
96-
}
9792
}
9893

9994
#[napi]
10095
#[instrument(level = "trace", skip_all)]
10196
pub fn transform(
10297
src: String,
10398
is_module: bool,
104-
options: JsBuffer,
99+
options: Buffer,
105100
signal: Option<AbortSignal>,
106101
) -> napi::Result<AsyncTask<TransformTask>> {
107102
crate::util::init_default_trace_subscriber();
@@ -112,11 +107,7 @@ pub fn transform(
112107
(Input::Source { src }, get_fresh_compiler())
113108
};
114109

115-
let task = TransformTask {
116-
c,
117-
input,
118-
options: options.into_ref()?,
119-
};
110+
let task = TransformTask { c, input, options };
120111
Ok(AsyncTask::with_optional_signal(task, signal))
121112
}
122113

@@ -171,7 +162,7 @@ pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result<
171162
pub fn transform_file(
172163
src: String,
173164
_is_module: bool,
174-
options: JsBuffer,
165+
options: Buffer,
175166
signal: Option<AbortSignal>,
176167
) -> napi::Result<AsyncTask<TransformTask>> {
177168
crate::util::init_default_trace_subscriber();
@@ -182,7 +173,7 @@ pub fn transform_file(
182173
let task = TransformTask {
183174
c,
184175
input: Input::File(path),
185-
options: options.into_ref()?,
176+
options,
186177
};
187178
Ok(AsyncTask::with_optional_signal(task, signal))
188179
}

bindings/binding_core_node/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn get_target_triple() -> napi::Result<String> {
2828

2929
#[napi]
3030
pub fn init_custom_trace_subscriber(
31-
mut env: Env,
31+
env: Env,
3232
trace_out_file_path: Option<String>,
3333
) -> napi::Result<()> {
3434
CUSTOM_TRACE_SUBSCRIBER.get_or_init(|| {

bindings/binding_html_node/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use swc_nodejs_common::{deserialize_json, get_deserialized, MapErr};
3434

3535
use crate::{tag_omission::TagOmission, util::try_with};
3636

37-
#[napi::module_init]
37+
#[napi_derive::module_init]
3838
fn init() {
3939
if cfg!(debug_assertions) || env::var("SWC_DEBUG").unwrap_or_default() == "1" {
4040
set_hook(Box::new(|panic_info| {

bindings/binding_minifier_node/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use backtrace::Backtrace;
1313
mod minify;
1414
mod util;
1515

16-
#[napi::module_init]
16+
#[napi_derive::module_init]
1717
fn init() {
1818
if cfg!(debug_assertions) || env::var("SWC_DEBUG").unwrap_or_default() == "1" {
1919
set_hook(Box::new(|panic_info| {

bindings/binding_minifier_node/src/minify.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use swc_nodejs_common::{deserialize_json, get_deserialized, MapErr};
3131

3232
use crate::util::try_with;
3333

34-
#[napi(object)]
34+
#[napi(object, object_to_js = false)]
3535
pub struct NapiMinifyExtra {
3636
#[napi(ts_type = "object")]
37-
pub mangle_name_cache: Option<NameMangleCache>,
37+
pub mangle_name_cache: Option<&'static NameMangleCache>,
3838
}
3939

4040
struct MinifyTask {
@@ -166,7 +166,7 @@ fn do_work(
166166
&swc_core::ecma::minifier::option::ExtraOptions {
167167
unresolved_mark,
168168
top_level_mark,
169-
mangle_name_cache: extras.mangle_name_cache.as_deref().cloned(),
169+
mangle_name_cache: extras.mangle_name_cache.as_deref().map(|s| (*s).clone()),
170170
},
171171
);
172172

0 commit comments

Comments
 (0)