Skip to content

Commit 41da06e

Browse files
committed
Support pre-transpiled code (#379)
1 parent a7d8394 commit 41da06e

File tree

6 files changed

+71
-5
lines changed

6 files changed

+71
-5
lines changed

src/utils/detectors.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import getSequenceExpressionValue from './getSequenceExpressionValue'
12
import { useTopLevelImportPathMatchers } from './options'
23

34
const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
@@ -81,6 +82,15 @@ export const isStyled = t => (tag, state) => {
8182
) {
8283
// styled.something()
8384
return isStyled(t)(tag.callee.object, state)
85+
} else if (
86+
t.isCallExpression(tag) &&
87+
t.isSequenceExpression(tag.callee) &&
88+
t.isMemberExpression(getSequenceExpressionValue(tag.callee)) &&
89+
getSequenceExpressionValue(tag.callee).property.name !==
90+
'default' /** ignore default for #93 below */
91+
) {
92+
// (..., styled).something()
93+
return isStyled(t)(getSequenceExpressionValue(tag.callee), state)
8494
} else {
8595
return (
8696
(t.isMemberExpression(tag) &&
@@ -94,6 +104,12 @@ export const isStyled = t => (tag, state) => {
94104
importLocalName('default', state, {
95105
cacheIdentifier: tag.callee.name,
96106
})) ||
107+
(t.isCallExpression(tag) &&
108+
t.isSequenceExpression(tag.callee) &&
109+
getSequenceExpressionValue(tag.callee).name ===
110+
importLocalName('default', state, {
111+
cacheIdentifier: getSequenceExpressionValue(tag.callee).name,
112+
})) ||
97113
/**
98114
* #93 Support require()
99115
* styled-components might be imported using a require()
@@ -111,6 +127,13 @@ export const isStyled = t => (tag, state) => {
111127
t.isMemberExpression(tag.callee) &&
112128
tag.callee.property.name === 'default' &&
113129
tag.callee.object.name === state.styledRequired) ||
130+
(state.styledRequired &&
131+
t.isCallExpression(tag) &&
132+
t.isSequenceExpression(tag.callee) &&
133+
t.isMemberExpression(getSequenceExpressionValue(tag.callee)) &&
134+
getSequenceExpressionValue(tag.callee).property.name === 'default' &&
135+
getSequenceExpressionValue(tag.callee).object.name ===
136+
state.styledRequired) ||
114137
(importLocalName('default', state) &&
115138
t.isMemberExpression(tag) &&
116139
t.isMemberExpression(tag.object) &&
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default path =>
2+
path.expressions && path.expressions.length
3+
? path.expressions[path.expressions.length - 1]
4+
: undefined

src/visitors/assignStyledRequired.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import { isValidTopLevelImport } from '../utils/detectors'
22

33
export default t => (path, state) => {
4+
let init = path.node.init;
5+
46
if (
57
t.isCallExpression(path.node.init) &&
68
t.isIdentifier(path.node.init.callee) &&
7-
path.node.init.callee.name === 'require' &&
8-
path.node.init.arguments &&
9-
path.node.init.arguments[0] &&
10-
t.isLiteral(path.node.init.arguments[0]) &&
11-
isValidTopLevelImport(path.node.init.arguments[0].value, state)
9+
init.callee.name === '_interopRequireDefault' &&
10+
init.arguments &&
11+
init.arguments[0]
12+
) {
13+
// _interopRequireDefault(require())
14+
init = path.node.init.arguments[0];
15+
}
16+
17+
if (
18+
t.isCallExpression(init) &&
19+
t.isIdentifier(init.callee) &&
20+
init.callee.name === 'require' &&
21+
init.arguments &&
22+
init.arguments[0] &&
23+
t.isLiteral(init.arguments[0]) &&
24+
isValidTopLevelImport(init.arguments[0].value, state)
1225
) {
1326
state.styledRequired = path.node.id.name
1427
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../src",
5+
{
6+
"fileName": false,
7+
"transpileTemplateLiterals": false,
8+
"ssr": true
9+
}
10+
],
11+
["@babel/plugin-proposal-class-properties", { "loose": true }]
12+
]
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var _styled = _interopRequireDefault(require("styled-components"));
2+
3+
const Test = (0, _styled.default)('div')({
4+
width: '100%',
5+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var _styled = _interopRequireDefault(require("styled-components"));
2+
3+
const Test = (0, _styled.default)('div').withConfig({
4+
displayName: "Test",
5+
componentId: "sc-1m3e07f-0"
6+
})({
7+
width: '100%'
8+
});

0 commit comments

Comments
 (0)