Skip to content

Commit 4187ada

Browse files
rajzikquantizor
andauthored
add namespace option. (#281)
Co-authored-by: Silhan Jan <[email protected]> Co-authored-by: Evan Jacobs <[email protected]>
1 parent 004d03f commit 4187ada

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

src/utils/options.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export const useMinify = state => getOption(state, 'minify')
1111
export const useTranspileTemplateLiterals = state =>
1212
getOption(state, 'transpileTemplateLiterals')
1313

14+
export const useNamespace = state => {
15+
const namespace = getOption(state, 'namespace', '')
16+
if (namespace) {
17+
return `${namespace}__`
18+
}
19+
return ''
20+
}
21+
1422
export const usePureAnnotation = state => getOption(state, 'pure', false)
1523

1624
export const useCssProp = state => getOption(state, 'cssProp', true)

src/visitors/displayNameAndId.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import path from 'path'
22
import fs from 'fs'
3-
import { useFileName, useDisplayName, useSSR } from '../utils/options'
3+
import {
4+
useFileName,
5+
useDisplayName,
6+
useSSR,
7+
useNamespace,
8+
} from '../utils/options'
49
import getName from '../utils/getName'
510
import prefixLeadingDigit from '../utils/prefixDigit'
611
import hash from '../utils/hash'
@@ -130,7 +135,9 @@ const getNextId = state => {
130135

131136
const getComponentId = state => {
132137
// Prefix the identifier with a character because CSS classes cannot start with a number
133-
return `${prefixLeadingDigit(getFileHash(state))}-${getNextId(state)}`
138+
return `${useNamespace(state)}${prefixLeadingDigit(
139+
getFileHash(state)
140+
)}-${getNextId(state)}`
134141
}
135142

136143
export default t => (path, state) => {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../src",
5+
{
6+
"namespace": "test-namespace"
7+
}
8+
]
9+
]
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import styled from 'styled-components'
2+
3+
const Test = styled.div`
4+
color: red;
5+
`
6+
7+
const before = styled.div`
8+
color: blue;
9+
`
10+
11+
styled.div``
12+
13+
export default styled.button``
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled from 'styled-components';
2+
const Test = styled.div.withConfig({
3+
displayName: "code__Test",
4+
componentId: "test-namespace__sc-3rfj0a-0"
5+
})(["color:red;"]);
6+
const before = styled.div.withConfig({
7+
displayName: "code__before",
8+
componentId: "test-namespace__sc-3rfj0a-1"
9+
})(["color:blue;"]);
10+
styled.div.withConfig({
11+
displayName: "code",
12+
componentId: "test-namespace__sc-3rfj0a-2"
13+
})([""]);
14+
export default styled.button.withConfig({
15+
displayName: "code",
16+
componentId: "test-namespace__sc-3rfj0a-3"
17+
})([""]);

0 commit comments

Comments
 (0)