Skip to content

Commit

Permalink
refactor: switch html-minifier to html-minifier-terser
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jan 28, 2024
1 parent 7809098 commit 0bee9ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 29 additions & 27 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = '<html><>?:"{}|_+</html>'

expect(() => {
h(invalid, { path })
}).toThrow(`Path: ${path}\nError: Parse Error`)
await expect(h(invalid, { path })).rejects.toThrow('Parse Error: <>?:"{}|_+</html>')
})
})

0 comments on commit 0bee9ad

Please sign in to comment.