Gathering detailed insights and metrics for babel-preset-react-scope-style
Gathering detailed insights and metrics for babel-preset-react-scope-style
Gathering detailed insights and metrics for babel-preset-react-scope-style
Gathering detailed insights and metrics for babel-preset-react-scope-style
a babel plugin that scope style for style files in react component
npm install babel-preset-react-scope-style
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (96%)
CSS (4%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
23 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Dec 19, 2024
Latest Version
0.0.13
Package Id
babel-preset-react-scope-style@0.0.13
Unpacked Size
65.43 kB
Size
14.89 kB
File Count
21
NPM Version
10.7.0
Node Version
18.20.4
Published on
Dec 19, 2024
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
6
32
A babel plugin that scope style for style files in react component
1 npm install --save-dev babel-preset-react-scope-style 2 // or 3 yarn add -D babel-preset-react-scope-style
babel.config.js:
1module.exports = { 2 presets: [ 3 ... 4 ['babel-preset-react-scope-style'] 5 ], 6 plugins: [ 7 ... 8 ] 9};
webpack.config.js:
1{ 2 module: { 3 rules: [ 4 { 5 test: /\.css$/, 6 use: [ 7 'css-loader', 8 'babel-preset-react-scope-style/loader', 9 ... 10 ] 11 }, 12 { 13 test: /\.scss$/, 14 use: [ 15 'css-loader', 16 'babel-preset-react-scope-style/loader', 17 ... 18 ] 19 }, 20 { 21 test: /\.less$/, 22 use: [ 23 'css-loader', 24 'babel-preset-react-scope-style/loader', 25 ... 26 ] 27 } 28 ] 29 } 30}
Type: RegExp
Default: /(.(?:le|sc|sa|c)ss)(?[a-z]+)?$/
Description: A regular expression used to match style files that need to be processed. By default, it matches .less, .scss, .sass, and .css files and supports query parameters (e.g., ?scoped).
Type: Boolean
Default: true
Description: Whether to enable scoped styling. If set to false, scoped styles will not be generated.
Type: (ext: string, query: string, options: { filename, source, scopeId, global, pkg }) => string
Default: null
Description: A custom function to modify the suffix and query parameters of imported style files.
1module.exports = { 2 presets: [ 3 [ 4 'babel-preset-react-scope-style', 5 { 6 scopeRegx: /(\.(?:le|sc|sa|c)ss)(\?scoped)?$/, 7 scope: true, 8 // Change the suffix of imported .scss/.less files to .css 9 scopeFn: (filename, pkgName) => '.css', 10 scopePrefix: 'v-', 11 scopeAttrs: true, 12 scopeAll: false, 13 scopeVersion: true, 14 pkg: require('./package.json'), 15 classAttrs: ['className', 'dropdownClassName'] 16 } 17 ] 18 ] 19};
If a js/jsx file imports a style file with the ?scoped suffix, it means that scoped styles are enabled for that file. For example:
1// test.js 2import React from 'react'; 3 4import './test.scss?scoped'; 5 6class Test extends React.Component { 7 8 renderSome() { 9 return <div> 10 <button>button</button> 11 </div> 12 } 13 14 render() { 15 return <div className="a"> 16 <a className="b">link</a> 17 { this.renderSome() } 18 </div> 19 } 20}
1// test.scss 2 3.a { 4 color: red; 5} 6 7.a .b { 8 color: green; 9} 10
During the build process, a hash string v-xxxxxxxx
is automatically generated based on the project name (package.json -> name
) and file path, and applied to all className
attributes in the JSX. For example:
1// test.js 2import React from 'react'; 3import { Button } from 'antd'; 4 5import './test.scss?scoped'; 6 7class Test extends React.Component { 8 9 renderSome() { 10 return <div className="v-xxxxxxxx"> 11 <Button className="v-xxxxxxxx">button1</Button> 12 </div> 13 } 14 15 render() { 16 return <div className="v-xxxxxxxx a"> 17 <a className="v-xxxxxxxx b">link</a> 18 </div> 19 } 20}
1// test.scss 2 3.a.v-xxxxxxxx { 4 color: red; 5} 6 7.a .b.v-xxxxxxxx { 8 color: green; 9} 10
It is recommended that the scoped style
file name and the corresponding JS file name maintain a 'one-to-one' relationship. That is, if the JS file name istest.js
, the style file name istest.scss
。
By default, the hash string is generated for the last selector in the style file. If you want to customize the hash generation location, you can use the :scope
pseudo-class or >>>
. For example, if the style file is as follows:
1// test.scss 2 3.a { 4 color: red; 5} 6 7.a:scope .b { 8 color: green; 9} 10 11.a >>> .c { 12 color: blue; 13} 14
The resulting style file will be:
1// test.scss 2 3.a.v-xxxxxxxx { 4 color: red; 5} 6 7.a.v-xxxxxxxx .b { 8 color: green; 9} 10 11.a.v-xxxxxxxx .c { 12 color: blue; 13} 14
In some usage scenarios, if you do not want to generate a hash scope for some styles, you can use the :global
pseudo-class. For example, if the style file is as follows:
1// test.scss 2 3.a { 4 color: red; 5} 6 7.a:scope .b { 8 color: green; 9} 10 11:global { 12 .a { 13 background: blue; 14 } 15} 16
The generated style file will be:
1// test.scss 2 3.a.v-xxxxxxxx { 4 color: red; 5} 6 7.a.v-xxxxxxxx .b { 8 color: green; 9} 10 11.a { 12 background: blue; 13} 14
A project generally contains some global style files that usually contain settings or utility style classes affecting the entire project. In cross-project integration, these global styles can easily lead to conflicts between projects. ?global-style
is designed to minimize this issue.
babel.config.js
:1... 2[ 3 'babel-preset-react-scope-style', 4 { 5 scopeNamespace: 'demo' // configure the scope style namespace, such as `demo` 6 } 7], 8...
In the above configuration, scopeNamespace
is set to demo. The generated hash string will now be v-demo-xxxxxxxx
instead of v-xxxxxxxx
.
?global
to the import
statement that references the global style:1// index.js 2import '~/assets/styles/global.css?global'; 3 4....
Assume the global.css
file looks like this:
1.something-class * { 2 box-sizing: border-box; 3} 4 5.something-class.fl { 6 float: left; 7} 8 9.something-class .fr { 10 float: right; 11} 12 13.something-class .fr:scope .dd { 14 color: red; 15}
The final generated style file will look like this:
1.something-class *[class*=v-demo-] { 2 box-sizing: border-box; 3 } 4 5 6.something-class.fl[class*=v-demo-] { 7 float: left; 8} 9 10.something-class .fr[class*=v-demo-] { 11 float: right; 12} 13 14.something-class .fr[class*=v-demo-] .dd { 15 color: red; 16}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
34 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