mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-09-13 06:40:20 +00:00
67 lines
2.5 KiB
JavaScript
67 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const yaml = require('js-yaml');
|
|
const { url_for } = require('hexo-util');
|
|
const { getVendors } = require('./utils');
|
|
|
|
let internal;
|
|
try {
|
|
internal = require('@next-theme/plugins');
|
|
} catch {
|
|
}
|
|
const vendorsFile = fs.readFileSync(path.join(__dirname, '../../../_vendors.yml'));
|
|
const dependencies = yaml.load(vendorsFile);
|
|
|
|
module.exports = hexo => {
|
|
const { vendors, creative_commons, pace } = hexo.theme.config;
|
|
if (typeof internal === 'function') {
|
|
internal(hexo, dependencies);
|
|
}
|
|
let { plugins = 'cdnjs' } = vendors;
|
|
if (plugins === 'local' && typeof internal === 'undefined') {
|
|
hexo.log.warn('Dependencies for `plugins: local` not found. The default CDN provider CDNJS is used instead.');
|
|
hexo.log.warn('Run `npm install @next-theme/plugins` in Hexo site root directory to install the plugin.');
|
|
plugins = 'cdnjs';
|
|
}
|
|
for (const [key, value] of Object.entries(dependencies)) {
|
|
// This script will be executed repeatedly when Hexo listens file changes
|
|
// But the variable vendors[key] only needs to be modified once
|
|
if (vendors[key] && typeof vendors[key] === 'string') {
|
|
vendors[key] = {
|
|
url: url_for.call(hexo, vendors[key])
|
|
};
|
|
continue;
|
|
}
|
|
if (key === 'creative_commons') {
|
|
value.file = `${value.dir}/${creative_commons.size}/${creative_commons.license.replace(/-/g, '_')}.svg`;
|
|
}
|
|
if (key === 'pace_css') {
|
|
value.file = `${value.dir}/${pace.color}/pace-theme-${pace.theme}.css`;
|
|
}
|
|
const { name, file } = value;
|
|
const links = getVendors({
|
|
...value,
|
|
minified: file,
|
|
local : url_for.call(hexo, `lib/${name}/${file}`),
|
|
custom : vendors.custom_cdn_url
|
|
});
|
|
vendors[key] = {
|
|
url : links[plugins] || links.cdnjs,
|
|
// Subresource Integrity only adds value for cross-origin CDN assets.
|
|
// The hashes in _vendors.yml are computed for the CDN builds; when
|
|
// self-hosting (`plugins: local`) the files are served same-origin and
|
|
// may differ byte-for-byte from those builds (e.g. a newer bundled
|
|
// version), so a hardcoded hash would make the browser block them.
|
|
// Omit integrity for local assets.
|
|
integrity: plugins === 'local' ? undefined : value.integrity
|
|
};
|
|
if (key === 'mathjax') {
|
|
vendors.mathjax_font_path = plugins === 'local'
|
|
? url_for.call(hexo, 'lib/@mathjax/mathjax-newcm-font')
|
|
: undefined;
|
|
}
|
|
}
|
|
};
|