{"version":3,"file":"index.mjs","names":[],"sources":["../../src/plugin/index.ts"],"sourcesContent":["import type { NextConfig } from 'next';\n\ninterface EntrolyticsPluginConfig {\n  /** Your Entrolytics website ID (required) */\n  websiteId: string;\n  /** Public collection API key (required for /collect endpoints) */\n  apiKey?: string;\n  /** Custom analytics host URL */\n  host?: string;\n  /** Proxy configuration for ad-blocker bypass */\n  proxy?: {\n    /** Enable proxy mode */\n    enabled: boolean;\n    /** Custom path for the tracking script (default: '/analytics.js') */\n    scriptPath?: string;\n    /** Custom path for the collection endpoint (default: '/api/collect') */\n    collectPath?: string;\n    /** Proxy mode: 'direct' or 'cloak' */\n    mode?: 'direct' | 'cloak';\n  };\n  /** Enable debug mode */\n  debug?: boolean;\n}\n\n/**\n * Next.js config plugin for Entrolytics.\n *\n * This plugin:\n * - Sets up environment variables for the tracker\n * - Configures rewrites for proxy mode\n * - Adds CSP headers if needed\n *\n * @example\n * ```ts\n * // next.config.ts\n * import { withEntrolytics } from '@entrolytics/nextjs/plugin';\n *\n * export default withEntrolytics({\n *   websiteId: process.env.NEXT_PUBLIC_ENTROLYTICS_WEBSITE_ID!,\n *   host: process.env.NEXT_PUBLIC_ENTROLYTICS_HOST,\n *   proxy: {\n *     enabled: true,\n *     mode: 'cloak',\n *   },\n * })({\n *   // Your existing Next.js config\n *   reactStrictMode: true,\n * });\n * ```\n */\nexport function withEntrolytics(pluginConfig: EntrolyticsPluginConfig) {\n  const { websiteId, apiKey, host, proxy, debug } = pluginConfig;\n\n  return (nextConfig: NextConfig = {}): NextConfig => {\n    // Merge environment variables\n    const env = {\n      ...nextConfig.env,\n      NEXT_PUBLIC_ENTROLYTICS_WEBSITE_ID: websiteId,\n      ...(apiKey && { NEXT_PUBLIC_ENTROLYTICS_API_KEY: apiKey }),\n      ...(host && { NEXT_PUBLIC_ENTROLYTICS_HOST: host }),\n      ...(debug && { NEXT_PUBLIC_ENTROLYTICS_DEBUG: 'true' }),\n    };\n\n    // Setup rewrites for proxy mode\n    const setupRewrites = async () => {\n      const existingRewrites = nextConfig.rewrites\n        ? await nextConfig.rewrites()\n        : { beforeFiles: [], afterFiles: [], fallback: [] };\n\n      // Normalize to object format\n      const rewrites = Array.isArray(existingRewrites)\n        ? { beforeFiles: existingRewrites, afterFiles: [], fallback: [] }\n        : existingRewrites;\n\n      if (proxy?.enabled && host) {\n        const scriptPath = proxy.scriptPath || '/analytics.js';\n        const collectPath = proxy.collectPath || '/api/collect';\n        const baseUrl = host.replace(/\\/$/, '');\n\n        // Add proxy rewrites\n        rewrites.beforeFiles = [\n          ...(rewrites.beforeFiles || []),\n          // Script proxy\n          {\n            source: scriptPath,\n            destination: `${baseUrl}/script.js`,\n          },\n          // Collection endpoint proxy (if not using route handler)\n          {\n            source: `${collectPath}/:path*`,\n            destination: `${baseUrl}/collect/:path*`,\n          },\n        ];\n      }\n\n      return rewrites;\n    };\n\n    // Setup headers for CSP\n    const setupHeaders = async () => {\n      const existingHeaders = nextConfig.headers ? await nextConfig.headers() : [];\n\n      if (host) {\n        // Add CSP header to allow connections to analytics host\n        const analyticsOrigin = new URL(host).origin;\n\n        existingHeaders.push({\n          source: '/(.*)',\n          headers: [\n            {\n              key: 'Content-Security-Policy',\n              value: `connect-src 'self' ${analyticsOrigin}; script-src 'self' 'unsafe-inline' 'unsafe-eval' ${analyticsOrigin};`,\n            },\n          ],\n        });\n      }\n\n      return existingHeaders;\n    };\n\n    return {\n      ...nextConfig,\n      env,\n      rewrites: proxy?.enabled ? setupRewrites : nextConfig.rewrites,\n      // Only add headers if host is specified and user hasn't disabled it\n      ...(host && !nextConfig.headers && { headers: setupHeaders }),\n    };\n  };\n}\n\n/**\n * Helper to create proxy route configuration.\n *\n * @example\n * ```ts\n * import { getProxyRoutes } from '@entrolytics/nextjs/plugin';\n *\n * const routes = getProxyRoutes({\n *   scriptPath: '/stats.js',\n *   collectPath: '/api/stats',\n * });\n *\n * // Use in next.config.ts rewrites\n * ```\n */\nexport function getProxyRoutes(config: {\n  host: string;\n  scriptPath?: string;\n  collectPath?: string;\n}) {\n  const { host, scriptPath = '/analytics.js', collectPath = '/api/collect' } = config;\n  const baseUrl = host.replace(/\\/$/, '');\n\n  return {\n    script: {\n      source: scriptPath,\n      destination: `${baseUrl}/script.js`,\n    },\n    collect: {\n      source: `${collectPath}/:path*`,\n      destination: `${baseUrl}/collect/:path*`,\n    },\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDA,SAAgB,gBAAgB,cAAuC;CACrE,MAAM,EAAE,WAAW,QAAQ,MAAM,OAAO,UAAU;CAElD,QAAQ,aAAyB,CAAC,MAAkB;EAElD,MAAM,MAAM;GACV,GAAG,WAAW;GACd,oCAAoC;GACpC,GAAI,UAAU,EAAE,iCAAiC,OAAO;GACxD,GAAI,QAAQ,EAAE,8BAA8B,KAAK;GACjD,GAAI,SAAS,EAAE,+BAA+B,OAAO;EACvD;EAGA,MAAM,gBAAgB,YAAY;GAChC,MAAM,mBAAmB,WAAW,WAChC,MAAM,WAAW,SAAS,IAC1B;IAAE,aAAa,CAAC;IAAG,YAAY,CAAC;IAAG,UAAU,CAAC;GAAE;GAGpD,MAAM,WAAW,MAAM,QAAQ,gBAAgB,IAC3C;IAAE,aAAa;IAAkB,YAAY,CAAC;IAAG,UAAU,CAAC;GAAE,IAC9D;GAEJ,IAAI,OAAO,WAAW,MAAM;IAC1B,MAAM,aAAa,MAAM,cAAc;IACvC,MAAM,cAAc,MAAM,eAAe;IACzC,MAAM,UAAU,KAAK,QAAQ,OAAO,EAAE;IAGtC,SAAS,cAAc;KACrB,GAAI,SAAS,eAAe,CAAC;KAE7B;MACE,QAAQ;MACR,aAAa,GAAG,QAAQ;KAC1B;KAEA;MACE,QAAQ,GAAG,YAAY;MACvB,aAAa,GAAG,QAAQ;KAC1B;IACF;GACF;GAEA,OAAO;EACT;EAGA,MAAM,eAAe,YAAY;GAC/B,MAAM,kBAAkB,WAAW,UAAU,MAAM,WAAW,QAAQ,IAAI,CAAC;GAE3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