Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

minify

5.1.1 • Public • Published

Minify License Dependency Status Build Status NPM version

Minify - a minifier of js, css, html and img files. To use minify as middleware try Mollify.

Install

npm i minify -g

How to use?

CLI

$ cat > hello.js
const hello = 'world';
 
for (let i = 0; i < hello.length; i++) {
    console.log(hello[i]);
}
^D
 
$ minify hello.js
const hello="world";for(let l=0;l<hello.length;l++)console.log(hello[l]);

Code Example

minify can be used as a promise:

const minify = require('minify');
const options = {
    html: {
        removeAttributeQuotes: false,
        removeOptionalTags: false
    },
};
 
minify('./client.js', options)
    .then(console.log)
    .catch(console.error);
 

Or with async-await and try-to-catch:

const minify = require('minify');
const tryToCatch = require('try-to-catch');
const options = {
    html: {
        removeAttributeQuotes: false,
        removeOptionalTags: false
    }
};
 
async () => {
    const [error, data] = await tryToCatch(minify, './client.js', options);
    
    if (error)
        return console.error(error.message);
    
    console.log(data);
}();

Options

The options object accepts configuration for html, css, js, and img like so:

const options = {
    html: {
        removeAttributeQuotes: false,
    },
    css: {
        compatibility: '*',
    },
    js: {
        ecma: 5,
    },
    img: {
        maxSize: 4096,
    }
}

Full documentation for options that each file type accepts can be found on the pages of the libraries used by minify to process the files:

Minify sets a few defaults for HTML that may differ from the base html-minifier settings:

  • removeComments: true
  • removeCommentsFromCDATA: true
  • removeCDATASectionsFromCDATA: true
  • collapseWhitespace: true
  • collapseBooleanAttributes: true
  • removeAttributeQuotes: true
  • removeRedundantAttributes: true
  • useShortDoctype: true
  • removeEmptyAttributes: true
  • removeEmptyElements: false
  • removeOptionalTags: true
  • removeScriptTypeAttributes: true
  • removeStyleLinkTypeAttributes: true
  • minifyJS: true
  • minifyCSS: true

License

MIT

Install

npm i minify

DownloadsWeekly Downloads

97,138

Version

5.1.1

License

MIT

Unpacked Size

31.3 kB

Total Files

12

Last publish

Collaborators

  • avatar