├── .gitignore ├── .travis.yml ├── test ├── fixtures │ ├── source.css │ ├── source.expected.css │ ├── filter.css │ ├── filter.expected.css │ ├── ignore.css │ └── ignore.expected.css └── test.js ├── CHANGELOG.md ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | - "iojs" 5 | -------------------------------------------------------------------------------- /test/fixtures/source.css: -------------------------------------------------------------------------------- 1 | .my-class { 2 | color: green; 3 | } 4 | 5 | .my-class .with-another-class { 6 | box-sizing: border-box; 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/source.expected.css: -------------------------------------------------------------------------------- 1 | .prfx-my-class { 2 | color: green; 3 | } 4 | 5 | .prfx-my-class .prfx-with-another-class { 6 | box-sizing: border-box; 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/filter.css: -------------------------------------------------------------------------------- 1 | .Component { 2 | color: green; 3 | } 4 | 5 | .Component.is-disabled .Subcomponent.is-done { 6 | box-sizing: border-box; 7 | } 8 | 9 | .Something > .Nothing { 10 | color: white; 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/filter.expected.css: -------------------------------------------------------------------------------- 1 | .prfx-Component { 2 | color: green; 3 | } 4 | 5 | .prfx-Component.is-disabled .prfx-Subcomponent.is-done { 6 | box-sizing: border-box; 7 | } 8 | 9 | .prfx-Something > .prfx-Nothing { 10 | color: white; 11 | } 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | === HEAD 2 | 3 | === 0.2.0 (May 31, 2015) 4 | 5 | Uses `postcss.plugin`. 6 | Adds support and tests for passing an array to the `ignore` option. 7 | 8 | === 0.1.0 (March 17, 2015) 9 | 10 | Initial release. Ported from rework-class-prefix. 11 | -------------------------------------------------------------------------------- /test/fixtures/ignore.css: -------------------------------------------------------------------------------- 1 | .Component .s-ignore { 2 | color: red; 3 | } 4 | 5 | .Component .s-also-ignore { 6 | font-size: 16px; 7 | } 8 | 9 | .Component .j-do-not-ignore { 10 | margin: 0; 11 | } 12 | 13 | .BoxWide, .BoxSmall { 14 | margin: 0; 15 | } 16 | 17 | .no-flexbox .Component { 18 | display: block; 19 | } 20 | 21 | .no-fontface .Component { 22 | font-size: 12px; 23 | } 24 | -------------------------------------------------------------------------------- /test/fixtures/ignore.expected.css: -------------------------------------------------------------------------------- 1 | .prfx-Component .s-ignore { 2 | color: red; 3 | } 4 | 5 | .prfx-Component .s-also-ignore { 6 | font-size: 16px; 7 | } 8 | 9 | .prfx-Component .prfx-j-do-not-ignore { 10 | margin: 0; 11 | } 12 | 13 | .BoxWide, .BoxSmall { 14 | margin: 0; 15 | } 16 | 17 | .no-flexbox .prfx-Component { 18 | display: block; 19 | } 20 | 21 | .no-fontface .prfx-Component { 22 | font-size: 12px; 23 | } 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-class-prefix", 3 | "version": "0.3.0", 4 | "description": "A class prefixer/namespacer for postcss", 5 | "files": [ 6 | "index.js" 7 | ], 8 | "dependencies": { 9 | "postcss": "^5.0.4" 10 | }, 11 | "devDependencies": { 12 | "mocha": "^2.0.0" 13 | }, 14 | "scripts": { 15 | "test": "mocha" 16 | }, 17 | "license": "MIT", 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/thompsongl/postcss-class-prefix.git" 21 | }, 22 | "keywords": [ 23 | "css", 24 | "postcss", 25 | "postcss-plugin" 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Greg Thompson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var fs = require('fs'); 3 | var postcss = require('postcss'); 4 | var classPrfx = require('..'); 5 | 6 | function fixture(name) { 7 | return fs.readFileSync('test/fixtures/' + name, 'utf8').trim(); 8 | } 9 | 10 | describe('postcss-class-prefix', function() { 11 | it('prefixes all classes', function() { 12 | var output = postcss() 13 | .use(classPrfx('prfx-')) 14 | .process(fixture('source.css')).css; 15 | var expected = fixture('source.expected.css'); 16 | 17 | assert.equal(output, expected); 18 | }); 19 | 20 | it('ignores a classes given in `ignore`', function() { 21 | var output = postcss() 22 | .use(classPrfx('prfx-', { ignore: /^is-/ })) 23 | .process(fixture('filter.css')).css; 24 | var expected = fixture('filter.expected.css'); 25 | 26 | assert.equal(output, expected); 27 | }); 28 | 29 | it('allows an array of ignores as regex or string', function() { 30 | var output = postcss() 31 | .use(classPrfx('prfx-', { 32 | ignore: [ 33 | /^s-[a-z-]+/, 34 | 'no-flexbox', 35 | 'no-fontface', 36 | /^Box[a-zA-Z]+/ 37 | ] 38 | })) 39 | .process(fixture('ignore.css')).css; 40 | var expected = fixture('ignore.expected.css'); 41 | 42 | assert.equal(output, expected); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var postcss = require('postcss'); 3 | 4 | module.exports = postcss.plugin('postcss-class-prefix', classPrefix); 5 | 6 | function classPrefix(prefix, options) { 7 | options = options || {}; 8 | 9 | return function(root) { 10 | 11 | root.walkRules(function (rule) { 12 | if (!rule.selectors){ 13 | return rule; 14 | } 15 | 16 | rule.selectors = rule.selectors.map(function(selector) { 17 | if (!isClassSelector(selector)) { 18 | return selector; 19 | } 20 | 21 | var classes = selector.split('.'); 22 | 23 | return classes.map(function(clss){ 24 | if (classMatchesTest(clss, options.ignore) || clss.trim().length === 0) { 25 | return clss; 26 | } 27 | return prefix + clss; 28 | }).join('.'); 29 | }); 30 | }); 31 | }; 32 | } 33 | 34 | /** 35 | * Determine if class passes test 36 | * 37 | * @param {string} clss 38 | * @param {string} test 39 | */ 40 | function classMatchesTest(clss, test) { 41 | if (!test) { 42 | return false; 43 | } 44 | 45 | clss = clss.trim(); 46 | 47 | if (test instanceof RegExp) { 48 | return test.exec(clss); 49 | } 50 | 51 | if (Array.isArray(test)) { 52 | // Reassign arguments 53 | var tests = test; 54 | test = undefined; 55 | 56 | return tests.some(function(test) { 57 | if (test instanceof RegExp) { 58 | return test.exec(clss); 59 | } else { 60 | return clss === test; 61 | } 62 | }); 63 | } 64 | 65 | return clss === test; 66 | } 67 | 68 | /** 69 | * Determine if selector is a class 70 | * 71 | * @param {string} selector 72 | */ 73 | function isClassSelector(selector) { 74 | return selector.indexOf('.') === 0; 75 | } 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postcss-class-prefix [![Build Status](https://secure.travis-ci.org/thompsongl/postcss-class-prefix.png?branch=master)](http://travis-ci.org/thompsongl/postcss-class-prefix) 2 | ___ 3 | 4 | ## ⚠️ Repo no longer actively maintained 5 | Please look to [postcss-prefixer](https://github.com/marceloucker/postcss-prefixer) or [postcss-slds-prefix](https://github.com/salesforce-ux/postcss-slds-prefix) for similar functionallity in active repos. 6 | 7 | ___ 8 | 9 | A [PostCSS](https://github.com/postcss/postcss) plugin to prefix/namespace classes. 10 | 11 | Avoid collisions with other libraries/stylesheets by prefixing your components with a namespace. 12 | 13 | __Example input__ 14 | 15 | ```css 16 | .Component { /* ... */ } 17 | .Component--modifier { /* ... */ } 18 | .Component-descendent { /* ... */ } 19 | ``` 20 | 21 | __Example output__ 22 | `classPrefix('pfx-')` 23 | ```css 24 | .pfx-Component { /* ... */ } 25 | .pfx-Component--modifier { /* ... */ } 26 | .pfx-Component-descendent { /* ... */ } 27 | ``` 28 | 29 | 30 | ## Installation 31 | 32 | ``` 33 | npm install postcss-class-prefix 34 | ``` 35 | 36 | ## Usage 37 | 38 | ```javascript 39 | var fs = require('fs'); 40 | var postcss = require('postcss'); 41 | var classPrfx = require('postcss-class-prefix'); 42 | 43 | var css = fs.readFileSync('css/my-file.css', 'utf8').toString(); 44 | var out = postcss() 45 | .use(classPrfx('my-prefix-')) 46 | .process(css); 47 | ``` 48 | 49 | ### Using the `ignore` option 50 | 51 | ```javascript 52 | var fs = require('fs'); 53 | var postcss = require('postcss'); 54 | var classPrfx = require('postcss-class-prefix'); 55 | 56 | var css = fs.readFileSync('css/my-file.css', 'utf8').toString(); 57 | var out = postcss() 58 | .use(classPrfx('my-prefix-', { ignore: [/ng-/, 'some-class-to-ignore']})) 59 | .process(css); 60 | ``` 61 | 62 | ## License 63 | 64 | MIT 65 | 66 | ## Acknowledgements 67 | 68 | * Based on [rework-class-prefix](https://github.com/jnv/rework-class-prefix) ([originally](https://github.com/johnotander/rework-class-prefix)) 69 | --------------------------------------------------------------------------------