Add gzip compression

This commit is contained in:
weyusi 2018-09-28 17:13:54 +09:30
parent 732d07325c
commit ea13ea9967
4 changed files with 67 additions and 9 deletions

View File

@ -8,7 +8,7 @@
> This project is based on [hexo-neat](https://github.com/rozbo/hexo-neat) > This project is based on [hexo-neat](https://github.com/rozbo/hexo-neat)
Yet Another Minifier for Hexo. Yet Another Minifier for Hexo. Minify and compress (gzip) html, js and css.
The original package has not been [updated](https://www.npmjs.com/package/hexo-neat) for a while. Its outdated dependencies suffer from minor [vulnerability](https://snyk.io/test/npm/hexo-neat). The original package has not been [updated](https://www.npmjs.com/package/hexo-neat) for a while. Its outdated dependencies suffer from minor [vulnerability](https://snyk.io/test/npm/hexo-neat).
@ -68,6 +68,13 @@ neat_js:
- **exclude**: Exclude files - **exclude**: Exclude files
**Note:** For more options, see '[UglifyJS](https://github.com/mishoo/UglifyJS2)' **Note:** For more options, see '[UglifyJS](https://github.com/mishoo/UglifyJS2)'
----------
``` yaml
gzip:
enable: true
```
- **enable** - Enable the plugin. Defaults to `true`.
## Credits ## Credits
All credits go to the following work: All credits go to the following work:
@ -75,3 +82,4 @@ All credits go to the following work:
- neat html by [HTMLMinifier](https://github.com/kangax/html-minifier) - neat html by [HTMLMinifier](https://github.com/kangax/html-minifier)
- neat css by [clean-css](https://github.com/jakubpawlowicz/clean-css) - neat css by [clean-css](https://github.com/jakubpawlowicz/clean-css)
- neat js by [UglifyJS](http://lisperator.net/uglifyjs/) - neat js by [UglifyJS](http://lisperator.net/uglifyjs/)
- gzip inspired by [hexo-generator-optimize](https://github.com/JackyRen/hexo-generator-optimize)

View File

@ -35,10 +35,16 @@ var assign = require('object-assign');
exclude: ['*.min.js'] exclude: ['*.min.js']
}, hexo.config.neat_js); }, hexo.config.neat_js);
// html, css, js compression
hexo.config.gzip = assign({
enable: true,
logger: true,
}, hexo.config.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('before_exit', filter.logic_gzip);
} }
//} //}

View File

@ -6,7 +6,8 @@ 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 zlib = require('zlib');
var fs = require('fs');
function logic_html(str, data) { function logic_html(str, data) {
var hexo = this, var hexo = this,
@ -28,7 +29,7 @@ function logic_html(str, data) {
var saved = ((str.length - result.length) / str.length * 100).toFixed(2); var saved = ((str.length - result.length) / str.length * 100).toFixed(2);
if (options.logger) { if (options.logger) {
var log = hexo.log || console.log; var log = hexo.log || console.log;
log.log('Minify the html: %s [ %s saved]', path, saved + '%'); log.log('Minify the html: %s [%s saved]', path, saved + '%');
} }
return result; return result;
}; };
@ -56,7 +57,7 @@ function logic_css(str, data) {
resolve(result.styles); resolve(result.styles);
if (options.logger) { if (options.logger) {
var log = hexo.log || console.log; var log = hexo.log || console.log;
log.log('Minify the css: %s [ %s saved]', path, saved + '%'); log.log('Minify the css: %s [%s saved]', path, saved + '%');
} }
}); });
}); });
@ -88,13 +89,52 @@ function logic_js(str, data) {
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2); var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
if (js_logger) { if (js_logger) {
var log = hexo.log || console.log; var log = hexo.log || console.log;
log.log('Minify the js: %s [ %s saved]', path, saved + '%'); log.log('Minify the js: %s [%s saved]', path, saved + '%');
} }
return result.code; return result.code;
} }
function logic_gzip() {
var hexo = this,
options = hexo.config.gzip;
// Return if disabled.
if (false === options.enable) return;
var publicFolder = hexo.base_dir+'public';
var compressFile = function (currentPath) {
var files = fs.readdirSync(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 gzip = zlib.createGzip('level=9');
var inp = fs.createReadStream(currentFile);
var out = fs.createWriteStream(currentFile+'.gz');
inp.pipe(gzip).pipe(out);
if (options.logger) {
var log = hexo.log || console.log;
log.log('Compress the file: %s', currentFile);
}
}
}
else if (stats.isDirectory()) {
compressFile(currentFile);
}
}
};
compressFile(publicFolder);
}
module.exports = { 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,
}; };

View File

@ -1,7 +1,7 @@
{ {
"name": "hexo-yam", "name": "hexo-yam",
"description": "Yet Another Minifier. Minify html, js and css", "description": "Yet Another Minifier. Minify and compress html, js and css",
"version": "0.3.0", "version": "0.4.0",
"readme": "README.md", "readme": "README.md",
"main": "index.js", "main": "index.js",
"directories": { "directories": {
@ -12,6 +12,7 @@
}, },
"author": "weyusi", "author": "weyusi",
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/weyusi/hexo-yam",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/weyusi/hexo-yam.git" "url": "https://github.com/weyusi/hexo-yam.git"
@ -29,7 +30,10 @@
"html", "html",
"js", "js",
"css", "css",
"hexo", "minify",
"minify" "compress",
"gzip",
"hexo-yam",
"hexo"
] ]
} }