or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-analysis.mdcode-generation.mdfile-processing.mdindex.mdregex-utilities.md
tile.json

tessl/npm-rollup--pluginutils

A set of utility functions commonly used by Rollup plugins

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@rollup/pluginutils@5.3.x

To install, run

npx @tessl/cli install tessl/npm-rollup--pluginutils@5.3.0

index.mddocs/

@rollup/pluginutils

A 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.

Package Information

  • Package Name: @rollup/pluginutils
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @rollup/pluginutils --save-dev
  • Requirements: Node.js v14.0.0+ and Rollup v1.20.0+

Core Imports

import { 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";

Basic Usage

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 };
    }
  };
}

Architecture

@rollup/pluginutils is organized around four main functional areas:

  • File Processing: Path manipulation, extension handling, and include/exclude filtering
  • AST Analysis: Scope tracking and variable name extraction for code analysis
  • Code Generation: Identifier creation and data-to-module transformation
  • Pattern Matching: Regular expression utilities for precise string matching

Each utility is designed to be lightweight, composable, and handle cross-platform concerns automatically.

Capabilities

File Processing

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;

File Processing

AST Analysis

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;
}

AST Analysis

Code Generation

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;
}

Code Generation

Regular Expression Utilities

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;

Regular Expression Utilities

Types

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;
}

Dependencies

  • @types/estree: AST type definitions for JavaScript/TypeScript
  • estree-walker: Tree traversal utilities for AST manipulation
  • picomatch: Advanced glob pattern matching (v4.0.2+)