babel-plugin-jsx-dom-expressions 0.35.16 → 0.35.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +14 -10
  2. package/index.js +40 -38
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -38,29 +38,33 @@ const view = ({ item }) => {
38
38
  Compiles to:
39
39
 
40
40
  ```jsx
41
- import { insert as _$insert } from "dom";
41
+ import { template as _$template } from "dom";
42
+ import { delegateEvents as _$delegateEvents } from "dom";
43
+ import { className as _$className } from "dom";
42
44
  import { effect as _$effect } from "dom";
45
+ import { insert as _$insert } from "dom";
43
46
 
44
- const _tmpl$ = document.createElement("template");
45
- _tmpl$.innerHTML = `<tr><td class="col-md-1"></td><td class="col-md-4"><a></a></td><td class="col-md-1"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td><td class="col-md-6"></td></tr>`;
46
-
47
- const view = ({ item }) => {
47
+ const _tmpl$ = /*#__PURE__*/_$template(`<tr><td class="col-md-1"></td><td class="col-md-4"><a></a></td><td class="col-md-1"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td><td class="col-md-6"></td></tr>`, 16);
48
+ const view = ({
49
+ item
50
+ }) => {
48
51
  const itemId = item.id;
49
- return (function() {
50
- const _el$ = _tmpl$.content.firstChild.cloneNode(true),
52
+ return (() => {
53
+ const _el$ = _tmpl$.cloneNode(true),
51
54
  _el$2 = _el$.firstChild,
52
55
  _el$3 = _el$2.nextSibling,
53
56
  _el$4 = _el$3.firstChild,
54
57
  _el$5 = _el$3.nextSibling,
55
58
  _el$6 = _el$5.firstChild;
56
- _$effect(() => (_el$.className = itemId === selected() ? "danger" : ""));
57
59
  _$insert(_el$2, itemId);
58
- _el$4.onclick = e => select(item, e);
60
+ _el$4.$$click = e => select(item, e);
59
61
  _$insert(_el$4, () => item.label);
60
- _el$6.onclick = e => del(item, e);
62
+ _el$6.$$click = e => del(item, e);
63
+ _$effect(() => _$className(_el$, itemId === selected() ? "danger" : ""));
61
64
  return _el$;
62
65
  })();
63
66
  };
67
+ _$delegateEvents(["click"]);
64
68
  ```
65
69
 
66
70
  The use of cloneNode improves repeat insert performance and precompilation reduces the number of references to the minimal traversal path. This is a basic example which doesn't leverage event delegation or any of the more advanced features described below.
package/index.js CHANGED
@@ -261,7 +261,7 @@ function registerImportMethod(path, name, moduleName) {
261
261
  // the cloning is required to play well with babel-preset-env which is
262
262
  // transpiling import as we add them and using the same identifier causes
263
263
  // problems with the multiple identifiers of the same thing
264
- return t__namespace.cloneDeep(iden);
264
+ return t__namespace.cloneNode(iden);
265
265
  }
266
266
  }
267
267
 
@@ -585,7 +585,7 @@ function transformElement$3(path, info) {
585
585
  isCustomElement = tagName.indexOf("-") > -1,
586
586
  results = {
587
587
  template: `<${tagName}`,
588
- decl: [],
588
+ declarations: [],
589
589
  exprs: [],
590
590
  dynamics: [],
591
591
  postExprs: [],
@@ -1157,7 +1157,7 @@ function transformAttributes$2(path, results) {
1157
1157
  nextElem = attribute.scope.generateUidIdentifier("el$");
1158
1158
  children = t__namespace.JSXText(" ");
1159
1159
  children.extra = { raw: " ", rawValue: " " };
1160
- results.decl.push(
1160
+ results.declarations.push(
1161
1161
  t__namespace.variableDeclarator(nextElem, t__namespace.memberExpression(elem, t__namespace.identifier("firstChild")))
1162
1162
  );
1163
1163
  }
@@ -1258,7 +1258,7 @@ function transformChildren$2(path, results, config) {
1258
1258
  t__namespace.identifier(tempPath),
1259
1259
  t__namespace.identifier(i === 0 ? "firstChild" : "nextSibling")
1260
1260
  );
1261
- results.decl.push(
1261
+ results.declarations.push(
1262
1262
  t__namespace.variableDeclarator(
1263
1263
  child.id,
1264
1264
  config.hydratable && tagName === "html"
@@ -1266,7 +1266,7 @@ function transformChildren$2(path, results, config) {
1266
1266
  : walk
1267
1267
  )
1268
1268
  );
1269
- results.decl.push(...child.decl);
1269
+ results.declarations.push(...child.declarations);
1270
1270
  results.exprs.push(...child.exprs);
1271
1271
  results.dynamics.push(...child.dynamics);
1272
1272
  results.postExprs.push(...child.postExprs);
@@ -1326,7 +1326,7 @@ function createPlaceholder(path, results, tempPath, i, char) {
1326
1326
  results.template += `<!${char}>`;
1327
1327
  if (config.hydratable && char === "/") {
1328
1328
  contentId = path.scope.generateUidIdentifier("co$");
1329
- results.decl.push(
1329
+ results.declarations.push(
1330
1330
  t__namespace.variableDeclarator(
1331
1331
  t__namespace.arrayPattern([exprId, contentId]),
1332
1332
  t__namespace.callExpression(
@@ -1336,7 +1336,7 @@ function createPlaceholder(path, results, tempPath, i, char) {
1336
1336
  )
1337
1337
  );
1338
1338
  } else
1339
- results.decl.push(
1339
+ results.declarations.push(
1340
1340
  t__namespace.variableDeclarator(
1341
1341
  exprId,
1342
1342
  t__namespace.memberExpression(
@@ -1602,8 +1602,8 @@ function registerTemplate(path, results) {
1602
1602
  ])
1603
1603
  );
1604
1604
  }
1605
- results.decl.unshift(decl);
1606
- results.decl = t__namespace.variableDeclaration("const", results.decl);
1605
+ results.declarations.unshift(decl);
1606
+ results.decl = t__namespace.variableDeclaration("const", results.declarations);
1607
1607
  }
1608
1608
 
1609
1609
  function wrapDynamics$1(path, dynamics) {
@@ -1639,7 +1639,7 @@ function wrapDynamics$1(path, dynamics) {
1639
1639
  ])
1640
1640
  );
1641
1641
  }
1642
- const decls = [],
1642
+ const declarations = [],
1643
1643
  statements = [],
1644
1644
  identifiers = [],
1645
1645
  prevId = t__namespace.identifier("_p$");
@@ -1649,7 +1649,7 @@ function wrapDynamics$1(path, dynamics) {
1649
1649
  value = t__namespace.unaryExpression("!", t__namespace.unaryExpression("!", value));
1650
1650
  }
1651
1651
  identifiers.push(identifier);
1652
- decls.push(t__namespace.variableDeclarator(identifier, value));
1652
+ declarations.push(t__namespace.variableDeclarator(identifier, value));
1653
1653
  if (key === "classList" || key === "style") {
1654
1654
  const prev = t__namespace.memberExpression(prevId, identifier);
1655
1655
  statements.push(
@@ -1685,7 +1685,7 @@ function wrapDynamics$1(path, dynamics) {
1685
1685
  t__namespace.arrowFunctionExpression(
1686
1686
  [prevId],
1687
1687
  t__namespace.blockStatement([
1688
- t__namespace.variableDeclaration("const", decls),
1688
+ t__namespace.variableDeclaration("const", declarations),
1689
1689
  ...statements,
1690
1690
  t__namespace.returnStatement(prevId)
1691
1691
  ])
@@ -1786,7 +1786,7 @@ function transformElement$2(path, info) {
1786
1786
  results = {
1787
1787
  template: [`<${tagName}`],
1788
1788
  templateValues: [],
1789
- decl: [],
1789
+ declarations: [],
1790
1790
  exprs: [],
1791
1791
  dynamics: [],
1792
1792
  tagName,
@@ -2387,7 +2387,7 @@ function transformElement$1(path, info) {
2387
2387
  let tagName = getTagName(path.node),
2388
2388
  results = {
2389
2389
  id: path.scope.generateUidIdentifier("el$"),
2390
- decl: [],
2390
+ declarations: [],
2391
2391
  exprs: [],
2392
2392
  dynamics: [],
2393
2393
  postExprs: [],
@@ -2395,7 +2395,7 @@ function transformElement$1(path, info) {
2395
2395
  renderer: "universal"
2396
2396
  };
2397
2397
 
2398
- results.decl.push(
2398
+ results.declarations.push(
2399
2399
  t__namespace.variableDeclarator(
2400
2400
  results.id,
2401
2401
  t__namespace.callExpression(
@@ -2611,7 +2611,7 @@ function transformChildren(path, results) {
2611
2611
  getRendererConfig(path, "universal").moduleName
2612
2612
  );
2613
2613
  if (multi) {
2614
- results.decl.push(
2614
+ results.declarations.push(
2615
2615
  t__namespace.variableDeclarator(
2616
2616
  child.id,
2617
2617
  t__namespace.callExpression(createTextNode, [
@@ -2625,7 +2625,7 @@ function transformChildren(path, results) {
2625
2625
  ]);
2626
2626
  }
2627
2627
  appends.push(t__namespace.expressionStatement(t__namespace.callExpression(insertNode, [results.id, insert])));
2628
- results.decl.push(...child.decl);
2628
+ results.declarations.push(...child.declarations);
2629
2629
  results.exprs.push(...child.exprs);
2630
2630
  results.dynamics.push(...child.dynamics);
2631
2631
  } else if (child.exprs.length) {
@@ -2749,7 +2749,7 @@ function processSpreads(path, attributes, { elem, hasChildren, wrapConditionals
2749
2749
  function createTemplate(path, result, wrap) {
2750
2750
  const config = getConfig(path);
2751
2751
  if (result.id) {
2752
- result.decl = t__namespace.variableDeclaration("const", result.decl);
2752
+ result.decl = t__namespace.variableDeclaration("const", result.declarations);
2753
2753
  if (
2754
2754
  !(result.exprs.length || result.dynamics.length || result.postExprs.length) &&
2755
2755
  result.decl.declarations.length === 1
@@ -2799,14 +2799,14 @@ function wrapDynamics(path, dynamics) {
2799
2799
  ])
2800
2800
  );
2801
2801
  }
2802
- const decls = [],
2802
+ const declarations = [],
2803
2803
  statements = [],
2804
2804
  identifiers = [],
2805
2805
  prevId = t__namespace.identifier("_p$");
2806
2806
  dynamics.forEach(({ elem, key, value }) => {
2807
2807
  const identifier = path.scope.generateUidIdentifier("v$");
2808
2808
  identifiers.push(identifier);
2809
- decls.push(t__namespace.variableDeclarator(identifier, value));
2809
+ declarations.push(t__namespace.variableDeclarator(identifier, value));
2810
2810
  const prev = t__namespace.memberExpression(prevId, identifier);
2811
2811
  statements.push(
2812
2812
  t__namespace.expressionStatement(
@@ -2830,7 +2830,7 @@ function wrapDynamics(path, dynamics) {
2830
2830
  t__namespace.arrowFunctionExpression(
2831
2831
  [prevId],
2832
2832
  t__namespace.blockStatement([
2833
- t__namespace.variableDeclaration("const", decls),
2833
+ t__namespace.variableDeclaration("const", declarations),
2834
2834
  ...statements,
2835
2835
  t__namespace.returnStatement(prevId)
2836
2836
  ])
@@ -2890,7 +2890,8 @@ function transformComponent(path) {
2890
2890
  : node.argument
2891
2891
  );
2892
2892
  } else {
2893
- const value = node.value || t__namespace.booleanLiteral(true),
2893
+ // handle weird babel bug around HTML entities
2894
+ const value = (t__namespace.isStringLiteral(node.value) ? t__namespace.stringLiteral(node.value.value): node.value) || t__namespace.booleanLiteral(true),
2894
2895
  id = convertJSXIdentifier(node.name),
2895
2896
  key = id.name;
2896
2897
  if (hasChildren && key === "children") return;
@@ -3032,11 +3033,15 @@ function transformComponentChildren(children, config) {
3032
3033
  const filteredChildren = filterChildren(children);
3033
3034
  if (!filteredChildren.length) return;
3034
3035
  let dynamic = false;
3036
+ let pathNodes = [];
3035
3037
 
3036
3038
  let transformedChildren = filteredChildren.reduce((memo, path) => {
3037
3039
  if (t__namespace.isJSXText(path.node)) {
3038
3040
  const v = htmlEntities.decode(trimWhitespace(path.node.extra.raw));
3039
- if (v.length) memo.push(t__namespace.stringLiteral(v));
3041
+ if (v.length) {
3042
+ pathNodes.push(path.node);
3043
+ memo.push(t__namespace.stringLiteral(v));
3044
+ }
3040
3045
  } else {
3041
3046
  const child = transformNode(path, {
3042
3047
  topLevel: true,
@@ -3051,6 +3056,7 @@ function transformComponentChildren(children, config) {
3051
3056
  ) {
3052
3057
  child.exprs[0] = child.exprs[0].body;
3053
3058
  }
3059
+ pathNodes.push(path.node);
3054
3060
  memo.push(getCreateTemplate(config, path, child)(path, child, filteredChildren.length > 1));
3055
3061
  }
3056
3062
  return memo;
@@ -3059,9 +3065,9 @@ function transformComponentChildren(children, config) {
3059
3065
  if (transformedChildren.length === 1) {
3060
3066
  transformedChildren = transformedChildren[0];
3061
3067
  if (
3062
- !t__namespace.isJSXExpressionContainer(filteredChildren[0]) &&
3063
- !t__namespace.isJSXSpreadChild(filteredChildren[0]) &&
3064
- !t__namespace.isJSXText(filteredChildren[0])
3068
+ !t__namespace.isJSXExpressionContainer(pathNodes[0]) &&
3069
+ !t__namespace.isJSXSpreadChild(pathNodes[0]) &&
3070
+ !t__namespace.isJSXText(pathNodes[0])
3065
3071
  ) {
3066
3072
  transformedChildren =
3067
3073
  t__namespace.isCallExpression(transformedChildren) &&
@@ -3137,21 +3143,21 @@ function transformNode(path, info = {}) {
3137
3143
  if (t__namespace.isJSXElement(node)) {
3138
3144
  return transformElement(config, path, info);
3139
3145
  } else if (t__namespace.isJSXFragment(node)) {
3140
- let results = { template: "", decl: [], exprs: [], dynamics: [] };
3146
+ let results = { template: "", declarations: [], exprs: [], dynamics: [] };
3141
3147
  // <><div /><Component /></>
3142
3148
  transformFragmentChildren(path.get("children"), results, config);
3143
3149
  return results;
3144
3150
  } else if (t__namespace.isJSXText(node) || (staticValue = getStaticExpression(path))) {
3145
3151
  const text =
3146
3152
  staticValue !== undefined
3147
- ? !info.doNotEscape
3148
- ? escapeHTML(staticValue.toString())
3149
- : staticValue.toString()
3153
+ ? info.doNotEscape
3154
+ ? staticValue.toString()
3155
+ : escapeHTML(staticValue.toString())
3150
3156
  : trimWhitespace(node.extra.raw);
3151
3157
  if (!text.length) return null;
3152
3158
  const results = {
3153
3159
  template: config.generate === "ssr" ? text : escapeBackticks(text),
3154
- decl: [],
3160
+ declarations: [],
3155
3161
  exprs: [],
3156
3162
  dynamics: [],
3157
3163
  postExprs: [],
@@ -3237,13 +3243,9 @@ function transformElement(config, path, info = {}) {
3237
3243
  // <div ...></div>
3238
3244
  // const element = getTransformElemet(config, path, tagName);
3239
3245
 
3240
- let tagRenderer;
3241
- for (var renderer of config.renderers ?? []) {
3242
- if (renderer.elements.indexOf(tagName) !== -1) {
3243
- tagRenderer = renderer;
3244
- break;
3245
- }
3246
- }
3246
+ const tagRenderer = (config.renderers ?? []).find(
3247
+ renderer => renderer.elements.includes(tagName)
3248
+ );
3247
3249
 
3248
3250
  if (tagRenderer?.name === "dom" || getConfig(path).generate === "dom") {
3249
3251
  return transformElement$3(path, info);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "babel-plugin-jsx-dom-expressions",
3
3
  "description": "A JSX to DOM plugin that wraps expressions for fine grained change detection",
4
- "version": "0.35.16",
4
+ "version": "0.35.18",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -31,5 +31,5 @@
31
31
  "devDependencies": {
32
32
  "@babel/core": "^7.20.12"
33
33
  },
34
- "gitHead": "764c717918d199a630bb0c7f85c7b8f772ef9e07"
34
+ "gitHead": "5e21b8cb58fbccc1f635bfdfd8904094e46edfea"
35
35
  }