feat: register compression after minify

This commit is contained in:
wherewhere 2024-12-03 16:18:51 +08:00
parent 83484f0e47
commit ac31b9edaa
13 changed files with 64 additions and 66 deletions

View File

@ -39,10 +39,11 @@ minify:
css: css:
js: js:
svg: svg:
gzip:
brotli:
xml: xml:
json: json:
gzip:
brotli:
zstd:
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
@ -51,10 +52,11 @@ minify:
- **css** - See [CSS](#css) section - **css** - See [CSS](#css) section
- **js** - See [JS](#js) section - **js** - See [JS](#js) section
- **svg** - See [SVG](#svg) section - **svg** - See [SVG](#svg) section
- **gzip** - See [Gzip](#gzip) section
- **brotli** - See [Brotli](#brotli) section
- **xml** - See [XML](#xml) section - **xml** - See [XML](#xml) section
- **json** - See [JSON](#json) section - **json** - See [JSON](#json) section
- **gzip** - See [Gzip](#gzip) section
- **brotli** - See [Brotli](#brotli) section
- **zstd** - See [Zstd](#zstd) section
## HTML ## HTML

View File

@ -58,33 +58,6 @@ hexo.config.minify.svg = {
...hexo.config.minify.svg ...hexo.config.minify.svg
} }
hexo.config.minify.gzip = {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true },
...hexo.config.minify.gzip
}
hexo.config.minify.brotli = {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true },
...hexo.config.minify.brotli
}
hexo.config.minify.zstd = {
enable: false,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true },
...hexo.config.minify.zstd
}
hexo.config.minify.xml = { hexo.config.minify.xml = {
enable: false, enable: false,
priority: 10, priority: 10,
@ -99,11 +72,38 @@ hexo.config.minify.json = {
enable: false, enable: false,
priority: 10, priority: 10,
verbose: false, verbose: false,
include: ['*.json', '!*.min.json'], include: ['*.json', '*.webmanifest', '!*.min.json', '!*.min.webmanifest'],
globOptions: { basename: true }, globOptions: { basename: true },
...hexo.config.minify.json ...hexo.config.minify.json
} }
hexo.config.minify.gzip = {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json', '*.webmanifest'],
globOptions: { basename: true },
...hexo.config.minify.gzip
}
hexo.config.minify.brotli = {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json', '*.webmanifest'],
globOptions: { basename: true },
...hexo.config.minify.brotli
}
hexo.config.minify.zstd = {
enable: false,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json', '*.webmanifest'],
globOptions: { basename: true },
...hexo.config.minify.zstd
}
if (hexo.config.minify.enable === true && !(hexo.config.minify.previewServer === true && ['s', 'server'].includes(hexo.env.cmd))) { if (hexo.config.minify.enable === true && !(hexo.config.minify.previewServer === true && ['s', 'server'].includes(hexo.env.cmd))) {
if (hexo.config.minify.html.enable === true) { if (hexo.config.minify.html.enable === true) {
hexo.extend.filter.register('after_render:html', require('./lib/html').minifyHtml, hexo.config.minify.html.priority) hexo.extend.filter.register('after_render:html', require('./lib/html').minifyHtml, hexo.config.minify.html.priority)
@ -117,6 +117,12 @@ if (hexo.config.minify.enable === true && !(hexo.config.minify.previewServer ===
if (hexo.config.minify.svg.enable === true) { if (hexo.config.minify.svg.enable === true) {
hexo.extend.filter.register('after_generate', require('./lib/svg').minifySvg, hexo.config.minify.svg.priority) hexo.extend.filter.register('after_generate', require('./lib/svg').minifySvg, hexo.config.minify.svg.priority)
} }
if (hexo.config.minify.xml.enable === true) {
hexo.extend.filter.register('after_generate', require('./lib/xml').minifyXml, hexo.config.minify.xml.priority)
}
if (hexo.config.minify.json.enable === true) {
hexo.extend.filter.register('after_generate', require('./lib/json').minifyJson, hexo.config.minify.json.priority)
}
if (hexo.config.minify.gzip.enable || hexo.config.minify.brotli.enable) { if (hexo.config.minify.gzip.enable || hexo.config.minify.brotli.enable) {
const zlib = require('./lib/zlib') const zlib = require('./lib/zlib')
if (hexo.config.minify.gzip.enable === true) { if (hexo.config.minify.gzip.enable === true) {
@ -134,10 +140,4 @@ if (hexo.config.minify.enable === true && !(hexo.config.minify.previewServer ===
log.warn(`ZSTD load failed. ${ex}`) log.warn(`ZSTD load failed. ${ex}`)
} }
} }
if (hexo.config.minify.xml.enable === true) {
hexo.extend.filter.register('after_generate', require('./lib/xml').minifyXml, hexo.config.minify.xml.priority)
}
if (hexo.config.minify.json.enable === true) {
hexo.extend.filter.register('after_generate', require('./lib/json').minifyJson, hexo.config.minify.json.priority)
}
} }

View File

@ -11,13 +11,13 @@ async function minifyCss(str, data) {
const options = hexo.config.minify.css const options = hexo.config.minify.css
if (!str) return str if (!str) return str
const { path } = data const path = data.path
const { exclude, globOptions, verbose } = options const { exclude, globOptions, verbose } = options
if (isMatch(path, exclude, globOptions)) return str if (isMatch(path, exclude, globOptions)) return str
try { try {
const { styles } = await new CleanCSS(options).minify(str) const styles = await new CleanCSS(options).minify(str).styles
if (verbose) logFn.call(this, str, styles, path, 'css') if (verbose) logFn.call(this, str, styles, path, 'css')
return styles return styles
} catch (err) { } catch (err) {

View File

@ -4,8 +4,8 @@ module.exports = {
... require('./css'), ... require('./css'),
... require('./js'), ... require('./js'),
... require('./svg'), ... require('./svg'),
... require('./zlib'),
... require('./zstd'),
... require('./xml'), ... require('./xml'),
... require('./json'), ... require('./json'),
... require('./zlib'),
... require('./zstd')
} }

View File

@ -11,7 +11,7 @@ async function minifyHtml(str, data) {
const options = hexo.config.minify.html const options = hexo.config.minify.html
if (!str) return str if (!str) return str
const { path } = data const path = data.path
const { exclude, globOptions, verbose } = options const { exclude, globOptions, verbose } = options
// Return if a path matches exclusion pattern // Return if a path matches exclusion pattern

View File

@ -11,23 +11,19 @@ async function minifyJs(str, data) {
const options = hexo.config.minify.js const options = hexo.config.minify.js
if (!str) return str if (!str) return str
const { path } = data const path = data.path
const { exclude, globOptions, verbose } = options const { exclude, globOptions, verbose, ...jsOptions } = options
if (isMatch(path, exclude, globOptions)) return str if (isMatch(path, exclude, globOptions)) return str
// Terser doesn't like unsupported options // Terser doesn't like unsupported options
const jsOptions = { ...options }
delete jsOptions.enable delete jsOptions.enable
delete jsOptions.priority delete jsOptions.priority
delete jsOptions.verbose
// Old option, retained to avoid crash when upgrading to v4 // Old option, retained to avoid crash when upgrading to v4
delete jsOptions.logger delete jsOptions.logger
delete jsOptions.exclude
delete jsOptions.globOptions
try { try {
const { code } = await terserMinify(str, jsOptions) const code = await terserMinify(str, jsOptions).code
if (verbose) logFn.call(this, str, code, path, 'js') if (verbose) logFn.call(this, str, code, path, 'js')
return code return code
} catch (err) { } catch (err) {

View File

@ -5,10 +5,10 @@ function minifyJson() {
const hexo = this const hexo = this
const options = hexo.config.minify.json const options = hexo.config.minify.json
const { route } = hexo const route = hexo.route
/** @type {string[]} */ /** @type {string[]} */
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose, useBigInt } = options
return Promise.all((match(routeList, include, globOptions)).map(path => { return Promise.all((match(routeList, include, globOptions)).map(path => {
return new Promise((/** @type {(value: string) => void} */ resolve, reject) => { return new Promise((/** @type {(value: string) => void} */ resolve, reject) => {

View File

@ -6,12 +6,12 @@ function minifySvg() {
const hexo = this const hexo = this
const options = hexo.config.minify.svg const options = hexo.config.minify.svg
const { route } = hexo const route = hexo.route
/** @type {string[]} */ /** @type {string[]} */
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options
// const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([]) // const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([])
const pluginCfg = Object.prototype.toString.call(options.plugins) === '[object Object]' ? { ...options.plugins } : {} const pluginCfg = typeof options.plugins === 'object' ? { ...options.plugins } : {}
const plugins = [{ const plugins = [{
name: 'preset-default', name: 'preset-default',
params: { params: {
@ -27,7 +27,7 @@ function minifySvg() {
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
const { data } = svgOptimize(assetTxt, { ...options, plugins }) const data = svgOptimize(assetTxt, { ...options, plugins }).data
if (verbose) logFn.call(this, assetTxt, data, path, 'svg') if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
resolve(route.set(path, data)) resolve(route.set(path, data))
} catch (err) { } catch (err) {

View File

@ -11,7 +11,7 @@ function isMatch(path = '', patterns = [], options = {}) {
if (typeof patterns === 'string') patterns = [patterns] if (typeof patterns === 'string') patterns = [patterns]
for (const pattern of patterns) { for (const pattern of patterns) {
// disable basename if a pattern includes a slash // disable basename if a pattern includes a slash
let { basename } = options let basename = options.basename
// only disable when basename is enabled // only disable when basename is enabled
basename = basename && !pattern.includes('/') basename = basename && !pattern.includes('/')
if (micromatch.isMatch(path, pattern, { ...options, basename })) { if (micromatch.isMatch(path, pattern, { ...options, basename })) {
@ -40,7 +40,7 @@ function match(paths = [], patterns = [], options = {}) {
if (exclude.length) input = micromatch(paths, exclude, options) if (exclude.length) input = micromatch(paths, exclude, options)
if (include.length) { if (include.length) {
for (const pattern of include) { for (const pattern of include) {
let { basename } = options let basename = options.basename
basename = basename && !pattern.includes('/') basename = basename && !pattern.includes('/')
const tmp = micromatch(input, pattern, { ...options, basename }) const tmp = micromatch(input, pattern, { ...options, basename })
if (tmp.length) output.push(...tmp) if (tmp.length) output.push(...tmp)

View File

@ -6,7 +6,7 @@ function minifyXml() {
const hexo = this const hexo = this
const options = hexo.config.minify.xml const options = hexo.config.minify.xml
const { route } = hexo const route = hexo.route
/** @type {string[]} */ /** @type {string[]} */
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options

View File

@ -9,11 +9,11 @@ function gzipFn() {
const hexo = this const hexo = this
const options = hexo.config.minify.gzip const options = hexo.config.minify.gzip
const { route } = hexo const route = hexo.route
/** @type {string} */ /** @type {string} */
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options
let { level } = options let level = options.level
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION
return Promise.all((match(routeList, include, globOptions)).map(path => { return Promise.all((match(routeList, include, globOptions)).map(path => {
@ -41,10 +41,10 @@ function brotliFn() {
const hexo = this const hexo = this
const options = hexo.config.minify.brotli const options = hexo.config.minify.brotli
const { route } = hexo const route = hexo.route
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options
let { level } = options let level = options.level
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY
return Promise.all((match(routeList, include, globOptions)).map(path => { return Promise.all((match(routeList, include, globOptions)).map(path => {

View File

@ -6,11 +6,11 @@ function zstdFn() {
const hexo = this const hexo = this
const options = hexo.config.minify.zstd const options = hexo.config.minify.zstd
const { route } = hexo const route = hexo.route
/** @type {string[]} */ /** @type {string[]} */
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options
let { level } = options let level = options.level
if (typeof level !== 'number') level = undefined if (typeof level !== 'number') level = undefined
return Promise.all((match(routeList, include, globOptions)).map(path => { return Promise.all((match(routeList, include, globOptions)).map(path => {

View File

@ -26,13 +26,13 @@
"url": "git+https://github.com/curbengh/hexo-yam.git" "url": "git+https://github.com/curbengh/hexo-yam.git"
}, },
"dependencies": { "dependencies": {
"@mongodb-js/zstd": "^1.2.0",
"clean-css": "^5.1.2", "clean-css": "^5.1.2",
"html-minifier-terser": "^7.2.0", "html-minifier-terser": "^7.2.0",
"micromatch": "^4.0.2", "micromatch": "^4.0.2",
"minify-xml": "^3.2.0", "minify-xml": "^3.2.0",
"svgo": "^3.0.0", "svgo": "^3.0.0",
"terser": "^5.3.0", "terser": "^5.3.0"
"@mongodb-js/zstd": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"hexo": "^7.1.0", "hexo": "^7.1.0",