Skip to content

Commit

Permalink
download and run wasm-opt
Browse files Browse the repository at this point in the history
  • Loading branch information
lopert committed Dec 9, 2024
1 parent 55b65ec commit 81920cf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/app/src/cli/services/build/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {runThemeCheck} from './theme-check.js'
import {AppInterface} from '../../models/app/app.js'
import {bundleExtension, bundleFlowTemplateExtension} from '../extensions/bundle.js'
import {buildJSFunction} from '../function/build.js'
import {buildJSFunction, runWasmOpt} from '../function/build.js'
import {ExtensionInstance} from '../../models/extensions/extension-instance.js'
import {FunctionConfigType} from '../../models/extensions/specifications/function.js'
import {exec} from '@shopify/cli-kit/node/system'
Expand Down Expand Up @@ -175,6 +175,11 @@ export async function buildFunctionExtension(
} else {
await buildOtherFunction(extension, options)
}

if (fileExistsSync(extension.outputPath)) {
await runWasmOpt(extension.outputPath)
}

if (fileExistsSync(extension.outputPath) && bundlePath !== extension.outputPath) {
const base64Contents = await readFile(extension.outputPath, {encoding: 'base64'})
await touchFile(bundlePath)
Expand Down
31 changes: 31 additions & 0 deletions packages/app/src/cli/services/function/binaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const JAVY_VERSION = 'v4.0.0'
// match the plugin version used in the function-runner version specified above.
const JAVY_PLUGIN_VERSION = 'v3.2.0'

const BINARYEN_VERSION = '120.0.0'

interface DownloadableBinary {
path: string
name: string
Expand Down Expand Up @@ -107,9 +109,30 @@ class JavyPlugin implements DownloadableBinary {
}
}

class WasmOptExecutable implements DownloadableBinary {
readonly name: string
readonly version: string
readonly path: string

constructor(name: string, version: string) {
this.name = name
this.version = version
this.path = joinPath(dirname(fileURLToPath(import.meta.url)), '..', 'bin', name)
}

downloadUrl(_processPlatform: string, _processArch: string) {
return `https://cdn.jsdelivr.net/npm/binaryen@${this.version}/bin/wasm-opt`
}

async processResponse(responseStream: PipelineSource<unknown>, outputStream: fs.WriteStream): Promise<void> {
await stream.pipeline(responseStream, outputStream)
}
}

let _javy: DownloadableBinary
let _javyPlugin: DownloadableBinary
let _functionRunner: DownloadableBinary
let _wasmOpt: DownloadableBinary

export function javyBinary() {
if (!_javy) {
Expand All @@ -132,6 +155,14 @@ export function functionRunnerBinary() {
return _functionRunner
}

export function wasmOptBinary() {
if (!_wasmOpt) {
_wasmOpt = new WasmOptExecutable('wasm-opt.cjs', BINARYEN_VERSION)
}

return _wasmOpt
}

export async function downloadBinary(bin: DownloadableBinary) {
const isDownloaded = await fileExists(bin.path)
if (isDownloaded) {
Expand Down
23 changes: 21 additions & 2 deletions packages/app/src/cli/services/function/build.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {downloadBinary, javyBinary, javyPluginBinary} from './binaries.js'
import {downloadBinary, javyBinary, javyPluginBinary, wasmOptBinary} from './binaries.js'
import {ExtensionInstance} from '../../models/extensions/extension-instance.js'
import {FunctionConfigType} from '../../models/extensions/specifications/function.js'
import {AppInterface} from '../../models/app/app.js'
import {EsbuildEnvVarRegex} from '../../constants.js'
import {hyphenate, camelize} from '@shopify/cli-kit/common/string'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {exec} from '@shopify/cli-kit/node/system'
import {joinPath} from '@shopify/cli-kit/node/path'
import {dirname, joinPath} from '@shopify/cli-kit/node/path'
import {build as esBuild, BuildResult} from 'esbuild'
import {findPathUp, inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {AbortSignal} from '@shopify/cli-kit/node/abort'
import {renderTasks} from '@shopify/cli-kit/node/ui'
import {pickBy} from '@shopify/cli-kit/common/object'
import {runWithTimer} from '@shopify/cli-kit/node/metadata'
import {Writable} from 'stream'
import {execSync} from 'child_process'

Check failure on line 18 in packages/app/src/cli/services/function/build.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/function/build.ts#L18

[no-restricted-imports] 'child_process' import is restricted from being used. Please use: import { exec } from '@shopify/cli-kit/node/system'

interface JSFunctionBuildOptions {
stdout: Writable
Expand Down Expand Up @@ -162,6 +163,24 @@ function getESBuildOptions(
return esbuildOptions
}

export async function downloadWasmOpt() {
const wasmOpt = wasmOptBinary()
await Promise.all([downloadBinary(wasmOpt)])
}

export async function runWasmOpt(modulePath: string) {
await downloadWasmOpt()

const wasmOptDir = dirname(wasmOptBinary().path)
const command = `node wasm-opt.cjs ${modulePath}`
const args = ['-Oz', '--enable-bulk-memory', '-o', modulePath].join(' ')

console.log('Attempting to run wasm-opt with the following command:')

Check failure on line 178 in packages/app/src/cli/services/function/build.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/function/build.ts#L178

[no-console] Unexpected console statement.
console.log(`${command} ${args}`)

Check failure on line 179 in packages/app/src/cli/services/function/build.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/function/build.ts#L179

[no-console] Unexpected console statement.

execSync(`${command} ${args}`, {cwd: wasmOptDir})
}

export async function runJavy(
fun: ExtensionInstance<FunctionConfigType>,
options: JSFunctionBuildOptions,
Expand Down

0 comments on commit 81920cf

Please sign in to comment.