1. strip-css-comments
Strip comments from CSS
strip-css-comments
Package: strip-css-comments
Created by: sindresorhus
Last modified: Mon, 27 Jun 2022 01:12:09 GMT
Version: 5.0.0
License: MIT
Downloads: 161,427
Repository: https://github.com/sindresorhus/strip-css-comments

Install

npm install strip-css-comments
yarn add strip-css-comments

strip-css-comments

Strip comments from CSS

Also available as a Gulp/Grunt/Broccoli plugin.

Usage

$ npm install strip-css-comments

Usage

 import stripCssComments from 'strip-css-comments';

// By default important comments `/*!` are preserved
stripCssComments('/*! <copyright> */ body { /* unicorns */color: hotpink; }');
//=> '/*! <copyright> */ body { color: hotpink; }'

// `preserve: false` will strip all comments including `/*!`
stripCssComments(
	'/*! <copyright> */ body { /* unicorns */color: hotpink; }',
	{preserve: false}
);
//=> 'body { color: hotpink; }'

// Preserve comments based on a regex
stripCssComments(
	'/*# preserved */ body { /* unicorns */color: hotpink; }',
	{preserve: /^#/}
);
//=> '/*# preserved */ body { color: hotpink; }'

// Preserve comments based on the return value of the supplied function
stripCssComments(
	'/*# preserved */ body { /* unicorns */color: hotpink; }',
	{
		preserve: comment => comment.charAt(0) === '#'
	}
);
//=> '/*# preserved */ body { color: hotpink; }'

API

stripCssComments(cssString, options?)

cssString

Type: string

String with CSS.

options

Type: object

preserve

Type: boolean | RegExp | Function
Default: true

  • true - Preserve important comments /*! */.
  • false - Strip all comments.
  • RegExp - Preserve comments where the comment body matches a regular expression.
  • Function - Preserve comments for which a function returns true. The function is called on each comment, gets the comment body as the first argument, and is expected to return a boolean of whether to preserve the comment.

whitespace

Type: boolean
Default: true

Replace comments with whitespace instead of stripping them entirely.

Benchmark

$ npm run bench

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code