strip-css-comments
Advanced tools
+57
| declare namespace stripCssComments { | ||
| interface Options { | ||
| /** | ||
| - `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. | ||
| @default true | ||
| */ | ||
| readonly preserve?: boolean | RegExp | ((comment: string) => boolean); | ||
| } | ||
| } | ||
| /** | ||
| Strip comments from CSS. | ||
| @param cssString - String with CSS. | ||
| @example | ||
| ``` | ||
| import stripCssComments = require('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; }' | ||
| ``` | ||
| */ | ||
| declare function stripCssComments( | ||
| cssString: string, | ||
| options?: stripCssComments.Options | ||
| ): string; | ||
| export = stripCssComments; |
+37
-41
| 'use strict'; | ||
| var isRegExp = require('is-regexp'); | ||
| const isRegExp = require('is-regexp'); | ||
| module.exports = function (str, opts) { | ||
| str = str.toString(); | ||
| opts = opts || {}; | ||
| module.exports = (cssString, options = {}) => { | ||
| let preserveImportant = !(options.preserve === false || options.all === true); | ||
| var preserveFilter; | ||
| var comment = ''; | ||
| var currentChar = ''; | ||
| var insideString = false; | ||
| var preserveImportant = !(opts.preserve === false || opts.all === true); | ||
| var ret = ''; | ||
| if (typeof opts.preserve === 'function') { | ||
| let preserveFilter; | ||
| if (typeof options.preserve === 'function') { | ||
| preserveImportant = false; | ||
| preserveFilter = opts.preserve; | ||
| } else if (isRegExp(opts.preserve)) { | ||
| preserveFilter = options.preserve; | ||
| } else if (isRegExp(options.preserve)) { | ||
| preserveImportant = false; | ||
| preserveFilter = function (comment) { | ||
| return opts.preserve.test(comment); | ||
| }; | ||
| preserveFilter = comment => options.preserve.test(comment); | ||
| } | ||
| for (var i = 0; i < str.length; i++) { | ||
| currentChar = str[i]; | ||
| let isInsideString = false; | ||
| let currentCharacter = ''; | ||
| let comment = ''; | ||
| let returnValue = ''; | ||
| if (str[i - 1] !== '\\') { | ||
| if (currentChar === '"' || currentChar === '\'') { | ||
| if (insideString === currentChar) { | ||
| insideString = false; | ||
| } else if (!insideString) { | ||
| insideString = currentChar; | ||
| for (let i = 0; i < cssString.length; i++) { | ||
| currentCharacter = cssString[i]; | ||
| if (cssString[i - 1] !== '\\') { | ||
| if (currentCharacter === '"' || currentCharacter === '\'') { | ||
| if (isInsideString === currentCharacter) { | ||
| isInsideString = false; | ||
| } else if (!isInsideString) { | ||
| isInsideString = currentCharacter; | ||
| } | ||
@@ -38,15 +34,15 @@ } | ||
| // find beginning of /* type comment | ||
| if (!insideString && currentChar === '/' && str[i + 1] === '*') { | ||
| // ignore important comment when configured to preserve comments using important syntax: /*! | ||
| if (!(preserveImportant && str[i + 2] === '!')) { | ||
| var j = i + 2; | ||
| // Find beginning of `/*` type comment | ||
| if (!isInsideString && currentCharacter === '/' && cssString[i + 1] === '*') { | ||
| // Ignore important comment when configured to preserve comments using important syntax: /*! | ||
| if (!(preserveImportant && cssString[i + 2] === '!')) { | ||
| let j = i + 2; | ||
| // iterate over comment | ||
| for (; j < str.length; j++) { | ||
| // find end of comment | ||
| if (str[j] === '*' && str[j + 1] === '/') { | ||
| // Iterate over comment | ||
| for (; j < cssString.length; j++) { | ||
| // Find end of comment | ||
| if (cssString[j] === '*' && cssString[j + 1] === '/') { | ||
| if (preserveFilter) { | ||
| // evaluate comment text | ||
| ret = preserveFilter(comment) ? ret + ('/*' + comment + '*/') : ret; | ||
| // Evaluate comment text | ||
| returnValue = preserveFilter(comment) ? returnValue + ('/*' + comment + '*/') : returnValue; | ||
| comment = ''; | ||
@@ -58,9 +54,9 @@ } | ||
| // store comment text to be evaluated by the filter when the end of the comment is reached | ||
| // Store comment text to be evaluated by the filter when the end of the comment is reached | ||
| if (preserveFilter) { | ||
| comment += str[j]; | ||
| comment += cssString[j]; | ||
| } | ||
| } | ||
| // resume iteration over CSS string from the end of the comment | ||
| // Resume iteration over CSS string from the end of the comment | ||
| i = j + 1; | ||
@@ -72,6 +68,6 @@ | ||
| ret += currentChar; | ||
| returnValue += currentCharacter; | ||
| } | ||
| return ret; | ||
| return returnValue; | ||
| }; |
+43
-41
| { | ||
| "name": "strip-css-comments", | ||
| "version": "3.0.0", | ||
| "description": "Strip comments from CSS", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/strip-css-comments", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && node test.js", | ||
| "bench": "matcha bench.js" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "keywords": [ | ||
| "css", | ||
| "style", | ||
| "stylesheet", | ||
| "strip", | ||
| "remove", | ||
| "delete", | ||
| "trim", | ||
| "comments", | ||
| "preprocess", | ||
| "transform", | ||
| "string" | ||
| ], | ||
| "dependencies": { | ||
| "is-regexp": "^1.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "ava": "0.0.4", | ||
| "matcha": "^0.6.0", | ||
| "xo": "*" | ||
| } | ||
| "name": "strip-css-comments", | ||
| "version": "4.0.0", | ||
| "description": "Strip comments from CSS", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/strip-css-comments", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava && tsd", | ||
| "bench": "matcha bench.js" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "index.d.ts" | ||
| ], | ||
| "keywords": [ | ||
| "css", | ||
| "style", | ||
| "stylesheet", | ||
| "strip", | ||
| "remove", | ||
| "delete", | ||
| "trim", | ||
| "comments", | ||
| "preprocess", | ||
| "transform", | ||
| "string" | ||
| ], | ||
| "dependencies": { | ||
| "is-regexp": "^2.1.0" | ||
| }, | ||
| "devDependencies": { | ||
| "ava": "^1.4.1", | ||
| "matcha": "^0.7.0", | ||
| "tsd": "^0.7.2", | ||
| "xo": "^0.24.0" | ||
| } | ||
| } |
+18
-16
@@ -5,3 +5,3 @@ # strip-css-comments [](https://travis-ci.org/sindresorhus/strip-css-comments) | ||
| 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. | ||
| 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. | ||
@@ -12,9 +12,12 @@ | ||
| ``` | ||
| $ npm install --save strip-css-comments | ||
| $ npm install strip-css-comments | ||
| ``` | ||
| ## Usage | ||
| ```js | ||
| var stripCssComments = require('strip-css-comments'); | ||
| const stripCssComments = require('strip-css-comments'); | ||
| // by default important comments `/*!` are preserved | ||
| // By default important comments `/*!` are preserved | ||
| stripCssComments('/*! <copyright> */ body { /* unicorns */color: hotpink; }'); | ||
@@ -30,3 +33,3 @@ //=> '/*! <copyright> */ body { color: hotpink; }' | ||
| // preserve comments based on a regex | ||
| // Preserve comments based on a regex | ||
| stripCssComments( | ||
@@ -38,9 +41,7 @@ '/*# preserved */ body { /* unicorns */color: hotpink; }', | ||
| // preserve comments based on the return value of the supplied function | ||
| // Preserve comments based on the return value of the supplied function | ||
| stripCssComments( | ||
| '/*# preserved */ body { /* unicorns */color: hotpink; }', | ||
| { | ||
| preserve: function (comment) { | ||
| return comment.charAt(0) === '#'; | ||
| } | ||
| preserve: comment => comment.charAt(0) === '#' | ||
| } | ||
@@ -54,7 +55,6 @@ ); | ||
| ### stripCssComments(input, [options]) | ||
| ### stripCssComments(cssString, [options]) | ||
| ## input | ||
| ## cssString | ||
| *Required* | ||
| Type: `string` | ||
@@ -66,9 +66,11 @@ | ||
| Type: `object` | ||
| ### preserve | ||
| Type: `boolean`, `RegExp`, `function` | ||
| Type: `boolean | RegExp | Function`<br> | ||
| Default: `true` | ||
| - `true` - Preserve comments that use the `/*! */` syntax | ||
| - `false` - Strip all comments | ||
| - `true` - Preserve important comments `/*! */`. | ||
| - `false` - Strip all comments. | ||
| - `RegExp` - Preserve comments where the comment body matches a regular expression. | ||
@@ -93,2 +95,2 @@ - `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. | ||
| MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
| MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
7729
29.03%5
25%105
72.13%90
2.27%4
33.33%+ Added
- Removed
Updated