@farmfe/plugin-wasm 0.0.1 → 0.0.3

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 for importing 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
+ Pre-compiled `.wasm` files can be imported using the `?init` query. The default export is an initialization function, which returns a Promise resolving to a [`WebAssembly.Instance`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance) object:
28
+
29
+ ```ts
30
+ import init from './example.wasm?init';
31
+ init().then((instance) => {
32
+ instance.exports.test();
33
+ });
34
+ ```
35
+
36
+ The `init` function can also take an import object as its second parameter, which is passed to [`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
+ In production builds, `.wasm` files smaller than the `assetInlineLimit` will be inlined as base64 strings. Otherwise, they will be treated as static assets and fetched on demand.
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.3",
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.3",
40
+ "farmfe-plugin-wasm-darwin-x64": "0.0.3",
41
+ "farmfe-plugin-wasm-linux-arm64-gnu": "0.0.3",
42
+ "farmfe-plugin-wasm-linux-arm64-musl": "0.0.3",
43
+ "farmfe-plugin-wasm-linux-x64-gnu": "0.0.3",
44
+ "farmfe-plugin-wasm-linux-x64-musl": "0.0.3",
45
+ "farmfe-plugin-wasm-win32-arm64-msvc": "0.0.3",
46
+ "farmfe-plugin-wasm-win32-ia32-msvc": "0.0.3",
47
+ "farmfe-plugin-wasm-win32-x64-msvc": "0.0.3"
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
+ }