Open jlarmstrongiv opened 7 months ago
As you mentioned window
is not available to web worker contexts.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers
The way the mjml-browser
build script works, it just dumps out a generic amd style export that binds the window
object.
build script source: https://github.com/mjmlio/mjml/blob/master/packages/mjml-browser/webpack.config.js
public compiled source: https://cdn.jsdelivr.net/npm/mjml-browser@4.15.3/lib/index.js
This would likely require a mjml-webworker
package to be built for this use case.
If we remove the window binding (which the mjml
library does not depend on), then the mjml-browser
package would work in web workers.
So, I’m not sure if a separate package is required, as long as we modify the generic amd style export in some way:
globalThis
instead of window
window
if it existsI think that would fix Deno as well, which reports (as of Deno v2):
Uncaught (in promise) ReferenceError: window is not defined
at https://esm.sh/v135/mjml-browser@4.14.1/denonext/mjml-browser.mjs:2:894
at https://esm.sh/v135/mjml-browser@4.14.1/denonext/mjml-browser.mjs:2:213
at https://esm.sh/v135/mjml-browser@4.14.1/denonext/mjml-browser.mjs:942:2375
info: window global is not available in Deno 2.
hint: Replace `window` with `globalThis`.
I'lll see what I can do when I'll finish #2908
Describe the bug
In a web worker:
To Reproduce Steps to reproduce the behavior:
import mjml2html from "mjml-browser";
Expected behavior I expect
mjml-browser
to work in web workers, without throwing any errors. It’s a cpu heavy process that would be perfect for web workersMJML environment (please complete the following information):
mjml-browser
Email sending environment(for rendering issues): N/A
Affected email clients (for rendering issues): N/A
Screenshots N/A
Additional context
window
is not a global variable in web workers https://stackoverflow.com/a/11237259