Thanks to visit codestin.com
Credit goes to github.com

Skip to content

aMarCruz/js-cleanup

Repository files navigation

js-cleanup

npm Version License Travis AppVeyor Coverage Maintainability

Smart comment and whitespace cleaner for JavaScript-like files.

With js-cleanup you have:

  • Removal of JavaScript comments through powerful filters (configurable).
  • Normalization of line endings to Unix, Mac, or Windows.
  • Empty lines compactation (configurable).
  • Removal of trailing whitespace, preserving ES6 Template Literal Strings.
  • TypeScript definitions.
  • Sourcemap support.

js-cleanup is not locked to a particular JavaScript dialect and must work in any JS-like file: TypeScript, ES2019, etc, but it is more of a post-processor, so it should run at a later stage of your toolchain, after any preprocessor or transpiler.

js-cleanup requires node v6.14 or above.

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.

Install

$ npm install js-cleanup -D
# or
$ yarn add js-cleanup -D

Usage

jsCleanup(sourceCode: string, options: Options): Result;

Result

The result is a plain JS object of this type:

{
  code: string;
  map?: object | null;
}
Name Description
code The processed code.
map A raw sourcemap object, or null if the text did not change.
Undefined if sourcemap:false.

Options

Type definition:

{
  comments?: string | RegExp,
  compactComments?: boolean,
  maxEmptyLines?: 0,           // use -1 to preserve all the lines
  lineEndings?: `unix`,      // 'mac' | 'unix' | 'win'
  sourcemap?: boolean,
  sourcemapOptions: {
    includeContent?: boolean,
    inlineMap?: boolean,
    hires?: boolean,
  }
}
Name Default Description
comments 'some' Filter or array of filter names and/or regexes.
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.
maxEmptyLines 0 Maximum successive empty lines to preserve in the output.
Use -1 to preserve all the lines
lineEndings 'unix' Line-ending type for normalization (always done).
sourcemap true Should js-cleanup generate a sourcemap?
sourcemapOptions
includeContent false Include the original source text in the sourcemap?
inlineMap false Inline the sourcemap in the processed text?
hires true Should a hi-res sourcemap be generated?

Note:
If you want to keep JSDoc comments, you should also set compactComments: false.
Since the JSDoc presentation depends on empty lines, these should be controlled by you.

Predefined Comment Filters

Instead the special 'all' or 'none' keywords, you can use any combination of this premaded filters:

Name Will preserve...
some Comments containing "@license", "@preserve", or starting with "!".
license Comments containing "@license".
eslint ESLint directives.
flow Flow directives, including flowlint.
istanbul istanbul ignore comments.
jsdoc JSDoc comments.
jshint JSHint directives.
jslint JSLint directives.
sources sourcemap directives (sourceURL and sourceMappingURL).
ts TypeScript directives, including @jsx and triple-slash.

Note:
Since none of this filters is really accurate (js-cleanup is not a parser), they are suitable for the job without introducing greater complexity.

See the regexes in src/predef-filters.ts

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 sourcemap directives and multiline comments starting with a dash:

  jsCleanup(code, { comments: ['sources', /^\*-/] })

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 for your support!

License

The MIT License (MIT)

About

Smart comment and whitespace cleaner for JavaScript-like files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published