2018-10-25 09:35:59 +00:00
|
|
|
'use strict'
|
2019-05-27 01:14:04 +00:00
|
|
|
|
2020-09-05 11:22:36 +00:00
|
|
|
const { minify: htmlMinify } = require('html-minifier')
|
2018-10-26 04:57:23 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
2020-09-05 10:56:33 +00:00
|
|
|
const { minify: terserMinify } = 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-12-27 08:00:16 +00:00
|
|
|
const br = promisify(zlib.brotliCompress)
|
2020-09-02 11:23:20 +00:00
|
|
|
const { minify: compressXml } = require('minify-xml')
|
2019-07-10 02:20:23 +00:00
|
|
|
const micromatch = require('micromatch')
|
|
|
|
|
2019-12-26 14:44:42 +00:00
|
|
|
const isMatch = (path = '', patterns = [], options = {}) => {
|
|
|
|
if (path && patterns) {
|
|
|
|
if (path.length && patterns.length) {
|
|
|
|
if (typeof patterns === 'string') patterns = [patterns]
|
|
|
|
for (const pattern of patterns) {
|
|
|
|
// disable basename if a pattern includes a slash
|
|
|
|
let { basename } = options
|
|
|
|
// only disable when basename is enabled
|
|
|
|
basename = basename && !pattern.includes('/')
|
|
|
|
if (micromatch.isMatch(path, pattern, { ...options, basename })) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 02:20:23 +00:00
|
|
|
}
|
2019-12-26 14:44:42 +00:00
|
|
|
return false
|
2019-07-10 02:20:23 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const match = (paths = [], patterns = [], options = {}) => {
|
|
|
|
let input = paths
|
|
|
|
if (paths && patterns) {
|
|
|
|
if (paths.length && patterns.length) {
|
|
|
|
const output = []
|
|
|
|
if (typeof patterns === 'string') patterns = [patterns]
|
|
|
|
const exclude = patterns.filter((pattern) => pattern.startsWith('!'))
|
|
|
|
const include = patterns.filter((pattern) => !pattern.startsWith('!'))
|
|
|
|
if (exclude.length) input = micromatch(paths, exclude, options)
|
|
|
|
if (include.length) {
|
|
|
|
for (const pattern of include) {
|
|
|
|
let { basename } = options
|
|
|
|
basename = basename && !pattern.includes('/')
|
|
|
|
const tmp = micromatch(input, pattern, { ...options, basename })
|
|
|
|
if (tmp.length) output.push(...tmp)
|
|
|
|
}
|
|
|
|
return [...new Set(output)]
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:54:09 +00:00
|
|
|
function logFn (original, minified, path, ext) {
|
2019-09-15 15:51:15 +00:00
|
|
|
const saved = ((original.length - minified.length) / original.length * 100).toFixed(2)
|
|
|
|
const log = this.log || console
|
|
|
|
log.log(`${ext}: ${path} [${saved}% saved]`)
|
|
|
|
}
|
|
|
|
|
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
|
2020-01-02 12:49:29 +00:00
|
|
|
if (options.enable === false || !str) return
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
2019-12-27 22:54:09 +00:00
|
|
|
const { exclude, globOptions, verbose } = options
|
2018-10-25 09:35:59 +00:00
|
|
|
|
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
|
|
|
|
2020-05-25 08:13:15 +00:00
|
|
|
try {
|
2020-09-05 11:22:36 +00:00
|
|
|
const result = htmlMinify(str, options)
|
2020-05-25 08:13:15 +00:00
|
|
|
if (verbose) logFn.call(this, str, result, path, 'html')
|
2019-09-15 15:51:15 +00:00
|
|
|
|
2020-05-25 08:13:15 +00:00
|
|
|
return result
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
throw new Error(`Path: ${path}\n${err}`)
|
2020-05-25 08:13:15 +00:00
|
|
|
}
|
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
|
2020-01-02 12:49:29 +00:00
|
|
|
if (options.enable === false || !str) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
2019-12-27 22:54:09 +00:00
|
|
|
const { exclude, globOptions, verbose } = options
|
2016-06-22 11:15:47 +00:00
|
|
|
|
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 {
|
2019-11-09 07:16:07 +00:00
|
|
|
const { styles } = await new CleanCSS(options).minify(str)
|
2019-12-27 22:54:09 +00:00
|
|
|
if (verbose) logFn.call(this, str, styles, path, 'css')
|
2019-11-09 07:16:07 +00:00
|
|
|
return styles
|
2019-09-25 00:14:37 +00:00
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
throw new Error(`Path: ${path}\n${err}`)
|
2019-09-25 00:14:37 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
async 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
|
2020-01-02 12:49:29 +00:00
|
|
|
if (options.enable === false || !str) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-09-24 22:55:07 +00:00
|
|
|
const { path } = data
|
2019-12-27 22:54:09 +00:00
|
|
|
const { exclude, globOptions, verbose } = options
|
2016-05-26 11:09:41 +00:00
|
|
|
|
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
|
2019-11-09 07:18:59 +00:00
|
|
|
delete jsOptions.priority
|
2019-12-27 22:54:09 +00:00
|
|
|
delete jsOptions.verbose
|
|
|
|
// Old option, retained to avoid crash when upgrading to v4
|
2019-04-23 03:00:47 +00:00
|
|
|
delete jsOptions.logger
|
2019-11-09 07:18:59 +00:00
|
|
|
delete jsOptions.exclude
|
2019-07-10 02:20:23 +00:00
|
|
|
delete jsOptions.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
try {
|
|
|
|
const { code } = await terserMinify(str, jsOptions)
|
|
|
|
if (verbose) logFn.call(this, str, code, path, 'js')
|
|
|
|
return code
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
throw new Error(`Path: ${path}\n${err}`)
|
2020-09-05 10:56:33 +00:00
|
|
|
}
|
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-12-27 22:54:09 +00:00
|
|
|
const { globOptions, include, verbose } = options
|
2019-04-23 07:59:35 +00:00
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
return Promise.all((match(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-12-16 08:38:48 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += 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-12-27 03:31:33 +00:00
|
|
|
const { data } = await new Svgo(options).optimize(assetTxt)
|
2019-12-27 22:54:09 +00:00
|
|
|
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
|
2019-12-27 03:31:33 +00:00
|
|
|
resolve(route.set(path, data))
|
2019-09-25 00:14:37 +00:00
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
2019-09-25 00:14:37 +00:00
|
|
|
}
|
2019-04-23 07:59:35 +00:00
|
|
|
}
|
2020-01-02 12:49:29 +00:00
|
|
|
resolve()
|
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-12-27 22:54:09 +00:00
|
|
|
const { globOptions, include, verbose } = options
|
2019-12-27 07:50:03 +00:00
|
|
|
let { level } = options
|
|
|
|
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
return Promise.all((match(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-12-16 08:38:48 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += 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-12-27 08:00:16 +00:00
|
|
|
const result = await gzip(assetTxt, { level })
|
2019-12-27 22:54:09 +00:00
|
|
|
if (verbose) logFn.call(this, assetTxt, result, path, 'gzip')
|
2019-09-25 00:14:37 +00:00
|
|
|
resolve(route.set(path + '.gz', result))
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
2019-09-25 00:14:37 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
2020-01-02 12:49:29 +00:00
|
|
|
resolve()
|
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-12-27 22:54:09 +00:00
|
|
|
const { globOptions, include, verbose } = options
|
2019-12-27 07:50:03 +00:00
|
|
|
let { level } = options
|
|
|
|
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
return Promise.all((match(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-12-16 08:38:48 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += 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-12-27 08:00:16 +00:00
|
|
|
const result = await br(assetTxt, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
|
2019-12-27 22:54:09 +00:00
|
|
|
if (verbose) logFn.call(this, assetTxt, result, path, 'brotli')
|
2019-09-25 00:14:37 +00:00
|
|
|
resolve(route.set(path + '.br', result))
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
2019-09-25 00:14:37 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
2020-01-02 12:49:29 +00:00
|
|
|
resolve()
|
2018-10-25 09:35:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
2018-09-28 07:43:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 00:35:25 +00:00
|
|
|
function minifyXml () {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.minify.xml
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
|
|
|
const { route } = hexo
|
|
|
|
const routeList = route.list()
|
2020-09-02 11:23:20 +00:00
|
|
|
const { globOptions, include, verbose } = options
|
2020-01-03 00:35:25 +00:00
|
|
|
|
|
|
|
return Promise.all((match(routeList, include, globOptions)).map((path) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const assetPath = route.get(path)
|
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
try {
|
2020-09-02 11:23:20 +00:00
|
|
|
const result = compressXml(assetTxt, { ...options })
|
2020-01-03 00:35:25 +00:00
|
|
|
if (verbose) logFn.call(this, assetTxt, result, path, 'xml')
|
|
|
|
resolve(route.set(path, result))
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
2020-01-03 00:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:16:38 +00:00
|
|
|
function minifyJson () {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.minify.json
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
|
|
|
const { route } = hexo
|
|
|
|
const routeList = route.list()
|
|
|
|
const { globOptions, include, verbose } = options
|
|
|
|
|
|
|
|
return Promise.all((match(routeList, include, globOptions)).map((path) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const assetPath = route.get(path)
|
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
try {
|
|
|
|
const result = JSON.stringify(JSON.parse(assetTxt))
|
|
|
|
if (verbose) logFn.call(this, assetTxt, result, path, 'json')
|
|
|
|
resolve(route.set(path, result))
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
2020-01-03 01:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-05-26 11:09:41 +00:00
|
|
|
module.exports = {
|
2019-11-22 11:48:21 +00:00
|
|
|
minifyHtml,
|
|
|
|
minifyCss,
|
|
|
|
minifyJs,
|
|
|
|
minifySvg,
|
|
|
|
gzipFn,
|
2020-01-03 00:35:25 +00:00
|
|
|
brotliFn,
|
2020-01-03 01:16:38 +00:00
|
|
|
minifyXml,
|
|
|
|
minifyJson
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|