Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

strip-comments

jonschlinkert18.7mMIT2.0.1TypeScript support: definitely-typed

Strip line and/or block comments from a string. Blazing fast, and works with JavaScript, Sass, CSS, Less.js, and a number of other languages.

ada comments, apl comments, applescript comments, block comment, block, block-comment, c comments, code comment, comment, comments, csharp comments, css comments, css, hashbang comments, haskell comments, html comments, java comments, javascript comments, javascript, js, less comments, less css, less, less.js, lessjs, line comment, line comments, line, line-comment, line-comments, lua comments, matlab comments, ocaml comments, pascal comments, perl comments, php comments, python comments, remove, ruby comments, sass comments, sass, shebang comments, sql comments, strip, swift comments, typscript comments, xml comments

readme

strip-comments Donate NPM version NPM monthly downloads NPM total downloads Build Status

Strip line and/or block comments from a string. Blazing fast, and works with JavaScript, Sass, CSS, Less.js, and a number of other languages.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

(TOC generated by verb using markdown-toc)

Install

Install with npm (requires Node.js >=10):

$ npm install --save strip-comments

What does this do?

Takes a string and returns a new string with comments removed. Works with line comments and/or block comments. Optionally removes the first comment only or ignores protected comments.

Works with:

  • ada
  • apl
  • applescript
  • c
  • csharp
  • css
  • hashbang
  • haskell
  • html
  • java
  • javascript
  • less
  • lua
  • matlab
  • ocaml
  • pascal
  • perl
  • php
  • python
  • ruby
  • sass
  • shebang
  • sql
  • swift
  • typscript
  • xml

Usage

By default all comments are stripped.

const strip = require('strip-comments');
const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
console.log(str);
// => 'const foo = "bar";\n'

For more use-cases see the tests

API

strip

Strip all code comments from the given input, including protected comments that start with !, unless disabled by setting options.keepProtected to true.

Params

  • input {String}: string from which to strip comments
  • options {Object}: optional options, passed to extract-comments

  • line {Boolean}: if false strip only block comments, default true

  • block {Boolean}: if false strip only line comments, default true
  • keepProtected {Boolean}: Keep ignored comments (e.g. /*! and //!)
  • preserveNewlines {Boolean}: Preserve newlines after comments are stripped
  • returns {String}: modified input

Example

const str = strip('const foo = "bar";// this is a comment\n /* me too */');
console.log(str);
// => 'const foo = "bar";'

.block

Strip only block comments.

Params

  • input {String}: string from which to strip comments
  • options {Object}: pass opts.keepProtected: true to keep ignored comments (e.g. /*!)
  • returns {String}: modified string

Example

const strip = require('..');
const str = strip.block('const foo = "bar";// this is a comment\n /* me too */');
console.log(str);
// => 'const foo = "bar";// this is a comment'

.line

Strip only line comments.

Params

  • input {String}: string from which to strip comments
  • options {Object}: pass opts.keepProtected: true to keep ignored comments (e.g. //!)
  • returns {String}: modified string

Example

const str = strip.line('const foo = "bar";// this is a comment\n /* me too */');
console.log(str);
// => 'const foo = "bar";\n/* me too */'

.first

Strip the first comment from the given input. Or, if opts.keepProtected is true, the first non-protected comment will be stripped.

Params

  • input {String}
  • options {Object}: pass opts.keepProtected: true to keep comments with !
  • returns {String}

Example

const output = strip.first(input, { keepProtected: true });
console.log(output);
// => '//! first comment\nfoo; '

.block

Parses a string and returns a basic CST (Concrete Syntax Tree).

Params

  • input {String}: string from which to strip comments
  • options {Object}: pass opts.keepProtected: true to keep ignored comments (e.g. /*!)
  • returns {String}: modified string

Example

const strip = require('..');
const str = strip.block('const foo = "bar";// this is a comment\n /* me too */');
console.log(str);
// => 'const foo = "bar";// this is a comment'

About

<summary>Contributing</summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

<summary>Running Tests</summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
<summary>Building docs</summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Contributors

Commits Contributor
82 jonschlinkert
4 tunnckoCore
2 mk-pmb
1 kgryte
1 briandipalma
1 epicoxymoron
1 XuluWarrior

Author

Jon Schlinkert

License

Copyright © 2019, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on November 13, 2019.

changelog

Release history

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

<summary>Guiding Principles</summary>
  • Changelogs are for humans, not machines.
  • There should be an entry for every single version.
  • The same types of changes should be grouped.
  • Versions and sections should be linkable.
  • The latest version comes first.
  • The release date of each versions is displayed.
  • Mention whether you follow Semantic Versioning.
<summary>Types of changes</summary>

Changelog entries are classified using the following labels (from keep-a-changelog):

  • Added for new features.
  • Changed for changes in existing functionality.
  • Deprecated for soon-to-be removed features.
  • Removed for now removed features.
  • Fixed for any bug fixes.
  • Security in case of vulnerabilities.

[2.0.0] - 2019-09-14

Changed

  • Refactored again to use a custom parser that is much faster and supports a number of different languages.

1.0.2 - 2018-05-03

  • refactored
  • Remove default objectRestSpread plugin #40 allows for user configured plugins
  • Merge pull request #42 from tallarium/remove-default-transform
  • Merge remote-tracking branch 'origin/master' into refactor
  • Merge remote-tracking branch 'origin/master' into refactor
  • Merge pull request #44 from jonschlinkert/refactor

1.0.1 - 2018-03-24

  • feat: Allow users to specify their own babylon plugins
  • Merge pull request #40 from briandipalma/patch-1
  • adds allowReturnOutsideFunction to defaults

1.0.0 - 2018-03-24

  • adds object-rest-spread plugin

0.4.4 - 2016-02-14

  • refactor to use babylon

0.4.3 - 2015-12-25

  • minor tweak

0.4.2 - 2015-12-11

0.4.0 - 2015-11-04

0.3.4 - 2015-10-22

  • refactored
  • expose first method. code comments, minor formatting
  • allow line/block to be specified as options
  • Merge pull request #23 from jonschlinkert/dev
  • fixes examples
  • adds editorconfig

0.3.0 - 2014-09-02

  • merge fix from origin
  • Merge pull request #13 from mk-pmb/tests_literals_nocomment
  • Merge pull request #15 from mk-pmb/tests_literals_snake
  • Merge pull request #22 from kgryte/patch-1
  • Merge pull request #9 from tunnckoCore/master
  • Merge remote-tracking branch 'origin/line-comments'
  • Merge remote-tracking branch 'origin/master'
  • tests: not a comment: snake-y ASCII art
  • tests: string and regexp literals
  • adds tests for URLs
  • fix globstars
  • lint

0.2.0 - 2014-08-10

  • fixes use strict statement

0.1.6 - 2014-02-13

[0.1.0] - 2014-02-10

  • first commit