pax_global_header00006660000000000000000000000064126566663350014534gustar00rootroot0000000000000052 comment=e7f580afe8fe456078791bb5a439077aab92531c postcss-discard-comments-2.0.4/000077500000000000000000000000001265666633500164675ustar00rootroot00000000000000postcss-discard-comments-2.0.4/.babelrc000066400000000000000000000002321265666633500200570ustar00rootroot00000000000000{ "presets": ["es2015-loose", "stage-0"], "plugins": ["add-module-exports"], "env": { "development": { "sourceMaps": "inline" } } } postcss-discard-comments-2.0.4/.editorconfig000066400000000000000000000002461265666633500211460ustar00rootroot00000000000000# editorconfig.org root = true [*] indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true postcss-discard-comments-2.0.4/.gitignore000066400000000000000000000000401265666633500204510ustar00rootroot00000000000000dist node_modules npm-debug.log postcss-discard-comments-2.0.4/.travis.yml000066400000000000000000000001031265666633500205720ustar00rootroot00000000000000sudo: false language: node_js node_js: - '5' - '4' - '0.12' postcss-discard-comments-2.0.4/CHANGELOG.md000066400000000000000000000032771265666633500203110ustar00rootroot00000000000000# 2.0.4 * Now compiled with Babel 6. # 2.0.3 * Fixes an issue where comments that were removed from selectors were replaced by a single space. # 2.0.2 * Fixes an integration issue where comments inside values transformed by other processors had their values reset to their original state before the comments were removed. # 2.0.1 * Replaces a dependency on node-balanced with internal comments parser. # 2.0.0 * Upgraded to PostCSS 5 (thanks to @avanes). # 1.2.1 * Improved performance by iterating the AST in a single pass. # 1.2.0 * Adds support for user-directed removal of comments, with the `remove` option (thanks to @dmitrykiselyov). * `removeAllButFirst` now operates on each CSS tree, rather than the first one passed to the plugin. * Fixes to pass the PostCSS plugin guidelines. # 1.1.3 * As PostCSS handles the source map content, there is no need to check for the existence of a '#' at position 0 of the comment. This patch fixes this behaviour. # 1.1.2 * Fixes an issue where comment separated values were being incorrectly transformed to not have spaces separating them instead, in `decl.value`. e.g. `10px/*test*/20px` became `10px20px` in `decl.value` but not `decl._value.raw`. # 1.1.1 * Fixes a bug where non-special comments, with an exclamation mark in any part of the text, were not being removed. # 1.1.0 * Now uses the PostCSS `4.1` plugin API. * Adds support for transforming comments inside `!important` values. # 1.0.2 * Adds a JSHint config, tidy up unnecessary lines of code. # 1.0.1 * Fixed a bug which affected initializing the plugin with no options. * Stopped the plugin from trying to match comments in empty strings. # 1.0.0 * Initial release. postcss-discard-comments-2.0.4/LICENSE-MIT000066400000000000000000000021041265666633500201200ustar00rootroot00000000000000Copyright (c) Ben Briggs (http://beneb.info) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. postcss-discard-comments-2.0.4/README.md000066400000000000000000000053541265666633500177550ustar00rootroot00000000000000# [postcss][postcss]-discard-comments [![Build Status](https://travis-ci.org/ben-eb/postcss-discard-comments.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-discard-comments.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-discard-comments.svg)][deps] > Discard comments in your CSS files with PostCSS. ## Install With [npm](https://npmjs.org/package/postcss-discard-comments) do: ``` npm install postcss-discard-comments --save ``` ## Example ### Input ```css h1/* heading */{ margin: 0 auto } ``` ### Output ```css h1 { margin: 0 auto } ``` This module discards comments from your CSS files; by default, it will remove all regular comments (`/* comment */`) and preserve comments marked as important (`/*! important */`). Note that this module does not handle source map comments because they are not available to it; PostCSS handles this internally, so if they are removed then you will have to [configure source maps in PostCSS][maps]. [maps]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md ## API ### comments([options]) #### options ##### remove(function) Type: `function` Return: `boolean` Variable: `comment` contains a comment without `/**/` For each comment, return true to remove, or false to keep the comment. ```js function(comment) {} ``` ```js var css = '/* headings *//*@ h1 */h1{margin:0 auto}/*@ h2 */h2{color:red}'; console.log(postcss(comments({ remove: function(comment) { return comment[0] == "@"; } })).process(css).css); //=> /* headings */h1{margin:0 auto}h2{color:red} ``` **NOTE:** If you use the `remove` function other options will not be available. ##### removeAll Type: `boolean` Default: `false` Remove all comments marked as important. ```js var css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}'; console.log(postcss(comments({removeAll: true})).process(css).css); //=> h1{margin:0 auto}h2{color:red} ``` ##### removeAllButFirst Type: `boolean` Default: `false` Remove all comments marked as important, but the first one. ```js var css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}'; console.log(postcss(comments({removeAllButFirst: true})).process(css).css); //=> /*! heading */h1{margin:0 auto}h2{color:red} ``` ## Usage See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for examples for your environment. ## Contributing Pull requests are welcome. If you add functionality, then please add unit tests to cover it. ## License MIT © Ben Briggs [ci]: https://travis-ci.org/ben-eb/postcss-discard-comments [deps]: https://gemnasium.com/ben-eb/postcss-discard-comments [npm]: http://badge.fury.io/js/postcss-discard-comments [postcss]: https://github.com/postcss/postcss postcss-discard-comments-2.0.4/package.json000066400000000000000000000024311265666633500207550ustar00rootroot00000000000000{ "name": "postcss-discard-comments", "version": "2.0.4", "description": "Discard comments in your CSS files with PostCSS.", "main": "dist/index.js", "files": [ "dist", "LICENSE-MIT" ], "scripts": { "pretest": "eslint src", "prepublish": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/", "test": "ava src/__tests__" }, "keywords": [ "css", "comments", "postcss", "postcss-plugin" ], "license": "MIT", "devDependencies": { "ava": "^0.11.0", "babel-cli": "^6.5.1", "babel-core": "^6.5.1", "babel-plugin-add-module-exports": "^0.1.2", "babel-preset-es2015": "^6.5.0", "babel-preset-es2015-loose": "^7.0.0", "babel-preset-stage-0": "^6.5.0", "del-cli": "^0.2.0", "eslint": "^1.10.3", "eslint-config-cssnano": "^1.0.0", "postcss-scss": "^0.1.3", "postcss-simple-vars": "^1.2.0" }, "homepage": "https://github.com/ben-eb/postcss-discard-comments", "author": { "name": "Ben Briggs", "email": "beneb.info@gmail.com", "url": "http://beneb.info" }, "repository": "ben-eb/postcss-discard-comments", "dependencies": { "postcss": "^5.0.14" }, "ava": { "require": "babel-core/register" }, "eslintConfig": { "extends": "cssnano" } } postcss-discard-comments-2.0.4/src/000077500000000000000000000000001265666633500172565ustar00rootroot00000000000000postcss-discard-comments-2.0.4/src/__tests__/000077500000000000000000000000001265666633500212145ustar00rootroot00000000000000postcss-discard-comments-2.0.4/src/__tests__/index.js000066400000000000000000000157701265666633500226730ustar00rootroot00000000000000import ava from 'ava'; import postcss from 'postcss'; import plugin from '..'; import {name} from '../../package.json'; import vars from 'postcss-simple-vars'; const tests = [{ message: 'should remove non-special comments', fixture: 'h1{font-weight:700!important/*test comment*/}', expected: 'h1{font-weight:700!important}' }, { message: 'should remove non-special comments 2', fixture: 'h1{/*test comment*/font-weight:700}', expected: 'h1{font-weight:700}' }, { message: 'should remove non-special comments 3', fixture: '/*test comment*/h1{font-weight:700}/*test comment*/', expected: 'h1{font-weight:700}' }, { message: 'should remove non-special comments 4', fixture: 'h1{font-weight:/*test comment*/700}', expected: 'h1{font-weight:700}' }, { message: 'should remove non-special comments 5', fixture: 'h1{margin:10px/*test*/20px}', expected: 'h1{margin:10px 20px}' }, { message: 'should remove non-special comments 6', fixture: 'h1{margin:10px /*test*/ 20px /*test*/ 30px /*test*/ 40px}', expected: 'h1{margin:10px 20px 30px 40px}' }, { message: 'should remove non-special comments 7', fixture: '/*comment*/*/*comment*/{margin:10px}', expected:'*{margin:10px}' }, { message: 'should remove non-special comments 8', fixture: 'h1,/*comment*/ h2, h3/*comment*/{margin:20px}', expected: 'h1, h2, h3{margin:20px}' }, { message: 'should remove non-special comments 9', fixture: '@keyframes /*test*/ fade{0%{opacity:0}to{opacity:1}}', expected: '@keyframes fade{0%{opacity:0}to{opacity:1}}' }, { message: 'should remove non-special comments 10', fixture: '@media only screen /*desktop*/ and (min-width:900px){body{margin:0 auto}}', expected: '@media only screen and (min-width:900px){body{margin:0 auto}}' }, { message: 'should remove non-special comments 11', fixture: '@media only screen and (min-width:900px)/*test*/{body{margin:0 auto}}', expected: '@media only screen and (min-width:900px){body{margin:0 auto}}', }, { message: 'should remove non-special comments 12', fixture: 'h1{margin/*test*/:20px}', expected: 'h1{margin:20px}' }, { message: 'should remove non-special comments 13', fixture: 'h1{margin:20px! /* test */ important}', expected: 'h1{margin:20px!important}' }, { message: 'should keep special comments', fixture: 'h1{font-weight:700!important/*!test comment*/}', expected: 'h1{font-weight:700!important/*!test comment*/}' }, { message: 'should keep special comments 2', fixture: 'h1{/*!test comment*/font-weight:700}', expected: 'h1{/*!test comment*/font-weight:700}' }, { message: 'should keep special comments 3', fixture: '/*!test comment*/h1{font-weight:700}/*!test comment*/', expected: '/*!test comment*/h1{font-weight:700}/*!test comment*/' }, { message: 'should keep special comments 4', fixture: 'h1{font-weight:/*!test comment*/700}', expected: 'h1{font-weight:/*!test comment*/700}' }, { message: 'should keep special comments 5', fixture: 'h1{margin:10px/*!test*/20px}', expected: 'h1{margin:10px/*!test*/20px}' }, { message: 'should keep special comments 6', fixture: 'h1{margin:10px /*!test*/ 20px /*!test*/ 30px /*!test*/ 40px}', expected: 'h1{margin:10px /*!test*/ 20px /*!test*/ 30px /*!test*/ 40px}' }, { message: 'should keep special comments 7', fixture: '/*!comment*/*/*!comment*/{margin:10px}', expected:'/*!comment*/*/*!comment*/{margin:10px}' }, { message: 'should keep special comments 8', fixture: 'h1,/*!comment*/h2,h3/*!comment*/{margin:20px}', expected: 'h1,/*!comment*/h2,h3/*!comment*/{margin:20px}' }, { message: 'should keep special comments 9', fixture: '@keyframes /*!test*/ fade{0%{opacity:0}to{opacity:1}}', expected: '@keyframes /*!test*/ fade{0%{opacity:0}to{opacity:1}}' }, { message: 'should keep special comments 10', fixture: '@media only screen /*!desktop*/ and (min-width:900px){body{margin:0 auto}}', expected: '@media only screen /*!desktop*/ and (min-width:900px){body{margin:0 auto}}' }, { message: 'should keep special comments 11', fixture: '@media only screen and (min-width:900px)/*!test*/{body{margin:0 auto}}', expected: '@media only screen and (min-width:900px)/*!test*/{body{margin:0 auto}}', }, { message: 'should keep special comments 12', fixture: 'h1{margin/*!test*/:20px}', expected: 'h1{margin/*!test*/:20px}' }, { message: 'should keep special comments 13', fixture: 'h1{margin:20px! /*! test */ important}', expected: 'h1{margin:20px! /*! test */ important}' }, { message: 'should remove comments marked as @ but keep other', fixture: '/* keep *//*@ remove */h1{color:#000;/*@ remove */font-weight:700}', expected: '/* keep */h1{color:#000;font-weight:700}', options: {remove: comment => comment[0] === "@"} }, { message: 'should remove all important comments, with a flag', fixture: '/*!license*/h1{font-weight:700}/*!license 2*/h2{color:#000}', expected: 'h1{font-weight:700}h2{color:#000}', options: {removeAll: true} }, { message: 'should remove all important comments but the first, with a flag', fixture: '/*!license*/h1{font-weight:700}/*!license 2*/h2{color:#000}', expected: '/*!license*/h1{font-weight:700}h2{color:#000}', options: {removeAllButFirst: true} }, { message: 'should remove non-special comments that have exclamation marks', fixture: '/* This makes a heading black! Wow! */h1{color:#000}', expected: 'h1{color:#000}' }, { message: 'should handle space appropriately in selectors', fixture: '.h/* ... */1{color:#000}', expected: '.h1{color:#000}' }, { message: 'should handle space appropriately in properties', fixture: 'h1{co/* ... */lor:#000}', expected: 'h1{color:#000}' }, { message: 'should remove block comments', fixture: '/*\n\n# Pagination\n\n...\n\n*/.pagination{color:#000}', expected: '.pagination{color:#000}' }, { message: 'should pass through when it doesn\'t find a comment', fixture: 'h1{color:#000;font-weight:700}', expected: 'h1{color:#000;font-weight:700}' }]; tests.forEach(test => { ava(test.message, t => { const out = postcss(plugin(test.options || {})).process(test.fixture); t.same(out.css, test.expected); }); }); ava('should use the postcss plugin api', t => { t.ok(plugin().postcssVersion, 'should be able to access version'); t.same(plugin().postcssPlugin, name, 'should be able to access name'); }); ava('should work with single line comments', t => { const css = '//!wow\n//wow\nh1{//color:red\n}'; const res = postcss(plugin).process(css, {syntax: require('postcss-scss')}).css; t.same(res, '//!wow\nh1{\n}'); }); ava('should handle comments from other plugins', t => { const css = '$color: red; :root { box-shadow: inset 0 -10px 12px 0 $color, /* some comment */ inset 0 0 5px 0 $color; }'; const res = postcss([ vars(), plugin]).process(css).css; t.same(res, ':root{ box-shadow:inset 0 -10px 12px 0 red, inset 0 0 5px 0 red; }'); }); postcss-discard-comments-2.0.4/src/index.js000066400000000000000000000054251265666633500207310ustar00rootroot00000000000000import CommentRemover from './lib/commentRemover'; import commentParser from './lib/commentParser'; import {plugin, list} from 'postcss'; const space = list.space; export default plugin('postcss-discard-comments', (opts = {}) => { const remover = new CommentRemover(opts); function matchesComments (source) { return commentParser(source).filter(node => node.type === 'comment'); } function replaceComments (source, separator = ' ') { if (!source) { return source; } const parsed = commentParser(source).reduce((value, node) => { if (node.type !== 'comment') { return value + node.value; } if (remover.canRemove(node.value)) { return value + separator; } return value + '/*' + node.value + '*/'; }, ''); return space(parsed).join(' '); } return css => { css.walk(node => { if (node.type === 'comment' && remover.canRemove(node.text)) { node.remove(); return; } if (node.raws.between) { node.raws.between = replaceComments(node.raws.between); } if (node.type === 'decl') { if (node.raws.value && node.raws.value.raw) { if (node.raws.value.value === node.value) { node.value = replaceComments(node.raws.value.raw); } else { node.value = replaceComments(node.value); } node.raws.value = null; } if (node.raws.important) { node.raws.important = replaceComments(node.raws.important); const b = matchesComments(node.raws.important); node.raws.important = b.length ? node.raws.important : '!important'; } return; } if (node.type === 'rule' && node.raws.selector && node.raws.selector.raw) { node.raws.selector.raw = replaceComments(node.raws.selector.raw, ''); return; } if (node.type === 'atrule') { if (node.raws.afterName) { const commentsReplaced = replaceComments(node.raws.afterName); if (!commentsReplaced.length) { node.raws.afterName = commentsReplaced + ' '; } else { node.raws.afterName = ' ' + commentsReplaced + ' '; } } if (node.raws.params && node.raws.params.raw) { node.raws.params.raw = replaceComments(node.raws.params.raw); } } }); }; }); postcss-discard-comments-2.0.4/src/lib/000077500000000000000000000000001265666633500200245ustar00rootroot00000000000000postcss-discard-comments-2.0.4/src/lib/commentParser.js000066400000000000000000000015611265666633500232040ustar00rootroot00000000000000export default function commentParser (input) { const tokens = []; const length = input.length; let pos = 0; let next; while (pos < length) { next = input.indexOf('/*', pos); if (~next) { tokens.push({ type: 'other', value: input.slice(pos, next) }); pos = next; next = input.indexOf('*/', pos + 2); if (!~next) { throw new Error('postcss-discard-comments: Unclosed */'); } tokens.push({ type: 'comment', value: input.slice(pos + 2, next) }); pos = next + 2; } else { tokens.push({ type: 'other', value: input.slice(pos) }); pos = length; } } return tokens; }; postcss-discard-comments-2.0.4/src/lib/commentRemover.js000066400000000000000000000012361265666633500233660ustar00rootroot00000000000000function CommentRemover (options) { this.options = options; } CommentRemover.prototype.canRemove = function (comment) { const remove = this.options.remove; if (remove) { return remove(comment); } else { const isImportant = comment.indexOf('!') === 0; if (!isImportant) { return true; } else if (isImportant) { if (this.options.removeAll || this._hasFirst) { return true; } else if (this.options.removeAllButFirst && !this._hasFirst) { this._hasFirst = true; return false; } } } }; export default CommentRemover;