plugin-web-update-notification-rsbuild 1.0.0
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 +19 -0
- package/README.md +28 -0
- package/package.json +39 -0
- package/src/index.ts +115 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# plugin-web-update-notification-rsbuild
|
|
2
|
+
|
|
3
|
+
基于项目:https://github.com/GreatAuk/plugin-web-update-notification
|
|
4
|
+
|
|
5
|
+
用于Rsbuild检测网页更新并通知用户刷新。
|
|
6
|
+
## 安装
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add plugin-web-update-notification-rsbuild -D
|
|
10
|
+
```
|
|
11
|
+
## 使用
|
|
12
|
+
基于plugin-web-update-notification,参数配置完全一致。
|
|
13
|
+
```ts
|
|
14
|
+
import { webUpdateNotice } from 'plugin-web-update-notification-rsbuild';
|
|
15
|
+
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
plugins: [
|
|
18
|
+
...,
|
|
19
|
+
webUpdateNotice({
|
|
20
|
+
...
|
|
21
|
+
})
|
|
22
|
+
],
|
|
23
|
+
...
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
[MIT](./LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plugin-web-update-notification-rsbuild",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Rsbuild plugin for detect web page updates and notify. based on @plugin-web-update-notification/core.",
|
|
6
|
+
"author": "Anlige",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/sometiny/plugin-web-update-notification-rsbuild",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/sometiny/plugin-web-update-notification-rsbuild"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sometiny/plugin-web-update-notification-rsbuild/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"rsbuild",
|
|
18
|
+
"rsbuild-plugin",
|
|
19
|
+
"plugin-web-update-notification-rsbuild",
|
|
20
|
+
"web-update-notification"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./src/index.js",
|
|
26
|
+
"default": "./src/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "src/index.js",
|
|
30
|
+
"module": "src/index.js",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@plugin-web-update-notification/core": "^2.0.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@rsbuild/core": "^1.4.13",
|
|
36
|
+
"@types/node": "^25.3.3",
|
|
37
|
+
"typescript": "^5.9.2"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import type { Options } from '@plugin-web-update-notification/core';
|
|
4
|
+
type HTMLTags = {
|
|
5
|
+
headTags: HtmlBasicTag[];
|
|
6
|
+
bodyTags: HtmlBasicTag[];
|
|
7
|
+
};
|
|
8
|
+
import {
|
|
9
|
+
DIRECTORY_NAME,
|
|
10
|
+
INJECT_SCRIPT_FILE_NAME,
|
|
11
|
+
INJECT_SCRIPT_TAG_ID,
|
|
12
|
+
INJECT_STYLE_FILE_NAME,
|
|
13
|
+
JSON_FILE_NAME,
|
|
14
|
+
NOTIFICATION_ANCHOR_CLASS_NAME,
|
|
15
|
+
generateJSONFileContent,
|
|
16
|
+
generateJsFileContent,
|
|
17
|
+
getFileHash,
|
|
18
|
+
getVersion,
|
|
19
|
+
get__Dirname,
|
|
20
|
+
} from '@plugin-web-update-notification/core';
|
|
21
|
+
import { HtmlBasicTag, RsbuildPlugin, RsbuildPluginAPI } from '@rsbuild/core';
|
|
22
|
+
|
|
23
|
+
// /**
|
|
24
|
+
// * Get the version of the current Vite
|
|
25
|
+
// *
|
|
26
|
+
// * if the viteVersion is undefined, we assume that vite is less than v3.0(after v3.0, vite export version)
|
|
27
|
+
// */
|
|
28
|
+
// async function getViteVersion(): Promise<string | undefined> {
|
|
29
|
+
// return await import('vite').then(({ version }) => version)
|
|
30
|
+
// }
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* It injects the hash into the HTML, and injects the notification anchor and the stylesheet and the
|
|
34
|
+
* script into the HTML
|
|
35
|
+
* @param {string} html - The original HTML of the page
|
|
36
|
+
* @param {string} version - The hash of the current commit
|
|
37
|
+
* @param {Options} options - Options
|
|
38
|
+
* @param cssFileHash
|
|
39
|
+
* @param jsFileHash
|
|
40
|
+
* @returns The html of the page with the injected script and css.
|
|
41
|
+
*/
|
|
42
|
+
function injectPluginHtml(
|
|
43
|
+
html: string,
|
|
44
|
+
version: string,
|
|
45
|
+
options: Options,
|
|
46
|
+
{ cssFileHash, jsFileHash }: { jsFileHash: string; cssFileHash: string }
|
|
47
|
+
) {
|
|
48
|
+
const { customNotificationHTML, hiddenDefaultNotification, injectFileBase = '' } = options;
|
|
49
|
+
|
|
50
|
+
const cssLinkHtml =
|
|
51
|
+
customNotificationHTML || hiddenDefaultNotification
|
|
52
|
+
? ''
|
|
53
|
+
: `<link rel="stylesheet" href="${injectFileBase}${DIRECTORY_NAME}/${INJECT_STYLE_FILE_NAME}.${cssFileHash}.css">`;
|
|
54
|
+
let res = html;
|
|
55
|
+
|
|
56
|
+
res = res.replace(
|
|
57
|
+
'<head>',
|
|
58
|
+
`<head>
|
|
59
|
+
${cssLinkHtml}
|
|
60
|
+
<script data-id="${INJECT_SCRIPT_TAG_ID}" data-v="${version}" src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>`
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!hiddenDefaultNotification) {
|
|
64
|
+
res = res.replace('</body>', `<div class="${NOTIFICATION_ANCHOR_CLASS_NAME}"></div></body>`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function webUpdateNotice(options: Options = {}): RsbuildPlugin {
|
|
71
|
+
const { versionType, customVersion, silence } = options;
|
|
72
|
+
let version = '';
|
|
73
|
+
if (versionType === 'custom') version = getVersion(versionType, customVersion!);
|
|
74
|
+
else version = getVersion(versionType!);
|
|
75
|
+
|
|
76
|
+
/** inject script file hash */
|
|
77
|
+
let jsFileHash = '';
|
|
78
|
+
/** inject css file hash */
|
|
79
|
+
let cssFileHash = '';
|
|
80
|
+
|
|
81
|
+
const cssFileSource = readFileSync(`${resolve(get__Dirname(), INJECT_STYLE_FILE_NAME)}.css`, 'utf8').toString();
|
|
82
|
+
cssFileHash = getFileHash(cssFileSource);
|
|
83
|
+
|
|
84
|
+
let jsFileSource = '';
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
name: 'vue-rsbuild-web-update-notice',
|
|
88
|
+
apply: 'build',
|
|
89
|
+
setup(api: RsbuildPluginAPI) {
|
|
90
|
+
const config = api.getRsbuildConfig();
|
|
91
|
+
if (options.injectFileBase === undefined) options.injectFileBase = config.server?.base || '/';
|
|
92
|
+
|
|
93
|
+
jsFileSource = generateJsFileContent(
|
|
94
|
+
readFileSync(`${resolve(get__Dirname(), INJECT_SCRIPT_FILE_NAME)}.js`, 'utf8').toString(),
|
|
95
|
+
version,
|
|
96
|
+
options
|
|
97
|
+
);
|
|
98
|
+
jsFileHash = getFileHash(jsFileSource);
|
|
99
|
+
|
|
100
|
+
api.modifyHTML((html: string) => {
|
|
101
|
+
return injectPluginHtml(html, version, options, { jsFileHash, cssFileHash });
|
|
102
|
+
});
|
|
103
|
+
api.modifyHTMLTags(({ headTags, bodyTags }: HTMLTags) => {
|
|
104
|
+
return { headTags, bodyTags };
|
|
105
|
+
});
|
|
106
|
+
api.processAssets({ stage: 'additional' }, ({ sources, compilation }) => {
|
|
107
|
+
compilation.emitAsset(`${DIRECTORY_NAME}/${JSON_FILE_NAME}.json`, new sources.RawSource(generateJSONFileContent(version, silence)));
|
|
108
|
+
|
|
109
|
+
compilation.emitAsset(`${DIRECTORY_NAME}/${INJECT_STYLE_FILE_NAME}.${cssFileHash}.css`, new sources.RawSource(cssFileSource));
|
|
110
|
+
|
|
111
|
+
compilation.emitAsset(`${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js`, new sources.RawSource(jsFileSource));
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|