Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-cleanup

aMarCruz115kMIT3.2.1TypeScript support: included

Rollup plugin to trim trailing spaces, compact empty lines, and normalize line endings

rollup, rollup-plugin, javascript, es6, modules, comments, removal, cleanup

readme

rollup-plugin-cleanup

npm Version License AppVeyor Status Travis Status Maintainability Coverage

Rollup plugin to remove comments, trim trailing spaces, compact empty lines, and normalize line endings in JavaScript files.

With the rollup-plugin-cleanup you have:

  • Compaction of empty lines within multiline comments and/or out of them.
  • Normalization of line endings to Unix, Mac, or Windows.
  • Removal of JavaScript comments through powerful, configurable filters.
  • Removal of trailing whitespace of each line.
  • TypeScript definitions.
  • Sourcemap support.

From v3.1, this plugin no longer uses acorn. See more in the Whats New section.

rollup-plugin-cleanup requires node v10.13 or above, but avoid the buggy v11.x

Important:

rollup-plugin-cleanup is based on js-cleanup and can handle any JS-like file: TypeScript, Flow, React, ES9, etc, but it is mainly a postprocessor, so it should be runned in a later stage of your toolchain, after any preprocessor or transpiler.

Why not Uglify?

Uglify is a excelent minifier but you have little control over the results, while with js-cleanup your coding style remains intact and the removal of comments is strictly under your control.

Support my Work

I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...

Of course, feedback, PRs, and stars are also welcome 🙃

Thanks!

Install

npm install rollup-plugin-cleanup --save-dev
# or with yarn
yarn add rollup-plugin-cleanup -D

Usage

import { rollup } from 'rollup';
import awesome from 'rollup-plugin-awesome';
import cleanup from 'rollup-plugin-cleanup';

rollup({
  input: 'src/main.js',
  plugins: [
    awesome(),        // other plugins
    cleanup()         // cleanup here
  ]
}).then(...)

That's it.

By default, only the .js, .jsx, and .tag files are processed, but you can expand or restrict the set of accepted files using the options include, exclude, and extensions (see below).

Options

From v3.1.0 normalizeEols is deprecated in favor of lineEndings and the properties ecmaVersion, sourceType, and acornOptions are ignored. See more in Whats New section.

Name Default Description
comments 'some' Filter or array of filters that determinates which comments should be preserved.
Use "all" to keep all, or "none" to remove all the comments.
compactComments true Should js-cleanup also compact whitespace and blank lines in the preserved multiline comments?
Line-ending normalization is always done.
lineEndings unix Type of Line-ending for normalization: "unix", "mac", "win".
maxEmptyLines 0 Maximum successive empty lines to preserve in the output.
Use a positive value, or -1 to keep all the lines.
sourcemap true Should a sourcemap be generated?
extensions ['js', 'jsx', 'mjs'] String or array of strings with extensions of files to process.
exclude (none) picomatch or array of picomatch patterns for paths to exclude of the process.
include (none) picomatch or array of picomatch patterns for paths to include in the process.

Predefined Comment Filters

Instead the special 'all' or 'none' keywords, you can use any combination of custom filters along with any of these predefined ones:

Name Will preserve...
some Comments containing "@license", "@preserve", or starting with "!".
license Comments containing "@license".
eslint ESLint directives.
flow Facebook Flow directives, comment types, and flowlint comments.
istanbul istanbul ignore comments.
jsdoc JSDoc comments.
jshint JSHint directives.
jslint JSLint directives.
sources Sourcemap directives sourceURL and sourceMappingURL.
ts MS TypeScript Triple-Slash and @ts-* directives, plus the @jsx pragma.
ts3s TypeScript Triple-Slash directives.

From v3.1.0, some does not includes '@cc_on' and the jscs filter was deprecated. See more in Whats New section.

'srcmaps' will be preserved as an alias to the 'sources' filter.

See the regexes in the js-cleanup src/predef-filters.ts file.

Custom Filters

You can set custom filters through regexes that matches the content of the comments that you want to preserve.

The string to which the regex is applied does not includes the first slash, nor the */ terminator of the multiline comments, so the multiline comments begins with an asterisk (*) and single-line comments begins with a slash (/).

For example, the following filters will preserve ESLint directives and multiline comments starting with a dash:

const cleanedCode = jsCleanup(code, null, { comments: ['eslint', /^\*-/] })

What's New

Changes in v3.2.1

  • Fixed #16 ? Thanks to @Aqours, @gevalo1 & @xania for repoting this issue
  • Using js-cleanup v1.2.0 and Rollup v2.0+
  • Requires NodeJS v10.14.2 or v12.0 and above
  • Updated dependencies

License

The MIT License (MIT)

© 2018-2020 Alberto Martínez

changelog

rollup-plugin-cleanup changes

[3.2.1] - 2020-09-22

Changed

  • Fixed #16 ? Thanks to @Aqours, @gevalo1 & @xania for repoting this issue
  • Using js-cleanup v1.2.0 and Rollup v2.0 and above
  • Requires NodeJS v10.14.2 or v12.0 and above
  • Updated dependencies

[3.1.1] - 2019-01-18

Changed

  • Update dependencies.

Fixed

  • 15 : Version 3.1.0 fails for certain template literals. Thanks to @stotter for repoting this issue.

[3.1.0] - 2018-12-27

Bye, acorn.

Although acorn is an excellent parser, its use in rollup-plugin-cleanup had caused several issues.

v3.1 removes this dependency and now is completely based on js-cleanup, which does not depend on acorn or another similar parser, with the advantage of a relative independency of the version and "dialect" of JavaScript and a little more efficiency, for being a specialized tool.

While js-cleanup is in its first version, I do not expect it to present major problems (the algorithm and the JS rules used for the replacement are fairly simple).

Added

  • compactComment option to control the compaction of multiline comments, useful to preserve the format of JSDoc blocks.
  • The flow filter for Facebook Flow comments and directives.
  • The ts filter for MS TypeScript directives.
  • TypeScript definitions.
  • Link to kofi.

Changed

  • The options ecmaVersion, sourceType, and acornOptions are ignored, acorn was removed in this version.
  • The normalizeEols option is deprecated in favor of lineEndings, which have the same behavior.
  • The some filter no longer includes @cc_on, but adds comments that begin with '!'.
  • The jscs filter is deprecated, jscs no longer exists.
  • Updated AppVeyor and Travis settings.
  • Updated format of the Changelog.
  • Updated devDependencies.
  • Replaced Jest with mocha, due to issues with coverage.

Fixed

  • Closes #13, acorn was removed in this version.
  • Coverage generation and badge.

Removed

  • Dependency on Jest, due to incompatibilities.
  • Dependency on the acorn parser.

[3.0.0] - 2018-07-06

  • Add an example of using Acorn plugin the README.
  • New option acornOptions, for advanced usage. This is an optional, plain JS object with additional settings passed to the Acorn parser. Properties of this object are merged with, and take precedence over, the existing ecmaVersion and sourceType options.
  • The mimimum node version is now 6.14, for compatibility with ESLint 5.
  • Updated devDependencies.

From v3.0.0-beta.1 (Unreleased)

  • Removed the riot .tag extension from the defaults, you can add this manually.
  • Now acorn ecmaVersion defaults to 9 (ES2018).
  • Closes #10 : Errors out on spread operator.
  • Updated devDependencies the to last acorn and rollup plugins.

[2.0.1] - 2018-04-10

  • Adds rollup >=0.50 as peerDependencies, hope to update devDependencies soon*.
  • Preserves empty lines inside multi-line strings.
  • Welcome to @mkhl to the rollup-plugin-cleanup team!

* MagicString v0.24.x has great enhancements, but it needs testing with this plugin.

[2.0.0] - 2017-10-19

  • Requires node v4.2 or later.
  • Emission of source map honors the Rollup sourceMap or sourcemap (lowercased) options.
  • Generated errors are displayed through the standard Rollup's error method.
  • Fixed tests to match rollup v0.48.x parameters and async operation.
  • Now the plugin operation is async and returns a Promise.
  • Using facebook jest for tests.

[1.0.1] - 2017-06-14

  • New filter "ts3s" to preserve TypeScript Triple-Slash Directives (See NOTE)
  • Closes #5 : cleanup didn't generate a sourcemap for the transformation.
  • Some refactorization to speed up some operations.
  • Updated devDependencies.

NOTE:

TypeScript source must be already compiled to JavaScript.

[1.0.0] - 2017-01-10

  • Improved regex to detect empty lines.
  • Fixed minor bug not processing the input when there's no empty spaces or lines to remove.
  • Fixed tests to match rollup v0.40.x output.
  • Changed default Acorn ecmaVersion from 6 to 7 to allow parsing ES2017 (See rollup#492).
  • Updated devDependencies.
  • 100% test coverage.

[0.1.4] - 2016-09-12

  • Default extensions are changed from "*" to ['.js', '.jsx', '.tag'] to avoid conflicts with other plugins.
  • The extensions option is case-sensitive by consistency with rollup plugins.

[0.1.3] - 2016-09-12

  • The string passed to the comment filters now includes a character preceding the content, "/" for one-line comments, and "*" for multiline comments.
  • Adds note to the README about the usage of cleanup as post-processor - See issue #1
  • Now, the default for extensions is "*". Because rollup is a JavaScript bundler and cleanup is a JavaScript post-processor, it should to work with any file handled by rollup.

[0.1.2] - 2016-09-08

  • Implements support for the removal of comments through configurable filters, using the acorn parser for secure detection.
  • Fix the lint script of npm.

[0.1.1] - 2016-09-06

  • The generated files includes CommonJS & ES6 module versions, already transpiled.
  • Fixes an error when the last empty line does not ends with eol.
  • Fixes errors in the build of previous versions (incomplete dist folder).
  • Fix Travis config, now using ESLint in the test for node 4+
  • Adds automatized test for Windows, with the AppVeyor service.

[0.1.0] - 2016-09-05

First public release.