Build background.js as an es module and not iife #161
Replies: 1 comment
-
What package are you using that requires this? If a package you're using does this, you'll need to dynamically import it instead of using a regular import. const packagePromise = import('your-package') I have never seen a reason to use ESM for service workers, so WXT always builds a bundled IIFE script. It's generally a bad practice to await in the main scope of a service worker because setting up listeners fail after an await statement or inside callbacks: https://developer.chrome.com/docs/extensions/migrating/to-service-workers/#register-listeners If you need to initialize something asynchronously, store the promise as a variable instead of awaiting it in the top level. Then await it before you need to use it (you can await promises multiple times, it always resolves to the same value). Here's an example where we open indexedbd, but use the promise instead of awaiting it: |
Beta Was this translation helpful? Give feedback.
-
I am unable to build the background.js as an es module, I need this as a package I use makes use of a top level await.
I have tried the settings for scriptViteConfig, but it seems to always build as iife
Beta Was this translation helpful? Give feedback.
All reactions