Comparing version
{ | ||
"name": "unwasm", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "WebAssembly tools for JavaScript", | ||
@@ -35,3 +35,5 @@ "repository": "unjs/unwasm", | ||
"*.d.ts", | ||
"examples/*.wasm" | ||
"examples/*.wasm", | ||
"examples/package.json", | ||
"examples/*.mjs" | ||
], | ||
@@ -54,2 +56,3 @@ "scripts": { | ||
"pathe": "^1.1.1", | ||
"pkg-types": "^1.0.3", | ||
"unplugin": "^1.6.0" | ||
@@ -59,4 +62,4 @@ }, | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@types/node": "^20.10.5", | ||
"@vitest/coverage-v8": "^1.1.0", | ||
"@types/node": "^20.10.6", | ||
"@vitest/coverage-v8": "^1.1.1", | ||
"assemblyscript": "^0.27.22", | ||
@@ -69,9 +72,9 @@ "changelogen": "^0.5.5", | ||
"prettier": "^3.1.1", | ||
"rollup": "^4.9.1", | ||
"rollup": "^4.9.2", | ||
"typescript": "^5.3.3", | ||
"unbuild": "^2.0.0", | ||
"vite": "^5.0.10", | ||
"vitest": "^1.1.0" | ||
"vitest": "^1.1.1" | ||
}, | ||
"packageManager": "pnpm@8.12.1" | ||
"packageManager": "pnpm@8.13.1" | ||
} |
@@ -28,10 +28,11 @@ [![npm version][npm-version-src]][npm-version-href] | ||
- [ ] Convention for library authors exporting wasm modules ([unjs/unwasm#7](https://github.com/unjs/unwasm/issues/7)) | ||
- [x] Auto resolve imports from the imports map | ||
## Bindings API | ||
When importing a `.wasm` module, unwasm resolves, reads, and then parses the module during build process to get the information about imports and exports and generate appropriate code bindings. | ||
When importing a `.wasm` module, unwasm resolves, reads, and then parses the module during the build process to get the information about imports and exports and even tries to [automatically resolve imports](#auto-imports) and generate appropriate code bindings for the bundler. | ||
If the target environment supports [top level `await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await) and also the wasm module requires no imports object (auto-detected after parsing), unwasm generates bindings to allow importing wasm module like any other ESM import. | ||
If the target environment supports [top level `await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await) and also the wasm module requires no imports object (or they are auto resolvable), unwasm generates bindings to allow importing wasm module like any other ESM import. | ||
If the target environment lacks support for top level `await` or the wasm module requires imports object or `lazy` plugin option is set to `true`, unwasm will export a wrapped [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object which can be called as a function to lazily evaluate the module with custom imports object. This way we still have a simple syntax as close as possible to ESM modules and also we can lazily initialize modules. | ||
If the target environment lacks support for top-level `await` or the wasm module requires an imports object or `lazy` plugin option is set to `true`, unwasm will export a wrapped [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object which can be called as a function to evaluate the module with custom imports object lazily. This way we still have a simple syntax as close as possible to ESM modules and also we can lazily initialize modules. | ||
@@ -50,3 +51,3 @@ **Example:** Using static import | ||
If your WebAssembly module requires an import object (which is likely!), the usage syntax would be slightly different as we need to initiate the module with an import object first. | ||
If your WebAssembly module requires an import object (unwasm can [automatically infer them](#auto-imports)), the usage syntax would be slightly different as we need to initiate the module with an import object first. | ||
@@ -174,2 +175,23 @@ **Example:** Using dynamic import with imports object | ||
## Auto Imports | ||
unwasm can automatically infer the imports object and bundle them using imports maps (read more: [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap), [Node.js](https://nodejs.org/api/packages.html#imports) and [WICG](https://github.com/WICG/import-maps)). | ||
To hint to the bundler how to resolve imports needed by the `.wasm` file, you need to define them in a parent `package.json` file. | ||
**Example:** | ||
```js | ||
{ | ||
"exports": { | ||
"./rand.wasm": "./rand.wasm" | ||
}, | ||
"imports": { | ||
"env": "./env.mjs" | ||
} | ||
} | ||
``` | ||
**Note:** The imports can also be prefixed with `#` like `#env` if you like to respect Node.js conventions. | ||
## Development | ||
@@ -176,0 +198,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
34820
12.8%19
18.75%733
15.62%217
11.28%6
20%+ Added