hexo-swpp 2.3.0-beta.2 → 2.3.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/lib/jsonBuilder.js +22 -6
- package/lib/sw-template.js +11 -8
- package/lib/swBuilder.js +69 -62
- package/package.json +1 -1
package/lib/jsonBuilder.js
CHANGED
|
@@ -13,7 +13,9 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
13
13
|
const root = config.url + (config.root ?? '/')
|
|
14
14
|
const domain = new URL(root).hostname
|
|
15
15
|
|
|
16
|
+
// noinspection JSUnresolvedVariable
|
|
16
17
|
hexo.extend.console.register('swpp', '生成前端更新需要的 json 文件以及相关缓存', {}, async () => {
|
|
18
|
+
// noinspection JSUnresolvedVariable
|
|
17
19
|
if (!fs.existsSync(config.public_dir))
|
|
18
20
|
return logger.info('未检测到发布目录,跳过 swpp 执行')
|
|
19
21
|
const cachePath = 'cacheList.json'
|
|
@@ -38,6 +40,7 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
38
40
|
|
|
39
41
|
/** 判断指定文件是否需要排除 */
|
|
40
42
|
const isExclude = pathname => {
|
|
43
|
+
// noinspection JSUnresolvedVariable
|
|
41
44
|
for (let reg of pluginConfig.json.exclude) {
|
|
42
45
|
if (pathname.match(new RegExp(reg, 'i'))) return true
|
|
43
46
|
}
|
|
@@ -63,10 +66,13 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
63
66
|
*/
|
|
64
67
|
const buildNewJson = path => new Promise(resolve => {
|
|
65
68
|
const result = {} // 存储新的 MD5 表
|
|
69
|
+
// noinspection JSUnresolvedVariable
|
|
66
70
|
const removeIndex = config.pretty_urls?.trailing_index
|
|
71
|
+
// noinspection JSUnresolvedVariable
|
|
67
72
|
const removeHtml = config.pretty_urls?.trailing_html
|
|
68
73
|
const taskList = [] // 拉取任务列表
|
|
69
74
|
const cache = new Set() // 已经计算过的文件
|
|
75
|
+
// noinspection JSUnresolvedVariable
|
|
70
76
|
eachAllFile(config.public_dir, path => {
|
|
71
77
|
if (!fs.existsSync(path)) return logger.error(`${path} 不存在!`)
|
|
72
78
|
let endIndex
|
|
@@ -118,10 +124,12 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
118
124
|
}
|
|
119
125
|
// 处理指定 JS
|
|
120
126
|
const handleJsContent = text => {
|
|
127
|
+
// noinspection JSUnresolvedVariable
|
|
121
128
|
if (!external.js) return
|
|
122
129
|
if (cache.has(text)) return
|
|
123
130
|
cache.add(text)
|
|
124
131
|
external.js.forEach(it => {
|
|
132
|
+
// noinspection JSUnresolvedVariable
|
|
125
133
|
const reg = new RegExp(`${it.head}(['"\`])(.*?)\\1${it.tail}`, 'mg')
|
|
126
134
|
text.match(reg)?.forEach(content => {
|
|
127
135
|
try {
|
|
@@ -173,6 +181,7 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
173
181
|
}
|
|
174
182
|
})
|
|
175
183
|
Promise.all(taskList).then(() => {
|
|
184
|
+
// noinspection JSUnresolvedVariable
|
|
176
185
|
const publicRoot = config.public_dir
|
|
177
186
|
fs.writeFileSync(nodePath.join(publicRoot, path), JSON.stringify(result), 'utf-8')
|
|
178
187
|
logger.info(`Generated: ${path}`)
|
|
@@ -240,15 +249,17 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
240
249
|
|
|
241
250
|
/** 判断指定资源是否需要合并 */
|
|
242
251
|
const isMerge = (pathname, tidied) => {
|
|
252
|
+
// noinspection JSUnresolvedVariable
|
|
243
253
|
const optional = pluginConfig.json.merge
|
|
254
|
+
const {tag_dir, archive_dir, category_dir} = config
|
|
244
255
|
if (!optional) return false
|
|
245
|
-
if (pathname.includes(`/${
|
|
256
|
+
if (pathname.includes(`/${tag_dir}/`)) {
|
|
246
257
|
if (optional.tags ?? true)
|
|
247
258
|
return tidied.tags = true
|
|
248
|
-
} else if (pathname.includes(`/${
|
|
259
|
+
} else if (pathname.includes(`/${archive_dir}/`)) {
|
|
249
260
|
if (optional.archives ?? true)
|
|
250
261
|
return tidied.archives = true
|
|
251
|
-
} else if (pathname.includes(`/${
|
|
262
|
+
} else if (pathname.includes(`/${category_dir}/`)) {
|
|
252
263
|
if (optional.categories ?? true)
|
|
253
264
|
return tidied.categories = true
|
|
254
265
|
} else if (pathname.startsWith('/page/') || pathname.length <= 1) {
|
|
@@ -325,6 +336,7 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
325
336
|
global: (oldUpdate?.global ?? 0) + (tidied.updateGlobal ? 1 : 0),
|
|
326
337
|
info: [newInfo]
|
|
327
338
|
}
|
|
339
|
+
// noinspection JSUnresolvedVariable
|
|
328
340
|
const charLimit = pluginConfig.json.charLimit ?? 1024
|
|
329
341
|
if (JSON.stringify(result).length > charLimit) {
|
|
330
342
|
return {
|
|
@@ -379,15 +391,17 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
379
391
|
const pushUpdateToInfo = (info, tidied) => {
|
|
380
392
|
const merges = [] // 要合并的更新
|
|
381
393
|
// 推送页面更新
|
|
394
|
+
// noinspection JSUnresolvedVariable
|
|
382
395
|
if (tidied.page.size > (pluginConfig.json.maxHtml ?? 15)) {
|
|
383
396
|
// 如果 html 数量超过阈值就直接清掉所有 html
|
|
384
397
|
info.change.push({flag: 'html'})
|
|
385
398
|
} else {
|
|
386
399
|
const pages = [] // 独立更新
|
|
387
400
|
tidied.page.forEach(it => pages.push(it))
|
|
388
|
-
|
|
389
|
-
if (tidied.
|
|
390
|
-
if (tidied.
|
|
401
|
+
const {tag_dir, archive_dir, category_dir} = config
|
|
402
|
+
if (tidied.tags) merges.push(tag_dir)
|
|
403
|
+
if (tidied.archives) merges.push(archive_dir)
|
|
404
|
+
if (tidied.categories) merges.push(category_dir)
|
|
391
405
|
if (tidied.index) {
|
|
392
406
|
pages.push(clipPageName(root, false))
|
|
393
407
|
merges.push('page')
|
|
@@ -426,6 +440,7 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
426
440
|
/** 标记是否更新 global 版本号 */
|
|
427
441
|
updateGlobal: expand?.global
|
|
428
442
|
}
|
|
443
|
+
// noinspection JSUnresolvedVariable
|
|
429
444
|
const mode = pluginConfig.json.precisionMode
|
|
430
445
|
for (let it of dif) {
|
|
431
446
|
const url = new URL(nodePath.join(root, it)) // 当前文件的 URL
|
|
@@ -475,6 +490,7 @@ module.exports = (hexo, config, pluginConfig, swRules) => {
|
|
|
475
490
|
for (let value of external.replace) {
|
|
476
491
|
for (let source of value.source) {
|
|
477
492
|
if (url.match(source)) {
|
|
493
|
+
// noinspection JSUnresolvedVariable
|
|
478
494
|
url = url.replace(source, value.dist)
|
|
479
495
|
}
|
|
480
496
|
}
|
package/lib/sw-template.js
CHANGED
|
@@ -129,14 +129,17 @@
|
|
|
129
129
|
})
|
|
130
130
|
}
|
|
131
131
|
return fetchFile(new Request('/update.json'), false)
|
|
132
|
-
.then(response =>
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
132
|
+
.then(response => {
|
|
133
|
+
if (checkResponse(response))
|
|
134
|
+
return response.json().then(json =>
|
|
135
|
+
parseJson(json).then(result =>
|
|
136
|
+
result.list ? deleteCache(result.list).then(list => {
|
|
137
|
+
return {list, version: result.version}
|
|
138
|
+
}) : {version: result}
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
else throw `加载 update.json 时遇到异常,状态码:${response.status}`
|
|
142
|
+
})
|
|
140
143
|
}
|
|
141
144
|
|
|
142
145
|
/**
|
package/lib/swBuilder.js
CHANGED
|
@@ -11,7 +11,9 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
11
11
|
|
|
12
12
|
const root = config.url + (config.root ?? '/')
|
|
13
13
|
|
|
14
|
+
// noinspection JSUnresolvedVariable
|
|
14
15
|
hexo.extend.generator.register('buildSw', () => {
|
|
16
|
+
// noinspection JSUnresolvedVariable
|
|
15
17
|
if (pluginConfig.sw.custom) return
|
|
16
18
|
const absPath = module.path + '/sw-template.js'
|
|
17
19
|
const rootPath = nodePath.resolve('./')
|
|
@@ -20,6 +22,7 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
20
22
|
let cache = fs.readFileSync('sw-rules.js', 'utf8')
|
|
21
23
|
.replaceAll('module.exports.', 'const ')
|
|
22
24
|
if (!fetchNoCache) {
|
|
25
|
+
// noinspection JSUnresolvedVariable
|
|
23
26
|
if (pluginConfig.sw.cdnRacing && getCdnList) {
|
|
24
27
|
cache +=`
|
|
25
28
|
const fetchFile = (request, banCache) => {
|
|
@@ -45,57 +48,60 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
45
48
|
})
|
|
46
49
|
}
|
|
47
50
|
`
|
|
48
|
-
} else
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
51
|
+
} else { // noinspection JSUnresolvedVariable
|
|
52
|
+
if (pluginConfig.sw.spareUrl && getSpareUrls) {
|
|
53
|
+
cache += `
|
|
54
|
+
const fetchFile = (request, banCache, spare = null) => {
|
|
55
|
+
const fetchArgs = {
|
|
56
|
+
cache: banCache ? 'no-store' : 'default',
|
|
57
|
+
mode: 'cors',
|
|
58
|
+
credentials: 'same-origin'
|
|
59
|
+
}
|
|
60
|
+
if (!spare) spare = getSpareUrls(request.url)
|
|
61
|
+
if (!spare) return fetch(request, fetchArgs)
|
|
62
|
+
const list = spare.list
|
|
63
|
+
const controllers = []
|
|
64
|
+
let error = 0
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const pull = () => {
|
|
67
|
+
const flag = controllers.length
|
|
68
|
+
if (flag === list.length) return
|
|
69
|
+
const plusError = () => {
|
|
70
|
+
if (++error === list.length) reject(\`请求 \${request.url} 失败\`)
|
|
71
|
+
else if (flag + 1 === controllers.length) {
|
|
72
|
+
clearTimeout(controllers[flag].id)
|
|
73
|
+
pull()
|
|
74
|
+
}
|
|
70
75
|
}
|
|
76
|
+
controllers.push({
|
|
77
|
+
ctrl: new AbortController(),
|
|
78
|
+
id: setTimeout(pull, spare.timeout)
|
|
79
|
+
})
|
|
80
|
+
fetch(new Request(list[flag], request), fetchArgs).then(response => {
|
|
81
|
+
if (checkResponse(response)) {
|
|
82
|
+
for (let i in controllers) {
|
|
83
|
+
if (i !== flag) controllers[i].ctrl.abort()
|
|
84
|
+
}
|
|
85
|
+
clearTimeout(controllers[controllers.length - 1].id)
|
|
86
|
+
resolve(response)
|
|
87
|
+
} else plusError()
|
|
88
|
+
}).catch(plusError)
|
|
71
89
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
clearTimeout(controllers[controllers.length - 1].id)
|
|
82
|
-
resolve(response)
|
|
83
|
-
} else plusError()
|
|
84
|
-
}).catch(plusError)
|
|
85
|
-
}
|
|
86
|
-
pull()
|
|
90
|
+
pull()
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
`
|
|
94
|
+
} else cache += `
|
|
95
|
+
const fetchFile = (request, banCache) => fetch(request, {
|
|
96
|
+
cache: banCache ? "no-store" : "default",
|
|
97
|
+
mode: 'cors',
|
|
98
|
+
credentials: 'same-origin'
|
|
87
99
|
})
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
} else cache += `
|
|
91
|
-
const fetchFile = (request, banCache) => fetch(request, {
|
|
92
|
-
cache: banCache ? "no-store" : "default",
|
|
93
|
-
mode: 'cors',
|
|
94
|
-
credentials: 'same-origin'
|
|
95
|
-
})
|
|
96
|
-
`
|
|
100
|
+
`
|
|
101
|
+
}
|
|
97
102
|
}
|
|
98
103
|
if (!getSpareUrls) cache += `\nconst getSpareUrls = _ => {}`
|
|
104
|
+
// noinspection JSUnresolvedVariable
|
|
99
105
|
let swContent = fs.readFileSync(relativePath, 'utf8')
|
|
100
106
|
.replaceAll("const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')", cache)
|
|
101
107
|
.replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
|
|
@@ -110,7 +116,8 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
110
116
|
}
|
|
111
117
|
if (blockRequest) {
|
|
112
118
|
swContent = swContent.replace('// [blockRequest call]', `
|
|
113
|
-
if (blockRequest(url))
|
|
119
|
+
if (blockRequest(url))
|
|
120
|
+
return event.respondWith(new Response(null, {status: 208}))
|
|
114
121
|
`)
|
|
115
122
|
}
|
|
116
123
|
return {
|
|
@@ -120,24 +127,24 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
120
127
|
})
|
|
121
128
|
|
|
122
129
|
// 生成注册 sw 的代码
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
// noinspection JSUnresolvedVariable
|
|
131
|
+
hexo.extend.injector.register('head_begin', () =>
|
|
132
|
+
`<script>
|
|
133
|
+
(() => {
|
|
134
|
+
const sw = navigator.serviceWorker
|
|
135
|
+
const error = () => ${pluginConfig.sw.onerror}
|
|
136
|
+
if (!sw?.register('${new URL(root).pathname}sw.js')?.then(() => {
|
|
137
|
+
if (!sw.controller) ${pluginConfig.sw.onsuccess}
|
|
138
|
+
})?.catch(error)) error()
|
|
139
|
+
})()
|
|
140
|
+
</script>`,
|
|
141
|
+
"default")
|
|
134
142
|
|
|
135
143
|
// 插入 sw-dom.js
|
|
136
144
|
if (!pluginConfig.dom?.custom) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
})
|
|
145
|
+
// noinspection JSUnresolvedVariable,HtmlUnknownTarget
|
|
146
|
+
hexo.extend.injector.register('body_begin', () => `<script src="/sw-dom.js"></script>`)
|
|
147
|
+
// noinspection JSUnresolvedVariable
|
|
141
148
|
hexo.extend.generator.register('buildDomJs', () => {
|
|
142
149
|
const absPath = module.path + '/sw-dom.js'
|
|
143
150
|
const rootPath = nodePath.resolve('./')
|