rollup-utils

raw JSON →
0.0.5 verified Mon Apr 27 auth: no javascript

A set of utility functions for Rollup plugins, forked from @rollup/pluginutils but using path-browserify instead of the Node.js path module to support browser environments. Version 0.0.5 (released on npm, but no release cadence noted). Requires Node >=14.0.0 and Rollup ^1.20.0||^2.0.0||^3.0.0||^4.0.0. Ships TypeScript type definitions. Key differentiators from @rollup/pluginutils: browser compatibility via path-browserify dependency. Includes addExtension, attachScopes, createFilter, dataToEsm, extractAssignedNames, makeLegalIdentifier, and normalizePath functions.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/rollup-utils/dist/index.js from /path/to/index.js not supported.
cause Using CommonJS require() on an ESM-only package.
fix
Switch to import syntax (e.g., import { createFilter } from 'rollup-utils').
error TypeError: (0 , _rollupUtils.createFilter) is not a function
cause Incorrect import style, probably using default import when createFilter is named export.
fix
Use import { createFilter } from 'rollup-utils' instead of import createFilter from 'rollup-utils'.
error Cannot find module 'rollup'
cause rollup is a peer dependency and not installed automatically.
fix
Ensure rollup is installed: npm install rollup --save-dev
breaking Breaking change: package is ESM-only. CommonJS require() will throw an error.
fix Use import syntax instead of require().
gotcha createFilter returns a function that expects a string or unknown id, not an object. Passing an object may silently return false.
fix Ensure you pass a string ID (e.g., the module path).
gotcha addExtension does not add an extension if the filename already has an extension (including '.' only).
fix Check filename before calling addExtension if you want to force extension addition.
gotcha attachScopes requires the AST to already have 'scope' property attached to nodes; it mutates the AST.
fix Call attachScopes once on the AST, then walk and manage scope manually.
npm install rollup-utils
yarn add rollup-utils
pnpm add rollup-utils

Shows how to import createFilter and use it in a Rollup plugin's transform hook to include only specific module IDs.

import { createFilter } from 'rollup-utils';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/index.js',
  output: { dir: 'dist', format: 'es' },
  plugins: [
    resolve(),
    {
      name: 'my-plugin',
      transform(code, id) {
        const filter = createFilter('src/**/*.js', 'node_modules/**');
        if (!filter(id)) return null;
        // Transform only included modules
        return code.replace(/__VERSION__/g, '1.0.0');
      }
    }
  ]
};