Poops integration
Next to being a standalone tool, poops-images is built to work with Poops.
It generates responsive variants that Poops consumes via discoverImageVariants() for
automatic srcset generation. Both the srcset filter and the image extension find
variants with one pattern:
/^(.+)-(\d+)w\.([a-z0-9]+)$/
Which is why output naming is a convention and not a preference: rename the files and the srcset stops assembling itself.
Running them together
# Build once, then run poops
npx poops-images && npx poops
# Watch mode alongside poops
npx poops-images --watch & npx poops
How it works
- poops-images generates variants from the image source directory into the static directory.
- In your templates, use the
imageextension to generate a whole<img>tag with asrcset, or thesrcsetfilter to generate only the attribute. - Both call
discoverImageVariants(imagePath, outputDir), which scans the output directory for matching files. - The
srcsetattribute is built from the widths that actually exist, withrelativePathPrefixprepended by default.
In a Nunjucks template
<!-- srcset filter -->
<img
src="/images/photo.jpg"
srcset="{{ 'images/photo.jpg' | srcset }}"
sizes="100vw"
alt="A photo"
/>
<!-- image extension (generates a complete <img> with srcset) -->
{% image "images/photo.jpg", "A photo" %}
Config inside poops.json
Instead of a separate poops-images.json, the config can live under the images key of
your poops.json:
{
"markup": { "...": "..." },
"images": {
"in": "src/images",
"out": "dist/static/images",
"sizes": [
{ "name": "thumb", "width": 300, "height": 300 },
{ "width": 800 },
{ "width": 1024 }
]
}
}
Do not process images in CI
Tip
If you deploy to GitHub Pages, do not run poops-images in the Actions run โ it burns
minutes re-encoding images that did not change. Output the images into the static
directory, commit that directory, and let Poops' copy move the static files into
dist during the build.