Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
wasm-loader
Advanced tools
A simple .wasm
binary file loader for Webpack. Import your wasm modules directly into your bundle as Constructors which return WebAssembly.Instance
. This avoids the need to use fetch and parse for your wasm files. Imported wasm files
are converted to Uint8Arrays and become part of the full JS bundle!
Install package:
npm install --save wasm-loader
Edit webpack.config.js:
loaders: [
{
test: /\.wasm$/,
loaders: ['wasm-loader']
}
]
This is an experimental feature and thus not activated by default.
You can activate it by passing dce=1
to the import and by specifying manually (for now) the exported elements you use, like the following example:
import createInstance from "./add.wasm?dce=1&add&test"
createInstance()
.then(m => {
console.log(m.instance.exports.add(1, 2));
console.log(m.instance.exports.test());
});
Everything else in the add.wasm
binary will be removed.
Grab your pre-built wasm file. For demo purposes we will use the excellent WasmExplorer.
factorial.wasm
file exports a function returning a factorial for a given number.
With the loader you can import this file directy
import makeFactorial from 'wasm/factorial';
The default export from the loader is a function returning native Promise
. The promise resolves to a WebAssembly.Instance.
makeFactorial().then(instance => {
// What is with the weird exports._Z4facti function?
// This is how the function name is encoded by the C++ to wasm compiler
const factorial = instance.exports._Z4facti;
console.log(factorial(1)); // 1
console.log(factorial(2)); // 2
console.log(factorial(3)); // 6
});
deps
can be passed in to
override defaults. For example
makeFactorial({
'global': {},
'env': {
'memory': new WebAssembly.Memory({initial: 100, limit: 1000}),
'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
}
}).then(instance => { /* code here */ });
Default deps are:
{
'global': {},
'env': {
'memory': new Memory({initial: 10, limit: 100}),
'table': new Table({initial: 0, element: 'anyfunc'})
}
}
Default importsObject
is meant to be used for a very basic wasm module. Most likely it will not suffice for something not dead simple compiled with emscripten. This is intentional. Supply your own
imports to match the requirements of your wasm module(s). Some options are compiling your source code into S-syntax(.wast
) examining that output, checking the imports. Compile the s-syntax file with
asm2wasm
into the final wasm module.
FAQs
Webpack loader for WASM
The npm package wasm-loader receives a total of 6,153 weekly downloads. As such, wasm-loader popularity was classified as popular.
We found that wasm-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.