# Modules By default the modules should only perform safe transforms, see the module documentation below for details. You can disable modules by passing `false` as option, and enable them by passing `true`. ### collapseAttributeWhitespace Collapse redundant white spaces in list-like attributes (`class`, `rel`, `ping`). #### Example Source: ```html ``` Minified: ```html ``` ### collapseWhitespace Collapses redundant white spaces (including new lines). It doesn’t affect white spaces in the elements `
``` Minified (with `all`): ```html
hello world!answer
``` Minified (with `aggressive`): ```html
hello world! answer
``` Minified (with `conservative`): ```html
hello world! answer
``` ### deduplicateAttributeValues Remove duplicate values from list-like attributes (`class`, `rel`, `ping`). #### Example Source: ```html ``` Minified: ```html ``` ### removeComments #### Options - `safe` – removes all HTML comments except the conditional comments and [``](https://yandex.com/support/webmaster/controlling-robot/html.xml) (default) - `all` — removes all HTML comments - A `RegExp` — only HTML comments matching the given regexp will be removed. - A `Function` that returns boolean — removes HTML comments that can make the given callback function returns truthy value. #### Example Source: ```js { removeComments: 'all' } ``` ```html
``` Minified: ```html
``` Source: ```js { removeComments: // } ``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` Minified: ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` Source: ```js { removeComments: (comments) => { if (comments.includes('noindex')) return true; return false; } } ``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` Minified: ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` ### removeEmptyAttributes Removes empty [safe-to-remove](https://github.com/posthtml/htmlnano/blob/master/lib/modules/removeEmptyAttributes.es6) attributes. #### Side effects This module could break your styles or JS if you use selectors with attributes: ```CSS img[style=""] { margin: 10px; } ``` #### Example Source: ```html ``` Minified: ```html ``` ### removeAttributeQuotes Remove quotes around attributes when possible, see [HTML Standard - 12.1.2.3 Attributes - Unquoted attribute value syntax](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). #### Example Source: ```html
``` Minified: ```html
``` #### Notice The feature is implemented by [posthtml-render's `quoteAllAttributes`](https://github.com/posthtml/posthtml-render#options), which is one of the PostHTML's option. So `removeAttributeQuotes` could be overriden by other PostHTML's plugins and PostHTML's configuration. For example: ```js posthtml([ htmlnano({ removeAttributeQuotes: true }) ]).process(html, { quoteAllAttributes: true }) ``` `removeAttributeQuotes` will not work because PostHTML's `quoteAllAttributes` takes the priority. ### removeUnusedCss Removes unused CSS inside ` ``` Optimized: ```html
``` ### minifyCss Minifies CSS with [cssnano](http://cssnano.co/) inside ` ``` Minified: ```html
``` ### minifyJs Minifies JS using [Terser](https://github.com/fabiosantoscode/terser) inside ` ``` Minified: ```html
``` ### minifyJson Minifies JSON inside ``. #### Example Source: ```html ``` Minified: ```html ``` ### minifySvg Minifies SVG inside `` tags using [SVGO](https://github.com/svg/svgo/). #### Options See [the documentation of SVGO](https://github.com/svg/svgo/blob/master/README.md) for all supported options. SVGO options can be passed directly to the `minifySvg` module: ```js htmlnano.process(html, { minifySvg: { plugins: [ { name: 'preset-default', params: { overrides: { builtinPluginName: { optionName: 'optionValue' }, }, }, } ] } }); ``` #### Example Source: ```html SVG ` ``` Minified: ```html SVG ``` ### minifyConditionalComments Minify content inside conditional comments. #### Example Source: ```html ``` Minified: ```html ``` ### removeRedundantAttributes Removes redundant attributes from tags if they contain default values: - `method="get"` from `
` - `type="text"` from `
click
``` Processed: ```html
click
``` **frequency** Source: ```html
``` Processed: ```html
``` ### sortAttributes Sort attributes inside elements. The module won't impact the plain-text size of the output. However it will improve the compression ratio of gzip/brotli used in HTTP compression. #### Options - `alphabetical`: Default option. Sort attributes in alphabetical order. - `frequency`: Sort attributes by frequency. #### Example **alphabetical** Source: ```html ``` Processed: ```html ``` **frequency** Source: ```html image ``` Processed: ```html image ``` ### minifyUrls Convert absolute URL to relative URL using [relateurl](https://www.npmjs.com/package/relateurl). You have to install `relateurl`, `terser` and `srcset` in order to use this feature: ```bash npm install --save-dev relateurl terser srcset # if you prefer yarn # yarn add --dev relateurl terser srcset # if you prefer pnpm # pnpm install --save-dev relateurl terser srcset ``` #### Options The base URL to resolve against. Support `String` & `URL`. ```js htmlnano.process(html, { minifyUrls: 'https://example.com' // Valid configuration }); ``` ```js htmlnano.process(html, { minifyUrls: new URL('https://example.com') // Valid configuration }); ``` ```js htmlnano.process(html, { minifyUrls: false // The module will be disabled }); ``` ```js htmlnano.process(html, { minifyUrls: true // Invalid configuration, the module will be disabled }); ``` #### Example **Basic Usage** Configuration: ```js htmlnano.process(html, { minifyUrls: 'https://example.com' }); ``` Source: ```html bar ``` Minified: ```html bar ``` **With sub-directory** Configuration: ```js htmlnano.process(html, { minifyUrls: 'https://example.com/foo/baz/' }); ``` Source: ```html bar ``` Minified: ```html bar ``` ### removeOptionalTags Remove certain tags that can be omitted, see [HTML Standard - 13.1.2.4 Optional tags](https://html.spec.whatwg.org/multipage/syntax.html#optional-tags). #### Example Source: ```html Title

Hi

``` Minified: ```html Title

Hi

``` #### Notice Due to [the limitation of PostHTML](https://github.com/posthtml/htmlnano/issues/99), htmlnano can't remove only the start tag or the end tag of an element. Currently, htmlnano only supports removing the following optional tags, as htmlnano can remove their start tag and end tag at the same time: - `html` - `head` - `body` - `colgroup` - `tbody` ### normalizeAttributeValues Normalize casing of attribute values. The module won't impact the plain-text size of the output. However it will improve the compression ratio of gzip/brotli used in HTTP compression. #### Example Source: ```html
``` Minified: ```html
```