Skip to content

Commit a41a0d3

Browse files
avivkellerovflowd
andauthored
feat(minify): use SWC (#621)
* feat(minify): use SWC * fixup! * Update html-minifier.mjs Co-authored-by: Claudio Wunder <cwunder@gnome.org> * fixup! --------- Co-authored-by: Claudio Wunder <cwunder@gnome.org>
1 parent 69710d5 commit a41a0d3

7 files changed

Lines changed: 20 additions & 26 deletions

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ updates:
6262
- 'recma-*'
6363
compiling:
6464
patterns:
65-
- '@minify-html/wasm'
65+
- '@swc/html-wasm'
6666
- '@rollup/*'
6767
- 'rolldown'
6868
- 'lightningcss-wasm'

npm-shrinkwrap.json

Lines changed: 8 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@orama/orama": "^3.1.18",
5050
"@orama/ui": "^1.5.4",
5151
"@rollup/plugin-virtual": "^3.0.2",
52+
"@swc/html-wasm": "^1.15.18",
5253
"acorn": "^8.16.0",
5354
"commander": "^14.0.3",
5455
"dedent": "^1.7.1",

src/generators/legacy-html-all/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function generate(input) {
6363
});
6464

6565
if (config.minify) {
66-
result = Buffer.from(await minifyHTML(result));
66+
result = await minifyHTML(result);
6767
}
6868

6969
if (config.output) {

src/generators/legacy-html/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export async function* generate(input, worker) {
130130
let result = replaceTemplateValues(apiTemplate, template, config);
131131

132132
if (config.minify) {
133-
result = Buffer.from(await minifyHTML(result));
133+
result = await minifyHTML(result);
134134
}
135135

136136
await writeFile(join(config.output, `${template.api}.html`), result);

src/generators/web/utils/processing.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ export async function processJSXEntries(
123123
.replace('{{speculationRules}}', SPECULATION_RULES)
124124
.replace('{{ogTitle}}', title);
125125

126-
const minifiedHtml = await minifyHTML(renderedHtml);
127-
const html = Buffer.from(minifiedHtml);
128-
129-
return { html, api };
126+
return { html: await minifyHTML(renderedHtml), api };
130127
})
131128
);
132129

src/utils/html-minifier.mjs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import { minify } from '@minify-html/wasm';
2-
3-
const DEFAULT_HTML_MINIFIER_OPTIONS = {
4-
minify_css: true,
5-
minify_js: true,
6-
};
7-
8-
const textEncoder = new TextEncoder();
9-
const textDecoder = new TextDecoder();
1+
import { minify } from '@swc/html-wasm';
102

113
/**
12-
* Minifies HTML with project defaults and optional overrides.
4+
* Minifies HTML with project defaults and optional overrides. At the moment,
5+
* swc's defaults are suitable for our needs, but in the event that this changes,
6+
* allowing project defaults is beneficial.
137
*
148
* @param {string} html
15-
* @param {Record<string, boolean | number | string | string[]>} [overrides]
9+
* @param {import('@swc/html-wasm').Options} [options]
1610
*/
17-
export const minifyHTML = async (html, overrides = {}) => {
18-
const minified = minify(textEncoder.encode(html), {
19-
...DEFAULT_HTML_MINIFIER_OPTIONS,
20-
...overrides,
21-
});
22-
23-
return textDecoder.decode(minified);
24-
};
11+
export const minifyHTML = async (html, options = {}) =>
12+
minify(html, options).then(({ code }) => code);

0 commit comments

Comments
 (0)