CLI to optimize front-end assets — WebP images, self-hosted fonts, LCP, dimensions, and deferred JS/CSS.
Author: Hamza Haseeb
npm install -g frontend-optimizerOr run without installing:
npx frontend-optimizer optimize ./your-projectOptimize an entire project folder:
npx frontend-optimizer optimize ./srcThis runs four steps:
- Images — Convert PNG, JPEG, GIF, etc. to compressed WebP
- Fonts — Self-host Google Fonts, add
font-display: swap, preload key fonts - HTML — Add
width/height, LCPfetchpriority="high", defer third-party scripts - Defer — Defer non-critical JavaScript and async-load non-critical CSS
| Command | Description |
|---|---|
optimize |
Full pipeline (images + fonts + HTML + defer) |
images |
Convert images to WebP |
fonts |
Download and self-host web fonts |
html |
Dimensions, LCP fetch priority, third-party scripts |
defer |
Defer non-critical JS and CSS |
# Full optimization
npx frontend-optimizer optimize ./public
# Images only (aggressive compression)
npx frontend-optimizer images ./assets -q 65 --max-width 1920
# Self-host Google Fonts
npx frontend-optimizer fonts ./index.html
# HTML performance (LCP image, dimensions)
npx frontend-optimizer html ./index.html --lcp-image hero
# Defer analytics and non-critical assets
npx frontend-optimizer defer ./index.htmlAfter global install, you can also use:
front-end optimize ./src- Converts PNG, JPG, JPEG, GIF, TIFF, BMP, AVIF → WebP
- Often reduces file size from MB to KB
- Optional resize (
--max-width,--max-height)
- Downloads fonts from Google Fonts, Bunny Fonts, and similar CDNs
- Self-hosts font files locally
- Adds
font-display: swapto every@font-face - Preloads key
.woff2files for faster rendering
fetchpriority="high"on the LCP (hero) image- Explicit
widthandheighton images, videos, iframes, and embeds (reduces layout shift) - Third-party scripts deferred (analytics, ads, tracking, etc.)
- Adds
deferto analytics, chat, and tracking scripts - Async-loads non-critical CSS (fonts, animations, icons) via the
media="print"pattern
import {
convertImages,
localizeFonts,
optimizeHtml,
deferAssets,
} from "frontend-optimizer";
await convertImages("./photos", { quality: 75 });
await localizeFonts("./index.html");
await optimizeHtml("./index.html", { lcpImage: "hero" });
await deferAssets("./index.html");- Node.js 18+
git clone https://github.com/Haseebcodejourney/frontend-optimizer.git
cd frontend-optimizer
npm install
npm run build
npm testBump version in package.json, then:
npm run build
npm test
npm publishMIT © Hamza Haseeb