简体   繁体   English

让webpack处理导入库中的服务工作者

[英]Have webpack handle a service worker that is in an imported library

I'm writing a library that contains a service worker. 我正在写一个包含服务工作者的库。 I'd like to create instructions for use something like this: 我想创建使用这样的说明:

npm install my-lib
...
import MyLib from 'my-lib'
...
MyLib.init();

And in the init() function I need to register the service worker. init()函数中,我需要注册服务工作者。

I'd like the actual service worker file to be in my library, and it would be nice if I didn't have to ask the client to change their webpack.config.js file. 我希望实际的服务工作者文件在我的库中,如果我不必要求客户端更改他们的webpack.config.js文件,那将是很好的。

Is there a way for my library to indicate that the service worker file should be copied to the server with no processing so that it can be registered? 有没有办法让我的库指示应该将服务工作者文件复制到服务器而不进行处理,以便可以注册它?

I was thinking my library would have a structure like: 我以为我的图书馆会有如下结构:

public
    sw.js
src
    etc.js
index.js
package.json

Thanks for any help you can give. 谢谢你提供的所有帮助。 Most of the info I've found concerns the PWA service worker that caches assets and I don't see much info on just simple, generic service workers. 我发现的大多数信息都涉及缓存资产的PWA服务工作者,而我看不到关于简单的通用服务工作者的太多信息。

您可以为工作脚本使用Blob而不是单独的文件: 如何从字符串创建Web Worker

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

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