Skip to content

Commit

Permalink
Merge pull request #334 from styled-components/297-reimplement
Browse files Browse the repository at this point in the history
move injected styled component to end of file
  • Loading branch information
quantizor authored Jun 29, 2021
2 parents 6a355b5 + 5bc989b commit 433a6ba
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 217 deletions.
4 changes: 2 additions & 2 deletions src/visitors/transpileCssProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ export default t => (path, state) => {
if (!injector) {
let parent = elem

while (!t.isProgram(parent.parentPath)) {
while (!t.isProgram(parent)) {
parent = parent.parentPath
}

injector = nodeToInsert => parent.insertBefore(nodeToInsert)
injector = nodeToInsert => parent.pushContainer('body', nodeToInsert)
}

injector(
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/css-declared-after-component/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
10 changes: 10 additions & 0 deletions test/fixtures/css-declared-after-component/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"
import { css } from "styled-components"

export default function Example() {
return <div css={someCss}>oops</div>
}

const someCss = css`
color: red;
`
12 changes: 12 additions & 0 deletions test/fixtures/css-declared-after-component/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import _styled from "styled-components";
import React from "react";
import { css } from "styled-components";
export default function Example() {
return <_StyledDiv>oops</_StyledDiv>;
}
const someCss = css(["color:red;"]);

var _StyledDiv = _styled("div").withConfig({
displayName: "code___StyledDiv",
componentId: "sc-7mydya-0"
})(["", ""], someCss);
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&

const someCss = (0, _styledComponents.css)([" background:purple;"]);

const App1 = () => {
return <_StyledIcons />;
};

const App2 = () => {
return <_StyledIconsFoo />;
};

const App3 = () => {
return <_StyledIconsFooBar />;
};

var _StyledIcons = (0, _styledComponents.default)(_icons.default).withConfig({
displayName: "code___StyledIcons",
componentId: "sc-1wxehft-0"
})(["", ""], someCss);

const App1 = () => {
return <_StyledIcons />;
};

var _StyledIconsFoo = (0, _styledComponents.default)(_icons.default.Foo).withConfig({
displayName: "code___StyledIconsFoo",
componentId: "sc-1wxehft-1"
})(["", ""], someCss);

const App2 = () => {
return <_StyledIconsFoo />;
};

var _StyledIconsFooBar = (0, _styledComponents.default)(_icons.default.Foo.Bar).withConfig({
displayName: "code___StyledIconsFooBar",
componentId: "sc-1wxehft-2"
})(["", ""], someCss);

const App3 = () => {
return <_StyledIconsFooBar />;
};
4 changes: 2 additions & 2 deletions test/fixtures/transpile-css-prop-add-import/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var _StyledDiv = (0, _styledComponents["default"])("div")(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 35em;\n "])));

var _default = function _default() {
return <_StyledDiv>
<_Card.default>
Expand All @@ -35,3 +33,5 @@ var _default = function _default() {
};

exports["default"] = _default;

var _StyledDiv = (0, _styledComponents["default"])("div")(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 35em;\n "])));
4 changes: 2 additions & 2 deletions test/fixtures/transpile-css-prop-add-require/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var _StyledDiv = (0, _styledComponents["default"])("div")(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 35em;\n "])));

var _default = function _default() {
return <_StyledDiv />;
};

exports["default"] = _default;

var _StyledDiv = (0, _styledComponents["default"])("div")(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 35em;\n "])));
Loading

0 comments on commit 433a6ba

Please sign in to comment.