UNPKG

@posthog/nextjs-config

Version:

NextJS configuration helper for Posthog 🦔

95 lines (94 loc) • 4.41 kB
"use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.n = (module)=>{ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module; __webpack_require__.d(getter, { a: getter }); return getter; }; })(); (()=>{ __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__, { SourcemapWebpackPlugin: ()=>SourcemapWebpackPlugin }); const external_path_namespaceObject = require("path"); var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject); const external_utils_js_namespaceObject = require("./utils.js"); 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 (0, external_utils_js_namespaceObject.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 (0, external_utils_js_namespaceObject.callPosthogCli)(cliOptions, envVars, this.posthogOptions.verbose); } constructor(posthogOptions, isServer, nextRuntime, distDir){ this.posthogOptions = posthogOptions; this.isServer = isServer; this.nextRuntime = nextRuntime; const resolvedDistDir = external_path_default().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 ? external_path_default().join(resolvedDistDir, 'server') : external_path_default().join(resolvedDistDir, 'static/chunks'); } } exports.SourcemapWebpackPlugin = __webpack_exports__.SourcemapWebpackPlugin; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "SourcemapWebpackPlugin" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });