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

@zubyjs/image

v1.0.74

Published

Zuby.js image plugin

Downloads

3

Readme

@zubyjs/image

The plugin for Zuby.js that adds image optimization support to your project on top of the existing <Image> component.

It supports both build-time and runtime optimizations and therefore works seamlessly with both static and server output modes.

By default, it performs build-time optimizations for images on pre-rendered pages and runtime optimizations for images on server rendered pages.

This plugin uses sharp binaries under the hood.

Installation

First, install the @zubyjs/image package using your favorite package manager. If you aren't sure, you can use npm:

npm install @zubyjs/image

Then add the @zubyjs/image plugin to your zuby.config.mjs file under the plugins option:

  import { defineConfig } from 'zuby';
  import preact from '@zubyjs/preact';
+ import image from '@zubyjs/image';

  export default defineConfig({
    outDir: '.zuby',
    jsx: preact(),
+   plugins: [
+       image()
+   ]
    ^^^^^^^^
  });

And that's it! NOTE: Always make sure that all zuby packages are in sync in your package.json file:

  {
    "name": "my-zuby-app",
    "version": "1.0.0",
    "dependencies": {
      "zuby": "latest",
      "@zubyjs/image": "latest"
    }
  }

Usage

Image Component

Now you can use the <Image> component in your Zuby.js project as you're used to. Example:

+ import Image from '@zubyjs/preact/image';

  export default function Home() {
    return (
      <div>
-       <img src="/path/to/image.jpg" alt="My image" />
+       <Image src="/path/to/image.jpg" alt="My image" />
      </div>
    );
  }

NOTE: Upper example is showing usage with @zubyjs/preact JsxProvider. If you are using different one, please replace @zubyjs/preact/image with the correct package name.

Image Component Props

Image component accepts following props:

  • src - path to the image
  • alt - alt text for the image
  • width - width of the image
  • height - height of the image
  • quality - quality of the image (default: 80)
  • format - format of the image (default: webp)

If no quality or format is provided, the default quality from zuby.config.mjs will be used. This can be changed globally by setting image.defaultFormat and image.defaultQuality in zuby.config.mjs.

If no width and height are provided, the image will be rendered in its original size with max resolution 1920px.

While resizing the image, the original aspect ratio is always preserved. The Image will try to load the src image with the nearest size to the provided width and height and then resize it to the exact size using CSS.

Runtime Image Optimization

For runtime image optimization, the plugin will add new endpoint to your server at /_image. This endpoint accepts input in the following format:

/_image/${quality}/${width}/${height}/${src}.${format}

The format is used to find the optimized images in the client directory on filesystem and allows you to easily serve already optimized images from the CDN or custom web server.

For example to optimize image on path /images/dog.jpg to 80% quality, 300px width, 200px height and output format webp you can use the following URL:

/_image/80/300/200/path/to/image.jpg.webp

To omit any of the parameters and use the default values or the original image values, you can use auto as the value. Example:

/_image/auto/300/200/path/to/image.jpg.webp

If the output format is same as the input format, you can leave the extension unchanged. Example:

/_image/auto/300/200/path/to/image.jpg

Build-time Image Optimization

During the build, the plugin will optimize all images on pre-rendered pages and put them into the client/_image directory. Those images won't be optimized again on runtime.

If you're satisfied with the build-time optimization results, you can copy the client/_image directory to your public directory to skip the build-time optimization for those images in the future.

The build-time optimization is very useful for static output mode and can save a lot of time and resources on the server. On the other hand, it can significantly increase your build time.

If you want to disable the build-time optimization, you can set buildTime plugin option to false in your zuby.config.mjs or set maximum number of images to optimize using maxBuildTime option. The default value is 1000 images and the rest is optimized on-demand on runtime.

  import { defineConfig } from 'zuby';
  import preact from '@zubyjs/preact';
+ import image from '@zubyjs/image';

  export default defineConfig({
    outDir: '.zuby',
    jsx: preact(),
    plugins: [
       image({
+           maxBuildTime: 100,
+           buildTime: true
            ^^^^^^^^
       })
    ]
  });

Contributing

This package is part of Zuby.js workspace and maintained by the team behind the Zuby package. Please refer to it for more details how to contribute.

License

MIT