Skip to content

Commit

Permalink
Make babel-relay-plugin run before other transforms
Browse files Browse the repository at this point in the history
Summary:
`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.
Closes #714

Reviewed By: yungsters

Differential Revision: D2862250

fb-gh-sync-id: 1c7342bda81b4dfbb7746ce1edee110ecd7b759b
  • Loading branch information
skevy authored and facebook-github-bot-2 committed Jan 26, 2016
1 parent 81d7f27 commit b5510b5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
33 changes: 31 additions & 2 deletions scripts/babel-relay-plugin/lib/babelAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,37 @@ var path = require('path');

function babelAdapter(Plugin, t, name, visitorsBuilder) {
if (Plugin == null) {
// Babel 6.
return visitorsBuilder(t);
var _ret = (function () {
// Babel 6.

var _visitorsBuilder = visitorsBuilder(t);

var _visitorsBuilder$visitor = _visitorsBuilder.visitor;
var _Program = _visitorsBuilder$visitor.Program;
var _TaggedTemplateExpression = _visitorsBuilder$visitor.TaggedTemplateExpression;

var taggedTemplateExpressionVisitor = {
TaggedTemplateExpression: function TaggedTemplateExpression(path) {
_TaggedTemplateExpression(path, this);
}
};

/**
* Run both transforms on Program to make sure that they run before other plugins.
*/
return {
v: {
visitor: {
Program: function Program(path, state) {
_Program(path, state);
path.traverse(taggedTemplateExpressionVisitor, state);
}
}
}
};
})();

if (typeof _ret === 'object') return _ret.v;
}
// Babel 5.
var legacyT = _extends({}, t, {
Expand Down
20 changes: 19 additions & 1 deletion scripts/babel-relay-plugin/src/babelAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@ function babelAdapter(
): mixed {
if (Plugin == null) {
// Babel 6.
return visitorsBuilder(t);
const {visitor: {Program, TaggedTemplateExpression}} = visitorsBuilder(t);

const taggedTemplateExpressionVisitor = {
TaggedTemplateExpression(path) {
TaggedTemplateExpression(path, this);
}
}

/**
* Run both transforms on Program to make sure that they run before other plugins.
*/
return {
visitor: {
Program(path, state) {
Program(path, state);
path.traverse(taggedTemplateExpressionVisitor, state);
}
}
}
}
// Babel 5.
const legacyT = {
Expand Down

0 comments on commit b5510b5

Please sign in to comment.