diff --git a/package-lock.json b/package-lock.json index cb5ef10..16413fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,8 @@ "@discoveryjs/discovery": "^1.0.0-beta.81", "@typescript-eslint/eslint-plugin": "^5.21.0", "@typescript-eslint/parser": "^5.21.0", - "eslint": "^8.7.0" + "eslint": "^8.7.0", + "wabt": "^1.0.32" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -3444,6 +3445,23 @@ "node": ">= 0.8" } }, + "node_modules/wabt": { + "version": "1.0.32", + "resolved": "https://registry.npmjs.org/wabt/-/wabt-1.0.32.tgz", + "integrity": "sha512-1aHvkKaSrrl7qFtAbQ1RWVHLuJApRh7PtUdYvRtiUEKEhk0MOV0sTuz5cLF6jL5jPLRyifLbZcR65AEga/xBhQ==", + "dev": true, + "bin": { + "wasm-decompile": "bin/wasm-decompile", + "wasm-interp": "bin/wasm-interp", + "wasm-objdump": "bin/wasm-objdump", + "wasm-opcodecnt": "bin/wasm-opcodecnt", + "wasm-strip": "bin/wasm-strip", + "wasm-validate": "bin/wasm-validate", + "wasm2c": "bin/wasm2c", + "wasm2wat": "bin/wasm2wat", + "wat2wasm": "bin/wat2wasm" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 8d9fb72..b2a19c8 100644 --- a/package.json +++ b/package.json @@ -33,11 +33,11 @@ "scripts": { "lint": "eslint src", "start": "discovery", - "build": "npm run build-app && npm run build-report-template", - "build-wasm": "wat2wasm app/prepare/build-trees.wat -o app/prepare/build-trees.wasm", + "build": "npm run build-wasm && npm run build-app && npm run build-report-template", + "build-wasm": "node scripts/wat-compile.js", "build-app": "discovery-build --clean --config .discoveryrc.app.js --single-file --no-data --entry-names \"app\"", "build-report-template": "discovery-build --single-file --no-data --no-model-data-upload --entry-names \"report\"", - "build-gh-pages": "discovery-build --config .discoveryrc.app.js --single-file --no-data -o .gh-pages", + "build-gh-pages": "npm run build-wasm && discovery-build --config .discoveryrc.app.js --single-file --no-data -o .gh-pages", "prepublishOnly": "npm run build" }, "dependencies": { @@ -51,6 +51,7 @@ "@discoveryjs/discovery": "^1.0.0-beta.81", "@typescript-eslint/eslint-plugin": "^5.21.0", "@typescript-eslint/parser": "^5.21.0", - "eslint": "^8.7.0" + "eslint": "^8.7.0", + "wabt": "^1.0.32" } } diff --git a/scripts/wat-compile.js b/scripts/wat-compile.js new file mode 100644 index 0000000..ef907bf --- /dev/null +++ b/scripts/wat-compile.js @@ -0,0 +1,37 @@ +/* eslint-env node */ +const path = require('path'); +const fs = require('fs'); +const wabtPromise = require('wabt')(); + +const files = [ + '../app/prepare/build-trees.wat' +].map(relpath => path.join(__dirname, relpath)); + +async function compileWatToWasm() { + console.log('Compile WAT into WASM'); + console.log(); + + const wabt = await wabtPromise; + + for (let filepath of files) { + process.stdout.write(path.relative(process.cwd(), filepath) + '...'); + + const destPath = filepath.replace(/\.wat$/, '.wasm'); + const watFileContent = fs.readFileSync(filepath, 'utf8'); + const wasmModule = wabt.parseWat(filepath, watFileContent); + const { buffer } = wasmModule.toBinary({}); + + fs.writeFileSync(destPath, buffer); + + console.log('OK'); + console.log(' Written to', path.relative(process.cwd(), destPath)); + } +} + +if (module === require.main) { + compileWatToWasm(); +} + +module.exports = { + compileWatToWasm +};