diff --git a/codecs/hqx/Cargo.toml b/codecs/hqx/Cargo.toml index 742d0751..6109503b 100644 --- a/codecs/hqx/Cargo.toml +++ b/codecs/hqx/Cargo.toml @@ -3,6 +3,9 @@ name = "squooshhqx" version = "0.1.0" authors = ["Surma "] +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + [lib] crate-type = ["cdylib"] diff --git a/codecs/hqx/pkg/squooshhqx.d.ts b/codecs/hqx/pkg/squooshhqx.d.ts index c4c43fa5..eaebddad 100644 --- a/codecs/hqx/pkg/squooshhqx.d.ts +++ b/codecs/hqx/pkg/squooshhqx.d.ts @@ -1,48 +1,30 @@ /* tslint:disable */ /* eslint-disable */ /** - * @param {Uint32Array} input_image - * @param {number} input_width - * @param {number} input_height - * @param {number} factor - * @returns {Uint32Array} - */ -export function resize( - input_image: Uint32Array, - input_width: number, - input_height: number, - factor: number, -): Uint32Array; +* @param {Uint32Array} input_image +* @param {number} input_width +* @param {number} input_height +* @param {number} factor +* @returns {Uint32Array} +*/ +export function resize(input_image: Uint32Array, input_width: number, input_height: number, factor: number): Uint32Array; -export type InitInput = - | RequestInfo - | URL - | Response - | BufferSource - | WebAssembly.Module; +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { readonly memory: WebAssembly.Memory; - readonly resize: ( - a: number, - b: number, - c: number, - d: number, - e: number, - f: number, - ) => void; + readonly resize: (a: number, b: number, c: number, d: number, e: number, f: number) => void; readonly __wbindgen_malloc: (a: number) => number; readonly __wbindgen_free: (a: number, b: number) => void; } /** - * If `module_or_path` is {RequestInfo} or {URL}, makes a request and - * for everything else, calls `WebAssembly.instantiate` directly. - * - * @param {InitInput | Promise} module_or_path - * - * @returns {Promise} - */ -export default function init( - module_or_path?: InitInput | Promise, -): Promise; +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {InitInput | Promise} module_or_path +* +* @returns {Promise} +*/ +export default function init (module_or_path?: InitInput | Promise): Promise; + \ No newline at end of file diff --git a/codecs/hqx/pkg/squooshhqx.js b/codecs/hqx/pkg/squooshhqx.js index 8fd6c83f..2d2f761b 100644 --- a/codecs/hqx/pkg/squooshhqx.js +++ b/codecs/hqx/pkg/squooshhqx.js @@ -1,107 +1,103 @@ + let wasm; let cachegetUint32Memory0 = null; function getUint32Memory0() { - if ( - cachegetUint32Memory0 === null || - cachegetUint32Memory0.buffer !== wasm.memory.buffer - ) { - cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); - } - return cachegetUint32Memory0; + if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) { + cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); + } + return cachegetUint32Memory0; } let WASM_VECTOR_LEN = 0; function passArray32ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 4); - getUint32Memory0().set(arg, ptr / 4); - WASM_VECTOR_LEN = arg.length; - return ptr; + const ptr = malloc(arg.length * 4); + getUint32Memory0().set(arg, ptr / 4); + WASM_VECTOR_LEN = arg.length; + return ptr; } let cachegetInt32Memory0 = null; function getInt32Memory0() { - if ( - cachegetInt32Memory0 === null || - cachegetInt32Memory0.buffer !== wasm.memory.buffer - ) { - cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachegetInt32Memory0; + if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { + cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory0; } function getArrayU32FromWasm0(ptr, len) { - return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len); + return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len); } /** - * @param {Uint32Array} input_image - * @param {number} input_width - * @param {number} input_height - * @param {number} factor - * @returns {Uint32Array} - */ +* @param {Uint32Array} input_image +* @param {number} input_width +* @param {number} input_height +* @param {number} factor +* @returns {Uint32Array} +*/ export function resize(input_image, input_width, input_height, factor) { - var ptr0 = passArray32ToWasm0(input_image, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - wasm.resize(8, ptr0, len0, input_width, input_height, factor); - var r0 = getInt32Memory0()[8 / 4 + 0]; - var r1 = getInt32Memory0()[8 / 4 + 1]; - var v1 = getArrayU32FromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 4); - return v1; + var ptr0 = passArray32ToWasm0(input_image, wasm.__wbindgen_malloc); + var len0 = WASM_VECTOR_LEN; + wasm.resize(8, ptr0, len0, input_width, input_height, factor); + var r0 = getInt32Memory0()[8 / 4 + 0]; + var r1 = getInt32Memory0()[8 / 4 + 1]; + var v1 = getArrayU32FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v1; } async function load(module, imports) { - if (typeof Response === 'function' && module instanceof Response) { - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - return await WebAssembly.instantiateStreaming(module, imports); - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { - console.warn( - '`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n', - e, - ); - } else { - throw e; + if (typeof Response === 'function' && module instanceof Response) { + + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } } - } - } - const bytes = await module.arrayBuffer(); - return await WebAssembly.instantiate(bytes, imports); - } else { - const instance = await WebAssembly.instantiate(module, imports); + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); - if (instance instanceof WebAssembly.Instance) { - return { instance, module }; } else { - return instance; + + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } } - } } async function init(input) { - if (typeof input === 'undefined') { - input = import.meta.url.replace(/\.js$/, '_bg.wasm'); - } - const imports = {}; + if (typeof input === 'undefined') { + input = import.meta.url.replace(/\.js$/, '_bg.wasm'); + } + const imports = {}; - if ( - typeof input === 'string' || - (typeof Request === 'function' && input instanceof Request) || - (typeof URL === 'function' && input instanceof URL) - ) { - input = fetch(input); - } - const { instance, module } = await load(await input, imports); + if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { + input = fetch(input); + } - wasm = instance.exports; - init.__wbindgen_wasm_module = module; + const { instance, module } = await load(await input, imports); - return wasm; + wasm = instance.exports; + init.__wbindgen_wasm_module = module; + + return wasm; } export default init; + diff --git a/codecs/hqx/pkg/squooshhqx_bg.wasm b/codecs/hqx/pkg/squooshhqx_bg.wasm index ab28fd7e..182d46dc 100644 Binary files a/codecs/hqx/pkg/squooshhqx_bg.wasm and b/codecs/hqx/pkg/squooshhqx_bg.wasm differ diff --git a/codecs/oxipng/Cargo.toml b/codecs/oxipng/Cargo.toml index 6ab15d3a..da8a1b8a 100644 --- a/codecs/oxipng/Cargo.toml +++ b/codecs/oxipng/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [package.metadata.wasm-pack.profile.release] -wasm-opt = ["-O", "--no-validation"] +wasm-opt = false [lib] crate-type = ["cdylib"] diff --git a/codecs/oxipng/build.sh b/codecs/oxipng/build.sh index 200481fb..85031098 100755 --- a/codecs/oxipng/build.sh +++ b/codecs/oxipng/build.sh @@ -4,8 +4,12 @@ set -e rm -rf pkg,{-parallel} export CFLAGS="${CFLAGS} -DUNALIGNED_ACCESS_IS_FAST=1" -wasm-pack build -t web -RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel +wasm-pack build -t web -- --locked +RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel --locked # Workaround https://github.com/rustwasm/wasm-bindgen/issues/2133: sed -i "s|maybe_memory:|maybe_memory?:|" pkg-parallel/squoosh_oxipng.d.ts +# Workaround wasm-pack using a very old wasm-opt. +echo "Optimizing wasm binaries with \`wasm-opt\`..." +wasm-opt -O pkg/squoosh_oxipng_bg.wasm -o pkg/squoosh_oxipng_bg.wasm +wasm-opt -O pkg-parallel/squoosh_oxipng_bg.wasm -o pkg-parallel/squoosh_oxipng_bg.wasm rm pkg{,-parallel}/.gitignore diff --git a/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm index 49fe70f3..13d3c9e7 100644 Binary files a/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm and b/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm differ diff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm index 97715f1a..bdcb5a79 100644 Binary files a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm and b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm differ diff --git a/codecs/png/Cargo.toml b/codecs/png/Cargo.toml index 2114d14b..09ebdb8e 100644 --- a/codecs/png/Cargo.toml +++ b/codecs/png/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [package.metadata.wasm-pack.profile.release] -wasm-opt = ["-O", "--no-validation"] +wasm-opt = false [lib] crate-type = ["cdylib"] diff --git a/codecs/png/pkg/squoosh_png_bg.wasm b/codecs/png/pkg/squoosh_png_bg.wasm index 65f76eaa..56caa8f0 100644 Binary files a/codecs/png/pkg/squoosh_png_bg.wasm and b/codecs/png/pkg/squoosh_png_bg.wasm differ diff --git a/codecs/resize/Cargo.toml b/codecs/resize/Cargo.toml index 76d2b153..0c0d10ff 100644 --- a/codecs/resize/Cargo.toml +++ b/codecs/resize/Cargo.toml @@ -4,8 +4,10 @@ version = "0.1.0" authors = ["Surma "] publish = false +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + [lib] -#crate-type = ["cdylib", "rlib"] crate-type = ["cdylib"] [features] @@ -25,8 +27,6 @@ console_error_panic_hook = { version = "0.1.1", optional = true } # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size # compared to the default allocator's ~10K. It is slower than the default # allocator, however. -# -# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. wee_alloc = { version = "0.4.2", optional = true } [dev-dependencies] diff --git a/codecs/resize/pkg/squoosh_resize_bg.wasm b/codecs/resize/pkg/squoosh_resize_bg.wasm index 0a10cca3..f6755685 100644 Binary files a/codecs/resize/pkg/squoosh_resize_bg.wasm and b/codecs/resize/pkg/squoosh_resize_bg.wasm differ diff --git a/codecs/rust.Dockerfile b/codecs/rust.Dockerfile index e72f0803..d95e19d4 100644 --- a/codecs/rust.Dockerfile +++ b/codecs/rust.Dockerfile @@ -16,4 +16,11 @@ COPY --from=wasm-tools /opt/wasm-tools/wasm-pack /usr/local/cargo/bin/ ENV CPATH="/wasm32/include" WORKDIR /src -CMD ["sh", "-c", "rm -rf pkg && wasm-pack build --target web -- --verbose --locked && rm pkg/.gitignore"] +CMD ["sh", "-c", "\ + rm -rf pkg && \ + wasm-pack build --target web -- --locked && \ + echo 'Optimising binaries...' && \ + wasm-opt -O --enable-mutable-globals pkg/*.wasm -o pkg/*.wasm && \ + rm pkg/.gitignore && \ + echo Done \ +"]