Skip to content

Commit

Permalink
Merge pull request #8 from mygu/master
Browse files Browse the repository at this point in the history
Some fixes and optimizations
  • Loading branch information
落月 authored Jun 29, 2018
2 parents 892cde9 + b44d024 commit 2896967
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ neat_html:
exclude:
```
- **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Print log switch. Defaults to `true`.
- **exclude**: Exclude files
**Note:** there are so many params please see '[HTMLMinifier](https://github.com/kangax/html-minifier)'
----------
Expand All @@ -37,6 +38,7 @@ neat_css:
- '*.min.css'
```
- **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Print log switch. Defaults to `true`.
- **exclude**: Exclude files

----------
Expand All @@ -52,6 +54,7 @@ neat_js:
```
- **enable** - Enable the plugin. Defaults to `true`.
- **mangle**: Mangle file names
- **logger** - Print log switch. Defaults to `true`.
- **output**: Output options
- **compress**: Compress options
- **exclude**: Exclude files
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var assign = require('object-assign');
// HTML minifier
hexo.config.neat_html = assign({
enable: true,
logger: true,
exclude: [],
ignoreCustomComments: [/^\s*more/],
removeComments: true,
Expand All @@ -20,13 +21,15 @@ var assign = require('object-assign');
// Css minifier
hexo.config.neat_css = assign({
enable: true,
logger: true,
exclude: ['*.min.css']
}, hexo.config.neat_css);

// Js minifier
hexo.config.neat_js = assign({
enable: true,
mangle: true,
logger: true,
output: {},
compress: {},
exclude: ['*.min.js']
Expand Down
30 changes: 17 additions & 13 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ function logic_html(str, data) {

if (path && exclude && exclude.length) {
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 saved = ((str.length - result.length) / str.length * 100).toFixed(2);
log.log('neat the html: %s [ %s saved]', path, saved + '%');
if (options.logger) {
var log = hexo.log || console.log;
log.log('neat the html: %s [ %s saved]', path, saved + '%');
}
var prefix = '<!-- build time:' + Date() + " -->";
var end = '<!-- rebuild by neat -->';
result = prefix + result + end;
Expand All @@ -46,20 +48,22 @@ function logic_css(str, data) {

if (path && exclude && exclude.length) {
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) {
new CleanCSS(options).minify(str, function(err, result) {
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);
var prefix = '/* build time:' + Date().toLocaleString() + "*/\n";
var end = '\n/* rebuild by neat */';
var css_result = prefix + result.styles + end;
resolve(css_result);
log.log('neat the css: %s [ %s saved]', path, saved + '%');
if (options.logger) {
var log = hexo.log || console.log;
log.log('neat the css: %s [ %s saved]', path, saved + '%');
}
});
});
}
Expand All @@ -76,22 +80,22 @@ function logic_js(str, data) {

if (path && exclude && exclude.length) {
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 saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
log.log('neat the js: %s [ %s saved]', path, saved + '%');
if (options.logger) {
var log = hexo.log || console.log;
log.log('neat the js: %s [ %s saved]', path, saved + '%');
}
var prefix = '// build time:' + Date().toLocaleString() + "\n";
var end = '\n//rebuild by neat ';
var js_result = prefix + result.code + end;
return js_result;
}



module.exports = {
logic_html: logic_html,
logic_css: logic_css,
Expand Down

0 comments on commit 2896967

Please sign in to comment.