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:html', filter.logic_html);
hexo.extend.filter.register('after_render:css', filter.logic_css); hexo.extend.filter.register('after_render:css', filter.logic_css);
hexo.extend.filter.register('after_render:js', filter.logic_js); hexo.extend.filter.register('after_render:js', filter.logic_js);
hexo.extend.filter.register('before_exit', filter.logic_gzip); hexo.extend.filter.register('after_generate', filter.logic_gzip);
hexo.extend.filter.register('before_exit', filter.logic_brotli); 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'); streamToArray = require('stream-to-array');
var Promise = require('bluebird'); var Promise = require('bluebird');
var minimatch = require('minimatch'); var minimatch = require('minimatch');
var fs = require('hexo-fs');
var zlib = require('zlib'); var zlib = require('zlib');
var br = require('iltorb'); var br = require('iltorb');
@ -101,38 +100,39 @@ function logic_gzip() {
// Return if disabled. // Return if disabled.
if (false === options.enable) return; if (false === options.enable) return;
var publicFolder = hexo.public_dir; var gz_logger = options.logger;
const route = hexo.route;
var compressFile = function (currentPath) { const routeList = route.list();
var fileExist = fs.existsSync(currentPath);
if (fileExist) { return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
var files = fs.listDirSync(currentPath); return new Promise((resolve, reject) => {
for (var i in files) { // Grab all assets using hexo router
var currentFile = currentPath + files[i]; const assetPath = route.get(path);
var stats = fs.statSync(currentFile); let assetTxt = '';
if (stats.isFile()) { // Extract the content
if(currentFile.endsWith(".htm") || assetPath.on('data', (chunk) => (assetTxt += chunk));
currentFile.endsWith(".html") || assetPath.on('end', () => {
currentFile.endsWith(".js") || if (assetTxt.length) {
currentFile.endsWith(".css") || // gzip compress using highest level
currentFile.endsWith(".txt")) { zlib.gzip(assetTxt, {level:zlib.Z_BEST_COMPRESSION}, (err, buffer) => {
var inp = fs.createReadStream(currentFile); if (!err) {
var out = fs.createWriteStream(currentFile+'.gz'); // Save the compressed file to .gz
var gzip = zlib.createGzip('level=9'); route.set(path + '.gz', buffer);
inp.pipe(gzip).pipe(out); //Logging
var saved = ((assetTxt.length - buffer.toString().length) / assetTxt.length * 100).toFixed(2);
if (options.logger) { if (gz_logger) {
var log = hexo.log || console.log; var log = hexo.log || console.log;
log.log('Gzipped the file: %s', currentFile); 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() { function logic_brotli() {
@ -141,38 +141,41 @@ function logic_brotli() {
// Return if disabled. // Return if disabled.
if (false === options.enable) return; if (false === options.enable) return;
var publicFolder = hexo.public_dir; var br_logger = options.logger;
const route = hexo.route;
var compressFile = function (currentPath) { const routeList = route.list();
var fileExist = fs.existsSync(currentPath);
if (fileExist) { return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
var files = fs.listDirSync(currentPath); return new Promise((resolve, reject) => {
for (var i in files) { // Grab all assets using hexo router
var currentFile = currentPath + files[i]; const assetPath = route.get(path);
var stats = fs.statSync(currentFile); let assetTxt = '';
if (stats.isFile()) { // Extract the content
if(currentFile.endsWith(".htm") || assetPath.on('data', (chunk) => (assetTxt += chunk));
currentFile.endsWith(".html") || assetPath.on('end', () => {
currentFile.endsWith(".js") || if (assetTxt.length) {
currentFile.endsWith(".css") || // Input has to be buffer for brotli
currentFile.endsWith(".txt")) { var buffer = new Buffer.from(assetTxt, "utf-8");
var inp = fs.createReadStream(currentFile); // brotli compress using highest level
var out = fs.createWriteStream(currentFile+'.br'); br.compress(buffer, {quality:br.BROTLI_MAX_QUALITY}, (err, output) => {
var brotli = br.compressStream('quality=11'); if (!err) {
inp.pipe(brotli()).pipe(out); // Save the compressed file to .br
route.set(path + '.br', output);
if (options.logger) { //Logging
var log = hexo.log || console.log; var saved = ((buffer.length - output.toString().length) / buffer.length * 100).toFixed(2);
log.log('Brotli-ed the file: %s', currentFile); 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 = { 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", "name": "hexo-yam",
"description": "Yet Another Minifier. Minify and compress html, js and css", "description": "Yet Another Minifier. Minify and compress html, js and css",
"version": "0.5.2", "version": "0.6.0",
"readme": "README.md", "readme": "README.md",
"main": "index.js", "main": "index.js",
"directories": { "directories": {
@ -20,7 +20,6 @@
"dependencies": { "dependencies": {
"bluebird": "^3.5.2", "bluebird": "^3.5.2",
"clean-css": "^4.2.1", "clean-css": "^4.2.1",
"hexo-fs": "^0.2.3",
"html-minifier": "^3.5.20", "html-minifier": "^3.5.20",
"iltorb": "^2.4.0", "iltorb": "^2.4.0",
"minimatch": "^3.0.4", "minimatch": "^3.0.4",