From 0bee9ada0efdf81aa8abd4fe192865c8614259be Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sun, 28 Jan 2024 10:26:21 +0000 Subject: [PATCH] refactor: switch html-minifier to html-minifier-terser https://github.com/kangax/html-minifier/issues/1135 --- lib/filter.js | 6 ++--- package.json | 2 +- test/html.test.js | 56 ++++++++++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/lib/filter.js b/lib/filter.js index ac09d71..48b3df4 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,6 +1,6 @@ 'use strict' -const { minify: htmlMinify } = require('html-minifier') +const { minify: htmlMinify } = require('html-minifier-terser') const CleanCSS = require('clean-css') const { minify: terserMinify } = require('terser') const { optimize: svgOptimize } = require('svgo') @@ -59,7 +59,7 @@ function logFn (original, minified, path, ext) { log.log(`${ext}: ${path} [${saved}% saved]`) } -function minifyHtml (str, data) { +async function minifyHtml (str, data) { const hexo = this const options = hexo.config.minify.html if (options.enable === false || !str) return @@ -71,7 +71,7 @@ function minifyHtml (str, data) { if (isMatch(path, exclude, globOptions)) return str try { - const result = htmlMinify(str, options) + const result = await htmlMinify(str, options) if (verbose) logFn.call(this, str, result, path, 'html') return result diff --git a/package.json b/package.json index 4db1a23..482d003 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "repository": "curbengh/hexo-yam", "dependencies": { "clean-css": "^5.1.2", - "html-minifier": "^4.0.0", + "html-minifier-terser": "^7.2.0", "micromatch": "^4.0.2", "minify-xml": "^3.2.0", "svgo": "^3.0.0", diff --git a/test/html.test.js b/test/html.test.js index 3b48989..9c5b5e1 100644 --- a/test/html.test.js +++ b/test/html.test.js @@ -2,7 +2,7 @@ 'use strict' const Hexo = require('hexo') -const { minify: htmlMinify } = require('html-minifier') +const { minify: htmlMinify } = require('html-minifier-terser') describe('html', () => { const hexo = new Hexo(__dirname) @@ -26,105 +26,107 @@ describe('html', () => { globOptions: { basename: true } } } - const expected = htmlMinify(input, defaultCfg.html) + let expected = '' + + beforeAll(async () => { + expected = await htmlMinify(input, defaultCfg.html) + }) beforeEach(() => { hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg)) }) - test('default', () => { - const result = h(input, { path }) + test('default', async () => { + const result = await h(input, { path }) expect(result).toBe(expected) }) - test('disable', () => { + test('disable', async () => { hexo.config.minify.html.enable = false - const result = h(input, { path }) + const result = await h(input, { path }) expect(result).toBeUndefined() }) - test('empty file', () => { - const result = h('', { path }) + test('empty file', async () => { + const result = await h('', { path }) expect(result).toBeUndefined() }) - test('option', () => { + test('option', async () => { const customOpt = { removeEmptyAttributes: false } hexo.config.minify.html = customOpt - const result = h(input, { path }) - const expected = htmlMinify(input, customOpt) + const result = await h(input, { path }) + const expected = await htmlMinify(input, customOpt) expect(result).toBe(input) expect(result).toBe(expected) }) - test('option - verbose', () => { + test('option - verbose', async () => { hexo.config.minify.html.verbose = true hexo.log.log = jest.fn() - h(input, { path }) + await h(input, { path }) expect(hexo.log.log.mock.calls[0][0]).toContain(`html: ${path}`) }) - test('exclude', () => { + test('exclude', async () => { const exclude = '*.min.html' hexo.config.minify.html.exclude = exclude - const result = h(input, { path: 'foo/bar.min.html' }) + const result = await h(input, { path: 'foo/bar.min.html' }) expect(result).toBe(input) }) - test('exclude - slash in pattern', () => { + test('exclude - slash in pattern', async () => { const exclude = '**/lectus/**/*.html' hexo.config.minify.html.exclude = exclude - const result = h(input, { path: 'eleifend/lectus/nullam/dapibus/netus.html' }) + const result = await h(input, { path: 'eleifend/lectus/nullam/dapibus/netus.html' }) expect(result).toBe(input) }) - test('exclude - basename is true + slash', () => { + test('exclude - basename is true + slash', async () => { const exclude = ['**/lectus/**/*.html', 'bar.html'] const globOptions = { basename: true } hexo.config.minify.html.exclude = exclude hexo.config.minify.html.globOptions = globOptions - const result = h(input, { path: 'foo/bar.html' }) + const result = await h(input, { path: 'foo/bar.html' }) expect(result).toBe(input) }) - test('exclude - basename is false + slash', () => { + test('exclude - basename is false + slash', async () => { const exclude = ['**/lectus/**/*.html', 'bar.html'] const globOptions = { basename: false } hexo.config.minify.html.exclude = exclude hexo.config.minify.html.globOptions = globOptions - const result = h(input, { path: 'foo/bar.html' }) + const result = await h(input, { path: 'foo/bar.html' }) expect(result).toBe(expected) }) - test('null', () => { + test('null', async () => { hexo.config.minify.html.exclude = null hexo.config.minify.html.globOptions = null - const result = h(input, { path: null }) + const result = await h(input, { path: null }) expect(result).toBe(expected) }) - test('invalid string', () => { + test('invalid string', async () => { const invalid = '<>?:"{}|_+' - expect(() => { - h(invalid, { path }) - }).toThrow(`Path: ${path}\nError: Parse Error`) + await expect(h(invalid, { path })).rejects.toThrow('Parse Error: <>?:"{}|_+') }) })