Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jpeg and webp fail to import inside web worker #50

Open
higbee4 opened this issue Feb 10, 2024 · 2 comments
Open

jpeg and webp fail to import inside web worker #50

higbee4 opened this issue Feb 10, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@higbee4
Copy link

higbee4 commented Feb 10, 2024

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.

@higbee4 higbee4 added the bug Something isn't working label Feb 10, 2024
@jamsinclair
Copy link
Owner

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

@jamsinclair
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants