babel-plugin-jsx-dom-expressions 0.30.6 → 0.31.0-beta.0

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 (2) hide show
  1. package/index.js +72 -53
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -319,12 +319,13 @@ function isDynamic(path, { checkMember, checkTags, checkCallExpressions = true,
319
319
 
320
320
  function getStaticExpression(path) {
321
321
  const node = path.node;
322
- let value;
322
+ let value, type;
323
323
  return (
324
324
  t__namespace.isJSXExpressionContainer(node) &&
325
325
  t__namespace.isJSXElement(path.parent) &&
326
326
  !isComponent(getTagName(path.parent)) &&
327
327
  (value = path.get("expression").evaluate().value) !== undefined &&
328
+ ((type = typeof value) === "string" || type === "number") &&
328
329
  value
329
330
  );
330
331
  }
@@ -447,9 +448,7 @@ function transformCondition(path, inline, deep) {
447
448
  nextPath.node.left = t__namespace.callExpression(id, []);
448
449
  }
449
450
  }
450
- if (dTest) {
451
- if (inline) return t__namespace.arrowFunctionExpression([], expr);
452
-
451
+ if (dTest && !inline) {
453
452
  const statements = [
454
453
  t__namespace.variableDeclaration("const", [
455
454
  t__namespace.variableDeclarator(
@@ -1099,6 +1098,7 @@ function transformChildren$2(path, results, config) {
1099
1098
  if (!child) return;
1100
1099
  results.template += child.template;
1101
1100
  if (child.id) {
1101
+ if (child.tagName === "head") return;
1102
1102
  if (config.hydratable && tagName === "html") {
1103
1103
  registerImportMethod(path, "getNextMatch");
1104
1104
  }
@@ -1450,6 +1450,52 @@ function wrapDynamics$1(path, dynamics) {
1450
1450
  );
1451
1451
  }
1452
1452
 
1453
+ function createTemplate$1(path, result) {
1454
+ if (!result.template) {
1455
+ return result.exprs[0];
1456
+ }
1457
+
1458
+ let template, id;
1459
+
1460
+ registerImportMethod(path, "ssr");
1461
+ if (!Array.isArray(result.template)) {
1462
+ template = t__namespace.stringLiteral(result.template);
1463
+ } else if (result.template.length === 1) {
1464
+ template = t__namespace.stringLiteral(result.template[0]);
1465
+ } else {
1466
+ const strings = result.template.map(tmpl => t__namespace.stringLiteral(tmpl));
1467
+ template = t__namespace.arrayExpression(strings);
1468
+ }
1469
+
1470
+ const templates =
1471
+ path.scope.getProgramParent().data.templates ||
1472
+ (path.scope.getProgramParent().data.templates = []);
1473
+ const found = templates.find(tmp => {
1474
+ if (t__namespace.isArrayExpression(tmp[1]) && t__namespace.isArrayExpression(template)) {
1475
+ return tmp[1].elements.every(
1476
+ (el, i) => template.elements[i] && el.value === template.elements[i].value
1477
+ );
1478
+ }
1479
+ return tmp[1].value === template.value;
1480
+ });
1481
+ if (!found) {
1482
+ id = path.scope.generateUidIdentifier("tmpl$");
1483
+ templates.push([id, template]);
1484
+ } else id = found[0];
1485
+
1486
+ return t__namespace.callExpression(
1487
+ t__namespace.identifier(`_$ssr`),
1488
+ result.template.length > 1 ? [id, ...result.templateValues] : [id]
1489
+ );
1490
+ }
1491
+
1492
+ function appendTemplates(path, templates) {
1493
+ const declarators = templates.map(template => {
1494
+ return t__namespace.variableDeclarator(template[0], template[1]);
1495
+ });
1496
+ path.node.body.unshift(t__namespace.variableDeclaration("const", declarators));
1497
+ }
1498
+
1453
1499
  function appendToTemplate(template, value) {
1454
1500
  let array;
1455
1501
  if (Array.isArray(value)) {
@@ -1468,7 +1514,8 @@ function transformElement$1(path, info) {
1468
1514
  templateValues: [],
1469
1515
  decl: [],
1470
1516
  exprs: [],
1471
- dynamics: []
1517
+ dynamics: [],
1518
+ tagName
1472
1519
  };
1473
1520
  if (info.topLevel && config.hydratable) {
1474
1521
  registerImportMethod(path, "ssrHydrationKey");
@@ -1478,7 +1525,7 @@ function transformElement$1(path, info) {
1478
1525
  transformAttributes$1(path, results);
1479
1526
  appendToTemplate(results.template, ">");
1480
1527
  if (!voidTag) {
1481
- transformChildren$1(path, results, config);
1528
+ transformChildren$1(path, results, {...config, ...info});
1482
1529
  appendToTemplate(results.template, `</${tagName}>`);
1483
1530
  }
1484
1531
  return results;
@@ -1815,6 +1862,24 @@ function transformChildren$1(path, results, { hydratable }) {
1815
1862
  const doNotEscape = path.doNotEscape;
1816
1863
  const filteredChildren = filterChildren(path.get("children"));
1817
1864
  filteredChildren.forEach(node => {
1865
+ if (t__namespace.isJSXElement(node.node) && (getTagName(node.node) === "head")) {
1866
+ const child = transformNode(node, { doNotEscape, hydratable: false });
1867
+ registerImportMethod(path, "NoHydration");
1868
+ results.template.push("");
1869
+ results.templateValues.push(
1870
+ t__namespace.callExpression(t__namespace.identifier("_$NoHydration"), [
1871
+ t__namespace.objectExpression([
1872
+ t__namespace.objectMethod(
1873
+ "get",
1874
+ t__namespace.identifier("children"),
1875
+ [],
1876
+ t__namespace.blockStatement([t__namespace.returnStatement(createTemplate$1(path, child))])
1877
+ )
1878
+ ])
1879
+ ])
1880
+ );
1881
+ return;
1882
+ }
1818
1883
  const child = transformNode(node, { doNotEscape });
1819
1884
  appendToTemplate(results.template, child.template);
1820
1885
  results.templateValues.push.apply(results.templateValues, child.templateValues || []);
@@ -1838,52 +1903,6 @@ function transformChildren$1(path, results, { hydratable }) {
1838
1903
  });
1839
1904
  }
1840
1905
 
1841
- function createTemplate$1(path, result) {
1842
- if (!result.template) {
1843
- return result.exprs[0];
1844
- }
1845
-
1846
- let template, id;
1847
-
1848
- registerImportMethod(path, "ssr");
1849
- if (!Array.isArray(result.template)) {
1850
- template = t__namespace.stringLiteral(result.template);
1851
- } else if (result.template.length === 1) {
1852
- template = t__namespace.stringLiteral(result.template[0]);
1853
- } else {
1854
- const strings = result.template.map(tmpl => t__namespace.stringLiteral(tmpl));
1855
- template = t__namespace.arrayExpression(strings);
1856
- }
1857
-
1858
- const templates =
1859
- path.scope.getProgramParent().data.templates ||
1860
- (path.scope.getProgramParent().data.templates = []);
1861
- const found = templates.find(tmp => {
1862
- if (t__namespace.isArrayExpression(tmp[1]) && t__namespace.isArrayExpression(template)) {
1863
- return tmp[1].elements.every(
1864
- (el, i) => template.elements[i] && el.value === template.elements[i].value
1865
- );
1866
- }
1867
- return tmp[1].value === template.value;
1868
- });
1869
- if (!found) {
1870
- id = path.scope.generateUidIdentifier("tmpl$");
1871
- templates.push([id, template]);
1872
- } else id = found[0];
1873
-
1874
- return t__namespace.callExpression(
1875
- t__namespace.identifier(`_$ssr`),
1876
- result.template.length > 1 ? [id, ...result.templateValues] : [id]
1877
- );
1878
- }
1879
-
1880
- function appendTemplates(path, templates) {
1881
- const declarators = templates.map(template => {
1882
- return t__namespace.variableDeclarator(template[0], template[1]);
1883
- });
1884
- path.node.body.unshift(t__namespace.variableDeclaration("const", declarators));
1885
- }
1886
-
1887
1906
  function transformElement(path, info) {
1888
1907
  let tagName = getTagName(path.node),
1889
1908
  results = {
@@ -2451,7 +2470,7 @@ function transformFragmentChildren(children, results, config) {
2451
2470
  if (v.length) memo.push(t__namespace.stringLiteral(v));
2452
2471
  } else {
2453
2472
  const child = transformNode(path, { topLevel: true, fragmentChild: true });
2454
- memo.push(createTemplate$3(path, child, !singleChild));
2473
+ memo.push(createTemplate$3(path, child, true));
2455
2474
  }
2456
2475
  return memo;
2457
2476
  }, []);
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.30.6",
4
+ "version": "0.31.0-beta.0",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -23,5 +23,5 @@
23
23
  "@babel/types": "^7.11.5",
24
24
  "html-entities": "2.3.2"
25
25
  },
26
- "gitHead": "a2cc48a877bd4edff32f398c6c84d0b7bf6d584e"
26
+ "gitHead": "bdb5b31867eb9002d0f87738a23d912ed025211b"
27
27
  }