# postcss-namespace-selector

> Prefix all CSS rules with a selector

Latest version **0.0.3** (published 2019-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install postcss-namespace-selector
pnpm add postcss-namespace-selector
yarn add postcss-namespace-selector
bun add postcss-namespace-selector
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2019-09-08 |
| First published | 2019-09-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | gxlmyacc |
| Maintainers | gxlmyacc |
| Keywords | postcss, namespace, selectors, postcss-plugin, css, selector, plugin |

## Links

- npm: https://www.npmjs.com/package/postcss-namespace-selector
- Repository: https://github.com/gxlmyacc/postcss-namespace-selector
- Issues: https://github.com/gxlmyacc/postcss-namespace-selector/issues
- npm.io page: https://npm.io/package/postcss-namespace-selector

## Dependencies (3)

- [find-up](https://npm.io/package/find-up.md) ^4.1.0
- [postcss](https://npm.io/package/postcss.md) ^7.0.0
- [postcss-cli](https://npm.io/package/postcss-cli.md) ^6.1.3

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2019-09-08
- 0.0.2 — 2019-09-04
- 0.0.1 — 2019-09-03

## README

# postcss-namespace-selector

> Prefix every CSS selector with a custom namespace `.a => .namespace .a`

## Table of Contents

- [Install](#install)
- [Usage with PostCSS](#usage-with-postcss)
- [Usage with Webpack](#usage-with-webpack)
- [Options](#options)
- [Maintainer](#maintainer)
- [Contribute](#contribute)
- [License](#license)

## Install

```console
$ npm install postcss-namespace-selector
```

## Usage with PostCSS

```js
const prefixer = require('postcss-namespace-selector')

// css to be processed
const css = fs.readFileSync("input.css", "utf8")

const out = postcss().use(prefixer({
  prefix: '.some-selector',
  exclude: ['.c'],

  // Optional transform callback for case-by-case overrides
  transform: function (prefix, selector, prefixedSelector) {
    if (selector === 'body') {
      return 'body' + prefix;
    } else {
      return prefixedSelector;
    }
  }
})).process(css).css
```

Using the options above and the CSS below...

```css
body {
  background: red;
}

.a, .b {
  color: aqua;
}

.c {
  color: coral;
}
```

You will get the following output

```css
body.some-selector {
  background: red;
}

.some-selector .a, .some-selector .b {
  color: aqua;
}

.c {
  color: coral;
}
```

## Usage with webpack

Use it like you'd use any other PostCSS plugin. If you also have `autoprefixer` in your webpack config then make sure that `postcss-namespace-selector` is called first. This is needed to avoid running the prefixer twice on both standard selectors and vendor specific ones (ex: `@keyframes` and `@webkit-keyframes`).

```js
const prefixer = require('postcss-namespace-selector');

module: {
  rules: [{
    test: /\.css$/,
    use: [
      require.resolve('style-loader'),
      require.resolve('css-loader'),
      {
        loader: require.resolve('postcss-loader'),
        options: {
          plugins: () => [
            prefixer({
              namespace: '.my-namespace'
            }),
            autoprefixer({
              browsers: ['last 4 versions']
            })
          ]
        }
      }
    ]
  }]
}
```

## Options

- `prefix` - This string is added before every CSS selector.
- `exclude` - It's possible to avoid prefixing some selectors by passing an array of selectors (strings or regular expressions).
- `transform` - In cases where you may want to use the prefix differently for different selectors, it is possible to pass in a custom transform method.
- `findConfig` - will find the nearest postcss.config.js configuration file when set to `true`.

---
_Source: https://npm.io/package/postcss-namespace-selector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
