UNPKG

@posthog/nextjs-config

Version:

NextJS configuration helper for Posthog 🦔

51 lines (50 loc) • 2.66 kB
import path from "path"; import { callPosthogCli } from "./utils.mjs"; class SourcemapWebpackPlugin { apply(compiler) { if ('edge' === this.nextRuntime) return; const onDone = async (_, callback)=>{ callback = callback || (()=>{}); try { await this.runInject(); await this.runUpload(); } catch (error) { const errorMessage = error instanceof Error ? error.message : error; return console.error('Error running PostHog sourcemap plugin:', errorMessage); } return callback(); }; if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone); else compiler.plugin('done', onDone); } async runInject() { const cliOptions = []; cliOptions.push('sourcemap', 'inject', '--directory', this.directory); await callPosthogCli(cliOptions, process.env, this.posthogOptions.verbose); } async runUpload() { const cliOptions = []; if (this.posthogOptions.host) cliOptions.push('--host', this.posthogOptions.host); cliOptions.push('sourcemap', 'upload'); cliOptions.push('--directory', this.directory); if (this.posthogOptions.sourcemaps.project) cliOptions.push('--project', this.posthogOptions.sourcemaps.project); if (this.posthogOptions.sourcemaps.version) cliOptions.push('--version', this.posthogOptions.sourcemaps.version); if (this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer) cliOptions.push('--delete-after'); const envVars = { ...process.env, POSTHOG_CLI_TOKEN: this.posthogOptions.personalApiKey, POSTHOG_CLI_ENV_ID: this.posthogOptions.envId }; await callPosthogCli(cliOptions, envVars, this.posthogOptions.verbose); } constructor(posthogOptions, isServer, nextRuntime, distDir){ this.posthogOptions = posthogOptions; this.isServer = isServer; this.nextRuntime = nextRuntime; const resolvedDistDir = path.resolve(null != distDir ? distDir : '.next'); if (!this.posthogOptions.personalApiKey) throw new Error("Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config"); if (!this.posthogOptions.envId) throw new Error("Environment ID not provided. If you are using turbo, make sure to add env variables to your turbo config"); this.directory = this.isServer ? path.join(resolvedDistDir, 'server') : path.join(resolvedDistDir, 'static/chunks'); } } export { SourcemapWebpackPlugin };