2022-04-14 08:53:53 +02:00
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
const { promisify } = require("util");
|
|
|
|
const exec = promisify(require("child_process").exec);
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
2022-04-22 08:20:21 +02:00
|
|
|
function wasmTextPlugin({ debug } = {}) {
|
2022-04-14 08:53:53 +02:00
|
|
|
return {
|
|
|
|
name: "wasm-text",
|
|
|
|
setup(build) {
|
|
|
|
build.onResolve({ filter: /.\.wat$/ }, async (args) => {
|
|
|
|
if (args.resolveDir === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const watPath = path.isAbsolute(args.path)
|
|
|
|
? args.path
|
|
|
|
: path.join(args.resolveDir, args.path);
|
|
|
|
return {
|
|
|
|
path: watPath,
|
|
|
|
namespace: "wasm-text",
|
|
|
|
watchFiles: [watPath],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
build.onLoad({ filter: /.*/, namespace: "wasm-text" }, async (args) => {
|
|
|
|
// Would be handy if we could get output from stdout without going through file
|
2022-04-15 15:28:48 +02:00
|
|
|
const out = `wat2wasm.${Math.floor(
|
|
|
|
Math.random() * 1000000000
|
|
|
|
)}.tmp.wasm`;
|
|
|
|
try {
|
2022-04-22 08:20:21 +02:00
|
|
|
let flags = [];
|
|
|
|
if (debug) {
|
|
|
|
flags.push("--debug-names");
|
|
|
|
}
|
2022-04-15 15:28:48 +02:00
|
|
|
// console.log("wat: compiling %s", args.path);
|
2022-04-22 08:20:21 +02:00
|
|
|
await exec(
|
|
|
|
`wat2wasm ${flags.join(" ")} --output=${out} ${args.path}`
|
|
|
|
);
|
2022-04-15 15:28:48 +02:00
|
|
|
return {
|
|
|
|
contents: await fs.promises.readFile(out),
|
|
|
|
loader: "binary",
|
|
|
|
};
|
|
|
|
} finally {
|
2022-04-15 15:36:13 +02:00
|
|
|
try {
|
2022-04-15 15:28:48 +02:00
|
|
|
await fs.promises.unlink(out);
|
2022-04-15 15:36:13 +02:00
|
|
|
} catch (e) {
|
|
|
|
// ignore
|
2022-04-15 15:28:48 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 08:53:53 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
wasmTextPlugin,
|
|
|
|
};
|