-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Make babel-relay-plugin run before other transforms #714
Conversation
babel-relay-plugin depends transforms the “Relay.QL” TaggedTemplateExpression nodes in the source. If it does not run before the “es2015-template-literals” transform, it will not transform the Relay.QL template literals correctly. This is important, because with Babel 6, you can’t control the plugin order. And so in a case like React Native, where plugins from React Native’s babelrc are loaded before the projects babelrc, it’s impossible to use the Babel Relay Plugin without overriding the entire transform list.
|
||
const taggedTemplateExpressionVisitor = { | ||
TaggedTemplateExpression(path) { | ||
return TaggedTemplateExpression(path, this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this
required?
In other words, why can't we just use:
path.traverse({TaggedTemplateExpression}, state);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the way I understand it is that TaggedTemplateExpression
, when in it's normal plugin form, takes in path
and state
as arguments. state
doesn't get passed in, I don't think, when using path.traverse
. Instead, state is passed in as this
.
Honestly, I'm not a super Babel expert either -- I followed what @thejameskyle did here:
https://phabricator.babeljs.io/rBC0b264aa38af5ba7fb121b216be2fb69eece7217c
@facebook-github-bot import |
Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/1509715672662867/int_phab to review. |
b5510b5
@skevy thanks! |
@josephsavona woot! one step closer. :) |
@skevy Since we ended up having to revert due to a conflict with an internal transform, let's reopen this to continue iterating - i'll follow-up hopefully tomorrow with more feedback about direction. |
@skevy We resolved the conflict with our internal plugin, so we could proceed with reimporting this. However, @DmitrySoshnikov has recently added an option to control plugin ordering in babel. I haven't dug into the React Native OSS setup in a while, but I'm wondering if that same type of configuration would allow the Relay plugin to run before the default RN transforms? |
This may not necessarily be needed anymore actually, with some changes that we've done on the RN side (we're going to use a preset now, which plays into taking advantage of @DmitrySoshnikov's changes). Will test when that gets merged in (should be tomorrow or so) and I'll report back. :-) Thanks @josephsavona! |
@josephsavona so as it turns out...presets still run before things defined in plugins...so really, @DmitrySoshnikov's doesn't help if we want to do this in babelrc:
This will still let The only way we'd be able to take advantage of
And then do this in your .babelrc:
It would work...doesn't seem ideal though and seems kind of confusing. Thoughts? |
@skevy i agree that isn't ideal, but it's not magic and makes it clear to users that the Relay plugin will run first. This means no code changes are required (right?) In that case, would you mind updating the plugin docs to describe how to configure for React Native? :-) |
Yah. I suppose it's fine. I can update the docs when this actually becomes possible. Us non-fb peeps have to wait until this feature is released out into the wild. Gonna check with the Babel folks to get a timeline on when that might be. |
Yeah, if you have presets, and passPerPreset: true,
presets: [
'some-preset',
{plugins: [ ... ]}, // manual preset
'other-preset'
] These presets should run in order. However plugins within each preset are still merged. |
When Babel 6.5.0 gets released (which I think is tomorrow?) I'm going to test all this out and I will report back. :) |
@skevy sounds great, thanks again for following up on this :-) |
@josephsavona I can confirm that doing:
works as we discussed. Sometimes, interestingly, this is not necessary, and you can do the more normal:
However, I think the fact that it works sometimes is just a fluke in how Babel runs the transforms. Therefore, I think from a documentation perspective, we should recommend |
Yeah, I think we would want to change |
Yeah, if you put
Exactly. I think documenting it is a temporary solution. And then we should just make it a default behavior in Babel: this increases maintainability, stability, and actually make it even working. So for users there is no need to mess with an explicit options, and Babel should just work for them. |
note that this is a prereq for #26 |
Any update on this? |
Ok, so the next steps here are:
I'm going to close this PR since we will make a documentation change instead of a code change. |
babel-relay-plugin
depends transforms the “Relay.QL” TaggedTemplateExpression nodes in the source. If it does not run before the “es2015-template-literals” transform, it will not transform the Relay.QL template literals correctly.This is important, because with Babel 6, you can’t control the plugin order. And so in a case like React Native, where plugins from React Native’s babelrc are loaded before the projects babelrc, it’s impossible to use the Babel Relay Plugin without overriding the entire transform list.
Note - I made this change in the babelAdapter, rather than in the plugin code itself, in order to retain Babel 5 compatibility.