hexo-swpp 2.8.1 → 2.8.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/index.cjs ADDED
@@ -0,0 +1,53 @@
1
+ // noinspection JSUnresolvedVariable
2
+
3
+ "use strict"
4
+
5
+ const config = hexo.config
6
+ const enable = (config.swpp ?? hexo.theme.config.swpp)?.enable
7
+ const { getSource } = require('./lib/utils')
8
+
9
+ if (enable) {
10
+ const configLoader = require('./lib/configLoader')
11
+ const rules = configLoader.load(hexo)
12
+ const ejectValues = calcEjectValues(hexo, rules)
13
+ // 排序
14
+ require('./lib/sort.js')(rules.config)
15
+ // 生成 update.json
16
+ require('./lib/jsonBuilder.js')(hexo, config, rules, ejectValues.obj)
17
+ // 生成 sw.js
18
+ require('./lib/swBuilder.js')(hexo, config, rules, ejectValues.str)
19
+ }
20
+
21
+ /**
22
+ * 计算导出的键值表
23
+ * @param hexo
24
+ * @param rules
25
+ * @return {?{str: string, obj: *}}
26
+ */
27
+ function calcEjectValues(hexo, rules) {
28
+ if (!('ejectValues' in rules)) return null
29
+ const obj = rules.ejectValues(hexo, rules)
30
+ const nodeObj = {}
31
+ let result = ''
32
+ for (let key in obj) {
33
+ if (!key.match(/^[A-Za-z0-9]+$/)) {
34
+ logger.error(`[SWPP EjectValues] 变量名 [${key}] 仅允许包含英文字母和阿拉伯数字!`)
35
+ throw "变量名异常:" + key
36
+ }
37
+ const data = obj[key]
38
+ const type = typeof data.value
39
+ nodeObj[key] = data.value
40
+ switch (type) {
41
+ case 'undefined': break
42
+ case 'boolean': case 'number': case 'string': case 'bigint':
43
+ result += ` ${data.prefix} eject${key[0].toUpperCase()}${key.substring(1)} = ${getSource(data.value)}`
44
+ break
45
+ default:
46
+ logger.error(`[SWPP EjectValues] 不支持导出 ${type} 类型的数据。`)
47
+ throw `不支持的键值:key=${key}, value type=${type}`
48
+ }
49
+ }
50
+ return {
51
+ obj: nodeObj, str: result
52
+ }
53
+ }
@@ -477,11 +477,11 @@ module.exports = (hexo, hexoConfig, swRules, ejectValues) => {
477
477
  }
478
478
  // noinspection JSUnresolvedVariable
479
479
  const mode = config.json.precisionMode
480
- let loseRule = false
480
+ let loseRule = []
481
481
  for (let it of dif) {
482
482
  const url = new URL(it.match(/^(https?|\/\/)/) ? it : nodePath.join(root, it)) // 当前文件的 URL
483
483
  const cache = findCache(url) // 查询缓存
484
- if (!cache) loseRule = true
484
+ if (!cache) loseRule.push(url.href)
485
485
  if (!cache?.clean) tidied.updateGlobal = true
486
486
  if (isMerge(url.pathname, tidied)) continue
487
487
  if (it.match(/(\/|\.html)$/)) { // 判断缓存是否是 html
@@ -498,10 +498,10 @@ module.exports = (hexo, hexoConfig, swRules, ejectValues) => {
498
498
  }
499
499
  }
500
500
  }
501
- if (loseRule)
501
+ if (loseRule.length !== 0)
502
502
  logger.warn(
503
503
  '[SWPP BuildUpdate] 由于缓存规则变动,部分 URL 未查询到缓存规则,将按照 clean=false 处理。' +
504
- '如果您没有修改缓存规则,希望您能向我反馈这一 BUG'
504
+ '如果您没有修改缓存规则,希望您能向我反馈这一 BUG:\n' + loseRule.map((it, index) => `\t${index}: ${it}`).join('\n')
505
505
  )
506
506
  return tidied
507
507
  }
@@ -518,7 +518,7 @@ module.exports = (hexo, hexoConfig, swRules, ejectValues) => {
518
518
  function replaceRequest(url) {
519
519
  if (!modifyRequest) return url
520
520
  const request = new Request(url)
521
- const newRequest = modifyRequest(request)
521
+ const newRequest = modifyRequest(request, ejectValues)
522
522
  return newRequest?.url ?? url
523
523
  }
524
524
 
@@ -7,6 +7,7 @@ module.exports = (hexo, hexoConfig, rules, ejectValues) => {
7
7
  blockRequest,
8
8
  config
9
9
  } = rules
10
+ const { getSource } = require('./utils')
10
11
  const nodePath = require('path')
11
12
  const fs = require('fs')
12
13
 
package/lib/utils.cjs ADDED
@@ -0,0 +1,50 @@
1
+ const logger = require('hexo-log')()
2
+
3
+ /**
4
+ * 获取任意对象(symbol 类型除外)的源码
5
+ * @param any {any} 对象
6
+ * @param separator {string} 分隔符
7
+ * @param whiteList {string[]?} 白名单
8
+ * @param mapper {?function(string):string} 输出转换函数
9
+ * @return {string}
10
+ */
11
+ function getSource(any, separator = '\n', whiteList = null, mapper = null) {
12
+ let result
13
+ switch (typeof any) {
14
+ case 'undefined':
15
+ result = `undefined${separator}`
16
+ break
17
+ case 'object': {
18
+ result = whiteList ? '' : '{\n'
19
+ result += Object.getOwnPropertyNames(any)
20
+ .filter(key => !whiteList || whiteList.includes(key))
21
+ .map(key => {
22
+ const value = any[key]
23
+ let nextMapper = null
24
+ if (whiteList && ['cacheList', 'modifyRequest'].includes(key)) {
25
+ nextMapper = str => str.replace(/\(\s*(.*?)\s*,\s*\$eject\s*\)/g, "$1")
26
+ .replaceAll(/\$eject\.(\w+)/g, (_, match) => `eject${match[0].toUpperCase()}${match.substring(1)}`)
27
+ }
28
+ return whiteList ? `const ${key} = ${getSource(value, '', null, nextMapper)}` : `${key}: ${getSource(value, '')}`
29
+ }).join(whiteList ? '\n' : ',\n')
30
+ result += (whiteList ? '' : '\n}') + separator
31
+ break
32
+ }
33
+ case 'string':
34
+ result = `'${any}'${separator}`
35
+ break
36
+ case 'bigint':
37
+ result = `${any.toString()}n${separator}`
38
+ break
39
+ default:
40
+ result = any.toString() + separator
41
+ break
42
+ case 'symbol':
43
+ logger.error("[SWPP ServiceWorkerBuilder] 不支持写入 symbol 类型,请从 sw-rules.js 中移除相关内容!")
44
+ throw '不支持写入 symbol 类型'
45
+ }
46
+ if (mapper) result = mapper(result)
47
+ return result
48
+ }
49
+
50
+ module.exports = { getSource }
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "2.8.1",
3
+ "version": "2.8.2",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
7
- "node-fetch": "^2.0.0",
7
+ "node-fetch": "^2.6.9",
8
8
  "cheerio": "^1.0.0-rc.12",
9
- "postcss": "^8.4.20"
9
+ "postcss": "^8.4.27"
10
10
  },
11
11
  "files": [
12
- "index.js",
12
+ "index.cjs",
13
13
  "lib/sw-template.js",
14
14
  "lib/sw-dom.js",
15
- "lib/swBuilder.js",
16
- "lib/jsonBuilder.js",
17
- "lib/sort.js",
18
- "lib/configLoader.js"
15
+ "lib/swBuilder.cjs",
16
+ "lib/jsonBuilder.cjs",
17
+ "lib/sort.cjs",
18
+ "lib/configLoader.cjs",
19
+ "lib/utils.cjs"
19
20
  ],
20
21
  "keywords": [
21
22
  "hexo",
package/index.js DELETED
@@ -1,99 +0,0 @@
1
- // noinspection JSUnresolvedVariable
2
-
3
- "use strict"
4
-
5
- const config = hexo.config
6
- const enable = (config.swpp ?? hexo.theme.config.swpp)?.enable
7
-
8
- if (enable) {
9
- const configLoader = require('./lib/configLoader')
10
- const rules = configLoader.load(hexo)
11
- const ejectValues = calcEjectValues(hexo, rules)
12
- // 排序
13
- require('./lib/sort.js')(rules.config)
14
- // 生成 update.json
15
- require('./lib/jsonBuilder.js')(hexo, config, rules, ejectValues.obj)
16
- // 生成 sw.js
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
99
- }
File without changes
File without changes