diff --git a/tools/garaga_rs/wasm-pack-build-and-patch b/tools/garaga_rs/wasm-pack-build-and-patch deleted file mode 100755 index 309106b5..00000000 --- a/tools/garaga_rs/wasm-pack-build-and-patch +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env node - -// This script will patch a 'wasm-pack --target web' output folder -// to make the code platform agnostic (browser vs node.js/commonjs vs esm) - -// Sample usage of garaga_rs as an NPM package: -// import * as garaga_rs from 'garaga_rs'; -// async function main() { -// await garaga_rs.init(); -// garaga_rs.msm_calldata_builder(/* ...args... */); -// } - -const fs = require('fs'); -const path = require('path'); -const child_process = require('child_process'); - -function transpileExports(input) { - // this is a simple/non-comprehensive implementation - // can be replaced by a real transpiler (e.g. npx mjs-to-cjs in.mjs out.cjs) - return input - // deals with: export function ... - .replace(/\bexport\s+function\s+(\w+)\s*\(/g, 'exports.$1 = $1;\nfunction $1(') - // deals with: export { ... } - .replace(/\bexport\s*\{([\w\s,]*)\}\s*;/g, (match, $1) => { - const ids = $1.split(',').map((id) => id.trim()); - return ids.map((id) => 'exports.' + id + ' = ' + id + ';').join('\n'); - }); -} - -function patch() { - // package folder - const pkgFolder = path.join(__dirname, 'pkg'); - - // package file name/path - const jsonName = 'package.json'; - const jsonFile = path.join(pkgFolder, jsonName); - - // package name - const pkgName = JSON.parse(fs.readFileSync(jsonFile, 'utf8'))['name']; - - // typing file name/path - const tsName = pkgName + '.d.ts'; - const tsFile = path.join(pkgFolder, tsName); - - // old file names/paths - const jsName = pkgName + '.js'; - const jsFile = path.join(pkgFolder, jsName); - - const wasmName = pkgName + '_bg.wasm'; - const wasmFile = path.join(pkgFolder, wasmName); - - const wasmtsName = pkgName + '_bg.wasm.d.ts'; - const wasmtsFile = path.join(pkgFolder, wasmtsName); - - // new file names/paths - const cjsName = pkgName + '.cjs'; - const cjsFile = path.join(pkgFolder, cjsName); - - const mjsName = pkgName + '.mjs'; - const mjsFile = path.join(pkgFolder, mjsName); - - const wasmcjsName = pkgName + '_bg.wasm.cjs'; - const wasmcjsFile = path.join(pkgFolder, wasmcjsName); - - const wasmmjsName = pkgName + '_bg.wasm.mjs'; - const wasmmjsFile = path.join(pkgFolder, wasmmjsName); - - // patches .json file: - // - sets .cjs as main - // - sets .mjs as module - // - sets sideEffects to false - // - replaces references to .js file by .cjs and .mjs files - // - replaces references to .wasm file by .wasm.cjs and .wasm.mjs files - // - adds exports section - { - const input = fs.readFileSync(jsonFile, 'utf8'); - const json = JSON.parse(input); - json['main'] = cjsName; - json['module'] = mjsName; - json['sideEffects'] = false; - json['files'] = json['files'] || []; - json['files'] = json['files'].filter((name) => ![jsName, wasmName].includes(name)); - json['files'].push(cjsName); - json['files'].push(mjsName); - json['files'].push(wasmcjsName); - json['files'].push(wasmmjsName); - json['exports'] = json['exports'] || {}; - json['exports']['.'] = json['exports']['.'] || {}; - json['exports']['.']['require'] = './' + json['main']; - json['exports']['.']['import'] = './' + json['module']; - json['exports']['.']['types'] = './' + json['types']; - const output = JSON.stringify(json, undefined, 2); - fs.writeFileSync(jsonFile, output, 'utf8'); - } - - // patches .d.ts file: - // - adds an export entry for the init() function - // - removes the default export - { - const exportFunc = 'export function init(): Promise;'; - const input = fs.readFileSync(tsFile, 'utf8') - .replace(/\bexport\s+default\s+[^;]+;/, ''); - const output = - exportFunc + '\n\n' + - input; - fs.writeFileSync(tsFile, output, 'utf8'); - } - - // creates .mjs file: - // - uses .wasm.mjs file as the default WASM module - // - adds and exports the init() function - // - replaces the default WASM load behavior via URL by direct use - // - removes the default export - { - const importStmt = 'import default_module_or_path from \'./' + wasmmjsName + '\';'; - const exportFunc = 'export function init() { return __wbg_init({ module_or_path: default_module_or_path }); }'; - const input = fs.readFileSync(jsFile, 'utf8') - .replace('new URL(\'' + wasmName + '\', import.meta.url)', 'default_module_or_path') - .replace(/\bexport\s+default\s+[^;]+;/, ''); - const output = - importStmt + '\n\n' + - exportFunc + '\n\n' + - input; - fs.writeFileSync(mjsFile, output, 'utf8'); - } - - // creates .cjs file: - // - uses .wasm.cjs file as the default WASM module - // - adds and exports the init function - // - replaces the default WASM load behavior via URL by the direct use - // - removes the default export - // - transpiles ESM style exports to CommonJS style exports - { - const importStmt = 'const default_module_or_path = require(\'./' + wasmcjsName + '\');'; - const exportFunc = 'exports.init = init;\nfunction init() { return __wbg_init({ module_or_path: default_module_or_path }); }'; - const input = fs.readFileSync(jsFile, 'utf8') - .replace('new URL(\'' + wasmName + '\', import.meta.url)', 'default_module_or_path') - .replace(/\bexport\s+default\s+[^;]+;/, ''); - const output = - '\'use strict\';' + '\n\n' + - importStmt + '\n\n' + - exportFunc + '\n\n' + - transpileExports(input); - fs.writeFileSync(cjsFile, output, 'utf8'); - } - - // encodes .wasm file as .wasm.cjs: - // - creates .wasm.cjs exporting the binary contents of the .wasm file - { - const input = fs.readFileSync(wasmFile).toString('base64'); - const output = - '\'use strict\';' + '\n\n' + - 'module.exports = "data:application/wasm;base64,' + input + '";'; - fs.writeFileSync(wasmcjsFile, output, 'utf8'); - } - - // encodes .wasm file as .wasm.mjs: - // - creates .wasm.mjs exporting the binary contents of the .wasm file - { - const input = fs.readFileSync(wasmFile).toString('base64'); - const output = - 'export default "data:application/wasm;base64,' + input + '";'; - fs.writeFileSync(wasmmjsFile, output, 'utf8'); - } - - // cleans up now obsolete files: - // - removes .js, .wasm, and .wasm.d.ts files - { - fs.rmSync(jsFile); - fs.rmSync(wasmFile); - fs.rmSync(wasmtsFile); - } -} - -function build() { - // executes wasm-pack in the current folder - const command = 'wasm-pack build --target web --release --no-default-features'; - child_process.execSync(command, { cwd: __dirname }); -} - -function clean() { - // removes the pkg folder - const pkgFolder = path.join(__dirname, 'pkg'); - fs.rmSync(pkgFolder, { recursive: true, force: true }); -} - -function main() { - clean(); - build(); - patch(); -} - -main(); diff --git a/tools/make/setup.sh b/tools/make/setup.sh index 36bee8cc..3487c8fb 100755 --- a/tools/make/setup.sh +++ b/tools/make/setup.sh @@ -82,7 +82,6 @@ if ! python3.10 -m venv venv; then exit 1 fi -echo 'export PATH="$PATH:$HOME/.cargo/bin"' >> venv/bin/activate echo 'export PYTHONPATH="$PWD/hydra:$PWD:$PYTHONPATH"' >> venv/bin/activate echo 'export PYTHONPYCACHEPREFIX="$PWD/venv/build/__pycache__"' >> venv/bin/activate echo "PROJECT_ROOT=$PWD" > .env @@ -98,9 +97,6 @@ pre-commit install echo "Compiling garaga_rs Rust extension..." maturin develop --release -echo "Installing wasm-pack" -cargo install wasm-pack - echo "All done!" # Check Scarb version and print warning if it's not