Web

Minify HTML

HTML made smaller for sending: spaces, comments and optional tags out, and the CSS and JavaScript inside minified too.

Version 1.0.0 · 3 functions · 1 crate

#html#css#javascript#performance#export

Functions

What use minify_html.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use minify_html, minify_html_with from minify_html (Quillo 0.6).

minify_html(html: str) → stror an error

html minified: whitespace between tags, comments, quotes and closing tags that browsers do not need are taken out, and <style> and <script> contents minified. The page shows the same; a page or a fragment alike.

minify_html_with(html: str, options: any) → stror an error

The same with options, each true or false: minify_css, minify_js (both true by default), keep_comments, keep_closing_tags (for HTML other tools will parse) and keep_templates (true: {{ }}, {% %} and <% %> left alone).

minify_html_file(from: str, to: str) → anyor an error

Minifies the HTML file from into to (folders are made): { before, after }, the sizes in bytes.

Get it

$ quillo install minify_html               # minify_html.qwer into this folder$ quillo install minify_html --example     # and its example, minify_html.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/minify_html.qwer$ curl -O https://quillolang.org/plugins/files/minify_html.qwe

Then, in your program, all of it or only what you call:

main.qwe
use minify_html.qweruse minify_html, minify_html_with from minify_html      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
minify-html = "0.16"

Example

minify_html.qwe
// Pages minified before they are sent or saved for a static export.////   quillo run minify_html.qwe      then open http://localhost:8080/pageuse "minify_html.qwer"let PAGE = `<!DOCTYPE html><html>  <head>    <title>  Menu  </title>    <style>      body { margin: 0px; color: #ff0000; }      .price   { font-weight: bold; }    </style>  </head>  <body>    <!-- the menu of the day -->    <h1 class="title">Today's menu</h1>    <ul>      <li>Soup  <span class="price">6.50</span></li>      <li>Tart  <span class="price">11.00</span></li>    </ul>    <script>      const total = function (a, b) { return a + b; };      console.log( total(6.5, 11) );    </script>  </body></html>`route GET /page = respond(200, minify_html(PAGE)?, { "content-type": "text/html; charset=utf-8" })route GET /sizes {    write_file("data/menu.html", PAGE)    {        file: minify_html_file("data/menu.html", "data/export/menu.html")?,        with_comments: minify_html_with(PAGE, { keep_comments: true, minify_js: false })?,    }}
$ quillo run minify_html.qwe$ curl 'localhost:8080/page'$ curl 'localhost:8080/sizes'

Source

minify_html.qwer
//! HTML made smaller for sending: spaces, comments and optional tags taken out, and the CSS and JavaScript inside it minified too.//! version: 1.0.0//!//! [dependencies]//! minify-html = "0.16"use serde_json::{json, Value};const MAX_BYTES: usize = 64 * 1024 * 1024;fn cfg(o: &Value) -> minify_html::Cfg {    let mut c = minify_html::Cfg::new();    let flag = |k: &str, d: bool| o.get(k).and_then(Value::as_bool).unwrap_or(d);    c.minify_css = flag("minify_css", true);    c.minify_js = flag("minify_js", true);    c.keep_comments = flag("keep_comments", false);    c.keep_closing_tags = flag("keep_closing_tags", false);    c.keep_html_and_head_opening_tags = flag("keep_html_and_head_opening_tags", false);    // Template syntax ({{ x }}, {% x %}, <% x %>) is kept as it is.    c.preserve_brace_template_syntax = flag("keep_templates", true);    c.preserve_chevron_percent_template_syntax = flag("keep_templates", true);    c}fn run(html: &str, o: &Value) -> Result<String, String> {    if html.len() > MAX_BYTES {        return Err("more than 64 MB of HTML".into());    }    String::from_utf8(minify_html::minify(html.as_bytes(), &cfg(o))).map_err(|e| e.to_string())}/// `html` minified: whitespace between tags, comments, quotes and closing tags that browsers do not need are taken out,/// and `<style>` and `<script>` contents minified. The page shows the same; a page or a fragment alike.pub fn minify_html(html: &str) -> Result<String, String> {    run(html, &Value::Null)}/// The same with `options`, each true or false: `minify_css`, `minify_js` (both true by default), `keep_comments`, `keep_closing_tags`/// (for HTML other tools will parse) and `keep_templates` (true: `{{ }}`, `{% %}` and `<% %>` left alone).pub fn minify_html_with(html: &str, options: serde_json::Value) -> Result<String, String> {    run(html, &options)}/// Minifies the HTML file `from` into `to` (folders are made): `{ before, after }`, the sizes in bytes.pub fn minify_html_file(from: &str, to: &str) -> Result<Value, String> {    let html = std::fs::read_to_string(from).map_err(|e| format!("{}: {}", from, e))?;    let out = run(&html, &Value::Null)?;    if let Some(dir) = std::path::Path::new(to).parent().filter(|d| !d.as_os_str().is_empty()) {        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {}", dir.display(), e))?;    }    std::fs::write(to, &out).map_err(|e| format!("{}: {}", to, e))?;    Ok(json!({ "before": html.len(), "after": out.len() }))}
Thread
59Useful? Like it, or add to its thread below.

Thread 0 posts

On Minify HTML: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.