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

sharp-transform-loader

v4.0.0

Published

Sharp transform loader module for webpack

Downloads

219

Readme

Sharp-transform-loader

Used sharp under the hood

A webpack loader for generating images different sizes from the source image. Optionally loader supports webp format and generates placeholder for this image.

Motivation: automatically resize source image and return requested dimensions in two format initial and webp as srcset string. Optionally generate blurred placeholder for the image.

Getting started

To begin, you'll need to install sharp-transform-loader:

  npm i -D sharp-transform-loader

Usage

Basically is two configuration for the loader:

  • loader itself
  • image that you want to be processed by loader

for all images general loader config:

const webpackConfig = {
  // Only showing relevant parts.
  module: {
    rules: [
      {
        // match image files
        test: /\.(jpe?g|png|svg|gif)$/,
        use: [
          {
            loader: 'sharp-transform-loader',
            options: {
              sizes: ['400w', '2x'],
              placeholder: true
            }
          },
          {
            loader: 'file-loader',
            options: {
              name: '[contenthash][name].[ext]',
              esModule: false,
            },
          }
          // chain of other loader
        ]
      }
    ]
  }

  // ...
};

direct for the image that should be processed:

import image './image.jpeg?sizes=400w+400w.webp+800w+800w.webp&placeholder'

or in more canonical way:

import image './image.jpeg?sizes[]=400w&sizes[]=800w&placeholder'

This allows us to separate the configuration of the loader, and the specification of the image sizes.

sharp-transform-loader options

image

image default field consist image in provided format.

sizes

sizes this is the main feature of the sharp-transform-loader, use this option to specify the different image sizes you wish to import. You can either specify the different sizes as a standard array (?sizes[]=100w&sizes[]=200w) or using the less verbose, syntax (?sizes=100w+200w)

sizes values must follow format <number>w, <multiplier>x, also size can be processed into webp format <number>w.webp.

placeholder

placeholder will generate tiny inline base64 20px width blurred image and return object with placeholder url and aspectRatio.

'webp'

webp will transform source to webp format and return path to xxxx.webp image.

Results of transforming image

sources : object - all requested sizes for the image

import image from './image.jpeg?sizes=200w+200w.webp+800w+800w.webp';
/*
  image.sources => {
    '200w': 'xxxx.jpeg',
    '200w.webp': 'xxxx.webp',
    '800w': 'xxxx.jpeg',
    '800w.webp': 'xxxx.webp',
  }
  */

placeholder : object - object with keys:

  • url: base64 string blurred small image.
  • aspectRatio: number the width to height ratio of the image
import image from './image.jpeg?sizes=200w+800w&placeholder';
/*
  image.placeholder => {
    'url': 'data:image/jpeg;base64,xxxxxxxxxxx...',
    'aspectRatio': 1.4992435703479576,
  }
  */

srcSet: string - srcset string like value for <img srcset="..."/>.

import image from './image.jpeg?sizes=200w+800w';
// image.srcSet => 'xxxx.jpeg 200w,xxxx.jpeg 800w'

srcSetWebp - srcset with webp format images

import image from './image.jpeg?sizes=200w.webp+800w.webp';
// image.srcSetWebp => 'xxxx.webp 200w,xxxx.webp 800w'

webp - path to webp format image

import image from './image.jpeg?webp';
// image.webp => 'xxxx.webp'

Inspired mostly by src-loader, file loader.