Skip to content

Commit

Permalink
feat: remove napi in core crate
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 20, 2024
1 parent 8befd7c commit 0c3f666
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ crate-type = ["cdylib"]
napi = { version = "2.12.0", default-features = false, features = ["napi4", "async"] }
napi-derive = { version = "2.12.2" }
swc_core = { version = "0.100.1", features = ["base_node"] }
svgr-rs = { path = "../core", features = ["node"] }
svgr-rs = { path = "../core" }

[build-dependencies]
napi-build = "2.0.1"
38 changes: 35 additions & 3 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,49 @@
#[macro_use]
extern crate napi_derive;

use svgr_rs::{transform, Config, State};
use svgr_rs::{transform, Caller, Config, State};
use swc_core::node::get_deserialized;

#[napi(object, object_to_js = false)]
pub struct JsCaller {
pub name: Option<String>,
pub previous_export: Option<String>,
}

impl From<JsCaller> for Caller {
fn from(val: JsCaller) -> Self {
Self {
name: val.name,
previous_export: val.previous_export,
}
}
}

#[napi(object, object_to_js = false)]
pub struct JsState {
pub file_path: Option<String>,
pub component_name: Option<String>,
pub caller: Option<JsCaller>,
}

impl From<JsState> for State {
fn from(val: JsState) -> Self {
Self {
file_path: val.file_path,
component_name: val.component_name,
caller: val.caller.map(|c| c.into()),
}
}
}

#[napi(js_name = "transform")]
pub async fn transform_node(
code: String,
config: napi::bindgen_prelude::Buffer,
state: Option<State>,
js_state: Option<JsState>,
) -> napi::bindgen_prelude::Result<String> {
let config: Config = get_deserialized(&config)?;
let state = state.unwrap_or_default();
let state = js_state.map(|s| s.into()).unwrap_or_default();
match transform(code, config, state) {
Ok(result) => napi::bindgen_prelude::Result::Ok(result),
Err(reason) => napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(
Expand Down
7 changes: 0 additions & 7 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ license = "MIT"
repository = "https://github.com/svg-rust/svgr-rs.git"
version = "0.1.3"

[features]
node = ["dep:napi", "dep:napi-derive"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.0", default-features = false, features = ["napi4", "async"], optional = true }
napi-derive = { version = "2.12.2", optional = true }

clap = { version = "4.2.0", features = ["derive"] }
regex = "1.7.3"
serde = { version = "1", features = ["derive"] }
Expand Down
18 changes: 0 additions & 18 deletions crates/core/src/core/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ use std::path::Path;
use lazy_static::lazy_static;
use regex::Regex;

#[cfg(feature = "node")]
#[napi(object)]
#[derive(Debug, Clone, Default)]
pub struct Caller {
pub name: Option<String>,
pub previous_export: Option<String>,
}

#[cfg(not(feature = "node"))]
#[derive(Debug, Clone, Default)]
pub struct Caller {
pub name: Option<String>,
pub previous_export: Option<String>,
}

/// The state linked to the transformation.
#[cfg(feature = "node")]
#[napi(object, js_name = "State")]
pub struct Config {
/// The name of the file that is generated, mainly used to find runtime config file to apply.
pub file_path: Option<String>,
Expand All @@ -44,13 +33,6 @@ impl Default for Config {
}
}

#[cfg(not(feature = "node"))]
pub struct Config {
pub file_path: Option<String>,
pub component_name: Option<String>,
pub caller: Option<Caller>,
}

#[derive(Debug)]
pub struct InternalConfig {
#[allow(dead_code)]
Expand Down
4 changes: 0 additions & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#![feature(path_file_prefix)]
#![deny(clippy::all)]

#[cfg(feature = "node")]
#[macro_use]
extern crate napi_derive;

use std::{borrow::Borrow, sync::Arc};

use swc_core::{
Expand Down

0 comments on commit 0c3f666

Please sign in to comment.