New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-htm

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-htm - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

291

dist/babel-plugin-htm.js

@@ -1,152 +0,171 @@

var jsdom = require('jsdom');
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var before = global.document;
global.document = new jsdom.JSDOM().window.document;
var htm = require('htm');
global.document = before;
var currentExpressions;
var htm = _interopDefault(require('htm'));
/**
* @param {Babel} babel
* @param {object} options
* @param {string} [options.pragma=h] JSX/hyperscript pragma.
* @param {string} [options.tag=html] The tagged template "tag" function name to process.
* @param {boolean} [options.monomorphic=false] Output monomorphic inline objects instead of using String literals.
* @param {boolean} [options.useBuiltIns=false] Use the native Object.assign instead of trying to polyfill it.
* @param {boolean} [options.variableArity=true] If `false`, always passes exactly 3 arguments to the pragma function.
*/
function htmBabelPlugin(ref, options) {
var t = ref.types;
if ( options === void 0 ) options = {};
var t = ref.types;
if ( options === void 0 ) options = {};
var pragma = options.pragma === false ? false : dottedIdentifier(options.pragma || 'h');
var inlineVNodes = options.monomorphic || pragma === false;
function dottedIdentifier(keypath) {
var path = keypath.split('.');
var out;
for (var i = 0;i < path.length; i++) {
var ident = propertyName(path[i]);
out = i === 0 ? ident : t.memberExpression(out, ident);
}
return out;
var pragma = options.pragma === false ? false : dottedIdentifier(options.pragma || 'h');
var useBuiltIns = options.useBuiltIns;
var inlineVNodes = options.monomorphic || pragma === false;
function dottedIdentifier(keypath) {
var path = keypath.split('.');
var out;
for (var i = 0; i < path.length; i++) {
var ident = propertyName(path[i]);
out = i === 0 ? ident : t.memberExpression(out, ident);
}
function patternStringToRegExp(str) {
var parts = str.split('/').slice(1);
var end = parts.pop() || '';
return new RegExp(parts.join('/'), end);
return out;
}
function patternStringToRegExp(str) {
var parts = str.split('/').slice(1);
var end = parts.pop() || '';
return new RegExp(parts.join('/'), end);
}
function propertyName(key) {
if (key.match(/(^\d|[^a-z0-9_$])/i)) { return t.stringLiteral(key); }
return t.identifier(key);
}
function stringValue(str) {
if (options.monomorphic) {
return t.objectExpression([t.objectProperty(propertyName('type'), t.numericLiteral(3)), t.objectProperty(propertyName('tag'), t.nullLiteral()), t.objectProperty(propertyName('props'), t.nullLiteral()), t.objectProperty(propertyName('children'), t.nullLiteral()), t.objectProperty(propertyName('text'), t.stringLiteral(str))]);
}
function propertyName(key) {
if (key.match(/(^\d|[^a-z0-9_$])/i))
{ return t.stringLiteral(key); }
return t.identifier(key);
return t.stringLiteral(str);
}
function createVNode(tag, props, children) {
// Never pass children=[[]].
if (children.elements.length === 1 && t.isArrayExpression(children.elements[0]) && children.elements[0].elements.length === 0) {
children = children.elements[0];
}
function stringValue(str) {
if (options.monomorphic) {
return t.objectExpression([t.objectProperty(propertyName('type'), t.numericLiteral(3)),
t.objectProperty(propertyName('tag'), t.nullLiteral()),t.objectProperty(propertyName('props'), t.nullLiteral()),
t.objectProperty(propertyName('children'), t.nullLiteral()),t.objectProperty(propertyName('text'), t.stringLiteral(str))]);
}
return t.stringLiteral(str);
if (inlineVNodes) {
return t.objectExpression([options.monomorphic && t.objectProperty(propertyName('type'), t.numericLiteral(1)), t.objectProperty(propertyName('tag'), tag), t.objectProperty(propertyName('props'), props), t.objectProperty(propertyName('children'), children), options.monomorphic && t.objectProperty(propertyName('text'), t.nullLiteral())].filter(Boolean));
} // Passing `{variableArity:false}` always produces `h(tag, props, children)` - where `children` is always an Array.
// Otherwise, the default is `h(tag, props, ...children)`.
if (options.variableArity !== false) {
children = children.elements;
}
function createVNode(tag, props, children) {
if (inlineVNodes) {
return t.objectExpression([options.monomorphic && t.objectProperty(propertyName('type'), t.numericLiteral(1)),
t.objectProperty(propertyName('tag'), tag),t.objectProperty(propertyName('props'), props),
t.objectProperty(propertyName('children'), children),options.monomorphic && t.objectProperty(propertyName('text'), t.nullLiteral())].filter(Boolean));
}
return t.callExpression(pragma, [tag,props,children]);
return t.callExpression(pragma, [tag, props].concat(children));
}
function spreadNode(args, state) {
// 'Object.assign({}, x)', can be collapsed to 'x'.
if (args.length === 2 && !t.isNode(args[0]) && Object.keys(args[0]).length === 0) {
return propsNode(args[1]);
}
var isVNode = t.isCallExpression;
if (inlineVNodes) {
isVNode = (function (node) {
if (!t.isObjectExpression(node))
{ return false; }
return node.properties[0].value.value !== 3;
});
var helper = useBuiltIns ? dottedIdentifier('Object.assign') : state.addHelper('extends');
return t.callExpression(helper, args.map(propsNode));
}
function propsNode(props) {
if (props == null) { return t.nullLiteral(); }
return t.isNode(props) ? props : t.objectExpression(Object.keys(props).map(function (key) {
var value = props[key];
if (typeof value === 'string') {
value = t.stringLiteral(value);
} else if (typeof value === 'boolean') {
value = t.booleanLiteral(value);
}
return t.objectProperty(propertyName(key), value);
}));
}
function transform(node, state) {
if (node === undefined) { return t.identifier('undefined'); }
if (node == null) { return t.nullLiteral(); }
var tag = node.tag;
var props = node.props;
var children = node.children;
function childMapper(child) {
if (typeof child === 'string') {
return stringValue(child);
}
return t.isNode(child) ? child : transform(child, state);
}
function childMapper(child, index, children) {
if (typeof child === 'string' && child.trim().length === 0 || child == null) {
if (index === 0 || index === children.length - 1)
{ return null; }
}
if (typeof child === 'string' && isVNode(children[index - 1]) && isVNode(children[index + 1])) {
child = child.trim();
}
if (typeof child === 'string') {
var matches = child.match(/\$\$\$_h_\[(\d+)\]/);
if (matches)
{ return currentExpressions[matches[1]]; }
return stringValue(child);
}
return child;
var newTag = typeof tag === 'string' ? t.stringLiteral(tag) : tag;
var newProps = !Array.isArray(props) ? propsNode(props) : spreadNode(props, state);
var newChildren = t.arrayExpression(children.map(childMapper));
return createVNode(newTag, newProps, newChildren);
}
function h(tag, props) {
var children = [], len = arguments.length - 2;
while ( len-- > 0 ) children[ len ] = arguments[ len + 2 ];
return {
tag: tag,
props: props,
children: children
};
}
var html = htm.bind(h);
function treeify(statics, expr) {
var assign = Object.assign;
try {
Object.assign = function () {
var objs = [], len = arguments.length;
while ( len-- ) objs[ len ] = arguments[ len ];
return objs;
};
return html.apply(void 0, [ statics ].concat( expr ));
} finally {
Object.assign = assign;
}
function h(tag, props) {
var arguments$1 = arguments;
} // The tagged template tag function name we're looking for.
// This is static because it's generally assigned via htm.bind(h),
// which could be imported from elsewhere, making tracking impossible.
if (typeof tag === 'string') {
var matches = tag.match(/\$\$\$_h_\[(\d+)\]/);
if (matches)
{ tag = currentExpressions[matches[1]]; }
else
{ tag = t.stringLiteral(tag); }
var htmlName = options.tag || 'html';
return {
name: 'htm',
visitor: {
TaggedTemplateExpression: function TaggedTemplateExpression(path, state) {
var tag = path.node.tag.name;
if (htmlName[0] === '/' ? patternStringToRegExp(htmlName).test(tag) : tag === htmlName) {
var statics = path.node.quasi.quasis.map(function (e) { return e.value.raw; });
var expr = path.node.quasi.expressions;
var tree = treeify(statics, expr);
path.replaceWith(transform(tree, state));
}
var propsNode = t.objectExpression(Object.keys(props).map(function (key) {
var value = props[key];
if (typeof value === 'string') {
var tokenizer = /\$\$\$_h_\[(\d+)\]/g;
var token, lhs, root, index = 0, lastIndex = 0;
var append = function (expr) {
if (lhs)
{ expr = t.binaryExpression('+', lhs, expr); }
root = (lhs = expr);
};
while (token = tokenizer.exec(value)) {
append(t.stringLiteral(value.substring(index, token.index)));
append(currentExpressions[token[1]]);
index = token.index;
lastIndex = tokenizer.lastIndex;
}
if (lastIndex < value.length) {
append(t.stringLiteral(value.substring(lastIndex)));
}
value = root;
} else if (typeof value === 'boolean') {
value = t.booleanLiteral(value);
}
return t.objectProperty(propertyName(key), value);
}));
var children = [];
if (arguments.length > 2) {
var stack = [];
for (var i = arguments.length;i-- > 2; )
{ stack.push(arguments$1[i]); }
while (stack.length) {
var child = stack.pop();
if (Array.isArray(child)) {
for (var i$1 = child.length;i$1--; )
{ stack.push(child[i$1]); }
} else if (child != null) {
children.push(child);
}
}
children = children.map(childMapper).filter(Boolean);
}
children = t.arrayExpression(children);
return createVNode(tag, propsNode, children);
}
}
var html = htm.bind(h);
var htmlName = options.tag || 'html';
return {
name: 'htm',
visitor: {
TaggedTemplateExpression: function TaggedTemplateExpression(path) {
var tag = path.node.tag.name;
if (htmlName[0] === '/' ? patternStringToRegExp(htmlName).test(tag) : tag === htmlName) {
var statics = path.node.quasi.quasis.map(function (e) { return e.value.raw; });
var expr = path.node.quasi.expressions;
currentExpressions = expr;
path.replaceWith(html.apply(void 0, [ statics ].concat( expr.map(function (p, i) { return ("$$$_h_[" + i + "]"); }) )));
}
}
}
};
};
}
module.exports = htmBabelPlugin;
{
"name": "babel-plugin-htm",
"version": "1.0.1",
"version": "2.0.0",
"description": "Babel plugin to compile htm's Tagged Template syntax to hyperscript or inline VNodes.",

@@ -12,4 +12,3 @@ "main": "dist/babel-plugin-htm.js",

"files": [
"dist",
"index.mjs"
"dist"
],

@@ -32,10 +31,9 @@ "repository": "developit/htm",

"license": "Apache-2.0",
"homepage": "https://github.com/developit/htm/packages/babel-plugin-htm",
"homepage": "https://github.com/developit/htm/tree/master/packages/babel-plugin-htm",
"dependencies": {
"htm": "^1.0.0",
"jsdom": "^11.12.0"
"htm": "^2.0.0"
},
"devDependencies": {
"microbundle": "^0.6.0"
"microbundle": "^0.8.3"
}
}

@@ -32,2 +32,29 @@ # `babel-plugin-htm`

### `tag=html`
By default, `babel-plugin-htm` will process all Tagged Templates with a tag function named `html`. To use a different name, use the `tag` option in your Babel configuration:
```js
{"plugins":[
["babel-plugin-htm", {
"tag": "myCustomHtmlFunction"
}]
]}
```
### `useBuiltIns=false`
`babel-plugin-htm` transforms prop spreads (`<a ...${b}>`) into `Object.assign()` calls. For browser support reasons, Babel's standard `_extends` helper is used by default. To use native `Object.assign` directly, pass `{useBuiltIns:true}`.
### `variableArity=true`
By default, `babel-plugin-htm` transpiles to the same output as JSX would, which assumes a target function of the form `h(type, props, ...children)`. If, for the purposes of optimization or simplification, you would like all calls to `h()` to be passed exactly 3 arguments, specify `{variableArity:false}` in your Babel config:
```js
html`<div />` // h('div', null, [])
html`<div a />` // h('div', { a: true }, [])
html`<div>b</div>` // h('div', null, ['b'])
html`<div a>b</div>` // h('div', { a: true }, ['b'])
```
### `pragma=false` _(experimental)_

@@ -53,3 +80,3 @@

{ type: 1, tag:"div", props:{ id: "foo" }, text: null, children:[
{ type: 3, tag: null, props: null, text "hello ", children: null },
{ type: 3, tag: null, props: null, text: "hello ", children: null },
you

@@ -56,0 +83,0 @@ ] }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc