import init, { minify } from "https://wilsonl.in/minify-html/deno/0.11.1/index.js";
export interface MinifyHTMLOptions {
do_not_minify_doctype: boolean;
ensure_spec_compliant_unquoted_attribute_values: boolean;
keep_closing_tags: boolean;
keep_html_and_head_opening_tags: boolean;
keep_spaces_between_attributes: boolean;
keep_comments: boolean;
minify_css: boolean;
minify_css_level_1: boolean;
minify_css_level_2: boolean;
minify_css_level_3: boolean;
minify_js: boolean;
remove_bangs: boolean;
remove_processing_instructions: boolean;
}
await init();
export default async function(request: Request) {
const url = new URL(request.url);
if (request.method === "GET" || request.method === "HEAD") {
return new Response(String.raw`Usage:
curl "${url.origin}/" \
-d "<html> Hello, world! <!--to be minified--> </html>"
curl "${url.origin}/?do_not_minify_doctype=true&keep_comments=true" \
-d "<!DOCTYPE html><html> Hello, world! <!--keep--> </html>"
Detailed options can be found at:
https://esm.town/v/${
request.url
.match(/^http(?:s):\/\/(\w+)-(\w+)\.web\.val\.run/)!
.slice(1)
.join("/")
}
`);
}
const options: Partial<MinifyHTMLOptions> = Object.fromEntries(
Array.from(url.searchParams.entries()).map(([key, val]) => [
key,
val === "true" ? true : val === "false" ? false : undefined,
]),
);
const result = minify(new Uint8Array(await request.arrayBuffer()), options);
return new Response(result);
}