hexo-swpp 3.2.0 → 3.2.2
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 +40 -1
- package/dist/index.js +40 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,9 +24,21 @@ npm install hexo-swpp swpp-backends
|
|
|
24
24
|
|
|
25
25
|
```yml
|
|
26
26
|
swpp:
|
|
27
|
+
# 是否启用插件
|
|
27
28
|
enable: true
|
|
29
|
+
# 是否在发布前自动执行脚本
|
|
30
|
+
# auto_exec: true
|
|
28
31
|
```
|
|
29
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
|
+
|
|
30
42
|
插件的具体配置见 [Swpp Backends 官方文档 | 山岳库博](https://kmar.top/posts/b70ec88f/)。
|
|
31
43
|
|
|
32
44
|
### sort
|
|
@@ -45,4 +57,31 @@ module.exports.config = {
|
|
|
45
57
|
|
|
46
58
|
该配置项是为了对 hexo 中的一些变量进行排序,避免每次生成 HTML 时由于这些变量的顺序变动导致生成结果不完全相同。上方代码给出的值为插件的缺省值,用户设置该项不会直接覆盖这些值,只有用户也声明 `posts`、`pages` 或 `tags` 时才会覆盖对应的值。
|
|
47
59
|
|
|
48
|
-
其中 key 值为要排序的变量的名称,value 为变量排序时的依据,填 `false` 表示禁用该项排序,填 `true` 表示以 value 本身为键进行排序,填字符串表示以 `value[tag]` 为键进行排序。
|
|
60
|
+
其中 key 值为要排序的变量的名称,value 为变量排序时的依据,填 `false` 表示禁用该项排序,填 `true` 表示以 value 本身为键进行排序,填字符串表示以 `value[tag]` 为键进行排序。
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 更新日志
|
|
65
|
+
|
|
66
|
+
+ 3.2+
|
|
67
|
+
|
|
68
|
+
1. 支持 `swpp-backends@2` \[3.2.0]
|
|
69
|
+
2. 优化不运行 swpp 时的性能 \[3.2.1]
|
|
70
|
+
3. 支持发布前自动执行 swpp \[3.2.1]
|
|
71
|
+
4. 修复构建时报错的**严重漏洞** \[3.2.2]
|
|
72
|
+
5. 插入的 DOM JS 加载方式改为 async \[3.2.2]
|
|
73
|
+
+ 3.1+
|
|
74
|
+
|
|
75
|
+
1. 支持 `swpp-backends@1.1` \[3.1.0]
|
|
76
|
+
2. 修复无法正确读取主题配置文件的问题 \[3.1.1]
|
|
77
|
+
+ 3.0+
|
|
78
|
+
|
|
79
|
+
该版本与 V2 不兼容,请注意修改配置文件!
|
|
80
|
+
|
|
81
|
+
相比于 V2,V3 有以下改动:
|
|
82
|
+
1. 移除了配置项中的 `json.precisionMode` 选项
|
|
83
|
+
2. 修改了配置项 `json` 中的 `merge` 和 `exclude` 的写法
|
|
84
|
+
3. 修改了配置项中的 `external.js` 的写法
|
|
85
|
+
4. 将配置项 `external` 中的 `skip` 替换为 `stable`,`replace` 替换为 `replacer`
|
|
86
|
+
5. 规则文件中的 `cacheList` 替换为 `cacheRules`,`getCdnList` 替换为 `getRaceUrls`
|
|
87
|
+
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
|
+
hexo.on('generateBefore', () => {
|
|
40
|
+
loadRules(hexo);
|
|
41
|
+
sort(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,24 +80,9 @@ 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
|
-
await Promise.all([
|
|
59
|
-
swpp_backends_1.default.loader.loadUpdateJson(url + '/update.json'),
|
|
60
|
-
swpp_backends_1.default.loader.loadVersionJson(url + '/cacheList.json')
|
|
61
|
-
]);
|
|
62
|
-
await buildVersionJson(hexo);
|
|
63
|
-
const dif = swpp_backends_1.default.builder.analyzeVersion();
|
|
64
|
-
await buildUpdateJson(hexo, dif);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
85
|
+
return result;
|
|
67
86
|
}
|
|
68
87
|
async function buildUpdateJson(hexo, dif) {
|
|
69
88
|
const url = hexo.config.url;
|
|
@@ -105,7 +124,7 @@ function buildServiceWorker(hexo) {
|
|
|
105
124
|
// 生成 sw-dom.js
|
|
106
125
|
if (pluginConfig.dom) {
|
|
107
126
|
// noinspection HtmlUnknownTarget
|
|
108
|
-
hexo.extend.injector.register('body_begin', () => `<script src="/sw-dom.js"></script>`);
|
|
127
|
+
hexo.extend.injector.register('body_begin', () => `<script async src="/sw-dom.js"></script>`);
|
|
109
128
|
hexo.extend.generator.register('build_dom_js', () => {
|
|
110
129
|
return {
|
|
111
130
|
path: 'sw-dom.js',
|