hexo-swpp 1.3.1 → 1.3.3
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 +28 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
const fs = require('fs')
|
|
6
6
|
const logger = require('hexo-log')()
|
|
7
7
|
const fetch = require('node-fetch')
|
|
8
|
-
const
|
|
8
|
+
const nodePath = require('path')
|
|
9
9
|
const crypto = require("crypto")
|
|
10
10
|
const cheerio = require('cheerio')
|
|
11
11
|
const postcss = require('postcss')
|
|
12
12
|
|
|
13
|
-
const findScript = () =>
|
|
13
|
+
const findScript = () => nodePath.resolve('./', 'sw-cache')
|
|
14
14
|
|
|
15
15
|
const config = hexo.config
|
|
16
16
|
const pluginConfig = config.swpp || hexo.theme.config
|
|
@@ -36,8 +36,8 @@ if (pluginConfig?.enable) {
|
|
|
36
36
|
hexo.extend.generator.register('buildSw', () => {
|
|
37
37
|
if (pluginConfig.sw.custom) return
|
|
38
38
|
const absPath = module.path + '/sw-template.js'
|
|
39
|
-
const rootPath =
|
|
40
|
-
const relativePath =
|
|
39
|
+
const rootPath = nodePath.resolve('./')
|
|
40
|
+
const relativePath = nodePath.relative(rootPath, absPath)
|
|
41
41
|
const template = fs.readFileSync(relativePath, 'utf8')
|
|
42
42
|
const cache = fs.readFileSync('sw-cache.js', 'utf8')
|
|
43
43
|
.replaceAll('module.exports.cacheList', 'const cacheList')
|
|
@@ -68,8 +68,8 @@ if (pluginConfig?.enable) {
|
|
|
68
68
|
})
|
|
69
69
|
hexo.extend.generator.register('buildDomJs', () => {
|
|
70
70
|
const absPath = module.path + '/sw-dom.js'
|
|
71
|
-
const rootPath =
|
|
72
|
-
const relativePath =
|
|
71
|
+
const rootPath = nodePath.resolve('./')
|
|
72
|
+
const relativePath = nodePath.relative(rootPath, absPath)
|
|
73
73
|
const template = fs.readFileSync(relativePath, 'utf-8')
|
|
74
74
|
.replaceAll('// ${onSuccess}', pluginConfig.dom.onsuccess)
|
|
75
75
|
return {
|
|
@@ -86,8 +86,7 @@ const eachAllFile = (root, cb) => {
|
|
|
86
86
|
if (stats.isFile()) cb(root)
|
|
87
87
|
else {
|
|
88
88
|
const files = fs.readdirSync(root)
|
|
89
|
-
|
|
90
|
-
files.forEach(it => eachAllFile(root + it, cb))
|
|
89
|
+
files.forEach(it => eachAllFile(nodePath.join(root, it), cb))
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
92
|
|
|
@@ -119,7 +118,7 @@ const buildNewJson = path => new Promise(resolve => {
|
|
|
119
118
|
if (removeIndex && path.endsWith('/index.html')) endIndex = path.length - 10
|
|
120
119
|
else if (removeHtml && path.endsWith('.html')) endIndex = path.length - 5
|
|
121
120
|
else endIndex = path.length
|
|
122
|
-
const url = new URL(root
|
|
121
|
+
const url = new URL(nodePath.join(root, path.substring(7, endIndex)))
|
|
123
122
|
if (isExclude(url.href)) return
|
|
124
123
|
let content = null
|
|
125
124
|
if (findCache(url)) {
|
|
@@ -151,7 +150,7 @@ const buildNewJson = path => new Promise(resolve => {
|
|
|
151
150
|
const url = new URL(link.startsWith('/') ? `http:${link}` : link)
|
|
152
151
|
if (url.hostname === domain || !findCache(url) || isExclude(url.href)) return
|
|
153
152
|
taskList.push(
|
|
154
|
-
fetchFile(link, () =>
|
|
153
|
+
fetchFile(link, () => logger.log(`拉取 ${link} 时出现 404 错误`))
|
|
155
154
|
.then(response => response.text())
|
|
156
155
|
.then(text => {
|
|
157
156
|
const key = decodeURIComponent(link)
|
|
@@ -218,9 +217,8 @@ const buildNewJson = path => new Promise(resolve => {
|
|
|
218
217
|
}
|
|
219
218
|
})
|
|
220
219
|
Promise.all(taskList).then(() => {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
fs.writeFileSync(`${publicRoot}${path}`, JSON.stringify(result), 'utf-8')
|
|
220
|
+
const publicRoot = config.public_dir || 'public/'
|
|
221
|
+
fs.writeFileSync(nodePath.join(publicRoot, path), JSON.stringify(result), 'utf-8')
|
|
224
222
|
logger.info(`Generated: ${path}`)
|
|
225
223
|
resolve(result)
|
|
226
224
|
})
|
|
@@ -234,12 +232,14 @@ const buildNewJson = path => new Promise(resolve => {
|
|
|
234
232
|
*/
|
|
235
233
|
const fetchFile = async (link, onNotFound) => {
|
|
236
234
|
try {
|
|
235
|
+
link = replaceDevRequest(link)
|
|
237
236
|
// noinspection SpellCheckingInspection
|
|
238
237
|
const response = await fetch(link, {
|
|
239
238
|
headers: {
|
|
240
239
|
referer: new URL(link).hostname,
|
|
241
240
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.62'
|
|
242
|
-
}
|
|
241
|
+
},
|
|
242
|
+
timeout: 1500
|
|
243
243
|
})
|
|
244
244
|
switch (response.status) {
|
|
245
245
|
case 200: case 301: case 302:
|
|
@@ -263,7 +263,7 @@ const fetchFile = async (link, onNotFound) => {
|
|
|
263
263
|
* @param path 文件路径(相对于根目录)
|
|
264
264
|
*/
|
|
265
265
|
const getJsonFromNetwork = async path => {
|
|
266
|
-
const url = root
|
|
266
|
+
const url = nodePath.join(root, path)
|
|
267
267
|
const response = await fetchFile(
|
|
268
268
|
url,
|
|
269
269
|
() => logger.error(`拉取 ${link} 时出现 404,如果您是第一次构建请忽略这个错误`)
|
|
@@ -468,7 +468,7 @@ const tidyDiff = (dif, expand) => {
|
|
|
468
468
|
}
|
|
469
469
|
const mode = pluginConfig.precisionMode
|
|
470
470
|
for (let it of dif) {
|
|
471
|
-
const url = new URL(root
|
|
471
|
+
const url = new URL(nodePath.join(root, it)) // 当前文件的 URL
|
|
472
472
|
const cache = findCache(url) // 查询缓存
|
|
473
473
|
if (!cache) {
|
|
474
474
|
logger.error(`[buildUpdate] 指定 URL(${url.pathname}) 未查询到缓存规则!`)
|
|
@@ -514,6 +514,18 @@ function replaceRequest(url) {
|
|
|
514
514
|
return url
|
|
515
515
|
}
|
|
516
516
|
|
|
517
|
+
function replaceDevRequest(url) {
|
|
518
|
+
if (!pluginConfig.devReplace) return url
|
|
519
|
+
for (let value of pluginConfig.devReplace) {
|
|
520
|
+
for (let source of value.source) {
|
|
521
|
+
if (url.match(source)) {
|
|
522
|
+
url = url.replace(source, value.dist)
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return url
|
|
527
|
+
}
|
|
528
|
+
|
|
517
529
|
/** 对 hexo 的全局变量进行排序,以保证每次生成的结果一致 */
|
|
518
530
|
(() => {
|
|
519
531
|
const locals = hexo.locals
|