init code

This commit is contained in:
rozbo 2016-05-26 19:09:41 +08:00
commit b3be6d5b97
4 changed files with 246 additions and 0 deletions

79
README.md Normal file
View File

@ -0,0 +1,79 @@
# Hexo-all-minifier
[![npm version](https://badge.fury.io/js/hexo-all-minifier.svg)](https://badge.fury.io/js/hexo-all-minifier)
[![NPM Dependencies](https://david-dm.org/unhealthy/hexo-all-minifier.svg)](https://www.npmjs.com/package/hexo-all-minifier)
All in one. Minifier & Optimization plugin for [Hexo](https://hexo.io).
Since most of the optimize plugin for [HEXO](https://hexo.io) have been deprecated, and [HEXO](https://hexo.io) has upgraded to 3.XX, so I decide to implement this plugin.
## Installation
``` bash
$ npm install hexo-all-minifier --save
```
## Components
Integrate all the official minifier plugins of HEXO and a imagemin plugin:
- [hexo-html-minifier](https://github.com/hexojs/hexo-html-minifier), which is based on [HTMLMinifier](https://github.com/kangax/html-minifier)
- [hexo-clean-css](https://github.com/hexojs/hexo-clean-css), which is based on [clean-css](https://github.com/jakubpawlowicz/clean-css)
- [hexo-uglify](https://github.com/hexojs/hexo-uglify), which is based on [UglifyJS](http://lisperator.net/uglifyjs/)
- [hexo-imagemin](https://github.com/vseventer/hexo-imagemin), which is based on [imagemin](https://github.com/imagemin/imagemin)
Thanks for their works.
## Options
``` yaml
html_minifier:
enable: true
exclude:
```
- **enable** - Enable the plugin. Defaults to `true`.
- **exclude**: Exclude files
----------
``` yaml
css_minifier:
enable: true
exclude:
- '*.min.css'
```
- **enable** - Enable the plugin. Defaults to `true`.
- **exclude**: Exclude files
----------
``` yaml
js_minifier:
enable: true
mangle: true
output:
compress:
exclude:
- '*.min.js'
```
- **enable** - Enable the plugin. Defaults to `true`.
- **mangle**: Mangle file names
- **output**: Output options
- **compress**: Compress options
- **exclude**: Exclude files
----------
```yaml
image_minifier:
enable: true
interlaced: false
multipass: false
optimizationLevel: 2
pngquant: false
progressive: false
```
- **enable** - Enable the plugin. Defaults to `true`.
- **interlaced** - Interlace gif for progressive rendering. Defaults to `false`.
- **multipass** - Optimize svg multiple times until its fully optimized. Defaults to `false`.
- **optimizationLevel** - Select an optimization level between 0 and 7. Defaults to `2`.
- **pngquant** - Enable [imagemin-pngquant](https://github.com/imagemin/imagemin-pngquant) plugin. Defaults to `false`.
- **progressive** - Lossless conversion to progressive. Defaults to `false`.
To be continued

43
index.js Normal file
View File

@ -0,0 +1,43 @@
/* global hexo */
var assign = require('object-assign');
//module.exports = function (hexo) {
if (true === hexo.config.neat_enable) {
// HTML minifier
hexo.config.neat_html = assign({
enable: true,
exclude: [],
ignoreCustomComments: [/^\s*more/],
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true,
}, hexo.config.neat_html);
// Css minifier
hexo.config.neat_css = assign({
enable: true,
exclude: ['*.min.css']
}, hexo.config.neat_css);
// Js minifier
hexo.config.neat_js = assign({
enable: true,
mangle: true,
output: {},
compress: {},
exclude: ['*.min.js']
}, hexo.config.neat_js, {
fromString: true
});
var filter = require('./lib/filter');
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:js', filter.logic_js);
}
//}

91
lib/filter.js Normal file
View File

@ -0,0 +1,91 @@
/* global hexo */
'use strict';
var CleanCSS = require('clean-css'),
UglifyJS = require('uglify-js'),
Htmlminifier = require('html-minifier').minify,
streamToArray = require('stream-to-array');
var Promise = require('bluebird');
var minimatch = require('minimatch');
function logic_html(str, data) {
var hexo = this,
options = hexo.config.html_minifier;
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
var log = hexo.log || console.log;
var result = Htmlminifier(str, options);
var saved = ((str.length - result.length) / str.length * 100).toFixed(2);
log.log('neat the html: %s [ %s saved]', path, saved + '%');
return result;
};
function logic_css(str, data) {
var hexo = this,
options = hexo.config.css_minifier;
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
var log = hexo.log || console.log;
return new Promise(function (resolve, reject) {
new CleanCSS(options).minify(str, function (err, result) {
if (err) return reject(err);
var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2);
resolve(result.styles);
log.log('neat the css: %s [ %s saved]', path, saved + '%');
});
});
}
function logic_js(str, data) {
var hexo = this,
options = hexo.config.js_minifier;
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
var log = hexo.log || console;
var result = UglifyJS.minify(str, options);
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
log.log('neat the js: %s [ %s saved]', path, saved + '%');
return result.code;
}
module.exports = {
logic_html: logic_html,
logic_css: logic_css,
logic_js: logic_js,
};

33
package.json Normal file
View File

@ -0,0 +1,33 @@
{
"author": {
"name": "rozbo"
},
"dependencies": {
"bluebird": "^3.3.5",
"clean-css": "^2.9.x",
"html-minifier": "^2.1.2",
"minimatch": "^3.0.0",
"object-assign": "^4.1.0",
"stream-to-array": "^2.3.0",
"uglify-js": "~2.6.2"
},
"description": "auto Minify html、js、css and make it neat",
"devDependencies": {},
"directories": {},
"keywords": [
"html",
"js",
"css",
"hexo",
"minify"
],
"license": "MIT",
"main": "index.js",
"name": "hexo-neat",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.1.2"
}