@posthog/nextjs-config
Version:
NextJS configuration helper for Posthog 🦔
38 lines (37 loc) • 1.98 kB
JavaScript
import path from "path";
import { processSourceMaps } from "./utils.mjs";
class SourcemapWebpackPlugin {
apply(compiler) {
if ('edge' === this.nextRuntime) return;
const onDone = async (_, callback)=>{
callback = callback || (()=>{});
try {
this.posthogOptions.verbose && console.log('Processing source maps from webpack plugin...');
const posthogOptions = {
...this.posthogOptions,
sourcemaps: {
...this.posthogOptions.sourcemaps,
deleteAfterUpload: this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer
}
};
await processSourceMaps(posthogOptions, this.directory);
} 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);
}
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 };