Skip to content

Commit 03a58d5

Browse files
authored
Merge pull request #340 from styled-components/315-namespace-import-support
handle namespace import helpers
2 parents fcf18d5 + 62a2ef0 commit 03a58d5

File tree

11 files changed

+107
-40
lines changed

11 files changed

+107
-40
lines changed

src/utils/detectors.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export const isValidTopLevelImport = (x, state) =>
1515
const localNameCache = {}
1616

1717
export const importLocalName = (name, state, options = {}) => {
18-
const { cacheIdentifier, bypassCache = false } = options;
19-
const cacheKeyAffix = cacheIdentifier ? `|${cacheIdentifier}` : '';
20-
const cacheKey = name + state.file.opts.filename + cacheKeyAffix;
18+
const { cacheIdentifier, bypassCache = false } = options
19+
const cacheKeyAffix = cacheIdentifier ? `|${cacheIdentifier}` : ''
20+
const cacheKey = name + state.file.opts.filename + cacheKeyAffix
2121

2222
if (!bypassCache && cacheKey in localNameCache) {
2323
return localNameCache[cacheKey]
@@ -36,10 +36,13 @@ export const importLocalName = (name, state, options = {}) => {
3636

3737
if (isValidTopLevelImport(node.source.value, state)) {
3838
for (const specifier of path.get('specifiers')) {
39-
if (specifier.isImportSpecifier() && specifier.node.imported.name === 'styled') {
39+
if (
40+
specifier.isImportSpecifier() &&
41+
specifier.node.imported.name === 'styled'
42+
) {
4043
localName = 'styled'
4144
}
42-
45+
4346
if (specifier.isImportDefaultSpecifier()) {
4447
localName = specifier.node.local.name
4548
}
@@ -52,7 +55,7 @@ export const importLocalName = (name, state, options = {}) => {
5255
}
5356

5457
if (specifier.isImportNamespaceSpecifier()) {
55-
localName = specifier.node.local.name
58+
localName = name === 'default' ? specifier.node.local.name : name
5659
}
5760
}
5861
}
@@ -76,9 +79,16 @@ export const isStyled = t => (tag, state) => {
7679
} else {
7780
return (
7881
(t.isMemberExpression(tag) &&
79-
tag.object.name === importLocalName('default', state, { cacheIdentifier: tag.object.name })) ||
82+
tag.object.name ===
83+
importLocalName('default', state, {
84+
cacheIdentifier: tag.object.name,
85+
}) &&
86+
!isHelper(t)(tag.property, state)) ||
8087
(t.isCallExpression(tag) &&
81-
tag.callee.name === importLocalName('default', state, { cacheIdentifier: tag.callee.name })) ||
88+
tag.callee.name ===
89+
importLocalName('default', state, {
90+
cacheIdentifier: tag.callee.name,
91+
})) ||
8292
/**
8393
* #93 Support require()
8494
* styled-components might be imported using a require()
@@ -95,7 +105,17 @@ export const isStyled = t => (tag, state) => {
95105
t.isCallExpression(tag) &&
96106
t.isMemberExpression(tag.callee) &&
97107
tag.callee.property.name === 'default' &&
98-
tag.callee.object.name === state.styledRequired)
108+
tag.callee.object.name === state.styledRequired) ||
109+
(importLocalName('default', state) &&
110+
t.isMemberExpression(tag) &&
111+
t.isMemberExpression(tag.object) &&
112+
tag.object.property.name === 'default' &&
113+
tag.object.object.name === importLocalName('default', state)) ||
114+
(importLocalName('default', state) &&
115+
t.isCallExpression(tag) &&
116+
t.isMemberExpression(tag.callee) &&
117+
tag.object.property.name === 'default' &&
118+
tag.object.object.name === importLocalName('default', state))
99119
)
100120
}
101121
}
@@ -116,13 +136,20 @@ export const isKeyframesHelper = t => (tag, state) =>
116136
export const isWithThemeHelper = t => (tag, state) =>
117137
t.isIdentifier(tag) && tag.name === importLocalName('withTheme', state)
118138

139+
export const isUseTheme = t => (tag, state) =>
140+
t.isIdentifier(tag) && tag.name === importLocalName('useTheme', state)
141+
119142
export const isHelper = t => (tag, state) =>
143+
isCreateGlobalStyleHelper(t)(tag, state) ||
120144
isCSSHelper(t)(tag, state) ||
145+
isInjectGlobalHelper(t)(tag, state) ||
146+
isUseTheme(t)(tag, state) ||
121147
isKeyframesHelper(t)(tag, state) ||
122148
isWithThemeHelper(t)(tag, state)
123149

124150
export const isPureHelper = t => (tag, state) =>
151+
isCreateGlobalStyleHelper(t)(tag, state) ||
125152
isCSSHelper(t)(tag, state) ||
126153
isKeyframesHelper(t)(tag, state) ||
127-
isCreateGlobalStyleHelper(t)(tag, state) ||
154+
isUseTheme(t)(tag, state) ||
128155
isWithThemeHelper(t)(tag, state)

src/visitors/displayNameAndId.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
import getName from '../utils/getName'
1010
import prefixLeadingDigit from '../utils/prefixDigit'
1111
import hash from '../utils/hash'
12-
import { isStyled } from '../utils/detectors'
12+
import {
13+
isCreateGlobalStyleHelper,
14+
isCSSHelper,
15+
isStyled,
16+
} from '../utils/detectors'
1317

1418
const addConfig = t => (path, displayName, componentId) => {
1519
if (!displayName && !componentId) {
@@ -211,8 +215,8 @@ export default t => (path, state) => {
211215
t.isMemberExpression(path.node.callee.callee) &&
212216
path.node.callee.callee.property &&
213217
path.node.callee.callee.property.name &&
214-
path.node.callee.callee.property.name == 'withConfig' &&
215-
!path.node.callee.arguments[0].properties.some((prop) =>
218+
path.node.callee.callee.property.name === 'withConfig' &&
219+
!path.node.callee.arguments[0].properties.some(prop =>
216220
['displayName', 'componentId'].includes(prop.key.name)
217221
))
218222
) {
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
import { createGlobalStyle } from 'styled-components';
2-
const GlobalStyle = /*#__PURE__*/createGlobalStyle`
3-
body {
4-
color: red;
5-
}
6-
`;
2+
const GlobalStyle = /*#__PURE__*/createGlobalStyle(["body{color:red;}"]);
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { css, keyframes } from 'styled-components';
1+
import { createGlobalStyle, css, keyframes } from 'styled-components'
22

33
const key = keyframes`
44
to {
55
transform: rotate(360deg);
66
}
7-
`;
7+
`
88

99
const color = css`
1010
color: ${theColor};
11-
`;
11+
`
12+
13+
const GlobalStyles = createGlobalStyle`
14+
html {
15+
color: red;
16+
}
17+
`
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import { css, keyframes } from 'styled-components';
1+
import { createGlobalStyle, css, keyframes } from 'styled-components';
22
const key = keyframes`to{transform:rotate(360deg);}`;
33
const color = css`color:${theColor};`;
4+
const GlobalStyles = createGlobalStyle`html{color:red;}`;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"presets": [
3-
"@babel/preset-env"
4-
],
2+
"presets": ["@babel/preset-env"],
53
"plugins": [
64
[
75
"../../../src",
@@ -13,4 +11,4 @@
1311
}
1412
]
1513
]
16-
}
14+
}

test/fixtures/transpile-template-literals-with-config/code.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components'
1+
import styled, { css, createGlobalStyle } from 'styled-components'
22

33
const Named = styled.div`
44
width: 100%;
@@ -15,3 +15,13 @@ const Wrapped = styled(Inner)`
1515
const Foo = styled.div({
1616
color: 'green',
1717
})
18+
19+
const style = css`
20+
background: green;
21+
`
22+
23+
const GlobalStyle = createGlobalStyle`
24+
html {
25+
background: silver;
26+
}
27+
`

test/fixtures/transpile-template-literals-with-config/output.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"use strict";
22

3-
var _styledComponents = _interopRequireDefault(require("styled-components"));
3+
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
44

5-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
5+
var _styledComponents = _interopRequireWildcard(require("styled-components"));
6+
7+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
8+
9+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
610

711
var Named = _styledComponents["default"].div.withConfig({
812
displayName: "code__Named"
@@ -23,3 +27,6 @@ var Foo = _styledComponents["default"].div.withConfig({
2327
})({
2428
color: 'green'
2529
});
30+
31+
var style = (0, _styledComponents.css)(["\n background: green;\n"]);
32+
var GlobalStyle = (0, _styledComponents.createGlobalStyle)(["\n html {\n background: silver;\n }\n"]);

test/fixtures/use-namespace/.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
}
88
]
99
]
10-
}
10+
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
import styled from 'styled-components'
1+
import * as styled from 'styled-components'
22

3-
const Test = styled.div`
3+
const css = styled.css`
4+
background: black;
5+
`
6+
7+
const GlobalStyle = styled.createGlobalStyle`
8+
html {
9+
background: black;
10+
}
11+
`
12+
13+
const Test = styled.default.div`
414
color: red;
515
`
616

7-
const before = styled.div`
17+
const before = styled.default.div`
818
color: blue;
919
`
1020

11-
styled.div``
21+
styled.default.div``
1222

13-
export default styled.button``
23+
export default styled.default.button``

0 commit comments

Comments
 (0)