Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with swc #87

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ module.exports = {

// For `@swc/html`:
//
// Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5
//
// new HtmlMinimizerPlugin({
// minify: HtmlMinimizerPlugin.swcMinify,
// minimizerOptions: {}
Expand Down Expand Up @@ -450,6 +452,8 @@ module.exports = {

### `swc/html`

Available [`options`](https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5).

```js
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
Expand Down Expand Up @@ -479,7 +483,7 @@ module.exports = {
new HtmlMinimizerPlugin({
minify: HtmlMinimizerPlugin.swcMinify,
minimizerOptions: {
// Options
// Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts#L5
},
}),
],
Expand Down
226 changes: 113 additions & 113 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@babel/preset-env": "^7.18.9",
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@swc/html": "^0.0.16",
"@swc/html": "^0.0.17",
"@types/serialize-javascript": "^5.0.2",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^29.1.2",
Expand Down
24 changes: 21 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,30 @@ 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), {
// TODO `import("@swc/html").Options`
const options = /** @type {*} */ ({
...minimizerOptions,
});

return { code: result };
const [[, code]] = Object.entries(input);
const result = await swcMinifier.minify(Buffer.from(code), options);

let errors;

if (typeof result.errors !== "undefined") {
errors = result.errors.map((diagnostic) => {
const error = new Error(diagnostic.message);

// @ts-ignore
error.span = diagnostic.span;
// @ts-ignore
error.level = diagnostic.level;

return error;
});
}

return { code: result.code, errors };
}

module.exports = { throttleAll, htmlMinifierTerser, swcMinify };
67 changes: 51 additions & 16 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,64 @@ exports[`"minify" option should work minify function: warnings 1`] = `[]`;

exports[`"minify" option should work with 'swcMinify' and options: assets 1`] = `
{
"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>
"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>",
}
`;

<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
exports[`"minify" option should work with 'swcMinify' and options: errors 1`] = `[]`;

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

<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>",
exports[`"minify" option should work with 'swcMinify' and throw errors: assets 1`] = `
{
"broken-html-syntax.html": "Text &lt; img src="image.png" >
Text &lt;<img src=image.png>
Text ><img src=image.png>

<a foo><bar></bar></a><boo>boohay
&lt;&lt;&lt;&lt;>foo
>>&lt;</boo>",
}
`;

exports[`"minify" option should work with 'swcMinify' and options: errors 1`] = `[]`;
exports[`"minify" option should work with 'swcMinify' and throw errors: errors 1`] = `
[
"Error: broken-html-syntax.html from Html Minimizer plugin
Abrupt closing of empty comment",
"Error: broken-html-syntax.html from Html Minimizer plugin
Cdata in html content",
"Error: broken-html-syntax.html from Html Minimizer plugin
End of file seen and there were open elements",
"Error: broken-html-syntax.html from Html Minimizer plugin
End tag "a" violates nesting rules",
"Error: broken-html-syntax.html from Html Minimizer plugin
Eof in tag",
"Error: broken-html-syntax.html from Html Minimizer plugin
Incorrectly opened comment",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Invalid first character of tag name",
"Error: broken-html-syntax.html from Html Minimizer plugin
Non void html element start tag with trailing solidus",
"Error: broken-html-syntax.html from Html Minimizer plugin
Non-space characters found without seeing a doctype first, expected "<!DOCTYPE html>"",
"Error: broken-html-syntax.html from Html Minimizer plugin
Unexpected question mark instead of tag name",
]
`;

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

exports[`"minify" option should work with 'swcMinify': assets 1`] = `
{
Expand Down
1 change: 1 addition & 0 deletions test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function getCompiler(htmlFixture, config = {}) {
entry: path.resolve(__dirname, "../fixtures/entry.js"),
optimization: {
minimize: false,
emitOnErrors: true,
},
output: {
pathinfo: false,
Expand Down
17 changes: 16 additions & 1 deletion test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,28 @@ describe('"minify" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should work with 'swcMinify' and throw errors", async () => {
const testHtmlId = "./broken-html-syntax.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,
collapseWhitespaces: "advanced-conservative",
},
minify: HtmlMinimizerPlugin.swcMinify,
}).apply(compiler);
Expand Down