diff --git a/README.md b/README.md index 732469c..39efae3 100644 --- a/README.md +++ b/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 diff --git a/index.js b/index.js index 95d17ff..2b64661 100644 --- a/index.js +++ b/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) - } } \ No newline at end of file diff --git a/lib/css.js b/lib/css.js index 1029c68..5fe78e7 100644 --- a/lib/css.js +++ b/lib/css.js @@ -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) { diff --git a/lib/filter.js b/lib/filter.js index 45b1c42..9cc9099 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -4,8 +4,8 @@ module.exports = { ... require('./css'), ... require('./js'), ... require('./svg'), - ... require('./zlib'), - ... require('./zstd'), ... require('./xml'), ... require('./json'), + ... require('./zlib'), + ... require('./zstd') } \ No newline at end of file diff --git a/lib/html.js b/lib/html.js index 7ff5e12..bfcb758 100644 --- a/lib/html.js +++ b/lib/html.js @@ -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 diff --git a/lib/js.js b/lib/js.js index 6947b56..f32378c 100644 --- a/lib/js.js +++ b/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) { diff --git a/lib/json.js b/lib/json.js index 3464165..cd83658 100644 --- a/lib/json.js +++ b/lib/json.js @@ -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) => { diff --git a/lib/svg.js b/lib/svg.js index 59d7814..d1f60d6 100644 --- a/lib/svg.js +++ b/lib/svg.js @@ -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) { diff --git a/lib/tools.js b/lib/tools.js index d156c97..2268fd1 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -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) diff --git a/lib/xml.js b/lib/xml.js index 3a504fc..985e2df 100644 --- a/lib/xml.js +++ b/lib/xml.js @@ -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 diff --git a/lib/zlib.js b/lib/zlib.js index 6f5b510..0b26a29 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -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 => { diff --git a/lib/zstd.js b/lib/zstd.js index f5fe4f1..435381b 100644 --- a/lib/zstd.js +++ b/lib/zstd.js @@ -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 => { diff --git a/package.json b/package.json index 1ff3593..c1cebe9 100644 --- a/package.json +++ b/package.json @@ -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",