refactor: switch html-minifier to html-minifier-terser

https://github.com/kangax/html-minifier/issues/1135
This commit is contained in:
MDLeom 2024-01-28 10:26:21 +00:00
parent 7809098034
commit 0bee9ada0e
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
3 changed files with 33 additions and 31 deletions

View File

@ -1,6 +1,6 @@
'use strict' 'use strict'
const { minify: htmlMinify } = require('html-minifier') const { minify: htmlMinify } = require('html-minifier-terser')
const CleanCSS = require('clean-css') const CleanCSS = require('clean-css')
const { minify: terserMinify } = require('terser') const { minify: terserMinify } = require('terser')
const { optimize: svgOptimize } = require('svgo') const { optimize: svgOptimize } = require('svgo')
@ -59,7 +59,7 @@ function logFn (original, minified, path, ext) {
log.log(`${ext}: ${path} [${saved}% saved]`) log.log(`${ext}: ${path} [${saved}% saved]`)
} }
function minifyHtml (str, data) { async function minifyHtml (str, data) {
const hexo = this const hexo = this
const options = hexo.config.minify.html const options = hexo.config.minify.html
if (options.enable === false || !str) return if (options.enable === false || !str) return
@ -71,7 +71,7 @@ function minifyHtml (str, data) {
if (isMatch(path, exclude, globOptions)) return str if (isMatch(path, exclude, globOptions)) return str
try { try {
const result = htmlMinify(str, options) const result = await htmlMinify(str, options)
if (verbose) logFn.call(this, str, result, path, 'html') if (verbose) logFn.call(this, str, result, path, 'html')
return result return result

View File

@ -24,7 +24,7 @@
"repository": "curbengh/hexo-yam", "repository": "curbengh/hexo-yam",
"dependencies": { "dependencies": {
"clean-css": "^5.1.2", "clean-css": "^5.1.2",
"html-minifier": "^4.0.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",

View File

@ -2,7 +2,7 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const { minify: htmlMinify } = require('html-minifier') const { minify: htmlMinify } = require('html-minifier-terser')
describe('html', () => { describe('html', () => {
const hexo = new Hexo(__dirname) const hexo = new Hexo(__dirname)
@ -26,105 +26,107 @@ describe('html', () => {
globOptions: { basename: true } globOptions: { basename: true }
} }
} }
const expected = htmlMinify(input, defaultCfg.html) let expected = ''
beforeAll(async () => {
expected = await htmlMinify(input, defaultCfg.html)
})
beforeEach(() => { beforeEach(() => {
hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg)) hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg))
}) })
test('default', () => { test('default', async () => {
const result = h(input, { path }) const result = await h(input, { path })
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
test('disable', () => { test('disable', async () => {
hexo.config.minify.html.enable = false hexo.config.minify.html.enable = false
const result = h(input, { path }) const result = await h(input, { path })
expect(result).toBeUndefined() expect(result).toBeUndefined()
}) })
test('empty file', () => { test('empty file', async () => {
const result = h('', { path }) const result = await h('', { path })
expect(result).toBeUndefined() expect(result).toBeUndefined()
}) })
test('option', () => { test('option', async () => {
const customOpt = { removeEmptyAttributes: false } const customOpt = { removeEmptyAttributes: false }
hexo.config.minify.html = customOpt hexo.config.minify.html = customOpt
const result = h(input, { path }) const result = await h(input, { path })
const expected = htmlMinify(input, customOpt) const expected = await htmlMinify(input, customOpt)
expect(result).toBe(input) expect(result).toBe(input)
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
test('option - verbose', () => { test('option - verbose', async () => {
hexo.config.minify.html.verbose = true hexo.config.minify.html.verbose = true
hexo.log.log = jest.fn() hexo.log.log = jest.fn()
h(input, { path }) await h(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`html: ${path}`) expect(hexo.log.log.mock.calls[0][0]).toContain(`html: ${path}`)
}) })
test('exclude', () => { test('exclude', async () => {
const exclude = '*.min.html' const exclude = '*.min.html'
hexo.config.minify.html.exclude = exclude 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) expect(result).toBe(input)
}) })
test('exclude - slash in pattern', () => { test('exclude - slash in pattern', async () => {
const exclude = '**/lectus/**/*.html' const exclude = '**/lectus/**/*.html'
hexo.config.minify.html.exclude = exclude 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) expect(result).toBe(input)
}) })
test('exclude - basename is true + slash', () => { test('exclude - basename is true + slash', async () => {
const exclude = ['**/lectus/**/*.html', 'bar.html'] const exclude = ['**/lectus/**/*.html', 'bar.html']
const globOptions = { basename: true } const globOptions = { basename: true }
hexo.config.minify.html.exclude = exclude hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions 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) expect(result).toBe(input)
}) })
test('exclude - basename is false + slash', () => { test('exclude - basename is false + slash', async () => {
const exclude = ['**/lectus/**/*.html', 'bar.html'] const exclude = ['**/lectus/**/*.html', 'bar.html']
const globOptions = { basename: false } const globOptions = { basename: false }
hexo.config.minify.html.exclude = exclude hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions 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) expect(result).toBe(expected)
}) })
test('null', () => { test('null', async () => {
hexo.config.minify.html.exclude = null hexo.config.minify.html.exclude = null
hexo.config.minify.html.globOptions = null hexo.config.minify.html.globOptions = null
const result = h(input, { path: null }) const result = await h(input, { path: null })
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
test('invalid string', () => { test('invalid string', async () => {
const invalid = '<html><>?:"{}|_+</html>' const invalid = '<html><>?:"{}|_+</html>'
expect(() => { await expect(h(invalid, { path })).rejects.toThrow('Parse Error: <>?:"{}|_+</html>')
h(invalid, { path })
}).toThrow(`Path: ${path}\nError: Parse Error`)
}) })
}) })