npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-eleventy-img

v0.6.2

Published

Astro + eleventy-img

Downloads

106

Readme

Astro + eleventy-img

A tiny script and component intended to be used with Astro for generating images with eleventy-img. It also supports creating blurry placeholders for said images

It was mostly made for my own website and I do not intend to really support this outside of my own needs as more modern and generalist solutions are in the work for Astro

Installation

npm install astro-eleventy-img

Due to an issue in the current version of Vite used by Astro, you need to add the following to your astro project config file:

vite: {
    ssr: {
      external: ["@11ty/eleventy-img"],
    },
},

Usage

The best way to use this in my opinion is to use it to generate images in the public folder. That way, they're copied directly to the result website. This is the default behavior.

Javascript Usage

Generating images

---
import { generateImage } from 'astro-eleventy-img';

const myImage = await generateImage('src/assets/my_image.png', {
  // The options here are passed directly to eleventy-img
  widths: [300],
  formats: ['webp', 'avif'],
});
---

<img src={myImage.webp[0].url} />

generateImage returns an object containing all the formats generated and metadata you can use in your HTML, so myImage is equal to:

{
  webp: [
    {
      format: 'webp',
      width: 300,
      height: 240,
      url: '/assets/images/RfSNa8TCUW-300.webp',
      sourceType: 'image/webp',
      srcset: '/assets/images/RfSNa8TCUW-300.webp 300w',
      filename: 'RfSNa8TCUW-300.webp',
      outputPath: 'public/assets/images/RfSNa8TCUW-300.webp'
    }
  ],
  avif: [
    {
      format: 'avif',
      width: 300,
      height: 240,
      url: '/assets/images/RfSNa8TCUW-300.avif',
      sourceType: 'image/avif',
      srcset: '/assets/images/RfSNa8TCUW-300.avif 300w',
      filename: 'RfSNa8TCUW-300.avif',
      outputPath: 'public/assets/images/RfSNa8TCUW-300.avif'
    }
  ]
}

The following files will be generated in the public/assets/images folder:

RfSNa8TCUW-300.webp
RfSNa8TCUW-300.avif

Generating placeholders

---
import { generatePlaceholder } from 'astro-eleventy-img';

const myPlaceholder = generatePlaceholder('src/assets/my_image.png');
---

<img src={myPlaceholder.dataURI} width={myPlaceholder.width} height={myPlaceholder.height} />

generatePlaceholder return an object containing all the properties needed for showing the image. So myPlaceholder in this example is equal to:

{
  dataURI: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAHCAIAAABV+fA3AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAz0lEQVQImQHEADv/AJ7Ho7PYwTt5UhdkNjmLSUOPUCB4MSFzMkGMNACixqksb04WUy4wdz9cnFxPnksbaS0HTCo3hDUAnLmcRnFLQ289ZpZXn7Z9MpA+OYU9EFstVZpHAJ3GaqnPbJXFW5iyZNzLqZWvco+9YHeqVlKXSACu1myt13Cc1GW6ynnFrIjK4ouSzFZ/vlNsskgAytqD1d6Q3+Kevsl/xb951+OXyt6FstVytNRzAIS0S4i2S5u8TK3NXrPOZ57CYqLGXWiqOoi4Rz9UYMGpm241AAAAAElFTkSuQmCC';
  width: 640;
  height: 514;
  quality: 60;
}

Component Usage

Alternatively, it can also be used through a provided component to automatically generate the proper HTML for including the image and its placeholder:

---
import { Image } from 'astro-eleventy-img';
---

<Image src="src/assets/my_image.png" alt="My super image!" caption="This is my favorite image." />

will generate the following HTML:

<figure>
  <picture>
    <source type="image/avif" srcset="/assets/images/4dWK95ygTq-640.avif 640w" />
    <source type="image/webp" srcset="/assets/images/4dWK95ygTq-640.webp 640w" />
    <img
      alt="My super image!"
      loading="lazy"
      decoding="async"
      style="
      background-size: cover;
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAHCAIAAABV+fA3AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAz0lEQVQImQHEADv/AJ7Ho7PYwTt5UhdkNjmLSUOPUCB4MSFzMkGMNACixqksb04WUy4wdz9cnFxPnksbaS0HTCo3hDUAnLmcRnFLQ289ZpZXn7Z9MpA+OYU9EFstVZpHAJ3GaqnPbJXFW5iyZNzLqZWvco+9YHeqVlKXSACu1myt13Cc1GW6ynnFrIjK4ouSzFZ/vlNsskgAytqD1d6Q3+Kevsl/xb951+OXyt6FstVytNRzAIS0S4i2S5u8TK3NXrPOZ57CYqLGXWiqOoi4Rz9UYMGpm241AAAAAElFTkSuQmCC);
    "
      onload="this.style.backgroundImage='none'"
      src="/assets/images/4dWK95ygTq-640.png"
      width="640"
      height="514"
    />
  </picture>
  <figcaption>
    <p>This is my favorite image.</p>
  </figcaption>
</figure>

and the following files will be generated in the public/assets/images folder:

4dWK95ygTq-640.avif
4dWK95ygTq-640.webp
4dWK95ygTq-640.png

The included Image component is a thin wrapper around generateImage and generatePlaceholder, it works for most needs but do not hesitate to build your own!

Complete Docs available here

Troubleshooting

require is not defined

Make sure to add the following to your astro.config.js config:

vite: {
    ssr: {
      external: ["@11ty/eleventy-img"],
    },
},