Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

StenciljsError: ReferenceError: File is not defined #92

Closed Scan0815 closed 3 years ago

Scan0815 commented 3 years ago

hi, i have a problem with my test suite and the browser-image-compression lib.

ReferenceError: File is not defined, also not working in ssr, get the same error.

i read that you will fix it in 1.0.8, but now i on 1.0.14 and is not working.

You write from a trycatch workaound, but the error will get direct on import call. import imageCompression from 'browser-image-compression';

error

Donaldcwl commented 3 years ago

@Scan0815 Do your test environment has the window object defined? The library detects browser environments by checking the existence of the window object through typeof window !== 'undefined'.

Scan0815 commented 3 years ago

@Donaldcwl hi, thanks for answer. Yes that´s right, StencilJS has on the test env and on SSR a own window object only with some functions and without File. i remove for testing your lib and than everything work fin.

I think React prerender, use also a internal window object, hydrating components is not possible without.

Maybe you can check if File exists in window.

Donaldcwl commented 3 years ago

@Scan0815 Thanks for your suggestion, extra checking on whether File and FileReader exist in the Window object will be added in the next release.

Scan0815 commented 3 years ago

@Donaldcwl i have debug a little deeper. I found that not the isBrowser check is the problem. CustomFile=isBrowser&(moduleMapper&&moduleMapper.getOriginalSymbol(window,"File")||window.File),CustomFileReader=isBrowser&&(moduleMapper&&moduleMapper.getOriginalSymbol(window,"FileReader")||window.FileReader) i change the two "or" from File | ReadFile to window.File | window.FileReader and now it work.

i hope this help.

Donaldcwl commented 3 years ago

fixed in v1.0.15