Coder Social home page Coder Social logo

fastify-minify's Introduction

fastify-minify

A plugin for Fastify to minify HTML, JS, JSON and CSS. And you can transform any response as your like.

Usage

const fastify = require("fastify")();

fastify.register(require("fastify-minify"), {
    cache: 2000,
});

fastify.get("/minfiedCSS", function (req, rep) {
    const css = getSomeCSS();
    rep.type("text/css").send(this.minifyCSS(css));
});

Three methods are added to the fastify instance. minifyHTML, minifyJS and minifyCSS (See transformers). All of them take a string as input and return a promise.

By default no response is minified automatically. You can enable it with global or minInfix.

Options

fastify.register(require("fastify-minify"), {
    cache: 2500,
    global: true,
    minInfix: (req, filePath) => req.query.mini === "true",
    validate: (req, rep, payload) => typeof payload === "string",
    htmlOptions: { caseSensitive: true },
    jsOptions: { "keep-classnames": true },
    cssOptions: {},
    transformers: [
        {
            suffix: "txt",
            contentType: "text/plain",
            func: value => value.toUpperCase(),
            decorate: "upperCaseText",
            useCache: false
        }
    ]
});

cache

  • Size of the lru-cache or a cache object with the functions set(string, string) and get(string)=>string | get(string)=>Promise<string>. tiny-lru is used by default. It is used to cache the minification results.
  • default: 0
  • type: number | object

global

  • If truthy, every response with content type html, js, json or css (See transformers) will be minified. See validate.
  • default: false
  • type: boolean

minInfix

  • If truthy or a function (function(req, filePath)) that returns a truthy value, a new path for static files will be added with min-infix e.g. public/foo.min.css for public/foo.css. You need to have fastify-static installed to use it. The function is called onRequest. โ— If you have static files with min-infix, they will be ignored.
  • default: false
  • type: boolean | function

validate

  • If this function (function(req, rep, payload)) returns a truthy value, the payload will be minified. It is called onSend and only if global or minInfix is enabled.
  • default: () => true
  • type: function

htmlOptions

jsOptions

  • An object that will be passed to terser.
  • default: {}
  • type: object

cssOptions

  • An object that will be passed to csso.
  • default: {}
  • type: object

transformers

  • An array of transformers to transform any response. There are 4 transformers built-in (JS, HTML, CSS, JSON).
    • suffix
      • Suffix of files to transform. Used when minInfix is enabled.
      • type: string | array of strings
    • contentType
      • Content type of response. Used when global or minInfix is enabled.
      • type: string | array of strings
    • func
      • Function to transform the response.
      • type: function (string => string | string => Promise<string>)
    • decorate (optional)
      • Method name of the decorated function.
      • type: string
    • useCache (optional)
      • If true, cache is used when transforming. Enabled by default.
      • type: boolean
    • To disable one of the built-in transformers you can add a transformer with respective suffix e.g. { suffix: "js", func: null }
  • default: []
  • type: array

fastify-minify's People

Contributors

jelenkee avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

andremacola

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.