1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| /** * AnZhiYu * Merge CDN */
"use strict";
const { version } = require("../../package.json"); const path = require("path");
hexo.extend.filter.register("before_generate", () => { const themeConfig = hexo.theme.config; const { CDN } = themeConfig;
const thirdPartySrc = hexo.render.renderSync({ path: path.join(hexo.theme_dir, "/plugins.yml"), engine: "yaml" }); const internalSrc = { main: { name: "hexo-theme-anzhiyu", file: "js/main.js", version, }, utils: { name: "hexo-theme-anzhiyu", file: "js/utils.js", version, }, translate: { name: "hexo-theme-anzhiyu", file: "js/tw_cn.js", version, }, local_search: { name: "hexo-theme-anzhiyu", file: "js/search/local-search.js", version, }, algolia_js: { name: "hexo-theme-anzhiyu", file: "js/search/algolia.js", version, }, random_friends_post_js: { name: "hexo-theme-anzhiyu", file: "js/anzhiyu/random_friends_post.js", version, }, right_click_menu_js: { name: "hexo-theme-anzhiyu", file: "js/anzhiyu/right_click_menu.js", version, }, comment_barrage_js: { name: "hexo-theme-anzhiyu", file: "js/anzhiyu/comment_barrage.js", version, }, ai_abstract_js: { name: "hexo-theme-anzhiyu", file: "js/anzhiyu/ai_abstract.js", version, }, people_js: { name: "hexo-theme-anzhiyu", file: "js/anzhiyu/people.js", version, }, };
const minFile = file => { return file.replace(/(?<!\.min)\.(js|css)$/g, ext => ".min" + ext); };
const createCDNLink = (data, type, cond = "") => { Object.keys(data).map(key => { let { name, version, file, other_name } = data[key];
const cdnjs_name = other_name || name; const cdnjs_file = file.replace(/^[lib|dist]*\/|browser\//g, ""); const min_cdnjs_file = minFile(cdnjs_file); if (cond === "internal") file = `source/${file}`; const min_file = minFile(file); const verType = CDN.version ? `@${version}` : "";
const value = { version, name, file, cdnjs_file, min_file, min_cdnjs_file, cdnjs_name, }; const cdnSource = { local: cond === "internal" ? cdnjs_file : `/pluginsSrc/${name}/${file}`, jsdelivr: `https://cdn.jsdelivr.net/npm/${name}${verType}/${min_file}`, unpkg: `https://unpkg.com/${name}${verType}/${file}`, cdnjs: `https://cdnjs.cloudflare.com/ajax/libs/${cdnjs_name}/${version}/${min_cdnjs_file}`, elemecdn: `https://npm.elemecdn.com/${name}${verType}/${file}`, onmicrosoft: `https://npm.onmicrosoft.cn/${name}${verType}/${file}`, cbd: `https://cdn.cbd.int/${name}${verType}/${file}`, anheyu: `https://cdn.anheyu.com/npm/${name}${verType}/${min_file}`, jsdmirror: `https://cdn.jsdmirror.com/npm/${name}${verType}/${min_file}`, custom: (CDN.custom_format || "").replace(/\$\{(.+?)\}/g, (match, $1) => value[$1]), };
data[key] = cdnSource[type]; });
if (cond === "internal") data["main_css"] = "css/index.css"; return data; };
// delete null value const deleteNullValue = obj => { if (!obj) return; for (const i in obj) { obj[i] === null && delete obj[i]; } return obj; };
themeConfig.asset = Object.assign( createCDNLink(internalSrc, CDN.internal_provider, "internal"), createCDNLink(thirdPartySrc, CDN.third_party_provider), deleteNullValue(CDN.option) ); });
|