Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/strip-css-comments
Strip comments from CSS
https://github.com/sindresorhus/strip-css-comments
Last synced: 20 days ago
JSON representation
Strip comments from CSS
- Host: GitHub
- URL: https://github.com/sindresorhus/strip-css-comments
- Owner: sindresorhus
- License: mit
- Created: 2014-10-22T22:28:35.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2023-05-29T13:56:57.000Z (over 1 year ago)
- Last Synced: 2024-10-29T22:38:31.982Z (about 1 month ago)
- Language: CSS
- Size: 54.7 KB
- Stars: 121
- Watchers: 8
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - strip-css-comments - CSS 去除注释 (包 / 解析)
- awesome-nodejs - strip-css-comments - Strip comments from CSS. ![](https://img.shields.io/github/stars/sindresorhus/strip-css-comments.svg?style=social&label=Star) (Repository / Minifiers)
- awesome-nodejs-cn - strip-css-comments - **star:121** 去掉CSS中的注释 (包 / 解析)
- awesome-nodejs - strip-css-comments - Strip comments from CSS. (Packages / Parsing)
- awesome-nodejs - strip-css-comments - Strip comments from CSS - ★ 83 (Parsing)
- awesome-node - strip-css-comments - Strip comments from CSS. (Packages / Parsing)
- awesome-nodejs-cn - strip-css-comments - CSS注释剔除工具. (目录 / 解析工具)
README
# strip-css-comments
> Strip comments from CSS
Also available as a [Gulp](https://github.com/sindresorhus/gulp-strip-css-comments)/[Grunt](https://github.com/sindresorhus/grunt-strip-css-comments)/[Broccoli](https://github.com/sindresorhus/broccoli-strip-css-comments) plugin.
## Usage
```
$ npm install strip-css-comments
```## Usage
```js
import stripCssComments from 'strip-css-comments';// By default important comments `/*!` are preserved
stripCssComments('/*! */ body { /* unicorns */color: hotpink; }');
//=> '/*! */ body { color: hotpink; }'// `preserve: false` will strip all comments including `/*!`
stripCssComments(
'/*! */ 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
```## Related
- [strip-css-comments-cli](https://github.com/sindresorhus/strip-css-comments-cli) - CLI for this module
- [strip-json-comments](https://github.com/sindresorhus/strip-json-comments) - Strip comments from JSON