Merge pull request #8 from mygu/master

Some fixes and optimizations
This commit is contained in:
落月 2018-06-29 18:18:02 +08:00 committed by GitHub
commit 28969670e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 13 deletions

View File

@ -26,6 +26,7 @@ neat_html:
exclude: exclude:
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Print log switch. Defaults to `true`.
- **exclude**: Exclude files - **exclude**: Exclude files
**Note:** there are so many params please see '[HTMLMinifier](https://github.com/kangax/html-minifier)' **Note:** there are so many params please see '[HTMLMinifier](https://github.com/kangax/html-minifier)'
---------- ----------
@ -37,6 +38,7 @@ neat_css:
- '*.min.css' - '*.min.css'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Print log switch. Defaults to `true`.
- **exclude**: Exclude files - **exclude**: Exclude files
---------- ----------
@ -52,6 +54,7 @@ neat_js:
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **mangle**: Mangle file names - **mangle**: Mangle file names
- **logger** - Print log switch. Defaults to `true`.
- **output**: Output options - **output**: Output options
- **compress**: Compress options - **compress**: Compress options
- **exclude**: Exclude files - **exclude**: Exclude files

View File

@ -6,6 +6,7 @@ var assign = require('object-assign');
// HTML minifier // HTML minifier
hexo.config.neat_html = assign({ hexo.config.neat_html = assign({
enable: true, enable: true,
logger: true,
exclude: [], exclude: [],
ignoreCustomComments: [/^\s*more/], ignoreCustomComments: [/^\s*more/],
removeComments: true, removeComments: true,
@ -20,6 +21,7 @@ var assign = require('object-assign');
// Css minifier // Css minifier
hexo.config.neat_css = assign({ hexo.config.neat_css = assign({
enable: true, enable: true,
logger: true,
exclude: ['*.min.css'] exclude: ['*.min.css']
}, hexo.config.neat_css); }, hexo.config.neat_css);
@ -27,6 +29,7 @@ var assign = require('object-assign');
hexo.config.neat_js = assign({ hexo.config.neat_js = assign({
enable: true, enable: true,
mangle: true, mangle: true,
logger: true,
output: {}, output: {},
compress: {}, compress: {},
exclude: ['*.min.js'] exclude: ['*.min.js']

View File

@ -20,14 +20,16 @@ function logic_html(str, data) {
if (path && exclude && exclude.length) { if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) { for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str; if (minimatch(path, exclude[i], {matchBase: true})) return str;
} }
} }
var log = hexo.log || console.log;
var result = Htmlminifier(str, options); var result = Htmlminifier(str, options);
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) {
var log = hexo.log || console.log;
log.log('neat the html: %s [ %s saved]', path, saved + '%'); log.log('neat the html: %s [ %s saved]', path, saved + '%');
}
var prefix = '<!-- build time:' + Date() + " -->"; var prefix = '<!-- build time:' + Date() + " -->";
var end = '<!-- rebuild by neat -->'; var end = '<!-- rebuild by neat -->';
result = prefix + result + end; result = prefix + result + end;
@ -46,20 +48,22 @@ function logic_css(str, data) {
if (path && exclude && exclude.length) { if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) { for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str; if (minimatch(path, exclude[i], {matchBase: true})) return str;
} }
} }
var log = hexo.log || console.log; return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) { new CleanCSS(options).minify(str, function (err, result) {
new CleanCSS(options).minify(str, function(err, result) {
if (err) return reject(err); if (err) return reject(err);
var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2); var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2);
var prefix = '/* build time:' + Date().toLocaleString() + "*/\n"; var prefix = '/* build time:' + Date().toLocaleString() + "*/\n";
var end = '\n/* rebuild by neat */'; var end = '\n/* rebuild by neat */';
var css_result = prefix + result.styles + end; var css_result = prefix + result.styles + end;
resolve(css_result); resolve(css_result);
if (options.logger) {
var log = hexo.log || console.log;
log.log('neat the css: %s [ %s saved]', path, saved + '%'); log.log('neat the css: %s [ %s saved]', path, saved + '%');
}
}); });
}); });
} }
@ -76,22 +80,22 @@ function logic_js(str, data) {
if (path && exclude && exclude.length) { if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) { for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str; if (minimatch(path, exclude[i], {matchBase: true})) return str;
} }
} }
var log = hexo.log || console;
var result = UglifyJS.minify(str, options); var result = UglifyJS.minify(str, options);
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 (options.logger) {
var log = hexo.log || console.log;
log.log('neat the js: %s [ %s saved]', path, saved + '%'); log.log('neat the js: %s [ %s saved]', path, saved + '%');
}
var prefix = '// build time:' + Date().toLocaleString() + "\n"; var prefix = '// build time:' + Date().toLocaleString() + "\n";
var end = '\n//rebuild by neat '; var end = '\n//rebuild by neat ';
var js_result = prefix + result.code + end; var js_result = prefix + result.code + end;
return js_result; return js_result;
} }
module.exports = { module.exports = {
logic_html: logic_html, logic_html: logic_html,
logic_css: logic_css, logic_css: logic_css,