babel-plugin-transform-react-create-element
Advanced tools
Comparing version
{ | ||
"name": "babel-plugin-transform-react-create-element", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Shorten JSX React.createElement calls using a local variable.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -39,2 +39,4 @@ # babel-plugin-transform-react-create-element | ||
Install via `npm install -D babel-plugin-transform-react-create-element` or `yarn add -D babel-plugin-transform-react-create-element`. | ||
In your `.babelrc`: | ||
@@ -41,0 +43,0 @@ |
@@ -5,8 +5,3 @@ "use strict"; | ||
const template_1 = tslib_1.__importDefault(require("@babel/template")); | ||
const PRAGMA_ELEM_NAME = "createElement"; | ||
const LOCAL_VARIABLE_TEMPLATE = template_1.default(` | ||
const %%pragma%% = /*#__PURE__*/ React.createElement; | ||
`, { | ||
preserveComments: true, | ||
}); | ||
const LOCAL_VARIABLE_TEMPLATE = template_1.default("const %%localIdentifier%% = /*#__PURE__*/ React.createElement;", { preserveComments: true }); | ||
const getReactImport = (path) => { | ||
@@ -18,3 +13,4 @@ const binding = path.scope.getBinding(path.node.name); | ||
const bindingPath = binding.path; | ||
if (bindingPath.isImportDefaultSpecifier()) { | ||
if (bindingPath.isImportDefaultSpecifier() || | ||
bindingPath.isImportNamespaceSpecifier()) { | ||
const parentPath = bindingPath.parentPath; | ||
@@ -39,3 +35,3 @@ if (parentPath.isImportDeclaration() && | ||
}; | ||
const getCreateElementAndImport = (path) => { | ||
const getCreateElementCallAndReactImport = (path) => { | ||
const callee = path.get("callee"); | ||
@@ -50,2 +46,5 @@ if (!callee.isMemberExpression()) { | ||
const object = callee.get("object"); | ||
if (!object.isIdentifier()) { | ||
return null; | ||
} | ||
const reactImport = getReactImport(object); | ||
@@ -58,5 +57,5 @@ if (!reactImport) { | ||
const insertLocalVariable = (importSite) => { | ||
const uniqueIdent = importSite.scope.generateUidIdentifier(PRAGMA_ELEM_NAME); | ||
const uniqueIdent = importSite.scope.generateUidIdentifier("createElement"); | ||
const variableDeclaration = LOCAL_VARIABLE_TEMPLATE({ | ||
pragma: uniqueIdent, | ||
localIdentifier: uniqueIdent, | ||
}); | ||
@@ -71,3 +70,3 @@ const [node] = importSite.parentPath.insertAfter(variableDeclaration); | ||
CallExpression(path, state) { | ||
const data = getCreateElementAndImport(path); | ||
const data = getCreateElementCallAndReactImport(path); | ||
if (!data) { | ||
@@ -74,0 +73,0 @@ return; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
9109
3.31%66
3.13%87
-1.14%