Skip to content

Commit

Permalink
feat: add gzipped size calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed May 16, 2024
1 parent c689953 commit 38e5bd2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ideal for fast prototyping, static HTML pages, Markdown-generated documents, and
-**No** JavaScript
-**No** configuration needed
-**No** refactoring required
-`~6kB` gzipped _(can be further reduced)_

### 📸 Screenshot examples

Expand Down
4 changes: 1 addition & 3 deletions app/build/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ export async function css(exclude = [] as string[]) {
for (const { path } of files) {
css += await Deno.readTextFile(path)
}
const bundled = await bundle(css, { minify: true, banner, rules: { "no-descending-specificity": false, "no-duplicate-selectors": false } })
console.log(`Bundle size: ${new Blob([bundled], { type: "text/css" }).size} bytes`)
return bundled
return bundle(css, { minify: true, banner, rules: { "no-descending-specificity": false, "no-duplicate-selectors": false } })
}
8 changes: 8 additions & 0 deletions app/build/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { default as hljs } from "https://esm.sh/[email protected]/lib/language
import { default as hlmd } from "https://esm.sh/[email protected]/lib/languages/markdown"
import { default as hlsh } from "https://esm.sh/[email protected]/lib/languages/diff"
import { DOMParser } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts"
import { gzipSize } from "https://deno.land/x/[email protected]/mod.ts"
import { css } from "./css.ts"
syntax.registerLanguage("xml", hlxml)
syntax.registerLanguage("css", hlcss)
syntax.registerLanguage("lisp", hllisp)
Expand Down Expand Up @@ -69,6 +71,12 @@ export async function html() {
}
})
document.querySelector("aside > nav")!.innerHTML = `<ul>${nav.join("")}</ul>`
// Compute gzip size
const size = document.querySelector("[data-matcha-size]") ? gzipSize(await css()) : NaN
Array.from(document.querySelectorAll("[data-matcha-size]")).forEach((_element) => {
const element = _element as unknown as HTMLElement
element.innerText = `~${new Intl.NumberFormat("en-US", { style: "unit", unit: "kilobyte", unitDisplay: "narrow", maximumSignificantDigits: 3 }).format(size / 1000)}`
})
return `<!DOCTYPE html>${document.documentElement!.outerHTML}`
}

Expand Down
1 change: 1 addition & 0 deletions app/mod.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ <h1 id="matcha"><a href="#matcha">matcha.css</a></h1>
<li><b class="success"></b> <em>No</em> JavaScript</li>
<li><b class="success"></b> <em>No</em> configuration needed</li>
<li><b class="success"></b> <em>No</em> refactoring required</li>
<li><b class="success"></b> <code data-matcha-size></code> gzipped <small><i class="muted">(can be further reduced)</i></small></li>
</ul>
<p class="muted">
<small><b>🍵 Fun Fact:</b> This very page is styled using <em>matcha.css</em>!</small>
Expand Down
109 changes: 74 additions & 35 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 38e5bd2

Please sign in to comment.