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

svimg_fork

v3.2.0

Published

Svelte image component with image preprocessing and lazy loading

Downloads

12

Readme

svimg

svimg is an image preprocessing and lazy loading component for Svelte. It consists of:

  • A Svelte preprocessor that automatically resizes your images to multiple resolutions in a srcset, creates additional AVIF and WebP versions, and generates blurred placeholder images
  • A Svelte component that displays the blurred placeholder and automatically lazy loads the proper resolution image when it comes into view

svimg uses native browser lazy loading with a fallback to IntersectionObserver, and automatically calculates the appropriate sizes attribute. Some other image components do not set a proper sizes attribute, which can cause the browser to download a much larger resolution image than necessary if you are resizing the image with CSS. svimg will also use a literal width if specified to control image preprocessing and only generate the necessary image files for that width.

Getting Started

Installation

Since svimg is an external Svelte component, you'll want to make sure it gets bundled by Svelte during compile by installing it as a dev dependency (or modifying your bundler config to not treat it as an external).

npm install -D svimg

In svelte.config.js, add imagePreprocessor as a preprocessor:

import { vitePreprocess } from '@sveltejs/kit/vite';
import { imagePreprocessor } from 'svimg';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: [
        imagePreprocessor({
            inputDir: 'static',
            outputDir: 'static/g',
            webp: true,
            avif: true
        }), 
        vitePreprocess()
    ]
};

export default config;

rollup-plugin-svelte does not yet have support for svelte.config.js, so if you're using it you must pass the options inline. In rollup.config.js, add imagePreprocessor as a preprocessor for rollup-plugin-svelte:

import { imagePreprocessor } from 'svimg';

export default {
    plugins: [
        svelte({
            preprocess: [
                imagePreprocessor({
                    inputDir: 'public',
                    outputDir: 'public/g',
                    webp: true,
                    avif: true
                })
            ]
        })
    ]
};

Usage

Svelte Component

<script>
import Image from 'svimg';
</script>

<Image src="images/splash.jpg" />

<Image src="images/avatar.jpg" width="150" alt="Avatar" class="blue-border" quality="85" immediate />

The Image component will render a blurred placeholder, a srcset with multiple resolutions, a sizes attribute, and sources of type image/avif with avif images and image/webp with webp images.

Custom Element

svimg is also exposed as a custom element, which means it can be used independently of Svelte with the <s-image> tag.

Usage as a custom element expects that the attributes that would normally be filled in by the Svelte preprocessor (srcset, srcsetavif, srcsetwebp, placeholder) are populated by another method.

Generally, you'd use another tool to create these elements such as rehype-svimg rather than using the custom element directly.

<script>
import 'svimg/dist/s-image';
</script>

<s-image srcset="images/splash-600.jpg 600w, images/splash-1200.jpg 1200w" srcsetavif="images/splash-600.avif 600w, images/splash-1200.avif 1200w" srcsetwebp="images/splash-600.webp 600w, images/splash-1200.webp 1200w" />

Configuration

Component Attributes

| Property | Default | | | --------- | ---------- | --------- | | src | required | Image url | | alt | | Alternate text for the image | | class | | CSS classes to apply to image | | width | | Resize image to specified width in pixels. If not specified, generates images of widths 480, 1024, 1920, and 2560. | | immediate | false | Set to true to disable lazy-loading | | blur | 40 | Amount of blur to apply to placeholder | | quality | sharp default | Quality of the resized images, defaults to sharp's default quality for each image format |

The following properties will be automatically populated by the preprocessor:

| Property | | | ----------- | ------- | | srcset | Responsive images and widths | | srcsetavif | Responsive AVIF images and widths | | srcsetwebp | Responsive WebP images and widths | | placeholder | Blurred placeholder image | | aspectratio | Aspect ratio of image | | placeholdersrc | Placeholder file src | | placeholderwebp | Placeholder webp file src | | placeholderavif | Placeholder avif file src |

Preprocessor Options

| Option | Default | | | --------- | ---------- | ---------- | | inputDir | required | The static asset directory where image urls are retrieved from | | outputDir | required | The output directory where resized image files should be written to. This should usually be a subdirectory within the normal static asset directory | | srcGenerator | | An optional function to override the logic of how src URLs are generated for the srcset. This is called once per generated image file, and can be used to customize the generated image URLs - for example, to add or remove path components or to specify a CDN domain.The expected callback signature is:(path: string, { src, inputDir, outputDir }?: SrcGeneratorInfo) => stringThe first parameter is the path to the generated image relative to the outputDir, with path separators already normalized to /. The second optional parameter provides the original image src and the inputDir/outputDir options, and the return value is the URL for the image to be used in the srcset.The default behavior without this parameter will work for common use cases, where the outputDir is a subdirectory of the inputDir static asset directory and the site is served from the root of the domain. | publicPath | The outputDir relative to the inputDir static asset directory | DEPRECATED publicPath is deprecated and will be removed in the next major version. Use a srcGenerator function instead: (path) => '/my/public/path' + pathThe public path that images will be served from. This will be prepended to the src url during preprocessing. | avif | true | Whether to generate AVIF versions of images in addition to the original image formats | | webp | true | Whether to generate WebP versions of images in addition to the original image formats | | embedPlaceholder | true | Set to false to generate placeholder images as separate image files, rather than embedding them into the document. This will save network traffic since standalone image files are noticeably smaller than ones embedded in the HTML document, will allow the placeholder to be served in next-gen image formats like avif/webp with fallbacks like the main image is, and will allow the browser to better optimize caching of the files. However, as a separate network request, there is the potential for a slight delay in render of the placeholder, particularly if the image is above-the fold (consider using immediate to disable lazy-loading for known above-the-fold images, which will perform better). Non-embedded placeholders are likely to become the default in a future major release.

Built With

Authors

  • Chris Han - Initial work - xiphux

License

This project is licensed under the ISC License - see the LICENSE.md file for details

Acknowledgements