我把前端部署用户提醒做成了一个npm包

背景

最近发现很多手机APP和小程序在打开或者使用过程中会弹框提示用户新版本发布,是否更新。我就想着自己能不能实现一个。

点此跳转Github查看源码

功能要求

  • 纯前端实现,不限制框架用React、Vue、Angular都可
  • 不限制打包工具Webpack、Vite都可使用
  • 使用发布订阅模式实现,支持用户自定义更新提醒事件
  • 发布到npm,安装后开箱即用

功能实现

功能实现也非常简单,大致实现如下:

  1. 打包构建的时候拿到webpack或者vite的打包id,通过Node的fs模块将其放到打包(/dist)目录下(最好是可访问的静态资源里面,例如/public/config.json)。
// /public/config.json
{
  hash: "123456"
}
  1. 我们需要写一个webpack或者vite插件在打包构建完成后拿到hash生成文件,然后输出到/dist目录里,还好我们有unplugin,一套代码可以适配webpack、vite插件。

    注意:Webpack的afterEmit钩子里有提供当前打包构建的hash值,而Vite没有(希望只是我没有找到,有没有大佬告诉我Vite哪个钩子可以获取到打包构建的hash值)所以我是自己生成了uuid作为hash

    代码如下:

    import { createUnplugin } from "unplugin"
    import { writeFileSync, mkdirSync } from "node:fs"
    import path from "node:path"
    
    // 生成config.json文件
    const generateConfig = (configPath: string, hash: string) => {
      // 确保目录存在
      mkdirSync(path.dirname(configPath), { recursive: true })
      writeFileSync(
        configPath,
        JSON.stringify(
          {
            hash,
          },
          null,
          2
        )
      )
    }
    
    // filePath不传默认生成到/dist目录下
    export default createUnplugin((filePath: string = "config.json") => {
      let viteOutDir = "dist"
      return {
        name: "unplugin-app-update",
        // 生成Vite插件
        vite: {
          configResolved(config) {
            viteOutDir = config.build.outDir
          },
          closeBundle() {
            const configPath = path.join(process.cwd(), viteOutDir, filePath)
            generateConfig(configPath, uuid())
          },
        },
        // 生成webpack插件
        webpack(compiler) {
          // 只在生产模式下生成文件
          if (compiler.options.mode !== "production") {
            return
          }
          compiler.hooks.afterEmit.tap(
            "unplugin-app-update",
            (compilation: any) => {
              const configPath = path.join(
                compiler.options.output.path as string,
                filePath
              )
              generateConfig(configPath, compilation.hash)
            }
          )
        },
      }
    })
    
    
  2. 然后前端通过轮询这个文件,对比hash有变化则打开一个弹窗提醒用户有新功能发布。

    我用发布订阅模式写了一个AppUpdate的类去实现这个功能,。

    首先我们需要有一个获取config.json配置文件的方法,为了不引入axios直接使用fetch API

export class AppUpdate {
  // 请求配置文件的url,默认不传请求url: http://your.website.com/config.json
  constructor({url = "config.json"}) {
    this.url = url
  }
  /* ... */
  async getConfig() {
    const config = await fetch(this.url, {
      // 强制开启协商缓存
      headers: { "Cache-Control": "max-age=0" },
    }).then((res) => res.text())
    return JSON.parse(config)
  }
  /* ... */
  1. 然后我们需要在首次进入页面时加载一次当前配置文件获取初始hash值
async init() {
  this.oldConfig = await this.getConfig()
}
  1. 开启轮询,对比hash是否变化
// 开始检查
check() {
  this.stop()
  this.timer = setInterval(async () => {
    this.newConfig = await this.getConfig()
    this.compare()
  }, this.interval) as unknown as number
}

// 停止检查
stop() {
  clearInterval(this.timer)
}

// 对比
compare() {
  if (this.newConfig.hash === this.oldConfig.hash) {
    this.dispatch("notUpdate")
  } else {
    this.oldConfig = this.newConfig
    this.newConfig = {}
    this.dispatch("update")
  }
}

// 触发事件
dispatch(key: "notUpdate" | "update") {
  this.callbacks[key]?.forEach((callback: AnyMethod) => {
    callback()
  })
}
  1. 支持用户自定义更新和未更新的事件回调
on(key: "notUpdate" | "update", callback: AnyMethod) {
  ;(this.callbacks[key] || (this.callbacks[key] = [])).push(callback)
}

off(key: "notUpdate" | "update", callback: AnyMethod) {
  const index = (this.callbacks[key] || (this.callbacks[key] = [])).findIndex(
    (item) => item === callback
  )
  if (index !== -1) {
    this.callbacks[key].splice(index, 1)
  }
}
  1. 默认添加应用更新弹窗提醒
export class AppUpdate {
    // 初始化
    constructor({
      url = "config.json",
      interval = 30000,
      locate,
      custom = false,
    }) {
      // 国际化,默认为当前浏览器语言
      if (locate) {
        i18n.changeLanguage(locate)
      }
      this.url = url
      this.interval = interval
      // 初次获取config文件hash值
      this.init()
      // 开始轮询
      this.check()
      // 添加默认提醒事件,自定义设置可设置custom: true
      if (!custom) {
        this.on("update", () => {
          if (!this.modal) {
            this.modal = Modal.confirm({
              title: i18n.t("updateModelTitle"),
              content: i18n.t("updateModelContent"),
              style: {
                top: 200,
              },
              okText: i18n.t("comfirm"),
              cancelText: i18n.t("cancel"),
              onOk: () => {
                window.location.reload()
              },
              onCancel: () => {
                this.modal = null
              },
            })
          }
        })
      }
    }
  /* ... */
}

如何使用

安装

// npm
npm i unplugin-app-update -S
// pnpm
pnpm i unplugin-app-update -S
// yarn
yarn add unplugin-app-update

Webpack

// webpack.config.js
const appUpdate = require("unplugin-app-update/webpack")

module.exports = {
  /* ... */
  plugins: [
    appUpdate('/path/to/config.json'),
  ],
}

Vite

// vite.config.ts
import appUpdate from "unplugin-app-update/vite"

export default defineConfig({
  plugins: [
    // default to the dist directory
    appUpdate(),
  ],
})

入口配置

// main.js or index.js
import { AppUpdate } from "unplugin-app-update"
const appUpdate = new AppUpdate({ /* Options */ })

// 停止轮询
// appUpdate.stop()
// 继续轮询
// appUpdate.check()

// 事件:update, notUpdate
const update = ()=>{
  console.log("Update")
}
// 自定义更新用户提醒
appUpdate.on("update", update)
appUpdate.on("notUpdate", ()=>{
  console.log("Not Update")
})
// 解绑事件
appUpdate.off("update", update)

AppUpdate选项

属性类型描述默认值
urlStringconfig.json配置文件urlconfig.json
intervalNumber轮询时间间隔30000
locatezh_CN | en_US国际化Default language of browser
customBoolean设置为true可删除默认弹出提醒,添加on(‘update’,fn)事件自定义弹出false

注意

在本地开发时,需要在公共目录中放置一个config.json文件

手动更改哈希值以模拟项目构建

如果webpack或vite调整了公共目录,您应该新建AppUpdate({url:‘your/customer/path’)

// /public/config.json
{
  hash: "123456"
}

404错误

在生产和本地开发过程中,配置时经常遇到404错误。找不到json

// webpack.config.js or vite.config.ts
{
  output: {
    // 修改打包后访问路径
    publicPath: "/",
  }
}

最后

  1. 都看到这了,可以给我的Github仓库点个小小的Star吗?这真的对我很重要!重要!重要!欢迎给我提Issue、共建。
  2. 有兴趣可以加我微信号:vgbire,一起了解更多前端资讯。

在这里插入图片描述

  • 16
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将一个模块打包npm 本地,可以按照以下步骤进行操作: 1. 在模块的根目录下创建一个 `package.json` 文件,其中含模块的名称、版本、描述等信息。可以使用 `npm init` 命令来自动生成这个文件,然后根据需要进行编辑。 2. 将模块的源代码和相关文件放在一个目录中,例如 `src` 目录。 3. 在模块的根目录下创建一个 `index.js` 文件,该文件是模块的入口文件,用于导出模块的功能。在这个文件中,可以使用 `module.exports` 语句来导出模块中的函数、变量等。例如: ```javascript module.exports = { greet: function(name) { console.log('Hello, ' + name + '!'); } }; ``` 4. (可选)如果模块依赖其他的模块,可以在 `package.json` 文件的 `dependencies` 字段中列出这些依赖项。例如: ```json { "name": "my-module", "version": "1.0.0", "description": "My awesome module", "dependencies": { "lodash": "^4.17.21" } } ``` 5. 在模块的根目录下执行以下命令将模块打包一个本地压缩: ``` npm pack ``` 6. 执行完上述命令后,会在模块的根目录下生成一个类似于 `my-module-1.0.0.tgz` 的压缩。这个文件就是我们的 npm 本地了。 7. 要在本地项目中使用这个本地,可以使用以下命令进行安装: ``` npm install /path/to/my-module-1.0.0.tgz ``` 其中 `/path/to/my-module-1.0.0.tgz` 是本地的路径。安装完成后,就可以在项目中使用这个模块了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vgbire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值