2024-12-02 08:48:09 +00:00
|
|
|
'use strict'
|
|
|
|
const CleanCSS = require('clean-css')
|
2024-12-04 08:41:23 +00:00
|
|
|
const { isMatch, match, logFn } = require('./tools')
|
2024-12-02 08:48:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @param {{ path: string }} data
|
|
|
|
*/
|
|
|
|
async function minifyCss(str, data) {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.minify.css
|
|
|
|
if (!str) return str
|
|
|
|
|
2024-12-03 08:18:51 +00:00
|
|
|
const path = data.path
|
2024-12-02 08:48:09 +00:00
|
|
|
const { exclude, globOptions, verbose } = options
|
|
|
|
|
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
|
|
|
|
|
|
|
try {
|
2024-12-03 08:18:51 +00:00
|
|
|
const styles = await new CleanCSS(options).minify(str).styles
|
2024-12-02 08:48:09 +00:00
|
|
|
if (verbose) logFn.call(this, str, styles, path, 'css')
|
|
|
|
return styles
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Path: ${path}\n${err}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 08:41:23 +00:00
|
|
|
function minifyCssWithMap() {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.minify.css
|
|
|
|
const { parse } = require('path')
|
|
|
|
|
|
|
|
const route = hexo.route
|
|
|
|
const routeList = route.list()
|
|
|
|
/** @type {{ exclude: string[] }} */
|
|
|
|
const { exclude, globOptions, verbose } = options
|
|
|
|
const include = ['*.css', ...exclude.map(x => `!${x}`)]
|
|
|
|
const cleanCSS = new CleanCSS(options)
|
|
|
|
|
|
|
|
return Promise.all((match(routeList, include, globOptions)).map(path => {
|
|
|
|
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {
|
|
|
|
const assetPath = route.get(path)
|
|
|
|
let assetTxt = ''
|
|
|
|
assetPath.on('data', chunk => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', async () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
try {
|
|
|
|
const { base, ext, name } = parse(path)
|
|
|
|
const { styles, sourceMap } = await cleanCSS.minify(assetTxt)
|
|
|
|
if (verbose) logFn.call(this, assetTxt, result, path, 'css')
|
|
|
|
route.set(path, `${styles}\n/*# sourceMappingURL=${base}.map */`)
|
|
|
|
const map = sourceMap.toJSON()
|
|
|
|
map.sources = [`${name}.source${ext}`]
|
|
|
|
if (options.mapIncludeSources === true) {
|
|
|
|
map.sourcesContent = [assetTxt]
|
|
|
|
}
|
|
|
|
route.set(`${path}.map`, JSON.stringify(map))
|
|
|
|
} catch (err) {
|
|
|
|
reject(new Error(`Path: ${path}\n${err}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-12-02 08:48:09 +00:00
|
|
|
module.exports = {
|
2024-12-04 08:41:23 +00:00
|
|
|
minifyCss,
|
|
|
|
minifyCssWithMap
|
2024-12-02 08:48:09 +00:00
|
|
|
}
|