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

prefix-selector

v2.0.0

Published

批量给css文件加前缀选择器

Readme

prefix-selector

批量给 css 文件加前缀选择器

本项目基于 postcss 和 postcss-prefix-selector 进行的整合的库 主要支持的是针对整个文件夹的 css 前缀选择器的添加

命令行使用方式

  # 查看使用方法
  prefix-selector -h

  prefix-selector <string> [options] 

    Arguments:
      string                   glob 定义的文件夹路径 https://www.npmjs.com/package/glob

    Options:
      -V, --version            output the version number
      -p, --prefix <prefix>    需添加的 css 前缀选择器
      -e, --exclude <exclude>  无需处理的选择器
      -b, --body               是否修改 body 为 `body${prefix}`
      -o, --id-class-only      只对 包含 id 或 class 选择器 的做替换
      -h, --help               display help for command

js 调用

  const { prefixSelector } = require('prefixSelector');

  // 具体参考 https://www.npmjs.com/package/postcss-prefix-selector#usage-with-postcss
  prefixSelector('glob路径', {
    // 前缀选择器
    prefix?: string | undefined;
    // 无需处理的选择器
    exclude?: ReadonlyArray<string | RegExp> | undefined;
    // 只对 包含 id 或 class 选择器 的做替换
    idClassOnly: boolean;
    // 自定义处理函数
    transform?: ((prefix: Readonly<string>, selector: Readonly<string>, prefixedSelector: Readonly<string>, file: Readonly<string>) => string) | undefined;
  })

postcss 插件调用

我们参考 postcss-prefix-selector 实现了 createPlugin,其入参与 js 调用的 方式的 第二个参数相同

const { createPlugin } = require('prefix-selector');
createPlugin(
  // 与 js 调用的第二个参数相同
  ...options,
  // 对于低版本的 post-css 需要开启该选项
  isOld: boolean,
)

运行时使用

考虑到存在 styled-components 等运行时 css 方案,我们这里提供了一个专用于运行时的插件

执行前后 css 比较

命令行:

  prefix-selector './a.css' -p '[customer-prefix]' -b

执行前 a.css:

  @media screen and (min-width: 480px) {
    body {
      background-color: lightgreen;
    }
  }

  #main {
    border: 1px solid black;
  }

  ul li {
    padding: 5px;
  }

执行后 a.css:

  @media screen and (min-width: 480px) {
    body[customer-prefix] {
      background-color: lightgreen;
    }
  }

  [customer-prefix] #main {
    border: 1px solid black;
  }

  [customer-prefix] ul li {
    padding: 5px;
  }

使用场景,qiankun 等微前端框架的样式隔离

  1. 在微前端的应用中子项目的样式会互相影响,通过本工具 你可以给每个子项目打包后的 css 选择器增加 前缀选择器,来实现样式隔离。
  2. 在主项目中可以监听子项目的预挂载事件动态修改 body 的属性(选择器)来进行对应匹配。
  let previousAppName;
  // 以 qiankun 为例

  registerMicroApps(..., {
    beforeMount: [
      (app: any) => {
        document.body.removeAttribute(previousAppName);
        document.body.setAttribute(app.name, '');
        previousAppName = app.name;
      },
    ],
  })