emsdk-env 0.7.0 → 0.9.0

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 CHANGED
@@ -28,6 +28,8 @@ export default defineConfig({
28
28
  plugins: [
29
29
  // Add as a plugin
30
30
  emsdkEnv({
31
+ // Generate a runtime loader code
32
+ generatedLoader: { enable: true },
31
33
  // Build targets
32
34
  targets: {
33
35
  // Generate "add.wasm"
@@ -35,7 +37,9 @@ export default defineConfig({
35
37
  // Compiler options
36
38
  options: ['-O3', '-std=c99'],
37
39
  // Linker options
38
- linkOptions: ['-s', 'STANDALONE_WASM=1', '--no-entry'],
40
+ linkOptions: ['--no-entry'],
41
+ // Linker directives
42
+ linkDirectives: { STANDALONE_WASM: 1 },
39
43
  // Exported symbols
40
44
  exports: ['_add'],
41
45
  },
@@ -86,12 +90,15 @@ project/
86
90
  ├── package.json
87
91
  ├── vite.config.ts
88
92
  ├── src/
93
+ │ ├── generated/
94
+ │ │ └── wasm-loader.ts // (Generate automatically)
89
95
  │ └── wasm/
90
- │ └── add.wasm
96
+ │ └── add.wasm // (Built WASM binary)
91
97
  └── wasm/
92
98
  └── add.c
93
99
  ```
94
100
 
101
+ - `wasm-loader.ts` is helper code that loads and makes WASM binaries usable.
95
102
  - In addition to the above, a temporary build directory is created under the OS temp directory.
96
103
  The default location is `${TMPDIR}/emsdk-env` (typically `/tmp/emsdk-env` on Unix).
97
104
  This directory is used during the build process and is typically deleted after the build completes.
@@ -99,7 +106,36 @@ project/
99
106
 
100
107
  Of course, you can change these. Specify them in the Vite plugin options.
101
108
 
102
- You might find it odd that the built binary is placed in `src/wasm/`, but this is because the Vite server defaults to a path where it can easily access WASM binaries.
109
+ You might find it odd that the built binary is placed in `src/wasm/`,
110
+ but this is because the Vite server defaults to a path where it can easily access WASM binaries.
111
+
112
+ If `generatedLoader.enable` is set to `true`, emsdk-env also generates a WASM loader helper code by default at `src/generated/wasm-loader.ts`.
113
+ That loader can call the final WASM exports directly:
114
+
115
+ ```typescript
116
+ import { loadAddWasm } from './generated/wasm-loader';
117
+
118
+ // WASM exported function declaration (You need to define it)
119
+ interface AddExports {
120
+ add?: (a: number, b: number) => number;
121
+ }
122
+
123
+ // Load WASM binary and instantiates it
124
+ const wasm = await loadAddWasm<AddExports>();
125
+
126
+ // Get `add()` function entry point
127
+ const add = wasm.exports.add;
128
+ if (typeof add !== 'function') {
129
+ throw new Error('add function not found in wasm exports.');
130
+ }
131
+
132
+ // Then use it now
133
+ const result = add(1, 2);
134
+ ```
135
+
136
+ - You need to define WASM export functions yourself.
137
+ When doing so, the symbol name for the exported function is the same as the C/C++ function name in TypeScript,
138
+ but the symbol name specified in `exports: [...]` typically requires an underscore prefix (`add()` --> `_add`).
103
139
 
104
140
  If you plan to operate with the default settings, there is essentially no configuration work required.
105
141