1.0.2 • Published 8 months ago

@randajan/esbuild-plugin-image-size v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@randajan/esbuild-plugin-image-size

NPM JavaScript Style Guide


šŸš€ Features

āœ… Extracts image metadata (width, height, aspectRatio)
āœ… Generates an importable object when using images in ESBuild
āœ… Supports file filtering using entries (like .gitignore)
āœ… Works on all platforms (Windows, Linux, macOS)
āœ… Optimized for performance – filters files directly in onLoad


šŸ“¦ Installation

npm install esbuild-plugin-image-meta --save-dev

šŸ“Œ Usage

šŸ–¼ Importing an image

import image from "./assets/photo.jpg";

console.log(image);
/*
{
  src: "/images/photo.jpg",
  width: 1920,
  height: 1080,
  aspectRatio: 1.777
}
*/

šŸ”§ Configuration

You can configure the plugin with the following options:

import esbuild from "esbuild";
import imageSizePlugin from "esbuild-plugin-image-meta";

esbuild.build({
    entryPoints: ["src/index.js"],
    bundle: true,
    outdir: "dist",
    plugins: [
        imageSizePlugin({
            entries: ["src/images/**", "!src/excluded/**"], // Filter files
            extensions: ["png", "jpg"],                    // Allowed image types
            outputDir: "assets",                           // Output directory
            trait: (meta, buffer) => ({                    // Modify metadata (optional)
                ...meta,
                size: buffer.byteLength
            })
        })
    ]
}).catch(() => process.exit(1));

šŸŽÆ Options

OptionTypeDefaultDescription
entriesstring | string[]"*" (process all)Defines which images should be processed (supports glob patterns).
extensionsstring[]["png", "jpg", "jpeg", "gif", "webp", "svg"]Specifies which image types to process.
outputDirstring"images"Defines where processed images are stored in the output directory.
trait(meta, buffer) => objectIdentity functionAllows modifying the export object before bundling.

šŸ“Œ Example with Custom Metadata

If you want to add the file size to the exported object:

imageSizePlugin({
    trait: (meta, buffer) => ({
        ...meta,
        size: buffer.byteLength
    })
});

Would generate:

{
  src: "/images/photo.jpg",
  width: 1920,
  height: 1080,
  aspectRatio: 1.777,
  size: 345678
}

šŸ“„ License

MIT Ā© Your Name


šŸ”„ Now your ESBuild pipeline automatically processes images and gives you structured metadata! šŸš€