A set of utility functions commonly used by Rollup plugins
npx @tessl/cli install tessl/npm-rollup--pluginutils@5.3.0A comprehensive set of utility functions commonly used by Rollup plugins. This package provides essential tools for plugin development including file filtering, path normalization, AST analysis, code generation, and pattern matching utilities.
npm install @rollup/pluginutils --save-devimport { createFilter, dataToEsm, makeLegalIdentifier } from "@rollup/pluginutils";For CommonJS:
const { createFilter, dataToEsm, makeLegalIdentifier } = require("@rollup/pluginutils");Legacy default export (to be removed in future versions):
import utils from "@rollup/pluginutils";import { createFilter, addExtension, normalizePath } from "@rollup/pluginutils";
// Create a file filter for plugin processing
const filter = createFilter(
["src/**/*.js", "src/**/*.ts"], // include patterns
["src/**/*.test.*"], // exclude patterns
{ resolve: process.cwd() }
);
// Use in plugin transform hook
export default function myPlugin(options = {}) {
return {
resolveId(id) {
// Add .js extension if missing
return addExtension(id);
},
transform(code, id) {
// Normalize path for cross-platform compatibility
const normalizedId = normalizePath(id);
// Only process files that match our filter
if (!filter(normalizedId)) return;
// Transform the code...
return { code: transformedCode };
}
};
}@rollup/pluginutils is organized around four main functional areas:
Each utility is designed to be lightweight, composable, and handle cross-platform concerns automatically.
Essential utilities for handling file paths, extensions, and filtering in plugin workflows.
function createFilter(
include?: FilterPattern,
exclude?: FilterPattern,
options?: { resolve?: string | false | null }
): (id: string | unknown) => boolean;
function addExtension(filename: string, ext?: string): string;
function normalizePath(filename: string): string;
type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null;Tools for analyzing Abstract Syntax Trees, tracking variable scopes, and extracting assignment patterns.
function attachScopes(ast: BaseNode, propertyName?: string): AttachedScope;
function extractAssignedNames(param: BaseNode): string[];
interface AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };
addDeclaration(node: BaseNode, isBlockDeclaration: boolean, isVar: boolean): void;
contains(name: string): boolean;
}Utilities for generating valid JavaScript code, identifiers, and ES modules from data structures.
function makeLegalIdentifier(str: string): string;
function dataToEsm(data: unknown, options?: DataToEsmOptions): string;
interface DataToEsmOptions {
compact?: boolean;
includeArbitraryNames?: boolean;
indent?: string;
namedExports?: boolean;
objectShorthand?: boolean;
preferConst?: boolean;
}Pattern matching utilities for creating precise RegExp patterns, commonly used in plugin hook filters.
function exactRegex(str: string | string[], flags?: string): RegExp;
function prefixRegex(str: string | string[], flags?: string): RegExp;
function suffixRegex(str: string | string[], flags?: string): RegExp;Core type definitions used across the API:
import type { BaseNode } from 'estree';
type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null;
interface AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };
addDeclaration(node: BaseNode, isBlockDeclaration: boolean, isVar: boolean): void;
contains(name: string): boolean;
}
interface DataToEsmOptions {
compact?: boolean;
includeArbitraryNames?: boolean;
indent?: string;
namedExports?: boolean;
objectShorthand?: boolean;
preferConst?: boolean;
}