BuilderIO / partytown

Relocate resource intensive third-party scripts off of the main thread and into a web worker. 🎉
https://partytown.builder.io
MIT License
13.07k stars 435 forks source link

Allow to use modules with type="text/partytown" #195

Closed crisgrim closed 2 years ago

crisgrim commented 2 years ago

Is your feature request related to a problem? Please describe. I would like to try Partytown with some internal libraries that we are using with type="module" and nomodule. But the point is that if I include the type="text/partytown" then, when they are loaded they have been included as regular scripts. And it doesn't work.

I received this error in the console: SyntaxError: Cannot use import statement outside a module.

Describe the solution you'd like Be able to use partytown with module and nomodule scripts.

Describe alternatives you've considered I think that I can provide you an idea about a possible solution. When you do the replacement of these assets on the page you can look for the extension and if the file ends with .esm.js is a module. If not, it's a nomodule.

adamdbradley commented 2 years ago

At this time we'll hold off on this feature request since at this time web workers do not have have full support for ES module: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker

frandiox commented 2 years ago

It seems Safari already supports ESM, and the only one remaining is Firefox 😬

Question: when ESM is fully supported, would that mean a module script could run import and new URL(import.meta.url)? We are considering using something like this in @shopify/hydrogen and would love to have compatibility with Partytown in the future by just changing <script type="module" src="..." /> to <script type="text/partytown" src="..." />.

maiieul commented 1 year ago

Seems like Firefox supports ESM since the 6th of June. Shouldn't this be reopened @adamdbradley?

zhondori commented 5 months ago

@adamdbradley Firefox got support 1 year ago. Maybe consider support for ESM in partytown scripts

RomanSkrypnik commented 3 months ago

Any updates?