### Install htmlnano Source: https://htmlnano.netlify.app/usage Install htmlnano using npm. This is the first step before using it in your project. ```bash npm install htmlnano ``` -------------------------------- ### Install Dependencies for minifyUrls Source: https://htmlnano.netlify.app/modules Instructions for installing the necessary npm packages to use the minifyUrls module. ```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 ``` -------------------------------- ### Install cssnano and postcss Source: https://htmlnano.netlify.app/modules Installs cssnano and postcss development dependencies using npm, yarn, or pnpm. ```bash npm install --save-dev cssnano postcss # if you prefer yarn # yarn add --dev cssnano postcss # if you prefer pnpm # pnpm install --save-dev cssnano postcss ``` -------------------------------- ### Install uncss for htmlnano Source: https://htmlnano.netlify.app/modules Install the `uncss` package as a development dependency using npm, yarn, or pnpm to enable its use with htmlnano. ```bash npm install --save-dev uncss # if you prefer yarn # yarn add --dev uncss # if you prefer pnpm # pnpm install --save-dev uncss ``` -------------------------------- ### Install gulp-posthtml Source: https://htmlnano.netlify.app/usage Install gulp-posthtml for Gulp integration. This package allows using PostHTML plugins, including htmlnano, within Gulp tasks. ```bash npm i -D gulp-posthtml ``` -------------------------------- ### Install html-minimizer-webpack-plugin Source: https://htmlnano.netlify.app/usage Install the html-minimizer-webpack-plugin for Webpack. This plugin integrates htmlnano into the Webpack build process. ```bash npm install html-minimizer-webpack-plugin --save-dev ``` -------------------------------- ### Collapse Whitespace Example Source: https://htmlnano.netlify.app/modules Demonstrates how collapseWhitespace affects HTML structure with different options. ```html
hello world! answer
``` ```html
hello world!answer
``` ```html
hello world! answer
``` ```html
hello world! answer
``` -------------------------------- ### Configure minifyJs Options Source: https://htmlnano.netlify.app/modules Example of passing Terser options to htmlnano's `minifyJs` module, such as setting quote style. ```javascript htmlnano.process(html, { minifyJs: { output: { quote_style: 1 }, }, }); ``` -------------------------------- ### Configure minifyUrls with String Base URL Source: https://htmlnano.netlify.app/modules Example of configuring the minifyUrls module with a string representing the base URL for relative URL resolution. ```javascript htmlnano.process(html, { minifyUrls: 'https://example.com' // Valid configuration }); ``` -------------------------------- ### Install Terser for JavaScript Minification Source: https://htmlnano.netlify.app/modules Installs Terser development dependency using npm, yarn, or pnpm for JavaScript minification. ```bash npm install --save-dev terser # if you prefer yarn # yarn add --dev terser # if you prefer pnpm # pnpm install --save-dev terser ``` -------------------------------- ### Example of CSS Optimization Source: https://htmlnano.netlify.app/modules Demonstrates the removal of unused CSS rules from a style tag within an HTML structure. ```html
``` ```html
``` -------------------------------- ### Configure minifyUrls with URL Object Source: https://htmlnano.netlify.app/modules Example of configuring the minifyUrls module with a URL object representing the base URL for relative URL resolution. ```javascript htmlnano.process(html, { minifyUrls: new URL('https://example.com') // Valid configuration }); ``` -------------------------------- ### Merge Styles Example Source: https://htmlnano.netlify.app/modules Demonstrates merging multiple style tags with the same normalized media and type attributes into a single tag. Other attributes must also match strictly. ```html ``` ```html ``` -------------------------------- ### Configure Gulp with htmlnano Source: https://htmlnano.netlify.app/usage Set up a Gulp task to minify HTML using htmlnano via gulp-posthtml. This example defines a default task to process HTML files. ```javascript const gulp = require('gulp'); const posthtml = require('gulp-posthtml'); const htmlnano = require('htmlnano'); const options = { removeComments: false }; gulp.task('default', function () { return gulp .src('./index.html') .pipe(posthtml([ // Add `htmlnano` as a final plugin htmlnano(options) ])) .pipe(gulp.dest('./build')); }); ``` -------------------------------- ### Collapse Boolean Attributes Example Source: https://htmlnano.netlify.app/modules Illustrates the transformation of HTML boolean attributes and empty string attributes before and after minification. ```html ``` ```html ``` -------------------------------- ### Install PurgeCSS for CSS Removal Source: https://htmlnano.netlify.app/modules Installs PurgeCSS as a development dependency for the `removeUnusedCss` module in htmlnano. This is the recommended tool for removing unused CSS. ```bash npm install --save-dev purgecss # if you prefer yarn # yarn add --dev purgecss # if you prefer pnpm # pnpm install --save-dev purgecss ``` -------------------------------- ### Configure minifyCss Options Source: https://htmlnano.netlify.app/modules Example of configuring htmlnano's `minifyCss` module with custom cssnano options, such as disabling comment removal. ```javascript htmlnano.process(html, { minifyCss: { preset: ['default', { discardComments: { removeAll: true, }, }] } }); ``` -------------------------------- ### Minify Attributes Example Source: https://htmlnano.netlify.app/modules Demonstrates the minification of meta http-equiv="refresh" content attributes, including removing 'url=' prefixes and trimming whitespace. ```html ``` ```html ``` -------------------------------- ### Deduplicate Attribute Values Example Source: https://htmlnano.netlify.app/modules Shows how the deduplicateAttributeValues module removes duplicate tokens from list-like attributes while preserving whitespace. ```html click ``` ```html click ``` -------------------------------- ### CommonJS JavaScript API for htmlnano Source: https://htmlnano.netlify.app/usage Integrate htmlnano using CommonJS modules. This example shows how to process HTML and handle potential errors. ```javascript const htmlnano = require('htmlnano'); const options = { removeEmptyAttributes: false, collapseWhitespace: 'conservative' }; htmlnano .process(html, options) .then((result) => { // result.html is minified }) .catch((err) => { console.error(err); }); ``` -------------------------------- ### Omit Optional Tags Example Source: https://htmlnano.netlify.app/modules Demonstrates how htmlnano can omit optional HTML tags like html, head, and body when their omission is valid according to HTML standards and htmlnano's rules. ```html Title

Hi

``` ```html Title

Hi

``` -------------------------------- ### Collapse Boolean Attributes Side Effect Example Source: https://htmlnano.netlify.app/modules Demonstrates a potential side effect of the collapseBooleanAttributes module on CSS selectors that rely on specific attribute string values. ```css button[disabled="disabled"] { color: red; } ``` -------------------------------- ### Minify SVG Content Source: https://htmlnano.netlify.app/modules Minifies SVG content within `` tags using SVGO. The example shows a complex SVG structure being reduced to a more compact form, including attribute optimization. ```html SVG ``` ```html SVG ``` -------------------------------- ### Remove Attribute Quotes Example Source: https://htmlnano.netlify.app/modules Shows how htmlnano removes quotes around attribute values when possible, adhering to HTML Standard's unquoted attribute value syntax. ```html
``` ```html
``` -------------------------------- ### Remove Redundant Attributes - HTML Source: https://htmlnano.netlify.app/modules Removes attributes that match default HTML values, such as `method="get"` on `
` or `type="text"` on ``. This module is disabled by default. ```html
``` ```html
``` -------------------------------- ### Create and Use a Custom Preset by Extending 'safe' Source: https://htmlnano.netlify.app/presets Demonstrates creating a custom preset by extending the built-in 'safe' preset and adding or modifying configurations. This allows for reusable custom minification pipelines. ```javascript const htmlnano = require('htmlnano'); const emailPreset = { ...htmlnano.presets.safe, mergeStyles: true, minifyCss: { safe: true } }; htmlnano.process(html, { removeComments: false }, emailPreset) .then((result) => { // result.html is minified }) .catch((err) => { console.error(err); }); ``` -------------------------------- ### htmlnano CLI Help Source: https://htmlnano.netlify.app/usage Display help information for the htmlnano command-line interface. This is useful for understanding available commands and options. ```bash npx htmlnano --help ``` -------------------------------- ### htmlnano CLI with Configuration File Source: https://htmlnano.netlify.app/usage Use htmlnano via the CLI with options specified in a JSON configuration file. This allows for persistent and shareable configurations. ```bash echo '{"collapseWhitespace": "all", "removeComments": "all"}' > config.json npx htmlnano test.html -c config.json ``` -------------------------------- ### Use htmlnano with Custom Options (No Preset) Source: https://htmlnano.netlify.app/presets Illustrates how to process HTML with custom options without applying any preset by passing an empty object as the preset argument. This is useful for fine-grained control. ```javascript const htmlnano = require('htmlnano'); const options = { // Your options }; htmlnano .process(html, options, {}) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` -------------------------------- ### Use ampSafe Preset with htmlnano (CommonJS) Source: https://htmlnano.netlify.app/presets Demonstrates how to use the 'ampSafe' preset with htmlnano using CommonJS modules. Ensure htmlnano is imported and the preset is accessed via htmlnano.presets. ```javascript const htmlnano = require('htmlnano'); const ampSafePreset = require('htmlnano').presets.ampSafe; htmlnano.process(html, { collapseWhitespace: 'conservative' }, ampSafePreset) .then((result) => { // result.html is minified }) .catch((err) => { console.error(err); }); ``` -------------------------------- ### Import and Use ampSafe Preset with htmlnano (ESM) Source: https://htmlnano.netlify.app/presets Shows how to import the 'ampSafe' preset directly for use with htmlnano in an ES Module environment. This method allows for cleaner imports. ```javascript import htmlnano from 'htmlnano'; import ampSafe from 'htmlnano/presets/ampSafe'; const result = await htmlnano.process(html, {}, ampSafe); ``` -------------------------------- ### Configure htmlnano with a JSON preset Source: https://htmlnano.netlify.app/config Use a JSON configuration file to set presets and specific options. This configuration is overridden by direct options passed to htmlnano. ```json { "preset": "max", "collapseWhitespace": "conservative", "removeComments": false } ``` -------------------------------- ### Add multiple custom minification modules Source: https://htmlnano.netlify.app/modules Provide a list of custom minification functions to be applied sequentially by htmlnano. ```javascript const options = { custom: [ function (tree, options) { // Some minification return tree; }, function (tree, options) { // Some other minification return tree; } ] }; ``` -------------------------------- ### Minify URLs with htmlnano.process Source: https://htmlnano.netlify.app/modules Use the minifyUrls option to shorten URLs. Provide a base URL to enable this feature. The module handles relative paths effectively. ```javascript htmlnano.process(html, { minifyUrls: 'https://example.com' }); ``` ```html bar ``` ```html bar ``` ```javascript htmlnano.process(html, { minifyUrls: 'https://example.com/foo/baz/' }); ``` ```html bar ``` ```html bar ``` ```javascript htmlnano.process(html, { minifyUrls: 'https://example.com/foo/baz/' }); ``` ```html ``` ```html ``` -------------------------------- ### Specify custom configuration file path Source: https://htmlnano.netlify.app/config Pass a custom configuration file path to htmlnano.process using the 'configPath' option. ```javascript htmlnano.process(html, { configPath: 'config.json' }) ``` -------------------------------- ### Silence optional dependency warnings Source: https://htmlnano.netlify.app/config Set 'skipInternalWarnings' to true to suppress warnings about missing optional peer dependencies. ```javascript htmlnano.process(html, { skipInternalWarnings: true }) ``` -------------------------------- ### Configure uncss with htmlnano Source: https://htmlnano.netlify.app/modules Pass uncss options directly to the `removeUnusedCss` module. The `stylesheets` and `ignoreSheets` options are ignored when passed this way. ```javascript htmlnano.process(html, { removeUnusedCss: { ignore: ['.do-not-remove'] } }); ``` -------------------------------- ### Invalid minifyUrls Configuration Source: https://htmlnano.netlify.app/modules Illustrates an invalid configuration for the minifyUrls module where setting it to true disables the module. ```javascript htmlnano.process(html, { minifyUrls: true // Invalid configuration, the module will be disabled }); ``` -------------------------------- ### Disable configuration file loading Source: https://htmlnano.netlify.app/config Prevent htmlnano from loading any configuration files by setting 'skipConfigLoading' to true. ```javascript htmlnano.process(html, { skipConfigLoading: true }) ``` -------------------------------- ### Configure Webpack with htmlnano Source: https://htmlnano.netlify.app/usage Configure Webpack to use htmlnano for HTML minification. This involves setting up the HtmlMinimizerWebpackPlugin in the optimization.minimizer array. ```javascript // webpack.config.js const HtmlMinimizerWebpackPlugin = require('html-minimizer-webpack-plugin'); const htmlnano = require('htmlnano'); module.exports = { optimization: { minimize: true, minimizer: [ // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`). // `...`, new HtmlMinimizerWebpackPlugin({ // Add HtmlMinimizerWebpackPlugin options here. // test: /\.html(\?.*)?$/i, // Use htmlnano as HtmlMinimizerWebpackPlugin's minimizer. minify: htmlnano.htmlMinimizerWebpackPluginMinify, minimizerOptions: { // Add htmlnano options here. removeComments: false, collapseWhitespace: 'conservative' } }) ] } } ``` -------------------------------- ### Configure SVGO Options for SVG Minification Source: https://htmlnano.netlify.app/modules Configures htmlnano's SVG minification module using SVGO. Options are passed directly to SVGO, including plugin presets and specific plugin parameters. ```javascript htmlnano.process(html, { minifySvg: { plugins: [ { name: 'preset-default', params: { overrides: { builtinPluginName: { optionName: 'optionValue' }, }, }, } ] } }); ``` -------------------------------- ### Collapse Boolean Attributes Configuration Source: https://htmlnano.netlify.app/modules Configure the collapseBooleanAttributes module to handle AMP-specific boolean attributes when their value is empty, true, or matches the attribute name. ```json "collapseBooleanAttributes": { "amphtml": true } ``` -------------------------------- ### Configure PurgeCSS with htmlnano Source: https://htmlnano.netlify.app/modules Pass PurgeCSS options directly to the `removeUnusedCss` module. The `content` and `css` options are ignored when passed this way. ```javascript htmlnano.process(html, { removeUnusedCss: { tool: 'purgeCSS', safelist: ['.do-not-remove'] } }); ``` -------------------------------- ### Add a single custom minification module Source: https://htmlnano.netlify.app/modules Integrate a custom minification function into htmlnano's processing pipeline. ```javascript const options = { custom: function (tree, options) { // Some minification return tree; } }; ``` -------------------------------- ### Disable minifyUrls Module Source: https://htmlnano.netlify.app/modules Shows how to disable the minifyUrls module by setting its option to false. ```javascript htmlnano.process(html, { minifyUrls: false // The module will be disabled }); ``` -------------------------------- ### Integrate htmlnano with PostHTML Source: https://htmlnano.netlify.app/usage Add htmlnano as a final plugin in your PostHTML pipeline. This allows htmlnano to process HTML after other PostHTML plugins. ```javascript const posthtml = require('posthtml'); const options = { removeComments: false, collapseWhitespace: 'conservative' }; const posthtmlPlugins = [ /* other PostHTML plugins */ require('htmlnano')(options) ]; const posthtmlOptions = { // See PostHTML docs }; posthtml(posthtmlPlugins) .process(html, posthtmlOptions) .then((result) => { // result.html is minified }) .catch((err) => { console.error(err); }); ``` -------------------------------- ### Sort attributes alphabetically Source: https://htmlnano.netlify.app/modules Sorts attributes within HTML elements alphabetically. This can improve gzip/brotli compression ratios. ```html ``` ```html ``` -------------------------------- ### Configure Minify HTML Template Rules Source: https://htmlnano.netlify.app/modules Minifies HTML within template containers like ` ``` ```html ``` -------------------------------- ### Minify CSS within Style Tags Source: https://htmlnano.netlify.app/modules Demonstrates minification of CSS within ` ``` ```html
``` -------------------------------- ### Remove Empty Elements (Basic) Source: https://htmlnano.netlify.app/modules Removes empty HTML elements that do not have any attributes. ```html
hello
``` ```html
hello
``` -------------------------------- ### ESM JavaScript API for htmlnano Source: https://htmlnano.netlify.app/usage Use htmlnano with ECMAScript Modules (ESM) for processing HTML. You can pass custom options and PostHTML options. ```javascript import htmlnano, { presets } from 'htmlnano'; const options = { removeEmptyAttributes: false, collapseWhitespace: 'conservative' }; // See PostHTML docs const postHtmlOptions = { sync: true, lowerCaseTags: true, quoteAllAttributes: false }; // "preset" arg might be skipped (see "Presets" section below for more info) // "postHtmlOptions" arg might be skipped const result = await htmlnano.process(html, options, presets.safe, postHtmlOptions); // result.html is minified ``` -------------------------------- ### Sort list-like attribute values by frequency Source: https://htmlnano.netlify.app/modules Sorts values in list-like attributes by their frequency across the document. Preserves duplicates and ignores redundant whitespace. ```html
``` ```html
``` -------------------------------- ### Normalize Attribute Values - HTML Source: https://htmlnano.netlify.app/modules Normalizes casing and applies default values for specific case-insensitive attributes like `method`, `loading`, and `type`. Trims whitespace from normalized values. ```html
``` ```html
``` -------------------------------- ### Minify JSON in Script Tags Source: https://htmlnano.netlify.app/modules Processes ` ``` ```html ``` ```html ``` ```html ``` -------------------------------- ### Sort attributes by frequency Source: https://htmlnano.netlify.app/modules Sorts attributes based on their frequency across the document. Preserves original casing and uses alphabetical order for ties. ```html image ``` ```html image ``` -------------------------------- ### Remove HTML Comments via JSON Config Source: https://htmlnano.netlify.app/modules Demonstrates configuring comment removal using a JSON configuration file with a regular expression. ```json { "removeComments": "//" } ``` -------------------------------- ### Remove Empty Elements (With Attributes) Source: https://htmlnano.netlify.app/modules Removes empty HTML elements, even if they possess attributes. This can affect elements used for styling or scripting. ```html
hello
``` -------------------------------- ### Force Remove Attribute Quotes Source: https://htmlnano.netlify.app/modules Illustrates how to force the removal of attribute quotes using the `force` option, even when `quoteAllAttributes` is set to true in PostHTML. ```javascript posthtml([ htmlnano({ removeAttributeQuotes: true }) ]).process(html, { quoteAllAttributes: true }) ``` ```javascript posthtml([ htmlnano({ removeAttributeQuotes: { force: true } }) ]).process(html, { quoteAllAttributes: true }) ``` -------------------------------- ### Minify Conditional Comments Source: https://htmlnano.netlify.app/modules Minifies the HTML content within IE conditional comments while preserving the comment wrappers. ```html ``` ```html ``` -------------------------------- ### Merge Inline JavaScript Snippets Source: https://htmlnano.netlify.app/modules Merges inline JavaScript snippets with mergeable types (`text/javascript`, `application/javascript`). Scripts with `src` or `integrity` attributes, or different boolean attributes (`async`, `defer`, `nomodule`), are not merged. ```html ``` ```html ``` -------------------------------- ### Remove Empty Attributes - HTML Source: https://htmlnano.netlify.app/modules Removes attributes that are empty or contain only whitespace. Event handlers are always removed when empty. Some attributes are removed only on specific tags. ```html
``` ```html
``` -------------------------------- ### Minify JavaScript within Script Tags Source: https://htmlnano.netlify.app/modules Minifies JavaScript code within ` ``` ```html
``` -------------------------------- ### Collapse Attribute Whitespace - HTML Source: https://htmlnano.netlify.app/modules Collapses redundant whitespace within attribute values for list-like attributes (e.g., `class`, `rel`) and trims whitespace for single-value attributes (e.g., `href`, `style`). ```html ``` ```html ``` -------------------------------- ### Remove All HTML Comments Source: https://htmlnano.netlify.app/modules Configures htmlnano to remove all HTML comments, including conditional ones. ```javascript { removeComments: 'all' } ``` ```html
``` ```html
``` -------------------------------- ### Remove HTML Comments Matching RegExp Source: https://htmlnano.netlify.app/modules Removes HTML comments that match a specified regular expression. Non-matching comments are retained. ```javascript { removeComments: // } ``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` -------------------------------- ### Merge Adjacent Script Tags Source: https://htmlnano.netlify.app/modules Merges adjacent inline script tags that share the same normalized attributes (excluding src, integrity, and type). Content is appended to the last script. ```html ``` ```html ``` -------------------------------- ### Remove Safe HTML Comments Source: https://htmlnano.netlify.app/modules Configures htmlnano to remove only standard HTML comments, preserving special types like conditional comments and excerpt markers. ```javascript { removeComments: 'safe' } ``` ```html indexed? Lorem ipsum dolor sit amet ``` ```html indexed? Lorem ipsum dolor sit amet ``` -------------------------------- ### Attribute Selector Side Effect - CSS Source: https://htmlnano.netlify.app/modules Demonstrates a potential side effect of removing empty attributes, where CSS rules targeting empty attributes might be broken. ```css img[style=""] { margin: 10px; } ``` -------------------------------- ### Attribute Selector Side Effect - CSS Source: https://htmlnano.netlify.app/modules Illustrates a potential side effect of removing redundant attributes, where CSS rules targeting specific default attribute values might become ineffective. ```css form[method="get"] { color: red; } ``` -------------------------------- ### Remove HTML Comments via Function Source: https://htmlnano.netlify.app/modules Removes HTML comments based on a custom function's return value. Comments for which the function returns true are removed. ```javascript { removeComments: (comment) => { if (comment.includes('noindex')) return true; return false; } } ``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` ```html
this text will not be indexedLorem ipsum dolor sit ametLorem ipsum dolor sit amet
``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.