rollup-plugin-code-raker
v2.1.1
Published
A Rollup plugin that rakes your bundle to remove dead leaves.
Maintainers
Readme
rollup-plugin-code-raker
A Rollup/Vite plugin that rakes your bundle to remove dead leaves.
Features
- Removes leftover comments,
console.*calls anddebuggerstatements from your bundle. - Does not come in the way of treeshaking: remaining function annotations are removed after Rollup/Vite did their magic.
- Fully configurable.
- Comes with sensible defaults for both application and library bundling.
Requirements
This plugin requires Rollup 4 or later or Vite 5 or later.
Usage
Install the plugin using your favorite package manager:
npm install --save-dev rollup-plugin-code-rakerThen import the plugin in your rollup.config.js/vite.config.js:
import codeRaker from 'rollup-plugin-code-raker'
export default {
...
plugins: [
codeRaker(/* options */)
]
}Options
code-raker uses presets to decide what to remove from code.
- The default preset is a "kill'em all" preset that blindly removes all comments (including licensing and documentation comments), all
console.*calls anddebuggerstatements. - The
applicationpreset preserves licensing comments andconsole.log,console.info,console.warnandconsole.errorcalls. - The
librarypreset preserves licensing and documentation comments andconsole.log,console.info,console.warnandconsole.errorcalls.
[!NOTE]
- Licensing comments are block comments that start with
/*!followed by a space or a newline, or documentation comments that contain the@licensetag.- Documentation, or JsDoc comments, are block comments that start with
/**followed by a space or a newline.- Annotations are block comments that contain one of these strings:
#__PURE__,@__PURE__,#__NO_SIDE_EFFECTS__,@__NO_SIDE_EFFECTS__. See https://rollupjs.org/configuration-options/#treeshake-annotations for more info.
All options are optional.
interface Options {
/**
* The name of a preset to use or extend upon.
*
* Default: undefined.
*/
preset?: 'library' | 'application'
/**
* Set to `true` to remove all comments, `false` to remove none,
* or an object to only remove select comments.
*
* Default depends on the selected preset:
* - default: remove all comments.
* - 'library': preserve licensing, JsDoc and annotation comments,
* remove all others.
* - 'application': preserve licensing comments,
* remove all others.
*
* Note that this setting only targets "meaningful" comments;
* simple block comments and line comments are always removed.
*/
comments?: boolean | {
/**
* Whether to remove licensing comments.
*/
licenses?: boolean | ((comment: string) => boolean)
/**
* Whether to remove documentation comments.
*/
docs?: boolean | ((comment: string) => boolean)
/**
* Whether to remove bundler annotations.
*/
annotations?: boolean
}
/**
* Set to `true` to remove all `console` calls, `false` to remove none,
* or a callback or an object to only remove select `console` calls.
*
* Default depends on the selected preset:
* - default: remove all `console.*` calls.
* - 'library': preserve `log`, `info`, `warn` and `error` methods calls,
* remove all others.
* - 'application': preserve `log`, `info`, `warn` and `error` methods calls,
* remove all others.
*/
console?: boolean | ((method: string) => boolean) | {
/**
* An array of console methods names to remove.
*/
include?: string[]
/**
* An array of console methods names to preserve.
*/
exclude?: string[]
}
/**
* Set to `true` to remove `debugger` statements, or `false` to leave them
* in code.
*
* Default: `true` in all presets.
*/
debugger?: boolean
/**
* Set to `true` to remove blank lines, or `false` to leave them in code.
*
* Default: `true` in all presets.
*/
blankLines?: boolean
}License
MIT
