mirror of https://github.com/curbengh/hexo-yam
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:
parent
d99b1d3158
commit
1dafc72850
4
index.js
4
index.js
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
113
lib/filter.js
113
lib/filter.js
|
@ -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;
|
||||||
|
const routeList = route.list();
|
||||||
|
|
||||||
var compressFile = function (currentPath) {
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
|
||||||
var fileExist = fs.existsSync(currentPath);
|
return new Promise((resolve, reject) => {
|
||||||
if (fileExist) {
|
// Grab all assets using hexo router
|
||||||
var files = fs.listDirSync(currentPath);
|
const assetPath = route.get(path);
|
||||||
for (var i in files) {
|
let assetTxt = '';
|
||||||
var currentFile = currentPath + files[i];
|
// Extract the content
|
||||||
var stats = fs.statSync(currentFile);
|
assetPath.on('data', (chunk) => (assetTxt += chunk));
|
||||||
if (stats.isFile()) {
|
assetPath.on('end', () => {
|
||||||
if(currentFile.endsWith(".htm") ||
|
if (assetTxt.length) {
|
||||||
currentFile.endsWith(".html") ||
|
// gzip compress using highest level
|
||||||
currentFile.endsWith(".js") ||
|
zlib.gzip(assetTxt, {level:zlib.Z_BEST_COMPRESSION}, (err, buffer) => {
|
||||||
currentFile.endsWith(".css") ||
|
if (!err) {
|
||||||
currentFile.endsWith(".txt")) {
|
// Save the compressed file to .gz
|
||||||
var inp = fs.createReadStream(currentFile);
|
route.set(path + '.gz', buffer);
|
||||||
var out = fs.createWriteStream(currentFile+'.gz');
|
//Logging
|
||||||
var gzip = zlib.createGzip('level=9');
|
var saved = ((assetTxt.length - buffer.toString().length) / assetTxt.length * 100).toFixed(2);
|
||||||
inp.pipe(gzip).pipe(out);
|
if (gz_logger) {
|
||||||
|
|
||||||
if (options.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;
|
||||||
|
const routeList = route.list();
|
||||||
|
|
||||||
var compressFile = function (currentPath) {
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
|
||||||
var fileExist = fs.existsSync(currentPath);
|
return new Promise((resolve, reject) => {
|
||||||
if (fileExist) {
|
// Grab all assets using hexo router
|
||||||
var files = fs.listDirSync(currentPath);
|
const assetPath = route.get(path);
|
||||||
for (var i in files) {
|
let assetTxt = '';
|
||||||
var currentFile = currentPath + files[i];
|
// Extract the content
|
||||||
var stats = fs.statSync(currentFile);
|
assetPath.on('data', (chunk) => (assetTxt += chunk));
|
||||||
if (stats.isFile()) {
|
assetPath.on('end', () => {
|
||||||
if(currentFile.endsWith(".htm") ||
|
if (assetTxt.length) {
|
||||||
currentFile.endsWith(".html") ||
|
// Input has to be buffer for brotli
|
||||||
currentFile.endsWith(".js") ||
|
var buffer = new Buffer.from(assetTxt, "utf-8");
|
||||||
currentFile.endsWith(".css") ||
|
// brotli compress using highest level
|
||||||
currentFile.endsWith(".txt")) {
|
br.compress(buffer, {quality:br.BROTLI_MAX_QUALITY}, (err, output) => {
|
||||||
var inp = fs.createReadStream(currentFile);
|
if (!err) {
|
||||||
var out = fs.createWriteStream(currentFile+'.br');
|
// Save the compressed file to .br
|
||||||
var brotli = br.compressStream('quality=11');
|
route.set(path + '.br', output);
|
||||||
inp.pipe(brotli()).pipe(out);
|
//Logging
|
||||||
|
var saved = ((buffer.length - output.toString().length) / buffer.length * 100).toFixed(2);
|
||||||
if (options.logger) {
|
if (br_logger) {
|
||||||
var log = hexo.log || console.log;
|
var log = hexo.log || console.log;
|
||||||
log.log('Brotli-ed the file: %s', currentFile);
|
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 = {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -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",
|
||||||
|
|
Loading…
Reference in New Issue