mirror of https://github.com/curbengh/hexo-yam
refactor: async/await
- https://dev.to/gafi/7-reasons-to-always-use-async-await-over-plain-promises-tutorial-4ej9 - https://stackoverflow.com/a/56975197
This commit is contained in:
parent
90bfe2ca36
commit
60a045e429
|
@ -5,6 +5,8 @@ const CleanCSS = require('clean-css')
|
|||
const Terser = require('terser')
|
||||
const Svgo = require('svgo')
|
||||
const zlib = require('zlib')
|
||||
const { promisify } = require('util')
|
||||
const gzip = promisify(zlib.gzip)
|
||||
const br = require('iltorb')
|
||||
const micromatch = require('micromatch')
|
||||
|
||||
|
@ -25,7 +27,6 @@ function verbose (original, minified, path, ext) {
|
|||
function minifyHtml (str, data) {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.html
|
||||
// Return if disabled.
|
||||
if (options.enable === false) return
|
||||
|
||||
const { path } = data
|
||||
|
@ -44,7 +45,7 @@ function minifyHtml (str, data) {
|
|||
return result
|
||||
}
|
||||
|
||||
function minifyCss (str, data) {
|
||||
async function minifyCss (str, data) {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.css
|
||||
if (options.enable === false) return
|
||||
|
@ -58,13 +59,13 @@ function minifyCss (str, data) {
|
|||
|
||||
if (isMatch(path, exclude, globOptions)) return str
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
new CleanCSS(options).minify(str, (err, result) => {
|
||||
if (err) return reject(err)
|
||||
try {
|
||||
const result = await new CleanCSS(options).minify(str)
|
||||
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) {
|
||||
|
@ -97,7 +98,6 @@ function minifyJs (str, data) {
|
|||
function minifySvg () {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.svg
|
||||
// Return if disabled.
|
||||
if (options.enable === false) return
|
||||
|
||||
const { route } = hexo
|
||||
|
@ -115,17 +115,16 @@ function minifySvg () {
|
|||
let assetTxt = ''
|
||||
// Extract the content
|
||||
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
||||
assetPath.on('end', () => {
|
||||
assetPath.on('end', async () => {
|
||||
if (assetTxt.length) {
|
||||
// Minify using svgo
|
||||
new Svgo(options).optimize(assetTxt).then((result) => {
|
||||
// Replace the original file with the minified.
|
||||
route.set(path, result.data)
|
||||
|
||||
try {
|
||||
const result = await new Svgo(options).optimize(assetTxt)
|
||||
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg')
|
||||
|
||||
resolve(assetTxt)
|
||||
})
|
||||
resolve(route.set(path, result.data))
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -135,7 +134,6 @@ function minifySvg () {
|
|||
function gzipFn () {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.gzip
|
||||
// Return if disabled.
|
||||
if (options.enable === false) return
|
||||
|
||||
const { route } = hexo
|
||||
|
@ -153,21 +151,17 @@ function gzipFn () {
|
|||
let assetTxt = ''
|
||||
// Extract the content
|
||||
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
||||
assetPath.on('end', () => {
|
||||
assetPath.on('end', async () => {
|
||||
if (assetTxt.length) {
|
||||
try {
|
||||
// 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)
|
||||
|
||||
if (options.logger) verbose.call(this, assetTxt, Input.toString(), path, 'gzip')
|
||||
|
||||
resolve(assetTxt)
|
||||
} else {
|
||||
const result = await gzip(assetTxt, { level: zlib.constants.Z_BEST_COMPRESSION })
|
||||
if (options.logger) verbose.call(this, assetTxt, result.toString(), path, 'gzip')
|
||||
resolve(route.set(path + '.gz', result))
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -177,7 +171,6 @@ function gzipFn () {
|
|||
function brotliFn () {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.brotli
|
||||
// Return if disabled.
|
||||
if (options.enable === false) return
|
||||
|
||||
const { route } = hexo
|
||||
|
@ -195,23 +188,19 @@ function brotliFn () {
|
|||
let assetTxt = ''
|
||||
// Extract the content
|
||||
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
||||
assetPath.on('end', () => {
|
||||
assetPath.on('end', async () => {
|
||||
if (assetTxt.length) {
|
||||
// Input has to be buffer for brotli
|
||||
// Input has to be buffer
|
||||
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')
|
||||
|
||||
resolve(assetTxt)
|
||||
} else {
|
||||
try {
|
||||
const result = await br.compress(input)
|
||||
if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
|
||||
resolve(route.set(path + '.br', result))
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue