@kenkaiiii/gg-pixel 4.3.92 → 4.3.93

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.
package/dist/index.cjs CHANGED
@@ -909,6 +909,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
909
909
  let spec = (0, import_node_path2.relative)(fromDir, clientInitPath).split(import_node_path2.sep).join("/");
910
910
  if (!spec.startsWith(".")) spec = "./" + spec;
911
911
  spec = spec.replace(/\.tsx$/, "");
912
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
913
+ if (astResult) return astResult;
914
+ return injectClientComponentViaRegex(layoutPath, content, spec);
915
+ }
916
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
917
+ let recast;
918
+ let bp;
919
+ let bt;
920
+ try {
921
+ recast = nodeRequire("recast");
922
+ bp = nodeRequire("@babel/parser");
923
+ bt = nodeRequire("@babel/types");
924
+ } catch {
925
+ return null;
926
+ }
927
+ let ast;
928
+ try {
929
+ ast = recast.parse(content, {
930
+ parser: {
931
+ parse: (src) => bp.parse(src, {
932
+ sourceType: "module",
933
+ plugins: ["jsx", "typescript"],
934
+ allowImportExportEverywhere: true,
935
+ tokens: true
936
+ })
937
+ }
938
+ });
939
+ } catch {
940
+ return null;
941
+ }
942
+ const program = ast.program;
943
+ if (!program || !Array.isArray(program.body)) return null;
944
+ const bodyEl = findFirstJsxElementByName(ast, "body");
945
+ if (!bodyEl) return null;
946
+ const newComponent = bt.jsxElement(
947
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
948
+ null,
949
+ [],
950
+ true
951
+ );
952
+ const leadingText = bt.jsxText("\n ");
953
+ const trailingText = bt.jsxText("\n ");
954
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
955
+ const importDecl = bt.importDeclaration(
956
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
957
+ bt.stringLiteral(importSpec)
958
+ );
959
+ const body = program.body;
960
+ let insertAt = 0;
961
+ for (let i = 0; i < body.length; i++) {
962
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
963
+ }
964
+ body.splice(insertAt, 0, importDecl);
965
+ let out;
966
+ try {
967
+ out = recast.print(ast).code;
968
+ } catch {
969
+ return null;
970
+ }
971
+ (0, import_node_fs3.writeFileSync)(layoutPath, out, "utf8");
972
+ return { kind: "injected", entryPath: layoutPath };
973
+ }
974
+ function findFirstJsxElementByName(ast, name) {
975
+ let found = null;
976
+ function walk(node) {
977
+ if (found || !node || typeof node !== "object") return;
978
+ if (Array.isArray(node)) {
979
+ for (const c of node) walk(c);
980
+ return;
981
+ }
982
+ const n = node;
983
+ if (n.type === "JSXElement") {
984
+ const opening = n.openingElement;
985
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
986
+ const namedNode = opening.name;
987
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
988
+ found = n;
989
+ return;
990
+ }
991
+ }
992
+ }
993
+ for (const key of Object.keys(n)) {
994
+ walk(n[key]);
995
+ }
996
+ }
997
+ walk(ast);
998
+ return found;
999
+ }
1000
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
912
1001
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
913
1002
  const lines = content.split("\n");
914
1003
  let insertImportAt = 0;