hexo-swpp 3.0.0 → 3.1.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.
Files changed (3) hide show
  1. package/README.md +69 -2
  2. package/dist/index.js +23 -6
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,3 +1,70 @@
1
-   该插件用于在 hexo 中自动构建可用的 ServiceWorker,插件内自带了一个缺省的 sw.js,也支持使用自定义的 sw。
1
+ 这是 `swpp-backends` 的 hexo 端实现,绝大多数功能由 [swpp-backends](https://github.com/EmptyDreams/swpp-backends) 提供。
2
2
 
3
-   三代版本仍在开发中,有兴趣的可以先看一看 v2 分支。
3
+ ## 安装
4
+
5
+ 使用时需要同时安装 `hexo-swpp` 和 `swpp-backends`:
6
+
7
+ ```bash
8
+ npm install hexo-swpp swpp-backends
9
+ ```
10
+
11
+ 当 `swpp-backends` 存在版本更新时,可以直接更新 `swpp-backends` 版本,不需要更新 `hexo-swpp` 的版本。(不过 `hexo-swpp` 有更新的话最好也跟进一下。)
12
+
13
+ ## 使用
14
+
15
+ 在 hexo 或主题的配置文件中添加如下内容即可启用插件:
16
+
17
+ ```yml
18
+ swpp:
19
+ enable: true
20
+ ```
21
+
22
+ 插件的具体配置见 [Swpp Backends 官方文档 | 山岳库博](https://kmar.top/posts/b70ec88f/)。
23
+
24
+ ### sort
25
+
26
+ `hexo-swpp` 在规则文件中添加了一个配置项——`sort`,用法如下:
27
+
28
+ ```javascript
29
+ module.exports.config = {
30
+ sort: {
31
+ posts: 'title',
32
+ pages: 'title',
33
+ tags: 'name'
34
+ }
35
+ }
36
+ ```
37
+
38
+ 该配置项是为了对 hexo 中的一些变量进行排序,避免每次生成 HTML 时由于这些变量的顺序变动导致生成结果不完全相同。上方代码给出的值为插件的缺省值,用户设置该项不会直接覆盖这些值,只有用户也声明 `posts`、`pages` 或 `tags` 时才会覆盖对应的值。
39
+
40
+ 其中 key 值为要排序的变量的名称,value 为变量排序时的依据,填 `false` 表示禁用该项排序,填 `true` 表示以 value 本身为键进行排序,填字符串表示以 `value[tag]` 为键进行排序。
41
+
42
+ ### update
43
+
44
+ `hexo-swpp` 允许用户通过 `update` 项向插件手动提交更新,按照如下格式填写:
45
+
46
+ ```javascript
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` 类型
61
+
62
+ ### extraListenedUrls
63
+
64
+ `hexo-swpp` 支持用户通过 `extraListenedUrls` 项向插件添加需要监听的 URL,按照如下格式填写:
65
+
66
+ ```javascript
67
+ module.exports.extraListenedUrls = []
68
+ ```
69
+
70
+ `extraListenedUrls` 的值不一定要是数组,只要是包含 `forEach` 函数的对象就可以,元素类型必须是 `string`。
package/dist/index.js CHANGED
@@ -33,7 +33,7 @@ const logger = require('hexo-log').default();
33
33
  // noinspection JSUnusedGlobalSymbols
34
34
  function start(hexo) {
35
35
  const { config } = hexo;
36
- if (!(config['swpp']?.enable || hexo.theme.config['swpp']?.enable))
36
+ if (!(config['swpp']?.enable || config.theme_config['swpp']?.enable))
37
37
  return;
38
38
  const themeName = config.theme;
39
39
  swpp_backends_1.default.event.addRulesMapEvent(rules => {
@@ -55,8 +55,10 @@ function start(hexo) {
55
55
  if (!fs.existsSync(hexo.config.public_dir))
56
56
  return logger.warn(`[SWPP] 未检测到发布目录,跳过指令执行`);
57
57
  const url = hexo.config.url;
58
- await swpp_backends_1.default.loader.loadUpdateJson(url + '/update.json');
59
- const versionJson = await swpp_backends_1.default.loader.loadVersionJson(url + '/cacheList.json');
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]);
60
62
  let forceRefreshCache = false;
61
63
  if ('update' in rules) {
62
64
  const update = rules.update;
@@ -75,6 +77,20 @@ function start(hexo) {
75
77
  forceRefreshCache = true;
76
78
  }
77
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
+ }
78
94
  await buildVersionJson(hexo);
79
95
  const dif = swpp_backends_1.default.builder.analyze(swpp_backends_1.default.cache.readNewVersionJson());
80
96
  if (forceRefreshCache)
@@ -87,21 +103,22 @@ async function buildUpdateJson(hexo, dif) {
87
103
  const url = hexo.config.url;
88
104
  const json = swpp_backends_1.default.builder.buildNewInfo(url, dif);
89
105
  fs.writeFileSync(`${hexo.config.public_dir}/update.json`, JSON.stringify(json), 'utf-8');
106
+ logger.info('成功生成:update.json');
90
107
  }
91
108
  async function buildVersionJson(hexo) {
92
109
  const url = hexo.config.url;
93
110
  let protocol, domain;
94
111
  if (url.startsWith('https:')) {
95
112
  protocol = 'https://';
96
- domain = url.substring(protocol.length);
97
113
  }
98
114
  else {
99
115
  protocol = 'http://';
100
- domain = url.substring(protocol.length);
101
116
  }
117
+ domain = url.substring(protocol.length, url.endsWith('/') ? url.length - 1 : url.length);
102
118
  // @ts-ignore
103
119
  const json = await swpp_backends_1.default.builder.buildVersionJson(protocol, domain, path_1.default.resolve('./', hexo.config.public_dir));
104
120
  fs.writeFileSync(`${hexo.config.public_dir}/cacheList.json`, JSON.stringify(json), 'utf-8');
121
+ logger.info('成功生成:cacheList.json');
105
122
  }
106
123
  function buildServiceWorker(hexo) {
107
124
  const rules = swpp_backends_1.default.cache.readRules();
@@ -166,7 +183,7 @@ function sort(hexo) {
166
183
  tags: 'name'
167
184
  };
168
185
  // @ts-ignore
169
- Object.assign(list, swpp_backends_1.default.cache.readRules().config['sort']);
186
+ Object.assign(list, swpp_backends_1.default.cache.readRules().config['sort'] ?? {});
170
187
  const getter = Locals.get;
171
188
  Locals.get = function (name) {
172
189
  const result = getter.call(this, name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "3.0.0",
3
+ "version": "3.1.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": "^1.0.0"
33
+ "swpp-backends": "^1.1.1"
34
34
  },
35
35
  "scripts": {}
36
36
  }