hexo-yam/lib/filter.js

229 lines
6.7 KiB
JavaScript
Raw Normal View History

'use strict'
2019-05-27 01:14:04 +00:00
const Htmlminifier = require('html-minifier').minify
const CleanCSS = require('clean-css')
const Terser = require('terser')
2019-07-10 02:20:23 +00:00
const Svgo = require('svgo')
const zlib = require('zlib')
const br = require('iltorb')
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-09-15 15:55:36 +00:00
function minifyHtml (str, data) {
const hexo = this
const options = hexo.config.minify.html
// Return if disabled.
if (options.enable === false) return
2019-09-24 22:55:07 +00:00
const { path } = data
const { exclude, globOptions } = options
let excludeString = exclude || ''
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
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')
return result
}
2016-05-26 11:09:41 +00:00
2019-09-15 15:55:36 +00:00
function minifyCss (str, data) {
const hexo = this
const options = hexo.config.minify.css
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
let excludeString = exclude || ''
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
return new Promise((resolve, reject) => {
new CleanCSS(options).minify(str, (err, result) => {
if (err) return reject(err)
2019-09-15 15:51:15 +00:00
if (options.logger) verbose.call(this, str, result.styles, path, 'css')
resolve(result.styles)
})
})
2016-05-26 11:09:41 +00:00
}
2019-09-15 15:55:36 +00:00
function minifyJs (str, data) {
const hexo = this
const options = hexo.config.minify.js
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
let excludeString = exclude || ''
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
// Terser doesn't like unsupported options
2019-04-23 04:05:59 +00:00
const jsOptions = Object.assign({}, options)
delete jsOptions.enable
delete jsOptions.exclude
delete jsOptions.logger
2019-07-10 02:20:23 +00:00
delete jsOptions.globOptions
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')
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
const options = hexo.config.minify.svg
2019-04-23 07:59:35 +00:00
// Return if disabled.
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
let includeString = include || ''
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) => {
// Grab all assets using hexo router
2019-08-06 02:27:44 +00:00
const assetPath = route.get(path)
2019-04-23 07:59:35 +00:00
let assetTxt = ''
// Extract the content
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', () => {
if (assetTxt.length) {
2019-04-23 08:27:52 +00:00
// Minify using svgo
2019-07-10 02:20:23 +00:00
new Svgo(options).optimize(assetTxt).then((result) => {
2019-05-02 06:25:43 +00:00
// Replace the original file with the minified.
route.set(path, result.data)
2019-09-15 15:51:15 +00:00
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg')
2019-05-02 06:25:43 +00:00
resolve(assetTxt)
2019-04-23 07:59:35 +00:00
})
}
})
})
}))
}
2019-09-15 15:55:36 +00:00
function gzipFn () {
const hexo = this
const options = hexo.config.minify.gzip
// Return if disabled.
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
let includeString = include || ''
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) => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
2019-08-06 02:27:44 +00:00
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.constants.Z_BEST_COMPRESSION }, (err, Input) => {
if (!err) {
// Save the compressed file to .gz
route.set(path + '.gz', Input)
2019-09-15 15:51:15 +00:00
if (options.logger) verbose.call(this, assetTxt, Input.toString(), path, 'gzip')
resolve(assetTxt)
} else {
reject(err)
}
})
}
})
})
}))
2018-09-30 07:30:32 +00:00
}
2019-09-15 15:55:36 +00:00
function brotliFn () {
const hexo = this
const options = hexo.config.minify.brotli
// Return if disabled.
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
let includeString = include || ''
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) => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
2019-08-06 02:27:44 +00:00
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
2019-08-06 02:27:44 +00:00
const input = Buffer.from(assetTxt, 'utf-8')
// brotli defaults to max compression level
br.compress(input, (err, output) => {
if (!err) {
// Save the compressed file to .br
route.set(path + '.br', output)
2019-09-15 15:51:15 +00:00
if (options.logger) verbose.call(this, input, output.toString(), path, 'brotli')
resolve(assetTxt)
} else {
reject(err)
}
})
}
})
})
}))
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
}