Skip to content

Commit 8f0ad57

Browse files
authored
Merge pull request #181 from styled-components/make-css-prop-work-with-macro
Make the `css` prop work with the macro! 🎉
2 parents 94a86f1 + 85f98e3 commit 8f0ad57

File tree

7 files changed

+106
-39
lines changed

7 files changed

+106
-39
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.9.4",
2+
"version": "1.9.5-0",
33
"name": "babel-plugin-styled-components",
44
"description": "Improve the debugging experience and add server-side rendering support to styled-components",
55
"repository": "styled-components/babel-plugin-styled-components",
@@ -20,13 +20,15 @@
2020
"@babel/plugin-proposal-class-properties": "^7.0.0",
2121
"@babel/preset-env": "^7.0.0",
2222
"babel-core": "7.0.0-bridge.0",
23+
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
2324
"jest": "^23.6.0",
2425
"prettier": "^1.14.2",
2526
"rimraf": "^2.6.2",
2627
"styled-components": "^4.0.0"
2728
},
2829
"dependencies": {
2930
"@babel/helper-annotate-as-pure": "^7.0.0",
31+
"@babel/helper-module-imports": "^7.0.0",
3032
"babel-plugin-syntax-jsx": "^6.18.0",
3133
"lodash": "^4.17.10"
3234
},

src/index.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@ export default function({ types: t }) {
1010
return {
1111
inherits: syntax,
1212
visitor: {
13-
// These visitors insert newly generated code and missing import/require statements
14-
Program: {
15-
enter(path, state) {
16-
state.required = false
17-
state.items = []
18-
},
19-
exit(path, state) {
20-
const oldLength = path.node.body.length
21-
22-
path.node.body.push(...state.items)
23-
24-
const newLength = path.node.body.length
25-
26-
for (let i = 0; i < newLength - oldLength; i++) {
27-
// Queue all inserted items for revisiting so that tagged templates are transpiled
28-
path.requeue(path.get('body')[oldLength + i]);
29-
}
30-
},
31-
},
3213
JSXAttribute(path, state) {
3314
transpileCssProp(t)(path, state)
3415
},

src/visitors/transpileCssProp.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Most of this code was taken from @satya164's babel-plugin-css-prop
22
// @see https://github.com/satya164/babel-plugin-css-prop
3+
import { addDefault } from '@babel/helper-module-imports'
34
import { useCssProp } from '../utils/options'
45

56
const getName = (node, t) => {
@@ -18,27 +19,26 @@ export default t => (path, state) => {
1819
if (!useCssProp(state)) return
1920
if (path.node.name.name !== 'css') return
2021

21-
// Insert require('styled-components') if it doesn't exist yet
22-
const { bindings } = path.findParent(p => p.type === 'Program').scope
23-
if (!state.required) {
24-
if (!bindings.styled) {
25-
state.items.push(
26-
t.importDeclaration(
27-
[t.importDefaultSpecifier(t.identifier('styled'))],
28-
t.stringLiteral('styled-components')
29-
)
30-
)
31-
}
32-
state.required = true
22+
const program = state.file.path
23+
// state.customImportName is passed through from styled-components/macro if it's used
24+
// since the macro also inserts the import
25+
let importName = state.customImportName
26+
// Insert import if it doesn't exist yet
27+
const { bindings } = program.scope
28+
if (!importName || !bindings[importName.name]) {
29+
importName = addDefault(path, 'styled-components', {
30+
nameHint: importName ? importName.name : 'styled',
31+
})
3332
}
33+
if (!state.customImportName) state.customImportName = importName
3434

3535
const elem = path.parentPath
3636
const name = getName(elem.node.name, t)
3737
const id = path.scope.generateUidIdentifier(
3838
'Styled' + name.replace(/^([a-z])/, (match, p1) => p1.toUpperCase())
3939
)
4040

41-
const styled = t.callExpression(t.identifier('styled'), [
41+
const styled = t.callExpression(importName, [
4242
/^[a-z]/.test(name) ? t.stringLiteral(name) : t.identifier(name),
4343
])
4444

@@ -105,9 +105,13 @@ export default t => (path, state) => {
105105
return acc
106106
}, [])
107107

108-
state.items.push(
108+
// Add the tagged template expression and then requeue the newly added node
109+
// so Babel runs over it again
110+
const length = program.node.body.push(
109111
t.variableDeclaration('var', [
110112
t.variableDeclarator(id, t.taggedTemplateExpression(styled, css)),
111113
])
112114
)
115+
116+
program.requeue(program.get('body')[length - 1])
113117
}

test/__snapshots__/index.test.js.snap

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,14 @@ Object.defineProperty(exports, \\"__esModule\\", {
686686
});
687687
exports.default = void 0;
688688
689+
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
690+
689691
var _react = _interopRequireDefault(require(\\"react\\"));
690692
691693
var _Card = _interopRequireDefault(require(\\"../../shared/components/Card\\"));
692694
693695
var _config = _interopRequireDefault(require(\\"../../../config\\"));
694696
695-
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
696-
697697
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
698698
699699
// @flow
@@ -715,6 +715,31 @@ exports.default = _default;
715715
var _StyledDiv = (0, _styledComponents.default)(\\"div\\")(_templateObject());"
716716
`;
717717
718+
exports[`fixtures should transpile css prop add require 1`] = `
719+
"\\"use strict\\";
720+
721+
Object.defineProperty(exports, \\"__esModule\\", {
722+
value: true
723+
});
724+
725+
var _styledComponents = require(\\"styled-components\\");
726+
727+
var _styledComponents2 = _interopRequireDefault(_styledComponents);
728+
729+
var _react = require(\\"react\\");
730+
731+
var _react2 = _interopRequireDefault(_react);
732+
733+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
734+
735+
// @flow
736+
exports.default = () => <_StyledDiv />;
737+
738+
var _StyledDiv = (0, _styledComponents2.default)(\\"div\\")\`
739+
width: 35em;
740+
\`;"
741+
`;
742+
718743
exports[`fixtures should transpile require default 1`] = `
719744
"const styled_default = require(\\"styled-components\\");
720745
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"plugins": [
3+
"transform-es2015-modules-commonjs",
4+
[
5+
"../../../src",
6+
{
7+
"ssr": false,
8+
"displayName": false,
9+
"transpileTemplateLiterals": false,
10+
"minify": false,
11+
"cssProp": true
12+
}
13+
]
14+
]
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @flow
2+
import React from 'react'
3+
4+
export default () => (
5+
<div
6+
css={`
7+
width: 35em;
8+
`}
9+
/>
10+
)

yarn.lock

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@
147147

148148
"@babel/helper-module-imports@^7.0.0":
149149
version "7.0.0"
150-
resolved "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.0.0.tgz#96081b7111e486da4d2cd971ad1a4fe216cc2e3d"
150+
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.0.0.tgz#96081b7111e486da4d2cd971ad1a4fe216cc2e3d"
151+
integrity sha512-aP/hlLq01DWNEiDg4Jn23i+CXxW/owM4WpDLFUbpjxe4NS3BhLVZQ5i7E0ZrxuQ/vwekIeciyamgB1UIYxxM6A==
151152
dependencies:
152153
"@babel/types" "^7.0.0"
153154

@@ -907,6 +908,24 @@ babel-plugin-syntax-object-rest-spread@^6.13.0:
907908
version "6.13.0"
908909
resolved "http://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"
909910

911+
babel-plugin-transform-es2015-modules-commonjs@^6.26.2:
912+
version "6.26.2"
913+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.2.tgz#58a793863a9e7ca870bdc5a881117ffac27db6f3"
914+
integrity sha512-CV9ROOHEdrjcwhIaJNBGMBCodN+1cfkwtM1SbUHmvyy35KGT7fohbpOxkE2uLz1o6odKK2Ck/tz47z+VqQfi9Q==
915+
dependencies:
916+
babel-plugin-transform-strict-mode "^6.24.1"
917+
babel-runtime "^6.26.0"
918+
babel-template "^6.26.0"
919+
babel-types "^6.26.0"
920+
921+
babel-plugin-transform-strict-mode@^6.24.1:
922+
version "6.24.1"
923+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758"
924+
integrity sha1-1fr3qleKZbvlkc9e2uBKDGcCB1g=
925+
dependencies:
926+
babel-runtime "^6.22.0"
927+
babel-types "^6.24.1"
928+
910929
babel-preset-jest@^23.2.0:
911930
version "23.2.0"
912931
resolved "https://registry.npmjs.org/babel-preset-jest/-/babel-preset-jest-23.2.0.tgz#8ec7a03a138f001a1a8fb1e8113652bf1a55da46"
@@ -931,7 +950,18 @@ babel-template@^6.16.0:
931950
babylon "^6.11.0"
932951
lodash "^4.2.0"
933952

934-
babel-traverse@^6.0.0:
953+
babel-template@^6.26.0:
954+
version "6.26.0"
955+
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02"
956+
integrity sha1-3gPi0WOWsGn0bdn/+FIfsaDjXgI=
957+
dependencies:
958+
babel-runtime "^6.26.0"
959+
babel-traverse "^6.26.0"
960+
babel-types "^6.26.0"
961+
babylon "^6.18.0"
962+
lodash "^4.17.4"
963+
964+
babel-traverse@^6.0.0, babel-traverse@^6.26.0:
935965
version "6.26.0"
936966
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee"
937967
dependencies:
@@ -959,7 +989,7 @@ babel-traverse@^6.18.0, babel-traverse@^6.23.0:
959989
invariant "^2.2.0"
960990
lodash "^4.2.0"
961991

962-
babel-types@^6.0.0, babel-types@^6.18.0, babel-types@^6.23.0, babel-types@^6.26.0:
992+
babel-types@^6.0.0, babel-types@^6.18.0, babel-types@^6.23.0, babel-types@^6.24.1, babel-types@^6.26.0:
963993
version "6.26.0"
964994
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497"
965995
dependencies:

0 commit comments

Comments
 (0)