npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generate-register-sw-plugin

v1.0.4

Published

a plugin to generate a code snippet for registe service worker based on the template, inline or outreach to html

Downloads

2

Readme

使用方法

npm i generate-register-sw-plugin

测试例子

npm run inject:example npm run example

跑完之后可以在 dist 目录下找到对应的 service-worker.js swRegister.js precacheManifest.js 等文件

字段说明

  • swFilePathservice-worker 的规则文件路径,默认优先使用 workboxGenrateSWOption 配置中的 swDest 字段, 不存在上述字段的时候再使用 swFilePath 的值, 该字段会替换模版中的 {{swFilePath}} 字符串,具体请看 build/swTpl.js 文件

  • swRegisterFileName:生成的注册 service-workerjs 文件路径,没有默认使用 js/swRegister.js 路径

  • tplPath:生成的注册 service-workerjs 文件的参照模版路径,没有即将使用默认 build/swTpl.js 文件

  • inject:是否注册 service-worker 的代码直接插入到 html 的最后面,默认 falsefalse 将会以外链的方式插入到最后面,使用该功能需要把该插件放到 html-webpack-plugin 后面,否则会找不到 html 文件

  • tplData:模版所需要的字段,{{env}} 会被替换成 env 值的字符串,具体请看 build/swTpl.js 文件

  • injectHtmlPath: 存在该字段的时候,会在这个 html 下注入 js 代码, 并且忽略 html-webpack-plugin 生成的 html

  • workboxGenrateSWOption: 支持 workbox-webpack-pluginGenerateSW 插件配置(对该配置项完全透传给 workbox-webpack-plugin), 详细见: https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_generatesw_config, 使用该配置有些坑, 比如在 runtimeCaching 中不配置 cacheName 就无法使用 expiration, precacheManifestFilename 如果不包含 [manifestHash] 也跑不起来, 这是 workbox-webpack-plugin 本身存在的问题

  • workboxInjectManifestOption: 支持 workbox-webpack-plugin 的预加载 InjectManifest 插件,详细配置见 https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#injectmanifest_plugin_2

注意: 该插件最好配合 html-webpack-plugin 一起使用, 放在 html-webpack-plugin 插件后面, 会自动为所有生成的 html 文件插入注册的代码

new GenerateRegisterSWJS({
    swFilePath: './service-worker.' + process.env.BUILD_ENV + '.js',
    swRegisterFileName: 'js/swRegister.js',
    tplPath: undefined,
    inject: false,
    injectHtmlPath: './example/index.html',
    tplData: {
      env: process.env.BUILD_ENV,
      version: BUILD_VERSION,
      isOpenPWA: process.env.BUILD_ENV === 'dev' ? true : true
    },
    workboxGenrateSWOption: object || false,
    workboxInjectManifestOption: object || false
  })
})