Use js standard style

https://standardjs.com/
This commit is contained in:
weyusi 2018-10-25 20:05:59 +10:30
parent 7659d74fcb
commit 58adaeb34d
2 changed files with 219 additions and 213 deletions

View File

@ -1,55 +1,55 @@
/* global hexo */ /* global hexo */
var assign = require('object-assign'); var assign = require('object-assign')
if (true === hexo.config.neat_enable) { if (hexo.config.neat_enable === true) {
// HTML minifier // HTML minifier
hexo.config.neat_html = assign({ hexo.config.neat_html = assign({
enable: true, enable: true,
logger: true, logger: true,
exclude: [], exclude: [],
ignoreCustomComments: [/^\s*more/], ignoreCustomComments: [/^\s*more/],
removeComments: true, removeComments: true,
removeCommentsFromCDATA: true, removeCommentsFromCDATA: true,
collapseWhitespace: true, collapseWhitespace: true,
collapseBooleanAttributes: true, collapseBooleanAttributes: true,
removeEmptyAttributes: true, removeEmptyAttributes: true,
minifyJS: true, minifyJS: true,
minifyCSS: true, minifyCSS: true
}, hexo.config.neat_html); }, hexo.config.neat_html)
// Css minifier // Css minifier
hexo.config.neat_css = assign({ hexo.config.neat_css = assign({
enable: true, enable: true,
logger: true, logger: true,
exclude: ['*.min.css'] exclude: ['*.min.css']
}, hexo.config.neat_css); }, hexo.config.neat_css)
// Js minifier // Js minifier
hexo.config.neat_js = assign({ hexo.config.neat_js = assign({
enable: true, enable: true,
mangle: true, mangle: true,
logger: true, logger: true,
output: {}, output: {},
compress: {}, compress: {},
exclude: ['*.min.js'] exclude: ['*.min.js']
}, hexo.config.neat_js); }, hexo.config.neat_js)
// html, css, js compression // html, css, js compression
hexo.config.neat_gzip = assign({ hexo.config.neat_gzip = assign({
enable: true, enable: true,
logger: true, logger: true
}, hexo.config.neat_gzip); }, hexo.config.neat_gzip)
// html, css, js compression // html, css, js compression
hexo.config.neat_brotli = assign({ hexo.config.neat_brotli = assign({
enable: true, enable: true,
logger: true, logger: true
}, hexo.config.neat_brotli); }, hexo.config.neat_brotli)
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.logicHtml)
hexo.extend.filter.register('after_render:css', filter.logic_css); hexo.extend.filter.register('after_render:css', filter.logicCss)
hexo.extend.filter.register('after_render:js', filter.logic_js); hexo.extend.filter.register('after_render:js', filter.logicJs)
hexo.extend.filter.register('after_generate', filter.logic_gzip); hexo.extend.filter.register('after_generate', filter.logicGzip)
hexo.extend.filter.register('after_generate', filter.logic_brotli); hexo.extend.filter.register('after_generate', filter.logicBrotli)
} }

View File

@ -1,187 +1,193 @@
/* global hexo */ /* global hexo */
'use strict'; 'use strict'
var CleanCSS = require('clean-css'), 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');
var zlib = require('zlib');
var br = require('iltorb');
function logic_html(str, data) { var UglifyJS = require('uglify-js')
var hexo = this,
options = hexo.config.neat_html;
// Return if disabled.
if (false === options.enable) return;
var path = data.path; var Htmlminifier = require('html-minifier').minify
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) { var streamToArray = require('stream-to-array')
for (var i = 0, len = exclude.length; i < len; i++) { var Promise = require('bluebird')
if (minimatch(path, exclude[i], {matchBase: true})) return str; var minimatch = require('minimatch')
} var zlib = require('zlib')
var br = require('iltorb')
function logicHtml (str, data) {
var hexo = this
var options = hexo.config.neat_html
// Return if disabled.
if (options.enable === false) 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], { matchBase: true })) return str
} }
}
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) { 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
} }
function logic_css(str, data) { function logicCss (str, data) {
var hexo = this, var hexo = this
options = hexo.config.neat_css;
// Return if disabled.
if (false === options.enable) return;
var path = data.path; var options = hexo.config.neat_css
var exclude = options.exclude; // Return if disabled.
if (exclude && !Array.isArray(exclude)) exclude = [exclude]; if (options.enable === false) return
if (path && exclude && exclude.length) { var path = data.path
for (var i = 0, len = exclude.length; i < len; i++) { var exclude = options.exclude
if (minimatch(path, exclude[i], {matchBase: true})) return str; 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
} }
}
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
new CleanCSS({level:2}).minify(str, function (err, result) { new CleanCSS({ level: 2 }).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)
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 + '%')
}
})
})
}
function logicJs (str, data) {
var hexo = this
var options = hexo.config.neat_js
// Return if disabled.
if (options.enable === false) 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], { matchBase: true })) return str
}
}
// uglifyjs doesn't like unsupported options
delete options.enable
delete options.exclude
var jsLogger = options.logger
delete options.logger
var result = UglifyJS.minify(str, options)
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
if (jsLogger) {
var log = hexo.log || console.log
log.log('Minify the js: %s [%s saved]', path, saved + '%')
}
return result.code
}
function logicGzip () {
var hexo = this
var options = hexo.config.neat_gzip
// Return if disabled.
if (options.enable === false) return
const route = hexo.route
const routeList = route.list()
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
const assetPath = route.get(path)
let assetTxt = ''
// Extract the content
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', () => {
if (assetTxt.length) {
// gzip compress using highest level
zlib.gzip(assetTxt, { level: zlib.Z_BEST_COMPRESSION }, (err, Input) => {
if (!err) {
// Save the compressed file to .gz
route.set(path + '.gz', Input)
// Logging
var saved = ((assetTxt.length - Input.toString().length) / assetTxt.length * 100).toFixed(2)
if (options.logger) {
var log = hexo.log || console.log
log.log('Gzip-compressed %s [%s saved]', path, saved + '%')
}
resolve(assetTxt)
} else {
reject(err)
} }
}); })
});
}
function logic_js(str, data) {
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];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i], {matchBase: true})) return str;
} }
} })
})
//uglifyjs doesn't like unsupported options }))
delete options.enable;
delete options.exclude;
var js_logger = options.logger;
delete options.logger;
var result = UglifyJS.minify(str, options);
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
if (js_logger) {
var log = hexo.log || console.log;
log.log('Minify the js: %s [%s saved]', path, saved + '%');
}
return result.code;
} }
function logic_gzip() { function logicBrotli () {
var hexo = this, var hexo = this
options = hexo.config.neat_gzip;
// Return if disabled.
if (false === options.enable) return;
var gz_logger = options.logger; var options = hexo.config.neat_brotli
const route = hexo.route; // Return if disabled.
const routeList = route.list(); if (options.enable === false) return
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => { const route = hexo.route
return new Promise((resolve, reject) => { const routeList = route.list()
// Grab all assets using hexo router
const assetPath = route.get(path); return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
let assetTxt = ''; return new Promise((resolve, reject) => {
// Extract the content // Grab all assets using hexo router
assetPath.on('data', (chunk) => (assetTxt += chunk)); const assetPath = route.get(path)
assetPath.on('end', () => { let assetTxt = ''
if (assetTxt.length) { // Extract the content
// gzip compress using highest level assetPath.on('data', (chunk) => (assetTxt += chunk))
zlib.gzip(assetTxt, {level:zlib.Z_BEST_COMPRESSION}, (err, buffer) => { assetPath.on('end', () => {
if (!err) { if (assetTxt.length) {
// Save the compressed file to .gz // Input has to be buffer for brotli
route.set(path + '.gz', buffer); var input = new Buffer.from(assetTxt, 'utf-8')
//Logging // brotli compress using highest level
var saved = ((assetTxt.length - buffer.toString().length) / assetTxt.length * 100).toFixed(2); br.compress(input, { quality: br.BROTLI_MAX_QUALITY }, (err, output) => {
if (gz_logger) { if (!err) {
var log = hexo.log || console.log; // Save the compressed file to .br
log.log('Gzip-compressed %s [%s saved]', path, saved + '%'); route.set(path + '.br', output)
} // Logging
resolve(assetTxt); var saved = ((input.length - output.toString().length) / input.length * 100).toFixed(2)
} else { if (options.logger) {
reject(err); var log = hexo.log || console.log
log.log('Brotli-compressed %s [%s saved]', path, saved + '%')
} }
}); resolve(assetTxt)
} } else {
}); reject(err)
}); }
})); })
} }
})
function logic_brotli() { })
var hexo = this, }))
options = hexo.config.neat_brotli;
// Return if disabled.
if (false === options.enable) return;
var br_logger = options.logger;
const route = hexo.route;
const routeList = route.list();
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css'))).map(path => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
const assetPath = route.get(path);
let assetTxt = '';
// Extract the content
assetPath.on('data', (chunk) => (assetTxt += chunk));
assetPath.on('end', () => {
if (assetTxt.length) {
// Input has to be buffer for brotli
var buffer = new Buffer.from(assetTxt, "utf-8");
// brotli compress using highest level
br.compress(buffer, {quality:br.BROTLI_MAX_QUALITY}, (err, output) => {
if (!err) {
// Save the compressed file to .br
route.set(path + '.br', output);
//Logging
var saved = ((buffer.length - output.toString().length) / buffer.length * 100).toFixed(2);
if (br_logger) {
var log = hexo.log || console.log;
log.log('Brotli-compressed %s [%s saved]', path, saved + '%');
}
resolve(assetTxt);
} else {
reject(err);
}
});
}
});
});
}));
} }
module.exports = { module.exports = {
logic_html: logic_html, logicHtml: logicHtml,
logic_css: logic_css, logicCss: logicCss,
logic_js: logic_js, logicJs: logicJs,
logic_gzip: logic_gzip, logicGzip: logicGzip,
logic_brotli: logic_brotli logicBrotli: logicBrotli
}; }