@kenkaiiii/gg-pixel 4.3.73 → 4.3.75

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
@@ -779,17 +779,18 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
779
779
  const serverInitPath = pickPath(projectRoot, ["instrumentation.ts", "instrumentation.js"]);
780
780
  const finalServerPath = serverInitPath ?? (0, import_node_path2.join)(projectRoot, "instrumentation.ts");
781
781
  writeNextInstrumentation(finalServerPath, ingestUrl);
782
- const clientInitPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.client.mjs");
783
- (0, import_node_fs3.writeFileSync)(clientInitPath, renderBrowserInitFile(ingestUrl, projectKey), "utf8");
782
+ patchNextConfig(projectRoot);
783
+ const clientInitPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.client.tsx");
784
+ (0, import_node_fs3.writeFileSync)(clientInitPath, renderNextClientComponent(ingestUrl, projectKey), "utf8");
784
785
  const layoutPath = findNextLayout(projectRoot);
785
786
  let entryWiring;
786
787
  if (!layoutPath) {
787
788
  warnings.push(
788
- 'Could not auto-wire the Next.js client init \u2014 no app/layout.{tsx,jsx} or pages/_app.{tsx,jsx} found. Add `import "./gg-pixel.client.mjs";` to your root layout/_app.'
789
+ 'Could not auto-wire the Next.js client init \u2014 no app/layout.{tsx,jsx} or pages/_app.{tsx,jsx} found. Add `<GGPixelClient />` from "./gg-pixel.client" to your root layout/_app.'
789
790
  );
790
791
  entryWiring = { kind: "no_entry_found" };
791
792
  } else {
792
- entryWiring = injectImport(layoutPath, clientInitPath);
793
+ entryWiring = injectNextClientComponent(layoutPath, clientInitPath);
793
794
  }
794
795
  return {
795
796
  primaryInitPath: clientInitPath,
@@ -851,6 +852,112 @@ function findNextLayout(projectRoot) {
851
852
  }
852
853
  return null;
853
854
  }
855
+ function renderNextClientComponent(ingestUrl, projectKey) {
856
+ return `"use client";
857
+ // Client-only pixel init. Rendered from the root layout. The "use client"
858
+ // directive guarantees this module never executes during server-side
859
+ // rendering \u2014 \`window.onerror\` references would otherwise crash builds.
860
+ import { useEffect } from "react";
861
+ import { initPixel } from "@kenkaiiii/gg-pixel/browser";
862
+
863
+ let inited = false;
864
+
865
+ export default function GGPixelClient() {
866
+ useEffect(() => {
867
+ if (inited) return;
868
+ inited = true;
869
+ initPixel({
870
+ projectKey: ${JSON.stringify(projectKey)},
871
+ ingestUrl: ${JSON.stringify(ingestUrl)},
872
+ });
873
+ }, []);
874
+ return null;
875
+ }
876
+ `;
877
+ }
878
+ function injectNextClientComponent(layoutPath, clientInitPath) {
879
+ let content;
880
+ try {
881
+ content = (0, import_node_fs3.readFileSync)(layoutPath, "utf8");
882
+ } catch (err) {
883
+ return { kind: "skipped", reason: `unreadable: ${err.message}` };
884
+ }
885
+ if (content.includes("GGPixelClient") || content.includes("@kenkaiiii/gg-pixel")) {
886
+ return { kind: "already_present", entryPath: layoutPath };
887
+ }
888
+ const fromDir = (0, import_node_path2.dirname)(layoutPath);
889
+ let spec = (0, import_node_path2.relative)(fromDir, clientInitPath).split(import_node_path2.sep).join("/");
890
+ if (!spec.startsWith(".")) spec = "./" + spec;
891
+ spec = spec.replace(/\.tsx$/, "");
892
+ const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
893
+ const lines = content.split("\n");
894
+ let insertImportAt = 0;
895
+ for (let i = 0; i < lines.length; i++) {
896
+ if (/^\s*import\s/.test(lines[i] ?? "")) insertImportAt = i + 1;
897
+ }
898
+ lines.splice(insertImportAt, 0, importLine);
899
+ const updated = lines.join("\n");
900
+ const childrenIdx = updated.lastIndexOf("{children}");
901
+ if (childrenIdx === -1) {
902
+ (0, import_node_fs3.writeFileSync)(layoutPath, updated, "utf8");
903
+ return { kind: "skipped", reason: "added import but couldn't find {children} to render <GGPixelClient />" };
904
+ }
905
+ const before = updated.slice(0, childrenIdx);
906
+ const after = updated.slice(childrenIdx);
907
+ const finalContent = before + "<GGPixelClient />\n " + after;
908
+ (0, import_node_fs3.writeFileSync)(layoutPath, finalContent, "utf8");
909
+ return { kind: "injected", entryPath: layoutPath };
910
+ }
911
+ function patchNextConfig(projectRoot) {
912
+ const candidates = ["next.config.ts", "next.config.mjs", "next.config.js", "next.config.cjs"];
913
+ let configPath = null;
914
+ for (const c of candidates) {
915
+ const p = (0, import_node_path2.join)(projectRoot, c);
916
+ if ((0, import_node_fs3.existsSync)(p)) {
917
+ configPath = p;
918
+ break;
919
+ }
920
+ }
921
+ if (!configPath) {
922
+ configPath = (0, import_node_path2.join)(projectRoot, "next.config.ts");
923
+ (0, import_node_fs3.writeFileSync)(
924
+ configPath,
925
+ `import type { NextConfig } from "next";
926
+
927
+ const nextConfig: NextConfig = {
928
+ // Keeps Next's bundler from trying to compile better-sqlite3 (native dep).
929
+ serverExternalPackages: ["@kenkaiiii/gg-pixel"],
930
+ };
931
+
932
+ export default nextConfig;
933
+ `,
934
+ "utf8"
935
+ );
936
+ return;
937
+ }
938
+ const content = (0, import_node_fs3.readFileSync)(configPath, "utf8");
939
+ if (content.includes("@kenkaiiii/gg-pixel")) return;
940
+ if (content.includes("serverExternalPackages")) {
941
+ const updated = content.replace(
942
+ /serverExternalPackages\s*:\s*\[([^\]]*)\]/,
943
+ (_match, inside) => {
944
+ const trimmed = inside.trim();
945
+ const sep2 = trimmed.length > 0 ? ", " : "";
946
+ return `serverExternalPackages: [${trimmed}${sep2}"@kenkaiiii/gg-pixel"]`;
947
+ }
948
+ );
949
+ if (updated !== content) (0, import_node_fs3.writeFileSync)(configPath, updated, "utf8");
950
+ return;
951
+ }
952
+ const objStart = /(const\s+\w+\s*:\s*NextConfig\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
953
+ const m = objStart.exec(content);
954
+ if (m) {
955
+ const insertAt = m.index + m[0].length;
956
+ const updated = content.slice(0, insertAt) + `
957
+ serverExternalPackages: ["@kenkaiiii/gg-pixel"],` + content.slice(insertAt);
958
+ (0, import_node_fs3.writeFileSync)(configPath, updated, "utf8");
959
+ }
960
+ }
854
961
  function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
855
962
  const serverPath = (0, import_node_path2.join)(projectRoot, "src/hooks.server.ts");
856
963
  const clientPath = (0, import_node_path2.join)(projectRoot, "src/hooks.client.ts");