Skip to content

Commit

Permalink
Add topLevelImportPaths option (#288)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefen Alper <stefen.alper@gmail.com>
  • Loading branch information
salper and salper committed Jul 30, 2020
1 parent 670290d commit 325167b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/utils/detectors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useTopLevelImportPaths } from './options'

const VALID_TOP_LEVEL_IMPORT_PATHS = [
'styled-components',
'styled-components/no-tags',
'styled-components/native',
'styled-components/primitives',
]

export const isValidTopLevelImport = x =>
VALID_TOP_LEVEL_IMPORT_PATHS.includes(x)
export const isValidTopLevelImport = (x, state) =>
[...VALID_TOP_LEVEL_IMPORT_PATHS, ...useTopLevelImportPaths(state)].includes(
x
)

const localNameCache = {}

Expand All @@ -28,7 +32,7 @@ export const importLocalName = (name, state, bypassCache = false) => {
exit(path) {
const { node } = path

if (isValidTopLevelImport(node.source.value)) {
if (isValidTopLevelImport(node.source.value, state)) {
for (const specifier of path.get('specifiers')) {
if (specifier.isImportDefaultSpecifier()) {
localName = specifier.node.local.name
Expand Down Expand Up @@ -107,7 +111,9 @@ export const isWithThemeHelper = t => (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('withTheme', state)

export const isHelper = t => (tag, state) =>
isCSSHelper(t)(tag, state) || isKeyframesHelper(t)(tag, state) || isWithThemeHelper(t)(tag, state)
isCSSHelper(t)(tag, state) ||
isKeyframesHelper(t)(tag, state) ||
isWithThemeHelper(t)(tag, state)

export const isPureHelper = t => (tag, state) =>
isCSSHelper(t)(tag, state) ||
Expand Down
2 changes: 2 additions & 0 deletions src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function getOption({ opts }, name, defaultValue = true) {
}

export const useDisplayName = state => getOption(state, 'displayName')
export const useTopLevelImportPaths = state =>
getOption(state, 'topLevelImportPaths', [])
export const useSSR = state => getOption(state, 'ssr', true)
export const useFileName = state => getOption(state, 'fileName')
export const useMinify = state => getOption(state, 'minify')
Expand Down
2 changes: 1 addition & 1 deletion src/visitors/assignStyledRequired.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default t => (path, state) => {
path.node.init.arguments &&
path.node.init.arguments[0] &&
t.isLiteral(path.node.init.arguments[0]) &&
isValidTopLevelImport(path.node.init.arguments[0].value)
isValidTopLevelImport(path.node.init.arguments[0].value, state)
) {
state.styledRequired = path.node.id.name
}
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/add-identifier-with-top-level-import-paths/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"plugins": [
[
"../../../src",
{
"displayName": false,
"fileName": false,
"ssr": true,
"topLevelImportPaths": [
"@xstyled/styled-components",
"@xstyled/styled-components/no-tags",
"@xstyled/styled-components/native",
"@xstyled/styled-components/primitives"
],
"transpileTemplateLiterals": false
}
]
]
}
10 changes: 10 additions & 0 deletions test/fixtures/add-identifier-with-top-level-import-paths/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from '@xstyled/styled-components'

const Test = styled.div`
width: 100%;
`
const Test2 = true ? styled.div`` : styled.div``
const styles = { One: styled.div`` }
let Component
Component = styled.div``
const WrappedComponent = styled(Inner)``
21 changes: 21 additions & 0 deletions test/fixtures/add-identifier-with-top-level-import-paths/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import styled from '@xstyled/styled-components';
const Test = styled.div.withConfig({
componentId: "sc-1mlyrvc-0"
})`width:100%;`;
const Test2 = true ? styled.div.withConfig({
componentId: "sc-1mlyrvc-1"
})`` : styled.div.withConfig({
componentId: "sc-1mlyrvc-2"
})``;
const styles = {
One: styled.div.withConfig({
componentId: "sc-1mlyrvc-3"
})``
};
let Component;
Component = styled.div.withConfig({
componentId: "sc-1mlyrvc-4"
})``;
const WrappedComponent = styled(Inner).withConfig({
componentId: "sc-1mlyrvc-5"
})``;

0 comments on commit 325167b

Please sign in to comment.