[6a3a178] | 1 | # [postcss][postcss]-discard-comments
|
---|
| 2 |
|
---|
| 3 | > Discard comments in your CSS files with PostCSS.
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | ## Install
|
---|
| 7 |
|
---|
| 8 | With [npm](https://npmjs.org/package/postcss-discard-comments) do:
|
---|
| 9 |
|
---|
| 10 | ```
|
---|
| 11 | npm install postcss-discard-comments --save
|
---|
| 12 | ```
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | ## Example
|
---|
| 16 |
|
---|
| 17 | ### Input
|
---|
| 18 |
|
---|
| 19 | ```css
|
---|
| 20 | h1/* heading */{
|
---|
| 21 | margin: 0 auto
|
---|
| 22 | }
|
---|
| 23 | ```
|
---|
| 24 |
|
---|
| 25 | ### Output
|
---|
| 26 |
|
---|
| 27 | ```css
|
---|
| 28 | h1 {
|
---|
| 29 | margin: 0 auto
|
---|
| 30 | }
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | This module discards comments from your CSS files; by default, it will remove
|
---|
| 34 | all regular comments (`/* comment */`) and preserve comments marked as important
|
---|
| 35 | (`/*! important */`).
|
---|
| 36 |
|
---|
| 37 | Note that this module does not handle source map comments because they are not
|
---|
| 38 | available to it; PostCSS handles this internally, so if they are removed then
|
---|
| 39 | you will have to [configure source maps in PostCSS][maps].
|
---|
| 40 |
|
---|
| 41 | [maps]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | ## API
|
---|
| 45 |
|
---|
| 46 | ### comments([options])
|
---|
| 47 |
|
---|
| 48 | #### options
|
---|
| 49 |
|
---|
| 50 | ##### remove(function)
|
---|
| 51 |
|
---|
| 52 | Type: `function`
|
---|
| 53 | Return: `boolean`
|
---|
| 54 | Variable: `comment` contains a comment without `/**/`
|
---|
| 55 |
|
---|
| 56 | For each comment, return true to remove, or false to keep the comment.
|
---|
| 57 |
|
---|
| 58 | ```js
|
---|
| 59 | function(comment) {}
|
---|
| 60 | ```
|
---|
| 61 |
|
---|
| 62 | ```js
|
---|
| 63 | var css = '/* headings *//*@ h1 */h1{margin:0 auto}/*@ h2 */h2{color:red}';
|
---|
| 64 | console.log(postcss(comments({
|
---|
| 65 | remove: function(comment) { return comment[0] == "@"; }
|
---|
| 66 | })).process(css).css);
|
---|
| 67 | //=> /* headings */h1{margin:0 auto}h2{color:red}
|
---|
| 68 | ```
|
---|
| 69 | **NOTE:** If you use the `remove` function other options will not be available.
|
---|
| 70 |
|
---|
| 71 | ##### removeAll
|
---|
| 72 |
|
---|
| 73 | Type: `boolean`
|
---|
| 74 | Default: `false`
|
---|
| 75 |
|
---|
| 76 | Remove all comments marked as important.
|
---|
| 77 |
|
---|
| 78 | ```js
|
---|
| 79 | var css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}';
|
---|
| 80 | console.log(postcss(comments({removeAll: true})).process(css).css);
|
---|
| 81 | //=> h1{margin:0 auto}h2{color:red}
|
---|
| 82 | ```
|
---|
| 83 |
|
---|
| 84 | ##### removeAllButFirst
|
---|
| 85 |
|
---|
| 86 | Type: `boolean`
|
---|
| 87 | Default: `false`
|
---|
| 88 |
|
---|
| 89 | Remove all comments marked as important, but the first one.
|
---|
| 90 |
|
---|
| 91 | ```js
|
---|
| 92 | var css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}';
|
---|
| 93 | console.log(postcss(comments({removeAllButFirst: true})).process(css).css);
|
---|
| 94 | //=> /*! heading */h1{margin:0 auto}h2{color:red}
|
---|
| 95 | ```
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | ## Usage
|
---|
| 99 |
|
---|
| 100 | See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
|
---|
| 101 | examples for your environment.
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | ## Contributors
|
---|
| 105 |
|
---|
| 106 | See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | ## License
|
---|
| 110 |
|
---|
| 111 | MIT © [Ben Briggs](http://beneb.info)
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | [postcss]: https://github.com/postcss/postcss
|
---|