-
Notifications
You must be signed in to change notification settings - Fork 27
feat(minify): use SWC #621
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,20 @@ | ||
| import { minify } from '@minify-html/wasm'; | ||
| import { readFile } from 'node:fs/promises'; | ||
|
|
||
| const DEFAULT_HTML_MINIFIER_OPTIONS = { | ||
| minify_css: true, | ||
| minify_js: true, | ||
| }; | ||
| import { minify, default as initSync } from '@swc/html-wasm'; | ||
|
|
||
| const textEncoder = new TextEncoder(); | ||
| const textDecoder = new TextDecoder(); | ||
| // See https://github.com/swc-project/swc/issues/11599 for why we need to load | ||
| // the WASM file in this way | ||
| await initSync( | ||
| readFile(new URL(import.meta.resolve('@swc/html-wasm/wasm_bg.wasm'))) | ||
| ); | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+5
to
+9
|
||
|
|
||
| /** | ||
| * Minifies HTML with project defaults and optional overrides. | ||
| * Minifies HTML with project defaults and optional overrides. At the moment, | ||
| * swc's defaults are suitable for our needs, but in the event that this changes, | ||
| * allowing project defaults is beneficial. | ||
| * | ||
| * @param {string} html | ||
| * @param {Record<string, boolean | number | string | string[]>} [overrides] | ||
| * @param {import('@swc/html-wasm').Options} [options] | ||
| */ | ||
| export const minifyHTML = async (html, overrides = {}) => { | ||
| const minified = minify(textEncoder.encode(html), { | ||
| ...DEFAULT_HTML_MINIFIER_OPTIONS, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| return textDecoder.decode(minified); | ||
| }; | ||
| export const minifyHTML = async (html, options = {}) => | ||
| (await minify(html, options)).code; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's lock this PR till upstream gets fixed