DevelopMedia
@holtkeeper/media
Image and SVG optimization engine: plans and encodes responsive raster variants with sharp, optimizes SVGs with svgo, writes the markup that uses the variants and summarizes the savings. It works on buffers and strings only — no file system, no network — so callers (like @holtkeeper/assets) decide what to read and where results go.
sharp is loaded lazily (loadSharp, sharpAvailable, requireSharp): importing this package never fails on machines without the native binary; only encoding does.
Raster variants
Section titled “Raster variants”const info = await probeImage(bytes) // { format, width, height, alpha, animated, frames?, content, colors?, wideGamut? }const plan = planVariants(info, { usage: 'responsive' })const { variants, failures } = await encodeVariants(bytes, plan.specs, { info, concurrency: 2, signal,})probeImage— dimensions after EXIF orientation, alpha only when some pixel is not opaque,animated/frames,content: 'graphic'for flat art (≤ 256 exact colors, or few colors in a 64×64 sample), exactcolorswhen ≤ 256,wideGamutfor a non-sRGB ICC profile.planVariants(info, options)→{ usage, specs, skipped }:- Formats:
avif,webpand the source format re-encoded (originalOutput: PNG stays PNG, JPEG via mozjpeg, unknown formats become PNG or JPEG). JPEG is skipped for alpha, AVIF/PNG/JPEG for animated sources (sharp animates only WebP and GIF), GIF for stills — each with areason. - Sizes:
responsiveusesdefaultWidths(320, 640, 960, 1280, 1920, 2560) below the source width plus the source width itself (only for the source format when the source is wider than the ladder);fixed(displayWidth) uses@1x/@2x/@3xand never upscales;texturetreats the source as the highest density ([1, 2]by default) and scales down. - Settings:
defaultQuality(AVIF 55, WebP 78, JPEG 78); AVIF 4:4:4 for alpha and graphics (alpha at least q70), WebPalphaQuality: 100for alpha, lossless WebP for ≤ 256 colors and near-lossless for other graphics, palette PNG only when it is lossless, otherwise PNG level 9. The re-encode of the source format at full size hasreplacesOriginal(never for textures, whose files keep@Nxnames). targetSsim(e.g.0.985) marks lossy specs for a quality search betweenquality − 30andquality.
- Formats:
encodeVariants(input, specs, options)→{ variants, failures }. EveryEncodedVarianthasspec,bytes,width,height,format,quality,ssim?,dataandsha256. EXIF orientation is applied, metadata is stripped, the ICC profile is kept only when it is not sRGB. AtargetSsimspec binary-searches its quality with at most six encodes, scoringssim(8×8-window SSIM on luma downscaled to 256 px, flattened on white) against the source resized to the same size; when no quality reaches the target the spec’s own quality is used. A failing spec lands infailures; an abortedsignalthrows.ssim(a, b, width, height)— the metric, on 8-bit single-channel buffers.cache(VariantCache, the shape ofartifactCachefrom@holtkeeper/artifacts): each spec is looked up undervariantCacheKey(sourceSha256, spec, sharp.versions)=media/v1/<source sha256>/<sha256 of the spec (without id) and the versions of sharp, libvips and the codecs>; a hit (bytes plusformat,width,height,quality,ssimmeta) skips sharp, a miss is encoded and stored.EncodeResult.cachedcounts the hits. A different quality, SSIM target, size or a sharp upgrade is a new key; the cache never returns bytes for another spec.
optimizeSvg(text, { preset, precision, multipass, keepIds }) → { text, preset, bytes: { before, after }, gzip: { before, after }, breakdown }.
| Preset | What it does |
|---|---|
safe |
preset-default without anything CSS or scripts could depend on: IDs, <style> rules and classes, inline style, hidden elements, <desc>, shapes (no shape→path), groups, data/aria/role attributes are kept. Removes prolog, comments, metadata, editor namespaces; shortens numbers. |
default |
svgo preset-default plus convertStyleToAttrs; unreferenced IDs are removed and the rest minified, except keepIds. |
aggressive |
default plus removeXlink, reusePaths, removeOffCanvasPaths, precision 2. |
Precision defaults to 3 decimals. viewBox is never removed: svgo v4 keeps it, and if a plugin still drops it, it is restored and reported.
breakdown (SvgBreakdown): removedElements and removedAttributes (name → count), metadata (metadata elements, editor-namespaced elements, <?xml?>, doctype), comments, paths.before/after (PathStats: paths, commands, coordinates, digits, three longest d with their ids), styles (inline style attributes and <style> elements before/after), ids (before, after, minified) and issues (missing-viewbox, size-without-viewbox, aspect-mismatch, embedded-raster, viewbox-restored). scanTags and pathStats are the underlying scanners.
Inline SVG
Section titled “Inline SVG”optimizeInlineSvgs(text, { syntax, precision?, minSaving? }) → InlineSvgFinding[] for <svg>…</svg> elements and stray <path> elements of a source file (syntax: html, jsx, vue, svelte; inlineSyntaxOf(file) picks it from the extension). Each finding has kind, offsets start/end, 1-based line/endLine, oldText, newText, bytes, path digits before/after and paths.
svg: a static element in HTML, Vue or Svelte (no{},:/@/v-/#bindings) rewritten whole with thesafepreset.paths: JSX and elements with bindings keep every attribute as written; only literaldvalues (d="…",d={'…'},d={`…`}) are rewritten byoptimizePathData(d, precision)— svgo’sconvertPathDataat 3 decimals, run under round stroke caps so dot segments (h.01, zero-length subpaths) survive.
A fragment is reported when it saves at least minSaving (default 32) bytes and 5%. Findings are in file order and never overlap.
Snippets
Section titled “Snippets”pictureSnippets({ file, variants, framework, alt?, sizes?, width?, height?, naming?, attributes?, indent?, key? }) → { framework, language, code, notes }. file is the original as the markup references it; variant URLs come from variantFile(file, variant, naming) — next to it, {name}-{width}w.{ext} or {name}@{density}x.{ext} by default, the original extension for the original format (.jpg), the original name for replacesOriginal, or the variant’s own url when set.
| Framework | Output |
|---|---|
html |
<picture> with AVIF/WebP <source type srcset sizes> and <img src srcset sizes width height alt loading="lazy" decoding="async">; density variants use 1x/2x descriptors and no sizes |
react |
the same in JSX: srcSet, width={…}, self-closing tags |
vue |
HTML attributes with self-closing tags |
css |
background-image: url(original) fallback plus image-set(url() type()) (imageSetValue alone) |
next, nuxt, vite |
next/image, <NuxtPicture format="avif,webp">, vite-imagetools imports — with notes that these generate variants themselves |
pixi, phaser, three |
texture loading with a format fallback and resolution chosen from devicePixelRatio (Pixi @{1,2}x.{webp,png} patterns, Phaser device.features.webp, three createImageBitmap probes) |
Game engine snippets carry a KTX2/Basis Universal note: GPU memory, not download size, limits big textures.
Savings
Section titled “Savings”savingsSummary(original, variants, usage?) → { original, formats, best, viewports }: per format the largest variant’s bytes, saving and percent; per viewport (default 375, 768, 1440 CSS px at DPR 2, or displayWidth/fraction) the smallest variant at least as wide as the needed device pixels (density variants: the smallest density ≥ DPR), as srcset picks it. savingsMarkdown(summary, title?) renders both as Markdown tables for merge request comments.
Changes
Section titled “Changes”codeChangeSchema,editSchema— zod validation ofCodeChangeincludingwrite-binaryedits.replaceFile(file, oldText, newText)— a whole-filereplaceedit;rangeOf,positionAt— 1-based ranges from offsets.changeItem(change, { tone?, tags? })— a reportchangesitem: text edits diffed withchangeDiff, binary writes as files without hunks (added, ormodifiedwhenpreviousSha256is set).Artifact—{ sha256, bytes, data }, the bytes behind awrite-binaryedit’sartifact.
Limitations
Section titled “Limitations”- Animated sources are re-encoded at their own size only; animated AVIF and APNG output are not supported by sharp.
- SSIM is a cheap luma-only proxy; it ignores chroma and alpha.
scanTagsis a regex scanner meant for statistics, not a full XML parser.