# ⚡ spark-html-image Build-time image optimization for [spark-html](https://www.npmjs.com/package/spark-html) sites — a `spark-html-bun` pipeline step that converts your `` references to **webp/avif** with a responsive `srcset`, **zero config**. No more hand-written `scripts/optimize-images.js` wired into the build command. ```js // spark.config.js import prerender from 'spark-prerender/bun'; import image from 'spark-html-image/bun'; export default { pipeline: [prerender(), image()], }; ``` That's it. After the build, every local `.png`/`.jpg` referenced by an `` in the output — pages **and** component `.html` fragments — is: - converted to webp at several widths (never upscaled past the original), - rewritten with `srcset` + `sizes` (the original file stays as the `src` fallback), - given `width`/`height` (no layout shift) and `loading="lazy"` + `decoding="async"` when absent. ```html hero hero ``` External URLs, SVGs, and any `` that already has a `srcset` (or sits in a ``) are left alone — the author knows best. ## Install ```bash bun add -d spark-html-image ``` ## Options | Option | Default | Meaning | |--------|---------|---------| | `widths` | `[640, 960, 1280, 1920]` | srcset widths, capped at each image's intrinsic width. | | `formats` | `['webp']` | `'webp'` and/or `'avif'`; order = `` order in picture mode. | | `quality` | `80` | Encoder quality. | | `sizes` | `'100vw'` | Written alongside `srcset` when the img has no `sizes`. | | `picture` | `false` | Wrap in `` with one `` per format (use with avif). | | `lazy` | `true` | Add `loading="lazy"` + `decoding="async"` when absent. | ```js image({ formats: ['avif', 'webp'], picture: true, quality: 75 }) ``` It runs in `closeBundle` (order `post`), after [`spark-prerender`](https://www.npmjs.com/package/spark-prerender) has written its per-route HTML — so prerendered pages are optimized too. Conversion uses [sharp](https://sharp.pixelplumbing.com/). ## The Spark family Small, single-purpose packages that share one philosophy: no compiler, no virtual DOM, no build step required. Add only what you use. | Package | What it does | |---|---| | [`spark-html`](https://www.npmjs.com/package/spark-html) | The runtime — components, reactivity, stores, forms, scoped styles. 13 kB gzip, 0 deps. | | [`spark-html-bun`](https://www.npmjs.com/package/spark-html-bun) | Dev server, bundler & preview on Bun — scoped HMR, no-build dev, post-build pipeline. | | [`spark-html-router`](https://www.npmjs.com/package/spark-html-router) | `