• Stars
    star
    185
  • Rank 207,024 (Top 5 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Browser & Web Worker focussed wasm bundles derived from the Squoosh App.

jSquash πŸ₯

Collection of WebAssembly image codecs that support the browser and are derived from the Squoosh App

The aim of this library is to provide an easy experience to encode, decode and modify images with the tools you know and love from Squoosh in the Browser and Web Worker environments.

jSquash name is inspired by jQuery and Squoosh. It symbolizes the browser support focus of these packages.

⚠️ There is limited support for Node.js environments. The experimental Node.js support is provided for convenience and is not the primary focus of this project. For much better Node based alternatives please check out the Squoosh Node.js library and Sharp.

Differences with Squoosh

  • The codecs and tools are built for both Web and Web Worker environments
  • No dynamic code execution, the packages can be run in strict environments that do not allow code evaluation. Like Cloudflare Workers.
  • Does not rely on TextEncoder/TextDecoder API (could reduce performance) but allows it to be run in simpler V8 runtimes that only support UTF-8 (Cloudflare Workers, Vercel Edge Functions etc.)

Packages

⚠️ All packages are ESM modules. You may need to manually transpile the packages if your build environment still relies on Commonjs formats.

Usage in the Browser

You can use the packages directly from the Unpkg CDN and can be the easiest way to get started.

import { decode } from "https://unpkg.com/@jsquash/jpeg?module";
import { encode } from "https://unpkg.com/@jsquash/webp?module";

const imageResponse = await fetch("https://picsum.photos/200/300.jpg");
const imageData = await decode(await imageResponse.arrayBuffer());
const webpImageBuffer = await encode(imageData);

To target a specific version, you can use the @version syntax.

import { encode } from "https://unpkg.com/@jsquash/[email protected]?module";

Checkout the with CDN example for a working demo.

Usage in Node.js

Using jSquash modules with Node.js requires some additional steps so that the WASM binaries can be included. The support is limited and the WASM modules are not optimized for speed with Node.js.

Check out the with Node.js example for a working demo.

Usage in Cloudflare Workers

Using jSquash modules with Cloudflare Workers requires some additional steps so that the WASM binaries get included.

Depending on which format you are using check the examples below:

Other Examples

Known Issues

Issues with Vite and Vue build environments

This may present itself as any of the following errors:

  • TypeError: Failed to construct 'URL': Invalid URL
  • RuntimeError: Aborted(both async and sync fetching of the wasm failed). Build with -sASSERTIONS for more info.
  • Other console errors could also be related to this issue

As a workaround, update your vite.config.js file with the optimizeDeps property. Put affected module names in the exclude array. Vites dependency optimizer seems to be causing issues with the WASM modules.

import { defineConfig } from 'vite'

export default defineConfig({
  optimizeDeps: {
    exclude: ["@jsquash/png"]
  }
})

Issues with Nuxt build environments

This may present itself as a Cannot find module error. This is likely because Nuxt is anticipating third party modules to be in the Commonjs format.

Setting the following Nuxt config with the jSquash packages that your app uses seems to resolve it.

export default defineNuxtConfig({
  build: {
    transpile: ["@jsquash/png"],
  },
  vite: {
    optimizeDeps: {
      exclude: ["@jsquash/png"],
    },
  },
});

Issues with Nuxt/Vite and nested Web Workers

There is a known Vite bug breaking production code compilation when using a worker that references another worker, see issue #19 for more information.

Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:

In the meantime, you can install special builds that don't use workers to work around this issue:

More Repositories

1

ballast

Keep your audio balance from drifting! OSX Status Bar App
Swift
466
star
2

open-anki-jlpt-decks

Open source and updatable JLPT Vocabulary Anki Decks
Python
106
star
3

slackmojize

Easily resize images in your browser for Slack Emoji Upload!
JavaScript
23
star
4

wifiqr

Minimal JS Wi-Fi QR Code Generator
HTML
9
star
5

jumpnbump.js

A JavaScript port of the classic Jump n' Bump game. Play in your browser! https://jumpnbump.net
TypeScript
8
star
6

gif-spin

Generate spinning gif animations from a static image in your browser!
JavaScript
7
star
7

gifski-wasm

JavaScript
6
star
8

opifancontrol

A simple PWM fan controller for Orange Pi boards
Shell
6
star
9

budou-node

Node.js port of Budou, an automatic organizer tool for beautiful line breaking in CJK (Chinese, Japanese, and Korean).
JavaScript
4
star
10

ow-camera-android

Fork of Mark Harman's Open Camera app which adds Pebble Watch Timer Control
Java
3
star
11

webtrack

A collection of media players for Tracker files in the browser. For sounds and music created for ProTracker, Fasttracker II etc.
JavaScript
2
star
12

ascender

[WIP] Lightweight utilities to support drag'n'drop upload
TypeScript
2
star
13

wanikani-new-tab-page

Repository for the Wanikani New Tab Page Chrome & Firefox Extension
JavaScript
2
star
14

adventofcode-2020

Ho ho ho, I'm bad at code.
JavaScript
2
star
15

ow-camera-remote

Simple app to utilise pebble watch as a camera timer remote
C
2
star
16

summeroftech-js-resources

Links and resources to help discover and master JavaScript and Node.js
2
star
17

react-player-circle-controls

Circular UI controls for playing media
JavaScript
2
star
18

jamsinclair.github.io

Personal Website
CSS
1
star
19

me

Personal Website of Jamie Sinclair [Old]
HTML
1
star
20

resume

CSS
1
star
21

babel-plugin-dynamic-import-node-and-webpack

Transpile `import()` to support both deferred `require()` for node and `require.ensure` for Webpack
JavaScript
1
star
22

web-ext-reload

Reload your chrome extension during development
JavaScript
1
star
23

oidua

JavaScript
1
star
24

hanasu

A simple web client to send/receive SMS via Twilio
JavaScript
1
star
25

blog

Personal blog on dev things and whatevs.
CSS
1
star
26

shippai

GitHub Pull Request Commit Status Visualiser
JavaScript
1
star
27

gif-guardian-action

The silent but animated protector of Pull Requests. Automatically label Pull Requests without gifs.
JavaScript
1
star
28

isomorphic-image-size

Get image size (width & height) without rendering in the browser and node! πŸ“ΈπŸ“
JavaScript
1
star
29

github-lite

⚠️ Work In Progress - A light github api wrapper, optimised for newer browsers
JavaScript
1
star