Gathering detailed insights and metrics for rollup-plugin-react-scoped-css
Gathering detailed insights and metrics for rollup-plugin-react-scoped-css
Gathering detailed insights and metrics for rollup-plugin-react-scoped-css
Gathering detailed insights and metrics for rollup-plugin-react-scoped-css
A rollup plugin designed to allow scoped css to be run in react (Compatible with vite and rollup)
npm install rollup-plugin-react-scoped-css
Typescript
Module System
Node Version
NPM Version
TypeScript (81.98%)
JavaScript (18.02%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
47 Stars
61 Commits
6 Forks
3 Watchers
7 Branches
6 Contributors
Updated on May 02, 2025
Latest Version
1.2.1
Package Id
rollup-plugin-react-scoped-css@1.2.1
Unpacked Size
35.47 kB
Size
8.83 kB
File Count
25
NPM Version
10.9.0
Node Version
20.18.1
Published on
Jan 18, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
26
While using react in a professional context, I realized that it was lacking the scopped css feature that I learned to love from Vue and Angular. After some reasearch I came across good plugins, but sadly were not compatible with vite and/or rollup. Thus, I decided to create this plugin which was greatly inspired by the amazing work of gaoxiaoliangz with his react-scoped-css plugin.
1$ npm i rollup-plugin-react-scoped-css
in vite:
1// vite.config.js 2import { defineConfig } from 'vite' 3import react from '@vitejs/plugin-react' 4import { reactScopedCssPlugin } from 'rollup-plugin-react-scoped-css' 5 6// https://vitejs.dev/config/ 7export default defineConfig({ 8 plugins: [react(), reactScopedCssPlugin()] 9})
in rollup:
1// rollup.config.js 2import reactScopedCssPlugin from 'rollup-plugin-react-scoped-css'; 3 4export default { 5 input: 'src/input.js', 6 output: { file: 'ouput.js' }, 7 plugins: [ reactScopedCssPlugin() ] 8};
Notes: Because this plugin has been built with vite as its primary usecase, it doesn't transpile LESS, SCSS or other preprocessors. For that reason, you will likely need to add your transpilation plugins before reactScopedCssPlugin if you plan on using this without vite.
There are a few options available to customize how the plugin works
1{ 2 /** 3 * Which files should be included and parsed by the plugin 4 * Default: undefined 5 */ 6 include?: FilterPattern; 7 8 /** 9 * Which files should be exluded and that should not be parsed by the plugin 10 * Default: undefined 11 */ 12 exclude?: FilterPattern; 13 14 /** 15 * If you want regular files to be scoped & global files to be .global.css 16 * Default: false 17 */ 18 scopeStyleByDefault?: boolean; 19 20 /** 21 * If you want to customize the pattern for scoped styles. 22 * This will only work if scopeStyleByDefault is false 23 * Default: 'scoped' 24 */ 25 scopedStyleSuffix?: string; 26 27 /** 28 * If you want to customize the pattern for global styles. 29 * This will only work if scopeStyleByDefault is true 30 * Default: 'global' 31 */ 32 globalStyleSuffix?: string; 33 34 /** 35 * If you want to customize the pattern for style files. 36 * Default: ['css', 'scss', 'sass', 'less'] 37 */ 38 styleFileExtensions?: string[]; 39 40 /** 41 * If you want to customize the pattern for jsx files. 42 * Default: ['jsx', 'tsx'] 43 */ 44 jsxFileExtensions?: string[]; 45 46 /** 47 * If you want to customize the attribute prefix that is added to the jsx elements 48 * Default: 'v' 49 */ 50 hashPrefix?: string; 51}
Since this plugin works in two parts, you might need to expose the first part, then add any other plugin, and then expose the second part of the plugin. This part is automatically handled with vite thanks to the enforce attribute.
1const reactScopedPlugins = reactScopedCssPlugin() 2export default { 3 //... 4 plugins: [ reactScopedPlugins[0], {...stylingPlugins}, reactScopedPlugins[1] ] 5};
1// Component.jsx 2import './Component.scoped.scss' 3 4export default function Sub() { 5 return ( 6 <div className="wrap"> 7 <h1>My Component</h1> 8 </div> 9 ) 10}
1// Component.scoped.scss 2.wrap { 3 width: 500px; 4 h1 { color: red; } 5}
And just like that the styles will be scoped to the component.
Due to the way this plugin is working, it will apply the scope to the file and not the component individually... This may differ from other frameworks since they don't really let you define multiple components in the same file. This then means that if you have 2 components in the same file, the styles might conflict.
If you want a selector in scoped styles to be "deep", i.e. affecting child components, you can use the ::deep combinator:
1.a::deep .b { /* ... */ }
The above will be compiled into:
1.a[data-f3f3eg9] .b { /* ... */ }
Another exepted format, which will generate the same resutl, is:
1.a::v-deep .b { /* ... */ }
This is primarly for backwards compatibility, we recommend the ::deep
selector.
DOM content created with dangerouslySetInnerHTML are not affected by scoped styles, but you can still style them using deep selectors.
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit.
Be careful with descendant selectors in recursive components! For a CSS rule with the selector .a .b, if the element that matches .a contains a recursive child component, then all .b in that child component will be matched by the rule.
Anyone is free to open a PR and contribute to this project... just be civilized!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
packaging workflow detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
15 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More