UNPKG

@posthog/nextjs-config

Version:

NextJS configuration helper for Posthog 🦔

80 lines (79 loc) • 4.03 kB
import { SourcemapWebpackPlugin } from "./webpack-plugin.mjs"; import { hasCompilerHook, isTurbopackEnabled, processSourceMaps } from "./utils.mjs"; function withPostHogConfig(userNextConfig, posthogConfig) { const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig); const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled; const isCompilerHookSupported = hasCompilerHook(); const turbopackEnabled = isTurbopackEnabled(); if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1'); return async (phase, param)=>{ let { defaultConfig } = param; const { webpack: userWebPackConfig, compiler: userCompilerConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig); return { ...userConfig, distDir, productionBrowserSourceMaps: sourceMapEnabled, webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir), compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete) }; }; } function resolveUserConfig(userNextConfig, phase, defaultConfig) { if ('function' == typeof userNextConfig) { const maybePromise = userNextConfig(phase, { defaultConfig }); if (maybePromise instanceof Promise) return maybePromise; return Promise.resolve(maybePromise); } if ('object' == typeof userNextConfig) return Promise.resolve(userNextConfig); throw new Error('Invalid user config'); } function resolvePostHogConfig(posthogProvidedConfig) { const { personalApiKey, envId, host, verbose, sourcemaps = {} } = posthogProvidedConfig; var _sourcemaps_enabled, _sourcemaps_deleteAfterUpload; return { personalApiKey, envId, host: null != host ? host : 'https://us.posthog.com', verbose: null != verbose ? verbose : true, sourcemaps: { enabled: null != (_sourcemaps_enabled = sourcemaps.enabled) ? _sourcemaps_enabled : 'production' == process.env.NODE_ENV, project: sourcemaps.project, version: sourcemaps.version, deleteAfterUpload: null != (_sourcemaps_deleteAfterUpload = sourcemaps.deleteAfterUpload) ? _sourcemaps_deleteAfterUpload : true } }; } function withWebpackConfig(userWebpackConfig, posthogConfig, distDir) { const defaultWebpackConfig = userWebpackConfig || ((config)=>config); const sourceMapEnabled = posthogConfig.sourcemaps.enabled; return (config, options)=>{ const turbopackEnabled = isTurbopackEnabled(); const webpackConfig = defaultWebpackConfig(config, options); if (sourceMapEnabled) { if (options.isServer) webpackConfig.devtool = 'source-map'; if (!turbopackEnabled) { webpackConfig.plugins = webpackConfig.plugins || []; webpackConfig.plugins.push(new SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir)); } } return webpackConfig; }; } function withCompilerConfig(userCompilerConfig, posthogConfig) { const sourceMapEnabled = posthogConfig.sourcemaps.enabled; const turbopackEnabled = isTurbopackEnabled(); if (sourceMapEnabled && turbopackEnabled && hasCompilerHook()) { const newConfig = userCompilerConfig || {}; const userCompilerHook = null == userCompilerConfig ? void 0 : userCompilerConfig.runAfterProductionCompile; newConfig.runAfterProductionCompile = async (config)=>{ await (null == userCompilerHook ? void 0 : userCompilerHook(config)); posthogConfig.verbose && console.debug('Processing source maps from compilation hook...'); await processSourceMaps(posthogConfig, config.distDir); }; return newConfig; } return userCompilerConfig; } export { withPostHogConfig };