JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5496294
  • Score
    100M100P100Q208298F

Strip comments from code. Remove both line comments and block comments.

Package Exports

  • strip-comments

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (strip-comments) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

strip-comments NPM version

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.

Install

npm

 npm i strip-comments --save

bower

 npm install strip-comments --save

API

Table of contents

strip(str[, opts])

Strip all comments

  • str {String} file content or string to strip to
  • opts {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.block(str[, opts])

Strip only block comments

  • str {String} file content or string to strip to
  • opts {Object} if safe: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.line(str[, opts])

Strip only line comments

  • str {String} file content or string to strip to
  • opts {Object} if safe: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, '') : '';
};

Tests

mocha -R spec

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license