jamsinclair / jSquash

Browser & Web Worker focussed wasm bundles derived from the Squoosh App.
Apache License 2.0
222 stars 14 forks source link

jpeg and webp fail to import inside web worker #50

Open higbee4 opened 7 months ago

higbee4 commented 7 months ago

Describe the bug When I try to import jpeg or webp inside a web worker, I get this error:

⨯ ./node_modules/@jsquash/jpeg/codec/enc/mozjpeg_enc.js
Module parse failed: Identifier 'Module' has already been declared (5:12)
|     return function() {
|         let Module = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
>         var Module = typeof Module != "undefined" ? Module : {};
|         var readyPromiseResolve, readyPromiseReject;
|         Module["ready"] = new Promise(function(resolve, reject) {

avif and png both work though.

Solution I noticed that avif and png use a slightly different syntax in the enc/dec files and they don't have this problem. I can work on a PR if preferred, or you can just change this inside jpeg/codec/enc/mozjpeg_enc.js, jpeg/codec/dec/mozjpeg_dec.js, webp/codec/enc/webp_enc.js, and webp/codec/dec/webp_dec.js to match the syntax used in avif and png that should fix it. I tried changing them locally and it fixed the problem for me.

Change

var Module = (() => {
  var _scriptDir = import.meta.url;

  return (
function(Module = {})  {

to

var Module = (function() {
  var _scriptDir = import.meta.url;

  return (
function(Module) {
  Module = Module || {};

Thanks.

jamsinclair commented 6 months ago

Thanks, I'll try take a look at these when I can 👀

jamsinclair commented 5 months ago

Thanks for your patience @higbee4. I've taken a look at it now and cannot reproduce it.

The issue is happening as it appears you have some kind of preprocessor/module bundler that is modifying the package code. The issue is that it's modified the code and tried to define Module with let and then again with var hence the error.

If you are using Vite or Nuxt we recommend disabling transpilation of jsquash modules (See the note in the README)

If you can help with following it help us solve the issue quicker:

  1. Which version of the packages are you using?
  2. Which module bundler are you using? (Webpack, Vite etc.)
  3. Are you able to share a reproduction with codesandbox or stackblitz?