2024-12-02 08:48:09 +00:00
|
|
|
'use strict'
|
|
|
|
const { optimize: svgOptimize } = require('svgo')
|
|
|
|
const { match, logFn } = require('./tools')
|
|
|
|
|
|
|
|
function minifySvg() {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.minify.svg
|
|
|
|
|
2024-12-03 08:18:51 +00:00
|
|
|
const route = hexo.route
|
2024-12-02 08:48:09 +00:00
|
|
|
/** @type {string[]} */
|
|
|
|
const routeList = route.list()
|
|
|
|
const { globOptions, include, verbose } = options
|
|
|
|
// const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([])
|
2024-12-03 08:18:51 +00:00
|
|
|
const pluginCfg = typeof options.plugins === 'object' ? { ...options.plugins } : {}
|
2024-12-02 08:48:09 +00:00
|
|
|
const plugins = [{
|
|
|
|
name: 'preset-default',
|
|
|
|
params: {
|
|
|
|
overrides: pluginCfg
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
|
|
|
|
return Promise.all((match(routeList, include, globOptions)).map(path => {
|
2024-12-03 09:26:45 +00:00
|
|
|
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {
|
2024-12-02 08:48:09 +00:00
|
|
|
const assetPath = route.get(path)
|
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', chunk => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', async () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
try {
|
2024-12-03 08:18:51 +00:00
|
|
|
const data = svgOptimize(assetTxt, { ...options, plugins }).data
|
2024-12-02 08:48:09 +00:00
|
|
|
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
|
2024-12-03 09:26:45 +00:00
|
|
|
route.set(path, data)
|
2024-12-02 08:48:09 +00:00
|
|
|
} catch (err) {
|
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
|
|
|
}
|
|
|
|
}
|
2024-12-03 09:26:45 +00:00
|
|
|
resolve()
|
2024-12-02 08:48:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
minifySvg
|
|
|
|
}
|