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.
Files changed (2) hide show
  1. package/index.js +28 -16
  2. 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 path = require('path')
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 = () => path.resolve('./', 'sw-cache')
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 = path.resolve('./')
40
- const relativePath = path.relative(rootPath, absPath)
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 = path.resolve('./')
72
- const relativePath = path.relative(rootPath, absPath)
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
- if (!root.endsWith('/')) root += '/'
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 + path.substring(7, endIndex))
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, () => console.log(`拉取 ${link} 时出现 404 错误`))
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
- let publicRoot = config.public_dir || 'public/'
222
- if (!publicRoot.endsWith('/')) publicRoot += '/'
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 + path
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 + it) // 当前文件的 URL
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",