Skip to content

Commit b98ee86

Browse files
committed
to release wasm
1 parent 0b92605 commit b98ee86

File tree

8 files changed

+60
-27
lines changed

8 files changed

+60
-27
lines changed

.idea/.gitignore

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

.idea/markdown-rs.iml

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

.idea/modules.xml

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

.idea/vcs.xml

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
"test": "pnpm -r test",
77
"lint": "cargo fmt --all && cargo clippy --all-features --all-targets --workspace"
88
},
9-
"devDependencies": {},
109
"packageManager": "[email protected]"
11-
}
10+
}

wasm/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,3 @@ console_error_panic_hook = "0.1"
2121
version = "0.3"
2222
features = ["console"]
2323

24-
[profile.release]
25-
opt-level = "z"
26-
lto = true

wasm/lib/index.mjs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { readFile } from 'node:fs/promises';
22
import { fileURLToPath } from 'node:url';
33
import { dirname, join } from 'node:path';
4-
import init, {
5-
to_html,
6-
to_html_with_options
7-
} from '../pkg/markdown_rs_wasm.js';
4+
5+
// Dynamic import to handle the ES module
6+
let wasmModule = null;
7+
8+
async function loadWasmModule() {
9+
if (!wasmModule) {
10+
wasmModule = await import('../pkg/markdown_rs_wasm.js');
11+
}
12+
return wasmModule;
13+
}
814

915
// Initialize WASM module once
1016
let initialized = false;
@@ -13,12 +19,13 @@ let initPromise = null;
1319
async function ensureInitialized() {
1420
if (initialized) return;
1521
if (!initPromise) {
22+
const wasm = await loadWasmModule();
1623
// Read WASM file for Node.js
1724
const __dirname = dirname(fileURLToPath(import.meta.url));
1825
const wasmPath = join(__dirname, '..', 'pkg', 'markdown_rs_wasm_bg.wasm');
1926
const wasmBuffer = await readFile(wasmPath);
20-
21-
initPromise = init({ module_or_path: wasmBuffer }).then(() => {
27+
28+
initPromise = wasm.default({ module_or_path: wasmBuffer }).then(() => {
2229
initialized = true;
2330
});
2431
}
@@ -32,7 +39,8 @@ async function ensureInitialized() {
3239
*/
3340
export async function toHtml(input) {
3441
await ensureInitialized();
35-
return to_html(input);
42+
const wasm = await loadWasmModule();
43+
return wasm.to_html(input);
3644
}
3745

3846
/**
@@ -48,30 +56,25 @@ export async function toHtml(input) {
4856
*/
4957
export async function toHtmlWithOptions(input, options) {
5058
await ensureInitialized();
51-
return to_html_with_options(input, options);
59+
const wasm = await loadWasmModule();
60+
return wasm.to_html_with_options(input, options);
5261
}
5362

5463
// Also export sync versions after initialization
5564
export function toHtmlSync(input) {
56-
if (!initialized) {
65+
if (!initialized || !wasmModule) {
5766
throw new Error('WASM not initialized. Call toHtml() first or await init()');
5867
}
59-
return to_html(input);
68+
return wasmModule.to_html(input);
6069
}
6170

6271
export function toHtmlWithOptionsSync(input, options) {
63-
if (!initialized) {
72+
if (!initialized || !wasmModule) {
6473
throw new Error('WASM not initialized. Call toHtmlWithOptions() first or await init()');
6574
}
66-
return to_html_with_options(input, options);
75+
return wasmModule.to_html_with_options(input, options);
6776
}
6877

69-
export { init };
70-
71-
export default {
72-
init,
73-
toHtml,
74-
toHtmlWithOptions,
75-
toHtmlSync,
76-
toHtmlWithOptionsSync
77-
};
78+
export async function init() {
79+
await ensureInitialized();
80+
}

wasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ pub fn to_html_with_options(input: &str, options: JsValue) -> Result<String, JsV
6969
// Convert markdown to HTML
7070
markdown::to_html_with_options(input, &markdown_options)
7171
.map_err(|e| JsValue::from_str(&format!("Markdown error: {}", e)))
72-
}
72+
}

0 commit comments

Comments
 (0)