Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Update oxipng build system
Ingvar Stepanyan05416768d52c
diff --git a/codecs/build-rust-nightly.sh b/codecs/build-rust-nightly.shnew file mode 100644index 00000000..ab5ae1ea--- /dev/null+++ b/codecs/build-rust-nightly.sh@@ -0,0 +1,4 @@+set -e++docker build -t squoosh-rust-nightly --build-arg RUST_IMG=rustlang/rust:nightly - < ../rust.Dockerfile+docker run --rm -v $PWD:/src squoosh-rust-nightly "$@"diff --git a/codecs/oxipng/Dockerfile b/codecs/oxipng/Dockerfiledeleted file mode 100644index 089be3c7..00000000--- a/codecs/oxipng/Dockerfile+++ /dev/null@@ -1,12 +0,0 @@-FROM rustlang/rust:nightly-RUN rustup target add wasm32-unknown-unknown-RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh--RUN mkdir /opt/wabt && \- curl -L https://github.com/WebAssembly/wabt/releases/download/1.0.17/wabt-1.0.17-ubuntu.tar.gz | tar -xzf - -C /opt/wabt --strip 1--RUN mkdir /opt/wasi-sdk && \- curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-linux.tar.gz | tar -xzf - -C /opt/wasi-sdk --strip 1--ENV PATH="/opt/wabt/bin:/opt/wasi-sdk/bin:${PATH}"-WORKDIR /srcdiff --git a/codecs/oxipng/build.sh b/codecs/oxipng/build.shindex a46bae68..c730d6d6 100644--- a/codecs/oxipng/build.sh+++ b/codecs/oxipng/build.sh@@ -2,24 +2,8 @@set -e-echo "============================================="-echo "Compiling wasm"-echo "============================================="-(- export CC=/opt/wasi-sdk/bin/clang- rm -rf pkg,{-parallel}- 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- sed -i "s|input = import.meta.url.replace(/\\\.js$/, '_bg.wasm');||" pkg{,-parallel}/oxipng.js- rm pkg{,-parallel}/.gitignore-)-echo "============================================="-echo "Compiling wasm done"-echo "============================================="--echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"-echo "Did you update your docker image?"-echo "Run \`docker pull ubuntu\`"-echo "Run \`docker pull rust\`"-echo "Run \`docker build -t squoosh-hqx .\`"-echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+rm -rf pkg,{-parallel}+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+sed -i "s|input = import.meta.url.replace(/\\\.js$/, '_bg.wasm');||" pkg{,-parallel}/squoosh_oxipng.js+rm pkg{,-parallel}/.gitignorediff --git a/codecs/oxipng/package.json b/codecs/oxipng/package.jsonindex 552448fd..8bdd4abd 100644--- a/codecs/oxipng/package.json+++ b/codecs/oxipng/package.json@@ -1,6 +1,6 @@{"name": "oxipng","scripts": {- "build": "../build-rust.sh"+ "build": "../build-rust-nightly.sh ./build.sh"}}diff --git a/codecs/oxipng/pkg-parallel/oxipng.d.ts b/codecs/oxipng/pkg-parallel/oxipng.d.tsdeleted file mode 100644index 06012665..00000000--- a/codecs/oxipng/pkg-parallel/oxipng.d.ts+++ /dev/null@@ -1,46 +0,0 @@-/* tslint:disable */-/* eslint-disable */-/**-* @param {Uint8Array} data-* @param {number} level-* @returns {Uint8Array}-*/-export function optimise(data: Uint8Array, level: number): Uint8Array;-/**-* @param {number} num-* @returns {any}-*/-export function worker_initializer(num: number): any;-/**-*/-export function start_main_thread(): void;-/**-*/-export function start_worker_thread(): void;--export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;--export interface InitOutput {- readonly optimise: (a: number, b: number, c: number, d: number) => void;- readonly malloc: (a: number) => number;- readonly free: (a: number) => void;- readonly worker_initializer: (a: number) => number;- readonly start_main_thread: () => void;- readonly start_worker_thread: () => void;- readonly __wbindgen_export_0: WebAssembly.Memory;- readonly __wbindgen_malloc: (a: number) => number;- readonly __wbindgen_free: (a: number, b: number) => void;- readonly __wbindgen_start: () => 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-* @param {WebAssembly.Memory} maybe_memory-*-* @returns {Promise<InitOutput>}-*/-export default function init (module_or_path?: InitInput | Promise<InitInput>, maybe_memory: WebAssembly.Memory): Promise<InitOutput>;-\ No newline at end of filediff --git a/codecs/oxipng/pkg-parallel/oxipng.js b/codecs/oxipng/pkg-parallel/oxipng.jsdeleted file mode 100644index 3d4a2566..00000000--- a/codecs/oxipng/pkg-parallel/oxipng.js+++ /dev/null@@ -1,174 +0,0 @@--let wasm;-let memory;--const heap = new Array(32).fill(undefined);--heap.push(undefined, null, true, false);--let heap_next = heap.length;--function addHeapObject(obj) {- if (heap_next === heap.length) heap.push(heap.length + 1);- const idx = heap_next;- heap_next = heap[idx];-- heap[idx] = obj;- return idx;-}--let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });--cachedTextDecoder.decode();--let cachegetUint8Memory0 = null;-function getUint8Memory0() {- if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.__wbindgen_export_0.buffer) {- cachegetUint8Memory0 = new Uint8Array(wasm.__wbindgen_export_0.buffer);- }- return cachegetUint8Memory0;-}--function getStringFromWasm0(ptr, len) {- return cachedTextDecoder.decode(getUint8Memory0().slice(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.__wbindgen_export_0.buffer) {- cachegetInt32Memory0 = new Int32Array(wasm.__wbindgen_export_0.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) {- var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);- var len0 = WASM_VECTOR_LEN;- wasm.optimise(8, ptr0, len0, level);- var r0 = getInt32Memory0()[8 / 4 + 0];- var r1 = getInt32Memory0()[8 / 4 + 1];- var v1 = getArrayU8FromWasm0(r0, r1).slice();- wasm.__wbindgen_free(r0, r1 * 1);- return v1;-}--function getObject(idx) { return heap[idx]; }--function dropObject(idx) {- if (idx < 36) return;- heap[idx] = heap_next;- heap_next = idx;-}--function takeObject(idx) {- const ret = getObject(idx);- dropObject(idx);- return ret;-}-/**-* @param {number} num-* @returns {any}-*/-export function worker_initializer(num) {- var ret = wasm.worker_initializer(num);- return takeObject(ret);-}--/**-*/-export function start_main_thread() {- wasm.start_main_thread();-}--/**-*/-export function start_worker_thread() {- wasm.start_worker_thread();-}--async function load(module, imports, maybe_memory) {- if (typeof Response === 'function' && module instanceof Response) {- memory = imports.wbg.memory = new WebAssembly.Memory({initial:17,maximum:16384,shared:true});- 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 {- memory = imports.wbg.memory = maybe_memory;- const instance = await WebAssembly.instantiate(module, imports);-- if (instance instanceof WebAssembly.Instance) {- return { instance, module };-- } else {- return instance;- }- }-}--async function init(input, maybe_memory) {- if (typeof input === 'undefined') {-- }- const imports = {};- imports.wbg = {};- imports.wbg.__wbindgen_module = function() {- var ret = init.__wbindgen_wasm_module;- return addHeapObject(ret);- };- imports.wbg.__wbindgen_memory = function() {- var ret = wasm.__wbindgen_export_0;- return addHeapObject(ret);- };- imports.wbg.__wbg_of_9335425aa94288f5 = function(arg0, arg1) {- var ret = Array.of(takeObject(arg0), takeObject(arg1));- return addHeapObject(ret);- };- 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, maybe_memory);-- wasm = instance.exports;- init.__wbindgen_wasm_module = module;- wasm.__wbindgen_start();- return wasm;-}--export default init;-diff --git a/codecs/oxipng/pkg-parallel/oxipng_bg.d.ts b/codecs/oxipng/pkg-parallel/oxipng_bg.d.tsdeleted file mode 100644index e4189249..00000000--- a/codecs/oxipng/pkg-parallel/oxipng_bg.d.ts+++ /dev/null@@ -1,12 +0,0 @@-/* tslint:disable */-/* eslint-disable */-export function optimise(a: number, b: number, c: number, d: number): void;-export function malloc(a: number): number;-export function free(a: number): void;-export function worker_initializer(a: number): number;-export function start_main_thread(): void;-export function start_worker_thread(): void;-export const __wbindgen_export_0: WebAssembly.Memory;-export function __wbindgen_malloc(a: number): number;-export function __wbindgen_free(a: number, b: number): void;-export function __wbindgen_start(): void;diff --git a/codecs/oxipng/pkg-parallel/oxipng_bg.wasm b/codecs/oxipng/pkg-parallel/oxipng_bg.wasmdeleted file mode 100644index 20831161..00000000Binary files a/codecs/oxipng/pkg-parallel/oxipng_bg.wasm and /dev/null differdiff --git a/codecs/oxipng/pkg-parallel/squoosh_oxipng.js b/codecs/oxipng/pkg-parallel/squoosh_oxipng.jsindex 1111760d..0988144b 100644--- a/codecs/oxipng/pkg-parallel/squoosh_oxipng.js+++ b/codecs/oxipng/pkg-parallel/squoosh_oxipng.js@@ -144,7 +144,7 @@ async function load(module, imports, maybe_memory) {async function init(input, maybe_memory) {if (typeof input === 'undefined') {- input = import.meta.url.replace(/\.js$/, '_bg.wasm');+}const imports = {};imports.wbg = {};diff --git a/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasmindex fc151b24..f93ce3c4 100644Binary files a/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm and b/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm differdiff --git a/codecs/oxipng/pkg/oxipng_bg.wasm b/codecs/oxipng/pkg/oxipng_bg.wasmdeleted file mode 100644index f893fb89..00000000Binary files a/codecs/oxipng/pkg/oxipng_bg.wasm and /dev/null differdiff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasmindex 029c326e..70dc23b8 100644Binary files a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm and b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm differdiff --git a/codecs/oxipng/spawn.ts b/codecs/oxipng/spawn.tsindex 3abecb97..f6c77093 100644--- a/codecs/oxipng/spawn.ts+++ b/codecs/oxipng/spawn.ts@@ -3,7 +3,8 @@ import initOxiPNG, {start_main_thread,optimise,} from './pkg-parallel';-import wasmUrl from './pkg-parallel/oxipng_bg.wasm';+// @ts-ignore+import wasmUrl from './pkg-parallel/squoosh_oxipng_bg.wasm';import { WorkerInit } from './worker';function initWorker(worker: Worker, workerInit: WorkerInit) {diff --git a/codecs/rust.Dockerfile b/codecs/rust.Dockerfileindex f21b0468..902c8b0e 100644--- a/codecs/rust.Dockerfile+++ b/codecs/rust.Dockerfile@@ -1,9 +1,13 @@+ARG RUST_IMG=rust:1.44-stretch+FROM emscripten/emsdk:1.39.19 AS wasm-toolsWORKDIR /opt/wasm-toolsRUN wget -qO- https://github.com/rustwasm/wasm-pack/releases/download/v0.9.1/wasm-pack-v0.9.1-x86_64-unknown-linux-musl.tar.gz | tar -xzf - --strip 1-FROM rust:1.44-stretch AS rust+FROM $RUST_IMG AS rust+ARG RUST_IMGRUN rustup target add wasm32-unknown-unknown+RUN if [ "$RUST_IMG" = "rustlang/rust:nightly" ] ; then rustup component add rust-src ; fiCOPY --from=wasm-tools /emsdk/upstream/bin/wasm-opt /emsdk/upstream/bin/clang /usr/local/bin/COPY --from=wasm-tools /emsdk/upstream/lib/ /usr/local/lib/COPY --from=wasm-tools /emsdk/upstream/emscripten/system/include/libc/ /wasm32/include/diff --git a/webpack.config.js b/webpack.config.jsindex 981ea7f9..4e3e902d 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: /(?<!oxipng)_bg\.wasm$/,+ exclude: /(?<!\bsquoosh_oxipng)_bg\.wasm$/,// This is needed to make webpack NOT process wasm files.// See https://github.com/webpack/webpack/issues/6725type: 'javascript/auto',@@ -160,7 +160,7 @@ module.exports = async function(_, env) {},{// Wasm modules generated by Rust + wasm-pack work great with Webpack.- test: /(?<!oxipng)_bg\.wasm$/,+ test: /(?<!\bsquoosh_oxipng)_bg\.wasm$/,type: 'webassembly/experimental',},{