Home > Back-end >  Webpack pack to global scope? (For manifest v3 service worker)
Webpack pack to global scope? (For manifest v3 service worker)

Time:06-13

Manifest v3 service workers require listeners are defined in the global scope of a file, webpack compiles to inside an anonymous function. These two things seems basically incompatible. Does anyone have a solution better than ditching webpack?

CodePudding user response:

There's no such requirement, it's an incorrect/inaccurate claim in the documentation: the person who wrote "top level of your script" misunderstood that the actual requirement is to register the listeners in the main (synchronous) phase of the first task of the JS event loop, which may certainaly happen inside a function:

(() => {
  (() => {
    chrome.runtime.onMessage.addListener(msg => { /* ..... */ });
  })();
})();
  • Related