Skip to content

Commit afaf453

Browse files
fix: compatibility with swc (#87)
1 parent 0ae3eef commit afaf453

File tree

7 files changed

+208
-135
lines changed

7 files changed

+208
-135
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ module.exports = {
9595

9696
// For `@swc/html`:
9797
//
98+
// Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5
99+
//
98100
// new HtmlMinimizerPlugin({
99101
// minify: HtmlMinimizerPlugin.swcMinify,
100102
// minimizerOptions: {}
@@ -450,6 +452,8 @@ module.exports = {
450452

451453
### `swc/html`
452454

455+
Available [`options`](https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5).
456+
453457
```js
454458
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
455459
const CopyPlugin = require("copy-webpack-plugin");
@@ -479,7 +483,7 @@ module.exports = {
479483
new HtmlMinimizerPlugin({
480484
minify: HtmlMinimizerPlugin.swcMinify,
481485
minimizerOptions: {
482-
// Options
486+
// Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5
483487
},
484488
}),
485489
],

package-lock.json

Lines changed: 113 additions & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@babel/preset-env": "^7.18.9",
6363
"@commitlint/cli": "^17.1.2",
6464
"@commitlint/config-conventional": "^17.1.0",
65-
"@swc/html": "^0.0.16",
65+
"@swc/html": "^0.0.17",
6666
"@types/serialize-javascript": "^5.0.2",
6767
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
6868
"babel-jest": "^29.1.2",

src/utils.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,30 @@ async function swcMinify(input, minimizerOptions = {}) {
113113
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved
114114
const swcMinifier = require("@swc/html");
115115

116-
const [[, code]] = Object.entries(input);
117-
const result = await swcMinifier.minify(Buffer.from(code), {
116+
// TODO `import("@swc/html").Options`
117+
const options = /** @type {*} */ ({
118118
...minimizerOptions,
119119
});
120120

121-
return { code: result };
121+
const [[, code]] = Object.entries(input);
122+
const result = await swcMinifier.minify(Buffer.from(code), options);
123+
124+
let errors;
125+
126+
if (typeof result.errors !== "undefined") {
127+
errors = result.errors.map((diagnostic) => {
128+
const error = new Error(diagnostic.message);
129+
130+
// @ts-ignore
131+
error.span = diagnostic.span;
132+
// @ts-ignore
133+
error.level = diagnostic.level;
134+
135+
return error;
136+
});
137+
}
138+
139+
return { code: result.code, errors };
122140
}
123141

124142
module.exports = { throttleAll, htmlMinifierTerser, swcMinify };

test/__snapshots__/minify-option.test.js.snap

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,64 @@ exports[`"minify" option should work minify function: warnings 1`] = `[]`;
7171

7272
exports[`"minify" option should work with 'swcMinify' and options: assets 1`] = `
7373
{
74-
"simple.html": "<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><h1>My First Heading</h1>
75-
<p>My first paragraph.</p>
76-
<h2>An Unordered HTML List</h2>
74+
"simple.html": "<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><h1>My First Heading</h1> <p>My first paragraph.</p> <h2>An Unordered HTML List</h2> <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> <h2>An Ordered HTML List</h2> <ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol>",
75+
}
76+
`;
7777

78-
<ul>
79-
<li>Coffee</li>
80-
<li>Tea</li>
81-
<li>Milk</li>
82-
</ul>
78+
exports[`"minify" option should work with 'swcMinify' and options: errors 1`] = `[]`;
8379

84-
<h2>An Ordered HTML List</h2>
80+
exports[`"minify" option should work with 'swcMinify' and options: warnings 1`] = `[]`;
8581

86-
<ol>
87-
<li>Coffee</li>
88-
<li>Tea</li>
89-
<li>Milk</li>
90-
</ol>",
82+
exports[`"minify" option should work with 'swcMinify' and throw errors: assets 1`] = `
83+
{
84+
"broken-html-syntax.html": "Text &lt; img src="image.png" >
85+
Text &lt;<img src=image.png>
86+
Text ><img src=image.png>
87+
88+
<a foo><bar></bar></a><boo>boohay
89+
&lt;&lt;&lt;&lt;>foo
90+
>>&lt;</boo>",
9191
}
9292
`;
9393

94-
exports[`"minify" option should work with 'swcMinify' and options: errors 1`] = `[]`;
94+
exports[`"minify" option should work with 'swcMinify' and throw errors: errors 1`] = `
95+
[
96+
"Error: broken-html-syntax.html from Html Minimizer plugin
97+
Abrupt closing of empty comment",
98+
"Error: broken-html-syntax.html from Html Minimizer plugin
99+
Cdata in html content",
100+
"Error: broken-html-syntax.html from Html Minimizer plugin
101+
End of file seen and there were open elements",
102+
"Error: broken-html-syntax.html from Html Minimizer plugin
103+
End tag "a" violates nesting rules",
104+
"Error: broken-html-syntax.html from Html Minimizer plugin
105+
Eof in tag",
106+
"Error: broken-html-syntax.html from Html Minimizer plugin
107+
Incorrectly opened comment",
108+
"Error: broken-html-syntax.html from Html Minimizer plugin
109+
Invalid first character of tag name",
110+
"Error: broken-html-syntax.html from Html Minimizer plugin
111+
Invalid first character of tag name",
112+
"Error: broken-html-syntax.html from Html Minimizer plugin
113+
Invalid first character of tag name",
114+
"Error: broken-html-syntax.html from Html Minimizer plugin
115+
Invalid first character of tag name",
116+
"Error: broken-html-syntax.html from Html Minimizer plugin
117+
Invalid first character of tag name",
118+
"Error: broken-html-syntax.html from Html Minimizer plugin
119+
Invalid first character of tag name",
120+
"Error: broken-html-syntax.html from Html Minimizer plugin
121+
Invalid first character of tag name",
122+
"Error: broken-html-syntax.html from Html Minimizer plugin
123+
Non void html element start tag with trailing solidus",
124+
"Error: broken-html-syntax.html from Html Minimizer plugin
125+
Non-space characters found without seeing a doctype first, expected "<!DOCTYPE html>"",
126+
"Error: broken-html-syntax.html from Html Minimizer plugin
127+
Unexpected question mark instead of tag name",
128+
]
129+
`;
95130

96-
exports[`"minify" option should work with 'swcMinify' and options: warnings 1`] = `[]`;
131+
exports[`"minify" option should work with 'swcMinify' and throw errors: warnings 1`] = `[]`;
97132

98133
exports[`"minify" option should work with 'swcMinify': assets 1`] = `
99134
{

test/helpers/getCompiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function getCompiler(htmlFixture, config = {}) {
1616
entry: path.resolve(__dirname, "../fixtures/entry.js"),
1717
optimization: {
1818
minimize: false,
19+
emitOnErrors: true,
1920
},
2021
output: {
2122
pathinfo: false,

test/minify-option.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,28 @@ describe('"minify" option', () => {
206206
expect(getWarnings(stats)).toMatchSnapshot("warnings");
207207
});
208208

209+
it("should work with 'swcMinify' and throw errors", async () => {
210+
const testHtmlId = "./broken-html-syntax.html";
211+
const compiler = getCompiler(testHtmlId);
212+
213+
new HtmlMinimizerPlugin({
214+
minify: HtmlMinimizerPlugin.swcMinify,
215+
}).apply(compiler);
216+
217+
const stats = await compile(compiler);
218+
219+
expect(readAssets(compiler, stats, /\.html$/i)).toMatchSnapshot("assets");
220+
expect(getErrors(stats)).toMatchSnapshot("errors");
221+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
222+
});
223+
209224
it("should work with 'swcMinify' and options", async () => {
210225
const testHtmlId = "./simple.html";
211226
const compiler = getCompiler(testHtmlId);
212227

213228
new HtmlMinimizerPlugin({
214229
minimizerOptions: {
215-
collapseBooleanAttributes: false,
230+
collapseWhitespaces: "advanced-conservative",
216231
},
217232
minify: HtmlMinimizerPlugin.swcMinify,
218233
}).apply(compiler);

0 commit comments

Comments
 (0)