Use after_generate instead of before_exit

before_exit was a workaround instead of proper after_generate
Use hexo router instead of filesystem manipulation, so hexo-fs is not required anymore
this approach also allows size comparison of original and compressed file, just like minifier currently have
inspired by
 - https://www.travisgeis.com/2018/04/23/lazy-image-resizing-in-hexo-image-sizes-v2/
 - https://github.com/ottobonn/hexo-image-sizes/blob/master/lib/ImageResizer.js
 - https://github.com/chenzhutian/hexo-all-minifier/blob/master/lib/concatJS.js
 - https://stackoverflow.com/a/39441194
This commit is contained in:
weyusi 2018-10-05 12:05:07 +09:30
parent d99b1d3158
commit 1dafc72850
4 changed files with 68 additions and 1924 deletions

View File

@ -50,6 +50,6 @@ if (true === hexo.config.neat_enable) {
hexo.extend.filter.register('after_render:html', filter.logic_html);
hexo.extend.filter.register('after_render:css', filter.logic_css);
hexo.extend.filter.register('after_render:js', filter.logic_js);
hexo.extend.filter.register('before_exit', filter.logic_gzip);
hexo.extend.filter.register('before_exit', filter.logic_brotli);
hexo.extend.filter.register('after_generate', filter.logic_gzip);
hexo.extend.filter.register('after_generate', filter.logic_brotli);
}

View File

@ -6,7 +6,6 @@ var CleanCSS = require('clean-css'),
streamToArray = require('stream-to-array');
var Promise = require('bluebird');
var minimatch = require('minimatch');
var fs = require('hexo-fs');
var zlib = require('zlib');
var br = require('iltorb');
@ -101,38 +100,39 @@ function logic_gzip() {
// Return if disabled.
if (false === options.enable) return;
var publicFolder = hexo.public_dir;
var compressFile = function (currentPath) {
var fileExist = fs.existsSync(currentPath);
if (fileExist) {
var files = fs.listDirSync(currentPath);
for (var i in files) {
var currentFile = currentPath + files[i];
var stats = fs.statSync(currentFile);
if (stats.isFile()) {
if(currentFile.endsWith(".htm") ||
currentFile.endsWith(".html") ||
currentFile.endsWith(".js") ||
currentFile.endsWith(".css") ||
currentFile.endsWith(".txt")) {
var inp = fs.createReadStream(currentFile);
var out = fs.createWriteStream(currentFile+'.gz');
var gzip = zlib.createGzip('level=9');
inp.pipe(gzip).pipe(out);
if (options.logger) {
var log = hexo.log || console.log;
log.log('Gzipped the file: %s', currentFile);
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);
}
}
} else if (stats.isDirectory()) {
compressFile(currentFile);
});
}
}
}
}
compressFile(publicFolder);
});
});
}));
}
function logic_brotli() {
@ -141,38 +141,41 @@ function logic_brotli() {
// Return if disabled.
if (false === options.enable) return;
var publicFolder = hexo.public_dir;
var compressFile = function (currentPath) {
var fileExist = fs.existsSync(currentPath);
if (fileExist) {
var files = fs.listDirSync(currentPath);
for (var i in files) {
var currentFile = currentPath + files[i];
var stats = fs.statSync(currentFile);
if (stats.isFile()) {
if(currentFile.endsWith(".htm") ||
currentFile.endsWith(".html") ||
currentFile.endsWith(".js") ||
currentFile.endsWith(".css") ||
currentFile.endsWith(".txt")) {
var inp = fs.createReadStream(currentFile);
var out = fs.createWriteStream(currentFile+'.br');
var brotli = br.compressStream('quality=11');
inp.pipe(brotli()).pipe(out);
if (options.logger) {
var log = hexo.log || console.log;
log.log('Brotli-ed the file: %s', currentFile);
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);
}
}
} else if (stats.isDirectory()) {
compressFile(currentFile);
});
}
}
}
}
compressFile(publicFolder);
});
});
}));
}
module.exports = {

1860
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "hexo-yam",
"description": "Yet Another Minifier. Minify and compress html, js and css",
"version": "0.5.2",
"version": "0.6.0",
"readme": "README.md",
"main": "index.js",
"directories": {
@ -20,7 +20,6 @@
"dependencies": {
"bluebird": "^3.5.2",
"clean-css": "^4.2.1",
"hexo-fs": "^0.2.3",
"html-minifier": "^3.5.20",
"iltorb": "^2.4.0",
"minimatch": "^3.0.4",