简体   繁体   English

Webpack 打包到全局范围? (对于 manifest v3 service worker)

[英]Webpack pack to global scope? (For manifest v3 service worker)

Manifest v3 service workers require listeners are defined in the global scope of a file, webpack compiles to inside an anonymous function. Manifest v3 服务工作者需要在文件的全局范围内定义侦听器,webpack 编译到匿名函数内部。 These two things seems basically incompatible.这两件事似乎基本不相容。 Does anyone have a solution better than ditching webpack?有没有人有比放弃 webpack 更好的解决方案?

There's no such requirement, it's an incorrect/inaccurate claim in the documentation : "top level of your script" is a misrepresentation of the technical terms used to describe the JS engine internals.没有这样的要求,这是文档中不正确/不准确的声明: “脚本的顶层”是对用于描述 JS 引擎内部的技术术语的歪曲。

The actual requirement is to register the listeners in the main (synchronous) phase of the first task of the JS event loop , which may certainly happen inside a function:实际要求在 JS 事件循环的第一个任务的主(同步)阶段注册监听器,这肯定会发生在函数内部:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM