html-minimizer-webpack-plugin
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -18,4 +18,5 @@ "use strict"; | ||
const { | ||
throttleAll, | ||
htmlMinifierTerser, | ||
throttleAll | ||
swcMinify | ||
} = require("./utils"); | ||
@@ -478,2 +479,3 @@ | ||
HtmlMinimizerPlugin.htmlMinifierTerser = htmlMinifierTerser; | ||
HtmlMinimizerPlugin.swcMinify = swcMinify; | ||
module.exports = HtmlMinimizerPlugin; |
@@ -5,2 +5,4 @@ "use strict"; | ||
/** @typedef {import("./index.js").CustomOptions} CustomOptions */ | ||
/** @typedef {import("./index.js").Input} Input */ | ||
@@ -68,3 +70,3 @@ | ||
* @param {Input} input | ||
* @param {HtmlMinifierTerserOptions | undefined} [minimizerOptions] | ||
* @param {CustomOptions | undefined} [minimizerOptions] | ||
* @returns {Promise<MinimizedResult>} | ||
@@ -108,6 +110,27 @@ */ | ||
} | ||
/** | ||
* @param {Input} input | ||
* @param {CustomOptions | undefined} [minimizerOptions] | ||
* @returns {Promise<MinimizedResult>} | ||
*/ | ||
/* istanbul ignore next */ | ||
async function swcMinify(input, minimizerOptions = {}) { | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved | ||
const swcMinifier = require("@swc/html"); | ||
const [[, code]] = Object.entries(input); | ||
const result = await swcMinifier.minify(Buffer.from(code), { ...minimizerOptions | ||
}); | ||
return { | ||
code: result | ||
}; | ||
} | ||
module.exports = { | ||
throttleAll, | ||
htmlMinifierTerser | ||
htmlMinifierTerser, | ||
swcMinify | ||
}; |
{ | ||
"name": "html-minimizer-webpack-plugin", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "html minimizer plugin for Webpack", | ||
@@ -47,6 +47,11 @@ "license": "MIT", | ||
}, | ||
"peerDependenciesMeta": { | ||
"@swc/html": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
"@types/html-minifier-terser": "^7.0.0", | ||
"html-minifier-terser": "^7.0.0", | ||
"jest-worker": "^27.5.1", | ||
"jest-worker": "^29.0.3", | ||
"schema-utils": "^4.0.0", | ||
@@ -57,9 +62,10 @@ "serialize-javascript": "^6.0.0" | ||
"@babel/cli": "^7.18.9", | ||
"@babel/core": "^7.18.9", | ||
"@babel/core": "^7.18.13", | ||
"@babel/preset-env": "^7.18.9", | ||
"@commitlint/cli": "^17.0.3", | ||
"@commitlint/config-conventional": "^17.0.3", | ||
"@commitlint/cli": "^17.1.2", | ||
"@commitlint/config-conventional": "^17.1.0", | ||
"@types/serialize-javascript": "^5.0.2", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^28.1.3", | ||
"babel-jest": "^29.0.3", | ||
"@swc/html": "^0.0.15", | ||
"copy-webpack-plugin": "^9.0.1", | ||
@@ -69,7 +75,7 @@ "cross-env": "^7.0.3", | ||
"del-cli": "^4.0.1", | ||
"eslint": "^8.21.0", | ||
"eslint": "^8.23.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"husky": "^8.0.1", | ||
"jest": "^28.1.3", | ||
"jest": "^29.0.3", | ||
"lint-staged": "^13.0.3", | ||
@@ -76,0 +82,0 @@ "memfs": "^3.4.7", |
@@ -16,4 +16,7 @@ <div align="center"> | ||
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 | ||
@@ -39,2 +42,20 @@ | ||
**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: | ||
@@ -72,3 +93,13 @@ | ||
// `...` | ||
// For `html-minifier-terser`: | ||
// | ||
new HtmlMinimizerPlugin(), | ||
// For `@swc/html`: | ||
// | ||
// new HtmlMinimizerPlugin({ | ||
// minify: HtmlMinimizerPlugin.swcMinify, | ||
// minimizerOptions: {} | ||
// }) | ||
], | ||
@@ -84,2 +115,9 @@ }, | ||
> **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 | ||
@@ -260,2 +298,8 @@ | ||
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. | ||
@@ -410,2 +454,43 @@ | ||
## 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 | ||
@@ -412,0 +497,0 @@ |
@@ -138,2 +138,3 @@ export = HtmlMinimizerPlugin; | ||
htmlMinifierTerser, | ||
swcMinify, | ||
Schema, | ||
@@ -193,2 +194,3 @@ Compiler, | ||
import { htmlMinifierTerser } from "./utils"; | ||
import { swcMinify } from "./utils"; | ||
type Schema = import("schema-utils/declarations/validate").Schema; | ||
@@ -195,0 +197,0 @@ type Compilation = import("webpack").Compilation; |
export type Task<T> = () => Promise<T>; | ||
export type MinimizedResult = import("./index.js").MinimizedResult; | ||
export type CustomOptions = import("./index.js").CustomOptions; | ||
export type Input = import("./index.js").Input; | ||
@@ -19,3 +20,3 @@ export type HtmlMinifierTerserOptions = import("html-minifier-terser").Options; | ||
* @param {Input} input | ||
* @param {HtmlMinifierTerserOptions | undefined} [minimizerOptions] | ||
* @param {CustomOptions | undefined} [minimizerOptions] | ||
* @returns {Promise<MinimizedResult>} | ||
@@ -25,3 +26,12 @@ */ | ||
input: Input, | ||
minimizerOptions?: HtmlMinifierTerserOptions | undefined | ||
minimizerOptions?: CustomOptions | undefined | ||
): Promise<MinimizedResult>; | ||
/** | ||
* @param {Input} input | ||
* @param {CustomOptions | undefined} [minimizerOptions] | ||
* @returns {Promise<MinimizedResult>} | ||
*/ | ||
export function swcMinify( | ||
input: Input, | ||
minimizerOptions?: CustomOptions | undefined | ||
): Promise<MinimizedResult>; |
45027
968
512
25
+ Added@jest/schemas@29.6.3(transitive)
+ Added@jest/types@29.6.3(transitive)
+ Added@sinclair/typebox@0.27.8(transitive)
+ Added@types/istanbul-lib-coverage@2.0.6(transitive)
+ Added@types/istanbul-lib-report@3.0.3(transitive)
+ Added@types/istanbul-reports@3.0.4(transitive)
+ Added@types/yargs@17.0.32(transitive)
+ Added@types/yargs-parser@21.0.3(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedci-info@3.9.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedjest-util@29.7.0(transitive)
+ Addedjest-worker@29.7.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
Updatedjest-worker@^29.0.3