Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Add feature detection to OxiPNG

Ingvar Stepanyanbae243ccdbf8
diff --git a/codecs/oxipng/build.sh b/codecs/oxipng/build.sh
index c730d6d6..d9dff0b9 100644
--- a/codecs/oxipng/build.sh
+++ b/codecs/oxipng/build.sh
@@ -3,7 +3,7 @@
set -e
rm -rf pkg,{-parallel}
-wasm-pack build -t web
+wasm-pack build
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel
sed -i "s|input = import.meta.url.replace(/\\\.js$/, '_bg.wasm');||" pkg{,-parallel}/squoosh_oxipng.js
rm pkg{,-parallel}/.gitignore
diff --git a/codecs/oxipng/index.ts b/codecs/oxipng/index.ts
new file mode 100644
index 00000000..285d916e
--- /dev/null
+++ b/codecs/oxipng/index.ts
@@ -0,0 +1,10 @@
+import { threads } from 'wasm-feature-detect';
+
+async function init() {
+ if (await threads()) {
+ return (await import('./spawn')).default;
+ }
+ return import('./pkg');
+}
+
+export default init();
diff --git a/codecs/oxipng/pkg/squoosh_oxipng.d.ts b/codecs/oxipng/pkg/squoosh_oxipng.d.ts
index 196593fb..1c95bae4 100644
--- a/codecs/oxipng/pkg/squoosh_oxipng.d.ts
+++ b/codecs/oxipng/pkg/squoosh_oxipng.d.ts
@@ -6,25 +6,3 @@
* @returns {Uint8Array}
*/
export function optimise(data: Uint8Array, level: number): Uint8Array;
-
-export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
-
-export interface InitOutput {
- readonly memory: WebAssembly.Memory;
- readonly optimise: (a: number, b: number, c: number, d: number) => void;
- readonly malloc: (a: number) => number;
- readonly free: (a: 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<InitInput>} module_or_path
-*
-* @returns {Promise<InitOutput>}
-*/
-export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
-
\ No newline at end of file
diff --git a/codecs/oxipng/pkg/squoosh_oxipng.js b/codecs/oxipng/pkg/squoosh_oxipng.js
index a894a364..24abe864 100644
--- a/codecs/oxipng/pkg/squoosh_oxipng.js
+++ b/codecs/oxipng/pkg/squoosh_oxipng.js
@@ -1,118 +1,2 @@
-
-let wasm;
-
-let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
-
-cachedTextDecoder.decode();
-
-let cachegetUint8Memory0 = null;
-function getUint8Memory0() {
- if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
- cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
- }
- return cachegetUint8Memory0;
-}
-
-function getStringFromWasm0(ptr, len) {
- return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
-}
-
-let WASM_VECTOR_LEN = 0;
-
-function passArray8ToWasm0(arg, malloc) {
- const ptr = malloc(arg.length * 1);
- getUint8Memory0().set(arg, ptr / 1);
- 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;
-}
-
-function getArrayU8FromWasm0(ptr, len) {
- return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
-}
-/**
-* @param {Uint8Array} data
-* @param {number} level
-* @returns {Uint8Array}
-*/
-export function optimise(data, level) {
- try {
- const retptr = wasm.__wbindgen_export_0.value - 16;
- wasm.__wbindgen_export_0.value = retptr;
- var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
- var len0 = WASM_VECTOR_LEN;
- wasm.optimise(retptr, ptr0, len0, level);
- var r0 = getInt32Memory0()[retptr / 4 + 0];
- var r1 = getInt32Memory0()[retptr / 4 + 1];
- var v1 = getArrayU8FromWasm0(r0, r1).slice();
- wasm.__wbindgen_free(r0, r1 * 1);
- return v1;
- } finally {
- wasm.__wbindgen_export_0.value += 16;
- }
-}
-
-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;
- }
- }
- }
-
- const bytes = await module.arrayBuffer();
- return await WebAssembly.instantiate(bytes, imports);
-
- } else {
-
- 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') {
-
- }
- const imports = {};
- imports.wbg = {};
- imports.wbg.__wbindgen_throw = function(arg0, arg1) {
- throw new Error(getStringFromWasm0(arg0, arg1));
- };
-
- 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);
-
- wasm = instance.exports;
- init.__wbindgen_wasm_module = module;
-
- return wasm;
-}
-
-export default init;
-
+import * as wasm from "./squoosh_oxipng_bg.wasm";
+export * from "./squoosh_oxipng_bg.js";
\ No newline at end of file
diff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.js b/codecs/oxipng/pkg/squoosh_oxipng_bg.js
new file mode 100644
index 00000000..c9da8937
--- /dev/null
+++ b/codecs/oxipng/pkg/squoosh_oxipng_bg.js
@@ -0,0 +1,66 @@
+import * as wasm from './squoosh_oxipng_bg.wasm';
+
+const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
+
+let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
+
+cachedTextDecoder.decode();
+
+let cachegetUint8Memory0 = null;
+function getUint8Memory0() {
+ if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
+ cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
+ }
+ return cachegetUint8Memory0;
+}
+
+function getStringFromWasm0(ptr, len) {
+ return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
+}
+
+let WASM_VECTOR_LEN = 0;
+
+function passArray8ToWasm0(arg, malloc) {
+ const ptr = malloc(arg.length * 1);
+ getUint8Memory0().set(arg, ptr / 1);
+ 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;
+}
+
+function getArrayU8FromWasm0(ptr, len) {
+ return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
+}
+/**
+* @param {Uint8Array} data
+* @param {number} level
+* @returns {Uint8Array}
+*/
+export function optimise(data, level) {
+ try {
+ const retptr = wasm.__wbindgen_export_0.value - 16;
+ wasm.__wbindgen_export_0.value = retptr;
+ var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
+ var len0 = WASM_VECTOR_LEN;
+ wasm.optimise(retptr, ptr0, len0, level);
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
+ wasm.__wbindgen_free(r0, r1 * 1);
+ return v1;
+ } finally {
+ wasm.__wbindgen_export_0.value += 16;
+ }
+}
+
+export const __wbindgen_throw = function(arg0, arg1) {
+ throw new Error(getStringFromWasm0(arg0, arg1));
+};
+
diff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm
index 34f4f5a2..aef8cca8 100644
Binary files a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm and b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm differ
diff --git a/src/codecs/oxipng/encoder.ts b/src/codecs/oxipng/encoder.ts
index 4af107b9..ac54dd89 100644
--- a/src/codecs/oxipng/encoder.ts
+++ b/src/codecs/oxipng/encoder.ts
@@ -1,5 +1,5 @@
// @ts-ignore
-import optimiser from '../../../codecs/oxipng/spawn';
+import optimiser from '../../../codecs/oxipng';
import { EncodeOptions } from './encoder-meta';
export async function compress(data: ArrayBuffer, options: EncodeOptions): Promise<ArrayBuffer> {
diff --git a/webpack.config.js b/webpack.config.js
index 4e3e902d..03a837fd 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -149,7 +149,7 @@ module.exports = async function(_, env) {
{
// Emscripten modules don't work with Webpack's Wasm loader.
test: /\.wasm$/,
- exclude: /(?<!\bsquoosh_oxipng)_bg\.wasm$/,
+ exclude: /(?<!\bpkg-parallel[\\/]squoosh_oxipng)_bg\.wasm$/,
// This is needed to make webpack NOT process wasm files.
// See https://github.com/webpack/webpack/issues/6725
type: 'javascript/auto',
@@ -160,7 +160,7 @@ module.exports = async function(_, env) {
},
{
// Wasm modules generated by Rust + wasm-pack work great with Webpack.
- test: /(?<!\bsquoosh_oxipng)_bg\.wasm$/,
+ test: /(?<!\bpkg-parallel[\\/]squoosh_oxipng)_bg\.wasm$/,
type: 'webassembly/experimental',
},
{