Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
.DS_Store*
# OS #
###################
.DS_Store
.idea
Thumbs.db
tmp
temp


# Node.js #
###################
node_modules
package-lock.json


# NYC #
###################
coverage
.nyc_output
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
12 changes: 12 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extension": [
".js"
],
"reporter": [
"text-lcov",
"text",
"lcov"
],
"report-dir": "./coverage",
"temp-dir": "./.nyc_output"
}
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
node_js:
- "7"
language: node_js
script: "npm run test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
node_js:
- 10
- 12
- 14
script:
- npm run test-ci
after_script:
- npm install coveralls
- cat ./coverage/lcov.info | coveralls
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong me@jongleberry.com
Copyright (c) 2020 Koa.js contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# [Koa HTML Minifier][repo]

# Koa HTML Minifier

Middleware that minifies your HTML responses using [html-minifier](https://github.com/kangax/html-minifier).
It uses `html-minifier`'s default options which are all turned off by default,
Middleware that minifies your HTML responses using [html-minifier-terser][html-minifier-terser-repo].
It uses `html-minifier-terser`'s default options which are all turned off by default,
so you __have__ to set the options otherwise it's not going to do anything.

[![NPM version][npm-img]][npm-url]
[![NPM Downloads][downloads-image]][npm-url]
[![Build status][travis-img]][travis-url]
[![Test coverage][coveralls-img]][coveralls-url]
[![License][license-img]][license-url]

## Install

```bash
# npm ..
npm i koa-html-minifier
# yarn ..
yarn add koa-html-minifier
```

## API

```js
Expand All @@ -19,4 +33,28 @@ app.use(require('koa-html-minifier')({

### Options

See: https://github.com/kangax/html-minifier#options-quick-reference
See: https://github.com/DanielRuf/html-minifier-terser#minification-comparison

## Contributors

| Name | Website |
| ----------------- | --------------------------------- |
| **Jonathan Ong** | <http://jongleberry.com> |
| **Imed Jaberi** | <https://www.3imed-jaberi.com/> |
| **João Carmona** | |


[npm-img]: https://img.shields.io/npm/v/@koa/html-minifier.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-html-minifier

[travis-img]: https://img.shields.io/travis/koajs/html-minifier.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/html-minifier

[coveralls-img]: https://img.shields.io/coveralls/koajs/html-minifier.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/html-minifier?branch=master

[license-img]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
[license-url]: LICENSE

[github-repo]: https://github.com/koajs/html-minifier
[html-minifier-terser-repo]: https://github.com/DanielRuf/html-minifier-terser
33 changes: 20 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const minify = require('html-minifier').minify;

module.exports = (options = {}) => {
return async (ctx, next) => {
await next();
if (!ctx.response.is('html')) return;
let { body } = ctx.response;
if (!body) return;
if (typeof body.pipe === 'function') return;
if (Buffer.isBuffer(body)) body = body.toString('utf8');
else if (typeof body === 'object') return;
ctx.response.body = await minify(body, options);
};
const { minify } = require('html-minifier-terser');

module.exports = function koaHtmlMinifier(options) {
return async (ctx, next) => {
await next();
if (
!options ||
!ctx.response.is('html') ||
!ctx.response.body ||
typeof ctx.response.body.pipe === 'function'
)
return;

if (Buffer.isBuffer(ctx.response.body))
ctx.response.body = ctx.response.body.toString('utf8');

if (typeof ctx.response.body === 'object') return;

ctx.response.body = await minify(ctx.response.body, options);
};
};
Loading