@posthog/nextjs-config
Version:
NextJS configuration helper for Posthog 🦔
120 lines (119 loc) • 6.1 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, definition)=>{
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
get: definition[key]
});
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
withPostHogConfig: ()=>withPostHogConfig
});
const webpack_plugin_namespaceObject = require("@posthog/webpack-plugin");
const external_utils_js_namespaceObject = require("./utils.js");
const external_strip_sourcemap_comments_js_namespaceObject = require("./strip-sourcemap-comments.js");
const INVOCATION_TIMEOUT_MS = 5000;
function withPostHogConfig(userNextConfig, posthogConfig) {
const resolvedConfig = (0, webpack_plugin_namespaceObject.resolveConfig)(posthogConfig);
const sourceMapEnabled = resolvedConfig.sourcemaps.enabled;
const isCompilerHookSupported = (0, external_utils_js_namespaceObject.hasCompilerHook)();
const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1');
let invoked = false;
const nextConfigFn = async (phase, param)=>{
let { defaultConfig } = param;
invoked = true;
const { webpack: userWebPackConfig, compiler: userCompilerConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
const nextConfig = {
...userConfig,
distDir,
webpack: withWebpackConfig(userWebPackConfig, resolvedConfig),
compiler: withCompilerConfig(userCompilerConfig, resolvedConfig)
};
if (turbopackEnabled && sourceMapEnabled) nextConfig.productionBrowserSourceMaps = true;
return nextConfig;
};
if ('function' == typeof setTimeout) {
var _timer_unref;
const timer = setTimeout(()=>{
if (!invoked) console.warn("[@posthog/nextjs-config] withPostHogConfig was loaded but Next.js never invoked the config function it returns. This usually means another config wrapper (e.g. withNextIntl, withSentryConfig) is wrapping withPostHogConfig and producing a plain object that drops the PostHog hooks. Move withPostHogConfig(...) to be the OUTERMOST wrapper in next.config.js so source maps upload and other build hooks run. See https://github.com/PostHog/posthog-js/issues/3572");
}, INVOCATION_TIMEOUT_MS);
null == (_timer_unref = timer.unref) || _timer_unref.call(timer);
}
return nextConfigFn;
}
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 withWebpackConfig(userWebpackConfig, posthogConfig) {
const defaultWebpackConfig = userWebpackConfig || ((config)=>config);
const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
return (config, options)=>{
const webpackConfig = defaultWebpackConfig(config, options);
const isServer = options.isServer;
if (sourceMapEnabled) {
if (!turbopackEnabled) {
webpackConfig.plugins = webpackConfig.plugins || [];
let currentConfig = posthogConfig;
if (isServer) currentConfig = {
...posthogConfig,
sourcemaps: {
...posthogConfig.sourcemaps,
deleteAfterUpload: false
}
};
webpackConfig.plugins.push(new webpack_plugin_namespaceObject.PosthogWebpackPlugin(currentConfig, true));
}
}
return webpackConfig;
};
}
function withCompilerConfig(userCompilerConfig, posthogConfig) {
const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
if (sourceMapEnabled && turbopackEnabled && (0, external_utils_js_namespaceObject.hasCompilerHook)()) {
const newConfig = userCompilerConfig || {};
const userCompilerHook = null == userCompilerConfig ? void 0 : userCompilerConfig.runAfterProductionCompile;
newConfig.runAfterProductionCompile = async (config)=>{
await (null == userCompilerHook ? void 0 : userCompilerHook(config));
console.debug('Processing source maps from compilation hook...');
await (0, external_utils_js_namespaceObject.processSourceMaps)(posthogConfig, config.distDir);
if (posthogConfig.sourcemaps.deleteAfterUpload) await (0, external_strip_sourcemap_comments_js_namespaceObject.stripDanglingSourceMapComments)(config.distDir);
};
return newConfig;
}
return userCompilerConfig;
}
exports.withPostHogConfig = __webpack_exports__.withPostHogConfig;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
"withPostHogConfig"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
value: true
});