@posthog/nextjs-config 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __webpack_exports__ = {};
3
+ const external_utils_js_namespaceObject = require("./utils.js");
4
+ describe('buildLocalBinaryPaths', ()=>{
5
+ it('generates possible binary locations', ()=>{
6
+ const cwd = '/home/user';
7
+ const result = (0, external_utils_js_namespaceObject.buildLocalBinaryPaths)(cwd);
8
+ expect(result.includes('/home/user/node_modules/.bin')).toBe(true);
9
+ expect(result.includes('/home/node_modules/.bin')).toBe(true);
10
+ expect(result.includes('/node_modules/.bin')).toBe(true);
11
+ });
12
+ });
13
+ for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
14
+ Object.defineProperty(exports, '__esModule', {
15
+ value: true
16
+ });
@@ -0,0 +1,10 @@
1
+ import { buildLocalBinaryPaths } from "./utils.mjs";
2
+ describe('buildLocalBinaryPaths', ()=>{
3
+ it('generates possible binary locations', ()=>{
4
+ const cwd = '/home/user';
5
+ const result = buildLocalBinaryPaths(cwd);
6
+ expect(result.includes('/home/user/node_modules/.bin')).toBe(true);
7
+ expect(result.includes('/home/node_modules/.bin')).toBe(true);
8
+ expect(result.includes('/node_modules/.bin')).toBe(true);
9
+ });
10
+ });
@@ -0,0 +1,13 @@
1
+ import { PostHogNextConfigComplete } from './config';
2
+ type NextRuntime = 'edge' | 'nodejs' | undefined;
3
+ export declare class SourcemapWebpackPlugin {
4
+ private posthogOptions;
5
+ private isServer;
6
+ private nextRuntime;
7
+ directory: string;
8
+ constructor(posthogOptions: PostHogNextConfigComplete, isServer: boolean, nextRuntime: NextRuntime, distDir?: string);
9
+ apply(compiler: any): void;
10
+ runInject(): Promise<void>;
11
+ runUpload(): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ SourcemapWebpackPlugin: ()=>SourcemapWebpackPlugin
37
+ });
38
+ const external_path_namespaceObject = require("path");
39
+ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
40
+ const external_utils_js_namespaceObject = require("./utils.js");
41
+ class SourcemapWebpackPlugin {
42
+ apply(compiler) {
43
+ if ('edge' === this.nextRuntime) return;
44
+ const onDone = async (_, callback)=>{
45
+ callback = callback || (()=>{});
46
+ try {
47
+ await this.runInject();
48
+ await this.runUpload();
49
+ } catch (error) {
50
+ const errorMessage = error instanceof Error ? error.message : error;
51
+ return console.error('Error running PostHog sourcemap plugin:', errorMessage);
52
+ }
53
+ return callback();
54
+ };
55
+ if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
56
+ else compiler.plugin('done', onDone);
57
+ }
58
+ async runInject() {
59
+ const cliOptions = [];
60
+ cliOptions.push('sourcemap', 'inject', '--directory', this.directory);
61
+ await (0, external_utils_js_namespaceObject.callPosthogCli)(cliOptions, process.env, this.posthogOptions.verbose);
62
+ }
63
+ async runUpload() {
64
+ const cliOptions = [];
65
+ if (this.posthogOptions.host) cliOptions.push('--host', this.posthogOptions.host);
66
+ cliOptions.push('sourcemap', 'upload');
67
+ cliOptions.push('--directory', this.directory);
68
+ if (this.posthogOptions.sourcemaps.project) cliOptions.push('--project', this.posthogOptions.sourcemaps.project);
69
+ if (this.posthogOptions.sourcemaps.version) cliOptions.push('--version', this.posthogOptions.sourcemaps.version);
70
+ if (this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer) cliOptions.push('--delete-after');
71
+ const envVars = {
72
+ ...process.env,
73
+ POSTHOG_CLI_TOKEN: this.posthogOptions.personalApiKey,
74
+ POSTHOG_CLI_ENV_ID: this.posthogOptions.envId
75
+ };
76
+ await (0, external_utils_js_namespaceObject.callPosthogCli)(cliOptions, envVars, this.posthogOptions.verbose);
77
+ }
78
+ constructor(posthogOptions, isServer, nextRuntime, distDir){
79
+ this.posthogOptions = posthogOptions;
80
+ this.isServer = isServer;
81
+ this.nextRuntime = nextRuntime;
82
+ const resolvedDistDir = external_path_default().resolve(null != distDir ? distDir : '.next');
83
+ 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");
84
+ 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");
85
+ this.directory = this.isServer ? external_path_default().join(resolvedDistDir, 'server') : external_path_default().join(resolvedDistDir, 'static/chunks');
86
+ }
87
+ }
88
+ exports.SourcemapWebpackPlugin = __webpack_exports__.SourcemapWebpackPlugin;
89
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
90
+ "SourcemapWebpackPlugin"
91
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
92
+ Object.defineProperty(exports, '__esModule', {
93
+ value: true
94
+ });
@@ -0,0 +1,50 @@
1
+ import path from "path";
2
+ import { callPosthogCli } from "./utils.mjs";
3
+ class SourcemapWebpackPlugin {
4
+ apply(compiler) {
5
+ if ('edge' === this.nextRuntime) return;
6
+ const onDone = async (_, callback)=>{
7
+ callback = callback || (()=>{});
8
+ try {
9
+ await this.runInject();
10
+ await this.runUpload();
11
+ } catch (error) {
12
+ const errorMessage = error instanceof Error ? error.message : error;
13
+ return console.error('Error running PostHog sourcemap plugin:', errorMessage);
14
+ }
15
+ return callback();
16
+ };
17
+ if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
18
+ else compiler.plugin('done', onDone);
19
+ }
20
+ async runInject() {
21
+ const cliOptions = [];
22
+ cliOptions.push('sourcemap', 'inject', '--directory', this.directory);
23
+ await callPosthogCli(cliOptions, process.env, this.posthogOptions.verbose);
24
+ }
25
+ async runUpload() {
26
+ const cliOptions = [];
27
+ if (this.posthogOptions.host) cliOptions.push('--host', this.posthogOptions.host);
28
+ cliOptions.push('sourcemap', 'upload');
29
+ cliOptions.push('--directory', this.directory);
30
+ if (this.posthogOptions.sourcemaps.project) cliOptions.push('--project', this.posthogOptions.sourcemaps.project);
31
+ if (this.posthogOptions.sourcemaps.version) cliOptions.push('--version', this.posthogOptions.sourcemaps.version);
32
+ if (this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer) cliOptions.push('--delete-after');
33
+ const envVars = {
34
+ ...process.env,
35
+ POSTHOG_CLI_TOKEN: this.posthogOptions.personalApiKey,
36
+ POSTHOG_CLI_ENV_ID: this.posthogOptions.envId
37
+ };
38
+ await callPosthogCli(cliOptions, envVars, this.posthogOptions.verbose);
39
+ }
40
+ constructor(posthogOptions, isServer, nextRuntime, distDir){
41
+ this.posthogOptions = posthogOptions;
42
+ this.isServer = isServer;
43
+ this.nextRuntime = nextRuntime;
44
+ const resolvedDistDir = path.resolve(null != distDir ? distDir : '.next');
45
+ 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");
46
+ 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");
47
+ this.directory = this.isServer ? path.join(resolvedDistDir, 'server') : path.join(resolvedDistDir, 'static/chunks');
48
+ }
49
+ }
50
+ export { SourcemapWebpackPlugin };
package/package.json CHANGED
@@ -1,42 +1,65 @@
1
1
  {
2
2
  "name": "@posthog/nextjs-config",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "NextJS configuration helper for Posthog 🦔",
5
- "main": "./lib/index.cjs",
6
- "module": "./lib/index.mjs",
7
- "types": "./lib/index.d.ts",
8
- "scripts": {
9
- "prepublishOnly": "cd .. && yarn build"
10
- },
11
5
  "repository": {
12
6
  "type": "git",
13
- "url": "https://github.com/PostHog/posthog-js-lite.git",
14
- "directory": "posthog-nextjs-config"
7
+ "url": "https://github.com/PostHog/posthog-js.git",
8
+ "directory": "packages/nextjs-config"
15
9
  },
16
10
  "author": {
17
11
  "name": "PostHog",
18
12
  "email": "hey@posthog.com",
19
13
  "url": "https://posthog.com"
20
14
  },
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.mjs",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ }
29
+ },
21
30
  "engines": {
22
31
  "node": ">=18.0.0"
23
32
  },
24
33
  "license": "MIT",
25
34
  "dependencies": {
26
- "@posthog/cli": "^0.3.5"
35
+ "@posthog/cli": "^0.3.6"
27
36
  },
28
37
  "keywords": [
29
38
  "posthog",
30
39
  "nextjs"
31
40
  ],
32
41
  "devDependencies": {
42
+ "@rslib/core": "^0.10.5",
33
43
  "@types/node": "^22.15.23",
34
- "next": "^12.1.0"
44
+ "next": "^12.1.0",
45
+ "jest": "^29.7.0",
46
+ "ts-jest": "29.4.0",
47
+ "@posthog-tooling/tsconfig-base": "1.0.0",
48
+ "@posthog-tooling/rollup-utils": "1.0.0"
35
49
  },
36
50
  "peerDependencies": {
37
51
  "next": ">12.1.0"
38
52
  },
39
53
  "files": [
40
- "lib/"
41
- ]
42
- }
54
+ "dist/"
55
+ ],
56
+ "scripts": {
57
+ "clean": "rimraf dist",
58
+ "lint": "eslint src",
59
+ "lint:fix": "eslint src --fix",
60
+ "build": "rslib build",
61
+ "test:unit": "jest",
62
+ "dev": "rslib build --watch",
63
+ "package": "pnpm pack --out $PACKAGE_DEST/%s.tgz"
64
+ }
65
+ }
package/lib/index.cjs DELETED
@@ -1,210 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var child_process = require('child_process');
6
- var path = require('path');
7
- var fs = require('fs');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
12
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
13
-
14
- function resolveBinaryPath(envPath, cwd, binName) {
15
- const envLocations = envPath.split(path__default["default"].delimiter);
16
- const localLocations = buildLocalBinaryPaths(cwd);
17
- const directories = [...new Set([...envLocations, ...localLocations])];
18
- for (const directory of directories) {
19
- const binaryPath = path__default["default"].join(directory, binName);
20
- if (fs__default["default"].existsSync(binaryPath)) {
21
- return binaryPath;
22
- }
23
- }
24
- throw new Error(`Binary ${binName} not found`);
25
- }
26
- const buildLocalBinaryPaths = cwd => {
27
- const localPaths = getLocalPaths(path__default["default"].resolve(cwd)).map(localPath => path__default["default"].join(localPath, 'node_modules/.bin'));
28
- return localPaths;
29
- };
30
- const getLocalPaths = startPath => {
31
- const paths = [];
32
- let currentPath = startPath;
33
- while (true) {
34
- paths.push(currentPath);
35
- const parentPath = path__default["default"].resolve(currentPath, '..');
36
- // If we've reached the root directory, stop
37
- if (parentPath === currentPath) {
38
- break;
39
- }
40
- currentPath = parentPath;
41
- }
42
- return paths;
43
- };
44
-
45
- class SourcemapWebpackPlugin {
46
- constructor(posthogOptions, isServer, nextRuntime, distDir) {
47
- this.posthogOptions = posthogOptions;
48
- this.isServer = isServer;
49
- this.nextRuntime = nextRuntime;
50
- const resolvedDistDir = path__default["default"].resolve(distDir ?? '.next');
51
- if (!this.posthogOptions.personalApiKey) {
52
- throw new Error(`Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config`);
53
- }
54
- if (!this.posthogOptions.envId) {
55
- throw new Error(`Environment ID not provided. If you are using turbo, make sure to add env variables to your turbo config`);
56
- }
57
- this.directory = this.isServer ? path__default["default"].join(resolvedDistDir, 'server') : path__default["default"].join(resolvedDistDir, 'static/chunks');
58
- }
59
- apply(compiler) {
60
- if (this.nextRuntime === 'edge') {
61
- // TODO: edge and nodejs runtime output files in the same location
62
- // to support edge runtime we need a way to pass a list of files to the cli
63
- return;
64
- }
65
- const onDone = async (_, callback) => {
66
- callback = callback || (() => {});
67
- try {
68
- await this.runInject();
69
- await this.runUpload();
70
- } catch (error) {
71
- const errorMessage = error instanceof Error ? error.message : error;
72
- return console.error('Error running PostHog sourcemap plugin:', errorMessage);
73
- }
74
- return callback();
75
- };
76
- if (compiler.hooks) {
77
- compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
78
- } else {
79
- compiler.plugin('done', onDone);
80
- }
81
- }
82
- async runInject() {
83
- const cliOptions = [];
84
- cliOptions.push('sourcemap', 'inject', '--directory', this.directory);
85
- await callPosthogCli(cliOptions, process.env, this.posthogOptions.verbose);
86
- }
87
- async runUpload() {
88
- const cliOptions = [];
89
- if (this.posthogOptions.host) {
90
- cliOptions.push('--host', this.posthogOptions.host);
91
- }
92
- cliOptions.push('sourcemap', 'upload');
93
- cliOptions.push('--directory', this.directory);
94
- if (this.posthogOptions.sourcemaps.project) {
95
- cliOptions.push('--project', this.posthogOptions.sourcemaps.project);
96
- }
97
- if (this.posthogOptions.sourcemaps.version) {
98
- cliOptions.push('--version', this.posthogOptions.sourcemaps.version);
99
- }
100
- if (this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer) {
101
- cliOptions.push('--delete-after');
102
- }
103
- // Add env variables
104
- const envVars = {
105
- ...process.env,
106
- POSTHOG_CLI_TOKEN: this.posthogOptions.personalApiKey,
107
- POSTHOG_CLI_ENV_ID: this.posthogOptions.envId
108
- };
109
- await callPosthogCli(cliOptions, envVars, this.posthogOptions.verbose);
110
- }
111
- }
112
- async function callPosthogCli(args, env, verbose) {
113
- let binaryLocation;
114
- try {
115
- binaryLocation = resolveBinaryPath(process.env.PATH ?? '', __dirname, 'posthog-cli');
116
- } catch (e) {
117
- throw new Error(`Binary ${e} not found. Make sure postinstall script has been allowed for @posthog/cli`);
118
- }
119
- if (verbose) {
120
- console.log('running posthog-cli from ', binaryLocation);
121
- }
122
- const child = child_process.spawn(binaryLocation, [...args], {
123
- stdio: verbose ? 'inherit' : 'ignore',
124
- env,
125
- cwd: process.cwd()
126
- });
127
- await new Promise((resolve, reject) => {
128
- child.on('close', code => {
129
- if (code === 0) {
130
- resolve();
131
- } else {
132
- reject(new Error(`Command failed with code ${code}`));
133
- }
134
- });
135
- child.on('error', error => {
136
- reject(error);
137
- });
138
- });
139
- }
140
-
141
- function withPostHogConfig(userNextConfig, posthogConfig) {
142
- const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
143
- return async (phase, {
144
- defaultConfig
145
- }) => {
146
- const {
147
- webpack: userWebPackConfig,
148
- distDir,
149
- ...userConfig
150
- } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
151
- const defaultWebpackConfig = userWebPackConfig || (config => config);
152
- const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
153
- return {
154
- ...userConfig,
155
- distDir,
156
- productionBrowserSourceMaps: sourceMapEnabled,
157
- webpack: (config, options) => {
158
- const webpackConfig = defaultWebpackConfig(config, options);
159
- if (webpackConfig && options.isServer && sourceMapEnabled) {
160
- webpackConfig.devtool = 'source-map';
161
- }
162
- if (sourceMapEnabled) {
163
- webpackConfig.plugins = webpackConfig.plugins || [];
164
- webpackConfig.plugins.push(new SourcemapWebpackPlugin(posthogNextConfigComplete, options.isServer, options.nextRuntime, distDir));
165
- }
166
- return webpackConfig;
167
- }
168
- };
169
- };
170
- }
171
- function resolveUserConfig(userNextConfig, phase, defaultConfig) {
172
- if (typeof userNextConfig === 'function') {
173
- const maybePromise = userNextConfig(phase, {
174
- defaultConfig
175
- });
176
- if (maybePromise instanceof Promise) {
177
- return maybePromise;
178
- } else {
179
- return Promise.resolve(maybePromise);
180
- }
181
- } else if (typeof userNextConfig === 'object') {
182
- return Promise.resolve(userNextConfig);
183
- } else {
184
- throw new Error('Invalid user config');
185
- }
186
- }
187
- function resolvePostHogConfig(posthogProvidedConfig) {
188
- const {
189
- personalApiKey,
190
- envId,
191
- host,
192
- verbose,
193
- sourcemaps = {}
194
- } = posthogProvidedConfig;
195
- return {
196
- personalApiKey,
197
- envId,
198
- host: host ?? 'https://us.posthog.com',
199
- verbose: verbose ?? true,
200
- sourcemaps: {
201
- enabled: sourcemaps.enabled ?? process.env.NODE_ENV == 'production',
202
- project: sourcemaps.project,
203
- version: sourcemaps.version,
204
- deleteAfterUpload: sourcemaps.deleteAfterUpload ?? true
205
- }
206
- };
207
- }
208
-
209
- exports.withPostHogConfig = withPostHogConfig;
210
- //# sourceMappingURL=index.cjs.map
package/lib/index.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/webpack-plugin.ts","../src/config.ts"],"sourcesContent":["import path from 'path'\nimport fs from 'fs'\n\nexport function resolveBinaryPath(envPath: string, cwd: string, binName: string): string {\n const envLocations = envPath.split(path.delimiter)\n const localLocations = buildLocalBinaryPaths(cwd)\n const directories = [...new Set([...envLocations, ...localLocations])]\n for (const directory of directories) {\n const binaryPath = path.join(directory, binName)\n if (fs.existsSync(binaryPath)) {\n return binaryPath\n }\n }\n throw new Error(`Binary ${binName} not found`)\n}\n\nexport const buildLocalBinaryPaths = (cwd: string): string[] => {\n const localPaths = getLocalPaths(path.resolve(cwd)).map((localPath: string) =>\n path.join(localPath, 'node_modules/.bin')\n )\n return localPaths\n}\n\nconst getLocalPaths = (startPath: string): string[] => {\n const paths: string[] = []\n let currentPath = startPath\n\n while (true) {\n paths.push(currentPath)\n const parentPath = path.resolve(currentPath, '..')\n\n // If we've reached the root directory, stop\n if (parentPath === currentPath) {\n break\n }\n\n currentPath = parentPath\n }\n\n return paths\n}\n","import { PostHogNextConfigComplete } from './config'\nimport { spawn } from 'child_process'\nimport path from 'path'\nimport { resolveBinaryPath } from './utils'\n\ntype NextRuntime = 'edge' | 'nodejs' | undefined\n\nexport class SourcemapWebpackPlugin {\n directory: string\n\n constructor(\n private posthogOptions: PostHogNextConfigComplete,\n private isServer: boolean,\n private nextRuntime: NextRuntime,\n distDir?: string\n ) {\n const resolvedDistDir = path.resolve(distDir ?? '.next')\n if (!this.posthogOptions.personalApiKey) {\n throw new Error(\n `Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config`\n )\n }\n if (!this.posthogOptions.envId) {\n throw new Error(\n `Environment ID not provided. If you are using turbo, make sure to add env variables to your turbo config`\n )\n }\n this.directory = this.isServer ? path.join(resolvedDistDir, 'server') : path.join(resolvedDistDir, 'static/chunks')\n }\n\n apply(compiler: any): void {\n if (this.nextRuntime === 'edge') {\n // TODO: edge and nodejs runtime output files in the same location\n // to support edge runtime we need a way to pass a list of files to the cli\n return\n }\n\n const onDone = async (_: any, callback: any): Promise<void> => {\n callback = callback || (() => {})\n try {\n await this.runInject()\n await this.runUpload()\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : error\n return console.error('Error running PostHog sourcemap plugin:', errorMessage)\n }\n return callback()\n }\n\n if (compiler.hooks) {\n compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone)\n } else {\n compiler.plugin('done', onDone)\n }\n }\n\n async runInject(): Promise<void> {\n const cliOptions = []\n cliOptions.push('sourcemap', 'inject', '--directory', this.directory)\n await callPosthogCli(cliOptions, process.env, this.posthogOptions.verbose)\n }\n\n async runUpload(): Promise<void> {\n const cliOptions = []\n if (this.posthogOptions.host) {\n cliOptions.push('--host', this.posthogOptions.host)\n }\n cliOptions.push('sourcemap', 'upload')\n cliOptions.push('--directory', this.directory)\n if (this.posthogOptions.sourcemaps.project) {\n cliOptions.push('--project', this.posthogOptions.sourcemaps.project)\n }\n if (this.posthogOptions.sourcemaps.version) {\n cliOptions.push('--version', this.posthogOptions.sourcemaps.version)\n }\n if (this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer) {\n cliOptions.push('--delete-after')\n }\n // Add env variables\n const envVars = {\n ...process.env,\n POSTHOG_CLI_TOKEN: this.posthogOptions.personalApiKey,\n POSTHOG_CLI_ENV_ID: this.posthogOptions.envId,\n }\n await callPosthogCli(cliOptions, envVars, this.posthogOptions.verbose)\n }\n}\n\nasync function callPosthogCli(args: string[], env: NodeJS.ProcessEnv, verbose: boolean): Promise<void> {\n let binaryLocation\n try {\n binaryLocation = resolveBinaryPath(process.env.PATH ?? '', __dirname, 'posthog-cli')\n } catch (e) {\n throw new Error(`Binary ${e} not found. Make sure postinstall script has been allowed for @posthog/cli`)\n }\n\n if (verbose) {\n console.log('running posthog-cli from ', binaryLocation)\n }\n\n const child = spawn(binaryLocation, [...args], {\n stdio: verbose ? 'inherit' : 'ignore',\n env,\n cwd: process.cwd(),\n })\n\n await new Promise<void>((resolve, reject) => {\n child.on('close', (code) => {\n if (code === 0) {\n resolve()\n } else {\n reject(new Error(`Command failed with code ${code}`))\n }\n })\n\n child.on('error', (error) => {\n reject(error)\n })\n })\n}\n","import type { NextConfig } from 'next'\nimport { SourcemapWebpackPlugin } from './webpack-plugin'\n\ntype NextFuncConfig = (phase: string, { defaultConfig }: { defaultConfig: NextConfig }) => NextConfig\ntype NextAsyncConfig = (phase: string, { defaultConfig }: { defaultConfig: NextConfig }) => Promise<NextConfig>\ntype UserProvidedConfig = NextConfig | NextFuncConfig | NextAsyncConfig\n\nexport type PostHogNextConfig = {\n personalApiKey: string\n envId: string\n host?: string\n verbose?: boolean\n sourcemaps?: {\n enabled?: boolean\n project?: string\n version?: string\n deleteAfterUpload?: boolean\n }\n}\n\nexport type PostHogNextConfigComplete = {\n personalApiKey: string\n envId: string\n host: string\n verbose: boolean\n sourcemaps: {\n enabled: boolean\n project?: string\n version?: string\n deleteAfterUpload: boolean\n }\n}\n\nexport function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogConfig: PostHogNextConfig): NextConfig {\n const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig)\n return async (phase: string, { defaultConfig }: { defaultConfig: NextConfig }) => {\n const {\n webpack: userWebPackConfig,\n distDir,\n ...userConfig\n } = await resolveUserConfig(userNextConfig, phase, defaultConfig)\n const defaultWebpackConfig = userWebPackConfig || ((config: any) => config)\n const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled\n return {\n ...userConfig,\n distDir,\n productionBrowserSourceMaps: sourceMapEnabled,\n webpack: (config: any, options: any) => {\n const webpackConfig = defaultWebpackConfig(config, options)\n if (webpackConfig && options.isServer && sourceMapEnabled) {\n webpackConfig.devtool = 'source-map'\n }\n if (sourceMapEnabled) {\n webpackConfig.plugins = webpackConfig.plugins || []\n webpackConfig.plugins.push(\n new SourcemapWebpackPlugin(posthogNextConfigComplete, options.isServer, options.nextRuntime, distDir)\n )\n }\n return webpackConfig\n },\n }\n }\n}\n\nfunction resolveUserConfig(\n userNextConfig: UserProvidedConfig,\n phase: string,\n defaultConfig: NextConfig\n): Promise<NextConfig> {\n if (typeof userNextConfig === 'function') {\n const maybePromise = userNextConfig(phase, { defaultConfig })\n if (maybePromise instanceof Promise) {\n return maybePromise\n } else {\n return Promise.resolve(maybePromise)\n }\n } else if (typeof userNextConfig === 'object') {\n return Promise.resolve(userNextConfig)\n } else {\n throw new Error('Invalid user config')\n }\n}\n\nfunction resolvePostHogConfig(posthogProvidedConfig: PostHogNextConfig): PostHogNextConfigComplete {\n const { personalApiKey, envId, host, verbose, sourcemaps = {} } = posthogProvidedConfig\n return {\n personalApiKey,\n envId,\n host: host ?? 'https://us.posthog.com',\n verbose: verbose ?? true,\n sourcemaps: {\n enabled: sourcemaps.enabled ?? process.env.NODE_ENV == 'production',\n project: sourcemaps.project,\n version: sourcemaps.version,\n deleteAfterUpload: sourcemaps.deleteAfterUpload ?? true,\n },\n }\n}\n"],"names":["resolveBinaryPath","envPath","cwd","binName","envLocations","split","path","delimiter","localLocations","buildLocalBinaryPaths","directories","Set","directory","binaryPath","join","fs","existsSync","Error","localPaths","getLocalPaths","resolve","map","localPath","startPath","paths","currentPath","push","parentPath","SourcemapWebpackPlugin","constructor","posthogOptions","isServer","nextRuntime","distDir","resolvedDistDir","personalApiKey","envId","apply","compiler","onDone","_","callback","runInject","runUpload","error","errorMessage","message","console","hooks","done","tapAsync","plugin","cliOptions","callPosthogCli","process","env","verbose","host","sourcemaps","project","version","deleteAfterUpload","envVars","POSTHOG_CLI_TOKEN","POSTHOG_CLI_ENV_ID","args","binaryLocation","PATH","__dirname","e","log","child","spawn","stdio","Promise","reject","on","code","withPostHogConfig","userNextConfig","posthogConfig","posthogNextConfigComplete","resolvePostHogConfig","phase","defaultConfig","webpack","userWebPackConfig","userConfig","resolveUserConfig","defaultWebpackConfig","config","sourceMapEnabled","enabled","productionBrowserSourceMaps","options","webpackConfig","devtool","plugins","maybePromise","posthogProvidedConfig","NODE_ENV"],"mappings":";;;;;;;;;;;;;SAGgBA,iBAAiBA,CAACC,OAAe,EAAEC,GAAW,EAAEC,OAAe,EAAA;EAC7E,MAAMC,YAAY,GAAGH,OAAO,CAACI,KAAK,CAACC,wBAAI,CAACC,SAAS,CAAC,CAAA;AAClD,EAAA,MAAMC,cAAc,GAAGC,qBAAqB,CAACP,GAAG,CAAC,CAAA;AACjD,EAAA,MAAMQ,WAAW,GAAG,CAAC,GAAG,IAAIC,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