### 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
```
--------------------------------
### 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 `