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

loader-probe-image-size

v1.0.5

Published

A webpack image loader with additional size information (using probe-image-size)

Downloads

20

Readme

Webpack image loader with additional size information

This loader uses probe-image-size. While there are already a few other webpack loaders out there that will additionally fetch images sizes - some did not seem very active to me. some had problems loading guetzli-fied JPEGs, etc.

So with all the best intentions - here is yet another loader.

Why?

There are certain use cases where it is beneficial to know the dimensions of the image you (or your page) is about to show.

  • an image album (e.g. a bit like masonry)
  • using images without a page re-render when loading is done (usually a page 'jumps' when the actua image is shown - moving content further down the page as more images load. If you know their sizes you are able to reserve space in the layout and offer a slightly better UX)
  • .. even more

Similar Projects

  • https://www.npmjs.com/package/probe-image-size-loader
  • https://github.com/boopathi/image-size-loader
  • https://github.com/tremby/image-dimensions-loader
  • https://github.com/dashed/sizeof-loader

Usage

Documentation: Using loaders

Installation

npm  install --save-dev loader-probe-image-size

Configuration

Add the loader to your webpack configuration (or replace your existing image loader)

// webpack.config.[ts|js]
// ...
  module: {
    loaders: [
      {
        test: /\.(gif|jpe?g|png|svg|webp|ico)$/,
		use: [{
		  loader: 'loader-probe-image-size',
		    options: {
			  context: path.resolve(__dirname, 'src')
		    }
		  }
		]
      }
    ]
  }
// ...

Advanced usage The loader uses the interpolatename function from loader-utils - meaning you can modify the filename of the emitted file via options: {name: ...} .

The following tokens are replaced in the name parameter:

  • [ext] the extension of the resource
  • [name] the basename of the resource
  • [path] the path of the resource relative to the context query parameter or option.
  • [folder] the folder the resource is in.
  • [emoji] a random emoji representation of options.content
  • [emoji:<length>] same as above, but with a customizable number of emojis
  • [contenthash] the hash of options.content (Buffer) (by default it's the hex digest of the md5 hash)
  • [<hashType>:contenthash:<digestType>:<length>] optionally one can configure
    • other hashTypes, i. e. sha1, md5, sha256, sha512
    • other digestTypes, i. e. hex, base26, base32, base36, base49, base52, base58, base62, base64
    • and length the length in chars
  • [hash] the hash of options.content (Buffer) (by default it's the hex digest of the md5 hash)
  • [<hashType>:hash:<digestType>:<length>] optionally one can configure
    • other hashTypes, i. e. sha1, md5, sha256, sha512
    • other digestTypes, i. e. hex, base26, base32, base36, base49, base52, base58, base62, base64
    • and length the length in chars
  • [N] the N-th match obtained from matching the current file name against options.regExp

Shamelessly stolen from / source: https://github.com/webpack/loader-utils#interpolatename

Usage

Import images from within your code

import image from './myimagefile.jpeg';

// => emits myimagefile.jpeg
// => image is an object {
//	src: string,
//	height: number
//	width: number
// 	type: [svg|webp|jpeg|png|...]
//  toString: () => string ...
// }

Usage with TypeScript

And for those fancy typescript users - you might want to add additional info to your image modules, e.g. /src/@types/images.d.ts, so your IDE of choice will assist you a bit more.

declare module '*.jpg' {
	export const src: string;
	export const type: string;
	export const width: number;
	export const height: number;
}