Import JPG, PNG, GIF, SVG, and WebP files as base64 strings or DOM Image elements
npx @tessl/cli install tessl/npm-rollup--plugin-image@3.0.0A Rollup plugin that imports JPG, PNG, GIF, SVG, and WebP files directly into JavaScript modules. The plugin converts images to base64-encoded strings or DOM Image elements, making them immediately available at startup without requiring asynchronous loading.
Images are encoded using base64, which means they will be 33% larger than the size on disk. This plugin is ideal for small images where the convenience of immediate availability outweighs the 33% size increase, such as icons, logos, or UI elements that need to be available synchronously.
npm install @rollup/plugin-image --save-devimport image from '@rollup/plugin-image';For CommonJS:
const image = require('@rollup/plugin-image');// rollup.config.js
import image from '@rollup/plugin-image';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [image()]
};// src/index.js
import logo from './rollup.png';
console.log(logo); // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."// rollup.config.js with DOM mode enabled
import image from '@rollup/plugin-image';
export default {
input: 'src/index.js',
output: { dir: 'output', format: 'cjs' },
plugins: [image({ dom: true })]
};// src/index.js
import logo from './rollup.png';
document.body.appendChild(logo); // logo is now a DOM Image elementCreates a Rollup plugin instance that processes image files.
/**
* Creates a Rollup plugin for importing image files as base64 strings or DOM elements
* @param options - Configuration options for the plugin
* @returns Rollup plugin object
*/
function image(options?: RollupImageOptions): Plugin;interface RollupImageOptions {
/**
* A picomatch pattern, or array of patterns, which specifies the files
* the plugin should operate on. By default all files are targeted.
*/
include?: FilterPattern;
/**
* A picomatch pattern, or array of patterns, which specifies the files
* the plugin should ignore. By default no files are ignored.
*/
exclude?: FilterPattern;
/**
* If true, generates DOM Image elements instead of base64 strings.
* If false (default), generates base64 data URI strings.
* @default false
*/
dom?: boolean;
}Usage Examples:
// Basic usage - processes all image files
export default {
plugins: [image()]
};
// Filter specific files
export default {
plugins: [image({
include: ['**/*.png', '**/*.jpg'],
exclude: ['**/test-images/**']
})]
};
// DOM mode for browser usage
export default {
plugins: [image({ dom: true })]
};The plugin returns a standard Rollup plugin object with the following structure:
interface Plugin {
/** Plugin identifier */
name: 'image';
/**
* Load hook that processes image files and returns transformed code
* @param id - File path being loaded
* @returns Transformed JavaScript code or null if file should be ignored
*/
load(id: string): string | null;
/**
* Adds a file to Rollup's watch list (called internally by load hook)
* @param id - File path to watch for changes
*/
addWatchFile?(id: string): void;
}The plugin processes files with the following extensions and MIME types:
image/jpeg): .jpg, .jpegimage/png): .pngimage/gif): .gifimage/svg+xml): .svgimage/webp): .webpWhen dom: false (default), imported images are exported as base64-encoded data URI strings:
import logo from './image.png';
// logo is a string: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
// Can be used in CSS, HTML, or canvas contexts
const img = new Image();
img.src = logo;When dom: true, imported images are exported as ready-to-use DOM Image elements:
import logo from './image.png';
// logo is an Image object that can be directly appended to DOM
document.body.appendChild(logo);
// or
canvas.getContext('2d').drawImage(logo, 0, 0);import type { FilterPattern } from '@rollup/pluginutils';
import type { Plugin } from 'rollup';
type FilterPattern = string | RegExp | Array<string | RegExp> | null;createFilter() utility and FilterPattern typefs.readFileSync() for file reading, path.extname() for extension detectionThe plugin handles various error conditions during the build process:
.jpg, .jpeg, .png, .gif, .svg, .webp) are ignored (plugin returns null)null)fs.readFileSync() to throw system errors, failing the buildError: Could not load ./path/to/image.png (imported by src/index.js)This occurs when the image file doesn't exist at the specified path.
ENOENT: no such file or directory, open './path/to/image.png'System error when the file cannot be read from disk.