Home

Awesome

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

whitespace

Type: boolean
Default: true

Replace comments with whitespace instead of stripping them entirely.

Benchmark

$ npm run bench

Related