Skip to content

Commit

Permalink
Merge pull request #22 from svg-rust/cargo
Browse files Browse the repository at this point in the history
Cargo
  • Loading branch information
SyMind committed Jun 28, 2023
2 parents 1626403 + b9db3e1 commit 1177889
Show file tree
Hide file tree
Showing 33 changed files with 228 additions and 116 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ env:
- docs/**
pull_request: null
jobs:
cargo-test:
name: Test cargo
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
target: x86_64-unknown-linux-gnu
- name: Run cargo test
run: cargo test -p svgr-rs
build:
strategy:
fail-fast: false
Expand Down Expand Up @@ -47,6 +59,8 @@ jobs:
target: aarch64-pc-windows-msvc
build: yarn build --target aarch64-pc-windows-msvc
name: stable - ${{ matrix.settings.target }} - node@18
needs:
- cargo-test
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ $RECYCLE.BIN/

#Added by cargo

/target
target/
Cargo.lock

.pnp.*
Expand Down
14 changes: 12 additions & 2 deletions Cargo.lock

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

52 changes: 5 additions & 47 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
[package]
authors = ["SyMind <dacongsama@live.com>"]
description = "A tool to transform SVG into React components"
name = "svgr-rs"
edition = "2021"
license = "MIT"
repository = "https://github.com/svg-rust/svgr-rs.git"
version = "0.1.0"

[lib]
crate-type = ["cdylib"]

[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"] }
swc_xml = "0.10.3"
swc_core = { version = "0.74.2", features = [
"ecma_ast",
"ecma_ast_serde",
"common_concurrent",
"bundler",
"ecma_loader",
"ecma_transforms",
"ecma_visit",
"ecma_codegen",
"base_node",
"__parser",
] }
linked-hash-map = { version = "0.5.6", features = ["serde_impl"] }
linked_hash_set = "0.1.4"

[build-dependencies]
napi-build = "2.0.1"

[dev-dependencies]
testing = "0.32.5"

[profile.release]
lto = true
[workspace]
members = [
"crates/core",
"crates/binding"
]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

> ⚠️ RVGR RS is in early development and should not be used in production, expect bugs! 🐛
If you are using SVGR RS from Rust, see [rustdoc](https://github.com/svg-rust/svgr-rs) and for most users.

# Node.js

Use SVGR RS in Node.js to complex transformations or tools.
Expand Down
8 changes: 6 additions & 2 deletions binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string
map?: string
}
export interface Caller {
name?: string
previousExport?: string
}
export interface Config {
export interface State {
filePath?: string
componentName?: string
caller?: Caller
}
export function transform(code: string, config: Buffer, state?: Config | undefined | null): Promise<string>
export function transform(code: string, config: Buffer, state?: State | undefined | null): Promise<string>
21 changes: 21 additions & 0 deletions crates/binding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
authors = ["SyMind <dacongsama@live.com>"]
name = "binding"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT"

[lib]
bench = false
crate-type = ["cdylib"]

[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"] }
napi-derive = { version = "2.12.2" }
swc_core = { version = "0.74.2", features = ["base_node"] }
svgr-rs = { path = "../core", features = ["node"] }

[build-dependencies]
napi-build = "2.0.1"
File renamed without changes.
22 changes: 22 additions & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(path_file_prefix)]

#![deny(clippy::all)]

#[macro_use]
extern crate napi_derive;

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

#[napi]
pub async fn transform(code: String, config: napi::bindgen_prelude::Buffer, state: Option<State>) -> napi::bindgen_prelude::Result<String> {
let config: Config = get_deserialized(&config)?;
let state = match state {
Some(state) => state,
None => Default::default()
};
match transform_core(code, config, state) {
Ok(result) => napi::bindgen_prelude::Result::Ok(result),
Err(reason) => napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(reason)),
}
}
40 changes: 40 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
authors = ["SyMind <dacongsama@live.com>"]
description = "A tool to transform SVG into React components"
name = "svgr-rs"
edition = "2021"
license = "MIT"
repository = "https://github.com/svg-rust/svgr-rs.git"
version = "0.1.2"

[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"] }
swc_xml = "0.10.3"
swc_core = { version = "0.74.2", features = [
"ecma_ast",
"ecma_ast_serde",
"common_concurrent",
"bundler",
"ecma_loader",
"ecma_transforms",
"ecma_visit",
"ecma_codegen",
"__parser",
] }
linked-hash-map = { version = "0.5.6", features = ["serde_impl"] }
linked_hash_set = "0.1.4"

[dev-dependencies]
testing = "0.32.5"

[profile.release]
lto = true
1 change: 1 addition & 0 deletions crates/core/README.md
File renamed without changes
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions src/core/config.rs → crates/core/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,54 +108,80 @@ pub enum ExportType {
Default,
}

/// The options used to transform the SVG.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// Setting this to `true` will forward ref to the root SVG tag.
#[serde(default)]
#[serde(rename(serialize = "ref"))]
pub _ref: Option<bool>,

/// Add title tag via title property.
/// If title_prop is set to true and no title is provided at render time, this will fallback to an existing title element in the svg if exists.
#[serde(default)]
pub title_prop: Option<bool>,

/// Add desc tag via desc property.
/// If desc_prop is set to true and no description is provided at render time, this will fallback to an existing desc element in the svg if exists.
#[serde(default)]
pub desc_prop: Option<bool>,

/// All properties given to component will be forwarded on SVG tag.
/// Possible values: "start", "end" or false.
#[serde(default)]
pub expand_props: ExpandProps,

/// Keep `width` and `height` attributes from the root SVG tag.
/// Removal is guaranteed if `dimensions: false`, unlike the `remove_dimensions: true` SVGO plugin option which also generates a `viewBox` from the dimensions if no `viewBox` is present.
#[serde(default)]
pub dimensions: Option<bool>,

/// Replace SVG `width` and `height` by a custom value.
/// If value is omitted, it uses `1em` in order to make SVG size inherits from text size.
#[serde(default)]
pub icon: Option<Icon>,

/// Modify all SVG nodes with uppercase and use a specific template with `react-native-svg` imports.
/// All unsupported nodes will be removed.
#[serde(default)]
pub native: Option<bool>,

/// Add props to the root SVG tag.
#[serde(default)]
// Deserialize object/map while maintaining order
// here: https://github.com/serde-rs/serde/issues/269
pub svg_props: Option<LinkedHashMap<String, String>>,

/// Generates `.tsx` files with TypeScript typings.
#[serde(default)]
pub typescript: Option<bool>,

/// Setting this to `true` will wrap the exported component in `React.memo`.
#[serde(default)]
pub memo: Option<bool>,

/// Replace an attribute value by an other.
/// The main usage of this option is to change an icon color to "currentColor" in order to inherit from text color.
#[serde(default)]
pub replace_attr_values: Option<LinkedHashMap<String, String>>,

/// Specify a JSX runtime to use.
/// * "classic": adds `import * as React from 'react'` on the top of file
/// * "automatic": do not add anything
/// * "classic-preact": adds `import { h } from 'preact'` on the top of file
#[serde(default)]
pub jsx_runtime: Option<JSXRuntime>,

/// Specify a custom JSX runtime source to use. Allows to customize the import added at the top of generated file.
#[serde(default)]
pub jsx_runtime_import: Option<JSXRuntimeImport>,

/// The named export defaults to `ReactComponent`, can be customized with the `named_export` option.
#[serde(default = "default_named_export")]
pub named_export: String,

/// If you prefer named export in any case, you may set the `export_type` option to `named`.
#[serde(default)]
pub export_type: Option<ExportType>,
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 1177889

Please sign in to comment.