@bytecodealliance/jco 1.13.3 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cmd/opt.js CHANGED
@@ -1,8 +1,8 @@
1
+ import { writeFile } from 'node:fs/promises';
2
+
1
3
  import { $init, tools } from '../../obj/wasm-tools.js';
2
4
  const { metadataShow, print } = tools;
3
- import { writeFile } from 'fs/promises';
4
5
  import { fileURLToPath } from 'url';
5
- import c from 'chalk-template';
6
6
  import {
7
7
  readFile,
8
8
  sizeStr,
@@ -14,6 +14,8 @@ import {
14
14
  } from '../common.js';
15
15
  import ora from '#ora';
16
16
 
17
+ import { styleText } from '../common.js';
18
+
17
19
  export async function opt(componentPath, opts, program) {
18
20
  await $init;
19
21
  const varIdx = program.parent.rawArgs.indexOf('--');
@@ -34,33 +36,35 @@ export async function opt(componentPath, opts, program) {
34
36
  totalAfterBytes = 0;
35
37
 
36
38
  if (!opts.quiet) {
37
- console.log(c`
38
- {bold Optimized WebAssembly Component Internal Core Modules:}
39
+ const tableContent = table(
40
+ [
41
+ ...compressionInfo.map(({ beforeBytes, afterBytes }, i) => {
42
+ totalBeforeBytes += beforeBytes;
43
+ totalAfterBytes += afterBytes;
44
+ return [
45
+ ` - Core Module ${i + 1}: `,
46
+ sizeStr(beforeBytes),
47
+ ' -> ',
48
+ `${styleText('cyan', sizeStr(afterBytes))} `,
49
+ `(${fixedDigitDisplay((afterBytes / beforeBytes) * 100, 2)}%)`,
50
+ ];
51
+ }),
52
+ ['', '', '', '', ''],
53
+ [
54
+ ` = Total: `,
55
+ `${sizeStr(totalBeforeBytes)}`,
56
+ ` => `,
57
+ `${styleText('cyan', sizeStr(totalAfterBytes))} `,
58
+ `(${fixedDigitDisplay((totalAfterBytes / totalBeforeBytes) * 100, 2)}%)`,
59
+ ],
60
+ ],
61
+ [, , , , 'right']
62
+ );
39
63
 
40
- ${table(
41
- [
42
- ...compressionInfo.map(({ beforeBytes, afterBytes }, i) => {
43
- totalBeforeBytes += beforeBytes;
44
- totalAfterBytes += afterBytes;
45
- return [
46
- ` - Core Module ${i + 1}: `,
47
- sizeStr(beforeBytes),
48
- ' -> ',
49
- c`{cyan ${sizeStr(afterBytes)}} `,
50
- `(${fixedDigitDisplay((afterBytes / beforeBytes) * 100, 2)}%)`,
51
- ];
52
- }),
53
- ['', '', '', '', ''],
54
- [
55
- ` = Total: `,
56
- `${sizeStr(totalBeforeBytes)}`,
57
- ` => `,
58
- c`{cyan ${sizeStr(totalAfterBytes)}} `,
59
- `(${fixedDigitDisplay((totalAfterBytes / totalBeforeBytes) * 100, 2)}%)`,
60
- ],
61
- ],
62
- [, , , , 'right']
63
- )}`);
64
+ console.log(`
65
+ ${styleText('bold', "Optimized WebAssembly Component Internal Core Modules:")}
66
+
67
+ ${tableContent}`);
64
68
  }
65
69
  }
66
70
 
@@ -104,7 +108,7 @@ export async function optimizeComponent(componentBytes, opts) {
104
108
  // log number of core Wasm modules to be run with wasm-opt
105
109
  let completed = 0;
106
110
  const spinnerText = () =>
107
- c`{cyan ${completed} / ${coreModules.length}} Running Binaryen on WebAssembly Component Internal Core Modules \n`;
111
+ `${styleText('cyan', `${completed} / ${coreModules.length}`)} Running Binaryen on WebAssembly Component Internal Core Modules \n`;
108
112
  if (showSpinner) {
109
113
  spinner = ora({
110
114
  color: 'cyan',
@@ -117,11 +121,11 @@ export async function optimizeComponent(componentBytes, opts) {
117
121
  const args = opts?.optArgs
118
122
  ? [...opts.optArgs]
119
123
  : [
120
- '-Oz',
121
- '--low-memory-unused',
122
- '--enable-bulk-memory',
123
- '--strip-debug',
124
- ];
124
+ '-Oz',
125
+ '--low-memory-unused',
126
+ '--enable-bulk-memory',
127
+ '--strip-debug',
128
+ ];
125
129
  if (opts?.asyncify) {
126
130
  args.push('--asyncify');
127
131
  }
@@ -136,7 +140,8 @@ export async function optimizeComponent(componentBytes, opts) {
136
140
  metadata.range[0],
137
141
  metadata.range[1]
138
142
  ),
139
- args
143
+ args,
144
+ opts
140
145
  );
141
146
 
142
147
  // compute the size change, including the change to
@@ -280,13 +285,13 @@ export async function optimizeComponent(componentBytes, opts) {
280
285
  * @param {Array<string>} args
281
286
  * @returns {Promise<Uint8Array>}
282
287
  */
283
- async function wasmOpt(source, args) {
284
- const wasmOptPath = fileURLToPath(
285
- import.meta.resolve('binaryen/bin/wasm-opt')
286
- );
288
+ async function wasmOpt(source, args, transpileOpts) {
289
+ const wasmOptBin =
290
+ transpileOpts?.wasmOptBin ??
291
+ fileURLToPath(import.meta.resolve('binaryen/bin/wasm-opt'));
287
292
 
288
293
  try {
289
- return await spawnIOTmp(wasmOptPath, source, [...args, '-o']);
294
+ return await spawnIOTmp(wasmOptBin, source, [...args, '-o']);
290
295
  } catch (e) {
291
296
  if (e.toString().includes('BasicBlock requested')) {
292
297
  return wasmOpt(source, args);
package/src/cmd/run.js CHANGED
@@ -1,11 +1,11 @@
1
- import { getTmpDir } from '../common.js';
2
- import { transpile } from './transpile.js';
3
1
  import { rm, mkdir, writeFile, symlink } from 'node:fs/promises';
4
2
  import { basename, resolve, extname } from 'node:path';
5
3
  import { spawn } from 'node:child_process';
6
4
  import process from 'node:process';
7
5
  import { fileURLToPath, pathToFileURL } from 'node:url';
8
- import c from 'chalk-template';
6
+
7
+ import { getTmpDir, styleText } from '../common.js';
8
+ import { transpile } from './transpile.js';
9
9
 
10
10
  const DEFAULT_SERVE_HOST = 'localhost';
11
11
 
@@ -54,8 +54,8 @@ export async function serve(componentPath, args, opts) {
54
54
  const server = new HTTPServer(mod.incomingHandler);
55
55
  let port = ${port};
56
56
  ${
57
- tryFindPort
58
- ? `
57
+ tryFindPort
58
+ ? `
59
59
  while (true) {
60
60
  try {
61
61
  server.listen(port, ${JSON.stringify(host)});
@@ -67,8 +67,8 @@ export async function serve(componentPath, args, opts) {
67
67
  port++;
68
68
  }
69
69
  `
70
- : `server.listen(port, ${JSON.stringify(host)})`
71
- }
70
+ : `server.listen(port, ${JSON.stringify(host)})`
71
+ }
72
72
  console.error(\`Server listening @ ${host}:${port}...\`);
73
73
  `
74
74
  );
@@ -117,7 +117,7 @@ async function runComponent(componentPath, args, opts, executor) {
117
117
  '../../../'
118
118
  );
119
119
  } catch (err) {
120
- let msg = c`{red.bold error} Failed to resolve {bold @bytecodealliance/preview2-shim}, ensure it is installed.`;
120
+ let msg = `${styleText(['red', 'bold'], 'error')} Failed to resolve ${styleText('bold', '@bytecodealliance/preview2-shim')}, ensure it is installed.`;
121
121
  msg += `\nERROR:\n${err.toString()}`;
122
122
  throw new Error(msg);
123
123
  }