npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

postcss-wrap-selectors

v0.1.1

Published

Wrap selectors with a scope class to prevent style bleed.

Downloads

9

Readme

postcss-wrap-selectors

English

A PostCSS plugin that automatically wraps all CSS selectors with a given parent class (e.g., .l-wrapper). Useful for scoping styles to avoid conflicts with legacy or client CSS. Includes examples for both CLI and Gulp usage.

Features

  • Scope all selectors under a wrapper class
  • Avoids duplicate wrapping if the wrapper already exists
  • Automatically excludes html, body, :root
  • Skips @keyframes rules
  • Configurable via plugin.config.json or options

Installation

npm i -D postcss postcss-cli postcss-wrap-selectors

Peer dependency:

  • postcss (v8)

Usage

With PostCSS CLI

postcss.config.js

import wrap from "postcss-wrap-selectors";
import conf from "./plugin.config.json" assert { type: "json" };

export default {
  plugins: [
    wrap({
      wrapper: conf.wrapper ?? ".l-wrapper",
      exclude: conf.exclude ?? ["html", "body", ":root"],
      skipKeyframes: conf.skipKeyframes ?? true,
    }),
  ],
};

Run:

npx postcss input.css -o output.css

With Gulp

gulpfile.js

import gulp from "gulp";
import sass from "gulp-dart-sass";
import postcss from "gulp-postcss";
import wrap from "postcss-wrap-selectors";
import conf from "./plugin.config.json" assert { type: "json" };

export const css = () =>
  gulp
    .src("src/style.scss")
    .pipe(sass({ outputStyle: "expanded" }))
    .pipe(postcss([wrap(conf)]))
    .pipe(gulp.dest("dist"));

export default css;

Example

Input:

.button {
  &--primary {
    color: red;
  }
}

Output:

.l-wrapper .button--primary {
  color: red;
}

Options

| Option | Type | Default | Description | | --------------- | ---------- | --------------------------- | -------------------------------------------- | | wrapper | string | .l-wrapper | Class name to wrap selectors with | | exclude | string[] | ["html", "body", ":root"] | Selectors that should not be wrapped | | skipKeyframes | boolean | true | Whether to skip wrapping inside @keyframes |

Development

Build:

npm run build

Run examples:

cd examples/gulp
npm i
npm run build

cd ../cli
npm i
npm run build

License

MIT


日本語

特定のクラスで全てのセレクタを自動的にラップする PostCSS プラグインです。 クライアント側の既存 CSS がこちらのスタイルを上書きしてしまうケースなどで、 全体を親クラス配下にスコープさせたいときに便利です。 CLI と Gulp の両方の使用例を含んでいます。

特長

  • 指定したラップ用クラスで全セレクタをスコープ化
  • 既にラップクラスを含むセレクタは重複して付与しない
  • html, body, :root などは自動的に除外
  • @keyframes 内は自動的にスキップ
  • 設定ファイル(plugin.config.json)またはオプションで柔軟に制御可能

インストール

npm i -D postcss postcss-cli postcss-wrap-selectors

ピア依存関係:

  • postcss (v8)

使い方

PostCSS CLI の場合

postcss.config.js

import wrap from "postcss-wrap-selectors";
import conf from "./plugin.config.json" assert { type: "json" };

export default {
  plugins: [
    wrap({
      wrapper: conf.wrapper ?? ".l-wrapper",
      exclude: conf.exclude ?? ["html", "body", ":root"],
      skipKeyframes: conf.skipKeyframes ?? true,
    }),
  ],
};

実行:

npx postcss input.css -o output.css

Gulp の場合

gulpfile.js

import gulp from "gulp";
import sass from "gulp-dart-sass";
import postcss from "gulp-postcss";
import wrap from "postcss-wrap-selectors";
import conf from "./plugin.config.json" assert { type: "json" };

export const css = () =>
  gulp
    .src("src/style.scss")
    .pipe(sass({ outputStyle: "expanded" }))
    .pipe(postcss([wrap(conf)]))
    .pipe(gulp.dest("dist"));

export default css;

実行例

入力:

.button {
  &--primary {
    color: red;
  }
}

出力:

.l-wrapper .button--primary {
  color: red;
}

オプション

| オプション | 型 | デフォルト | 説明 | | --------------- | ---------- | --------------------------- | ------------------------- | | wrapper | string | .l-wrapper | ラップに使用するクラス名 | | exclude | string[] | ["html", "body", ":root"] | ラップを除外するセレクタ | | skipKeyframes | boolean | true | @keyframes 内をラップ対象から外すか |

開発方法

ビルド:

npm run build

サンプル実行:

cd examples/gulp
npm i
npm run build

cd ../cli
npm i
npm run build

ライセンス

MIT