Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix injection location of generated components via css prop #228

Merged
merged 1 commit into from
Jun 27, 2019

Conversation

quantizor
Copy link
Collaborator

@quantizor quantizor commented Jun 24, 2019

inject the generated components before the tree they're being used in rather than to the end of the file, this fixes situations where a bundler might wrap a component in an IIFE and our component generated from the css prop hasn't been initialized yet (fixes #221, #203)

program.node.body.push(
let parent = elem

while (!t.isProgram(parent.parentPath)) {
Copy link
Collaborator Author

@quantizor quantizor Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this should be a safe thing to do since before we were basically doing the same thing but a file append instead of a sibling prepend to the node tree that we're currently working on.

@quantizor quantizor changed the title fix ssr transpilation of object styles in the css prop fix a few issues Jun 25, 2019
@quantizor
Copy link
Collaborator Author

quantizor commented Jun 25, 2019

This is available to test under [email protected] if anyone wants to.

Copy link
Member

@mxstbr mxstbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@quantizor
Copy link
Collaborator Author

Waiting for codesandbox to work again so I can test these changes against the babel macro

@quantizor
Copy link
Collaborator Author

I'm going to extract out the object css prop and @bpierre's changes into a separate branch, I'm getting weird errors building projects after the insert position changes and it needs more bake time

@quantizor quantizor force-pushed the css-prop-object-syntax branch from 9e4e8bc to aa02769 Compare June 27, 2019 03:25
@quantizor quantizor changed the title fix a few issues fix injection location of generated components via css prop Jun 27, 2019
@quantizor
Copy link
Collaborator Author

Tested against a babel macro project and a fairly hefty nextjs project with [email protected] and it looks good.

generally speaking, the component should be injected just before
it's needed, unless it's a styled() HOC and referring to another
component in scope, in which case we need to find that component
and inject immediately after it
@quantizor quantizor force-pushed the css-prop-object-syntax branch from aa02769 to 5d3f1e8 Compare June 27, 2019 04:03
@quantizor
Copy link
Collaborator Author

cleaned up the PR - all the content is the same as before just compacted and rebased

@UXDart
Copy link

UXDart commented Sep 18, 2021

I'm having this problem when using rollup to bundle, everything seems to be working but the generated code has the styledDiv creation after and outside the React.createElement, so I have error saying createElement received undefined. I assume this is because rollup ast must be different? what can I do? TIA!

@UXDart
Copy link

UXDart commented Sep 18, 2021

it seems this is the code that sets the injector, if the injector is null, then it adds to the program body, but at the end... for some reason

`
if (TAG_NAME_REGEXP.test(name)) {
styled = t.callExpression(importName, [t.stringLiteral(name)]);
} else {
styled = t.callExpression(importName, [nameExpression]);

if (bindings[name] && !t.isImportDeclaration(bindings[name].path.parent)) {
  injector = nodeToInsert => (t.isVariableDeclaration(bindings[name].path.parent) ? bindings[name].path.parentPath : bindings[name].path).insertAfter(nodeToInsert);
}

}
`

how can I get into the else part of the if? not sure what name should be and where... thx

@UXDart
Copy link

UXDart commented Sep 18, 2021

oh... nvm just found why this happens, I was using css prop on the main jsx file, I used it now inside a second file, and it works. it seems if css prop is used on main file, it is injecting the new var outside the main scope. thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants