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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-scoped-css

v0.1.12

Published

Vue-like scoped CSS for React — compile-time CSS isolation

Readme

vite-plugin-scoped-css

类 Vue scoped CSS 的 React 方案——零运行时开销,纯编译时样式隔离。

原理

所有转换在编译时完成。useScoped() 是一个编译标记——调用语句和导入语句都会在最终产物中被移除。

| 编译前(你的代码) | 编译后(产物) | |---|---| | <div className="red"> | <div className="red" data-v-19c20bfb> | | .red { color: red; } | .red[data-v-19c20bfb] { color: red; } |

每个 CSS 文件会生成唯一的 hash。<div className="red"> 只会命中来自该 scoped 样式表的 .red 规则——不会受其他文件或第三方 CSS 影响。

安装

npm install vite-plugin-scoped-css

配置

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import vitePluginScopedCSS from 'vite-plugin-scoped-css'

export default defineConfig({
  plugins: [vitePluginScopedCSS(), react()],
})

注意: scoped 插件必须放在 @vitejs/plugin-react 前面

TypeScript 项目需要在 vite-env.d.ts 中添加类型引用:

declare module '*.css' {
  const css: string;
  export default css;
}

使用

import styles from './style.css'
import { useScoped } from 'vite-plugin-scoped-css'

function App() {
  useScoped(styles)

  return (
    <div>
      <h1 className="title">Hello World</h1>
      <p className="red">这段文字是红色的——但只在这个组件里。</p>
    </div>
  )
}
/* style.css */
.title { font-weight: bold; }
.red   { color: red; }

就这么简单。 样式被限定在 App 组件内,不会泄漏到其他组件或第三方代码中。

多个样式表

一个组件可以引入多个 scoped CSS 文件:

import styles1 from './layout.css'
import styles2 from './theme.css'
import { useScoped } from 'vite-plugin-scoped-css'

function App() {
  useScoped(styles1)
  useScoped(styles2)

  return <div className="container primary">...</div>
}

产物效果

编译后 useScoped 完全消失,零运行时开销:

$ npm run build
dist/assets/index-xxx.css    # 选择器追加了 [data-v-hash]
dist/assets/index-xxx.js     # 元素挂上了 data-v-hash,useScoped 已移除

License

MIT