vite-plugin-reload-on-rebuild 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +67 -0
- package/dist/index.cjs +93 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +68 -0
- package/package.json +56 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
MIT No Attribution (MIT-0)
|
|
2
|
+
|
|
3
|
+
Copyright 2026 Konstantin Barabanov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# vite-plugin-reload-on-rebuild
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
npm install vite-plugin-reload-on-rebuild --save-dev
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
`vite-plugin-reload-on-rebuild` plugin adds `reload-on-rebuild.js` script on the page which polls `window.location.href` and reloads the page if `etag` or `last-modified` headers are changed.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Add `vite-plugin-reload-on-rebuild` plugin to your project:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install vite-plugin-reload-on-rebuild --save-dev
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Use it as a `vite` plugin:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// vite.config.js
|
|
21
|
+
import { defineConfig } from "vite";
|
|
22
|
+
import { reloadOnRebuild } from "vite-plugin-reload-on-rebuild";
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
plugins: [reloadOnRebuild()],
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Run `vite` in `watch` mode:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npx vite build -w
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Change the source code and see how your page reloads 🚀
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
interface ReloadOnRebuildOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Is plugin enabled
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
enabled?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Should plugin work only in "watch" mode (detect "vite build --watch") or "always"
|
|
48
|
+
* @default "watch"
|
|
49
|
+
*/
|
|
50
|
+
mode?: "watch" | "always";
|
|
51
|
+
/**
|
|
52
|
+
* Filename for emitted asset script
|
|
53
|
+
* @default "reload-on-rebuild.js"
|
|
54
|
+
*/
|
|
55
|
+
fileName?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Poll interval (ms)
|
|
58
|
+
* @default 3000
|
|
59
|
+
*/
|
|
60
|
+
interval?: number;
|
|
61
|
+
/**
|
|
62
|
+
* List of headers that should be checked
|
|
63
|
+
* @default - ["etag", "last-modified"]
|
|
64
|
+
*/
|
|
65
|
+
headers?: string[];
|
|
66
|
+
}
|
|
67
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
reloadOnRebuild: () => reloadOnRebuild
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
function reloadOnRebuild({
|
|
27
|
+
enabled = true,
|
|
28
|
+
mode = "watch",
|
|
29
|
+
fileName = "reload-on-rebuild.js",
|
|
30
|
+
interval = 3e3,
|
|
31
|
+
headers = ["etag", "last-modified"]
|
|
32
|
+
} = {}) {
|
|
33
|
+
let isWatchMode = false;
|
|
34
|
+
let basePath;
|
|
35
|
+
return {
|
|
36
|
+
name: "vite-plugin-reload-on-rebuild",
|
|
37
|
+
configResolved(config) {
|
|
38
|
+
isWatchMode = !!config.build.watch;
|
|
39
|
+
basePath = config.base;
|
|
40
|
+
},
|
|
41
|
+
async generateBundle() {
|
|
42
|
+
if (!enabled) return;
|
|
43
|
+
if (mode === "watch" && !isWatchMode) return;
|
|
44
|
+
this.emitFile({
|
|
45
|
+
type: "asset",
|
|
46
|
+
fileName,
|
|
47
|
+
source: `(function() {
|
|
48
|
+
const headers = ${JSON.stringify(headers.map((h) => h.toLocaleLowerCase().trim()))}
|
|
49
|
+
const savedValues = {}
|
|
50
|
+
async function check() {
|
|
51
|
+
try {
|
|
52
|
+
const res = await fetch(window.location.href, { method: 'HEAD', cache: 'no-cache' });
|
|
53
|
+
|
|
54
|
+
for (const header of headers) {
|
|
55
|
+
const value = res.headers.get(header)
|
|
56
|
+
if (header in savedValues && savedValues[header] !== value) {
|
|
57
|
+
console.log('[vite-plugin-reload-on-rebuild]', 'Build changed. Reloading...');
|
|
58
|
+
location.reload();
|
|
59
|
+
}
|
|
60
|
+
savedValues[header] = value
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {}
|
|
63
|
+
setTimeout(check, ${interval});
|
|
64
|
+
}
|
|
65
|
+
if (headers.length > 0) check()
|
|
66
|
+
})();
|
|
67
|
+
`.trim()
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
transformIndexHtml(html) {
|
|
71
|
+
if (!enabled) return;
|
|
72
|
+
if (mode === "watch" && !isWatchMode) return;
|
|
73
|
+
return {
|
|
74
|
+
html,
|
|
75
|
+
tags: [
|
|
76
|
+
{
|
|
77
|
+
tag: "script",
|
|
78
|
+
attrs: {
|
|
79
|
+
src: `${basePath}${fileName}`,
|
|
80
|
+
id: "vite-plugin-reload-on-rebuild",
|
|
81
|
+
type: "module"
|
|
82
|
+
},
|
|
83
|
+
injectTo: "head"
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
reloadOnRebuild
|
|
93
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
interface ReloadOnRebuildOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Is plugin enabled
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Should plugin work only in "watch" mode (detect "vite build --watch") or "always"
|
|
11
|
+
* @default "watch"
|
|
12
|
+
*/
|
|
13
|
+
mode?: "watch" | "always";
|
|
14
|
+
/**
|
|
15
|
+
* Filename for emitted asset script
|
|
16
|
+
* @default "reload-on-rebuild.js"
|
|
17
|
+
*/
|
|
18
|
+
fileName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Poll interval (ms)
|
|
21
|
+
* @default 3000
|
|
22
|
+
*/
|
|
23
|
+
interval?: number;
|
|
24
|
+
/**
|
|
25
|
+
* List of headers that should be checked
|
|
26
|
+
* @default - ["etag", "last-modified"]
|
|
27
|
+
*/
|
|
28
|
+
headers?: string[];
|
|
29
|
+
}
|
|
30
|
+
declare function reloadOnRebuild({ enabled, mode, fileName, interval, headers, }?: ReloadOnRebuildOptions): Plugin;
|
|
31
|
+
|
|
32
|
+
export { type ReloadOnRebuildOptions, reloadOnRebuild };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
interface ReloadOnRebuildOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Is plugin enabled
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Should plugin work only in "watch" mode (detect "vite build --watch") or "always"
|
|
11
|
+
* @default "watch"
|
|
12
|
+
*/
|
|
13
|
+
mode?: "watch" | "always";
|
|
14
|
+
/**
|
|
15
|
+
* Filename for emitted asset script
|
|
16
|
+
* @default "reload-on-rebuild.js"
|
|
17
|
+
*/
|
|
18
|
+
fileName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Poll interval (ms)
|
|
21
|
+
* @default 3000
|
|
22
|
+
*/
|
|
23
|
+
interval?: number;
|
|
24
|
+
/**
|
|
25
|
+
* List of headers that should be checked
|
|
26
|
+
* @default - ["etag", "last-modified"]
|
|
27
|
+
*/
|
|
28
|
+
headers?: string[];
|
|
29
|
+
}
|
|
30
|
+
declare function reloadOnRebuild({ enabled, mode, fileName, interval, headers, }?: ReloadOnRebuildOptions): Plugin;
|
|
31
|
+
|
|
32
|
+
export { type ReloadOnRebuildOptions, reloadOnRebuild };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function reloadOnRebuild({
|
|
3
|
+
enabled = true,
|
|
4
|
+
mode = "watch",
|
|
5
|
+
fileName = "reload-on-rebuild.js",
|
|
6
|
+
interval = 3e3,
|
|
7
|
+
headers = ["etag", "last-modified"]
|
|
8
|
+
} = {}) {
|
|
9
|
+
let isWatchMode = false;
|
|
10
|
+
let basePath;
|
|
11
|
+
return {
|
|
12
|
+
name: "vite-plugin-reload-on-rebuild",
|
|
13
|
+
configResolved(config) {
|
|
14
|
+
isWatchMode = !!config.build.watch;
|
|
15
|
+
basePath = config.base;
|
|
16
|
+
},
|
|
17
|
+
async generateBundle() {
|
|
18
|
+
if (!enabled) return;
|
|
19
|
+
if (mode === "watch" && !isWatchMode) return;
|
|
20
|
+
this.emitFile({
|
|
21
|
+
type: "asset",
|
|
22
|
+
fileName,
|
|
23
|
+
source: `(function() {
|
|
24
|
+
const headers = ${JSON.stringify(headers.map((h) => h.toLocaleLowerCase().trim()))}
|
|
25
|
+
const savedValues = {}
|
|
26
|
+
async function check() {
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch(window.location.href, { method: 'HEAD', cache: 'no-cache' });
|
|
29
|
+
|
|
30
|
+
for (const header of headers) {
|
|
31
|
+
const value = res.headers.get(header)
|
|
32
|
+
if (header in savedValues && savedValues[header] !== value) {
|
|
33
|
+
console.log('[vite-plugin-reload-on-rebuild]', 'Build changed. Reloading...');
|
|
34
|
+
location.reload();
|
|
35
|
+
}
|
|
36
|
+
savedValues[header] = value
|
|
37
|
+
}
|
|
38
|
+
} catch (e) {}
|
|
39
|
+
setTimeout(check, ${interval});
|
|
40
|
+
}
|
|
41
|
+
if (headers.length > 0) check()
|
|
42
|
+
})();
|
|
43
|
+
`.trim()
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
transformIndexHtml(html) {
|
|
47
|
+
if (!enabled) return;
|
|
48
|
+
if (mode === "watch" && !isWatchMode) return;
|
|
49
|
+
return {
|
|
50
|
+
html,
|
|
51
|
+
tags: [
|
|
52
|
+
{
|
|
53
|
+
tag: "script",
|
|
54
|
+
attrs: {
|
|
55
|
+
src: `${basePath}${fileName}`,
|
|
56
|
+
id: "vite-plugin-reload-on-rebuild",
|
|
57
|
+
type: "module"
|
|
58
|
+
},
|
|
59
|
+
injectTo: "head"
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
reloadOnRebuild
|
|
68
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-reload-on-rebuild",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Reloads page when current html file changes (on rebuild; without dev server)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"require": {
|
|
11
|
+
"types": "./dist/index.d.cts",
|
|
12
|
+
"default": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"prepack": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/crutch12/vite-plugin-reload-on-rebuild.git"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"vite",
|
|
29
|
+
"plugin",
|
|
30
|
+
"vite-plugin",
|
|
31
|
+
"vitejs",
|
|
32
|
+
"rebuild",
|
|
33
|
+
"reload",
|
|
34
|
+
"watch"
|
|
35
|
+
],
|
|
36
|
+
"author": {
|
|
37
|
+
"name": "Konstantin Barabanov",
|
|
38
|
+
"email": "criitch@yandex.ru"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT-0",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/crutch12/vite-plugin-reload-on-rebuild/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/crutch12/vite-plugin-reload-on-rebuild#readme",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^25.3.0",
|
|
47
|
+
"tsup": "^8.5.1",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vite": "^7.3.1"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"README.md",
|
|
54
|
+
"LICENSE.md"
|
|
55
|
+
]
|
|
56
|
+
}
|