Skip to content

Commit 226986d

Browse files
authored
Merge pull request #137 from Peefy/feat-kcl-wasm-in-browser
feat: kcl wasm lib both for node.js and browser
2 parents 2a63c36 + 8bb96f2 commit 226986d

File tree

18 files changed

+463
-432
lines changed

18 files changed

+463
-432
lines changed

.github/workflows/wasm-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: npm install
3838

3939
- name: Build
40-
run: npm run compile
40+
run: npm run build
4141

4242
- name: Publish Dry Run
4343
if: "startsWith(github.ref, 'refs/tags/') && contains(github.ref, '-')"

wasm/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ npm-debug.log*
99
yarn-debug.log*
1010
yarn-error.log*
1111
lerna-debug.log*
12+
package-lock.json
1213

1314
# Diagnostic reports (https://nodejs.org/api/report.html)
1415
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
@@ -121,7 +122,7 @@ dist
121122
.AppleDouble
122123
.LSOverride
123124

124-
# Icon must end with two
125+
# Icon must end with two
125126
Icon
126127

127128

wasm/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ npm install @kcl-lang/wasm-lib
99
```
1010

1111
```typescript
12-
import { load, invokeKCLRun } from '@kcl-lang/wasm-lib'
12+
import { load, invokeKCLRun } from "@kcl-lang/wasm-lib";
1313

1414
async function main() {
1515
const inst = await load();
@@ -21,10 +21,10 @@ schema Person:
2121
2222
p = Person {name = "Alice"}`,
2323
});
24-
console.log(result)
24+
console.log(result);
2525
}
2626

27-
main()
27+
main();
2828
```
2929

3030
### Rust

wasm/examples/browser/.gitignore

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/node
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=node
3+
4+
### Node ###
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
package-lock.json
13+
14+
# Diagnostic reports (https://nodejs.org/api/report.html)
15+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Directory for instrumented libs generated by jscoverage/JSCover
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
coverage
28+
*.lcov
29+
30+
# nyc test coverage
31+
.nyc_output
32+
33+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
34+
.grunt
35+
36+
# Bower dependency directory (https://bower.io/)
37+
bower_components
38+
39+
# node-waf configuration
40+
.lock-wscript
41+
42+
# Compiled binary addons (https://nodejs.org/api/addons.html)
43+
build/Release
44+
45+
# Dependency directories
46+
node_modules/
47+
jspm_packages/
48+
49+
# TypeScript v1 declaration files
50+
typings/
51+
52+
# TypeScript cache
53+
*.tsbuildinfo
54+
55+
# Optional npm cache directory
56+
.npm
57+
58+
# Optional eslint cache
59+
.eslintcache
60+
61+
# Microbundle cache
62+
.rpt2_cache/
63+
.rts2_cache_cjs/
64+
.rts2_cache_es/
65+
.rts2_cache_umd/
66+
67+
# Optional REPL history
68+
.node_repl_history
69+
70+
# Output of 'npm pack'
71+
*.tgz
72+
73+
# Yarn Integrity file
74+
.yarn-integrity
75+
76+
# dotenv environment variables file
77+
.env
78+
.env.test
79+
80+
# parcel-bundler cache (https://parceljs.org/)
81+
.cache
82+
83+
# Next.js build output
84+
.next
85+
86+
# Nuxt.js build / generate output
87+
.nuxt
88+
dist
89+
90+
# Gatsby files
91+
.cache/
92+
# Comment in the public line in if your project uses Gatsby and not Next.js
93+
# https://nextjs.org/blog/next-9-1#public-directory-support
94+
# public
95+
96+
# vuepress build output
97+
.vuepress/dist
98+
99+
# Serverless directories
100+
.serverless/
101+
102+
# FuseBox cache
103+
.fusebox/
104+
105+
# DynamoDB Local files
106+
.dynamodb/
107+
108+
# TernJS port file
109+
.tern-port
110+
111+
# Stores VSCode versions used for testing VSCode extensions
112+
.vscode-test
113+
114+
# End of https://www.toptal.com/developers/gitignore/api/node
115+
116+
# Created by https://www.toptal.com/developers/gitignore/api/macos
117+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
118+
119+
### macOS ###
120+
# General
121+
.DS_Store
122+
.AppleDouble
123+
.LSOverride
124+
125+
# Icon must end with two
126+
Icon
127+
128+
129+
# Thumbnails
130+
._*
131+
132+
# Files that might appear in the root of a volume
133+
.DocumentRevisions-V100
134+
.fseventsd
135+
.Spotlight-V100
136+
.TemporaryItems
137+
.Trashes
138+
.VolumeIcon.icns
139+
.com.apple.timemachine.donotpresent
140+
141+
# Directories potentially created on remote AFP share
142+
.AppleDB
143+
.AppleDesktop
144+
Network Trash Folder
145+
Temporary Items
146+
.apdisk
147+
148+
### macOS Patch ###
149+
# iCloud generated files
150+
*.icloud
151+
152+
# End of https://www.toptal.com/developers/gitignore/api/macos
153+
154+
# Created by https://www.toptal.com/developers/gitignore/api/windows
155+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows
156+
157+
### Windows ###
158+
# Windows thumbnail cache files
159+
Thumbs.db
160+
Thumbs.db:encryptable
161+
ehthumbs.db
162+
ehthumbs_vista.db
163+
164+
# Dump file
165+
*.stackdump
166+
167+
# Folder config file
168+
[Dd]esktop.ini
169+
170+
# Recycle Bin used on file shares
171+
$RECYCLE.BIN/
172+
173+
# Windows Installer files
174+
*.cab
175+
*.msi
176+
*.msix
177+
*.msm
178+
*.msp
179+
180+
# Windows shortcuts
181+
*.lnk
182+
183+
# End of https://www.toptal.com/developers/gitignore/api/windows
184+
185+
#Added by cargo
186+
187+
/target
188+
Cargo.lock
189+
190+
.pnp.*
191+
.yarn/*
192+
!.yarn/patches
193+
!.yarn/plugins
194+
!.yarn/releases
195+
!.yarn/sdks
196+
!.yarn/versions
197+
198+
*.node

wasm/examples/browser/kcl.wasm

6.75 MB
Binary file not shown.

wasm/examples/browser/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "browser-kcl-wasm-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"serve": "webpack serve --config webpack.config.js",
7+
"build": "webpack --mode=production",
8+
"build:dev": "webpack --watch",
9+
"format": "prettier --write ."
10+
},
11+
"main": "./dist/main.js",
12+
"types": "./dist/main.d.ts",
13+
"files": [
14+
"dist/"
15+
],
16+
"dependencies": {
17+
"@kcl-lang/wasm-lib": "file:../..",
18+
"@wasmer/wasi": "^1.2.2"
19+
},
20+
"devDependencies": {
21+
"copy-webpack-plugin": "^8.1.1",
22+
"css-loader": "^5.2.4",
23+
"cssnano": "^5.0.1",
24+
"html-webpack-plugin": "^5.3.1",
25+
"mini-css-extract-plugin": "^1.5.0",
26+
"postcss": "^8.2.10",
27+
"postcss-import": "^14.0.1",
28+
"postcss-loader": "^5.2.0",
29+
"prettier": "^2.8.4",
30+
"typescript": "^5.3.3",
31+
"webpack": "^5.34.0",
32+
"webpack-cli": "^4.6.0",
33+
"webpack-dev-server": "^3.11.2"
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Button Click</title>
5+
</head>
6+
<body>
7+
<button id="myButton">Click Me</button>
8+
9+
<script>
10+
var button = document.getElementById("myButton");
11+
function handleClick() {
12+
var script = document.createElement("script");
13+
script.src = "dist/main.js";
14+
}
15+
button.addEventListener("click", handleClick);
16+
</script>
17+
</body>
18+
</html>

wasm/examples/browser/src/main.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { load, invokeKCLRun } from "@kcl-lang/wasm-lib";
2+
3+
async function main() {
4+
const inst = await load();
5+
const result = invokeKCLRun(inst, {
6+
filename: "test.k",
7+
source: `
8+
schema Person:
9+
name: str
10+
11+
p = Person {name = "Alice"}`,
12+
});
13+
console.log(result);
14+
}
15+
16+
main();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"lib": ["es2022", "dom"],
7+
"sourceMap": true,
8+
"outDir": "./dist",
9+
"rootDir": "./src",
10+
"strict": true,
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"noImplicitThis": true,
14+
"alwaysStrict": true,
15+
"noUnusedLocals": true,
16+
"noUnusedParameters": true,
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"allowSyntheticDefaultImports": true,
20+
"experimentalDecorators": true,
21+
"emitDecoratorMetadata": true,
22+
"declaration": true,
23+
"baseUrl": ".",
24+
"esModuleInterop": true
25+
},
26+
"exclude": ["node_modules"]
27+
}

0 commit comments

Comments
 (0)