diff --git a/packages/png/codec/package.json b/packages/png/codec/package.json index 0baa377..5b8878e 100644 --- a/packages/png/codec/package.json +++ b/packages/png/codec/package.json @@ -9,7 +9,10 @@ "squoosh_png.js", "squoosh_png.d.ts" ], + "scripts": { + "patch-pre-script": "cat pre.js >> squoosh_png.js" + }, "module": "squoosh_png.js", "types": "squoosh_png.d.ts", "sideEffects": false -} \ No newline at end of file +} diff --git a/packages/png/codec/pre.js b/packages/png/codec/pre.js new file mode 100644 index 0000000..4f0530c --- /dev/null +++ b/packages/png/codec/pre.js @@ -0,0 +1,15 @@ +(function () { + const isRunningInCloudFlareWorkers = caches.default !== undefined; + if (isRunningInCloudFlareWorkers) { + if (!globalThis.ImageData) { + // Simple Polyfill for ImageData Object + globalThis.ImageData = class ImageData { + constructor(data, width, height) { + this.data = data; + this.width = width; + this.height = height; + } + }; + } + } +})(); diff --git a/packages/png/codec/squoosh_png.js b/packages/png/codec/squoosh_png.js index 881ee83..0b0975b 100644 --- a/packages/png/codec/squoosh_png.js +++ b/packages/png/codec/squoosh_png.js @@ -173,3 +173,18 @@ async function init(input) { export default init; +(function () { + const isRunningInCloudFlareWorkers = caches.default !== undefined; + if (isRunningInCloudFlareWorkers) { + if (!globalThis.ImageData) { + // Simple Polyfill for ImageData Object + globalThis.ImageData = class ImageData { + constructor(data, width, height) { + this.data = data; + this.width = width; + this.height = height; + } + }; + } + } +})();