Skip to content

Commit 1856d4d

Browse files
committed
feat(minify): use SWC
1 parent 64655f3 commit 1856d4d

7 files changed

Lines changed: 35 additions & 41 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: 17 additions & 16 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
@@ -43,12 +43,12 @@
4343
"dependencies": {
4444
"@actions/core": "^3.0.0",
4545
"@heroicons/react": "^2.2.0",
46-
"@minify-html/wasm": "^0.18.1",
4746
"@node-core/rehype-shiki": "^1.4.0",
4847
"@node-core/ui-components": "^1.6.0",
4948
"@orama/orama": "^3.1.18",
5049
"@orama/ui": "^1.5.4",
5150
"@rollup/plugin-virtual": "^3.0.2",
51+
"@swc/html-wasm": "^1.15.17",
5252
"acorn": "^8.15.0",
5353
"commander": "^14.0.3",
5454
"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: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import { minify } from '@minify-html/wasm';
1+
import { readFile } from 'node:fs/promises';
22

3-
const DEFAULT_HTML_MINIFIER_OPTIONS = {
4-
minify_css: true,
5-
minify_js: true,
6-
};
3+
import { minify, default as initSync } from '@swc/html-wasm';
74

8-
const textEncoder = new TextEncoder();
9-
const textDecoder = new TextDecoder();
5+
// See https://github.com/swc-project/swc/issues/11599 for why we need to load
6+
// the WASM file in this way
7+
await initSync(
8+
readFile(new URL(import.meta.resolve('@swc/html-wasm/wasm_bg.wasm')))
9+
);
1010

1111
/**
12-
* Minifies HTML with project defaults and optional overrides.
12+
* Minifies HTML with project defaults and optional overrides. At the moment,
13+
* swc's defaults are suitable for our needs, but in the event that this changes,
14+
* allowing project defaults is beneficial.
1315
*
1416
* @param {string} html
15-
* @param {Record<string, boolean | number | string | string[]>} [overrides]
17+
* @param {import('@swc/html-wasm').Options} [options]
1618
*/
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-
};
19+
export const minifyHTML = async (html, options = {}) =>
20+
(await minify(html, options)).code;

0 commit comments

Comments
 (0)