From 3b7ea1d8af0f2d897bb0bad9ab730fd18c3d15d5 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 9 Sep 2022 21:19:44 +0300 Subject: [PATCH] feat: added SWC HTML minifier --- README.md | 39 +- package-lock.json | 350 ++++++++++++++++++ package.json | 6 + src/index.js | 3 +- src/utils.js | 20 +- test/__snapshots__/minify-option.test.js.snap | 30 ++ test/minify-option.test.js | 33 ++ types/index.d.ts | 2 + types/utils.d.ts | 9 + 9 files changed, 489 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 40d0705..b27ab6b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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** @@ -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: {} + // }) ], }, }; @@ -253,6 +284,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** diff --git a/package-lock.json b/package-lock.json index 5bf62a9..9ff874e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@babel/preset-env": "^7.18.9", "@commitlint/cli": "^17.1.2", "@commitlint/config-conventional": "^17.1.0", + "@swc/html": "^0.0.5", "@types/serialize-javascript": "^5.0.2", "@webpack-contrib/eslint-config-webpack": "^3.0.0", "babel-jest": "^28.1.3", @@ -50,6 +51,11 @@ }, "peerDependencies": { "webpack": "^5.1.0" + }, + "peerDependenciesMeta": { + "@swc/html": { + "optional": true + } } }, "node_modules/@ampproject/remapping": { @@ -3270,6 +3276,238 @@ "@sinonjs/commons": "^1.7.0" } }, + "node_modules/@swc/html": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@swc/html/-/html-0.0.5.tgz", + "integrity": "sha512-vyRSxLj0U3x4jDT7lNyPnI/td0CbkN2niWvaTp7bdwjB7XSmU0GOxSh+kkg9yzTMKtBKkRz0Q3Rd51GbyjDMVg==", + "dev": true, + "engines": { + "node": ">=10" + }, + "optionalDependencies": { + "@swc/html-android-arm-eabi": "0.0.7", + "@swc/html-android-arm64": "0.0.7", + "@swc/html-darwin-arm64": "0.0.7", + "@swc/html-darwin-x64": "0.0.7", + "@swc/html-freebsd-x64": "0.0.7", + "@swc/html-linux-arm-gnueabihf": "0.0.7", + "@swc/html-linux-arm64-gnu": "0.0.7", + "@swc/html-linux-arm64-musl": "0.0.7", + "@swc/html-linux-x64-gnu": "0.0.7", + "@swc/html-linux-x64-musl": "0.0.7", + "@swc/html-win32-arm64-msvc": "0.0.7", + "@swc/html-win32-ia32-msvc": "0.0.7", + "@swc/html-win32-x64-msvc": "0.0.7" + } + }, + "node_modules/@swc/html-android-arm-eabi": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-android-arm-eabi/-/html-android-arm-eabi-0.0.7.tgz", + "integrity": "sha512-i5z4Fmnex/RQrD5pMkTDi3mmRNEn9ybR8UjXZA94hXuxA6H4qEWH7tb7l1xK0TxD41/fEfKHRx3IHP30gubPJg==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-android-arm64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-android-arm64/-/html-android-arm64-0.0.7.tgz", + "integrity": "sha512-nsVJoVXNKnmQ00GxU9a0IAq9C04DcD00h2MtbQ5o6C7jOOAXX6CppL+bGo8JK8kvPYGEwuoZWI7WVRFYzvyNmg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-darwin-arm64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-darwin-arm64/-/html-darwin-arm64-0.0.7.tgz", + "integrity": "sha512-lro4wBZ4FIXUiYzeG12Ne+MfSdeZy7jj0dHcpsVNEwpJRsVeFTBQc98LoTgglzfKsDL5EmkA4U8Npf21KidbTw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-darwin-x64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-darwin-x64/-/html-darwin-x64-0.0.7.tgz", + "integrity": "sha512-DXp2jgAWzwDi/c2LITe3YNmzLc04FIfs1weK+745rwh+uX/xgVNbI0UZflKPnc3T0OcmB2OjBIOuqCVJeJqCow==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-freebsd-x64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-freebsd-x64/-/html-freebsd-x64-0.0.7.tgz", + "integrity": "sha512-q2gDDTFtMjlSuyQpsOK8KZXMHEuoB5GpcLZ+21j8toxioU1uSnXENCZkHr3tzDaR6knZlDBrfgiUTN+r2rTepQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-linux-arm-gnueabihf": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm-gnueabihf/-/html-linux-arm-gnueabihf-0.0.7.tgz", + "integrity": "sha512-hj1sa/F5dXYybn7oCCeVh93vKP1KY/vuWX5N3NANEVSYm8tTAqS8+3xtOtxZbqNMcmWiFTnjeZa5gF01AdU1dA==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-linux-arm64-gnu": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm64-gnu/-/html-linux-arm64-gnu-0.0.7.tgz", + "integrity": "sha512-QGfQ+HQYd6z8ODSoGuXZxUrpNU/K/JsCGYIjlm9AtTokZkoPY8b16bwck9aVGhE0m2J2N46M2gkoc+Nc9j14yg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-linux-arm64-musl": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm64-musl/-/html-linux-arm64-musl-0.0.7.tgz", + "integrity": "sha512-OUXkKLrzAIW+dZSf2spi2hJu5j2NAfWyWWRLnk4q+6SS3dUJFvYuOR+gh5UCrvsI6YsdvL5nPLsdSRESB//Vqw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-linux-x64-gnu": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-x64-gnu/-/html-linux-x64-gnu-0.0.7.tgz", + "integrity": "sha512-UNCmnDuExhnYzOYJVjNjJVgdPxXME4wPkeJ0ZC6MSRUCc9ecWdGrRaIMH9Ox1CurAxteedHLel7YZSETnj91Eg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-linux-x64-musl": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-x64-musl/-/html-linux-x64-musl-0.0.7.tgz", + "integrity": "sha512-1yv5l0BuC11JgpX6Mow+EwjbdYMzbGUWAOJCz76LSCulklq9H2xCyY4UHMEo42IzC2faqPPQlrYp03NvVmv2yw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-win32-arm64-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-arm64-msvc/-/html-win32-arm64-msvc-0.0.7.tgz", + "integrity": "sha512-TCBdUfFoCExetwuGmpV84pyxho5Kpy484v4D3SBnTPSd1zq1kiMD2HeIve8vh3kK6p2FIM/VNMzrZJsdmsfdig==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-win32-ia32-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-ia32-msvc/-/html-win32-ia32-msvc-0.0.7.tgz", + "integrity": "sha512-4BDgtxrLY1UX7TOxAVtbhVknNtagXm8CY8LEMN1vHEh4tp1P066qLA+9F2UwA++dT25Ih13fDISm0hG6YffUtg==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/html-win32-x64-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-x64-msvc/-/html-win32-x64-msvc-0.0.7.tgz", + "integrity": "sha512-sHi3HmAqWDpgc1t3jHc5PsEq4MJLk32lyKWr90ubGeOJPlroClj75+pXgJ1paQ61592uV1CIBSne6daF13fkjw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, "node_modules/@tsconfig/node10": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", @@ -14959,6 +15197,118 @@ "@sinonjs/commons": "^1.7.0" } }, + "@swc/html": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@swc/html/-/html-0.0.5.tgz", + "integrity": "sha512-vyRSxLj0U3x4jDT7lNyPnI/td0CbkN2niWvaTp7bdwjB7XSmU0GOxSh+kkg9yzTMKtBKkRz0Q3Rd51GbyjDMVg==", + "dev": true, + "requires": { + "@swc/html-android-arm-eabi": "0.0.7", + "@swc/html-android-arm64": "0.0.7", + "@swc/html-darwin-arm64": "0.0.7", + "@swc/html-darwin-x64": "0.0.7", + "@swc/html-freebsd-x64": "0.0.7", + "@swc/html-linux-arm-gnueabihf": "0.0.7", + "@swc/html-linux-arm64-gnu": "0.0.7", + "@swc/html-linux-arm64-musl": "0.0.7", + "@swc/html-linux-x64-gnu": "0.0.7", + "@swc/html-linux-x64-musl": "0.0.7", + "@swc/html-win32-arm64-msvc": "0.0.7", + "@swc/html-win32-ia32-msvc": "0.0.7", + "@swc/html-win32-x64-msvc": "0.0.7" + } + }, + "@swc/html-android-arm-eabi": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-android-arm-eabi/-/html-android-arm-eabi-0.0.7.tgz", + "integrity": "sha512-i5z4Fmnex/RQrD5pMkTDi3mmRNEn9ybR8UjXZA94hXuxA6H4qEWH7tb7l1xK0TxD41/fEfKHRx3IHP30gubPJg==", + "dev": true, + "optional": true + }, + "@swc/html-android-arm64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-android-arm64/-/html-android-arm64-0.0.7.tgz", + "integrity": "sha512-nsVJoVXNKnmQ00GxU9a0IAq9C04DcD00h2MtbQ5o6C7jOOAXX6CppL+bGo8JK8kvPYGEwuoZWI7WVRFYzvyNmg==", + "dev": true, + "optional": true + }, + "@swc/html-darwin-arm64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-darwin-arm64/-/html-darwin-arm64-0.0.7.tgz", + "integrity": "sha512-lro4wBZ4FIXUiYzeG12Ne+MfSdeZy7jj0dHcpsVNEwpJRsVeFTBQc98LoTgglzfKsDL5EmkA4U8Npf21KidbTw==", + "dev": true, + "optional": true + }, + "@swc/html-darwin-x64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-darwin-x64/-/html-darwin-x64-0.0.7.tgz", + "integrity": "sha512-DXp2jgAWzwDi/c2LITe3YNmzLc04FIfs1weK+745rwh+uX/xgVNbI0UZflKPnc3T0OcmB2OjBIOuqCVJeJqCow==", + "dev": true, + "optional": true + }, + "@swc/html-freebsd-x64": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-freebsd-x64/-/html-freebsd-x64-0.0.7.tgz", + "integrity": "sha512-q2gDDTFtMjlSuyQpsOK8KZXMHEuoB5GpcLZ+21j8toxioU1uSnXENCZkHr3tzDaR6knZlDBrfgiUTN+r2rTepQ==", + "dev": true, + "optional": true + }, + "@swc/html-linux-arm-gnueabihf": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm-gnueabihf/-/html-linux-arm-gnueabihf-0.0.7.tgz", + "integrity": "sha512-hj1sa/F5dXYybn7oCCeVh93vKP1KY/vuWX5N3NANEVSYm8tTAqS8+3xtOtxZbqNMcmWiFTnjeZa5gF01AdU1dA==", + "dev": true, + "optional": true + }, + "@swc/html-linux-arm64-gnu": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm64-gnu/-/html-linux-arm64-gnu-0.0.7.tgz", + "integrity": "sha512-QGfQ+HQYd6z8ODSoGuXZxUrpNU/K/JsCGYIjlm9AtTokZkoPY8b16bwck9aVGhE0m2J2N46M2gkoc+Nc9j14yg==", + "dev": true, + "optional": true + }, + "@swc/html-linux-arm64-musl": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-arm64-musl/-/html-linux-arm64-musl-0.0.7.tgz", + "integrity": "sha512-OUXkKLrzAIW+dZSf2spi2hJu5j2NAfWyWWRLnk4q+6SS3dUJFvYuOR+gh5UCrvsI6YsdvL5nPLsdSRESB//Vqw==", + "dev": true, + "optional": true + }, + "@swc/html-linux-x64-gnu": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-x64-gnu/-/html-linux-x64-gnu-0.0.7.tgz", + "integrity": "sha512-UNCmnDuExhnYzOYJVjNjJVgdPxXME4wPkeJ0ZC6MSRUCc9ecWdGrRaIMH9Ox1CurAxteedHLel7YZSETnj91Eg==", + "dev": true, + "optional": true + }, + "@swc/html-linux-x64-musl": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-linux-x64-musl/-/html-linux-x64-musl-0.0.7.tgz", + "integrity": "sha512-1yv5l0BuC11JgpX6Mow+EwjbdYMzbGUWAOJCz76LSCulklq9H2xCyY4UHMEo42IzC2faqPPQlrYp03NvVmv2yw==", + "dev": true, + "optional": true + }, + "@swc/html-win32-arm64-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-arm64-msvc/-/html-win32-arm64-msvc-0.0.7.tgz", + "integrity": "sha512-TCBdUfFoCExetwuGmpV84pyxho5Kpy484v4D3SBnTPSd1zq1kiMD2HeIve8vh3kK6p2FIM/VNMzrZJsdmsfdig==", + "dev": true, + "optional": true + }, + "@swc/html-win32-ia32-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-ia32-msvc/-/html-win32-ia32-msvc-0.0.7.tgz", + "integrity": "sha512-4BDgtxrLY1UX7TOxAVtbhVknNtagXm8CY8LEMN1vHEh4tp1P066qLA+9F2UwA++dT25Ih13fDISm0hG6YffUtg==", + "dev": true, + "optional": true + }, + "@swc/html-win32-x64-msvc": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@swc/html-win32-x64-msvc/-/html-win32-x64-msvc-0.0.7.tgz", + "integrity": "sha512-sHi3HmAqWDpgc1t3jHc5PsEq4MJLk32lyKWr90ubGeOJPlroClj75+pXgJ1paQ61592uV1CIBSne6daF13fkjw==", + "dev": true, + "optional": true + }, "@tsconfig/node10": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", diff --git a/package.json b/package.json index 2c323b7..98f4999 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,11 @@ "peerDependencies": { "webpack": "^5.1.0" }, + "peerDependenciesMeta": { + "@swc/html": { + "optional": true + } + }, "dependencies": { "@types/html-minifier-terser": "^7.0.0", "html-minifier-terser": "^7.0.0", @@ -59,6 +64,7 @@ "@commitlint/config-conventional": "^17.1.0", "@types/serialize-javascript": "^5.0.2", "@webpack-contrib/eslint-config-webpack": "^3.0.0", + "@swc/html": "^0.0.5", "babel-jest": "^28.1.3", "copy-webpack-plugin": "^9.0.1", "cross-env": "^7.0.3", diff --git a/src/index.js b/src/index.js index 0c1a4c4..03f7c63 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ const { Worker } = require("jest-worker"); const schema = require("./options.json"); -const { htmlMinifierTerser, throttleAll } = require("./utils"); +const { throttleAll, htmlMinifierTerser, swcMinify } = require("./utils"); const { minify } = require("./minify"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ @@ -474,5 +474,6 @@ class HtmlMinimizerPlugin { } HtmlMinimizerPlugin.htmlMinifierTerser = htmlMinifierTerser; +HtmlMinimizerPlugin.swcMinify = swcMinify; module.exports = HtmlMinimizerPlugin; diff --git a/src/utils.js b/src/utils.js index 209ba85..576cfb6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -102,4 +102,22 @@ async function htmlMinifierTerser(input, minimizerOptions = {}) { return { code: result }; } -module.exports = { throttleAll, htmlMinifierTerser }; +/** + * @param {Input} input + * @param {* | undefined} [minimizerOptions] + * @returns {Promise} + */ +/* 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(code, { + ...minimizerOptions, + }); + + return { code: result }; +} + +module.exports = { throttleAll, htmlMinifierTerser, swcMinify }; diff --git a/test/__snapshots__/minify-option.test.js.snap b/test/__snapshots__/minify-option.test.js.snap index b935967..c77bfb0 100644 --- a/test/__snapshots__/minify-option.test.js.snap +++ b/test/__snapshots__/minify-option.test.js.snap @@ -68,3 +68,33 @@ Object { exports[`"minify" option should work minify function: errors 1`] = `Array []`; exports[`"minify" option should work minify function: warnings 1`] = `Array []`; + +exports[`"minify" option should work with 'swcMinify' and options: assets 1`] = ` +Object { + "simple.html": "Error: ENOENT: no such file or directory, open '/home/akait/IdeaProjects/html-minimizer-webpack-plugin/test/dist/simple.html'", +} +`; + +exports[`"minify" option should work with 'swcMinify' and options: errors 1`] = ` +Array [ + "Error: simple.html from Html Minimizer plugin +Failed to create reference from Buffer", +] +`; + +exports[`"minify" option should work with 'swcMinify' and options: warnings 1`] = `Array []`; + +exports[`"minify" option should work with 'swcMinify': assets 1`] = ` +Object { + "simple.html": "Error: ENOENT: no such file or directory, open '/home/akait/IdeaProjects/html-minimizer-webpack-plugin/test/dist/simple.html'", +} +`; + +exports[`"minify" option should work with 'swcMinify': errors 1`] = ` +Array [ + "Error: simple.html from Html Minimizer plugin +Failed to create reference from Buffer", +] +`; + +exports[`"minify" option should work with 'swcMinify': warnings 1`] = `Array []`; diff --git a/test/minify-option.test.js b/test/minify-option.test.js index 633b11f..74abd4c 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -190,4 +190,37 @@ describe('"minify" option', () => { expect(getErrors(stats)).toMatchSnapshot("errors"); expect(getWarnings(stats)).toMatchSnapshot("warnings"); }); + + it("should work with 'swcMinify'", async () => { + const testHtmlId = "./simple.html"; + const compiler = getCompiler(testHtmlId); + + new HtmlMinimizerPlugin({ + minify: HtmlMinimizerPlugin.swcMinify, + }).apply(compiler); + + const stats = await compile(compiler); + + expect(readAssets(compiler, stats, /\.html$/i)).toMatchSnapshot("assets"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + }); + + it("should work with 'swcMinify' and options", async () => { + const testHtmlId = "./simple.html"; + const compiler = getCompiler(testHtmlId); + + new HtmlMinimizerPlugin({ + minimizerOptions: { + collapseBooleanAttributes: false, + }, + minify: HtmlMinimizerPlugin.swcMinify, + }).apply(compiler); + + const stats = await compile(compiler); + + expect(readAssets(compiler, stats, /\.html$/i)).toMatchSnapshot("assets"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + }); }); diff --git a/types/index.d.ts b/types/index.d.ts index 7f082df..6d5a08f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -135,6 +135,7 @@ declare class HtmlMinimizerPlugin { declare namespace HtmlMinimizerPlugin { export { htmlMinifierTerser, + swcMinify, Schema, Compiler, Compilation, @@ -189,6 +190,7 @@ type DefinedDefaultMinimizerAndOptions = minimizerOptions?: MinimizerOptions | undefined; }; import { htmlMinifierTerser } from "./utils"; +import { swcMinify } from "./utils"; type Schema = import("schema-utils/declarations/validate").Schema; type Compilation = import("webpack").Compilation; type WebpackError = import("webpack").WebpackError; diff --git a/types/utils.d.ts b/types/utils.d.ts index b416dc4..3fb4105 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -23,3 +23,12 @@ export function htmlMinifierTerser( input: Input, minimizerOptions?: HtmlMinifierTerserOptions | undefined ): Promise; +/** + * @param {Input} input + * @param {* | undefined} [minimizerOptions] + * @returns {Promise} + */ +export function swcMinify( + input: Input, + minimizerOptions?: any | undefined +): Promise;