Skip to content

Commit 249a647

Browse files
committed
initial commit
0 parents  commit 249a647

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stage": 0
3+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lib
2+
node_modules
3+
npm-debug.log

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# react-prefix-loader
2+
3+
A Webpack loader that prefixes classes in React components
4+
The idea is to isolate styles in framework components
5+
6+
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
7+
8+
MyComponent.jsx
9+
10+
```jsx
11+
class MyComponent extends React.Component {
12+
render () {
13+
return <div className='myclass'></div>
14+
}
15+
}
16+
17+
export default MyComponent
18+
```
19+
20+
Output:
21+
22+
```jsx
23+
class MyComponent extends React.Component {
24+
render () {
25+
return <div className='MyComponent-myclass'></div>
26+
}
27+
}
28+
29+
export default MyComponent
30+
```
31+
32+
Ignores:
33+
- filenames that starts from not capital letter
34+
- modifiers (classes that starts from hyphen)
35+
- classes that starts from capital letter
36+
37+
Caveats:
38+
- searches for 'export default ComponentName' construction to find component name
39+
- prefixes only className fields that are set as string
40+
41+
## Installation
42+
43+
```
44+
npm install react-prefix-loader
45+
```
46+
47+
## License
48+
49+
MIT
50+
51+
## Recommendation
52+
53+
* Use it with [postcss-filename-prefix](https://github.com/vmakhaev/postcss-filename-prefix) for css files

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "react-prefix-loader",
3+
"version": "0.1.0",
4+
"description": "Webpack loader that prefixes classes in React components",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"prepublish": "babel --optional runtime src --out-dir lib",
8+
"postpublish": "rm -rf lib",
9+
"lint": "standard | snazzy",
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": "vmakhaev/react-prefix-loader",
13+
"keywords": [
14+
"react",
15+
"webpack"
16+
],
17+
"author": "Vladimir Makhaev",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"babel": "^5.8.34",
21+
"babel-eslint": "^4.1.5",
22+
"snazzy": "^2.0.1",
23+
"standard": "^5.4.1"
24+
},
25+
"standard": {
26+
"globals": [
27+
"assert",
28+
"describe",
29+
"it",
30+
"before",
31+
"beforeEach",
32+
"after",
33+
"afterEach"
34+
],
35+
"parser": "babel-eslint"
36+
}
37+
}

src/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const exportDefaultContainerRegex = /export default .*\(([a-zA-Z]*)/
2+
const exportDefaultRegex = /export default ([a-zA-Z]*)/
3+
const classNameRegex = /className=\'([a-zA-Z\-\s]*)\'/g
4+
const needPrefixRegex = /^[a-z]/
5+
6+
function prefix (className, name) {
7+
if (needPrefixRegex.test(className)) return `${name}-${className}`
8+
9+
return className
10+
}
11+
12+
function loader (source, inputSourceMap) {
13+
let matches = exportDefaultContainerRegex.exec(source) ||
14+
exportDefaultRegex.exec(source)
15+
16+
if (matches) {
17+
let name = matches[1]
18+
19+
source = source.replace(classNameRegex, (text, classNames) => {
20+
let prefixedClassNames = classNames
21+
.split(' ')
22+
.map((className) => prefix(className, name))
23+
.join(' ')
24+
25+
return `className='${prefixedClassNames}'`
26+
})
27+
}
28+
29+
this.callback(null, source, inputSourceMap)
30+
}
31+
32+
export default loader

0 commit comments

Comments
 (0)