mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const asc = require("assemblyscript/bin/asc.js");
|
|
const base64 = require("@protobufjs/base64");
|
|
|
|
const MAGIC = Buffer.from([ 0x00, 0x61, 0x73, 0x6D ]);
|
|
|
|
module.exports = loader;
|
|
|
|
function loader(buffer) {
|
|
if (MAGIC.compare(target, 0, 4) !== 0)
|
|
return compile.call(this);
|
|
else
|
|
return bundle.call(this, buffer);
|
|
}
|
|
|
|
loader.raw = true;
|
|
|
|
function compile() {
|
|
const basePath = this.resourcePath.replace(/\.\w+$/, "");
|
|
const args = [
|
|
path.basename(this.resourcePath),
|
|
"--baseDir", path.dirname(this.resourcePath),
|
|
"--binaryFile", basePath + ".wasm",
|
|
"--textFile", basePath + ".wast",
|
|
"--validate",
|
|
"--optimize"
|
|
];
|
|
if (this.sourceMap)
|
|
args.push("--sourceMap");
|
|
asc.main(args, err => {
|
|
if (err)
|
|
return this.callback(err);
|
|
fs.readFile(basePath + ".wasm", (err, binary) => {
|
|
if (err)
|
|
return this.callback(err);
|
|
if (!this.sourceMap)
|
|
return this.callback(null, bundle(binary));
|
|
fs.readFile(basePath + ".wasm.map", (err, sourceMap) => {
|
|
if (err)
|
|
return this.callback(err);
|
|
return this.callback(null, bundle(binary), sourceMap.toString("utf8"));
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function bundle(binary) {
|
|
const data = base64.encode(binary, 0, binary.wasm);
|
|
return [
|
|
'var data = "' + data + '", wasm;',
|
|
'module.exports = function AssemblyScriptModule(options) {',
|
|
' if (!wasm)',
|
|
' wasm = new WebAssembly.Module(require("@assemblyscript/webpack/decode")(data));',
|
|
' return new WebAssembly.Instance(wasm, options && options.imports || {}).exports;',
|
|
'};'
|
|
].join("\n") + "\n";
|
|
}
|