Skip to content

Commit

Permalink
Minify HTML decryption template
Browse files Browse the repository at this point in the history
  • Loading branch information
Greenheart committed Jan 17, 2025
1 parent 00fb090 commit cba2b50
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"@types/node": "^22.10.7",
"esbuild": "^0.20.2",
"html-minifier-terser": "^7.2.0",
"prettier": "^3.4.2",
"typescript": "^5.7.3",
"vite": "^6.0.7",
Expand Down
122 changes: 103 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion scripts/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { cp, readFile, writeFile, rm, readdir, mkdir } from 'fs/promises'
import { execSync } from 'child_process'
import { resolve, join } from 'path'
import { performance } from 'perf_hooks'
import { minify } from 'html-minifier-terser'

const readJSON = (path) => readFile(path, 'utf-8').then(JSON.parse)
const writeJSON = (path, content, indentation = 4) =>
Expand All @@ -21,6 +22,26 @@ async function emptyDir(dir) {
)
}

async function minifyHTML(html) {
const minifiedHTML = await minify(html, {
removeComments: true,
removeEmptyAttributes: true,
html5: true,
decodeEntities: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
})
console.log(
`HTML size reduced by ${Math.round(
100 - (minifiedHTML.length / html.length) * 100,
)}%`,
)

return minifiedHTML
}

const startTime = performance.now()
const pkg = await readJSON('./package.json')

Expand All @@ -31,6 +52,19 @@ const distDir = resolve(outDir)

await emptyDir(distDir)

const minifyHTMLPlugin = {
name: 'minifyl-html-plugin',
setup(build) {
build.onLoad({ filter: /\.html$/ }, async (args) => {
const contents = await readFile(args.path, 'utf8')
return {
contents: await minifyHTML(contents),
loader: 'text',
}
})
},
}

esbuild
.build({
entryPoints: ['src/index.ts', 'src/core.ts', 'src/cli.ts'],
Expand All @@ -42,8 +76,8 @@ esbuild
format: 'esm',
target: ['esnext'],
platform: 'node',
loader: { '.html': 'text' },
external: ['rfc4648', 'sade'],
plugins: [minifyHTMLPlugin],
})
.then(async () => {
// Build declaration files with TSC since they aren't built by esbuild.
Expand Down
2 changes: 1 addition & 1 deletion src/decrypt-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<p id="msg">This page is password protected.</p>
</header>
<form class="hidden">
<input type="password" id="pwd" name="pwd" aria-label="Password" autofocus />
<input type="password" id="pwd" name="pwd" aria-label="Password" autofocus>
<input type="submit" value="Submit">
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<p id="msg">This page is password protected.</p>
</header>
<form class="hidden">
<input type="password" id="pwd" name="pwd" aria-label="Password" autofocus />
<input type="password" id="pwd" name="pwd" aria-label="Password" autofocus>
<input type="submit" value="Submit">
</form>
</div>
Expand Down

0 comments on commit cba2b50

Please sign in to comment.