@posthog/nextjs-config 1.4.1 → 1.5.0

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.
package/dist/config.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { NextConfig } from 'next';
2
+ import { PluginConfig } from '@posthog/webpack-plugin';
2
3
  type NextFuncConfig = (phase: string, { defaultConfig }: {
3
4
  defaultConfig: NextConfig;
4
5
  }) => NextConfig;
@@ -6,32 +7,5 @@ type NextAsyncConfig = (phase: string, { defaultConfig }: {
6
7
  defaultConfig: NextConfig;
7
8
  }) => Promise<NextConfig>;
8
9
  type UserProvidedConfig = NextConfig | NextFuncConfig | NextAsyncConfig;
9
- type LogLevel = 'debug' | 'info' | 'warn' | 'error';
10
- export type PostHogNextConfig = {
11
- cliBinaryPath?: string;
12
- personalApiKey: string;
13
- envId: string;
14
- host?: string;
15
- logLevel?: LogLevel;
16
- sourcemaps?: {
17
- enabled?: boolean;
18
- project?: string;
19
- version?: string;
20
- deleteAfterUpload?: boolean;
21
- };
22
- };
23
- export type PostHogNextConfigComplete = {
24
- cliBinaryPath: string;
25
- personalApiKey: string;
26
- envId: string;
27
- host: string;
28
- logLevel: LogLevel;
29
- sourcemaps: {
30
- enabled: boolean;
31
- project?: string;
32
- version?: string;
33
- deleteAfterUpload: boolean;
34
- };
35
- };
36
- export declare function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogConfig: PostHogNextConfig): NextConfig;
10
+ export declare function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogConfig: PluginConfig): NextConfig;
37
11
  export {};
package/dist/config.js CHANGED
@@ -26,12 +26,11 @@ __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  withPostHogConfig: ()=>withPostHogConfig
28
28
  });
29
- const external_webpack_plugin_js_namespaceObject = require("./webpack-plugin.js");
29
+ const webpack_plugin_namespaceObject = require("@posthog/webpack-plugin");
30
30
  const external_utils_js_namespaceObject = require("./utils.js");
31
- const process_namespaceObject = require("@posthog/core/process");
32
31
  function withPostHogConfig(userNextConfig, posthogConfig) {
33
- const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
34
- const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
32
+ const resolvedConfig = (0, webpack_plugin_namespaceObject.resolveConfig)(posthogConfig);
33
+ const sourceMapEnabled = resolvedConfig.sourcemaps.enabled;
35
34
  const isCompilerHookSupported = (0, external_utils_js_namespaceObject.hasCompilerHook)();
36
35
  const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
37
36
  if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1');
@@ -42,8 +41,8 @@ function withPostHogConfig(userNextConfig, posthogConfig) {
42
41
  ...userConfig,
43
42
  distDir,
44
43
  productionBrowserSourceMaps: sourceMapEnabled,
45
- webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir),
46
- compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete)
44
+ webpack: withWebpackConfig(userWebPackConfig, resolvedConfig),
45
+ compiler: withCompilerConfig(userCompilerConfig, resolvedConfig)
47
46
  };
48
47
  };
49
48
  }
@@ -58,39 +57,16 @@ function resolveUserConfig(userNextConfig, phase, defaultConfig) {
58
57
  if ('object' == typeof userNextConfig) return Promise.resolve(userNextConfig);
59
58
  throw new Error('Invalid user config');
60
59
  }
61
- function resolvePostHogConfig(posthogProvidedConfig) {
62
- const { personalApiKey, envId, host, logLevel, sourcemaps = {} } = posthogProvidedConfig;
63
- var _process_env_PATH, _posthogProvidedConfig_cliBinaryPath;
64
- const cliBinaryPath = null != (_posthogProvidedConfig_cliBinaryPath = posthogProvidedConfig.cliBinaryPath) ? _posthogProvidedConfig_cliBinaryPath : (0, process_namespaceObject.resolveBinaryPath)('posthog-cli', {
65
- path: null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '',
66
- cwd: __dirname
67
- });
68
- var _sourcemaps_enabled, _sourcemaps_deleteAfterUpload;
69
- return {
70
- cliBinaryPath,
71
- personalApiKey,
72
- envId,
73
- host: null != host ? host : 'https://us.i.posthog.com',
74
- logLevel: null != logLevel ? logLevel : 'info',
75
- sourcemaps: {
76
- enabled: null != (_sourcemaps_enabled = sourcemaps.enabled) ? _sourcemaps_enabled : 'production' == process.env.NODE_ENV,
77
- project: sourcemaps.project,
78
- version: sourcemaps.version,
79
- deleteAfterUpload: null != (_sourcemaps_deleteAfterUpload = sourcemaps.deleteAfterUpload) ? _sourcemaps_deleteAfterUpload : true
80
- }
81
- };
82
- }
83
- function withWebpackConfig(userWebpackConfig, posthogConfig, distDir) {
60
+ function withWebpackConfig(userWebpackConfig, posthogConfig) {
84
61
  const defaultWebpackConfig = userWebpackConfig || ((config)=>config);
85
62
  const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
86
63
  return (config, options)=>{
87
64
  const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
88
65
  const webpackConfig = defaultWebpackConfig(config, options);
89
66
  if (sourceMapEnabled) {
90
- if (options.isServer) webpackConfig.devtool = 'source-map';
91
67
  if (!turbopackEnabled) {
92
68
  webpackConfig.plugins = webpackConfig.plugins || [];
93
- webpackConfig.plugins.push(new external_webpack_plugin_js_namespaceObject.SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir));
69
+ webpackConfig.plugins.push(new webpack_plugin_namespaceObject.PosthogWebpackPlugin(posthogConfig));
94
70
  }
95
71
  }
96
72
  return webpackConfig;
package/dist/config.mjs CHANGED
@@ -1,12 +1,8 @@
1
- import { fileURLToPath as __webpack_fileURLToPath__ } from "node:url";
2
- import { dirname as __webpack_dirname__ } from "node:path";
3
- import { SourcemapWebpackPlugin } from "./webpack-plugin.mjs";
1
+ import { PosthogWebpackPlugin, resolveConfig } from "@posthog/webpack-plugin";
4
2
  import { hasCompilerHook, isTurbopackEnabled, processSourceMaps } from "./utils.mjs";
5
- import { resolveBinaryPath } from "@posthog/core/process";
6
- var config_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));
7
3
  function withPostHogConfig(userNextConfig, posthogConfig) {
8
- const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
9
- const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
4
+ const resolvedConfig = resolveConfig(posthogConfig);
5
+ const sourceMapEnabled = resolvedConfig.sourcemaps.enabled;
10
6
  const isCompilerHookSupported = hasCompilerHook();
11
7
  const turbopackEnabled = isTurbopackEnabled();
12
8
  if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1');
@@ -17,8 +13,8 @@ function withPostHogConfig(userNextConfig, posthogConfig) {
17
13
  ...userConfig,
18
14
  distDir,
19
15
  productionBrowserSourceMaps: sourceMapEnabled,
20
- webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir),
21
- compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete)
16
+ webpack: withWebpackConfig(userWebPackConfig, resolvedConfig),
17
+ compiler: withCompilerConfig(userCompilerConfig, resolvedConfig)
22
18
  };
23
19
  };
24
20
  }
@@ -33,39 +29,16 @@ function resolveUserConfig(userNextConfig, phase, defaultConfig) {
33
29
  if ('object' == typeof userNextConfig) return Promise.resolve(userNextConfig);
34
30
  throw new Error('Invalid user config');
35
31
  }
36
- function resolvePostHogConfig(posthogProvidedConfig) {
37
- const { personalApiKey, envId, host, logLevel, sourcemaps = {} } = posthogProvidedConfig;
38
- var _process_env_PATH, _posthogProvidedConfig_cliBinaryPath;
39
- const cliBinaryPath = null != (_posthogProvidedConfig_cliBinaryPath = posthogProvidedConfig.cliBinaryPath) ? _posthogProvidedConfig_cliBinaryPath : resolveBinaryPath('posthog-cli', {
40
- path: null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '',
41
- cwd: config_dirname
42
- });
43
- var _sourcemaps_enabled, _sourcemaps_deleteAfterUpload;
44
- return {
45
- cliBinaryPath,
46
- personalApiKey,
47
- envId,
48
- host: null != host ? host : 'https://us.i.posthog.com',
49
- logLevel: null != logLevel ? logLevel : 'info',
50
- sourcemaps: {
51
- enabled: null != (_sourcemaps_enabled = sourcemaps.enabled) ? _sourcemaps_enabled : 'production' == process.env.NODE_ENV,
52
- project: sourcemaps.project,
53
- version: sourcemaps.version,
54
- deleteAfterUpload: null != (_sourcemaps_deleteAfterUpload = sourcemaps.deleteAfterUpload) ? _sourcemaps_deleteAfterUpload : true
55
- }
56
- };
57
- }
58
- function withWebpackConfig(userWebpackConfig, posthogConfig, distDir) {
32
+ function withWebpackConfig(userWebpackConfig, posthogConfig) {
59
33
  const defaultWebpackConfig = userWebpackConfig || ((config)=>config);
60
34
  const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
61
35
  return (config, options)=>{
62
36
  const turbopackEnabled = isTurbopackEnabled();
63
37
  const webpackConfig = defaultWebpackConfig(config, options);
64
38
  if (sourceMapEnabled) {
65
- if (options.isServer) webpackConfig.devtool = 'source-map';
66
39
  if (!turbopackEnabled) {
67
40
  webpackConfig.plugins = webpackConfig.plugins || [];
68
- webpackConfig.plugins.push(new SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir));
41
+ webpackConfig.plugins.push(new PosthogWebpackPlugin(posthogConfig));
69
42
  }
70
43
  }
71
44
  return webpackConfig;
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { PostHogNextConfigComplete } from './config';
1
+ import { ResolvedPluginConfig } from '@posthog/webpack-plugin';
2
2
  export declare function getNextJsVersion(): string;
3
3
  export declare function hasCompilerHook(): boolean;
4
- export declare function processSourceMaps(posthogOptions: PostHogNextConfigComplete, directory: string): Promise<void>;
4
+ export declare function processSourceMaps(posthogOptions: ResolvedPluginConfig, directory: string): Promise<void>;
5
5
  export declare function isTurbopackEnabled(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/nextjs-config",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "NextJS configuration helper for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,20 +32,21 @@
32
32
  },
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@posthog/cli": "~0.5.9",
35
+ "@posthog/cli": "~0.5.13",
36
36
  "semver": "^7.7.2",
37
- "@posthog/core": "1.5.3"
37
+ "@posthog/core": "1.5.5",
38
+ "@posthog/webpack-plugin": "1.0.0"
38
39
  },
39
40
  "keywords": [
40
41
  "posthog",
41
42
  "nextjs"
42
43
  ],
43
44
  "devDependencies": {
44
- "@rslib/core": "^0.10.5",
45
+ "@rslib/core": "0.10.6",
45
46
  "@types/node": "^22.15.23",
46
47
  "@types/semver": "^7.7.0",
47
- "@types/jest": "^29.5.0",
48
- "jest": "^29.7.0",
48
+ "@types/jest": "^29.5.14",
49
+ "jest": "29.7.0",
49
50
  "next": "^15.4.1",
50
51
  "ts-jest": "29.4.0",
51
52
  "@posthog-tooling/rollup-utils": "1.0.0",
@@ -1,11 +0,0 @@
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
- }
11
- export {};
@@ -1,81 +0,0 @@
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
- console.log('Processing source maps from webpack plugin...');
48
- const posthogOptions = {
49
- ...this.posthogOptions,
50
- sourcemaps: {
51
- ...this.posthogOptions.sourcemaps,
52
- deleteAfterUpload: this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer
53
- }
54
- };
55
- await (0, external_utils_js_namespaceObject.processSourceMaps)(posthogOptions, this.directory);
56
- } catch (error) {
57
- const errorMessage = error instanceof Error ? error.message : error;
58
- return console.error('Error running PostHog sourcemap plugin:', errorMessage);
59
- }
60
- return callback();
61
- };
62
- if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
63
- else compiler.plugin('done', onDone);
64
- }
65
- constructor(posthogOptions, isServer, nextRuntime, distDir){
66
- this.posthogOptions = posthogOptions;
67
- this.isServer = isServer;
68
- this.nextRuntime = nextRuntime;
69
- const resolvedDistDir = external_path_default().resolve(null != distDir ? distDir : '.next');
70
- 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");
71
- 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");
72
- this.directory = this.isServer ? external_path_default().join(resolvedDistDir, 'server') : external_path_default().join(resolvedDistDir, 'static/chunks');
73
- }
74
- }
75
- exports.SourcemapWebpackPlugin = __webpack_exports__.SourcemapWebpackPlugin;
76
- for(var __webpack_i__ in __webpack_exports__)if (-1 === [
77
- "SourcemapWebpackPlugin"
78
- ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
79
- Object.defineProperty(exports, '__esModule', {
80
- value: true
81
- });
@@ -1,37 +0,0 @@
1
- import path from "path";
2
- import { processSourceMaps } 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
- console.log('Processing source maps from webpack plugin...');
10
- const posthogOptions = {
11
- ...this.posthogOptions,
12
- sourcemaps: {
13
- ...this.posthogOptions.sourcemaps,
14
- deleteAfterUpload: this.posthogOptions.sourcemaps.deleteAfterUpload && !this.isServer
15
- }
16
- };
17
- await processSourceMaps(posthogOptions, this.directory);
18
- } catch (error) {
19
- const errorMessage = error instanceof Error ? error.message : error;
20
- return console.error('Error running PostHog sourcemap plugin:', errorMessage);
21
- }
22
- return callback();
23
- };
24
- if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
25
- else compiler.plugin('done', onDone);
26
- }
27
- constructor(posthogOptions, isServer, nextRuntime, distDir){
28
- this.posthogOptions = posthogOptions;
29
- this.isServer = isServer;
30
- this.nextRuntime = nextRuntime;
31
- const resolvedDistDir = path.resolve(null != distDir ? distDir : '.next');
32
- 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");
33
- 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");
34
- this.directory = this.isServer ? path.join(resolvedDistDir, 'server') : path.join(resolvedDistDir, 'static/chunks');
35
- }
36
- }
37
- export { SourcemapWebpackPlugin };