From 046f33717676351fdd468594973f4e9a1d9cad0d Mon Sep 17 00:00:00 2001 From: Omar Tawfik <15987992+OmarTawfik@users.noreply.github.com> Date: Wed, 4 Sep 2024 09:41:02 -0700 Subject: [PATCH] use `import.meta.resolve` instead of hard-coded paths (#493) --- src/cmd/opt.js | 13 ++++--------- src/cmd/run.js | 6 +++--- src/cmd/transpile.js | 11 +++-------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/cmd/opt.js b/src/cmd/opt.js index e8e630707..3a1ceeb6e 100644 --- a/src/cmd/opt.js +++ b/src/cmd/opt.js @@ -6,13 +6,6 @@ import c from 'chalk-template'; import { readFile, sizeStr, fixedDigitDisplay, table, spawnIOTmp, setShowSpinner, getShowSpinner } from '../common.js'; import ora from '#ora'; -let WASM_OPT; -try { - WASM_OPT = fileURLToPath(new URL('../../node_modules/binaryen/bin/wasm-opt', import.meta.url)); -} catch { - WASM_OPT = new URL('../../node_modules/binaryen/bin/wasm-opt', import.meta.url); -} - export async function opt (componentPath, opts, program) { await $init; const varIdx = program.parent.rawArgs.indexOf('--'); @@ -139,9 +132,11 @@ export async function optimizeComponent (componentBytes, opts) { * @param {Uint8Array} source * @returns {Promise} */ -async function wasmOpt (source, args = ['-O1', '--low-memory-unused', '--enable-bulk-memory']) { +async function wasmOpt(source, args = ['-O1', '--low-memory-unused', '--enable-bulk-memory']) { + const wasmOptPath = fileURLToPath(import.meta.resolve('binaryen/bin/wasm-opt')); + try { - return await spawnIOTmp(WASM_OPT, source, [ + return await spawnIOTmp(wasmOptPath, source, [ ...args, '-o' ]); } catch (e) { diff --git a/src/cmd/run.js b/src/cmd/run.js index b44914371..8faecbe01 100644 --- a/src/cmd/run.js +++ b/src/cmd/run.js @@ -85,9 +85,9 @@ async function runComponent (componentPath, args, opts, executor) { await writeFile(resolve(outDir, 'package.json'), JSON.stringify({ type: 'module' })); - let preview2ShimPath + let preview2ShimPath; try { - preview2ShimPath = fileURLToPath(new URL('../..', import.meta.resolve('@bytecodealliance/preview2-shim'))); + preview2ShimPath = resolve(fileURLToPath(import.meta.resolve('@bytecodealliance/preview2-shim')), '../../../'); } catch { throw c`Unable to locate the {bold @bytecodealliance/preview2-shim} package, make sure it is installed.`; } @@ -130,4 +130,4 @@ async function runComponent (componentPath, args, opts, executor) { // empty } } -} \ No newline at end of file +} diff --git a/src/cmd/transpile.js b/src/cmd/transpile.js index 20c56d8b4..6a58dda17 100644 --- a/src/cmd/transpile.js +++ b/src/cmd/transpile.js @@ -102,20 +102,15 @@ export async function transpile (componentPath, opts, program) { await writeFiles(files, opts.quiet ? false : 'Transpiled JS Component Files'); } -let WASM_2_JS; -try { - WASM_2_JS = fileURLToPath(new URL('../../node_modules/binaryen/bin/wasm2js', import.meta.url)); -} catch { - WASM_2_JS = new URL('../../node_modules/binaryen/bin/wasm2js', import.meta.url); -} - /** * @param {Uint8Array} source * @returns {Promise} */ async function wasm2Js (source) { + const wasm2jsPath = fileURLToPath(import.meta.resolve('binaryen/bin/wasm2js')); + try { - return await spawnIOTmp(WASM_2_JS, source, ['-Oz', '-o']); + return await spawnIOTmp(wasm2jsPath, source, ['-Oz', '-o']); } catch (e) { if (e.toString().includes('BasicBlock requested')) return wasm2Js(source);