curbengh 2019-09-25 01:14:37 +01:00
parent 90bfe2ca36
commit 60a045e429
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
1 changed files with 38 additions and 49 deletions

View File

@ -5,6 +5,8 @@ const CleanCSS = require('clean-css')
const Terser = require('terser') const Terser = require('terser')
const Svgo = require('svgo') const Svgo = require('svgo')
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util')
const gzip = promisify(zlib.gzip)
const br = require('iltorb') const br = require('iltorb')
const micromatch = require('micromatch') const micromatch = require('micromatch')
@ -25,7 +27,6 @@ function verbose (original, minified, path, ext) {
function minifyHtml (str, data) { function minifyHtml (str, data) {
const hexo = this const hexo = this
const options = hexo.config.minify.html const options = hexo.config.minify.html
// Return if disabled.
if (options.enable === false) return if (options.enable === false) return
const { path } = data const { path } = data
@ -44,7 +45,7 @@ function minifyHtml (str, data) {
return result return result
} }
function minifyCss (str, data) { async function minifyCss (str, data) {
const hexo = this const hexo = this
const options = hexo.config.minify.css const options = hexo.config.minify.css
if (options.enable === false) return if (options.enable === false) return
@ -58,13 +59,13 @@ function minifyCss (str, data) {
if (isMatch(path, exclude, globOptions)) return str if (isMatch(path, exclude, globOptions)) return str
return new Promise((resolve, reject) => { try {
new CleanCSS(options).minify(str, (err, result) => { const result = await new CleanCSS(options).minify(str)
if (err) return reject(err)
if (options.logger) verbose.call(this, str, result.styles, path, 'css') if (options.logger) verbose.call(this, str, result.styles, path, 'css')
resolve(result.styles) return result.styles
}) } catch (err) {
}) throw new Error(err)
}
} }
function minifyJs (str, data) { function minifyJs (str, data) {
@ -97,7 +98,6 @@ function minifyJs (str, data) {
function minifySvg () { function minifySvg () {
const hexo = this const hexo = this
const options = hexo.config.minify.svg const options = hexo.config.minify.svg
// Return if disabled.
if (options.enable === false) return if (options.enable === false) return
const { route } = hexo const { route } = hexo
@ -115,17 +115,16 @@ function minifySvg () {
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', async () => {
if (assetTxt.length) { if (assetTxt.length) {
// Minify using svgo try {
new Svgo(options).optimize(assetTxt).then((result) => { const result = await new Svgo(options).optimize(assetTxt)
// Replace the original file with the minified.
route.set(path, result.data)
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg') if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg')
resolve(route.set(path, result.data))
resolve(assetTxt) } catch (err) {
}) reject(err)
throw new Error(err)
}
} }
}) })
}) })
@ -135,7 +134,6 @@ function minifySvg () {
function gzipFn () { function gzipFn () {
const hexo = this const hexo = this
const options = hexo.config.minify.gzip const options = hexo.config.minify.gzip
// Return if disabled.
if (options.enable === false) return if (options.enable === false) return
const { route } = hexo const { route } = hexo
@ -153,21 +151,17 @@ function gzipFn () {
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', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try {
// gzip compress using highest level // gzip compress using highest level
zlib.gzip(assetTxt, { level: zlib.constants.Z_BEST_COMPRESSION }, (err, Input) => { const result = await gzip(assetTxt, { level: zlib.constants.Z_BEST_COMPRESSION })
if (!err) { if (options.logger) verbose.call(this, assetTxt, result.toString(), path, 'gzip')
// Save the compressed file to .gz resolve(route.set(path + '.gz', result))
route.set(path + '.gz', Input) } catch (err) {
if (options.logger) verbose.call(this, assetTxt, Input.toString(), path, 'gzip')
resolve(assetTxt)
} else {
reject(err) reject(err)
throw new Error(err)
} }
})
} }
}) })
}) })
@ -177,7 +171,6 @@ function gzipFn () {
function brotliFn () { function brotliFn () {
const hexo = this const hexo = this
const options = hexo.config.minify.brotli const options = hexo.config.minify.brotli
// Return if disabled.
if (options.enable === false) return if (options.enable === false) return
const { route } = hexo const { route } = hexo
@ -195,23 +188,19 @@ function brotliFn () {
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', async () => {
if (assetTxt.length) { if (assetTxt.length) {
// Input has to be buffer for brotli // Input has to be buffer
const input = Buffer.from(assetTxt, 'utf-8') 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)
if (options.logger) verbose.call(this, input, output.toString(), path, 'brotli') try {
const result = await br.compress(input)
resolve(assetTxt) if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
} else { resolve(route.set(path + '.br', result))
} catch (err) {
reject(err) reject(err)
throw new Error(err)
} }
})
} }
}) })
}) })