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 🙏

© 2025 – Pkg Stats / Ryan Hefner

next-universal-image-optimizer

v0.0.2

Published

A build-time image optimization tool for Next.js, supporting both internal and external image sources.

Readme

next-universal-image-optimizer

A build-time image optimization tool for Next.js (and other static build environments) that supports both internal and external images. It allows you to:

  • Fetch images from remote URLs (e.g., from a CMS).
  • Discover and optimize local images via glob patterns (e.g., public/images/**/*).
  • Generate multiple responsive variants in various widths or scales.
  • Convert images to optimized formats (e.g., WebP) with configurable quality.

Note: This tool is designed for static builds and works well with next export scenarios, where dynamic optimization provided by next/image is not available.

Features

  • Universal Input: Supports both local files and remote URLs.
  • Configurable: Easily adjust widths, quality, output directories, and modes (widths/scales) via config or runtime options.
  • Uses Sharp: sharp handles efficient resizing and conversion.
  • Next.js Integration: Ideal for next export scenarios to ensure that final static output images are optimized.

Installation

Use your package manager of choice:

pnpm add next-universal-image-optimizer

(You can also use npm or yarn.)

Usage

1. Configuration File

Create nuio.config.ts (or .js) at your project root:

export default {
   inputPaths: [
      // 'https://example.com/remote-image.jpg',
      // 'public/images/local-file.png'
   ],
   outputPatterns: [
      'public/images/**/*.{jpg,png,webp}'
   ],
   outputDir: 'out/images',
   widths: [640, 1280, 1920],
   quality: 80,
   modes: ['scales', 'widths'], // enable scaling and widths mode
   scales: {
      '@3x': 1,
      '@2x': 2/3,
      '@1x': 1/3
   },
   envMode: process.env.ENV_MODE || 'development'
}

2. Calling optimizeImages

Call optimizeImages() without arguments to use nuio.config.ts defaults:

import { optimizeImages } from 'next-universal-image-optimizer'

async function run() {
  await optimizeImages()
  console.log('Images optimized successfully!')
}

run().catch(err => {
  console.error('Failed to optimize images:', err)
  process.exit(1)
})

You can override defaults by passing options:

await optimizeImages({
  inputPaths: ['https://example.com/another-remote.jpg'],
  outputPatterns: ['public/images/**/*.png'],
  widths: [320, 640],
  quality: 90
})

3. Integrating with Next.js (next export)

For next export scenarios, run optimizeImages after exporting:

Example package.json scripts:

{
   "scripts": {
      "prebuild": "node scripts/fetch-images.js",
      "build": "next build && next export && node scripts/post-export-optimize.js"
   }
}

In scripts/post-export-optimize.js:

import { optimizeImages } from 'next-universal-image-optimizer'

async function postExport() {
   await optimizeImages()
   console.log('Images optimized successfully!')
}

postExport().catch(console.error)

After next export, optimized images will be in out/images. Reference them directly in your static HTML.

4. Testing

Use Jest or another test runner. For examples, see test/index.test.ts for mock and snapshot testing patterns.

5. Advanced Usage

  • External Image Lists: Fetch a list of remote images from a CMS and pass them to optimizeImages before building.
  • Different Formats: By default, images are converted to WebP. Adjust sharp options to convert to other formats if needed.

Contributing

Contributions are welcome! Feel free to open issues or PRs on GitHub. For feature requests, please describe your use case and desired outcome.

License

MIT License