Skip to content

Commit

Permalink
Feature/migrate html minifier terser (#378)
Browse files Browse the repository at this point in the history
* feat: migrate to html-minifier-terser

* chore: use html-minifier-terser in benchmark
  • Loading branch information
multivoltage authored Feb 28, 2024
1 parent 77fd561 commit 8113f2e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 67 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ Properties from `reply.locals` will override those from `defaultContext`, but no

## Minifying HTML on render

To utilize [`html-minifier`](https://www.npmjs.com/package/html-minifier) in the rendering process, you can add the option `useHtmlMinifier` with a reference to `html-minifier`,
and the optional `htmlMinifierOptions` option is used to specify the `html-minifier` options:
To utilize [`html-minifier-terser`](https://www.npmjs.com/package/html-minifier-terser) in the rendering process, you can add the option `useHtmlMinifier` with a reference to `html-minifier-terser`,
and the optional `htmlMinifierOptions` option is used to specify the `html-minifier-terser` options:

```js
// get a reference to html-minifier
const minifier = require('html-minifier')
// optionally defined the html-minifier options
// get a reference to html-minifier-terser
const minifier = require('html-minifier-terser')
// optionally defined the html-minifier-terser options
const minifierOpts = {
removeComments: true,
removeCommentsFromCDATA: true,
Expand All @@ -220,9 +220,9 @@ const minifierOpts = {

To filter some paths from minification, you can add the option `pathsToExcludeHtmlMinifier` with list of paths
```js
// get a reference to html-minifier
const minifier = require('html-minifier')
// in options configure the use of html-minifier and set paths to exclude from minification
// get a reference to html-minifier-terser
const minifier = require('html-minifier-terser')
// in options configure the use of html-minifier-terser and set paths to exclude from minification
const options = {
useHtmlMinifier: minifier,
pathsToExcludeHtmlMinifier: ['/test']
Expand Down
2 changes: 1 addition & 1 deletion benchmark/fastify-ejs-minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('./setup.js')({
engine: { ejs: require('ejs') },
route: (req, reply) => { reply.view('index.ejs', { text: 'text' }) },
options: {
useHtmlMinifier: require('html-minifier'),
useHtmlMinifier: require('html-minifier-terser'),
htmlMinifierOptions: {
removeComments: true,
removeCommentsFromCDATA: true,
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function fastifyView (fastify, opts) {
}

if (minify && !isPathExcludedMinification(this)) {
this.send(minify(result, globalOptions.htmlMinifierOptions))
this.send(await minify(result, globalOptions.htmlMinifierOptions))
} else {
this.send(result)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"express": "^4.17.1",
"fastify": "^4.0.0-rc.2",
"handlebars": "^4.7.6",
"html-minifier": "^4.0.0",
"html-minifier-terser": "^7.2.0",
"liquidjs": "^10.0.0",
"mustache": "^4.0.1",
"nunjucks": "^3.2.1",
Expand Down
62 changes: 31 additions & 31 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const POV = require('..')
const Fastify = require('fastify')
const minifier = require('html-minifier')
const minifier = require('html-minifier-terser')
const fs = require('node:fs')
const dot = require('dot')
const handlebars = require('handlebars')
Expand All @@ -26,7 +26,7 @@ module.exports.dotHtmlMinifierTests = function (t, compileOptions, withMinifierO
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with dot engine and html-minifier', t => {
test('reply.view with dot engine and html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()
dot.log = false
Expand All @@ -52,17 +52,17 @@ module.exports.dotHtmlMinifierTests = function (t, compileOptions, withMinifierO
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
}, async (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(minifier.minify(dot.process(compileOptions).testdot(data), options), body.toString())
t.equal(await minifier.minify(dot.process(compileOptions).testdot(data), options), body.toString())
fastify.close()
})
})
})
test('reply.view with dot engine and paths excluded from html-minifier', t => {
test('reply.view with dot engine and paths excluded from html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()
dot.log = false
Expand Down Expand Up @@ -108,7 +108,7 @@ module.exports.etaHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with eta engine and html-minifier', t => {
test('reply.view with eta engine and html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand All @@ -132,18 +132,18 @@ module.exports.etaHtmlMinifierTests = function (t, withMinifierOptions) {
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
}, async (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(minifier.minify(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), body.toString())
t.equal(await minifier.minify(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), body.toString())
fastify.close()
})
})
})

test('reply.view with eta engine and async and html-minifier', t => {
test('reply.view with eta engine and async and html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand All @@ -168,17 +168,17 @@ module.exports.etaHtmlMinifierTests = function (t, withMinifierOptions) {
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
}, async (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(minifier.minify(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), body.toString())
t.equal(await minifier.minify(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), body.toString())
fastify.close()
})
})
})
test('reply.view with eta engine and paths excluded from html-minifier', t => {
test('reply.view with eta engine and paths excluded from html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand Down Expand Up @@ -219,7 +219,7 @@ module.exports.handleBarsHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('fastify.view with handlebars engine and html-minifier', t => {
test('fastify.view with handlebars engine and html-minifier-terser', t => {
t.plan(2)
const fastify = Fastify()

Expand All @@ -237,8 +237,8 @@ module.exports.handleBarsHtmlMinifierTests = function (t, withMinifierOptions) {
fastify.ready(err => {
t.error(err)

fastify.view('./templates/index.html', data).then(compiled => {
t.equal(minifier.minify(handlebars.compile(fs.readFileSync('./templates/index.html', 'utf8'))(data), options), compiled)
fastify.view('./templates/index.html', data).then(async compiled => {
t.equal(await minifier.minify(handlebars.compile(fs.readFileSync('./templates/index.html', 'utf8'))(data), options), compiled)
fastify.close()
})
})
Expand All @@ -249,7 +249,7 @@ module.exports.liquidHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with liquid engine and html-minifier', t => {
test('reply.view with liquid engine and html-minifier-terser', t => {
t.plan(7)
const fastify = Fastify()
const engine = new Liquid()
Expand Down Expand Up @@ -280,15 +280,15 @@ module.exports.liquidHtmlMinifierTests = function (t, withMinifierOptions) {
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
engine.renderFile('./templates/index.liquid', data)
.then((html) => {
.then(async (html) => {
t.error(err)
t.equal(minifier.minify(html, options), body.toString())
t.equal(await minifier.minify(html, options), body.toString())
})
fastify.close()
})
})
})
test('reply.view with liquid engine and paths excluded from html-minifier', t => {
test('reply.view with liquid engine and paths excluded from html-minifier-terser', t => {
t.plan(7)
const fastify = Fastify()
const engine = new Liquid()
Expand Down Expand Up @@ -334,7 +334,7 @@ module.exports.nunjucksHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with nunjucks engine, full path templates folder, and html-minifier', t => {
test('reply.view with nunjucks engine, full path templates folder, and html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand All @@ -359,18 +359,18 @@ module.exports.nunjucksHtmlMinifierTests = function (t, withMinifierOptions) {
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
}, async (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
// Global Nunjucks templates dir changed here.
t.equal(minifier.minify(nunjucks.render('./index.njk', data), options), body.toString())
t.equal(await minifier.minify(nunjucks.render('./index.njk', data), options), body.toString())
fastify.close()
})
})
})
test('reply.view with nunjucks engine, full path templates folder, and paths excluded from html-minifier', t => {
test('reply.view with nunjucks engine, full path templates folder, and paths excluded from html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand Down Expand Up @@ -413,7 +413,7 @@ module.exports.pugHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with pug engine and html-minifier', t => {
test('reply.view with pug engine and html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand All @@ -437,17 +437,17 @@ module.exports.pugHtmlMinifierTests = function (t, withMinifierOptions) {
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
}, async (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(minifier.minify(pug.render(fs.readFileSync('./templates/index.pug', 'utf8'), data), options), body.toString())
t.equal(await minifier.minify(pug.render(fs.readFileSync('./templates/index.pug', 'utf8'), data), options), body.toString())
fastify.close()
})
})
})
test('reply.view with pug engine and paths excluded from html-minifier', t => {
test('reply.view with pug engine and paths excluded from html-minifier-terser', t => {
t.plan(6)
const fastify = Fastify()

Expand Down Expand Up @@ -488,7 +488,7 @@ module.exports.twigHtmlMinifierTests = function (t, withMinifierOptions) {
const test = t.test
const options = withMinifierOptions ? minifierOpts : {}

test('reply.view with twig engine and html-minifier', t => {
test('reply.view with twig engine and html-minifier-terser', t => {
t.plan(7)
const fastify = Fastify()

Expand Down Expand Up @@ -517,15 +517,15 @@ module.exports.twigHtmlMinifierTests = function (t, withMinifierOptions) {
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], String(body.length))
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
Twig.renderFile('./templates/index.twig', data, (err, html) => {
Twig.renderFile('./templates/index.twig', data, async (err, html) => {
t.error(err)
t.equal(minifier.minify(html, options), body.toString())
t.equal(await minifier.minify(html, options), body.toString())
})
fastify.close()
})
})
})
test('reply.view with twig engine and paths excluded from html-minifier', t => {
test('reply.view with twig engine and paths excluded from html-minifier-terser', t => {
t.plan(7)
const fastify = Fastify()

Expand Down
Loading

0 comments on commit 8113f2e

Please sign in to comment.