|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 |
|
3 | | -const fs = require('fs'); |
4 | | -const path = require('path'); |
5 | | -const yaml = require('js-yaml'); |
6 | | -const { url_for } = require('hexo-util'); |
7 | | -const { getVendors } = require('./utils'); |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const yaml = require("js-yaml"); |
| 6 | +const { url_for } = require("hexo-util"); |
| 7 | +const { getVendors } = require("./utils"); |
8 | 8 |
|
9 | 9 | let internal; |
10 | 10 | try { |
11 | | - internal = require('@next-theme/plugins'); |
12 | | -} catch { |
13 | | -} |
14 | | -const vendorsFile = fs.readFileSync(path.join(__dirname, '../../../_vendors.yml')); |
| 11 | + internal = require("@next-theme/plugins"); |
| 12 | +} catch {} |
| 13 | +const vendorsFile = fs.readFileSync( |
| 14 | + path.join(__dirname, "../../../_vendors.yml"), |
| 15 | +); |
15 | 16 | const dependencies = yaml.load(vendorsFile); |
16 | 17 |
|
17 | | -module.exports = hexo => { |
| 18 | +module.exports = (hexo) => { |
18 | 19 | const { vendors, creative_commons, pace } = hexo.theme.config; |
19 | | - if (typeof internal === 'function') { |
| 20 | + if (typeof internal === "function") { |
20 | 21 | internal(hexo, dependencies); |
21 | 22 | } |
22 | | - let { plugins = 'cdnjs' } = vendors; |
23 | | - if (plugins === 'local' && typeof internal === 'undefined') { |
24 | | - hexo.log.warn('Dependencies for `plugins: local` not found. The default CDN provider CDNJS is used instead.'); |
25 | | - hexo.log.warn('Run `npm install @next-theme/plugins` in Hexo site root directory to install the plugin.'); |
26 | | - plugins = 'cdnjs'; |
| 23 | + let { plugins = "cdnjs" } = vendors; |
| 24 | + if (plugins === "local" && typeof internal === "undefined") { |
| 25 | + hexo.log.warn( |
| 26 | + "Dependencies for `plugins: local` not found. The default CDN provider CDNJS is used instead.", |
| 27 | + ); |
| 28 | + hexo.log.warn( |
| 29 | + "Run `npm install @next-theme/plugins` in Hexo site root directory to install the plugin.", |
| 30 | + ); |
| 31 | + plugins = "cdnjs"; |
27 | 32 | } |
28 | 33 | for (const [key, value] of Object.entries(dependencies)) { |
29 | 34 | // This script will be executed repeatedly when Hexo listens file changes |
30 | 35 | // But the variable vendors[key] only needs to be modified once |
31 | | - if (vendors[key] && typeof vendors[key] === 'string') { |
| 36 | + if (vendors[key] && typeof vendors[key] === "string") { |
32 | 37 | vendors[key] = { |
33 | | - url: url_for.call(hexo, vendors[key]) |
| 38 | + url: url_for.call(hexo, vendors[key]), |
34 | 39 | }; |
35 | 40 | continue; |
36 | 41 | } |
37 | | - if (key === 'creative_commons') { |
38 | | - value.file = `${value.dir}/${creative_commons.size}/${creative_commons.license.replace(/-/g, '_')}.svg`; |
| 42 | + if (key === "creative_commons") { |
| 43 | + value.file = `${value.dir}/${creative_commons.size}/${creative_commons.license.replace(/-/g, "_")}.svg`; |
39 | 44 | } |
40 | | - if (key === 'pace_css') { |
| 45 | + if (key === "pace_css") { |
41 | 46 | value.file = `${value.dir}/${pace.color}/pace-theme-${pace.theme}.css`; |
42 | 47 | } |
43 | 48 | const { name, file } = value; |
44 | 49 | const links = getVendors({ |
45 | 50 | ...value, |
46 | 51 | minified: file, |
47 | | - local : url_for.call(hexo, `lib/${name}/${file}`), |
48 | | - custom : vendors.custom_cdn_url |
| 52 | + local: url_for.call(hexo, `lib/${name}/${file}`), |
| 53 | + custom: vendors.custom_cdn_url, |
49 | 54 | }); |
| 55 | + |
| 56 | + // For local plugins, use calculated integrity hash from the plugin |
| 57 | + // For CDN, use the hardcoded hash from _vendors.yml |
| 58 | + let integrityHash = value.integrity; |
| 59 | + if (plugins === "local" && typeof internal === "function") { |
| 60 | + const localHash = internal.getLocalIntegrity(`lib/${name}/${file}`); |
| 61 | + if (localHash) { |
| 62 | + integrityHash = localHash; |
| 63 | + } |
| 64 | + } |
| 65 | + |
50 | 66 | vendors[key] = { |
51 | | - url : links[plugins] || links.cdnjs, |
52 | | - integrity: value.integrity |
| 67 | + url: links[plugins] || links.cdnjs, |
| 68 | + integrity: integrityHash, |
53 | 69 | }; |
54 | 70 | } |
55 | 71 | }; |
0 commit comments