2016-05-26 11:09:41 +00:00
|
|
|
/* global hexo */
|
|
|
|
'use strict';
|
|
|
|
var CleanCSS = require('clean-css'),
|
|
|
|
UglifyJS = require('uglify-js'),
|
|
|
|
Htmlminifier = require('html-minifier').minify,
|
|
|
|
streamToArray = require('stream-to-array');
|
|
|
|
var Promise = require('bluebird');
|
|
|
|
var minimatch = require('minimatch');
|
2018-09-30 07:30:32 +00:00
|
|
|
var zlib = require('zlib');
|
|
|
|
var br = require('iltorb');
|
2016-05-26 11:09:41 +00:00
|
|
|
|
|
|
|
function logic_html(str, data) {
|
|
|
|
var hexo = this,
|
2016-05-26 11:46:01 +00:00
|
|
|
options = hexo.config.neat_html;
|
2016-05-26 11:09:41 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (false === options.enable) return;
|
|
|
|
|
|
|
|
var path = data.path;
|
|
|
|
var exclude = options.exclude;
|
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
|
|
|
|
|
|
|
|
if (path && exclude && exclude.length) {
|
|
|
|
for (var i = 0, len = exclude.length; i < len; i++) {
|
2018-06-29 07:49:13 +00:00
|
|
|
if (minimatch(path, exclude[i], {matchBase: true})) return str;
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = Htmlminifier(str, options);
|
|
|
|
var saved = ((str.length - result.length) / str.length * 100).toFixed(2);
|
2018-06-29 08:08:47 +00:00
|
|
|
if (options.logger) {
|
|
|
|
var log = hexo.log || console.log;
|
2018-09-28 07:43:54 +00:00
|
|
|
log.log('Minify the html: %s [%s saved]', path, saved + '%');
|
2018-06-29 08:08:47 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
return result;
|
2018-09-29 05:38:45 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
|
|
|
function logic_css(str, data) {
|
|
|
|
var hexo = this,
|
2016-06-22 11:15:47 +00:00
|
|
|
options = hexo.config.neat_css;
|
2016-05-26 11:09:41 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (false === options.enable) return;
|
|
|
|
|
|
|
|
var path = data.path;
|
|
|
|
var exclude = options.exclude;
|
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
|
|
|
|
|
|
|
|
if (path && exclude && exclude.length) {
|
|
|
|
for (var i = 0, len = exclude.length; i < len; i++) {
|
2018-06-29 07:49:13 +00:00
|
|
|
if (minimatch(path, exclude[i], {matchBase: true})) return str;
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-22 11:15:47 +00:00
|
|
|
|
2018-06-29 08:08:47 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
new CleanCSS(options).minify(str, function (err, result) {
|
2016-05-26 11:09:41 +00:00
|
|
|
if (err) return reject(err);
|
|
|
|
var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2);
|
2018-09-27 05:27:16 +00:00
|
|
|
resolve(result.styles);
|
2018-06-29 08:08:47 +00:00
|
|
|
if (options.logger) {
|
|
|
|
var log = hexo.log || console.log;
|
2018-09-28 07:43:54 +00:00
|
|
|
log.log('Minify the css: %s [%s saved]', path, saved + '%');
|
2018-06-29 08:08:47 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function logic_js(str, data) {
|
|
|
|
var hexo = this,
|
2016-05-26 11:46:01 +00:00
|
|
|
options = hexo.config.neat_js;
|
2016-05-26 11:09:41 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (false === options.enable) return;
|
|
|
|
|
|
|
|
var path = data.path;
|
|
|
|
var exclude = options.exclude;
|
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
|
|
|
|
|
|
|
|
if (path && exclude && exclude.length) {
|
|
|
|
for (var i = 0, len = exclude.length; i < len; i++) {
|
2018-06-29 07:49:13 +00:00
|
|
|
if (minimatch(path, exclude[i], {matchBase: true})) return str;
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 05:38:45 +00:00
|
|
|
//uglifyjs doesn't like unsupported options
|
2018-09-27 08:43:19 +00:00
|
|
|
delete options.enable;
|
|
|
|
delete options.exclude;
|
|
|
|
var js_logger = options.logger;
|
|
|
|
delete options.logger;
|
|
|
|
|
2016-05-26 11:09:41 +00:00
|
|
|
var result = UglifyJS.minify(str, options);
|
|
|
|
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
|
2018-09-27 08:43:19 +00:00
|
|
|
if (js_logger) {
|
2018-06-29 08:08:47 +00:00
|
|
|
var log = hexo.log || console.log;
|
2018-09-28 07:43:54 +00:00
|
|
|
log.log('Minify the js: %s [%s saved]', path, saved + '%');
|
2018-06-29 08:08:47 +00:00
|
|
|
}
|
2018-09-27 05:27:16 +00:00
|
|
|
return result.code;
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 07:43:54 +00:00
|
|
|
function logic_gzip() {
|
|
|
|
var hexo = this,
|
2018-09-29 05:38:45 +00:00
|
|
|
options = hexo.config.neat_gzip;
|
2018-09-28 07:43:54 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (false === options.enable) return;
|
|
|
|
|
2018-10-05 02:35:07 +00:00
|
|
|
var gz_logger = options.logger;
|
|
|
|
const route = hexo.route;
|
|
|
|
const routeList = route.list();
|
|
|
|
|
|
|
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
|
|
|
const assetPath = route.get(path);
|
|
|
|
let assetTxt = '';
|
|
|
|
// Extract the content
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk));
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
// gzip compress using highest level
|
|
|
|
zlib.gzip(assetTxt, {level:zlib.Z_BEST_COMPRESSION}, (err, buffer) => {
|
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .gz
|
|
|
|
route.set(path + '.gz', buffer);
|
|
|
|
//Logging
|
|
|
|
var saved = ((assetTxt.length - buffer.toString().length) / assetTxt.length * 100).toFixed(2);
|
|
|
|
if (gz_logger) {
|
|
|
|
var log = hexo.log || console.log;
|
|
|
|
log.log('Gzip-compressed %s [%s saved]', path, saved + '%');
|
|
|
|
}
|
|
|
|
resolve(assetTxt);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
2018-10-05 02:35:07 +00:00
|
|
|
});
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
2018-10-05 02:35:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function logic_brotli() {
|
|
|
|
var hexo = this,
|
|
|
|
options = hexo.config.neat_brotli;
|
|
|
|
// Return if disabled.
|
|
|
|
if (false === options.enable) return;
|
|
|
|
|
2018-10-05 02:35:07 +00:00
|
|
|
var br_logger = options.logger;
|
|
|
|
const route = hexo.route;
|
|
|
|
const routeList = route.list();
|
|
|
|
|
|
|
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
|
|
|
const assetPath = route.get(path);
|
|
|
|
let assetTxt = '';
|
|
|
|
// Extract the content
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk));
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
// Input has to be buffer for brotli
|
|
|
|
var buffer = new Buffer.from(assetTxt, "utf-8");
|
|
|
|
// brotli compress using highest level
|
|
|
|
br.compress(buffer, {quality:br.BROTLI_MAX_QUALITY}, (err, output) => {
|
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .br
|
|
|
|
route.set(path + '.br', output);
|
|
|
|
//Logging
|
|
|
|
var saved = ((buffer.length - output.toString().length) / buffer.length * 100).toFixed(2);
|
|
|
|
if (br_logger) {
|
|
|
|
var log = hexo.log || console.log;
|
|
|
|
log.log('Brotli-compressed %s [%s saved]', path, saved + '%');
|
|
|
|
}
|
|
|
|
resolve(assetTxt);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
2018-09-29 05:38:45 +00:00
|
|
|
}
|
2018-10-05 02:35:07 +00:00
|
|
|
});
|
2018-09-28 07:43:54 +00:00
|
|
|
}
|
2018-10-05 02:35:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
2018-09-28 07:43:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 11:09:41 +00:00
|
|
|
module.exports = {
|
|
|
|
logic_html: logic_html,
|
|
|
|
logic_css: logic_css,
|
|
|
|
logic_js: logic_js,
|
2018-09-30 07:30:32 +00:00
|
|
|
logic_gzip: logic_gzip,
|
|
|
|
logic_brotli: logic_brotli
|
2016-06-22 11:15:47 +00:00
|
|
|
};
|