An open API service indexing awesome lists of open source software.

https://github.com/wx-dongxing/unplugin-detect-update

Detect web app version update for Vite, Webpack and Rollup
https://github.com/wx-dongxing/unplugin-detect-update

plugins rollup unplugin update version vite webpack

Last synced: 6 months ago
JSON representation

Detect web app version update for Vite, Webpack and Rollup

Awesome Lists containing this project

README

          

# unplugin-detect-update

[![NPM version](https://img.shields.io/npm/v/unplugin-detect-update?color=a1b858&label=)](https://www.npmjs.com/package/unplugin-detect-update)

Detect web app version update for Vite, Webpack and Rollup. Powered by [unplugin](https://github.com/unjs/unplugin).

---

## Install

```bash
npm i unplugin-detect-update -D
```

Vite

```ts
// vite.config.ts
import DetectUpdate from 'unplugin-detect-update/vite'

export default defineConfig({
plugins: [
DetectUpdate({
/* options */
}),
],
})
```


Rollup

```ts
// rollup.config.js
import DetectUpdate from 'unplugin-detect-update/rollup'

export default {
plugins: [
DetectUpdate({
/* options */
}),
],
}
```


Webpack

```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-detect-update/webpack')({
/* options */
}),
],
}
```

> This module works for Webpack >= 3


Nuxt

```ts
// nuxt.config.js
export default {
buildModules: [
[
'unplugin-detect-update/nuxt',
{
/* options */
},
],
],
}
```

> This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite)


Vue CLI

```ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-detect-update/webpack')({
/* options */
}),
],
},
}
```


## Usage

```ts
import { useDetectUpdate } from 'unplugin-detect-update/hooks'

const { start, cancel, detect, onUpdate } = useDetectUpdate({
immediate: true,
worker: false,
ms: 5 * 60000,
trigger: ['visibility', 'focus'],
})

onUpdate(val => {
console.log('update: ', val)
})
```

The `version` will be added to the `body` in each html file after build, and also stored in session, key was `detect-update-store`

```html








...

```

## Configuration

```ts
// plugin config
DetectUpdate({
// the name of generated version record file
fileName: 'version.json',
// the type of generated version
type: 'commit',
worker: {
// whether to generate worker file
enable: true,
// worker file name
fileName: 'worker.js',
},
// extra data in version.json
extra: {},
})
```

## Types

**Plugin types**

```ts
/**
* As type as version, package.json file version field or last commit id or current timestamp
*/
export type VersionType = 'package' | 'commit' | 'timestamp'

export interface WorkerOption {
/**
* Whether to generate worker file
*
* @default true
*/
enable?: boolean
/**
* The name of generated worker file
*
* @default worker.js
*/
fileName?: string
}

export interface Options {
/**
* The name of generated version record file
*
* @default version.json
*/
fileName?: string
/**
* The type of generated version, will be set to 'package' in development mode, and set the 'version' field in package.json under the current directory to the version
*
* @default commit
*/
type?: VersionType
/**
* Worker Options
*
* @default true
*/
worker?: boolean | WorkerOption
/**
* extra data in version.json
*
* @default {}
*/
extra?: Record
}
```

**UseDetectUpdate types**

```ts
/**
* Extra detect trigger type, trigger detection when window focused or visible
*/
export type Trigger = 'focus' | 'visibility'

export interface UseDetectUpdateOptions {
/**
* Execute the detect immediately on calling
* @default true
*/
immediate?: boolean
/**
* Whether use worker, not work in development mode
* @default false
*/
worker?: boolean
/**
* cycle time, ms
* @default 5 * 60000
*/
ms?: number
/**
* Extra detect trigger types
* @default []
*/
trigger?: Trigger[]
}

export interface UseDetectUpdateReturn {
/**
* Cancel detect on calling
*/
cancel: () => void
/**
* Start detect on calling
*/
start: () => void
/**
* Active trigger version detection
*/
detect: () => void
/**
* Called when version changed
*/
onUpdate: EventHookOn
}
```

## License

[MIT](./LICENSE) License © 2023 [Dong Xing](https://github.com/WX-DongXing)