Coder Social home page Coder Social logo

meszaros-lajos-gyorgy / express-minify-html-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from melonmanchan/express-minify-html

12.0 2.0 1.0 194 KB

Express.js middleware wrapper around html-minifier-terser

License: MIT License

JavaScript 66.80% HTML 15.71% EJS 6.31% Handlebars 6.24% Pug 4.95%
express-middleware html-minify

express-minify-html-2's Introduction

express-minify-html-2

Express middleware wrapper around html-minifier-terser

Note that this is a fork of express-minify-html

This fork is made in order to keep the express-minify-html project alive. All credits go to the contributors in the original repo.

Description

This express middleware simply enchances the regular 'render' method of the response object for minifying HTML.

Usage

npm install --save --production express-minify-html-2 express
var express = require('express')
var minifyHTML = require('express-minify-html-2')

var app = express()

app.use(
  minifyHTML({
    override: true,
    /**
     * exceptionUrls can also be spelled as exception_url for backwards compatibility
     */
    exceptionUrls: false,
    htmlMinifier: {
      removeComments: true,
      collapseWhitespace: true,
      collapseBooleanAttributes: true,
      removeAttributeQuotes: true,
      removeEmptyAttributes: true,
    },
  }),
)

app.get('hello', function (req, res, next) {
  res.render('helloTemplate', { hello: 'world' }, function (err, html) {
    // The output is minified, huzzah!
    console.log(html)
    res.send(html)
  })
})

Set 'override' to false if you don't want to hijack the ordinary res.render function. This adds an additional res.renderMin function to the response object to render minimized HTML.

The 'htmlMinifier' opts are simply passed on to the html-minifier-terser plugin. For all the available configuration options, see the original repo!

If no callback is provided, res.render/res.renderMin sends the minified HTML to the client just as the regular express res.render does. Otherwise, the callback is called with the error object and the minified HTML content, as demonstrated above.

the exceptionUrls (or it's alias exception_url) optional parameter is a single value, or an array of strings, regexes and functions that can be used to check whether minifying should be skipped entirely.

exceptionUrls: [
  'url_to_avoid_minify_html', // String.
  /regex_to_analyze_req_to_avoid_minify/i, // Regex.
  function (req, res) {
    // Function.
    // Code to analyze req and decide if skips or not minify.
    // Needs to return a boolean value.
    return true
  },
]

Full examples can naturally be found under the 'examples'-folder of this repository!

Other infos

The code inside minifier.js should be compatible with nodejs 6.0.0, but since html-minifier-terser requires "^14.13.1" the package also requires at least nodejs 14.0.0

License

MIT © Matti Jokitulppo

npm version npm downloads

express-minify-html-2's People

Contributors

juliovedovatto avatar melonmanchan avatar meszaros-lajos-gyorgy avatar radum avatar simonmilz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

almo7aya

express-minify-html-2's Issues

Security vulnerability in dependency [email protected]

[email protected] has a ReDOS vulnerability

Security information
Factors contributing to the scoring:
Snyk: CVSS 5.3 - Medium Severity
NVD: CVSS 7.5 - High Severity
Why are the scores different? Learn how Snyk evaluates vulnerability scores

Overview
Affected versions of this package are vulnerable to Regular Expression Denial of Service (ReDoS) due to insecure usage of regex in the cleanAttributeValue function in htmlminifier.js.

See also this issue on the html-minifier repo kangax/html-minifier#1135. I'm not sure the project is still maintained but there is an alternative fork maintained by the Terser team https://github.com/terser/html-minifier-terser

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.