hexo-swpp 3.1.2 → 3.2.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/README.md +39 -24
- package/dist/index.js +41 -61
- package/package.json +2 -2
- package/types/index.d.ts +1 -7
package/README.md
CHANGED
|
@@ -10,15 +10,35 @@ npm install hexo-swpp swpp-backends
|
|
|
10
10
|
|
|
11
11
|
当 `swpp-backends` 存在版本更新时,可以直接更新 `swpp-backends` 版本,不需要更新 `hexo-swpp` 的版本。(不过 `hexo-swpp` 有更新的话最好也跟进一下。)
|
|
12
12
|
|
|
13
|
+
注意:更新 `swpp-backends` 版本时需要注意其版本是否与 `hexo-swpp` 匹配,版本匹配列表如下:
|
|
14
|
+
|
|
15
|
+
| hexo-swpp 版本 | swpp-backends 版本 |
|
|
16
|
+
|:------------:|:----------------:|
|
|
17
|
+
| ~3.0 | ^1.0.0 |
|
|
18
|
+
| ~3.1 | ^1.1.0 |
|
|
19
|
+
| ~3.2 | ^2.0.0 |
|
|
20
|
+
|
|
13
21
|
## 使用
|
|
14
22
|
|
|
15
23
|
在 hexo 或主题的配置文件中添加如下内容即可启用插件:
|
|
16
24
|
|
|
17
25
|
```yml
|
|
18
26
|
swpp:
|
|
27
|
+
# 是否启用插件
|
|
19
28
|
enable: true
|
|
29
|
+
# 是否在发布前自动执行脚本
|
|
30
|
+
# auto_exec: true
|
|
20
31
|
```
|
|
21
32
|
|
|
33
|
+
插件会在生成网站时自动生成 Service Worker、注册代码、DOM 端支持代码(如果功能开启了的话),版本更新文件需要通过 `hexo swpp` 命令手动生成。
|
|
34
|
+
|
|
35
|
+
`auto_exec` 配置项允许用户在执行 `hexo deploy` 指令时自动执行 `hexo swpp` 的内容(注意开启该配置项后无法再使用 `hexo swpp` 命令)。
|
|
36
|
+
|
|
37
|
+
⚠ 注意:
|
|
38
|
+
|
|
39
|
+
+ 尽可能在压缩网站内容前执行 `hexo swpp`,因为部分压缩插件可能会出现同样的内容连续压缩结果不一样的问题,这会导致插件错误地更新缓存。
|
|
40
|
+
+ 如果你的网站发布过程不使用 `hexo deploy` 指令,则不要启用 `auto_exec` 选项。
|
|
41
|
+
|
|
22
42
|
插件的具体配置见 [Swpp Backends 官方文档 | 山岳库博](https://kmar.top/posts/b70ec88f/)。
|
|
23
43
|
|
|
24
44
|
### sort
|
|
@@ -39,32 +59,27 @@ module.exports.config = {
|
|
|
39
59
|
|
|
40
60
|
其中 key 值为要排序的变量的名称,value 为变量排序时的依据,填 `false` 表示禁用该项排序,填 `true` 表示以 value 本身为键进行排序,填字符串表示以 `value[tag]` 为键进行排序。
|
|
41
61
|
|
|
42
|
-
|
|
62
|
+
---
|
|
43
63
|
|
|
44
|
-
|
|
64
|
+
## 更新日志
|
|
45
65
|
|
|
46
|
-
|
|
47
|
-
module.exports.update = {
|
|
48
|
-
flag: true,
|
|
49
|
-
force: false,
|
|
50
|
-
/** @type string[] */
|
|
51
|
-
refresh: [],
|
|
52
|
-
/** @type ChangeExpression[] */
|
|
53
|
-
change: []
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
+ `flag` 是更新标记,只有当本次更新标记与上次值不相同时 `update` 的内容才会生效,所以修改 `update` 后没有必要手动清空 `update` 的内容。
|
|
58
|
-
+ `force` 为强制更新标记,当设置为 `true` 时会清除前端所有缓存。
|
|
59
|
-
+ `refresh` 为 URL 刷新列表,填写想要刷新缓存的 URL
|
|
60
|
-
+ `change` 为刷新列表,填写要提交的缓存刷新表达式,表达式写法见 `swpp-backends` 中的 `ChangeExpression` 类型
|
|
66
|
+
+ 3.2+
|
|
61
67
|
|
|
62
|
-
|
|
68
|
+
1. 支持 `swpp-backends@2` \[3.2.0]
|
|
69
|
+
2. 优化不运行 swpp 时的性能 \[3.2.1]
|
|
70
|
+
3. 支持发布前自动执行 swpp \[3.2.1]
|
|
71
|
+
+ 3.1+
|
|
63
72
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
1. 支持 `swpp-backends@1.1` \[3.1.0]
|
|
74
|
+
2. 修复无法正确读取主题配置文件的问题 \[3.1.1]
|
|
75
|
+
+ 3.0+
|
|
76
|
+
|
|
77
|
+
该版本与 V2 不兼容,请注意修改配置文件!
|
|
69
78
|
|
|
70
|
-
|
|
79
|
+
相比于 V2,V3 有以下改动:
|
|
80
|
+
1. 移除了配置项中的 `json.precisionMode` 选项
|
|
81
|
+
2. 修改了配置项 `json` 中的 `merge` 和 `exclude` 的写法
|
|
82
|
+
3. 修改了配置项中的 `external.js` 的写法
|
|
83
|
+
4. 将配置项 `external` 中的 `skip` 替换为 `stable`,`replace` 替换为 `replacer`
|
|
84
|
+
5. 规则文件中的 `cacheList` 替换为 `cacheRules`,`getCdnList` 替换为 `getRaceUrls`
|
|
85
|
+
6. 兼容 hexo 7
|
package/dist/index.js
CHANGED
|
@@ -32,10 +32,44 @@ const path_1 = __importDefault(require("path"));
|
|
|
32
32
|
const logger = require('hexo-log').default();
|
|
33
33
|
// noinspection JSUnusedGlobalSymbols
|
|
34
34
|
function start(hexo) {
|
|
35
|
-
const
|
|
36
|
-
|
|
35
|
+
const config = hexo.config;
|
|
36
|
+
const pluginConfig = config['swpp'] ?? config.theme_config['swpp'];
|
|
37
|
+
if (!pluginConfig.enable)
|
|
37
38
|
return;
|
|
38
|
-
|
|
39
|
+
sort(hexo);
|
|
40
|
+
hexo.on('generateBefore', () => {
|
|
41
|
+
loadRules(hexo);
|
|
42
|
+
buildServiceWorker(hexo);
|
|
43
|
+
});
|
|
44
|
+
if (pluginConfig['auto_exec']) {
|
|
45
|
+
hexo.on('deployBefore', async () => {
|
|
46
|
+
await runSwpp(hexo);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
hexo.extend.console.register('swpp', '生成前端更新需要的 json 文件及后端使用的版本文件', {}, async () => {
|
|
51
|
+
await runSwpp(hexo);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function runSwpp(hexo) {
|
|
56
|
+
const config = hexo.config;
|
|
57
|
+
if (!fs.existsSync(config.public_dir))
|
|
58
|
+
return logger.warn(`[SWPP] 未检测到发布目录,跳过指令执行`);
|
|
59
|
+
const rules = loadRules(hexo);
|
|
60
|
+
if (!rules.config.json)
|
|
61
|
+
return logger.error(`[SWPP] JSON 生成功能未开启,跳过指令执行`);
|
|
62
|
+
const url = config.url;
|
|
63
|
+
await Promise.all([
|
|
64
|
+
swpp_backends_1.default.loader.loadUpdateJson(url + '/update.json'),
|
|
65
|
+
swpp_backends_1.default.loader.loadVersionJson(url + '/cacheList.json')
|
|
66
|
+
]);
|
|
67
|
+
await buildVersionJson(hexo);
|
|
68
|
+
const dif = swpp_backends_1.default.builder.analyzeVersion();
|
|
69
|
+
await buildUpdateJson(hexo, dif);
|
|
70
|
+
}
|
|
71
|
+
function loadRules(hexo) {
|
|
72
|
+
const themeName = hexo.config.theme;
|
|
39
73
|
swpp_backends_1.default.event.addRulesMapEvent(rules => {
|
|
40
74
|
if ('cacheList' in rules && !('cacheRules' in rules)) {
|
|
41
75
|
rules.cacheRules = rules['cacheList'];
|
|
@@ -46,62 +80,13 @@ function start(hexo) {
|
|
|
46
80
|
delete rules['getCdnList'];
|
|
47
81
|
}
|
|
48
82
|
});
|
|
49
|
-
const
|
|
83
|
+
const result = swpp_backends_1.default.loader.loadRules('./', 'sw-rules', [`./themes/${themeName}/`, `./node_modules/hexo-${themeName}/`]);
|
|
50
84
|
swpp_backends_1.default.builder.calcEjectValues(hexo);
|
|
51
|
-
|
|
52
|
-
buildServiceWorker(hexo);
|
|
53
|
-
if (rules.config.json) {
|
|
54
|
-
hexo.extend.console.register('swpp', '生成前端更新需要的 json 文件及后端使用的版本文件', {}, async () => {
|
|
55
|
-
if (!fs.existsSync(hexo.config.public_dir))
|
|
56
|
-
return logger.warn(`[SWPP] 未检测到发布目录,跳过指令执行`);
|
|
57
|
-
const url = hexo.config.url;
|
|
58
|
-
const versionJson = await Promise.all([
|
|
59
|
-
swpp_backends_1.default.loader.loadUpdateJson(url + '/update.json'),
|
|
60
|
-
swpp_backends_1.default.loader.loadVersionJson(url + '/cacheList.json')
|
|
61
|
-
]).then(array => array[1]);
|
|
62
|
-
let forceRefreshCache = false;
|
|
63
|
-
if ('update' in rules) {
|
|
64
|
-
const update = rules.update;
|
|
65
|
-
const { flag, change, refresh, force } = update;
|
|
66
|
-
if (!flag) {
|
|
67
|
-
logger.error(`[SWPP Console] 规则文件的 update 项目必须包含 flag 值!`);
|
|
68
|
-
throw 'update.flag 缺失';
|
|
69
|
-
}
|
|
70
|
-
swpp_backends_1.default.event.submitCacheInfo('flag', flag);
|
|
71
|
-
if (flag !== versionJson?.external?.flag) {
|
|
72
|
-
if (change)
|
|
73
|
-
swpp_backends_1.default.event.submitChange(...change);
|
|
74
|
-
if (refresh)
|
|
75
|
-
refresh.forEach(swpp_backends_1.default.event.refreshUrl);
|
|
76
|
-
if (force)
|
|
77
|
-
forceRefreshCache = true;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if ('extraListenedUrls' in rules) {
|
|
81
|
-
const urls = rules.extraListenedUrls;
|
|
82
|
-
if (!('forEach' in urls)) {
|
|
83
|
-
logger.error(`[SWPP Console] extraListenedUrls 应当附带 forEach 函数!`);
|
|
84
|
-
throw 'extraListenedUrls 类型错误';
|
|
85
|
-
}
|
|
86
|
-
urls.forEach((it) => {
|
|
87
|
-
if (typeof it !== 'string') {
|
|
88
|
-
logger.error(`[SWPP Console] extraListenedUrls 中的 ${it} 类型不为 string`);
|
|
89
|
-
throw it;
|
|
90
|
-
}
|
|
91
|
-
swpp_backends_1.default.event.submitExternalUrl(it);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
await buildVersionJson(hexo);
|
|
95
|
-
const dif = swpp_backends_1.default.builder.analyze(swpp_backends_1.default.cache.readNewVersionJson());
|
|
96
|
-
if (forceRefreshCache)
|
|
97
|
-
dif.force = true;
|
|
98
|
-
await buildUpdateJson(hexo, dif);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
85
|
+
return result;
|
|
101
86
|
}
|
|
102
87
|
async function buildUpdateJson(hexo, dif) {
|
|
103
88
|
const url = hexo.config.url;
|
|
104
|
-
const json = swpp_backends_1.default.builder.
|
|
89
|
+
const json = swpp_backends_1.default.builder.buildUpdateJson(url, dif);
|
|
105
90
|
fs.writeFileSync(`${hexo.config.public_dir}/update.json`, JSON.stringify(json), 'utf-8');
|
|
106
91
|
logger.info('成功生成:update.json');
|
|
107
92
|
}
|
|
@@ -141,14 +126,9 @@ function buildServiceWorker(hexo) {
|
|
|
141
126
|
// noinspection HtmlUnknownTarget
|
|
142
127
|
hexo.extend.injector.register('body_begin', () => `<script src="/sw-dom.js"></script>`);
|
|
143
128
|
hexo.extend.generator.register('build_dom_js', () => {
|
|
144
|
-
const onsuccess = pluginConfig.dom.onsuccess;
|
|
145
|
-
let template = fs.readFileSync(path_1.default.resolve('./', 'node_modules/swpp-backends/dist/resources/sw-dom.js'), 'utf-8');
|
|
146
|
-
// @ts-ignore
|
|
147
|
-
if (onsuccess)
|
|
148
|
-
template = template.replaceAll(`// \${onSuccess}`, `(${pluginConfig.dom.onsuccess.toString()})()`);
|
|
149
129
|
return {
|
|
150
130
|
path: 'sw-dom.js',
|
|
151
|
-
data:
|
|
131
|
+
data: swpp_backends_1.default.builder.buildDomJs()
|
|
152
132
|
};
|
|
153
133
|
});
|
|
154
134
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hexo-swpp",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"hexo-log": "^4.1.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"swpp-backends": "^
|
|
33
|
+
"swpp-backends": "^2.0.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {}
|
|
36
36
|
}
|