hexo-swpp 1.3.2 → 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 +26 -15
- 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,6 +232,7 @@ 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: {
|
|
@@ -264,7 +263,7 @@ const fetchFile = async (link, onNotFound) => {
|
|
|
264
263
|
* @param path 文件路径(相对于根目录)
|
|
265
264
|
*/
|
|
266
265
|
const getJsonFromNetwork = async path => {
|
|
267
|
-
const url = root
|
|
266
|
+
const url = nodePath.join(root, path)
|
|
268
267
|
const response = await fetchFile(
|
|
269
268
|
url,
|
|
270
269
|
() => logger.error(`拉取 ${link} 时出现 404,如果您是第一次构建请忽略这个错误`)
|
|
@@ -469,7 +468,7 @@ const tidyDiff = (dif, expand) => {
|
|
|
469
468
|
}
|
|
470
469
|
const mode = pluginConfig.precisionMode
|
|
471
470
|
for (let it of dif) {
|
|
472
|
-
const url = new URL(root
|
|
471
|
+
const url = new URL(nodePath.join(root, it)) // 当前文件的 URL
|
|
473
472
|
const cache = findCache(url) // 查询缓存
|
|
474
473
|
if (!cache) {
|
|
475
474
|
logger.error(`[buildUpdate] 指定 URL(${url.pathname}) 未查询到缓存规则!`)
|
|
@@ -515,6 +514,18 @@ function replaceRequest(url) {
|
|
|
515
514
|
return url
|
|
516
515
|
}
|
|
517
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
|
+
|
|
518
529
|
/** 对 hexo 的全局变量进行排序,以保证每次生成的结果一致 */
|
|
519
530
|
(() => {
|
|
520
531
|
const locals = hexo.locals
|