UNPKG

@posthog/nextjs-config

Version:

NextJS configuration helper for Posthog 🦔

151 lines (150 loc) • 6.06 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__, { isTurbopackEnabled: ()=>isTurbopackEnabled, hasCompilerHook: ()=>hasCompilerHook, resolveBinaryPath: ()=>resolveBinaryPath, buildLocalBinaryPaths: ()=>buildLocalBinaryPaths, getNextJsVersion: ()=>getNextJsVersion, processSourceMaps: ()=>processSourceMaps }); const external_path_namespaceObject = require("path"); var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject); const external_fs_namespaceObject = require("fs"); var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject); const external_child_process_namespaceObject = require("child_process"); const package_json_namespaceObject = require("next/package.json"); var package_json_default = /*#__PURE__*/ __webpack_require__.n(package_json_namespaceObject); const external_semver_namespaceObject = require("semver"); var external_semver_default = /*#__PURE__*/ __webpack_require__.n(external_semver_namespaceObject); function resolveBinaryPath(envPath, cwd, binName) { const envLocations = envPath.split(external_path_default().delimiter); const localLocations = buildLocalBinaryPaths(cwd); const directories = [ ...new Set([ ...localLocations, ...envLocations ]) ]; for (const directory of directories){ const binaryPath = external_path_default().join(directory, binName); if (external_fs_default().existsSync(binaryPath)) return binaryPath; } throw new Error(`Binary ${binName} not found`); } const buildLocalBinaryPaths = (cwd)=>{ const localPaths = getLocalPaths(external_path_default().resolve(cwd)).map((localPath)=>external_path_default().join(localPath, 'node_modules/.bin')); return localPaths; }; const getLocalPaths = (startPath)=>{ const paths = []; let currentPath = startPath; while(true){ paths.push(currentPath); const parentPath = external_path_default().resolve(currentPath, '..'); if (parentPath === currentPath) break; currentPath = parentPath; } return paths; }; function getNextJsVersion() { return package_json_default().version; } function hasCompilerHook() { const nextJsVersion = getNextJsVersion(); return external_semver_default().gte(nextJsVersion, '15.4.1'); } async function processSourceMaps(posthogOptions, directory) { const cliOptions = []; if (posthogOptions.host) cliOptions.push('--host', posthogOptions.host); cliOptions.push('sourcemap', 'process'); cliOptions.push('--directory', directory); if (posthogOptions.sourcemaps.project) cliOptions.push('--project', posthogOptions.sourcemaps.project); if (posthogOptions.sourcemaps.version) cliOptions.push('--version', posthogOptions.sourcemaps.version); if (posthogOptions.sourcemaps.deleteAfterUpload) cliOptions.push('--delete-after'); const envVars = { ...process.env, POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey, POSTHOG_CLI_ENV_ID: posthogOptions.envId }; await callPosthogCli(cliOptions, envVars, posthogOptions.verbose); } async function callPosthogCli(args, env, verbose) { let binaryLocation; try { var _process_env_PATH; binaryLocation = resolveBinaryPath(null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '', __dirname, 'posthog-cli'); } catch (e) { throw new Error(`Binary ${e} not found. Make sure postinstall script has been allowed for @posthog/cli`); } if (verbose) console.log('running posthog-cli from ', binaryLocation); const child = (0, external_child_process_namespaceObject.spawn)(binaryLocation, [ ...args ], { shell: true, stdio: verbose ? 'inherit' : 'ignore', env, cwd: process.cwd() }); await new Promise((resolve, reject)=>{ child.on('close', (code)=>{ if (0 === code) resolve(); else reject(new Error(`Command failed with code ${code}`)); }); child.on('error', (error)=>{ reject(error); }); }); } function isTurbopackEnabled() { return '1' === process.env.TURBOPACK; } exports.buildLocalBinaryPaths = __webpack_exports__.buildLocalBinaryPaths; exports.getNextJsVersion = __webpack_exports__.getNextJsVersion; exports.hasCompilerHook = __webpack_exports__.hasCompilerHook; exports.isTurbopackEnabled = __webpack_exports__.isTurbopackEnabled; exports.processSourceMaps = __webpack_exports__.processSourceMaps; exports.resolveBinaryPath = __webpack_exports__.resolveBinaryPath; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "buildLocalBinaryPaths", "getNextJsVersion", "hasCompilerHook", "isTurbopackEnabled", "processSourceMaps", "resolveBinaryPath" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });