Skip to content

Commit 5f3d908

Browse files
committed
Split WebAssembly into single package.
1 parent 7a44f4b commit 5f3d908

File tree

10 files changed

+885
-764
lines changed

10 files changed

+885
-764
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
# Generated by Cargo
23
# will have compiled files and executables
34
target/
@@ -10,4 +11,5 @@ Cargo.lock
1011

1112
# These are backup files generated by rustfmt
1213
**/*.rs.bk
13-
this-will.git-ignore.rs
14+
this-will.git-ignore.rs
15+
*.log

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"autocorrect",
4+
"autocorrect-wasm",
45
"autocorrect-derive",
56
"autocorrect-cli",
67
]

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ install:
2323
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
2424
brew install binaryen
2525
wasm:
26-
wasm-pack build --release --scope huacnlee -d $(WORKDIR)/pkg autocorrect
26+
wasm-pack build --release --scope huacnlee -d $(WORKDIR)/pkg --out-name autocorrect autocorrect-wasm
27+
sed -ie "s/autocorrect\-wasm/autocorrect/" $(WORKDIR)/pkg/package.json
2728
wasm-opt -Os -o pkg/autocorrect_bg.wasm pkg/autocorrect_bg.wasm
2829
wasm\:publish:
2930
make wasm

autocorrect-wasm/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
edition = "2021"
3+
name = "autocorrect-wasm"
4+
version = "1.6.0"
5+
6+
[lib]
7+
crate-type = ["cdylib", "rlib"]
8+
9+
[dependencies]
10+
autocorrect = {path = "../autocorrect", version = "1.6.0"}
11+
serde = {version = "1.0.127", features = ["derive"]}
12+
serde_json = "1.0.66"
13+
wasm-bindgen = {version = "0.2.80", features = ["serde-serialize"]}
14+
wee_alloc = "0.4.5"
15+
16+
[package.metadata.wasm-pack.profile.release]
17+
wasm-opt = false

autocorrect-wasm/src/ignorer.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate wasm_bindgen;
2+
use wasm_bindgen::prelude::*;
3+
4+
#[wasm_bindgen(constructor)]
5+
pub struct Ignorer {
6+
ignorer: autocorrect::ignorer::Ignorer,
7+
}
8+
9+
#[wasm_bindgen]
10+
impl Ignorer {
11+
#[wasm_bindgen(constructor)]
12+
pub fn new(work_dir: &str) -> Ignorer {
13+
let ignorer = autocorrect::ignorer::Ignorer::new(work_dir);
14+
15+
Ignorer { ignorer }
16+
}
17+
18+
#[wasm_bindgen(js_name = "isIgnored")]
19+
pub fn is_ignored(&self, path: &str) -> bool {
20+
self.ignorer.is_ignored(path)
21+
}
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn test_is_ignored() {
30+
let current_dir = std::env::current_dir().unwrap();
31+
let work_dir = current_dir.parent().unwrap().to_str().unwrap();
32+
let ignorer = Ignorer::new(work_dir);
33+
assert!(ignorer.is_ignored("src/main.rs"));
34+
assert!(ignorer.is_ignored("pkg/foo/bar"));
35+
}
36+
}

autocorrect-wasm/src/lib.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
extern crate wasm_bindgen;
2+
use wasm_bindgen::prelude::*;
3+
4+
extern crate wee_alloc;
5+
#[global_allocator]
6+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
7+
8+
pub mod ignorer;
9+
10+
/// Automatically add spaces between Chinese and English words.
11+
///
12+
/// This method only work for plain text.
13+
///
14+
/// # Example
15+
///
16+
/// ```
17+
/// extern crate autocorrect;
18+
///
19+
/// println!("{}", autocorrect::format("学习如何用 Rust 构建 Application"));
20+
/// // => "学习如何用 Rust 构建 Application"
21+
///
22+
/// println!("{}", autocorrect::format("于 3 月 10 日开始"));
23+
/// // => "于 3 月 10 日开始"
24+
///
25+
/// println!("{}", autocorrect::format("既に、世界中の数百という企業が Rust を採用し、高速で低リソースのクロスプラットフォームソリューションを実現しています。"));
26+
/// // => "既に、世界中の数百という企業が Rust を採用し、高速で低リソースのクロスプラットフォームソリューションを実現しています。"
27+
/// ```
28+
#[wasm_bindgen]
29+
pub fn format(text: &str) -> String {
30+
autocorrect::format(text)
31+
}
32+
33+
/// Format content with filetype, and return a json result.
34+
#[wasm_bindgen(js_name = "formatFor")]
35+
pub fn format_for(raw: &str, filename_or_ext: &str) -> wasm_bindgen::JsValue {
36+
let result = autocorrect::format_for(raw, filename_or_ext);
37+
wasm_bindgen::JsValue::from_serde(&result).unwrap()
38+
}
39+
40+
/// Lint content with filetype, and return a json result.
41+
#[wasm_bindgen(js_name = "lintFor")]
42+
pub fn lint_for(raw: &str, filename_or_ext: &str) -> wasm_bindgen::JsValue {
43+
let result = autocorrect::lint_for(raw, filename_or_ext);
44+
wasm_bindgen::JsValue::from_serde(&result).unwrap()
45+
}
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
// autocorrect: false
52+
53+
#[test]
54+
fn test_format() {
55+
assert_eq!(
56+
"学习如何用 Rust 构建 Application",
57+
format("学习如何用Rust构建Application")
58+
);
59+
}
60+
}

autocorrect/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ repository = "https://github.com/huacnlee/autocorrect"
1111
version = "1.6.0"
1212

1313
[lib]
14-
crate-type = ["cdylib", "rlib"]
1514
name = "autocorrect"
1615
path = "src/lib.rs"
1716

@@ -25,8 +24,6 @@ pest_derive = "2.1.0"
2524
regex = "1"
2625
serde = {version = "1.0.127", features = ["derive"]}
2726
serde_json = "1.0.66"
28-
wasm-bindgen = {version = "0.2.75", features = ["serde-serialize"]}
29-
wee_alloc = "0.4.5"
3027

3128
[dev-dependencies]
3229
pretty_assertions = "1.0.0"

autocorrect/src/ignorer.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
use std::path::Path;
2-
extern crate wasm_bindgen;
3-
use wasm_bindgen::prelude::*;
42

5-
#[wasm_bindgen(constructor)]
63
pub struct Ignorer {
74
ignorer: ignore::gitignore::Gitignore,
85
}
96

107
static AUTOCORRECTIGNORE: &str = ".autocorrectignore";
118
static GITIGNORE: &str = ".gitignore";
129

13-
#[wasm_bindgen]
1410
impl Ignorer {
15-
#[wasm_bindgen(constructor)]
1611
pub fn new(work_dir: &str) -> Ignorer {
1712
let mut builder = ignore::gitignore::GitignoreBuilder::new(work_dir);
1813
builder.add(Path::join(Path::new(work_dir), AUTOCORRECTIGNORE));
@@ -24,7 +19,6 @@ impl Ignorer {
2419
Ignorer { ignorer }
2520
}
2621

27-
#[wasm_bindgen(js_name = "isIgnored")]
2822
pub fn is_ignored(&self, path: &str) -> bool {
2923
self.ignorer
3024
.matched_path_or_any_parents(path, false)

autocorrect/src/lib.rs

-23
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ mod strategery;
9090
use code::Results;
9191
pub use code::{format_for, get_file_extension, is_support_type, lint_for};
9292

93-
extern crate wasm_bindgen;
94-
use wasm_bindgen::prelude::*;
95-
96-
extern crate wee_alloc;
97-
#[global_allocator]
98-
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
99-
10093
use crate::strategery::Strategery;
10194
use regex::Regex;
10295

@@ -153,7 +146,6 @@ lazy_static! {
153146
/// println!("{}", autocorrect::format("既に、世界中の数百という企業が Rust を採用し、高速で低リソースのクロスプラットフォームソリューションを実現しています。"));
154147
/// // => "既に、世界中の数百という企業が Rust を採用し、高速で低リソースのクロスプラットフォームソリューションを実現しています。"
155148
/// ```
156-
#[wasm_bindgen]
157149
pub fn format(text: &str) -> String {
158150
let mut out = String::from(text);
159151

@@ -192,25 +184,10 @@ pub fn format(text: &str) -> String {
192184
/// "#;
193185
/// autocorrect::format_html(html);
194186
/// ```
195-
#[wasm_bindgen(js_name = "formatHTML")]
196187
pub fn format_html(html_str: &str) -> String {
197188
code::format_html(html_str).to_string()
198189
}
199190

200-
/// Format content with filetype, and return a json result.
201-
#[wasm_bindgen(js_name = "formatFor")]
202-
pub fn format_for_json_out(raw: &str, filename_or_ext: &str) -> wasm_bindgen::JsValue {
203-
let result = format_for(raw, filename_or_ext);
204-
wasm_bindgen::JsValue::from_serde(&result).unwrap()
205-
}
206-
207-
/// Lint content with filetype, and return a json result.
208-
#[wasm_bindgen(js_name = "lintFor")]
209-
pub fn lint_for_json_out(raw: &str, filename_or_ext: &str) -> wasm_bindgen::JsValue {
210-
let result = lint_for(raw, filename_or_ext);
211-
wasm_bindgen::JsValue::from_serde(&result).unwrap()
212-
}
213-
214191
fn space_dash_with_hans(text: &str) -> String {
215192
let mut out = String::from(text);
216193

0 commit comments

Comments
 (0)