🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

minify-html-css

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minify-html-css

🔽 A library to minify HTML and CSS.

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
17
112.5%
Maintainers
1
Weekly downloads
 
Created
Source

minify-html-css

🔽 A JavaScript (TypeScript) library to minify HTML and CSS.

This project provides a clean and easy-to-use interface for minifying HTML (and soon CSS) with well-documented options. It is designed to be fast, developer-friendly, and highly configurable for modern web projects.

Table of Contents

  • Intent
  • Features
  • Installation
  • Usage
  • API Documentation
  • TODO
  • Contributing
  • License
  • Acknowledgements

Intent

I created this project to bring together the most performant HTML and CSS minifier libraries as clean, easy-to-use functions with well-documented options. The goal is to make web minification accessible, fast, and developer-friendly.

Features

  • Minifies HTML strings
  • Removes unnecessary whitespace and comments
  • Works seamlessly in Node.js and browser environments
  • Written in TypeScript with type definitions included

Installation

npm install minify-html-css

Usage

Basic Example

import { minifyHTML } from 'minify-html-css';

const html = `
  <div>
    <!-- This is a comment -->
    <h1> Hello World! </h1>
    <style>
      body { color: red; }
    </style>
  </div>
`;

const minifiedHtml = minifyHTML(html).code;
console.log(minifiedHtml); // "<div><h1>Hello World!</h1><style>body{color:red}</style></div>"

API Documentation

minifyHTML(input: string, options?: MinifyHTMLOptions): string

Description: Minifies an HTML string by removing unnecessary whitespace, comments, and compressing inline JS and CSS (where supported).

Implementation note: This function is a wrapper around the @swc/html package and uses its minification logic under the hood.

Parameters:

  • input (string): The HTML code to minify.
  • options? (MinifyHTMLOptions): Configuration object for fine-grained control.

The available options are:

OptionTypeDefaultDescription
collapseWhitespaces'none' | 'all' | 'smart' | 'conservative' | 'advanced-conservative' | 'only-metadata''all'Controls how whitespace is collapsed and removed throughout the document.
removeEmptyMetadataElementsbooleantrueRemoves empty metadata elements such as <script>, <style>, <meta>, and <link>.
removeCommentsbooleantrueRemoves all HTML comments unless matched by preserveComments.
preserveCommentsstring[]Array of regex strings; comments matching any are preserved. You can override the default patterns.
minifyConditionalCommentsbooleantrueMinifies IE conditional comments.
removeEmptyAttributesbooleantrueRemoves empty attributes from HTML tags (when safe).
removeRedundantAttributes'none' | 'all' | 'smart''smart'Controls removal of redundant or default attributes.
collapseBooleanAttributesbooleantrueCollapses boolean attributes to their short form (e.g. checked).
normalizeAttributesbooleantrueCleans up attribute values by removing unnecessary spaces, and strips javascript: from event handlers.
minifyJsonboolean | { pretty?: boolean }trueMinifies embedded JSON within <script type="application/json">.
minifyJsbooleantrueMinifies inline JavaScript.
minifyCssbooleantrueMinifies inline CSS.
minifyAdditionalScriptsContent[string, MinifierType][]Minifies additional <script> types, specifying type pattern and minifier.
minifyAdditionalAttributes[string, MinifierType][]Minifies additional attribute values, specifying attribute name pattern and minifier.
sortSpaceSeparatedAttributeValuesbooleanfalseSorts space-separated attribute values like class or rel.
sortAttributesbooleanfalseSorts all attributes of each element in reverse alphabetical order.
tagOmissionbooleantrueOmits optional end tags when valid per HTML spec.
quotesbooleanfalseAlways wrap attribute values in quotes.

For detailed type definitions and documentation, see src/minify-html-types.ts.

TODO

  • Implement CSS minification function (minifyCSS) built on top of Lightning CSS
  • Implement CLI usage (command-line tool)

Contributing

We welcome contributions!

Requirements:

  • Bun (make sure you have Bun installed)

Steps:

  • Fork this repository.
  • Create a new branch: git switch -c my-feature
  • Make your changes and add tests if possible.
  • Run bun run lint && bun test (This is automatically done when you open a pull request) to ensure everything passes.
  • Commit your changes: git commit -m "Add my feature"
  • Push to your fork and submit a pull request.

Please read our CONTRIBUTING.md for more details.

License

This project is licensed under the MIT License.

Types License Notice:

  • The type definitions in src/minify-html-types.ts are adapted from the SWC Project and are licensed under the Apache License, Version 2.0.
  • See the NOTICE file and LICENSE-APACHE for more information and required attributions.

Acknowledgements

  • This project uses @swc/html under the hood for actual HTML minification.
    We adapt and update their type definitions, add documentation, and keep their license and notice files in the repository for compliance and transparency. See the repository for more information.

Keywords

minify

FAQs

Package last updated on 03 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts