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,50 +1,48 @@
/* 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({ enable: true,
enable: true, logger: true,
logger: true, exclude: [],
exclude: [], ignoreCustomComments: [/^\s*more/],
ignoreCustomComments: [/^\s*more/], removeComments: true,
removeComments: true, removeCommentsFromCDATA: true,
removeCommentsFromCDATA: true, collapseWhitespace: true,
collapseWhitespace: true, collapseBooleanAttributes: true,
collapseBooleanAttributes: true, removeEmptyAttributes: true,
removeEmptyAttributes: true, minifyJS: true,
minifyJS: true, minifyCSS: true,
minifyCSS: true, }, hexo.config.neat_html);
}, hexo.config.neat_html);
// Css minifier // Css minifier
hexo.config.neat_css = assign({ hexo.config.neat_css = assign({
enable: true, enable: true,
logger: true, logger: true,
exclude: ['*.min.css'] exclude: ['*.min.css']
}, hexo.config.neat_css); }, hexo.config.neat_css);
// Js minifier // Js minifier
hexo.config.neat_js = assign({ hexo.config.neat_js = assign({
enable: true, enable: true,
mangle: true, mangle: true,
logger: true, logger: true,
output: {}, output: {},
compress: {}, compress: {},
exclude: ['*.min.js'] exclude: ['*.min.js']
}, 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,39 +96,40 @@ 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);
for (var i in files) { if (fileExist) {
var currentFile = currentPath + '/' + files[i]; var files = fs.listDirSync(currentPath);
var stats = fs.statSync(currentFile); for (var i in files) {
if (stats.isFile()) { var currentFile = currentPath + files[i];
if(currentFile.endsWith(".htm") || var stats = fs.statSync(currentFile);
currentFile.endsWith(".html") || if (stats.isFile()) {
currentFile.endsWith(".js") || if(currentFile.endsWith(".htm") ||
currentFile.endsWith(".css") || currentFile.endsWith(".html") ||
currentFile.endsWith(".txt")) { currentFile.endsWith(".js") ||
var gzip = zlib.createGzip('level=9'); currentFile.endsWith(".css") ||
var inp = fs.createReadStream(currentFile); currentFile.endsWith(".txt")) {
var out = fs.createWriteStream(currentFile+'.gz'); var gzip = zlib.createGzip('level=9');
inp.pipe(gzip).pipe(out); var inp = fs.createReadStream(currentFile);
var out = fs.createWriteStream(currentFile+'.gz');
if (options.logger) { inp.pipe(gzip).pipe(out);
var log = hexo.log || console.log; if (options.logger) {
log.log('Compress the file: %s', currentFile); var log = hexo.log || console.log;
log.log('Compress the file: %s', currentFile);
}
} }
} else if (stats.isDirectory()) {
compressFile(currentFile);
} }
} }
else if (stats.isDirectory()) {
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",