Strip comments from code. Remove both line comments and block comments.
Should work with any language that uses the same syntax, e.g. JavaScript, LESS, SASS/SCSS, CSS.
npm i strip-comments --save
npm install strip-comments --save
Strip all comments
str
{String} file content or string to strip toopts
{Object} options are passed to.block
, and.line
return
{String}
Example:
/*!
* this multiline
* block comment ('top banner')
*/
'use strict';
/**!
* and this multiline
* block comment
*/
var foo = function(/* and these single-line block comments */) {};
/**
* and this multiline
* block comment
*/
var bar = function(/* and these single-line block comments */) {};
// this single-line line comment
var baz = function () {
// this multiline
// line comment
var some = true;
//this
var fafa = true; //and this
// var also = 'that';
var but = 'not'; //! that comment
};
// also this multiline
// line comment
var fun = false;
Source:
var strip = module.exports = function(str, opts) {
return str ? strip.block(strip.line(str, opts), opts) : '';
};
Strip only block comments
str
{String} file content or string to strip toopts
{Object} ifsafe:true
, strip only that not starts with/*!
or/**!
return
{String}
Example:
/**
* this multiline
* block comment
*/
var bar = function(/* and these single-line block comment */) {
/**
* also that comment
*/
var str = 'something'
};
Source:
strip.block = function(str, opts) {
opts = opts || {};
var re = new RegExp(reBlock + reBlockEnd, 'gm');
if(opts.safe) {
re = new RegExp(reBlockIgnore + reBlockEnd, 'gm');
}
return str ? str.replace(re, '') : '';
};
Strip only line comments
str
{String} file content or string to strip toopts
{Object} ifsafe:true
, strip all that not starts with//!
return
{String}
Example:
// this single-line line comment
var baz = function () {
// this multiline
// line comment
var some = true;
//this
var fafa = true; //and this
// var also = 'that';
var but = 'not'; //! that comment
};
Source:
strip.line = function(str, opts) {
opts = opts || {};
var re = new RegExp(reLine, 'gm');
if(opts.safe) {
re = new RegExp(reLineIgnore, 'gm');
}
return str ? str.replace(re, '') : '';
};
mocha -R spec
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license