|
1 | | -import { minify } from '@minify-html/wasm'; |
| 1 | +import { readFile } from 'node:fs/promises'; |
2 | 2 |
|
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'; |
7 | 4 |
|
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 | +); |
10 | 10 |
|
11 | 11 | /** |
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. |
13 | 15 | * |
14 | 16 | * @param {string} html |
15 | | - * @param {Record<string, boolean | number | string | string[]>} [overrides] |
| 17 | + * @param {import('@swc/html-wasm').Options} [options] |
16 | 18 | */ |
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