项目作者: topaxi

项目描述 :
Namespace your CSS selectors using postcss
高级语言: JavaScript
项目地址: git://github.com/topaxi/postcss-selector-namespace.git
创建时间: 2016-01-06T11:51:19Z
项目社区:https://github.com/topaxi/postcss-selector-namespace

开源协议:MIT License

下载


postcss-selector-namespace Build Status Test Coverage Code Climate

Installation

  1. $ npm install postcss-selector-namespace

Usage

  1. var postcss = require('postcss')
  2. var selectorNamespace = require('postcss-selector-namespace')
  3. var output = postcss()
  4. .use(selectorNamespace({ selfSelector: ':--component', namespace: 'my-component' }))
  5. .process(require('fs').readFileSync('input.css', 'utf8'))
  6. .css

input.css

  1. :--component {
  2. color: black;
  3. }
  4. :--component.danger {
  5. color: red;
  6. }
  7. h1, .h1 {
  8. font-weight: bold;
  9. }

will output the following css:

  1. .my-component {
  2. color: black;
  3. }
  4. .my-component.danger {
  5. color: red;
  6. }
  7. .my-component h1, .my-component .h1 {
  8. font-weight: bold;
  9. }

Prepending :root to a selector prevents the selector from being namespaced:

  1. :root h1 {
  2. font-weight: bold;
  3. }

will output the selector without any namespace:

  1. h1 {
  2. font-weight: bold;
  3. }

SCSS support

This plugin can also process scss files and output scss again using the
postcss-scss module.

  1. var postcss = require('postcss')
  2. var postscss = require('postcss-scss')
  3. var selectorNamespace = require('postcss-selector-namespace')
  4. var output = postcss()
  5. .use(selectorNamespace({ selfSelector: '&', namespace: 'my-component' }))
  6. .process(require('fs').readFileSync('input.css', 'utf8'), { syntax: postscss })
  7. .css
  1. $break = 320px;
  2. & {
  3. float: left;
  4. width: 250px;
  5. h1 {
  6. font-weight: bold;
  7. font-size: 32px;
  8. }
  9. @media screen and (max-width: $break-small) {
  10. width: 100px;
  11. float: none;
  12. h1 {
  13. font-size: 24px;
  14. }
  15. }
  16. }

outputs:

  1. $break = 320px;
  2. .my-component {
  3. float: left;
  4. width: 250px;
  5. h1 {
  6. font-weight: bold;
  7. font-size: 32px;
  8. }
  9. @media screen and (max-width: $break-small) {
  10. width: 100px;
  11. float: none;
  12. h1 {
  13. font-size: 24px;
  14. }
  15. }
  16. }

Example setup with postcss-import

Using the excellent plugin
postcss-import,
we can easily namespace each component with its filename.

components/my-button.css

  1. :--namespace {
  2. border: 1px solid #666;
  3. border-radius: 3px;
  4. }

components/my-tabs.css

  1. :--namespace {
  2. display: flex;
  3. }
  4. .tab {
  5. border: 1px solid #666;
  6. border-bottom: none;
  7. border-top-radius: 3px;
  8. margin: 0 5px;
  9. }

main.css

  1. @import 'components/my-button.css';
  2. @import 'components/my-tabs.css';
  3. body {
  4. margin: 0;
  5. color: #333;
  6. }

build.js

  1. const fs = require('fs')
  2. const path = require('path')
  3. const postcss = require('postcss')
  4. const postcssImport = require('postcss-import')
  5. const postcssSelectorNamespace = require('postcss-selector-namespace')
  6. let css = fs.readFileSync('main.css', 'utf8')
  7. function getComponentName(filename) {
  8. if (/components\//.test(filename)) {
  9. return path.basename(filename).replace(/\.css$/, '')
  10. }
  11. return null
  12. }
  13. postcss()
  14. .use(postcssImport({
  15. transform(css, filename, options) {
  16. let componentName = getComponentName(filename)
  17. if (!componentName) {
  18. return css
  19. }
  20. return postcss()
  21. .use(postcssSelectorNamespace({ namespace: '.' + componentName }))
  22. .process(css)
  23. .then(result => result.css)
  24. }
  25. }))
  26. .process(css, { from: 'main.css' })
  27. .then(result => {
  28. console.log(result.css)
  29. })

node build.js outputs:

  1. .my-button {
  2. border: 1px solid #666;
  3. border-radius: 3px;
  4. }
  5. .my-tabs {
  6. display: flex;
  7. }
  8. .my-tabs .tab {
  9. border: 1px solid #666;
  10. border-bottom: none;
  11. border-top-radius: 3px;
  12. margin: 0 5px;
  13. }
  14. body {
  15. margin: 0;
  16. color: #333;
  17. }

Options

namespace

(default: '.self')

The selector to prepend to each selector.

selfSelector

(default: :--namespace)

The selector to use to apply rules directly to your namespace selector.

ignoreRoot

(default: true)

Selector won’t be namespaced if they start with the :root pseudo-class.

rootSelector

(default: :root)

If prefixed with this selector, selectors won’t be namespaced.

dropRoot

(default: true)

If true, the rootSelector will be stripped from the output.


License