hexo-swpp 2.8.0 → 2.8.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/index.js +84 -2
- package/lib/jsonBuilder.js +3 -3
- package/lib/swBuilder.js +2 -61
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,10 +8,92 @@ const enable = (config.swpp ?? hexo.theme.config.swpp)?.enable
|
|
|
8
8
|
if (enable) {
|
|
9
9
|
const configLoader = require('./lib/configLoader')
|
|
10
10
|
const rules = configLoader.load(hexo)
|
|
11
|
+
const ejectValues = calcEjectValues(hexo, rules)
|
|
11
12
|
// 排序
|
|
12
13
|
require('./lib/sort.js')(rules.config)
|
|
13
14
|
// 生成 update.json
|
|
14
|
-
require('./lib/jsonBuilder.js')(hexo, config, rules)
|
|
15
|
+
require('./lib/jsonBuilder.js')(hexo, config, rules, ejectValues.obj)
|
|
15
16
|
// 生成 sw.js
|
|
16
|
-
require('./lib/swBuilder.js')(hexo, config, rules)
|
|
17
|
+
require('./lib/swBuilder.js')(hexo, config, rules, ejectValues.str)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 计算导出的键值表
|
|
22
|
+
* @param hexo
|
|
23
|
+
* @param rules
|
|
24
|
+
* @return {?{str: string, obj: *}}
|
|
25
|
+
*/
|
|
26
|
+
function calcEjectValues(hexo, rules) {
|
|
27
|
+
if (!('ejectValues' in rules)) return null
|
|
28
|
+
const obj = rules.ejectValues(hexo, rules)
|
|
29
|
+
const nodeObj = {}
|
|
30
|
+
let result = ''
|
|
31
|
+
for (let key in obj) {
|
|
32
|
+
if (!key.match(/^[A-Za-z0-9]+$/)) {
|
|
33
|
+
logger.error(`[SWPP EjectValues] 变量名 [${key}] 仅允许包含英文字母和阿拉伯数字!`)
|
|
34
|
+
throw "变量名异常:" + key
|
|
35
|
+
}
|
|
36
|
+
const data = obj[key]
|
|
37
|
+
const type = typeof data.value
|
|
38
|
+
nodeObj[key] = data.value
|
|
39
|
+
switch (type) {
|
|
40
|
+
case 'undefined': break
|
|
41
|
+
case 'boolean': case 'number': case 'string': case 'bigint':
|
|
42
|
+
result += ` ${data.prefix} eject${key[0].toUpperCase()}${key.substring(1)} = ${getSource(data.value)}`
|
|
43
|
+
break
|
|
44
|
+
default:
|
|
45
|
+
logger.error(`[SWPP EjectValues] 不支持导出 ${type} 类型的数据。`)
|
|
46
|
+
throw `不支持的键值:key=${key}, value type=${type}`
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
obj: nodeObj, str: result
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获取任意对象(symbol 类型除外)的源码
|
|
56
|
+
* @param any {any} 对象
|
|
57
|
+
* @param separator {string} 分隔符
|
|
58
|
+
* @param whiteList {string[]?} 白名单
|
|
59
|
+
* @param mapper {?function(string):string} 输出转换函数
|
|
60
|
+
* @return {string}
|
|
61
|
+
*/
|
|
62
|
+
function getSource(any, separator = '\n', whiteList = null, mapper = null) {
|
|
63
|
+
let result
|
|
64
|
+
switch (typeof any) {
|
|
65
|
+
case 'undefined':
|
|
66
|
+
result = `undefined${separator}`
|
|
67
|
+
break
|
|
68
|
+
case 'object': {
|
|
69
|
+
result = whiteList ? '' : '{\n'
|
|
70
|
+
result += Object.getOwnPropertyNames(any)
|
|
71
|
+
.filter(key => !whiteList || whiteList.includes(key))
|
|
72
|
+
.map(key => {
|
|
73
|
+
const value = any[key]
|
|
74
|
+
let nextMapper = null
|
|
75
|
+
if (whiteList && key === 'cacheList') {
|
|
76
|
+
nextMapper = str => str.replace(/\(\s*(.*?)\s*,\s*\$eject\s*\)/g, "$1")
|
|
77
|
+
.replaceAll(/\$eject\.(\w+)/g, (_, match) => `eject${match[0].toUpperCase()}${match.substring(1)}`)
|
|
78
|
+
}
|
|
79
|
+
return whiteList ? `const ${key} = ${getSource(value, '', null, nextMapper)}` : `${key}: ${getSource(value, '')}`
|
|
80
|
+
}).join(whiteList ? '\n' : ',\n')
|
|
81
|
+
result += (whiteList ? '' : '\n}') + separator
|
|
82
|
+
break
|
|
83
|
+
}
|
|
84
|
+
case 'string':
|
|
85
|
+
result = `'${any}'${separator}`
|
|
86
|
+
break
|
|
87
|
+
case 'bigint':
|
|
88
|
+
result = `${any.toString()}n${separator}`
|
|
89
|
+
break
|
|
90
|
+
default:
|
|
91
|
+
result = any.toString() + separator
|
|
92
|
+
break
|
|
93
|
+
case 'symbol':
|
|
94
|
+
logger.error("[SWPP ServiceWorkerBuilder] 不支持写入 symbol 类型,请从 sw-rules.js 中移除相关内容!")
|
|
95
|
+
throw '不支持写入 symbol 类型'
|
|
96
|
+
}
|
|
97
|
+
if (mapper) result = mapper(result)
|
|
98
|
+
return result
|
|
17
99
|
}
|
package/lib/jsonBuilder.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
module.exports = (hexo, hexoConfig, swRules) => {
|
|
1
|
+
module.exports = (hexo, hexoConfig, swRules, ejectValues) => {
|
|
2
2
|
const logger = require('hexo-log')()
|
|
3
3
|
const fs = require('fs')
|
|
4
4
|
const fetch = require('node-fetch')
|
|
@@ -271,7 +271,7 @@ module.exports = (hexo, hexoConfig, swRules) => {
|
|
|
271
271
|
const result = []
|
|
272
272
|
if (!oldCache) return result
|
|
273
273
|
const refresh = oldCache.version !== newCache.version
|
|
274
|
-
if (refresh) return
|
|
274
|
+
if (refresh) return undefined
|
|
275
275
|
if (oldCache.version) oldCache = oldCache.list
|
|
276
276
|
for (let path in oldCache) {
|
|
277
277
|
if (newCache.list[path] !== oldCache[path]) result.push(path)
|
|
@@ -510,7 +510,7 @@ module.exports = (hexo, hexoConfig, swRules) => {
|
|
|
510
510
|
url = new URL(replaceRequest(url.href))
|
|
511
511
|
for (let key in cacheList) {
|
|
512
512
|
const value = cacheList[key]
|
|
513
|
-
if (value.match(url)) return value
|
|
513
|
+
if (value.match(url, ejectValues)) return value
|
|
514
514
|
}
|
|
515
515
|
return null
|
|
516
516
|
}
|
package/lib/swBuilder.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module.exports = (hexo, hexoConfig, rules) => {
|
|
1
|
+
module.exports = (hexo, hexoConfig, rules, ejectValues) => {
|
|
4
2
|
const {
|
|
5
3
|
modifyRequest,
|
|
6
4
|
fetchNoCache,
|
|
@@ -113,7 +111,7 @@ module.exports = (hexo, hexoConfig, rules) => {
|
|
|
113
111
|
const keyword = "const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')"
|
|
114
112
|
// noinspection JSUnresolvedVariable
|
|
115
113
|
let swContent = fs.readFileSync(relativePath, 'utf8')
|
|
116
|
-
.replaceAll("// [insertion site] values",
|
|
114
|
+
.replaceAll("// [insertion site] values", ejectValues ?? '')
|
|
117
115
|
.replaceAll(keyword, cache)
|
|
118
116
|
.replaceAll("'@$$[escape]'", (serviceWorkerConfig.escape).toString())
|
|
119
117
|
.replaceAll("'@$$[cacheName]'", `'${serviceWorkerConfig.cacheName}'`)
|
|
@@ -174,61 +172,4 @@ module.exports = (hexo, hexoConfig, rules) => {
|
|
|
174
172
|
}
|
|
175
173
|
})
|
|
176
174
|
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 计算导出的键值表
|
|
181
|
-
* @return {?string}
|
|
182
|
-
*/
|
|
183
|
-
function calcEjectValues(hexo, rules) {
|
|
184
|
-
if (!('ejectValues' in rules)) return null
|
|
185
|
-
const obj = rules.ejectValues(hexo, rules)
|
|
186
|
-
let result = ''
|
|
187
|
-
for (let key in obj) {
|
|
188
|
-
if (!key.startsWith('eject')) {
|
|
189
|
-
logger.error(`[SWPP EjectValues] 变量名 [${key}] 应当以“eject”开头!`)
|
|
190
|
-
throw `变量名 [${key}] 不符合规范`
|
|
191
|
-
}
|
|
192
|
-
const data = obj[key]
|
|
193
|
-
const type = typeof data.value
|
|
194
|
-
switch (type) {
|
|
195
|
-
case 'undefined': break
|
|
196
|
-
case 'boolean': case 'number': case 'string': case 'bigint':
|
|
197
|
-
result += ` ${data.prefix} ${key} = ${getSource(data.value)}`
|
|
198
|
-
break
|
|
199
|
-
default:
|
|
200
|
-
logger.error(`[SWPP EjectValues] 不支持导出 ${type} 类型的数据。`)
|
|
201
|
-
throw `不支持的键值:key=${key}, value type=${type}`
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return result
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* 获取任意对象(symbol 类型除外)的源码
|
|
209
|
-
* @param any {any} 对象
|
|
210
|
-
* @param separator {string} 分隔符
|
|
211
|
-
* @param whiteList {string[]?} 白名单
|
|
212
|
-
* @return {string}
|
|
213
|
-
*/
|
|
214
|
-
function getSource(any, separator = '\n', whiteList = null) {
|
|
215
|
-
switch (typeof any) {
|
|
216
|
-
case 'undefined': return `undefined${separator}`
|
|
217
|
-
case 'object': {
|
|
218
|
-
let result = whiteList ? '' : '{\n'
|
|
219
|
-
result += Object.getOwnPropertyNames(any)
|
|
220
|
-
.filter(key => !whiteList || whiteList.includes(key))
|
|
221
|
-
.map(key => {
|
|
222
|
-
const value = any[key]
|
|
223
|
-
return whiteList ? `const ${key} = ${getSource(value, '')}` : `${key}: ${getSource(value, '')}`
|
|
224
|
-
}).join(whiteList ? '\n' : ',\n')
|
|
225
|
-
return result + (whiteList ? '' : '\n}') + separator
|
|
226
|
-
}
|
|
227
|
-
case 'string': return `'${any}'${separator}`
|
|
228
|
-
case 'bigint': return `${any.toString()}n${separator}`
|
|
229
|
-
default: return any.toString() + separator
|
|
230
|
-
case 'symbol':
|
|
231
|
-
logger.error("[SWPP ServiceWorkerBuilder] 不支持写入 symbol 类型,请从 sw-rules.js 中移除相关内容!")
|
|
232
|
-
throw '不支持写入 symbol 类型'
|
|
233
|
-
}
|
|
234
175
|
}
|