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
- Host: GitHub
- URL: https://github.com/wx-dongxing/unplugin-detect-update
- Owner: WX-DongXing
- License: mit
- Created: 2023-01-10T02:16:04.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-31T09:38:43.000Z (almost 3 years ago)
- Last Synced: 2025-03-21T23:33:53.957Z (10 months ago)
- Topics: plugins, rollup, unplugin, update, version, vite, webpack
- Language: TypeScript
- Homepage: https://detect-update-vite-app-ifyh.vercel.app/
- Size: 98.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unplugin-detect-update
[](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)