• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

RnbWd/gulp-strip-comments: Strips comments from code

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

RnbWd/gulp-strip-comments

开源软件地址(OpenSource Url):

https://github.com/RnbWd/gulp-strip-comments

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

gulp-strip-comments

Build Status Coverage Status downloads decomment David

decomment - removes comments from JSON, JavaScript, CSS, HTML, etc.

NPM

Features

  • Removes both single and multi-line comments from JSON, JavaScript and CSS/Text
  • Automatically recognizes HTML and removes all <!-- comments --> from it
  • Does not change layout / formatting of the original document
  • Removes lines that have only comments on them
  • Compatible with CSS3, JSON5 and ECMAScript 6

The library does not support mixed content - HTML with JavaScript or CSS in it. Once the input code is recognized as HTML, only the HTML comments will be removed from it.

Performance

For JSON and JavaScript this library uses [esprima] to guarantee correct processing for regular expressions.

As an example, it can process AngularJS 1.5 Core in under 100ms, which is 1.1MB ~ 30,000 lines of JavaScript.

Install

$ npm install --save-dev gulp-strip-comments

Usage

var gulp = require('gulp');
var strip = require('gulp-strip-comments');

gulp.task('default', function () {
  return gulp.src('template.js')
    .pipe(strip())
    .pipe(gulp.dest('dist'));
});

API

See decomment for examples and more information.

let strip = require('gulp-strip-comments') // == decomment

strip(options)

This method first checks if the code starts with <, as an HTML, and if so, all <!-- comment --> entries are removed, according to the options.

When the code is not recognized as HTML, it is assumed to be either JSON or JavaScript. It is then parsed through [esprima] for ECMAScript 6 compliance, and to extract details about regular expressions.

If [esprima] fails to validate the code, it will throw a parsing error. When successful, this method will remove // and /**/ comments according to the options (see below).

options.safe ⇒ Boolean
  • false (default) - remove all multi-line comments
  • true - keep special multi-line comments that begin with:
  • <!--[if - for conditional comments in HTML
  • /*! - for everything else (other than HTML)
options.ignore ⇒ RegExp | [RegExp,...]

Takes either a single or an array of regular expressions to match against. All matching blocks are then skipped, as well as any comment-like content inside them.

Examples:

  • CSS may contain Base64-encoded strings with comment-like symbols:
  src: url(data:font/woff;base64,d09GRg//ABAAAAAAZ)

You can isolate all url(*) blocks by using:

  {ignore: /url\([\w\s:\/=\-\+;,]*\)/g}
  • If you want to isolate jsDoc blocks (start with /**, followed by a line break, end with */), you can use the following:
{ignore: /\/\*\*\s*\n([^\*]*(\*[^\/])?)*\*\//g}
options.space ⇒ Boolean
  • false (default) - remove comment blocks entirely
  • true - replace comment blocks with white spaces where needed, in order to preserve the original line + column position of every code element.

NOTE: When this option is enabled, option trim is ignored.

options.trim ⇒ Boolean
  • false (default) - do not trim comments
  • true - remove empty lines that follow removed full-line comments

NOTE: This option has no effect when option space is enabled.

strip.text(options)

Unlike the default strip method, it instructs the library that text is not a JSON, JavaScript or HTML, rather a plain text that needs no parsing or validation, only to remove // and /**/ comments from it according to the options.

This method is good for any text file that uses syntax // and /**/ for comments, such as: .CSS, .CPP, .H, etc.

Please note that while the same rules apply for the text blocks ('', "" and ``), you should not use this method for JSON or JavaScript, as it can break your regular expressions.

strip.html(options)

Unlike the default strip method, it instructs the library not to parse or validate the input in any way, rather assume it to be HTML, and remove all <!-- comment --> entries from it according to the options.

strip.getEOL()

Returns End-of-Line string used within the text, based on the occurrence frequency:

  • \n - for Unix-encoded text
  • \r\n - for Windows-encoded text

When impossible to conclude (the same or 0 occurrence), it returns the default End-of-Line for the current OS.


licence




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap