Skip to content

Commit

Permalink
add namespace option. (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: Silhan Jan <jsilhan@oriflame.com>
Co-authored-by: Evan Jacobs <570070+probablyup@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 29, 2020
1 parent 004d03f commit 4187ada
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const useMinify = state => getOption(state, 'minify')
export const useTranspileTemplateLiterals = state =>
getOption(state, 'transpileTemplateLiterals')

export const useNamespace = state => {
const namespace = getOption(state, 'namespace', '')
if (namespace) {
return `${namespace}__`
}
return ''
}

export const usePureAnnotation = state => getOption(state, 'pure', false)

export const useCssProp = state => getOption(state, 'cssProp', true)
11 changes: 9 additions & 2 deletions src/visitors/displayNameAndId.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import path from 'path'
import fs from 'fs'
import { useFileName, useDisplayName, useSSR } from '../utils/options'
import {
useFileName,
useDisplayName,
useSSR,
useNamespace,
} from '../utils/options'
import getName from '../utils/getName'
import prefixLeadingDigit from '../utils/prefixDigit'
import hash from '../utils/hash'
Expand Down Expand Up @@ -130,7 +135,9 @@ const getNextId = state => {

const getComponentId = state => {
// Prefix the identifier with a character because CSS classes cannot start with a number
return `${prefixLeadingDigit(getFileHash(state))}-${getNextId(state)}`
return `${useNamespace(state)}${prefixLeadingDigit(
getFileHash(state)
)}-${getNextId(state)}`
}

export default t => (path, state) => {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/use-namespace/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
[
"../../../src",
{
"namespace": "test-namespace"
}
]
]
}
13 changes: 13 additions & 0 deletions test/fixtures/use-namespace/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import styled from 'styled-components'

const Test = styled.div`
color: red;
`

const before = styled.div`
color: blue;
`

styled.div``

export default styled.button``
17 changes: 17 additions & 0 deletions test/fixtures/use-namespace/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import styled from 'styled-components';
const Test = styled.div.withConfig({
displayName: "code__Test",
componentId: "test-namespace__sc-3rfj0a-0"
})(["color:red;"]);
const before = styled.div.withConfig({
displayName: "code__before",
componentId: "test-namespace__sc-3rfj0a-1"
})(["color:blue;"]);
styled.div.withConfig({
displayName: "code",
componentId: "test-namespace__sc-3rfj0a-2"
})([""]);
export default styled.button.withConfig({
displayName: "code",
componentId: "test-namespace__sc-3rfj0a-3"
})([""]);

0 comments on commit 4187ada

Please sign in to comment.