2018-10-25 09:35:59 +00:00
|
|
|
'use strict'
|
2019-05-27 01:14:04 +00:00
|
|
|
|
2019-04-23 08:16:56 +00:00
|
|
|
const Htmlminifier = require('html-minifier').minify
|
2018-10-26 04:57:23 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
2019-04-10 09:54:02 +00:00
|
|
|
const Terser = require('terser')
|
2019-07-10 02:20:23 +00:00
|
|
|
const Svgo = require('svgo')
|
2018-10-26 04:57:23 +00:00
|
|
|
const zlib = require('zlib')
|
2019-09-25 00:14:37 +00:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const gzip = promisify(zlib.gzip)
|
2019-09-25 00:33:47 +00:00
|
|
|
const br = require('zlib').brotliCompress ? promisify(require('zlib').brotliCompress) : require('iltorb').compress
|
2019-07-10 02:20:23 +00:00
|
|
|
const micromatch = require('micromatch')
|
|
|
|
|
2019-09-15 15:16:53 +00:00
|
|
|
const isMatch = (path, patterns, options) => {
|
2019-07-10 02:20:23 +00:00
|
|
|
if (path && patterns && patterns.length) {
|
|
|
|
return micromatch.isMatch(path, patterns, options)
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-15 15:51:15 +00:00
|
|
|
function verbose (original, minified, path, ext) {
|
|
|
|
const saved = ((original.length - minified.length) / original.length * 100).toFixed(2)
|
|
|
|
const log = this.log || console
|
|
|
|
log.log(`${ext}: ${path} [${saved}% saved]`)
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:27:25 +00:00
|
|
|
function error (msg) {
|
|
|
|
const log = this.log || console
|
|
|
|
log.error(msg)
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:55:36 +00:00
|
|
|
function minifyHtml (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.html
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
|
|
|
const { exclude, globOptions } = options
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let excludeString = exclude || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-04-23 04:05:59 +00:00
|
|
|
// Return if a path matches exclusion pattern
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const result = Htmlminifier(str, options)
|
2019-09-15 15:51:15 +00:00
|
|
|
if (options.logger) verbose.call(this, str, result, path, 'html')
|
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
return result
|
2018-09-29 05:38:45 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-25 00:14:37 +00:00
|
|
|
async function minifyCss (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.css
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
|
|
|
const { exclude, globOptions } = options
|
2016-06-22 11:15:47 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let excludeString = exclude || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString && excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-09-25 00:14:37 +00:00
|
|
|
try {
|
|
|
|
const result = await new CleanCSS(options).minify(str)
|
|
|
|
if (options.logger) verbose.call(this, str, result.styles, path, 'css')
|
|
|
|
return result.styles
|
|
|
|
} catch (err) {
|
2019-10-06 05:33:35 +00:00
|
|
|
error.call(this, err)
|
2019-09-25 00:14:37 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 15:55:36 +00:00
|
|
|
function minifyJs (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.js
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
|
|
|
const { exclude, globOptions } = options
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let excludeString = exclude || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString && excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-04-10 09:54:02 +00:00
|
|
|
// Terser doesn't like unsupported options
|
2019-04-23 04:05:59 +00:00
|
|
|
const jsOptions = Object.assign({}, options)
|
2019-04-23 03:00:47 +00:00
|
|
|
delete jsOptions.enable
|
|
|
|
delete jsOptions.exclude
|
|
|
|
delete jsOptions.logger
|
2019-07-10 02:20:23 +00:00
|
|
|
delete jsOptions.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const result = Terser.minify(str, jsOptions)
|
2019-09-15 15:51:15 +00:00
|
|
|
if (options.logger) verbose.call(this, str, result.code, path, 'js')
|
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
return result.code
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 15:55:36 +00:00
|
|
|
function minifySvg () {
|
2019-04-23 07:59:35 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.svg
|
2019-04-23 07:59:35 +00:00
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { route } = hexo
|
2019-08-06 02:27:44 +00:00
|
|
|
const routeList = route.list()
|
2019-09-24 22:55:07 +00:00
|
|
|
const { globOptions, include } = options
|
2019-04-23 07:59:35 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let includeString = include || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
|
2019-04-23 07:59:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2019-10-06 05:32:35 +00:00
|
|
|
const assetTxt = []
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
|
2019-09-25 00:14:37 +00:00
|
|
|
assetPath.on('end', async () => {
|
2019-04-23 07:59:35 +00:00
|
|
|
if (assetTxt.length) {
|
2019-09-25 00:14:37 +00:00
|
|
|
try {
|
2019-10-10 07:04:56 +00:00
|
|
|
const result = await new Svgo(options).optimize(assetTxt.join(''))
|
2019-10-06 08:24:25 +00:00
|
|
|
if (options.logger) verbose.call(this, assetTxt.join().toString(), result.data, path, 'svg')
|
2019-09-25 00:14:37 +00:00
|
|
|
resolve(route.set(path, result.data))
|
|
|
|
} catch (err) {
|
2019-10-06 05:32:35 +00:00
|
|
|
error.call(this, err)
|
2019-09-25 00:14:37 +00:00
|
|
|
reject(err)
|
|
|
|
}
|
2019-04-23 07:59:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:55:36 +00:00
|
|
|
function gzipFn () {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.gzip
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { route } = hexo
|
2019-08-06 02:27:44 +00:00
|
|
|
const routeList = route.list()
|
2019-09-24 22:55:07 +00:00
|
|
|
const { globOptions, include } = options
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let includeString = include || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2019-10-06 05:32:35 +00:00
|
|
|
const assetTxt = []
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
|
2019-09-25 00:14:37 +00:00
|
|
|
assetPath.on('end', async () => {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (assetTxt.length) {
|
2019-09-25 00:14:37 +00:00
|
|
|
try {
|
2019-10-06 08:24:25 +00:00
|
|
|
// TODO: Drop Buffer
|
2019-10-10 07:04:56 +00:00
|
|
|
const input = Buffer.from(assetTxt.join(''), 'utf-8')
|
2019-10-06 05:32:35 +00:00
|
|
|
const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION })
|
|
|
|
if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip')
|
2019-09-25 00:14:37 +00:00
|
|
|
resolve(route.set(path + '.gz', result))
|
|
|
|
} catch (err) {
|
2019-10-06 05:33:35 +00:00
|
|
|
error.call(this, err)
|
2019-09-25 00:14:37 +00:00
|
|
|
reject(err)
|
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 15:55:36 +00:00
|
|
|
function brotliFn () {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
2019-09-11 01:15:19 +00:00
|
|
|
const options = hexo.config.minify.brotli
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { route } = hexo
|
2019-08-06 02:27:44 +00:00
|
|
|
const routeList = route.list()
|
2019-09-24 22:55:07 +00:00
|
|
|
const { globOptions, include } = options
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-09 02:18:08 +00:00
|
|
|
let includeString = include || ''
|
2019-08-06 01:56:11 +00:00
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2019-10-06 05:32:35 +00:00
|
|
|
const assetTxt = []
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
|
2019-09-25 00:14:37 +00:00
|
|
|
assetPath.on('end', async () => {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (assetTxt.length) {
|
2019-09-25 00:14:37 +00:00
|
|
|
try {
|
2019-10-10 07:04:56 +00:00
|
|
|
const input = Buffer.from(assetTxt.join(''), 'utf-8')
|
2019-09-25 00:33:47 +00:00
|
|
|
const result = await br(input)
|
2019-09-25 00:14:37 +00:00
|
|
|
if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
|
|
|
|
resolve(route.set(path + '.br', result))
|
|
|
|
} catch (err) {
|
2019-10-06 05:33:35 +00:00
|
|
|
error.call(this, err)
|
2019-09-25 00:14:37 +00:00
|
|
|
reject(err)
|
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
2018-09-28 07:43:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 11:09:41 +00:00
|
|
|
module.exports = {
|
2019-09-15 15:55:36 +00:00
|
|
|
minifyHtml: minifyHtml,
|
|
|
|
minifyCss: minifyCss,
|
|
|
|
minifyJs: minifyJs,
|
|
|
|
minifySvg: minifySvg,
|
|
|
|
gzipFn: gzipFn,
|
|
|
|
brotliFn: brotliFn
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|