Tools Next.js Images
Image optimization plugin for Next.js — WebP conversion, LQIP placeholders, responsive images, SVG sprites, and compression.
@vitus-labs/tools-nextjs-images is a Next.js webpack plugin that provides image optimization via import-time resource queries. Convert to WebP, generate low-quality placeholders, create responsive variants, build SVG sprites, and compress images — all through import syntax.
Installation
npm install @vitus-labs/tools-nextjs-imagesRequirements
- Node.js
>= 22.12— imagemin plugins are ESM-only and are loaded synchronously viarequire(esm) - Peer dependency:
next >= 14 - webpack only — see below
Turbopack is not supported. Next.js 16 uses Turbopack by default for next dev and next build, and Turbopack ignores the webpack config function this plugin relies on — images would be silently left unprocessed. Run Next with webpack:
{
"scripts": {
"dev": "next dev --webpack",
"build": "next build --webpack"
}
}When Turbopack is detected (TURBOPACK is set by Next.js), the plugin prints a one-time warning.
Optional packages
All optimizers are optional. Install the ones for the features you use in your own project — they are detected from the project root (or from the directory given by overwriteImageLoaderPaths in your Next.js config), with a fallback to this package's own resolution.
| Feature | Packages |
|---|---|
| Compression of JPEG / PNG / GIF / SVG | imagemin-mozjpeg, imagemin-optipng or imagemin-pngquant, imagemin-gifsicle, imagemin-svgo |
?webp and .webp optimization | webp-loader |
?lqip, ?lqip-colors | lqip-loader |
?resize, ?size, ?sizes[] | responsive-loader and sharp (or jimp) |
?sprite | svg-sprite-loader |
?trace | image-trace-loader |
bun add -d imagemin-mozjpeg imagemin-optipng imagemin-pngquant imagemin-gifsicle imagemin-svgo webp-loader
bun add -d lqip-loader responsive-loader sharp svg-sprite-loader image-trace-loaderIf optimizeImages is enabled but no optimizer package is found, a warning is printed during the server compilation. Set optimizeImages: false to silence it.
Quick Start
// next.config.ts
import withOptimizedImages from '@vitus-labs/tools-nextjs-images'
export default withOptimizedImages()({
// Your standard Next.js config
})Import Queries
Use resource queries on imports to trigger different optimizations:
// Standard optimized image
import photo from './photo.jpg'
// Convert to WebP
import webp from './photo.jpg?webp'
// Low-quality placeholder (default export is the tiny base64 `preSrc`)
import placeholder from './photo.jpg?lqip'
// Color palette extraction (default export is the palette)
import palette from './photo.jpg?lqip-colors'
// SVG trace placeholder
import trace from './photo.jpg?trace'
// Force file URL (no inlining)
import url from './photo.jpg?url'
// Force data URI inline
import inline from './photo.jpg?inline'
// Raw SVG content
import svg from './icon.svg?include'
// SVG sprite (React component)
import Icon from './icon.svg?sprite'
// Responsive variants (responsive-loader syntax)
import responsive from './photo.jpg?resize&sizes[]=300&sizes[]=600&sizes[]=1200'
// Skip optimization
import original from './photo.jpg?original'
// Combine with ?original to skip optimization for url / inline / include / trace
import rawUrl from './photo.jpg?url&original'Queries are matched on parameter boundaries — ?webp matches ?webp, &webp, ?webp=… but not ?awebp or ?webpage=1. Combinations with original can be written in either order (?url&original or ?original&url).
Resource Query Reference
| Query | Purpose | Optimized | Output |
|---|---|---|---|
| (none) | Default processing | Yes | URL or data URI |
?url | Force file URL | Yes | URL string |
?inline | Force data URI | Yes | Data URI string |
?include | Raw content | Yes | String content |
?original | Skip optimization | No | URL or data URI |
?webp | Convert to WebP | Yes | URL string |
?lqip | Low-quality placeholder | No | preSrc data URI (lqip-loader's src / preSrc attached) |
?lqip-colors | Palette extraction | No | Palette (lqip-loader output attached) |
?trace | SVG trace placeholder | Yes | image-trace-loader output |
?sprite | SVG sprite component | If imagemin-svgo is installed | React component |
?resize / ?size / ?sizes[] | Responsive variants | No | responsive-loader output |
?url, ?inline, ?include and ?trace can be combined with &original to skip optimization. ?webp on JPEG/PNG/SVG/GIF files is only available when 'webp' is in handleImages, and ?sprite only when svg-sprite-loader is installed.
Configuration
import withOptimizedImages from '@vitus-labs/tools-nextjs-images'
export default withOptimizedImages({
// Core options
optimizeImages: true, // Optimize in production (default: true)
optimizeImagesInDev: false, // Optimize in development (default: false)
handleImages: ['jpeg', 'png', 'svg', 'webp', 'gif'], // Formats to process ('jpg' and 'ico' also accepted)
inlineImageLimit: 8192, // Bytes threshold for data URI inlining
defaultImageLoader: 'img-loader', // 'img-loader' or 'responsive-loader'
// Output
imagesFolder: 'images', // Output folder name
imagesName: '[name]-[hash].[ext]', // Filename pattern
removeOriginalExtension: false, // Strip extension on format conversion
// Optimizer options (pass-through to underlying libraries)
mozjpeg: { quality: 80 },
optipng: {},
pngquant: {},
gifsicle: { interlaced: true, optimizationLevel: 3 },
svgo: { plugins: [{ name: 'removeViewBox', active: false }] },
webp: { quality: 80 },
svgSpriteLoader: { symbolId: '[name]-[hash:8]' },
responsive: {}, // adapter auto-detected (sharp, else jimp) unless `adapter` is set
lqip: {},
imageTrace: {},
})({
// Standard Next.js config
// overwriteImageLoaderPaths: '/path/to/project', // where to resolve optimizer packages from
})OptimizedImagesConfig
| Option | Type | Default | Description |
|---|---|---|---|
optimizeImages | boolean | true | Enable optimization in production |
optimizeImagesInDev | boolean | false | Enable optimization in development |
handleImages | string[] | ['jpeg', 'png', 'svg', 'webp', 'gif'] | Image formats to process. Also accepts 'jpg' (same as 'jpeg') and 'ico' (copied via file-loader, never optimized) |
inlineImageLimit | number | 8192 | Bytes threshold for data URI inlining |
defaultImageLoader | string | 'img-loader' | Default loader: 'img-loader' or 'responsive-loader' (JPEG/PNG are then handled by responsive-loader instead of img-loader) |
imagesFolder | string | 'images' | Output folder name within static/ |
imagesName | string | '[name]-[hash].[ext]' | Output filename pattern |
removeOriginalExtension | boolean | false | Strip original extension when converting |
imagesPublicPath | string | — | Override public path (takes precedence over assetPrefix) |
imagesOutputPath | string | — | Override output directory |
overwriteImageLoaderPaths is read from the Next.js config (second call), not from OptimizedImagesConfig: it is the directory optimizer packages are resolved from.
Optimizer Options
| Option | Library | Formats |
|---|---|---|
mozjpeg | imagemin-mozjpeg | JPEG |
optipng | imagemin-optipng | PNG |
pngquant | imagemin-pngquant | PNG (alternative) |
gifsicle | imagemin-gifsicle | GIF |
svgo | imagemin-svgo | SVG |
webp | webp-loader | WebP conversion |
svgSpriteLoader | svg-sprite-loader | SVG sprites |
responsive | responsive-loader | JPEG, PNG responsive (adapter optional) |
lqip | lqip-loader | JPEG, PNG placeholders |
imageTrace | image-trace-loader | JPEG, PNG, GIF traces |
Format Support Matrix
| Format | Compress | WebP Convert | Responsive | Sprite | LQIP | Trace |
|---|---|---|---|---|---|---|
| JPEG | mozjpeg | ?webp | ?resize | — | ?lqip | ?trace |
| PNG | optipng/pngquant | ?webp | ?resize | — | ?lqip | ?trace |
| SVG | svgo | — | — | ?sprite | — | — |
| GIF | gifsicle | — | — | — | — | ?trace |
| WebP | webp-loader | Direct | — | — | — | — |
Detected Loaders
The plugin auto-detects which optimizer packages are installed. Detection runs once per project root and is memoized (webpack calls the config function once per compiler):
interface DetectedLoaders {
jpeg: string | false // 'imagemin-mozjpeg' or false
png: string | false // 'imagemin-optipng' or 'imagemin-pngquant' or false
gif: string | false // 'imagemin-gifsicle' or false
svg: string | false // 'imagemin-svgo' or false
svgSprite: string | false // 'svg-sprite-loader' or false
webp: string | false // 'webp-loader' or false
lqip: string | false // 'lqip-loader' or false
responsive: string | false // responsive-loader path or false
responsiveAdapter: string | false // 'sharp' or 'jimp' or false
}Build Phase Behavior
| Phase | Optimization |
|---|---|
| Production build | optimizeImages setting (default: true) |
| Static export | optimizeImages setting (default: true) |
| Development server | optimizeImagesInDev setting (default: false) |
| Any other phase | Not optimized |
The phase comes from the second argument of the returned function (the next-compose-plugins style { phase }). Without a phase, optimizeImages applies to non-dev builds and optimizeImagesInDev to dev builds.
When optimization is disabled, images are still processed (file-loader/url-loader) but no compression or conversion is applied.
SVG Sprite Components
SVG sprites generated via ?sprite become React components with metadata:
import Icon from './icon.svg?sprite'
// Render as component
<Icon />
// Access metadata
Icon.viewBox // '0 0 24 24'
Icon.id // 'icon-a1b2c3d4'
Icon.url // '#icon-a1b2c3d4'
Icon.content // symbol markupProps passed to the component are spread onto the <svg> element. The svgSpriteLoader options are forwarded to svg-sprite-loader.
Exports
import withOptimizedImages from '@vitus-labs/tools-nextjs-images'
import type {
DetectedLoaders,
NextConfig,
OptimizedImagesConfig,
} from '@vitus-labs/tools-nextjs-images'Last updated on