diff --git a/package.json b/package.json index 9fea0258eb..20e891f02c 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "babel-plugin-property-literals": "^1.0.1", "babel-plugin-proto-to-assign": "^1.0.3", "babel-plugin-react-constant-elements": "^1.0.3", + "babel-plugin-react-display-name": "^1.0.3", "babel-plugin-remove-console": "^1.0.1", "babel-plugin-remove-debugger": "^1.0.1", "babel-plugin-runtime": "^1.0.5", diff --git a/src/babel/transformation/helpers/build-react-transformer.js b/src/babel/transformation/helpers/build-react-transformer.js index 92c0052e1f..37d20b4cff 100644 --- a/src/babel/transformation/helpers/build-react-transformer.js +++ b/src/babel/transformation/helpers/build-react-transformer.js @@ -155,56 +155,5 @@ export default function (opts) { } }; - // display names - - var addDisplayName = function (id, call) { - var props = call.arguments[0].properties; - var safe = true; - - for (var i = 0; i < props.length; i++) { - var prop = props[i]; - var key = t.toComputedKey(prop); - if (t.isLiteral(key, { value: "displayName" })) { - safe = false; - break; - } - } - - if (safe) { - props.unshift(t.property("init", t.identifier("displayName"), t.literal(id))); - } - }; - - visitor.ExportDefaultDeclaration = function (node, parent, scope, file) { - if (react.isCreateClass(node.declaration)) { - addDisplayName(file.opts.basename, node.declaration); - } - }; - - visitor.AssignmentExpression = - visitor.Property = - visitor.VariableDeclarator = function (node) { - var left, right; - - if (t.isAssignmentExpression(node)) { - left = node.left; - right = node.right; - } else if (t.isProperty(node)) { - left = node.key; - right = node.value; - } else if (t.isVariableDeclarator(node)) { - left = node.id; - right = node.init; - } - - if (t.isMemberExpression(left)) { - left = left.property; - } - - if (t.isIdentifier(left) && react.isCreateClass(right)) { - addDisplayName(left.name, right); - } - }; - return visitor; } diff --git a/src/babel/transformation/helpers/react.js b/src/babel/transformation/helpers/react.js index 38288627a8..55eb56d694 100644 --- a/src/babel/transformation/helpers/react.js +++ b/src/babel/transformation/helpers/react.js @@ -1,24 +1,5 @@ import * as t from "../../types"; -var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass"); - -export function isCreateClass(node) { - if (!node || !t.isCallExpression(node)) return false; - - // not React.createClass call member object - if (!isCreateClassCallExpression(node.callee)) return false; - - // no call arguments - var args = node.arguments; - if (args.length !== 1) return false; - - // first node arg is not an object - var first = args[0]; - if (!t.isObjectExpression(first)) return false; - - return true; -} - export var isReactComponent = t.buildMatchMemberExpression("React.Component"); export function isCompatTag(tagName) { diff --git a/src/babel/transformation/pipeline.js b/src/babel/transformation/pipeline.js index e929c1e7fe..ae74d9d0bc 100644 --- a/src/babel/transformation/pipeline.js +++ b/src/babel/transformation/pipeline.js @@ -33,6 +33,10 @@ export default class Pipeline { plugin = PluginManager.memoisePluginContainer(plugin); plugin.key = key; plugin.metadata.optional = true; + + if (key === "react.displayName") { + plugin.metadata.optional = false; + } } else { plugin = new Plugin(key, plugin); } diff --git a/src/babel/transformation/transformers/index.js b/src/babel/transformation/transformers/index.js index e4f81d8102..1cf1b7e953 100644 --- a/src/babel/transformation/transformers/index.js +++ b/src/babel/transformation/transformers/index.js @@ -13,6 +13,7 @@ export default { "utility.inlineEnvironmentVariables": require("babel-plugin-inline-environment-variables"), "minification.deadCodeElimination": require("babel-plugin-dead-code-elimination"), _modules: require("./internal/modules"), + "react.displayName": require("babel-plugin-react-display-name"), "spec.functionName": require("./spec/function-name"), "es6.spec.templateLiterals": require("./es6/spec.template-literals"), "es6.templateLiterals": require("./es6/template-literals"), diff --git a/test/core/fixtures/transformation/optimisation.react.constant-elements/options.json b/test/core/fixtures/transformation/optimisation.react.constant-elements/options.json index 000e693290..68adf800b3 100644 --- a/test/core/fixtures/transformation/optimisation.react.constant-elements/options.json +++ b/test/core/fixtures/transformation/optimisation.react.constant-elements/options.json @@ -1,5 +1,5 @@ { "noCheckAst": true, "optional": ["optimisation.react.constantElements"], - "blacklist": ["react"] + "blacklist": ["react", "react.displayName"] }