@farmfe/plugin-wasm 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @farmfe/plugin-wasm
2
+
3
+ A farm plugin which imports Wasm modules.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i -D @farmfe/plugin-wasm
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Create a `farm.config.js` [configuration file](https://www.farmfe.org/docs/config/configuring-farm) and import the plugin:
14
+
15
+ ```ts
16
+ import { defineConfig } from '@farmfe/core';
17
+ import wasm from '@farmfe/plugin-wasm';
18
+ export default defineConfig({
19
+ plugins: [
20
+ wasm(),
21
+ ],
22
+ });
23
+ ```
24
+
25
+ ## WebAssembly
26
+
27
+ 预编译的 `.wasm` 文件可以通过`?init`来导入。 默认导出一个初始化函数,返回值为所导出 [`WebAssembly.Instance`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance) 实例对象的 Promise:
28
+
29
+ ```ts
30
+ import init from './example.wasm?init'
31
+ init().then((instance) => {
32
+ instance.exports.test()
33
+ })
34
+ ```
35
+
36
+ `init` 函数还可以将传递给 [`WebAssembly.Instance`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance) 的导入对象作为其第二个参数:
37
+
38
+ ```ts
39
+ import init from './example.wasm?init'
40
+ init({
41
+ imports: {
42
+ someFunc: () => {
43
+ /* ... */
44
+ },
45
+ },
46
+ }).then(() => {
47
+ /* ... */
48
+ })
49
+ ```
50
+
51
+ 在生产构建当中,体积小于 assetInlineLimit 的 .wasm 文件将会被内联为 base64 字符串。否则,它们将被视为 静态资源 ,并按需获取。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farmfe/plugin-wasm",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "scripts/index.js",
5
5
  "types": "scripts/index.d.ts",
6
6
  "type": "module",
@@ -27,22 +27,24 @@
27
27
  "types": "./scripts/index.d.ts",
28
28
  "default": "./scripts/index.js"
29
29
  },
30
- "./package.json": "./package.json"
30
+ "./package.json": "./package.json",
31
+ "./types": "./types/index.d.ts"
31
32
  },
32
33
  "files": [
33
34
  "scripts",
35
+ "types",
34
36
  "options.d.ts"
35
37
  ],
36
38
  "optionalDependencies": {
37
- "farmfe-plugin-wasm-darwin-arm64": "0.0.1",
38
- "farmfe-plugin-wasm-darwin-x64": "0.0.1",
39
- "farmfe-plugin-wasm-linux-arm64-gnu": "0.0.1",
40
- "farmfe-plugin-wasm-linux-arm64-musl": "0.0.1",
41
- "farmfe-plugin-wasm-linux-x64-gnu": "0.0.1",
42
- "farmfe-plugin-wasm-linux-x64-musl": "0.0.1",
43
- "farmfe-plugin-wasm-win32-arm64-msvc": "0.0.1",
44
- "farmfe-plugin-wasm-win32-ia32-msvc": "0.0.1",
45
- "farmfe-plugin-wasm-win32-x64-msvc": "0.0.1"
39
+ "farmfe-plugin-wasm-darwin-arm64": "0.0.2",
40
+ "farmfe-plugin-wasm-darwin-x64": "0.0.2",
41
+ "farmfe-plugin-wasm-linux-arm64-gnu": "0.0.2",
42
+ "farmfe-plugin-wasm-linux-arm64-musl": "0.0.2",
43
+ "farmfe-plugin-wasm-linux-x64-gnu": "0.0.2",
44
+ "farmfe-plugin-wasm-linux-x64-musl": "0.0.2",
45
+ "farmfe-plugin-wasm-win32-arm64-msvc": "0.0.2",
46
+ "farmfe-plugin-wasm-win32-ia32-msvc": "0.0.2",
47
+ "farmfe-plugin-wasm-win32-x64-msvc": "0.0.2"
46
48
  },
47
49
  "scripts": {
48
50
  "dev": "cargo watch -w src -s 'scripts/watch.sh'",
@@ -0,0 +1,6 @@
1
+ declare module '*.wasm?init' {
2
+ const initWasm: (
3
+ options?: WebAssembly.Imports,
4
+ ) => Promise<WebAssembly.Instance>
5
+ export default initWasm
6
+ }