Skip to content

Commit 2ec54fd

Browse files
committed
Refactor WASM build process to consolidate targets
Signed-off-by: Guan-Ming (Wesley) Chiu <105915352+guan404ming@users.noreply.github.com>
1 parent b2c2023 commit 2ec54fd

File tree

5 files changed

+19
-46
lines changed

5 files changed

+19
-46
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ jobs:
4040
- name: Install wasm-pack
4141
uses: jetli/wasm-pack-action@v0.4.0
4242

43-
- name: Build WASM (Node.js and Web)
44-
run: |
45-
wasm-pack build --target nodejs --out-dir ts/wasm
46-
wasm-pack build --target web --out-dir ts/wasm-web
47-
cp ts/wasm-web/sqlparser_rs_wasm.js ts/wasm/sqlparser_rs_wasm_web.js
48-
cp ts/wasm-web/sqlparser_rs_wasm_bg.wasm ts/wasm/sqlparser_rs_wasm_web_bg.wasm
49-
rm -rf ts/wasm-web
43+
- name: Build WASM
44+
run: wasm-pack build --target web --out-dir ts/wasm
5045

5146
- name: Build TypeScript
5247
working-directory: ts

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ node_modules/
77
# WASM build output
88
/pkg
99
/ts/wasm
10-
/ts/wasm-web
1110

1211
# TypeScript build output
1312
/ts/dist

scripts/build.sh

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,23 @@ echo "=========================================="
66
echo "Building sqlparser-ts npm package"
77
echo "=========================================="
88

9-
# Get the script directory
109
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1110
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
1211

1312
echo ""
1413
echo "Project directory: $PROJECT_DIR"
1514
echo ""
1615

17-
# Check for wasm-pack
1816
if ! command -v wasm-pack &> /dev/null; then
1917
echo "Error: wasm-pack is not installed."
2018
echo "Install it with: cargo install wasm-pack"
2119
echo "Or visit: https://rustwasm.github.io/wasm-pack/installer/"
2220
exit 1
2321
fi
2422

25-
# Build WASM for Node.js
26-
echo "Step 1a: Building WASM module for Node.js..."
23+
echo "Step 1: Building WASM module..."
2724
cd "$PROJECT_DIR"
28-
wasm-pack build --target nodejs --out-dir ts/wasm
29-
30-
# Build WASM for Web (browser)
31-
echo "Step 1b: Building WASM module for Web (browser)..."
32-
wasm-pack build --target web --out-dir ts/wasm-web
33-
34-
# Copy web wasm files to wasm directory with _web suffix
35-
cp ts/wasm-web/sqlparser_rs_wasm.js ts/wasm/sqlparser_rs_wasm_web.js
36-
cp ts/wasm-web/sqlparser_rs_wasm_bg.wasm ts/wasm/sqlparser_rs_wasm_web_bg.wasm
37-
rm -rf ts/wasm-web
25+
wasm-pack build --target web --out-dir ts/wasm
3826

3927
echo ""
4028
echo "Step 2: Installing npm dependencies..."
@@ -51,10 +39,10 @@ echo "Build complete!"
5139
echo "=========================================="
5240
echo ""
5341
echo "Output files:"
54-
echo " - WASM: ts/wasm/"
55-
echo " - ESM: ts/dist/esm/"
56-
echo " - CJS: ts/dist/cts/"
57-
echo " - Types: ts/dist/types/"
42+
echo " - WASM: ts/wasm/"
43+
echo " - ESM: ts/dist/index.mjs"
44+
echo " - CJS: ts/dist/index.cjs"
45+
echo " - Types: ts/dist/index.d.{mts,cts}"
5846
echo ""
5947
echo "To run tests: cd ts && npm test"
6048
echo "To publish: cd ts && npm publish"

ts/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
],
2121
"scripts": {
2222
"build": "npm run build:wasm && npm run build:ts",
23-
"build:wasm": "cd .. && wasm-pack build --target nodejs --out-dir ts/wasm && rm -f ts/wasm/.gitignore",
24-
"build:wasm:web": "cd .. && wasm-pack build --target web --out-dir ts/wasm-web",
23+
"build:wasm": "cd .. && wasm-pack build --target web --out-dir ts/wasm && rm -f ts/wasm/.gitignore",
2524
"build:ts": "tsdown",
2625
"test": "vitest run",
2726
"test:watch": "vitest",

ts/src/wasm.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,19 @@ export async function init(): Promise<void> {
3737

3838
if (!initPromise) {
3939
initPromise = (async () => {
40-
if (isBrowser) {
41-
try {
42-
const wasm = await import(/* @vite-ignore */ '../wasm/sqlparser_rs_wasm_web.js');
43-
const wasmBinaryUrl = new URL('../wasm/sqlparser_rs_wasm_web_bg.wasm', import.meta.url);
44-
45-
if (typeof wasm.default === 'function') {
46-
await wasm.default({ module_or_path: wasmBinaryUrl });
47-
}
40+
try {
41+
const wasm = await import(/* @vite-ignore */ '../wasm/sqlparser_rs_wasm.js');
4842

49-
wasmModule = wasm as WasmModule;
50-
} catch (error) {
51-
throw new WasmInitError(
52-
`Failed to load WASM module in browser: ${error instanceof Error ? error.message : String(error)}`
53-
);
43+
if (isBrowser) {
44+
const wasmUrl = new URL('../wasm/sqlparser_rs_wasm_bg.wasm', import.meta.url);
45+
await wasm.default({ module_or_path: wasmUrl });
46+
} else {
47+
const { readFile } = await import(/* @vite-ignore */ 'node:fs/promises');
48+
const wasmPath = new URL('../wasm/sqlparser_rs_wasm_bg.wasm', import.meta.url);
49+
const bytes = await readFile(wasmPath);
50+
await wasm.default({ module_or_path: bytes });
5451
}
55-
return;
56-
}
5752

58-
// Node.js
59-
try {
60-
const wasm = await import(/* @vite-ignore */ '../wasm/sqlparser_rs_wasm.js');
6153
wasmModule = wasm as WasmModule;
6254
} catch (error) {
6355
throw new WasmInitError(

0 commit comments

Comments
 (0)