Skip to content

Commit

Permalink
feat: added SWC HTML minifier (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Sep 29, 2022
1 parent b66a43a commit 8481f8c
Show file tree
Hide file tree
Showing 9 changed files with 1,639 additions and 1,081 deletions.
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

# html-minimizer-webpack-plugin

This plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) to optimize and minify your HTML.
This plugin can use 2 tools to optimize and minify your HTML:

- [`html-minifier-terser`](https://github.com/imagemin/imagemin) (by default) - JavaScript-based HTML minifier.
- [`swc`](https://github.com/swc-project/swc) - very fast Rust-based platform for the Web.

## Getting Started

Expand All @@ -35,6 +38,24 @@ or
pnpm add -D html-minimizer-webpack-plugin
```

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

```console
npm install @swc/html --save-dev
```

or

```console
yarn add -D @swc/html
```

or

```console
pnpm add -D @swc/html
```

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

**webpack.config.js**
Expand Down Expand Up @@ -67,7 +88,17 @@ module.exports = {
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// `...`

// For `html-minifier-terser`:
//
new HtmlMinimizerPlugin(),

// For `@swc/html`:
//
// new HtmlMinimizerPlugin({
// minify: HtmlMinimizerPlugin.swcMinify,
// minimizerOptions: {}
// })
],
},
};
Expand All @@ -78,6 +109,13 @@ If you want to run it also in development set the `optimization.minimize` option

And run `webpack` via your preferred method.

> **Note**
>
> 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.)
## Options

- **[`test`](#test)**
Expand Down Expand Up @@ -253,6 +291,12 @@ Default: `HtmlMinimizerPlugin.htmlMinifierTerser`

Allows you to override default minify function.
By default, plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) package.

We currently support:

- `HtmlMinimizerPlugin.htmlMinifierTerser`
- `HtmlMinimizerPlugin.swcMinify`

Useful for using and testing unpublished versions or forks.

> **Warning**
Expand Down Expand Up @@ -402,6 +446,47 @@ module.exports = {
};
```

## Examples

### `swc/html`

```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.swcMinify,
minimizerOptions: {
// Options
},
}),
],
},
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
Loading

0 comments on commit 8481f8c

Please sign in to comment.