photopea / UPNG.js

Fast and advanced PNG (APNG) decoder and encoder (lossy / lossless)
MIT License
2.1k stars 259 forks source link

ReferenceError: window is not defined [CDN] #91

Open Andrew-web-coder opened 5 months ago

Andrew-web-coder commented 5 months ago

I got this error while trying to use it in non-window environment from jsdelivr:

ReferenceError: window is not defined

Any tips on how to use it from jsdelivr?

The file I am trying to use:

https://cdn.jsdelivr.net/npm/upng-js@2.1.0/UPNG.min.js

But even this file:

https://cdn.jsdelivr.net/npm/upng-js@2.1.0/UPNG.js

contains else {window.UPNG = UPNG;} and I do not understand why, because source code does not contain that code :/

photopea commented 5 months ago

I have never used what you are using :( maybe write

var window = {};

before loading UPNG.js

Andrew-web-coder commented 5 months ago

I am trying to load it inside the worker using importScripts and it is not possible to define window object before loading the script.

If only there was a check in the code if window exists...

photopea commented 5 months ago

But are you sure that then, UPNG.js will work inside a worker? You could rewrite UPNG.js manually in a notepad.

Andrew-web-coder commented 5 months ago

Yes, it works if I download this code - https://greggman.github.io/doodles/js/UPNG.js. It is taken from https://webglfundamentals.org/webgl/lessons/webgl-qna-how-to-load-images-in-the-background-with-no-jank.html I believe it is the same code as in this repo (slightly different, maybe an older version) and, as you can see, it does not contain the unnecessary Make available for import code snippet that breaks usage in the worker.

photopea commented 5 months ago

I made UPNG.js to be used in a web browser. Maybe ask that guy to make you a version that works in your environment?

Andrew-web-coder commented 5 months ago

You see, if I download directly from https://github.com/photopea/UPNG.js/blob/master/UPNG.js it works fine, too. The problem is with that extra code that is added to the version that is hosted on CDN. I just wanted to use your code (and thank you so much for your work) from jsdeliver, because I already use other scripts from there.