tempo-sdk 0.0.7 → 0.0.8

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.
@@ -116,6 +116,19 @@ function formatProps(props) {
116
116
  return ` ${key}={${formatted}}`;
117
117
  }).filter((s) => s !== null).join("");
118
118
  }
119
+ function getExistingAttributeNames(element) {
120
+ const names = /* @__PURE__ */ new Set();
121
+ for (const attr of element.openingElement.attributes ?? []) {
122
+ if (attr.type !== "JSXAttribute") continue;
123
+ names.add(getTagName(attr.name));
124
+ }
125
+ return names;
126
+ }
127
+ function removeExistingProps(props, existingNames) {
128
+ return Object.fromEntries(
129
+ Object.entries(props).filter(([key]) => !existingNames.has(key))
130
+ );
131
+ }
119
132
  var FRAGMENT_TAG_NAMES = /* @__PURE__ */ new Set(["Fragment", "React.Fragment"]);
120
133
  function annotateFile(source, filename, computeAnnotatedProps) {
121
134
  const parseResult = (0, import_oxc_parser2.parseSync)(filename, source);
@@ -125,7 +138,10 @@ function annotateFile(source, filename, computeAnnotatedProps) {
125
138
  const element = node;
126
139
  const tagName = getTagName(element.openingElement.name);
127
140
  if (FRAGMENT_TAG_NAMES.has(tagName)) return;
128
- const props = computeAnnotatedProps({ element, tagName });
141
+ const props = removeExistingProps(
142
+ computeAnnotatedProps({ element, tagName }),
143
+ getExistingAttributeNames(element)
144
+ );
129
145
  if (Object.keys(props).length === 0) return;
130
146
  const openingElement = element.openingElement;
131
147
  let insertPos = openingElement.end - 1;
@@ -756,14 +772,13 @@ function resolveCanonicalRoot(rootContext) {
756
772
  function isInNodeModules(path3) {
757
773
  return path3.includes(`${import_node_path3.sep}node_modules${import_node_path3.sep}`);
758
774
  }
759
- function findFirstJSXHead(program) {
775
+ function findFirstJSXElement(program, tagName) {
760
776
  let result = null;
761
777
  walk(program, {
762
778
  JSXElement(node) {
763
779
  if (result) return;
764
780
  const element = node;
765
- const tagName = getTagName(element.openingElement.name);
766
- if (tagName === "head") {
781
+ if (getTagName(element.openingElement.name) === tagName) {
767
782
  result = element;
768
783
  }
769
784
  }
@@ -786,6 +801,16 @@ function hasRootMetaChild(headElement) {
786
801
  }
787
802
  return false;
788
803
  }
804
+ function injectFirstChildMarkup(source, element, childMarkup) {
805
+ const opening = element.openingElement;
806
+ if (!opening.selfClosing) {
807
+ const insertPos = opening.end;
808
+ return source.slice(0, insertPos) + childMarkup + source.slice(insertPos);
809
+ }
810
+ const tagName = getTagName(opening.name);
811
+ const reopened = source.slice(opening.start, opening.end).replace(/\s*\/>$/, ">");
812
+ return source.slice(0, opening.start) + `${reopened}${childMarkup}</${tagName}>` + source.slice(opening.end);
813
+ }
789
814
  function injectRootMetaIntoSource(source, filename, root) {
790
815
  let parseResult;
791
816
  try {
@@ -793,12 +818,21 @@ function injectRootMetaIntoSource(source, filename, root) {
793
818
  } catch {
794
819
  return source;
795
820
  }
796
- const headElement = findFirstJSXHead(parseResult.program);
797
- if (!headElement) return source;
798
- if (hasRootMetaChild(headElement)) return source;
799
- const insertPos = headElement.openingElement.end;
800
821
  const metaTag = `<meta name="${ROOT_META_NAME}" content={${JSON.stringify(root)}} />`;
801
- return source.slice(0, insertPos) + metaTag + source.slice(insertPos);
822
+ const headElement = findFirstJSXElement(parseResult.program, "head");
823
+ if (headElement) {
824
+ if (hasRootMetaChild(headElement)) return source;
825
+ return injectFirstChildMarkup(source, headElement, metaTag);
826
+ }
827
+ const htmlElement = findFirstJSXElement(parseResult.program, "html");
828
+ if (htmlElement) {
829
+ return injectFirstChildMarkup(
830
+ source,
831
+ htmlElement,
832
+ `<head>${metaTag}</head>`
833
+ );
834
+ }
835
+ return source;
802
836
  }
803
837
  function injectRootMetaIntoAnnotatedSource(annotatedSource, filename, root) {
804
838
  return injectRootMetaIntoSource(annotatedSource, filename, root);