Minify JS and CSS
JavaScript, CSS and JSON minified: comments and spaces out, for scripts and styles served or embedded in pages.
Version 1.0.0 · 4 functions · 1 crate
Functions
What use js_minify.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use minify_js, minify_css from js_minify (Quillo 0.6).
minify_js(code: str) → stror an errorJavaScript minified: comments, line breaks and spaces taken out where the code means the same without them. Names are kept.
minify_css(code: str) → stror an errorCSS minified: comments, line breaks and spaces out. An error for CSS it cannot read (an unclosed comment or string).
minify_json(text: str) → stror an errorJSON text minified: the spaces and line breaks between values out, strings kept as they are.
minify_file(from: str, to: str) → anyor an errorMinifies the file from into to (folders are made), as .js, .mjs, .css or .json by its extension: { before, after }, the sizes in bytes.
Get it
$ quillo install js_minify # js_minify.qwer into this folder$ quillo install js_minify --example # and its example, js_minify.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/js_minify.qwer$ curl -O https://quillolang.org/plugins/files/js_minify.qweThen, in your program, all of it or only what you call:
use js_minify.qweruse minify_js, minify_css from js_minify // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
minifier = { version = "0.3", default-features = false }Example
// A script and a style sheet minified when the site is built, and served small.//// quillo run js_minify.qwe then open http://localhost:8080/app.min.jsuse "js_minify.qwer"let SCRIPT = `// Adds up the basket.function total(items) { let sum = 0; /* in cents */ for (const item of items) { sum += item.price * item.qty; } return sum;}console.log("Total:", total([{ price: 650, qty: 2 }]));`let STYLE = `/* the cards */.card { padding: 12px; border-radius: 4px;}.card .price { font-weight: bold; }`route GET /app.min.js = respond(200, minify_js(SCRIPT)?, { "content-type": "text/javascript; charset=utf-8" })route GET /site.min.css = respond(200, minify_css(STYLE)?, { "content-type": "text/css; charset=utf-8" })route GET /build { write_file("data/src/app.js", SCRIPT) write_file("data/src/site.css", STYLE) { js: minify_file("data/src/app.js", "data/dist/app.min.js")?, css: minify_file("data/src/site.css", "data/dist/site.min.css")?, json: minify_json("{\n \"name\": \"Quillo Café\",\n \"open\": true\n}"), }}$ quillo run js_minify.qwe$ curl 'localhost:8080/app.min.js'$ curl 'localhost:8080/site.min.css'$ curl 'localhost:8080/build'Source
//! JavaScript, CSS and JSON minified: comments and spaces out, for scripts and styles served or embedded in pages.//! version: 1.0.0//!//! [dependencies]//! minifier = { version = "0.3", default-features = false }use serde_json::{json, Value};const MAX_BYTES: usize = 64 * 1024 * 1024;fn check(code: &str) -> Result<(), String> { if code.len() > MAX_BYTES { Err("more than 64 MB of code".into()) } else { Ok(()) }}/// JavaScript minified: comments, line breaks and spaces taken out where the code means the same without them. Names are kept.pub fn minify_js(code: &str) -> Result<String, String> { check(code)?; Ok(minifier::js::minify(code).to_string())}/// CSS minified: comments, line breaks and spaces out. An error for CSS it cannot read (an unclosed comment or string).pub fn minify_css(code: &str) -> Result<String, String> { check(code)?; minifier::css::minify(code).map(|m| m.to_string().trim().to_string()).map_err(|e| e.to_string())}/// JSON text minified: the spaces and line breaks between values out, strings kept as they are.pub fn minify_json(text: &str) -> Result<String, String> { check(text)?; Ok(minifier::json::minify(text).to_string())}/// Minifies the file `from` into `to` (folders are made), as .js, .mjs, .css or .json by its extension: `{ before, after }`, the sizes in bytes.pub fn minify_file(from: &str, to: &str) -> Result<Value, String> { let code = std::fs::read_to_string(from).map_err(|e| format!("{}: {}", from, e))?; let ext = from.rsplit_once('.').map(|(_, e)| e.to_lowercase()).unwrap_or_default(); let out = match ext.as_str() { "js" | "mjs" | "cjs" => minify_js(&code)?, "css" => minify_css(&code)?, "json" => minify_json(&code)?, _ => return Err(format!("{}: minify .js, .css or .json files", from)), }; 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": code.len(), "after": out.len() }))}Related
minify_html.qwer · 1.0.0HTML made smaller for sending: spaces, comments and optional tags out, and the CSS and JavaScript inside minified too.5 views · 9 downloadsCSS minifier css_minify.qwer · 1.0.0CSS made small and ready for the browsers you support (lightningcss): minified with rules merged, vendor prefixes added and nesting and new syntax lowered for a browserslist query, and errors reported by line.8 views · 7 downloadsJSON types json_types.qwer · 1.0.0Types inferred from JSON samples: TypeScript interfaces, a JSON Schema and Rust structs for an API's answers or a webhook's body, with optional and nullable fields found across samples.7 views · 7 downloadsCSS inliner css_inline.qwer · 1.0.0CSS inlined into HTML for email: style blocks and extra CSS written into each element's style attribute, with nothing ever fetched.7 views · 6 downloadsHTML rewriting html_rewrite.qwer · 1.0.0HTML rewritten as it streams, by CSS selector: links made absolute, attributes set or removed, elements removed or unwrapped, text replaced.8 views · 6 downloadsHTTP caching http_cache.qwer · 1.0.0HTTP caching as RFC 9111 says: Cache-Control read and written, ETags made, conditional requests answered 304, and a stored response's freshness, age and time to live in a browser or a CDN.7 views · 6 downloadsThread 0 posts
On Minify JS and CSS: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.