mirror of https://github.com/curbengh/hexo-yam
feat: register compression after minify
This commit is contained in:
parent
83484f0e47
commit
ac31b9edaa
10
README.md
10
README.md
|
@ -39,10 +39,11 @@ minify:
|
|||
css:
|
||||
js:
|
||||
svg:
|
||||
gzip:
|
||||
brotli:
|
||||
xml:
|
||||
json:
|
||||
gzip:
|
||||
brotli:
|
||||
zstd:
|
||||
```
|
||||
|
||||
- **enable** - Enable the plugin. Defaults to `true`.
|
||||
|
@ -51,10 +52,11 @@ minify:
|
|||
- **css** - See [CSS](#css) section
|
||||
- **js** - See [JS](#js) section
|
||||
- **svg** - See [SVG](#svg) section
|
||||
- **gzip** - See [Gzip](#gzip) section
|
||||
- **brotli** - See [Brotli](#brotli) section
|
||||
- **xml** - See [XML](#xml) section
|
||||
- **json** - See [JSON](#json) section
|
||||
- **gzip** - See [Gzip](#gzip) section
|
||||
- **brotli** - See [Brotli](#brotli) section
|
||||
- **zstd** - See [Zstd](#zstd) section
|
||||
|
||||
## HTML
|
||||
|
||||
|
|
68
index.js
68
index.js
|
@ -58,33 +58,6 @@ 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 = {
|
||||
enable: false,
|
||||
priority: 10,
|
||||
|
@ -99,11 +72,38 @@ hexo.config.minify.json = {
|
|||
enable: false,
|
||||
priority: 10,
|
||||
verbose: false,
|
||||
include: ['*.json', '!*.min.json'],
|
||||
include: ['*.json', '*.webmanifest', '!*.min.json', '!*.min.webmanifest'],
|
||||
globOptions: { basename: true },
|
||||
...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.html.enable === true) {
|
||||
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) {
|
||||
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) {
|
||||
const zlib = require('./lib/zlib')
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -11,13 +11,13 @@ async function minifyCss(str, data) {
|
|||
const options = hexo.config.minify.css
|
||||
if (!str) return str
|
||||
|
||||
const { path } = data
|
||||
const path = data.path
|
||||
const { exclude, globOptions, verbose } = options
|
||||
|
||||
if (isMatch(path, exclude, globOptions)) return str
|
||||
|
||||
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')
|
||||
return styles
|
||||
} catch (err) {
|
||||
|
|
|
@ -4,8 +4,8 @@ module.exports = {
|
|||
... require('./css'),
|
||||
... require('./js'),
|
||||
... require('./svg'),
|
||||
... require('./zlib'),
|
||||
... require('./zstd'),
|
||||
... require('./xml'),
|
||||
... require('./json'),
|
||||
... require('./zlib'),
|
||||
... require('./zstd')
|
||||
}
|
|
@ -11,7 +11,7 @@ async function minifyHtml(str, data) {
|
|||
const options = hexo.config.minify.html
|
||||
if (!str) return str
|
||||
|
||||
const { path } = data
|
||||
const path = data.path
|
||||
const { exclude, globOptions, verbose } = options
|
||||
|
||||
// Return if a path matches exclusion pattern
|
||||
|
|
10
lib/js.js
10
lib/js.js
|
@ -11,23 +11,19 @@ async function minifyJs(str, data) {
|
|||
const options = hexo.config.minify.js
|
||||
if (!str) return str
|
||||
|
||||
const { path } = data
|
||||
const { exclude, globOptions, verbose } = options
|
||||
const path = data.path
|
||||
const { exclude, globOptions, verbose, ...jsOptions } = options
|
||||
|
||||
if (isMatch(path, exclude, globOptions)) return str
|
||||
|
||||
// Terser doesn't like unsupported options
|
||||
const jsOptions = { ...options }
|
||||
delete jsOptions.enable
|
||||
delete jsOptions.priority
|
||||
delete jsOptions.verbose
|
||||
// Old option, retained to avoid crash when upgrading to v4
|
||||
delete jsOptions.logger
|
||||
delete jsOptions.exclude
|
||||
delete jsOptions.globOptions
|
||||
|
||||
try {
|
||||
const { code } = await terserMinify(str, jsOptions)
|
||||
const code = await terserMinify(str, jsOptions).code
|
||||
if (verbose) logFn.call(this, str, code, path, 'js')
|
||||
return code
|
||||
} catch (err) {
|
||||
|
|
|
@ -5,10 +5,10 @@ function minifyJson() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.json
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
/** @type {string[]} */
|
||||
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 new Promise((/** @type {(value: string) => void} */ resolve, reject) => {
|
||||
|
|
|
@ -6,12 +6,12 @@ function minifySvg() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.svg
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
/** @type {string[]} */
|
||||
const routeList = route.list()
|
||||
const { globOptions, include, verbose } = options
|
||||
// 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 = [{
|
||||
name: 'preset-default',
|
||||
params: {
|
||||
|
@ -27,7 +27,7 @@ function minifySvg() {
|
|||
assetPath.on('end', async () => {
|
||||
if (assetTxt.length) {
|
||||
try {
|
||||
const { data } = svgOptimize(assetTxt, { ...options, plugins })
|
||||
const data = svgOptimize(assetTxt, { ...options, plugins }).data
|
||||
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
|
||||
resolve(route.set(path, data))
|
||||
} catch (err) {
|
||||
|
|
|
@ -11,7 +11,7 @@ function isMatch(path = '', patterns = [], options = {}) {
|
|||
if (typeof patterns === 'string') patterns = [patterns]
|
||||
for (const pattern of patterns) {
|
||||
// disable basename if a pattern includes a slash
|
||||
let { basename } = options
|
||||
let basename = options.basename
|
||||
// only disable when basename is enabled
|
||||
basename = basename && !pattern.includes('/')
|
||||
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 (include.length) {
|
||||
for (const pattern of include) {
|
||||
let { basename } = options
|
||||
let basename = options.basename
|
||||
basename = basename && !pattern.includes('/')
|
||||
const tmp = micromatch(input, pattern, { ...options, basename })
|
||||
if (tmp.length) output.push(...tmp)
|
||||
|
|
|
@ -6,7 +6,7 @@ function minifyXml() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.xml
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
/** @type {string[]} */
|
||||
const routeList = route.list()
|
||||
const { globOptions, include, verbose } = options
|
||||
|
|
|
@ -9,11 +9,11 @@ function gzipFn() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.gzip
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
/** @type {string} */
|
||||
const routeList = route.list()
|
||||
const { globOptions, include, verbose } = options
|
||||
let { level } = options
|
||||
let level = options.level
|
||||
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION
|
||||
|
||||
return Promise.all((match(routeList, include, globOptions)).map(path => {
|
||||
|
@ -41,10 +41,10 @@ function brotliFn() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.brotli
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
const routeList = route.list()
|
||||
const { globOptions, include, verbose } = options
|
||||
let { level } = options
|
||||
let level = options.level
|
||||
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY
|
||||
|
||||
return Promise.all((match(routeList, include, globOptions)).map(path => {
|
||||
|
|
|
@ -6,11 +6,11 @@ function zstdFn() {
|
|||
const hexo = this
|
||||
const options = hexo.config.minify.zstd
|
||||
|
||||
const { route } = hexo
|
||||
const route = hexo.route
|
||||
/** @type {string[]} */
|
||||
const routeList = route.list()
|
||||
const { globOptions, include, verbose } = options
|
||||
let { level } = options
|
||||
let level = options.level
|
||||
if (typeof level !== 'number') level = undefined
|
||||
|
||||
return Promise.all((match(routeList, include, globOptions)).map(path => {
|
||||
|
|
|
@ -26,13 +26,13 @@
|
|||
"url": "git+https://github.com/curbengh/hexo-yam.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mongodb-js/zstd": "^1.2.0",
|
||||
"clean-css": "^5.1.2",
|
||||
"html-minifier-terser": "^7.2.0",
|
||||
"micromatch": "^4.0.2",
|
||||
"minify-xml": "^3.2.0",
|
||||
"svgo": "^3.0.0",
|
||||
"terser": "^5.3.0",
|
||||
"@mongodb-js/zstd": "^1.2.0"
|
||||
"terser": "^5.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"hexo": "^7.1.0",
|
||||
|
|
Loading…
Reference in New Issue