Skip to content

Commit 2021805

Browse files
committed
0.6.0
1 parent 81e8711 commit 2021805

File tree

10 files changed

+324
-3
lines changed

10 files changed

+324
-3
lines changed

dist/htm.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/htm.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var e={},t=document.createElement("template"),r=/(\$_h\[\d+\])/g;function n(e,t){return t.toUpperCase()}function a(e,t){var n=e.match(r),a=JSON.stringify(e);if(null!=n){if(n[0]===e)return e;a=a.replace(r,'"'+t+"$1"+t+'"').replace(/"[+,]"/g,""),","===t&&(a="["+a+"]")}return a}export default function(r){return(e[r]||(e[r]=function(e){for(var r=e[0],i=1;i<e.length;)r+="$_h["+i+"]"+e[i++];return t.innerHTML=r.replace(/<(?:(\/)\/|(\/?)(\$_h\[\d+\]))/g,"<$1$2c c@=$3").replace(/<([\w:-]+)(\s[^<>]*?)?\/>/gi,"<$1$2></$1>").trim(),Function("h","$_h","return "+function e(t){if(1!==t.nodeType)return 3===t.nodeType&&t.data?a(t.data,","):"null";for(var r='"'+t.localName+'"',i="{",u="",c="}",l=0;l<t.attributes.length;l++){var o=t.attributes[l],f=o.name,s=o.value;"c@"!=f?"..."!==f.substring(0,3)?(i+=u+'"'+f.replace(/:(\w)/g,n)+'":'+(!s||a(s,"+")),u=","):(c="})",i="Object.assign("+i+"},"+f.substring(3)+",{",u=""):r=s}i="h("+r+","+i+c;for(var g=t.firstChild;g;)i+=","+e(g),g=g.nextSibling;return i+")"}((t.content||t).firstChild))}(r)))(this,arguments)};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "htm",
3-
"version": "0.5.2",
3+
"version": "0.6.0",
44
"description": "The Tagged Template syntax for Virtual DOM. Only browser-compatible syntax.",
55
"main": "dist/htm.js",
66
"umd:main": "dist/htm.js",
@@ -11,7 +11,7 @@
1111
"build:preact": "microbundle src/integrations/preact/index.mjs -o preact/index.js -f es,umd --no-sourcemap --target web && cp src/integrations/preact/{index.d.ts,package.json} preact/",
1212
"build:babel": "cd packages/babel-plugin-htm && npm run build",
1313
"test": "eslint src/**/*.mjs test && jest test",
14-
"release": "npm run build && git checkout --detach && git add -f babel dist preact && git commit -am \"$npm_package_version\" && git tag $npm_package_version && git push --tags && git checkout master"
14+
"release": "npm run build && git checkout --detach && git add -f packages dist preact && git commit -am \"$npm_package_version\" && git tag $npm_package_version && git push --tags && git checkout master"
1515
},
1616
"files": [
1717
"dist",
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var jsdom = require('jsdom');
2+
3+
var before = global.document;
4+
global.document = new jsdom.JSDOM().window.document;
5+
var htm = require('htm');
6+
global.document = before;
7+
var currentExpressions;
8+
function htmBabelPlugin(ref, options) {
9+
var t = ref.types;
10+
if ( options === void 0 ) options = {};
11+
12+
var pragma = options.pragma === false ? false : dottedIdentifier(options.pragma || 'h');
13+
var inlineVNodes = options.monomorphic || pragma === false;
14+
function dottedIdentifier(keypath) {
15+
var path = keypath.split('.');
16+
var out;
17+
for (var i = 0;i < path.length; i++) {
18+
var ident = propertyName(path[i]);
19+
out = i === 0 ? ident : t.memberExpression(out, ident);
20+
}
21+
return out;
22+
}
23+
24+
function patternStringToRegExp(str) {
25+
var parts = str.split('/').slice(1);
26+
var end = parts.pop() || '';
27+
return new RegExp(parts.join('/'), end);
28+
}
29+
30+
function propertyName(key) {
31+
if (key.match(/(^\d|[^a-z0-9_$])/i))
32+
{ return t.stringLiteral(key); }
33+
return t.identifier(key);
34+
}
35+
36+
function stringValue(str) {
37+
if (options.monomorphic) {
38+
return t.objectExpression([t.objectProperty(propertyName('type'), t.numericLiteral(3)),
39+
t.objectProperty(propertyName('tag'), t.nullLiteral()),t.objectProperty(propertyName('props'), t.nullLiteral()),
40+
t.objectProperty(propertyName('children'), t.nullLiteral()),t.objectProperty(propertyName('text'), t.stringLiteral(str))]);
41+
}
42+
return t.stringLiteral(str);
43+
}
44+
45+
function createVNode(tag, props, children) {
46+
if (inlineVNodes) {
47+
return t.objectExpression([options.monomorphic && t.objectProperty(propertyName('type'), t.numericLiteral(1)),
48+
t.objectProperty(propertyName('tag'), tag),t.objectProperty(propertyName('props'), props),
49+
t.objectProperty(propertyName('children'), children),options.monomorphic && t.objectProperty(propertyName('text'), t.nullLiteral())].filter(Boolean));
50+
}
51+
return t.callExpression(pragma, [tag,props,children]);
52+
}
53+
54+
var isVNode = t.isCallExpression;
55+
if (inlineVNodes) {
56+
isVNode = (function (node) {
57+
if (!t.isObjectExpression(node))
58+
{ return false; }
59+
return node.properties[0].value.value !== 3;
60+
});
61+
}
62+
function childMapper(child, index, children) {
63+
if (typeof child === 'string' && child.trim().length === 0 || child == null) {
64+
if (index === 0 || index === children.length - 1)
65+
{ return null; }
66+
}
67+
if (typeof child === 'string' && isVNode(children[index - 1]) && isVNode(children[index + 1])) {
68+
child = child.trim();
69+
}
70+
if (typeof child === 'string') {
71+
var matches = child.match(/\$\$\$_h_\[(\d+)\]/);
72+
if (matches)
73+
{ return currentExpressions[matches[1]]; }
74+
return stringValue(child);
75+
}
76+
return child;
77+
}
78+
79+
function h(tag, props) {
80+
var arguments$1 = arguments;
81+
82+
if (typeof tag === 'string') {
83+
var matches = tag.match(/\$\$\$_h_\[(\d+)\]/);
84+
if (matches)
85+
{ tag = currentExpressions[matches[1]]; }
86+
else
87+
{ tag = t.stringLiteral(tag); }
88+
}
89+
var propsNode = t.objectExpression(Object.keys(props).map(function (key) {
90+
var value = props[key];
91+
if (typeof value === 'string') {
92+
var tokenizer = /\$\$\$_h_\[(\d+)\]/g;
93+
var token, lhs, root, index = 0, lastIndex = 0;
94+
var append = function (expr) {
95+
if (lhs)
96+
{ expr = t.binaryExpression('+', lhs, expr); }
97+
root = (lhs = expr);
98+
};
99+
while (token = tokenizer.exec(value)) {
100+
append(t.stringLiteral(value.substring(index, token.index)));
101+
append(currentExpressions[token[1]]);
102+
index = token.index;
103+
lastIndex = tokenizer.lastIndex;
104+
}
105+
if (lastIndex < value.length) {
106+
append(t.stringLiteral(value.substring(lastIndex)));
107+
}
108+
value = root;
109+
} else if (typeof value === 'boolean') {
110+
value = t.booleanLiteral(value);
111+
}
112+
return t.objectProperty(propertyName(key), value);
113+
}));
114+
var children = [];
115+
if (arguments.length > 2) {
116+
var stack = [];
117+
for (var i = arguments.length;i-- > 2; )
118+
{ stack.push(arguments$1[i]); }
119+
while (stack.length) {
120+
var child = stack.pop();
121+
if (Array.isArray(child)) {
122+
for (var i$1 = child.length;i$1--; )
123+
{ stack.push(child[i$1]); }
124+
} else if (child != null) {
125+
children.push(child);
126+
}
127+
}
128+
children = children.map(childMapper).filter(Boolean);
129+
}
130+
children = t.arrayExpression(children);
131+
return createVNode(tag, propsNode, children);
132+
}
133+
134+
var html = htm.bind(h);
135+
var htmlName = options.tag || 'html';
136+
return {
137+
name: 'htm',
138+
visitor: {
139+
TaggedTemplateExpression: function TaggedTemplateExpression(path) {
140+
var tag = path.node.tag.name;
141+
if (htmlName[0] === '/' ? patternStringToRegExp(htmlName).test(tag) : tag === htmlName) {
142+
var statics = path.node.quasi.quasis.map(function (e) { return e.value.raw; });
143+
var expr = path.node.quasi.expressions;
144+
currentExpressions = expr;
145+
path.replaceWith(html.apply(void 0, [ statics ].concat( expr.map(function (p, i) { return ("$$$_h_[" + i + "]"); }) )));
146+
}
147+
}
148+
}
149+
};
150+
}
151+
152+
module.exports = htmBabelPlugin;

packages/babel-plugin-htm/dist/babel-plugin-htm.mjs

Lines changed: 156 additions & 0 deletions
Large diffs are not rendered by default.

packages/babel-plugin-htm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-htm",
3-
"version": "0.5.2",
3+
"version": "0.6.0",
44
"description": "Babel plugin to compile htm's Tagged Template syntax to hyperscript or inline VNodes.",
55
"main": "dist/babel-plugin-htm.js",
66
"module": "dist/babel-plugin-htm.mjs",

preact/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { h, VNode } from 'preact';
2+
declare function render(tree: VNode, parent: HTMLElement): void;
3+
declare const html: (strings: TemplateStringsArray, ...values: any[]) => VNode;
4+
export { h, html, render };

preact/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)