Don't run gzip when public folder has/does not exist

Continuation of 71c5878400
This is necessary especially to prevent gzip from running when hexo clean
This commit is contained in:
weyusi 2018-09-29 15:08:45 +09:30
parent 64e72c6248
commit 2dc6c5f48e
4 changed files with 1983 additions and 73 deletions

View File

@ -1,7 +1,6 @@
/* global hexo */ /* global hexo */
var assign = require('object-assign'); var assign = require('object-assign');
//module.exports = function (hexo) {
if (true === hexo.config.neat_enable) { if (true === hexo.config.neat_enable) {
// HTML minifier // HTML minifier
hexo.config.neat_html = assign({ hexo.config.neat_html = assign({
@ -36,15 +35,14 @@ var assign = require('object-assign');
}, hexo.config.neat_js); }, hexo.config.neat_js);
// html, css, js compression // html, css, js compression
hexo.config.gzip = assign({ hexo.config.neat_gzip = assign({
enable: true, enable: true,
logger: true, logger: true,
}, hexo.config.gzip); }, hexo.config.neat_gzip);
var filter = require('./lib/filter'); var filter = require('./lib/filter');
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('after_generate', filter.logic_gzip); hexo.extend.filter.register('before_exit', filter.logic_gzip);
} }
//}

View File

@ -7,7 +7,7 @@ var CleanCSS = require('clean-css'),
var Promise = require('bluebird'); var Promise = require('bluebird');
var minimatch = require('minimatch'); var minimatch = require('minimatch');
var zlib = require('zlib'); var zlib = require('zlib');
var fs = require('fs'); var fs = require('hexo-fs');
function logic_html(str, data) { function logic_html(str, data) {
var hexo = this, var hexo = this,
@ -32,7 +32,7 @@ function logic_html(str, data) {
log.log('Minify the html: %s [%s saved]', path, saved + '%'); log.log('Minify the html: %s [%s saved]', path, saved + '%');
} }
return result; return result;
}; }
function logic_css(str, data) { function logic_css(str, data) {
var hexo = this, var hexo = this,
@ -79,7 +79,7 @@ function logic_js(str, data) {
} }
} }
//uglifyjs doesn't like irrelevant options //uglifyjs doesn't like unsupported options
delete options.enable; delete options.enable;
delete options.exclude; delete options.exclude;
var js_logger = options.logger; var js_logger = options.logger;
@ -96,16 +96,18 @@ function logic_js(str, data) {
function logic_gzip() { function logic_gzip() {
var hexo = this, var hexo = this,
options = hexo.config.gzip; options = hexo.config.neat_gzip;
// Return if disabled. // Return if disabled.
if (false === options.enable) return; if (false === options.enable) return;
var publicFolder = hexo.public_dir; var publicFolder = hexo.public_dir;
var compressFile = function (currentPath) { var compressFile = function (currentPath) {
var files = fs.readdirSync(currentPath); var fileExist = fs.existsSync(currentPath);
if (fileExist) {
var files = fs.listDirSync(currentPath);
for (var i in files) { for (var i in files) {
var currentFile = currentPath + '/' + files[i]; var currentFile = currentPath + files[i];
var stats = fs.statSync(currentFile); var stats = fs.statSync(currentFile);
if (stats.isFile()) { if (stats.isFile()) {
if(currentFile.endsWith(".htm") || if(currentFile.endsWith(".htm") ||
@ -117,18 +119,17 @@ function logic_gzip() {
var inp = fs.createReadStream(currentFile); var inp = fs.createReadStream(currentFile);
var out = fs.createWriteStream(currentFile+'.gz'); var out = fs.createWriteStream(currentFile+'.gz');
inp.pipe(gzip).pipe(out); inp.pipe(gzip).pipe(out);
if (options.logger) { if (options.logger) {
var log = hexo.log || console.log; var log = hexo.log || console.log;
log.log('Compress the file: %s', currentFile); log.log('Compress the file: %s', currentFile);
} }
} }
} } else if (stats.isDirectory()) {
else if (stats.isDirectory()) {
compressFile(currentFile); compressFile(currentFile);
} }
} }
}; }
}
compressFile(publicFolder); compressFile(publicFolder);
} }
@ -136,5 +137,5 @@ module.exports = {
logic_html: logic_html, logic_html: logic_html,
logic_css: logic_css, logic_css: logic_css,
logic_js: logic_js, logic_js: logic_js,
logic_gzip: logic_gzip, logic_gzip: logic_gzip
}; };

1912
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.4.2", "version": "0.4.3",
"readme": "README.md", "readme": "README.md",
"main": "index.js", "main": "index.js",
"directories": { "directories": {
@ -24,7 +24,8 @@
"minimatch": "^3.0.4", "minimatch": "^3.0.4",
"object-assign": "^4.1.1", "object-assign": "^4.1.1",
"stream-to-array": "^2.3.0", "stream-to-array": "^2.3.0",
"uglify-js": "^3.4.9" "uglify-js": "^3.4.9",
"hexo-fs": "^0.2.3"
}, },
"keywords": [ "keywords": [
"html", "html",