Skip to content

Commit

Permalink
feat: 允许切换Html缩小器
Browse files Browse the repository at this point in the history
  • Loading branch information
zkz098 committed Feb 20, 2024
1 parent 7c96aec commit 84d5ce4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ minify:
targets: ">= 0.5%" # browserslist format target
exclude: # Exclude files, accept string[], must match micromatch format
html:
minifier: html-minifier # minify-html(faster) or html-minifier(more stable)
enable: true # Enable HTML optimization
options:
comments: false # Whether to preserve comment content
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ hexo.config.minify.css = Object.assign({

hexo.config.minify.html = Object.assign({
enable: true,
minifier: 'html-minifier',
options: {
comments: false
},
Expand Down
23 changes: 16 additions & 7 deletions lib/html.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { minify } from '@minify-html/node'
import { minify as minifyHtmlFast } from '@minify-html/node'
import { minify as minifyHtmlStable } from 'html-minifier'
import { isExclude } from './utils'
import type Hexo from 'hexo'

interface HtmlMinifyConfig {
minifier: string
enable: boolean
options: {
comments: boolean
}
exclude: string[]
}
export function minifyHtml (this: Hexo, str:string, data:any) {
const { options, exclude } = this.config.minify.html as HtmlMinifyConfig

export async function minifyHtml (this: Hexo, str: string, data: any) {
const { options, exclude, minifier } = this.config.minify.html as HtmlMinifyConfig
if (isExclude(data.path, exclude)) return str
return minify(Buffer.from(str), {
keep_spaces_between_attributes: true,
keep_comments: options.comments
}).toString()
if (minifier === 'minify-html') {
return minifyHtmlFast(Buffer.from(str), {
keep_spaces_between_attributes: true,
keep_comments: options.comments
}).toString()
} else {
return minifyHtmlStable(str, {
removeComments: !options.comments
})
}
}
26 changes: 20 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-lightning-minify",
"version": "0.1.4",
"version": "0.1.5",
"description": "A Hexo plugin that can automatically minify JS, CSS, and HTML files at lightning speed.",
"main": "index.js",
"scripts": {
Expand All @@ -11,22 +11,36 @@
"license": "AGPL-3.0-or-later",
"dependencies": {
"@minify-html/node": "^0.15.0",
"@types/node": "^20.11.17",
"browserslist": "^4.23.0",
"cheerio": "1.0.0-rc.12",
"esbuild": "^0.20.0",
"esbuild": "^0.20.1",
"hexo": "^7.1.1",
"html-minifier": "^4.0.0",
"lightningcss": "^1.23.0",
"micromatch": "^4.0.5",
"sharp": "^0.33.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"@types/html-minifier": "^4.0.5",
"@types/node": "^20.11.19",
"@types/micromatch": "^4.0.6",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"eslint": "^8.56.0",
"eslint-config-standard": "~17",
"eslint-plugin-n": "^16.6.2"
},
"pnpm": {
"overrides": {
"array-includes": "npm:@nolyfill/array-includes@latest",
"array.prototype.findlastindex": "npm:@nolyfill/array.prototype.findlastindex@latest",
"array.prototype.flat": "npm:@nolyfill/array.prototype.flat@latest",
"array.prototype.flatmap": "npm:@nolyfill/array.prototype.flatmap@latest",
"hasown": "npm:@nolyfill/hasown@latest",
"object.fromentries": "npm:@nolyfill/object.fromentries@latest",
"object.groupby": "npm:@nolyfill/object.groupby@latest",
"object.values": "npm:@nolyfill/object.values@latest"
}
}
}
}

0 comments on commit 84d5ce4

Please sign in to comment.