Skip to content

Commit 2e19057

Browse files
author
Max Stoiber
committed
Use @babel/helper-module-import
1 parent d4a5469 commit 2e19057

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"@babel/helper-annotate-as-pure": "^7.0.0",
30+
"@babel/helper-module-imports": "^7.0.0",
3031
"babel-plugin-syntax-jsx": "^6.18.0",
3132
"lodash": "^4.17.10"
3233
},

src/visitors/transpileCssProp.js

Lines changed: 10 additions & 21 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) => {
@@ -19,29 +20,17 @@ export default t => (path, state) => {
1920
if (path.node.name.name !== 'css') return
2021

2122
const program = state.file.path
22-
const importName =
23-
state.customImportName || program.scope.generateUidIdentifier('styled')
24-
if (!state.customImportName) state.customImportName = importName
25-
// Insert require('styled-components') if it doesn't exist yet
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
2627
const { bindings } = program.scope
27-
if (!state.required) {
28-
if (!bindings[importName]) {
29-
program.node.body.push(
30-
t.variableDeclaration('var', [
31-
t.variableDeclarator(
32-
importName,
33-
t.memberExpression(
34-
t.callExpression(t.identifier('require'), [
35-
t.stringLiteral('styled-components'),
36-
]),
37-
t.identifier('default')
38-
)
39-
),
40-
])
41-
)
42-
}
43-
state.required = true
28+
if (!importName || !bindings[importName.name]) {
29+
importName = addDefault(path, 'styled-components', {
30+
nameHint: importName ? importName.name : 'styled',
31+
})
4432
}
33+
if (!state.customImportName) state.customImportName = importName
4534

4635
const elem = path.parentPath
4736
const name = getName(elem.node.name, t)

test/__snapshots__/index.test.js.snap

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ function _templateObject4() {
508508
return data;
509509
}
510510
511+
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
512+
513+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
514+
511515
function _templateObject3() {
512516
var data = _taggedTemplateLiteral([\\"\\\\n color: \\", \\";\\\\n \\"]);
513517
@@ -621,47 +625,45 @@ var NestedCompWithDot = function NestedCompWithDot(p) {
621625
return <_StyledButtonGhostNew>H</_StyledButtonGhostNew>;
622626
};
623627
624-
var _styled = require(\\"styled-components\\").default;
628+
var _StyledP = (0, _styledComponents.default)(\\"p\\")(_templateObject4());
625629
626-
var _StyledP = _styled(\\"p\\")(_templateObject4());
630+
var _StyledP2 = (0, _styledComponents.default)(\\"p\\")(_templateObject5());
627631
628-
var _StyledP2 = _styled(\\"p\\")(_templateObject5());
629-
630-
var _StyledP3 = _styled(\\"p\\")(_templateObject6(), function (p) {
632+
var _StyledP3 = (0, _styledComponents.default)(\\"p\\")(_templateObject6(), function (p) {
631633
return p._css;
632634
});
633635
634-
var _StyledP4 = _styled(\\"p\\")(_templateObject7());
636+
var _StyledP4 = (0, _styledComponents.default)(\\"p\\")(_templateObject7());
635637
636-
var _StyledP5 = _styled(\\"p\\")(_templateObject8());
638+
var _StyledP5 = (0, _styledComponents.default)(\\"p\\")(_templateObject8());
637639
638-
var _StyledParagraph = _styled(Paragraph)(_templateObject9());
640+
var _StyledParagraph = (0, _styledComponents.default)(Paragraph)(_templateObject9());
639641
640-
var _StyledP6 = _styled(\\"p\\")(_templateObject10(), function (p) {
642+
var _StyledP6 = (0, _styledComponents.default)(\\"p\\")(_templateObject10(), function (p) {
641643
return p._css2;
642644
});
643645
644-
var _StyledP7 = _styled(\\"p\\")(_templateObject11(), function (p) {
646+
var _StyledP7 = (0, _styledComponents.default)(\\"p\\")(_templateObject11(), function (p) {
645647
return p._css3;
646648
});
647649
648-
var _StyledP8 = _styled(\\"p\\")(_templateObject12(), function (props) {
650+
var _StyledP8 = (0, _styledComponents.default)(\\"p\\")(_templateObject12(), function (props) {
649651
return props.theme.a;
650652
});
651653
652-
var _StyledP9 = _styled(\\"p\\")(_templateObject13(), radius);
654+
var _StyledP9 = (0, _styledComponents.default)(\\"p\\")(_templateObject13(), radius);
653655
654-
var _StyledP10 = _styled(\\"p\\")(_templateObject14(), function (p) {
656+
var _StyledP10 = (0, _styledComponents.default)(\\"p\\")(_templateObject14(), function (p) {
655657
return p._css4;
656658
});
657659
658-
var _StyledP11 = _styled(\\"p\\")(_templateObject15(), function (props) {
660+
var _StyledP11 = (0, _styledComponents.default)(\\"p\\")(_templateObject15(), function (props) {
659661
return props.theme.color;
660662
});
661663
662-
var _StyledButtonGhost = _styled(Button.Ghost)(_templateObject16());
664+
var _StyledButtonGhost = (0, _styledComponents.default)(Button.Ghost)(_templateObject16());
663665
664-
var _StyledButtonGhostNew = _styled(Button.Ghost.New)(_templateObject17());"
666+
var _StyledButtonGhostNew = (0, _styledComponents.default)(Button.Ghost.New)(_templateObject17());"
665667
`;
666668
667669
exports[`fixtures should transpile css prop add import 1`] = `
@@ -684,6 +686,8 @@ Object.defineProperty(exports, \\"__esModule\\", {
684686
});
685687
exports.default = void 0;
686688
689+
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
690+
687691
var _react = _interopRequireDefault(require(\\"react\\"));
688692
689693
var _Card = _interopRequireDefault(require(\\"../../shared/components/Card\\"));
@@ -708,9 +712,7 @@ var _default = function _default() {
708712
709713
exports.default = _default;
710714
711-
var _styled = require(\\"styled-components\\").default;
712-
713-
var _StyledDiv = _styled(\\"div\\")(_templateObject());"
715+
var _StyledDiv = (0, _styledComponents.default)(\\"div\\")(_templateObject());"
714716
`;
715717
716718
exports[`fixtures should transpile require default 1`] = `

yarn.lock

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)