2024-12-02 08:48:09 +00:00
|
|
|
'use strict'
|
|
|
|
const CleanCSS = require('clean-css')
|
|
|
|
const { isMatch, logFn } = require('./tools')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
minifyCss
|
|
|
|
}
|