Skip to content

Commit

Permalink
feat: added @minify-html/node support (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 10, 2023
1 parent 057a43d commit 966de45
Show file tree
Hide file tree
Showing 9 changed files with 2,006 additions and 15,703 deletions.
74 changes: 69 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

# html-minimizer-webpack-plugin

This plugin can use 2 tools to optimize and minify your HTML:
This plugin can use 3 tools to optimize and minify your HTML:

- [`html-minifier-terser`](https://github.com/terser/html-minifier-terser) (by default) - JavaScript-based HTML minifier.
- [`swc`](https://github.com/swc-project/swc) - very fast Rust-based platform for the Web.
- [`html-minifier-terser`](https://github.com/terser/html-minifier-terser) (by default) - JavaScript-based HTML minifier.
- [`@minify-html/node`](https://github.com/wilsonzlin/minify-html) - A Rust HTML minifier meticulously optimised for speed and effectiveness, with bindings for other languages.

## Getting Started

Expand Down Expand Up @@ -56,6 +57,24 @@ or
pnpm add -D @swc/html
```

**Additional step**: If you want to use `@minify-html/node` you need to install it:

```console
npm install @minify-html/node --save-dev
```

or

```console
yarn add -D @minify-html/node
```

or

```console
pnpm add -D @minify-html/node
```

Then add the plugin to your `webpack` configuration. For example:

**webpack.config.js**
Expand Down Expand Up @@ -127,8 +146,9 @@ And run `webpack` via your preferred method.
>
> Removing and collapsing spaces in the tools differ (by default).
>
> - `html-minifier-terser` - always collapse multiple whitespaces to 1 space (never remove it entirely), but you can change it using [`options`](https://github.com/terser/html-minifier-terser#options-quick-reference)
> - `@swc/html` - remove and collapse whitespaces only in safe places (for example - around `html` and `body` elements, inside the `head` element and between metadata elements - `<meta>`/`script`/`link`/etc.)
> - `html-minifier-terser` - always collapse multiple whitespaces to 1 space (never remove it entirely), but you can change it using [`options`](https://github.com/terser/html-minifier-terser#options-quick-reference)
> - `@minify-html/node` - please read documentation https://github.com/wilsonzlin/minify-html#whitespace
## Options

Expand Down Expand Up @@ -308,9 +328,10 @@ By default, plugin uses [html-minifier-terser](https://github.com/terser/html-mi

We currently support:

- `HtmlMinimizerPlugin.htmlMinifierTerser`
- `HtmlMinimizerPlugin.swcMinify` (used to compress HTML documents, i.e. with HTML doctype and `<html>`/`<body>`/`<head>` tags)
- `HtmlMinimizerPlugin.swcMinifyFragment` (used to compress HTML fragments, i.e. when you have part of HTML which will be inserted into another HTML parts)
- `HtmlMinimizerPlugin.htmlMinifierTerser`
- `HtmlMinimizerPlugin.minifyHtmlNode`

> **Note**
>
Expand Down Expand Up @@ -511,7 +532,7 @@ module.exports = {
};
```

HTML Framgents:
HTML Fragments:

```js
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
Expand Down Expand Up @@ -551,6 +572,49 @@ module.exports = {
};
```

### `@minify-html/node`

Available [`options`](https://github.com/wilsonzlin/minify-html#minification).

HTML Documents:

```js
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
module: {
rules: [
{
test: /\.html$/i,
type: "asset/resource",
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "dist"),
from: "./src/*.html",
},
],
}),
],
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
minify: HtmlMinimizerPlugin.minifyHtmlNode,
minimizerOptions: {
// Options - https://github.com/wilsonzlin/minify-html#minification
},
}),
],
},
};
```

You can use multiple `HtmlMinimizerPlugin` plugins to compress different files with the different `minify` function.

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
"*": ["prettier --write --ignore-unknown", "cspell"],
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
"*.js": ["eslint --cache --fix"],
};
Loading

0 comments on commit 966de45

Please sign in to comment.