Skip to content

Commit 8663234

Browse files
authored
Merge pull request #467 from Peefy/kcl-wasm-lib-browser-target-guide
docs: add kcl wasm lib browser target user guide documents
2 parents 7644286 + 4090163 commit 8663234

File tree

4 files changed

+506
-4
lines changed

4 files changed

+506
-4
lines changed

docs/reference/xlang-api/wasm-api.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,132 @@ sidebar_position: 12
44

55
# WASM API
66

7-
The KCL core is written by Rust and can be compiled to the `wasm-wasi` target using toolchains such as cargo. With the help of WASM, we can also easily achieve multilingual and browser integration. Here is how we can use the KCL WASM module in Node.js, Go and Rust.
7+
The KCL core is written by Rust and can be compiled to the `wasm-wasi` target using toolchains such as cargo. With the help of WASM, we can also easily achieve multilingual and browser integration. Here is how we can use the KCL WASM module in Browser, Node.js, Go and Rust.
88

99
## Quick Start
1010

1111
We can find and download KCL WASM module from [here](https://github.com/kcl-lang/lib/tree/main/wasm)
1212

13+
## Browser
14+
15+
Install the dependency
16+
17+
```shell
18+
npm install buffer @wasmer/wasi @kcl-lang/wasm-lib
19+
```
20+
21+
> Note: buffer is required by @wasmer/wasi.
22+
23+
Write the code
24+
25+
```typescript
26+
import { load, invokeKCLRun } from "@kcl-lang/wasm-lib";
27+
28+
async function main() {
29+
const inst = await load();
30+
const result = invokeKCLRun(inst, {
31+
filename: "test.k",
32+
source: `
33+
schema Person:
34+
name: str
35+
36+
p = Person {name = "Alice"}`,
37+
});
38+
console.log(result);
39+
}
40+
41+
main();
42+
```
43+
44+
Here, we use `webpack` to bundle the website, the `webpack.config.js` config as follows.
45+
46+
> Note: This configuration includes necessary settings for @wasmer/wasi and other required plugins.
47+
48+
```js
49+
const HtmlWebpackPlugin = require("html-webpack-plugin");
50+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
51+
const path = require("path");
52+
const webpack = require("webpack");
53+
54+
const dist = path.resolve("./dist");
55+
const isProduction = process.argv.some((x) => x === "--mode=production");
56+
const hash = isProduction ? ".[contenthash]" : "";
57+
58+
module.exports = {
59+
mode: "development",
60+
entry: {
61+
main: "./src/main.ts",
62+
},
63+
target: "web",
64+
output: {
65+
path: dist,
66+
filename: `[name]${hash}.js`,
67+
clean: true,
68+
},
69+
devServer: {
70+
hot: true,
71+
port: 9000,
72+
},
73+
module: {
74+
rules: [
75+
{
76+
test: /\.css$/,
77+
use: [MiniCssExtractPlugin.loader, "css-loader"],
78+
},
79+
{
80+
test: /\.m?js$/,
81+
resourceQuery: { not: [/(raw|wasm)/] },
82+
},
83+
{
84+
resourceQuery: /raw/,
85+
type: "asset/source",
86+
},
87+
{
88+
resourceQuery: /wasm/,
89+
type: "asset/resource",
90+
generator: {
91+
filename: "wasm/[name][ext]",
92+
},
93+
},
94+
],
95+
},
96+
plugins: [
97+
new MiniCssExtractPlugin(),
98+
new HtmlWebpackPlugin({
99+
template: path.resolve(__dirname, "./src/index.html"),
100+
}),
101+
new webpack.IgnorePlugin({
102+
resourceRegExp: /^(path|ws|crypto|fs|os|util|node-fetch)$/,
103+
}),
104+
// needed by @wasmer/wasi
105+
new webpack.ProvidePlugin({
106+
Buffer: ["buffer", "Buffer"],
107+
}),
108+
],
109+
externals: {
110+
// needed by @wasmer/wasi
111+
"wasmer_wasi_js_bg.wasm": true,
112+
},
113+
resolve: {
114+
fallback: {
115+
// needed by @wasmer/wasi
116+
buffer: require.resolve("buffer/"),
117+
},
118+
},
119+
};
120+
```
121+
122+
For a complete working example, refer to [here](https://github.com/kcl-lang/lib/tree/main/wasm/examples/browser).
123+
124+
### Troubleshooting
125+
126+
If you encounter any issues, make sure:
127+
128+
- All dependencies are correctly installed.
129+
- Your `webpack.config.js` is properly set up.
130+
- You're using a modern browser that supports WebAssembly.
131+
- The KCL WASM module is correctly loaded and accessible.
132+
13133
## Node.js
14134

15135
Install the dependency
@@ -46,6 +166,8 @@ p:
46166
name: Alice
47167
```
48168
169+
The code example can be found [here](https://github.com/kcl-lang/lib/tree/main/wasm/examples/node).
170+
49171
## Rust
50172
51173
In Rust, we use `wasmtime` as an example, and of course, you can also use other runtime that supports WASI to accomplish this.
@@ -82,6 +204,8 @@ The output is
82204
a: 1
83205
```
84206

207+
The code example can be found [here](https://github.com/kcl-lang/lib/tree/main/wasm/examples/rust).
208+
85209
## Go
86210

87211
In Go, we use `wasmtime` as an example, and of course, you can also use other runtime that supports WASI to accomplish this.
@@ -118,3 +242,5 @@ The output is
118242
```yaml
119243
a: 1
120244
```
245+
246+
The code example can be found [here](https://github.com/kcl-lang/lib/tree/main/wasm/examples/go).

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/xlang-api/wasm-api.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,131 @@ sidebar_position: 12
44

55
# WASM API
66

7-
KCL 核心用 Rust 编写,可以使用 cargo 等工具链编译到 `wasm-wasi` 目标。在 WASM 的帮助下,也可以轻松实现多语言和浏览器集成。以下是在 Node.js 环境, Go 和 Rust 中使用 KCL WASM 模块的方法。
7+
KCL 核心用 Rust 编写,可以使用 cargo 等工具链编译到 `wasm-wasi` 目标。在 WASM 的帮助下,也可以轻松实现多语言和浏览器集成。以下是在浏览器, Node.js 环境, Go 和 Rust 中使用 KCL WASM 模块的方法。
88

99
## 快速开始
1010

1111
可以从[这里](https://github.com/kcl-lang/lib/tree/main/wasm)找到 KCL WASM 模块。
1212

13+
## 浏览器环境
14+
15+
安装依赖
16+
17+
```shell
18+
npm install buffer @wasmer/wasi @kcl-lang/wasm-lib
19+
```
20+
21+
> 注意:buffer 是 @wasmer/wasi 所需的依赖。
22+
23+
编写代码
24+
25+
```ts
26+
import { load, invokeKCLRun } from "@kcl-lang/wasm-lib";
27+
28+
async function main() {
29+
const inst = await load();
30+
const result = invokeKCLRun(inst, {
31+
filename: "test.k",
32+
source: `
33+
schema Person:
34+
name: str
35+
p = Person {name = "Alice"}`,
36+
});
37+
console.log(result);
38+
}
39+
40+
main();
41+
```
42+
43+
在这里,我们使用 `webpack` 来打包网站, `webpack.config.js` 的配置如下。
44+
45+
> 注意:此配置包含了 @wasmer/wasi 所需的必要设置和其他必需的插件。
46+
47+
```js
48+
const HtmlWebpackPlugin = require("html-webpack-plugin");
49+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
50+
const path = require("path");
51+
const webpack = require("webpack");
52+
53+
const dist = path.resolve("./dist");
54+
const isProduction = process.argv.some((x) => x === "--mode=production");
55+
const hash = isProduction ? ".[contenthash]" : "";
56+
57+
module.exports = {
58+
mode: "development",
59+
entry: {
60+
main: "./src/main.ts",
61+
},
62+
target: "web",
63+
output: {
64+
path: dist,
65+
filename: `[name]${hash}.js`,
66+
clean: true,
67+
},
68+
devServer: {
69+
hot: true,
70+
port: 9000,
71+
},
72+
module: {
73+
rules: [
74+
{
75+
test: /\.css$/,
76+
use: [MiniCssExtractPlugin.loader, "css-loader"],
77+
},
78+
{
79+
test: /\.m?js$/,
80+
resourceQuery: { not: [/(raw|wasm)/] },
81+
},
82+
{
83+
resourceQuery: /raw/,
84+
type: "asset/source",
85+
},
86+
{
87+
resourceQuery: /wasm/,
88+
type: "asset/resource",
89+
generator: {
90+
filename: "wasm/[name][ext]",
91+
},
92+
},
93+
],
94+
},
95+
plugins: [
96+
new MiniCssExtractPlugin(),
97+
new HtmlWebpackPlugin({
98+
template: path.resolve(__dirname, "./src/index.html"),
99+
}),
100+
new webpack.IgnorePlugin({
101+
resourceRegExp: /^(path|ws|crypto|fs|os|util|node-fetch)$/,
102+
}),
103+
// needed by @wasmer/wasi
104+
new webpack.ProvidePlugin({
105+
Buffer: ["buffer", "Buffer"],
106+
}),
107+
],
108+
externals: {
109+
// needed by @wasmer/wasi
110+
"wasmer_wasi_js_bg.wasm": true,
111+
},
112+
resolve: {
113+
fallback: {
114+
// needed by @wasmer/wasi
115+
buffer: require.resolve("buffer/"),
116+
},
117+
},
118+
};
119+
```
120+
121+
完整的代码示例可以在[这里](https://github.com/kcl-lang/lib/tree/main/wasm/examples/browser)找到。
122+
123+
## 错误排查
124+
125+
如果遇到任何问题,请确保:
126+
127+
- 所有依赖都正确安装。
128+
- webpack.config.js 配置正确。
129+
- 使用的是支持 WebAssembly 的现代浏览器。
130+
- KCL WASM 模块正确加载并可访问。
131+
13132
## Node.js
14133

15134
安装依赖:
@@ -46,6 +165,8 @@ p:
46165
name: Alice
47166
```
48167
168+
完整的代码示例可以在[这里](https://github.com/kcl-lang/lib/tree/main/wasm/examples/node)找到。
169+
49170
## Rust
50171
51172
在 Rust 中,我们以 `wasmtime` 为例,当然你也可以使用其他支持 WASI 的运行时来完成这一任务。
@@ -82,6 +203,8 @@ fn main() -> Result<()> {
82203
a: 1
83204
```
84205

206+
完整的代码示例可以在[这里](https://github.com/kcl-lang/lib/tree/main/wasm/examples/rust)找到。
207+
85208
## Go
86209

87210
在 Go 中,我们以 `wasmtime` 为例,当然你也可以使用其他支持 WASI 的运行时来完成这一任务。
@@ -118,3 +241,5 @@ func main() {
118241
```yaml
119242
a: 1
120243
```
244+
245+
完整的代码示例可以在[这里](https://github.com/kcl-lang/lib/tree/main/wasm/examples/go)找到。

0 commit comments

Comments
 (0)