refactor: var to let/const

https://hacks.mozilla.org/2015/07/es6-in-depth-let-and-const/
This commit is contained in:
weyusi 2018-10-26 15:27:23 +10:30
parent 4c742451e2
commit 9c97c00aa4
3 changed files with 49 additions and 54 deletions

View File

@ -1,5 +1,5 @@
/* global hexo */ /* global hexo */
var assign = require('object-assign') const assign = require('object-assign')
if (hexo.config.neat_enable === true) { if (hexo.config.neat_enable === true) {
// HTML minifier // HTML minifier
@ -46,7 +46,7 @@ if (hexo.config.neat_enable === true) {
logger: false logger: false
}, hexo.config.neat_brotli) }, hexo.config.neat_brotli)
var filter = require('./lib/filter') const filter = require('./lib/filter')
hexo.extend.filter.register('after_render:html', filter.logicHtml) hexo.extend.filter.register('after_render:html', filter.logicHtml)
hexo.extend.filter.register('after_render:css', filter.logicCss) hexo.extend.filter.register('after_render:css', filter.logicCss)
hexo.extend.filter.register('after_render:js', filter.logicJs) hexo.extend.filter.register('after_render:js', filter.logicJs)

View File

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

View File

@ -1,7 +1,7 @@
{ {
"name": "hexo-yam", "name": "hexo-yam",
"description": "Yet Another Minifier. Minify and compress html, js and css", "description": "Yet Another Minifier. Minify and compress html, js and css",
"version": "1.0.1", "version": "1.0.2",
"readme": "README.md", "readme": "README.md",
"main": "index.js", "main": "index.js",
"directories": { "directories": {