Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 17, 2021. It is now read-only.
/wasm-loaderPublic archive

✨ WASM webpack loader

License

NotificationsYou must be signed in to change notification settings

ballercat/wasm-loader

Repository files navigation

Build StatusPackage Quality

⚠️⚠️ This loader is DEPRECATED. Use NATIVE Webpack 5 Support for WebAssembly as describedhere or follow a tiny demo examplehere.⚠️⚠️

WASM Binary Module loader for Webpack

A simple.wasm binary file loader for Webpack. Import your wasm modules directly into your bundle as Constructors which returnWebAssembly.Instance. This avoids the need to use fetch and parse for your wasm files. Imported wasm filesare converted to Uint8Arrays and become part of the full JS bundle!

Install

Install package:npm install --save wasm-loader

Usage

Edit webpack.config.js:

  loaders: [    {      test: /\.wasm$/,      loaders: ['wasm-loader']    }  ]

Optimizations

Dead code elemination

This is an experimental feature and thus not activated by default.

You can activate it by passingdce=1 to the import and by specifying manually (for now) the exported elements you use, like the following example:

importcreateInstancefrom"./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 theadd.wasm binary will be removed.

Include wasm from your code

Grab your pre-built wasm file. For demo purposes we will use the excellentWasmExplorer.factorial.wasm file exports a function returning a factorial for a given number.

With the loader you can import this file directy

importmakeFactorialfrom'wasm/factorial';

The default export from the loader is a function returning nativePromise. The promise resolves to aWebAssembly.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 compilerconstfactorial=instance.exports._Z4facti;console.log(factorial(1));// 1console.log(factorial(2));// 2console.log(factorial(3));// 6});

deps can be passed in tooverride defaults. For example

makeFactorial({'global':{},'env':{'memory':newWebAssembly.Memory({initial:100,limit:1000}),'table':newWebAssembly.Table({initial:0,element:'anyfunc'})}}).then(instance=>{/* code here */});

Default deps are:

{'global':{},'env':{'memory':newMemory({initial:10,limit:100}),'table':newTable({initial:0,element:'anyfunc'})}}

A note about default deps(importsObject)

DefaultimportsObject 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 ownimports 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 withasm2wasm into the final wasm module.

About

✨ WASM webpack loader

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp