Skip to content
webpack loader that compiles assemblyscript to webassembly(wasm) and bundles it
JavaScript Other
Branch: master
Clone or download

README.md

assemblyscript-live-loader

Compile assemblyscript and WASM binary module loader for Webpack

  • Support fallback normal javascript module which is transpiled by typescript compiler.

Installation

npm install --save-dev assemblyscript-live-loader

Config

Add a webpack loader configuration into webpack.config.js

module: {
    loaders: [
        {
            test: /\.asc$/,       // your assemblyscript file extension
            exclude: 'node_modules/',
            loader: 'assemblyscript-live-loader'
        }
    ]
}

Example

Calculator.asc will be transformed to wasm and WebAssembly.Module. You can use it in javascript

export function add(a: int, b: int): int {
    return a + b;
}

export function subtract(a: int, b: int): int {
    return a - b;
}

export function multiply(a: int, b: int): int {
    return a * b;
}

export function divide(a: int, b: int): int {
    return a / b;
}

index.js

// Calculator is WebAssembly module
import Calculator from './asc/Calculator.asc';

const calc = new Calculator().exports;
const add = calc.add(44, 8832);
const subtract = calc.subtract(100, 20);
const multiply = calc.multiply(13, 4);
const divide = calc.divide(20, 4);

console.log(add);
console.log(subtract);
console.log(multiply);
console.log(divide);

Example: Bundled resource

var buffer = new ArrayBuffer(1163);var uint8 = new Uint8Array(buffer);uint8.set([0,97,115,109,1,0,0,0,1,150,128,128,128,0,4,96,3,127,127,127,1,127,96,2,127,127,1,127,96,1,127,1,127,96,0,0,3,136,128,128,128,0,7,0,2,1,1,1,1,3,4,132,128,128,128,0,1,112,0,0,5,131,128,128,128,0,1,0,1,6,134,128,128,128,0,1,127,1,65,0,11,7,175,128,128,128,0,5,6,109,101,109,111,114,121,2,0,3,97,100,100,0,2,8,115,117,98,116,114,97,99,116,0,3,8,109,117,108,116,105,112,108,121,0,4,6,100,105,118,105,100,101,0,5,8,129,128,128,128,0,6,9,129,128,128,128,0,0,10,161,135,128,128,0,7,255,130,128,128,0,2,2,127,1,126,2,127,2,64,32,2,69,13,0,32,0,32,2,106,34,3,65,127,106,32,1,58,0,0,32,0,32,1,58,0,0,32,2,65,3,73,13,0,32,3,65,126,106,32,1,58,0,0,32,0,32,1,58,0,1,32,3,65,125,106,32,1,58,0,0,32,0,32,1,58,0,2,32,2,65,7,73,13,0,32,3,65,124,106,32,1,58,0,0,32,0,32,1,58,0,3,32,2,65,9,73,13,0,32,0,65,0,32,0,107,65,3,113,34,4,106,34,3,32,1,65,255,1,113,65,129,130,132,8,108,34,1,54,2,0,32,3,32,2,32,4,107,65,124,113,34,4,106,34,2,65,124,106,32,1,54,2,0,32,4,65,9,73,13,0,32,3,32,1,54,2,8,32,3,32,1,54,2,4,32,2,65,120,106,32,1,54,2,0,32,2,65,116,106,32,1,54,2,0,32,4,65,25,73,13,0,32,3,32,1,54,2,16,32,3,32,1,54,2,12,32,3,32,1,54,2,20,32,3,32,1,54,2,24,32,2,65,104,106,32,1,54,2,0,32,2,65,100,106,32,1,54,2,0,32,2,65,108,106,32,1,54,2,0,32,2,65,112,106,32,1,54,2,0,32,4,32,3,65,4,113,65,24,114,34,4,107,34,2,65,32,73,13,0,32,1,173,34,5,66,32,134,32,5,132,33,5,32,3,32,4,106,33,1,3,64,32,1,32,5,55,3,0,32,1,65,8,106,32,5,55,3,0,32,1,65,16,106,32,5,55,3,0,32,1,65,24,106,32,5,55,3,0,32,1,65,32,106,33,1,32,2,65,96,106,34,2,65,31,75,13,0,11,11,32,0,11,11,218,131,128,128,0,1,8,127,2,127,32,0,33,3,63,0,65,16,116,32,0,107,33,4,65,192,0,65,192,0,40,2,0,65,16,107,34,8,54,2,0,65,0,33,6,65,200,0,40,2,0,34,2,69,4,64,65,204,0,66,128,128,132,128,128,128,192,0,55,2,0,65,212,0,66,127,55,2,0,65,200,0,32,8,65,12,106,65,112,113,65,216,170,213,170,5,115,34,2,54,2,0,65,220,0,65,0,54,2,0,11,2,64,32,4,65,137,4,73,13,0,65,0,33,6,65,248,123,65,204,0,40,2,0,107,32,4,77,13,0,65,0,33,1,32,3,65,120,32,3,107,65,7,113,65,0,32,3,65,8,106,65,7,113,27,106,34,0,65,8,106,34,6,65,0,65,224,3,16,0,33,5,32,0,65,227,3,54,2,4,32,0,65,188,3,106,32,4,54,2,0,32,0,65,184,3,106,32,4,54,2,0,32,0,65,204,3,106,32,4,54,2,0,32,0,65,44,106,32,2,54,2,0,32,0,65,40,106,65,127,54,2,0,32,0,65,24,106,32,3,54,2,0,32,0,65,200,3,106,32,3,54,2,0,32,0,65,216,3,106,65,0,54,2,0,65,220,0,40,2,0,33,2,32,0,65,220,3,106,65,0,54,2,0,32,0,65,196,3,106,32,2,65,4,114,54,2,0,3,64,32,0,32,1,106,34,2,65,56,106,32,2,65,48,106,34,7,54,2,0,32,2,65,60,106,32,7,54,2,0,32,1,65,8,106,34,1,65,128,2,71,13,0,11,32,5,32,5,65,124,106,40,2,0,65,120,113,106,34,1,65,120,106,34,2,65,0,32,1,107,65,7,113,65,0,32,1,65,7,113,27,34,1,106,34,7,32,3,32,4,106,34,0,32,2,107,65,88,106,32,1,107,34,1,65,1,114,54,2,4,32,5,65,8,54,2,204,3,32,5,65,216,0,40,2,0,54,2,28,32,5,32,7,54,2,24,32,5,32,1,54,2,12,32,0,65,92,106,65,40,54,2,0,11,65,192,0,32,8,65,16,106,54,2,0,32,6,11,11,135,128,128,128,0,0,32,0,32,1,106,11,135,128,128,128,0,0,32,0,32,1,107,11,135,128,128,128,0,0,32,0,32,1,108,11,135,128,128,128,0,0,32,0,32,1,109,11,136,128,128,128,0,0,65,8,16,1,36,0,11,0,202,128,128,128,0,4,110,97,109,101,1,191,128,128,128,0,7,0,6,109,101,109,115,101,116,1,11,109,115,112,97,99,101,95,105,110,105,116,2,3,97,100,100,3,8,115,117,98,116,114,97,99,116,4,8,109,117,108,116,105,112,108,121,5,6,100,105,118,105,100,101,6,6,46,115,116,97,114,116,]);// This file will not run on it's own

var WebAssemblyModule = function(deps) {
    var defaultDeps = {
        'global': { },
        'env': {
            'memory': new WebAssembly.Memory({
                initial: 10,
                limit: 100}),
            'table': new WebAssembly.Table({
                initial: 0,
                element: 'anyfunc'})
        }
    }

    return new WebAssembly.Instance(new WebAssembly.Module(buffer), deps || defaultDeps);
};

module.exports = WebAssemblyModule;

Uses

module.exports = {
    extends: 'tui'
};

Dependencies

  • assemblyscript
  • wasm-loader -> I refers wasm module loader codes because there is some error for uglifyingjs (it contains both es5, es6 syntax).
You can’t perform that action at this time.