{"id":15511409,"url":"https://github.com/terser/html-minifier-terser","last_synced_at":"2025-05-14T14:08:08.157Z","repository":{"id":38244612,"uuid":"219121534","full_name":"terser/html-minifier-terser","owner":"terser","description":"actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code","archived":false,"fork":false,"pushed_at":"2024-08-19T17:17:27.000Z","size":16043,"stargazers_count":415,"open_issues_count":37,"forks_count":33,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-03T04:39:44.060Z","etag":null,"topics":["cli","compress","ecmascript6","es6","minify","terser","uglify","uglify-js","uglifyjs"],"latest_commit_sha":null,"homepage":"https://terser.org/html-minifier-terser","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/terser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-02T07:49:27.000Z","updated_at":"2025-04-29T16:00:53.000Z","dependencies_parsed_at":"2023-10-13T11:08:35.682Z","dependency_job_id":"64f60494-37c8-40b5-986f-eeaa3d96119e","html_url":"https://github.com/terser/html-minifier-terser","commit_stats":{"total_commits":1226,"total_committers":108,"mean_commits":"11.351851851851851","dds":0.6745513866231647,"last_synced_commit":"c4a7ae0bd08b1a438d9ca12a229b4cbe93fc016a"},"previous_names":[],"tags_count":105,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terser%2Fhtml-minifier-terser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terser%2Fhtml-minifier-terser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terser%2Fhtml-minifier-terser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terser%2Fhtml-minifier-terser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/terser","download_url":"https://codeload.github.com/terser/html-minifier-terser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252288960,"owners_count":21724326,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","compress","ecmascript6","es6","minify","terser","uglify","uglify-js","uglifyjs"],"created_at":"2024-10-02T09:53:01.124Z","updated_at":"2025-05-14T14:08:08.134Z","avatar_url":"https://github.com/terser.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# HTMLMinifier\n\n[![NPM version](https://img.shields.io/npm/v/html-minifier-terser.svg)](https://www.npmjs.com/package/html-minifier-terser)\n[![Build Status](https://github.com/terser/html-minifier-terser/workflows/CI/badge.svg)](https://github.com/terser/html-minifier-terser/actions?workflow=CI)\n\n[HTMLMinifier](https://terser.org/html-minifier-terser/) is a highly **configurable**, **well-tested**, JavaScript-based HTML minifier.\n\n## Installation\n\nFrom NPM for use as a command line app:\n\n```shell\nnpm install html-minifier-terser -g\n```\n\nFrom NPM for programmatic use:\n\n```shell\nnpm install html-minifier-terser\n```\n\n## Usage\n\n**Note** that almost all options are disabled by default. Experiment and find what works best for you and your project.\n\nFor command line usage please see `html-minifier-terser --help` for a list of available options.\n\n**Sample command line:**\n\n```bash\nhtml-minifier-terser --collapse-whitespace --remove-comments --minify-js true\n```\n\n### Node.js\n\n```js\nconst { minify } = require('html-minifier-terser');\n\nconst result = await minify('\u003cp title=\"blah\" id=\"moo\"\u003efoo\u003c/p\u003e', {\n  removeAttributeQuotes: true,\n});\nresult; // '\u003cp title=blah id=moo\u003efoo\u003c/p\u003e'\n```\n\nSee [corresponding blog post](http://perfectionkills.com/experimenting-with-html-minifier) for all the gory details of [how it works](http://perfectionkills.com/experimenting-with-html-minifier#how_it_works), [description of each option](http://perfectionkills.com/experimenting-with-html-minifier#options), [testing results](http://perfectionkills.com/experimenting-with-html-minifier#field_testing) and [conclusions](http://perfectionkills.com/experimenting-with-html-minifier#cost_and_benefits).\n\nAlso see corresponding [Ruby wrapper](https://github.com/stereobooster/html_minifier), and for Node.js, [Grunt plugin](https://github.com/gruntjs/grunt-contrib-htmlmin), [Gulp plugin](https://github.com/pioug/gulp-html-minifier-terser), [Koa middleware wrapper](https://github.com/koajs/html-minifier) and [Express middleware wrapper](https://github.com/melonmanchan/express-minify-html).\n\nFor lint-like capabilities take a look at [HTMLLint](https://github.com/kangax/html-lint).\n\n## Minification comparison\n\nHow does HTMLMinifier compare to other solutions — [HTML Minifier from Will Peavy](http://www.willpeavy.com/minifier/) (1st result in [Google search for \"html minifier\"](https://www.google.com/#q=html+minifier)) as well as [htmlcompressor.com](http://htmlcompressor.com) and [minimize](https://github.com/Swaagie/minimize)?\n\n| Site                                                                         | Original size *(KB)* | HTMLMinifier | minimize | Will Peavy | htmlcompressor.com |\n| ---------------------------------------------------------------------------- |:--------------------:| ------------:| --------:| ----------:| ------------------:|\n| [Google](https://www.google.com/)                                            | 52                   | **48**       | 52       | 54         | n/a                |\n| [Stack Overflow](https://stackoverflow.com/)                                 | 177                  | **143**      | 154      | 154        | n/a                |\n| [HTMLMinifier](https://github.com/kangax/html-minifier)                      | 252                  | **171**      | 230      | 250        | n/a                |\n| [Bootstrap CSS](https://getbootstrap.com/docs/3.3/css/)                      | 271                  | **260**      | 269      | 229        | n/a                |\n| [BBC](https://www.bbc.co.uk/)                                                | 355                  | **324**      | 353      | 344        | n/a                |\n| [Amazon](https://www.amazon.co.uk/)                                          | 466                  | **430**      | 456      | 474        | n/a                |\n| [Twitter](https://twitter.com/)                                              | 469                  | **394**      | 462      | 513        | n/a                |\n| [Wikipedia](https://en.wikipedia.org/wiki/President_of_the_United_States)    | 703                  | **569**      | 682      | 708        | n/a                |\n| [Eloquent Javascript](https://eloquentjavascript.net/1st_edition/print.html) | 870                  | **815**      | 840      | 864        | n/a                |\n| [NBC](https://www.nbc.com/)                                                  | 1701                 | **1566**     | 1689     | 1705       | n/a                |\n| [New York Times](https://www.nytimes.com/)                                   | 1731                 | **1583**     | 1726     | 1680       | n/a                |\n| [ES draft](https://tc39.github.io/ecma262/)                                  | 6296                 | **5538**     | 5733     | n/a        | n/a                |\n\n## Options Quick Reference\n\nMost of the options are disabled by default.\n\n| Option                         | Description     | Default |\n|--------------------------------|-----------------|---------|\n| `caseSensitive`                | Treat attributes in case sensitive manner (useful for custom HTML tags) | `false` |\n| `collapseBooleanAttributes`    | [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier#collapse_boolean_attributes) | `false` |\n| `collapseInlineTagWhitespace`  | Don't leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |\n| `collapseWhitespace`           | [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier#collapse_whitespace) | `false` |\n| `conservativeCollapse`         | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |\n| `continueOnParseError`         | [Handle parse errors](https://html.spec.whatwg.org/multipage/parsing.html#parse-errors) instead of aborting. | `false` |\n| `customAttrAssign`             | Arrays of regex'es that allow to support custom attribute assign expressions (e.g. `'\u003cdiv flex?=\"{{mode != cover}}\"\u003e\u003c/div\u003e'`) | `[ ]` |\n| `customAttrCollapse`           | Regex that specifies custom attribute to strip newlines from (e.g. `/ng-class/`) | |\n| `customAttrSurround`           | Arrays of regex'es that allow to support custom attribute surround expressions (e.g. `\u003cinput {{#if value}}checked=\"checked\"{{/if}}\u003e`) | `[ ]` |\n| `customEventAttributes`        | Arrays of regex'es that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |\n| `decodeEntities`               | Use direct Unicode characters whenever possible | `false` |\n| `html5`                        | Parse input according to HTML5 specifications | `true` |\n| `ignoreCustomComments`         | Array of regex'es that allow to ignore certain comments, when matched | `[ /^!/, /^\\s*#/ ]` |\n| `ignoreCustomFragments`        | Array of regex'es that allow to ignore certain fragments, when matched (e.g. `\u003c?php ... ?\u003e`, `{{ ... }}`, etc.)  | `[ /\u003c%[\\s\\S]*?%\u003e/, /\u003c\\?[\\s\\S]*?\\?\u003e/ ]` |\n| `includeAutoGeneratedTags`     | Insert tags generated by HTML parser | `true` |\n| `keepClosingSlash`             | Keep the trailing slash on singleton elements | `false` |\n| `maxLineLength`                | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points |\n| `minifyCSS`                    | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text, type)`) |\n| `minifyJS`                     | Minify JavaScript in script elements and event attributes (uses [Terser](https://github.com/terser/terser)) | `false` (could be `true`, `Object`, `Function(text, inline)`) |\n| `minifyURLs`                   | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `String`, `Object`, `Function(text)`) |\n| `noNewlinesBeforeTagClose`     | Never add a newline before a tag that closes an element | `false` |\n| `preserveLineBreaks`           | Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with `collapseWhitespace=true` | `false` |\n| `preventAttributesEscaping`    | Prevents the escaping of the values of attributes | `false` |\n| `processConditionalComments`   | Process contents of conditional comments through minifier | `false` |\n| `processScripts`               | Array of strings corresponding to types of script elements to process through minifier (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.) | `[ ]` |\n| `quoteCharacter`               | Type of quote to use for attribute values (' or \") | |\n| `removeAttributeQuotes`        | [Remove quotes around attributes when possible](http://perfectionkills.com/experimenting-with-html-minifier#remove_attribute_quotes) | `false` |\n| `removeComments`               | [Strip HTML comments](http://perfectionkills.com/experimenting-with-html-minifier#remove_comments) | `false` |\n| `removeEmptyAttributes`        | [Remove all attributes with whitespace-only values](http://perfectionkills.com/experimenting-with-html-minifier#remove_empty_or_blank_attributes) | `false` (could be `true`, `Function(attrName, tag)`) |\n| `removeEmptyElements`          | [Remove all elements with empty contents](http://perfectionkills.com/experimenting-with-html-minifier#remove_empty_elements) | `false` |\n| `removeOptionalTags`           | [Remove optional tags](http://perfectionkills.com/experimenting-with-html-minifier#remove_optional_tags) | `false` |\n| `removeRedundantAttributes`    | [Remove attributes when value matches default.](http://perfectionkills.com/experimenting-with-html-minifier#remove_redundant_attributes) | `false` |\n| `removeScriptTypeAttributes`   | Remove `type=\"text/javascript\"` from `script` tags. Other `type` attribute values are left intact | `false` |\n| `removeStyleLinkTypeAttributes`| Remove `type=\"text/css\"` from `style` and `link` tags. Other `type` attribute values are left intact | `false` |\n| `removeTagWhitespace`          | Remove space between attributes whenever possible. **Note that this will result in invalid HTML!** | `false` |\n| `sortAttributes`               | [Sort attributes by frequency](#sorting-attributes--style-classes) | `false` |\n| `sortClassName`                | [Sort style classes by frequency](#sorting-attributes--style-classes) | `false` |\n| `trimCustomFragments`          | Trim white space around `ignoreCustomFragments`. | `false` |\n| `useShortDoctype`              | [Replaces the `doctype` with the short (HTML5) doctype](http://perfectionkills.com/experimenting-with-html-minifier#use_short_doctype) | `false` |\n\n### Sorting attributes / style classes\n\nMinifier options like `sortAttributes` and `sortClassName` won't impact the plain-text size of the output. However, they form long repetitive chains of characters that should improve compression ratio of gzip used in HTTP compression.\n\n## Special cases\n\n### Ignoring chunks of markup\n\nIf you have chunks of markup you would like preserved, you can wrap them `\u003c!-- htmlmin:ignore --\u003e`.\n\n### Minifying JSON-LD\n\nYou can minify script tags with JSON-LD by setting the option `{ processScripts: ['application/ld+json'] }`. Note that this minification is very rudimentary, it is mainly useful for removing newlines and excessive whitespace. \n\n### Preserving SVG tags\n\nSVG tags are automatically recognized, and when they are minified, both case-sensitivity and closing-slashes are preserved, regardless of the minification settings used for the rest of the file.\n\n### Working with invalid markup\n\nHTMLMinifier **can't work with invalid or partial chunks of markup**. This is because it parses markup into a tree structure, then modifies it (removing anything that was specified for removal, ignoring anything that was specified to be ignored, etc.), then it creates a markup out of that tree and returns it.\n\nInput markup (e.g. `\u003cp id=\"\"\u003efoo`)\n\n↓\n\nInternal representation of markup in a form of tree (e.g. `{ tag: \"p\", attr: \"id\", children: [\"foo\"] }`)\n\n↓\n\nTransformation of internal representation (e.g. removal of `id` attribute)\n\n↓\n\nOutput of resulting markup (e.g. `\u003cp\u003efoo\u003c/p\u003e`)\n\nHTMLMinifier can't know that original markup was only half of the tree; it does its best to try to parse it as a full tree and it loses information about tree being malformed or partial in the beginning. As a result, it can't create a partial/malformed tree at the time of the output.\n\n## Running benchmarks\n\nBenchmarks for minified HTML:\n\n```shell\ncd benchmarks\nnpm install\nnpm run benchmark\n```\n\n## Running local server\n\n```shell\nnpm run serve\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterser%2Fhtml-minifier-terser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fterser%2Fhtml-minifier-terser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterser%2Fhtml-minifier-terser/lists"}