Skip to content

Commit 6c5586f

Browse files
authored
Merge branch 'master' into patch-1
2 parents 881f79d + e1e745a commit 6c5586f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ console.log(html`
153153
// ]
154154
```
155155

156+
### Caching
157+
158+
The default build of `htm` caches template strings, which means that it can return the same Javascript object at multiple points in the tree. If you don't want this behaviour, you have three options:
159+
160+
* Change your `h` function to copy nodes when needed.
161+
* Add the code `this[0] = 3;` at the beginning of your `h` function, which disables caching of created elements.
162+
* Use `htm/mini`, which disables caching by default.
163+
156164
## Example
157165

158166
Curious to see what it all looks like? Here's a working app!

packages/babel-plugin-transform-jsx-to-htm/index.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import jsx from '@babel/plugin-syntax-jsx';
44
* @param {Babel} babel
55
* @param {object} [options]
66
* @param {string} [options.tag='html'] The tagged template "tag" function name to produce.
7+
* @param {boolean} [options.terse=false] Use `<//>` for closing component tags
78
* @param {string | boolean | object} [options.import=false] Import the tag automatically
89
*/
910
export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
1011
const tagString = options.tag || 'html';
1112
const tag = dottedIdentifier(tagString);
1213
const importDeclaration = tagImport(options.import || false);
14+
const terse = options.terse === true;
1315

1416
function tagImport(imp) {
1517
if (imp === false) {
@@ -117,7 +119,6 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
117119
for (let i = 0; i < children.length; i++) {
118120
let child = children[i];
119121
if (t.isStringLiteral(child)) {
120-
// @todo - expose `whitespace: true` option?
121122
escapeText(child.value);
122123
}
123124
else if (t.isJSXElement(child)) {
@@ -130,9 +131,14 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
130131

131132
if (!isFragment) {
132133
if (isComponentName(name)) {
133-
raw('</');
134-
expr(name);
135-
raw('>');
134+
if (terse) {
135+
raw('<//>');
136+
}
137+
else {
138+
raw('</');
139+
expr(name);
140+
raw('>');
141+
}
136142
}
137143
else {
138144
raw('</');

src/build.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const PROP_APPEND = MODE_PROP_APPEND;
1919
// convenient to analyze and transform (e.g. Babel plugins).
2020
// For example:
2121
// treeify(
22-
// build'<div href="1${a}" ...${b}><${x} /></div>`,
22+
// build`<div href="1${a}" ...${b}><${x} /></div>`,
2323
// [X, Y, Z]
2424
// )
2525
// returns:
2626
// {
2727
// tag: 'div',
28-
// props: [ { href: ["1", X] }, Y ],
28+
// props: [ { href: ["1", X] }, Y ],
2929
// children: [ { tag: Z, props: [], children: [] } ]
3030
// }
3131
export const treeify = (built, fields) => {

0 commit comments

Comments
 (0)