rollup-plugin-cleanup

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

Rollup plugin for removing JavaScript comments, compacting empty lines, trimming trailing spaces, and normalizing line endings. Current stable version 3.2.1 supports Rollup >=2.0 and Node.js >=10.14.2 or >=12.0.0. Unlike minifiers like Uglify, it preserves coding style and gives fine-grained control over comment removal via configurable filters. Since v3.1, it no longer depends on acorn, relying instead on js-cleanup. Offers TypeScript definitions, sourcemap support, and works with JS-like files (TS, Flow, React, ES9+).

error Error: The 'cleanup' plugin is not generating a sourcemap
cause The `sourcemap` option is set to `false` by default? Actually default is true, but if Rollup's `sourceMap` option is set to false, the plugin may not output a sourcemap.
fix
Set sourcemap: true in the plugin options and ensure Rollup's output option includes sourcemap: true.
error TypeError: cleanup is not a function
cause Importing the module incorrectly; using named import instead of default import.
fix
Use import cleanup from 'rollup-plugin-cleanup' or const cleanup = require('rollup-plugin-cleanup').
error Error: Cannot find module 'rollup-plugin-cleanup'
cause Package not installed or missing from node_modules.
fix
Run npm install rollup-plugin-cleanup --save-dev or yarn add rollup-plugin-cleanup -D.
breaking v3.0.0 drops the `acornOptions`, `ecmaVersion`, and `sourceType` options; they are ignored if provided. Remove them from your config.
fix Remove `acornOptions`, `ecmaVersion`, and `sourceType` from the plugin options object.
deprecated The `normalizeEols` option is deprecated in v3.1.0 and ignored in v3.2.0. Use `lineEndings` instead.
fix Replace `normalizeEols` with `lineEndings` property.
gotcha By default, the plugin processes only .js, .jsx, and .mjs files. If you process TypeScript or other extensions, you must set the `extensions` option explicitly.
fix Set `extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs']` in the options.
gotcha The plugin is designed as a post-processor and should run after transpilers (e.g., Babel, TypeScript). Running it before may break type annotations or syntax not yet compiled.
fix Place the `cleanup()` plugin after transpiler plugins in the `plugins` array.
gotcha The `comments` option accepts special strings 'all' or 'none', but also filter patterns. Using invalid patterns may cause all comments to be removed unexpectedly.
fix Review documentation for valid comment filters; test with a sample file to verify desired behavior.
breaking v2.0.0 changed plugin operation to async (returns Promise). If you are chaining with synchronous code, you may need to adapt.
fix Ensure your Rollup pipeline handles async plugins correctly (Rollup >=0.48 supports async).
npm install rollup-plugin-cleanup
yarn add rollup-plugin-cleanup
pnpm add rollup-plugin-cleanup

Shows typical usage with Rollup: import cleanup plugin, apply with common options, and bundle.

import cleanup from 'rollup-plugin-cleanup';
import { rollup } from 'rollup';
import nodeResolve from '@rollup/plugin-node-resolve';

(async () => {
  const bundle = await rollup({
    input: 'src/index.js',
    plugins: [
      nodeResolve(),
      cleanup({
        comments: 'some',          // keep some comments (default)
        compactComments: false,    // preserve whitespace in multiline comments
        maxEmptyLines: 1,          // allow at most 1 empty line
        lineEndings: 'unix',       // normalize to Unix line endings
        extensions: ['js', 'jsx'], // only process these file types
        sourcemap: true            // generate sourcemap
      })
    ]
  });
  await bundle.write({ file: 'dist/bundle.js', format: 'esm' });
})();