hexo-swpp 2.6.0-beta.0 → 2.6.0-beta.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.js CHANGED
@@ -22,11 +22,11 @@ function loadRules() {
22
22
  const fs = require('fs')
23
23
  const themeName = hexo.config.theme
24
24
  // 根目录下的文件
25
- const root = nodePath.resolve('./', 'sw-rules')
25
+ const root = nodePath.resolve('./', 'sw-rules.js')
26
26
  // themes 文件夹下的文件
27
- const themes = nodePath.resolve('./themes/', themeName)
27
+ const themes = nodePath.resolve('./themes/', themeName, 'sw-rules.js')
28
28
  // node_modules 文件下的文件
29
- const modules = nodePath.resolve('./node_modules/', `hexo-theme-${themeName}`)
29
+ const modules = nodePath.resolve('./node_modules/', `hexo-theme-${themeName}/sw-rules.js`)
30
30
  const exists = {
31
31
  root: fs.existsSync(root),
32
32
  themes: fs.existsSync(themes),
@@ -34,13 +34,16 @@ function loadRules() {
34
34
  }
35
35
  if (!(exists.root || exists.themes || exists.modules)) {
36
36
  const tip = "未找到 sw-rules.js 文件"
37
- logger.error(tip)
37
+ logger.error(`[sw-rules]: ${tip}`)
38
38
  throw tip
39
39
  }
40
40
  let result = {}
41
41
  if (exists.themes)
42
42
  result = require(themes)
43
- else if (exists.modules)
43
+ else if (exists.modules) {
44
44
  result = require(modules)
45
+ }
46
+ if ('afterTheme' in result)
47
+ logger.error("[sw-rules]: 主题目录下的 sw-rules.js 中不应当包含 afterTheme 函数!")
45
48
  return exists.root ? { ...result, ...require(root) } : result
46
49
  }
@@ -32,7 +32,7 @@
32
32
  self.addEventListener('activate', event => event.waitUntil(clients.claim()))
33
33
 
34
34
  // noinspection JSFileReferences
35
- const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')
35
+ const { cacheList, fetchFile, getSpareUrls, afterJoin, afterTheme } = require('../sw-rules')
36
36
 
37
37
  // 检查请求是否成功
38
38
  // noinspection JSUnusedLocalSymbols
package/lib/swBuilder.js CHANGED
@@ -8,6 +8,7 @@ module.exports = (hexo, config, pluginConfig, rules) => {
8
8
  } = rules
9
9
  const nodePath = require('path')
10
10
  const fs = require('fs')
11
+ const logger = require('hexo-log')()
11
12
 
12
13
  const root = config.url + (config.root ?? '/')
13
14
 
@@ -18,11 +19,37 @@ module.exports = (hexo, config, pluginConfig, rules) => {
18
19
  const absPath = module.path + '/sw-template.js'
19
20
  const rootPath = nodePath.resolve('./')
20
21
  const relativePath = nodePath.relative(rootPath, absPath)
22
+ /**
23
+ * 获取任意对象(symbol 类型除外)的源码
24
+ * @param any {any} 对象
25
+ * @param whiteList {string[]?} 白名单
26
+ * @return {string}
27
+ */
28
+ function getSource(any, whiteList = null) {
29
+ switch (typeof any) {
30
+ case 'undefined': return 'undefined\n'
31
+ case 'object': {
32
+ let result = ''
33
+ for (let key in any) {
34
+ if (whiteList && !whiteList.includes(key)) continue
35
+ const value = any[key]
36
+ result += `const ${key} = ${getSource(value)}\n`
37
+ }
38
+ return result + '\n'
39
+ }
40
+ case 'string': return `'${any}'\n`
41
+ case 'symbol':
42
+ logger.error("[sw builder]: 不支持写入 symbol 类型,请从 sw-rules.js 中移除相关内容!")
43
+ throw '不支持写入 symbol 类型'
44
+ case 'bigint': return `${any.toString()}n\n`
45
+ default: return any.toString() + '\n'
46
+ }
47
+ }
21
48
  // 获取拓展文件
22
- let cache = JSON.stringify(
23
- rules,
24
- (_, value) => typeof value === 'function' ? value.toString() : value
25
- )
49
+ let cache = getSource(rules, [
50
+ 'cacheList', 'modifyRequest', 'getCdnList', 'getSpareUrls', 'blockRequest',
51
+ ...('external' in rules && Array.isArray(rules.external) ? rules.external : [])
52
+ ])
26
53
  if (!fetchNoCache) {
27
54
  // noinspection JSUnresolvedVariable
28
55
  if (pluginConfig.sw.cdnRacing && getCdnList) {
@@ -103,9 +130,14 @@ module.exports = (hexo, config, pluginConfig, rules) => {
103
130
  }
104
131
  }
105
132
  if (!getSpareUrls) cache += `\nconst getSpareUrls = _ => {}`
133
+ if ('afterJoin' in rules)
134
+ cache += `(() => {${getSource(rules.afterJoin)}})()\n`
135
+ if ('afterTheme' in rules)
136
+ cache += `(() => {${getSource(rules.afterTheme)}}()\n`
137
+ const keyword = "const { cacheList, fetchFile, getSpareUrls, afterJoin, afterTheme } = require('../sw-rules')"
106
138
  // noinspection JSUnresolvedVariable
107
139
  let swContent = fs.readFileSync(relativePath, 'utf8')
108
- .replaceAll("const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')", cache)
140
+ .replaceAll(keyword, cache)
109
141
  .replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
110
142
  .replaceAll("'@$$[cacheName]'", `'${pluginConfig.sw.cacheName ?? 'kmarBlogCache'}'`)
111
143
  if (modifyRequest) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "2.6.0-beta.0",
3
+ "version": "2.6.0-beta.2",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",