babel-plugin-react-hyperscript
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -14,6 +14,12 @@ 'use strict'; | ||
return { | ||
name: "babel-plugin-react-hyperscript", | ||
name: 'babel-plugin-react-hyperscript', | ||
visitor: { | ||
CallExpression: function CallExpression(path) { | ||
CallExpression: function CallExpression(path, _ref) { | ||
var opts = _ref.opts; | ||
if (path.node.callee.name === 'h') { | ||
var pragma = opts.pragma; | ||
var _path$node$arguments = _toArray(path.node.arguments), | ||
@@ -24,3 +30,7 @@ element = _path$node$arguments[0], | ||
path.replaceWith(h(t, element, props, children)); | ||
if (pragma === 'h') { | ||
path.node.arguments = h(t, element, props, children); | ||
} else { | ||
path.replaceWith(t.CallExpression(parsePragmaMemd(pragma || 'React.createElement'), h(t, element, props, children))); | ||
} | ||
} | ||
@@ -32,2 +42,4 @@ } | ||
var _babylon = require('babylon'); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
@@ -37,2 +49,8 @@ | ||
function parsePragma(pragma) { | ||
return (0, _babylon.parse)(pragma).program.body[0].expression; | ||
} | ||
var parsePragmaMemd = memoize(parsePragma); | ||
function h(t, componentOrTag, properties, children) { | ||
@@ -59,6 +77,6 @@ var _parseTag = parseTag(t, componentOrTag), | ||
attrEntries.forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
key = _ref2[0], | ||
value = _ref2[1]; | ||
attrEntries.forEach(function (_ref2) { | ||
var _ref3 = _slicedToArray(_ref2, 2), | ||
key = _ref3[0], | ||
value = _ref3[1]; | ||
@@ -81,6 +99,6 @@ if (key === 'className' && hasClassName) { | ||
children.unshift(properties); | ||
properties = t.ObjectExpression(attrEntries.map(function (_ref3) { | ||
var _ref4 = _slicedToArray(_ref3, 2), | ||
key = _ref4[0], | ||
value = _ref4[1]; | ||
properties = t.ObjectExpression(attrEntries.map(function (_ref4) { | ||
var _ref5 = _slicedToArray(_ref4, 2), | ||
key = _ref5[0], | ||
value = _ref5[1]; | ||
@@ -116,6 +134,6 @@ return t.ObjectProperty(t.Identifier(key), t.StringLiteral(value)); | ||
return [prop.key.name, prop.value.value]; | ||
}).forEach(function (_ref5) { | ||
var _ref6 = _slicedToArray(_ref5, 2), | ||
name = _ref6[0], | ||
value = _ref6[1]; | ||
}).forEach(function (_ref6) { | ||
var _ref7 = _slicedToArray(_ref6, 2), | ||
name = _ref7[0], | ||
value = _ref7[1]; | ||
@@ -133,6 +151,6 @@ properties.properties.push(t.ObjectProperty(t.StringLiteral('data-' + name), t.StringLiteral(value))); | ||
return [prop.key.name, prop.value.value]; | ||
}).forEach(function (_ref7) { | ||
var _ref8 = _slicedToArray(_ref7, 2), | ||
name = _ref8[0], | ||
value = _ref8[1]; | ||
}).forEach(function (_ref8) { | ||
var _ref9 = _slicedToArray(_ref8, 2), | ||
name = _ref9[0], | ||
value = _ref9[1]; | ||
@@ -182,3 +200,3 @@ properties.properties.push(t.ObjectProperty(t.Identifier(name), t.StringLiteral(value))); | ||
return t.CallExpression(t.MemberExpression(t.Identifier('React'), t.Identifier('createElement')), args); | ||
return args; | ||
} | ||
@@ -251,2 +269,16 @@ | ||
return tagName ? [t.StringLiteral(tagName.toLowerCase()), props] : [t.StringLiteral('div'), props]; | ||
} | ||
function memoize(fn) { | ||
var mem = void 0; | ||
return function () { | ||
if (mem) { | ||
return mem; | ||
} else { | ||
mem = fn.apply(undefined, arguments); | ||
return mem; | ||
} | ||
}; | ||
} |
{ | ||
"name": "babel-plugin-react-hyperscript", | ||
"version": "0.0.1", | ||
"description": "HyperScript components syntax for React without runtime overhead", | ||
"version": "0.0.2", | ||
"description": "HyperScript syntax for React components without runtime overhead", | ||
"repository": "roman01la/babel-plugin-react-hyperscript", | ||
@@ -27,4 +27,5 @@ "main": "lib/index.js", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babylon": "^6.16.1", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -34,2 +34,13 @@ # babel-plugin-react-hyperscript | ||
## Plugin options | ||
**pragma** | ||
By default calls to `h` will be replaced with `React.createElement`, but you can override this with a custom pragma (including `h` itself when used with [Preact](https://github.com/developit/preact)). | ||
```js | ||
{ | ||
"plugins": ["react-hyperscript", { "pragma": "yo" }] | ||
} | ||
``` | ||
## Usage | ||
@@ -36,0 +47,0 @@ |
@@ -28,5 +28,10 @@ React.createElement('h1'); | ||
React.createElement('div', { dataset: { foo: 'bar', bar: 'oops' } }); | ||
React.createElement('div', { | ||
'data-foo': 'bar', | ||
'data-bar': 'oops' | ||
}); | ||
React.createElement('div', { attributes: { title: 'foo' } }); | ||
React.createElement('div', { | ||
title: 'foo' | ||
}); | ||
@@ -39,4 +44,4 @@ React.createElement(Component); | ||
React.createElement(Component, { children: [React.createElement('span', null, 'A child')] }); | ||
React.createElement(Component, null, React.createElement('span', null, 'A child')); | ||
React.createElement(FunctionComponent, null, React.createElement('span', null, 'A child')); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15786
14
341
70
4