@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/cli.js +89 -0
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +89 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
package/dist/index.js
CHANGED
|
@@ -881,6 +881,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
|
|
|
881
881
|
let spec = relative(fromDir, clientInitPath).split(sep).join("/");
|
|
882
882
|
if (!spec.startsWith(".")) spec = "./" + spec;
|
|
883
883
|
spec = spec.replace(/\.tsx$/, "");
|
|
884
|
+
const astResult = injectClientComponentViaAst(layoutPath, content, spec);
|
|
885
|
+
if (astResult) return astResult;
|
|
886
|
+
return injectClientComponentViaRegex(layoutPath, content, spec);
|
|
887
|
+
}
|
|
888
|
+
function injectClientComponentViaAst(layoutPath, content, importSpec) {
|
|
889
|
+
let recast;
|
|
890
|
+
let bp;
|
|
891
|
+
let bt;
|
|
892
|
+
try {
|
|
893
|
+
recast = nodeRequire("recast");
|
|
894
|
+
bp = nodeRequire("@babel/parser");
|
|
895
|
+
bt = nodeRequire("@babel/types");
|
|
896
|
+
} catch {
|
|
897
|
+
return null;
|
|
898
|
+
}
|
|
899
|
+
let ast;
|
|
900
|
+
try {
|
|
901
|
+
ast = recast.parse(content, {
|
|
902
|
+
parser: {
|
|
903
|
+
parse: (src) => bp.parse(src, {
|
|
904
|
+
sourceType: "module",
|
|
905
|
+
plugins: ["jsx", "typescript"],
|
|
906
|
+
allowImportExportEverywhere: true,
|
|
907
|
+
tokens: true
|
|
908
|
+
})
|
|
909
|
+
}
|
|
910
|
+
});
|
|
911
|
+
} catch {
|
|
912
|
+
return null;
|
|
913
|
+
}
|
|
914
|
+
const program = ast.program;
|
|
915
|
+
if (!program || !Array.isArray(program.body)) return null;
|
|
916
|
+
const bodyEl = findFirstJsxElementByName(ast, "body");
|
|
917
|
+
if (!bodyEl) return null;
|
|
918
|
+
const newComponent = bt.jsxElement(
|
|
919
|
+
bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
|
|
920
|
+
null,
|
|
921
|
+
[],
|
|
922
|
+
true
|
|
923
|
+
);
|
|
924
|
+
const leadingText = bt.jsxText("\n ");
|
|
925
|
+
const trailingText = bt.jsxText("\n ");
|
|
926
|
+
bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
|
|
927
|
+
const importDecl = bt.importDeclaration(
|
|
928
|
+
[bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
|
|
929
|
+
bt.stringLiteral(importSpec)
|
|
930
|
+
);
|
|
931
|
+
const body = program.body;
|
|
932
|
+
let insertAt = 0;
|
|
933
|
+
for (let i = 0; i < body.length; i++) {
|
|
934
|
+
if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
|
|
935
|
+
}
|
|
936
|
+
body.splice(insertAt, 0, importDecl);
|
|
937
|
+
let out;
|
|
938
|
+
try {
|
|
939
|
+
out = recast.print(ast).code;
|
|
940
|
+
} catch {
|
|
941
|
+
return null;
|
|
942
|
+
}
|
|
943
|
+
writeFileSync(layoutPath, out, "utf8");
|
|
944
|
+
return { kind: "injected", entryPath: layoutPath };
|
|
945
|
+
}
|
|
946
|
+
function findFirstJsxElementByName(ast, name) {
|
|
947
|
+
let found = null;
|
|
948
|
+
function walk(node) {
|
|
949
|
+
if (found || !node || typeof node !== "object") return;
|
|
950
|
+
if (Array.isArray(node)) {
|
|
951
|
+
for (const c of node) walk(c);
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const n = node;
|
|
955
|
+
if (n.type === "JSXElement") {
|
|
956
|
+
const opening = n.openingElement;
|
|
957
|
+
if (opening?.type === "JSXOpeningElement" && opening.name) {
|
|
958
|
+
const namedNode = opening.name;
|
|
959
|
+
if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
|
|
960
|
+
found = n;
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
for (const key of Object.keys(n)) {
|
|
966
|
+
walk(n[key]);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
walk(ast);
|
|
970
|
+
return found;
|
|
971
|
+
}
|
|
972
|
+
function injectClientComponentViaRegex(layoutPath, content, spec) {
|
|
884
973
|
const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
|
|
885
974
|
const lines = content.split("\n");
|
|
886
975
|
let insertImportAt = 0;
|