Skip to content

Commit

Permalink
fix: fix some js will crash
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbo committed Jul 8, 2020
1 parent 5138ec9 commit 7ed7c11
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 82 deletions.
11 changes: 11 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"useTabs": false,
"printWidth": 120,
"semi": true,
"tabWidth": 4,
"singleQuote": true,
"bracketSpacing": true,
"endOfLine": "lf",
"parser": "typescript"
}

162 changes: 80 additions & 82 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,105 @@
/* 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");
'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.neat_html || {};
// Return if disabled.
if (false === options.enable) return;
var hexo = this,
options = hexo.config.neat_html || {};
// Return if disabled.
if (false === options.enable) return;

var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
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], { matchBase: true })) return str;
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i], { matchBase: true })) return str;
}
}
}
options.continueOnParseError = true;
var result = Htmlminifier(str, options);
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 + "%");
}
var prefix = "<!-- build time:" + Date() + " -->";
var end = "<!-- rebuild by neat -->";
result = prefix + result + end;
return result;
options.continueOnParseError = true;
var result = Htmlminifier(str, options);
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 + '%');
}
var prefix = '<!-- build time:' + Date() + ' -->';
var end = '<!-- rebuild by neat -->';
result = prefix + result + end;
return result;
}

function logic_css(str, data) {
var hexo = this,
options = hexo.config.neat_css;
// Return if disabled.
if (false === options.enable) return;
var hexo = this,
options = hexo.config.neat_css;
// Return if disabled.
if (false === options.enable) return;

var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
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], { matchBase: true })) return str;
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i], { matchBase: true })) return str;
}
}
}

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);
if (options.logger) {
var log = hexo.log || console.log;
log.log("neat the css: %s [ %s saved]", path, saved + "%");
}
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);
if (options.logger) {
var log = hexo.log || console.log;
log.log('neat the css: %s [ %s saved]', path, saved + '%');
}
});
});
});
}

function logic_js(str, data) {
var hexo = this,
options = hexo.config.neat_js;
// Return if disabled.
if (false === options.enable) return;
let js_result = str;
var hexo = this,
options = hexo.config.neat_js;
// Return if disabled.
if (false === options.enable) return;

var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
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], { matchBase: true })) return str;
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i], { matchBase: true })) return str;
}
}
}

var result = UglifyJS.minify(str, options);
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 + "%");
}
var prefix = "// build time:" + Date().toLocaleString() + "\n";
var end = "\n//rebuild by neat ";
var js_result = prefix + result.code + end;
return js_result;
var result = UglifyJS.minify(str, options);
if (!result.error) {
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 + '%');
}
var prefix = '// build time:' + Date().toLocaleString() + '\n';
var end = '\n//rebuild by neat ';
js_result = prefix + result.code + end;
}
return js_result;
}

module.exports = {
logic_html: logic_html,
logic_css: logic_css,
logic_js: logic_js,
logic_html: logic_html,
logic_css: logic_css,
logic_js: logic_js,
};

0 comments on commit 7ed7c11

Please sign in to comment.