Skip to content

Commit fa65030

Browse files
authored
Merge pull request #354 from shortwave/rockwood/top-level-patterns
Change topLevelPaths option to take a regexp pattern
2 parents a20c303 + c612dab commit fa65030

File tree

9 files changed

+20
-21
lines changed

9 files changed

+20
-21
lines changed

src/utils/detectors.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import { useTopLevelImportPaths } from './options'
1+
import escapeRegExp from 'lodash/escapeRegExp'
2+
import { useTopLevelImportPathMatchers } from './options'
23

3-
const VALID_TOP_LEVEL_IMPORT_PATHS = [
4+
const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
45
'styled-components',
56
'styled-components/no-tags',
67
'styled-components/native',
78
'styled-components/primitives',
8-
]
9+
].map(literal => new RegExp(`^${escapeRegExp(literal)}$`))
910

10-
export const isValidTopLevelImport = (x, state) =>
11-
[...VALID_TOP_LEVEL_IMPORT_PATHS, ...useTopLevelImportPaths(state)].includes(
12-
x
13-
)
11+
export const isValidTopLevelImport = (x, state) => {
12+
return [
13+
...VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS,
14+
...useTopLevelImportPathMatchers(state),
15+
].some(re => re.test(x));
16+
}
1417

1518
const localNameCache = {}
1619

src/utils/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function getOption({ opts }, name, defaultValue = true) {
55
}
66

77
export const useDisplayName = state => getOption(state, 'displayName')
8-
export const useTopLevelImportPaths = state =>
9-
getOption(state, 'topLevelImportPaths', [])
8+
export const useTopLevelImportPathMatchers = state =>
9+
getOption(state, 'topLevelImportPaths', []).map(pattern => new RegExp(pattern))
1010
export const useSSR = state => getOption(state, 'ssr', true)
1111
export const useFileName = state => getOption(state, 'fileName')
1212
export const useMinify = state => getOption(state, 'minify')

src/visitors/minify.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ export default t => (path, state) => {
1414
templateLiteral.quasis.map(x => x.value.raw)
1515
)
1616

17-
const [
18-
cookedValuesMinfified,
19-
eliminatedExpressionIndices,
20-
] = minifyCookedValues(templateLiteral.quasis.map(x => x.value.cooked))
17+
const [cookedValuesMinfified, eliminatedExpressionIndices] =
18+
minifyCookedValues(templateLiteral.quasis.map(x => x.value.cooked))
2119

2220
eliminatedExpressionIndices.forEach((expressionIndex, iteration) => {
2321
templateLiteral.expressions.splice(expressionIndex - iteration, 1)

test/fixtures/add-identifier-with-top-level-import-paths-and-named-import/.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"displayName": true,
77
"fileName": false,
88
"ssr": true,
9-
"topLevelImportPaths": ["@example/example"],
9+
"topLevelImportPaths": [".*\/example$"],
1010
"transpileTemplateLiterals": false
1111
}
1212
]

test/fixtures/add-identifier-with-top-level-import-paths-and-named-import/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { styled } from '@example/example'
1+
import { styled } from '../../relative/example'
22

33
const Test = styled.div`
44
width: 100%;

test/fixtures/add-identifier-with-top-level-import-paths-and-named-import/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { styled } from '@example/example';
1+
import { styled } from '../../relative/example';
22
const Test = styled.div.withConfig({
33
displayName: "Test",
44
componentId: "sc-elhbfv-0"

test/fixtures/add-identifier-with-top-level-import-paths/.babelrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"ssr": true,
99
"topLevelImportPaths": [
1010
"@xstyled/styled-components",
11-
"@xstyled/styled-components/no-tags",
12-
"@xstyled/styled-components/native",
13-
"@xstyled/styled-components/primitives"
11+
"@xstyled/styled-components/*"
1412
],
1513
"transpileTemplateLiterals": false
1614
}

test/fixtures/add-identifier-with-top-level-import-paths/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from '@xstyled/styled-components'
1+
import styled from '@xstyled/styled-components/test'
22

33
const Test = styled.div`
44
width: 100%;

test/fixtures/add-identifier-with-top-level-import-paths/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from '@xstyled/styled-components';
1+
import styled from '@xstyled/styled-components/test';
22
const Test = styled.div.withConfig({
33
componentId: "sc-1mlyrvc-0"
44
})`width:100%;`;

0 commit comments

Comments
 (0)