Skip to content

Commit

Permalink
use import.meta.resolve instead of hard-coded paths (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik authored Sep 4, 2024
1 parent 9ae2fef commit 046f337
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
13 changes: 4 additions & 9 deletions src/cmd/opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('--');
Expand Down Expand Up @@ -139,9 +132,11 @@ export async function optimizeComponent (componentBytes, opts) {
* @param {Uint8Array} source
* @returns {Promise<Uint8Array>}
*/
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) {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
}
Expand Down Expand Up @@ -130,4 +130,4 @@ async function runComponent (componentPath, args, opts, executor) {
// empty
}
}
}
}
11 changes: 3 additions & 8 deletions src/cmd/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>}
*/
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);
Expand Down

0 comments on commit 046f337

Please sign in to comment.