@posthog/nextjs-config
Version:
NextJS configuration helper for Posthog 🦔
114 lines (113 loc) • 5.51 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 external_webpack_plugin_js_namespaceObject = require("./webpack-plugin.js");
const external_utils_js_namespaceObject = require("./utils.js");
function withPostHogConfig(userNextConfig, posthogConfig) {
const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
const sourceMapEnabled = posthogNextConfigComplete.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');
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 = (0, external_utils_js_namespaceObject.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 external_webpack_plugin_js_namespaceObject.SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir));
}
}
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));
posthogConfig.verbose && console.debug('Processing source maps from compilation hook...');
await (0, external_utils_js_namespaceObject.processSourceMaps)(posthogConfig, 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
});
;