@posthog/nextjs-config 1.1.2 → 1.2.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.js CHANGED
@@ -27,26 +27,22 @@ __webpack_require__.d(__webpack_exports__, {
27
27
  withPostHogConfig: ()=>withPostHogConfig
28
28
  });
29
29
  const external_webpack_plugin_js_namespaceObject = require("./webpack-plugin.js");
30
+ const external_utils_js_namespaceObject = require("./utils.js");
30
31
  function withPostHogConfig(userNextConfig, posthogConfig) {
31
32
  const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
33
+ const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
34
+ const isCompilerHookSupported = (0, external_utils_js_namespaceObject.hasCompilerHook)();
35
+ const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
36
+ if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1');
32
37
  return async (phase, param)=>{
33
38
  let { defaultConfig } = param;
34
- const { webpack: userWebPackConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
35
- const defaultWebpackConfig = userWebPackConfig || ((config)=>config);
36
- const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
39
+ const { webpack: userWebPackConfig, compiler: userCompilerConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
37
40
  return {
38
41
  ...userConfig,
39
42
  distDir,
40
43
  productionBrowserSourceMaps: sourceMapEnabled,
41
- webpack: (config, options)=>{
42
- const webpackConfig = defaultWebpackConfig(config, options);
43
- if (webpackConfig && options.isServer && sourceMapEnabled) webpackConfig.devtool = 'source-map';
44
- if (sourceMapEnabled) {
45
- webpackConfig.plugins = webpackConfig.plugins || [];
46
- webpackConfig.plugins.push(new external_webpack_plugin_js_namespaceObject.SourcemapWebpackPlugin(posthogNextConfigComplete, options.isServer, options.nextRuntime, distDir));
47
- }
48
- return webpackConfig;
49
- }
44
+ webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir),
45
+ compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete)
50
46
  };
51
47
  };
52
48
  }
@@ -77,6 +73,37 @@ function resolvePostHogConfig(posthogProvidedConfig) {
77
73
  }
78
74
  };
79
75
  }
76
+ function withWebpackConfig(userWebpackConfig, posthogConfig, distDir) {
77
+ const defaultWebpackConfig = userWebpackConfig || ((config)=>config);
78
+ const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
79
+ return (config, options)=>{
80
+ const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
81
+ const webpackConfig = defaultWebpackConfig(config, options);
82
+ if (sourceMapEnabled) {
83
+ if (options.isServer) webpackConfig.devtool = 'source-map';
84
+ if (!turbopackEnabled) {
85
+ webpackConfig.plugins = webpackConfig.plugins || [];
86
+ webpackConfig.plugins.push(new external_webpack_plugin_js_namespaceObject.SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir));
87
+ }
88
+ }
89
+ return webpackConfig;
90
+ };
91
+ }
92
+ function withCompilerConfig(userCompilerConfig, posthogConfig) {
93
+ const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
94
+ const turbopackEnabled = (0, external_utils_js_namespaceObject.isTurbopackEnabled)();
95
+ if (sourceMapEnabled && turbopackEnabled && (0, external_utils_js_namespaceObject.hasCompilerHook)()) {
96
+ const newConfig = userCompilerConfig || {};
97
+ const userCompilerHook = null == userCompilerConfig ? void 0 : userCompilerConfig.runAfterProductionCompile;
98
+ newConfig.runAfterProductionCompile = async (config)=>{
99
+ await (null == userCompilerHook ? void 0 : userCompilerHook(config));
100
+ posthogConfig.verbose && console.debug('Processing source maps from compilation hook...');
101
+ await (0, external_utils_js_namespaceObject.processSourceMaps)(posthogConfig, config.distDir);
102
+ };
103
+ return newConfig;
104
+ }
105
+ return userCompilerConfig;
106
+ }
80
107
  exports.withPostHogConfig = __webpack_exports__.withPostHogConfig;
81
108
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
82
109
  "withPostHogConfig"
package/dist/config.mjs CHANGED
@@ -1,24 +1,20 @@
1
1
  import { SourcemapWebpackPlugin } from "./webpack-plugin.mjs";
2
+ import { hasCompilerHook, isTurbopackEnabled, processSourceMaps } from "./utils.mjs";
2
3
  function withPostHogConfig(userNextConfig, posthogConfig) {
3
4
  const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig);
5
+ const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
6
+ const isCompilerHookSupported = hasCompilerHook();
7
+ const turbopackEnabled = isTurbopackEnabled();
8
+ if (turbopackEnabled && !isCompilerHookSupported) console.warn('[@posthog/nextjs-config] Turbopack support is only available with next version >= 15.4.1');
4
9
  return async (phase, param)=>{
5
10
  let { defaultConfig } = param;
6
- const { webpack: userWebPackConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
7
- const defaultWebpackConfig = userWebPackConfig || ((config)=>config);
8
- const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled;
11
+ const { webpack: userWebPackConfig, compiler: userCompilerConfig, distDir, ...userConfig } = await resolveUserConfig(userNextConfig, phase, defaultConfig);
9
12
  return {
10
13
  ...userConfig,
11
14
  distDir,
12
15
  productionBrowserSourceMaps: sourceMapEnabled,
13
- webpack: (config, options)=>{
14
- const webpackConfig = defaultWebpackConfig(config, options);
15
- if (webpackConfig && options.isServer && sourceMapEnabled) webpackConfig.devtool = 'source-map';
16
- if (sourceMapEnabled) {
17
- webpackConfig.plugins = webpackConfig.plugins || [];
18
- webpackConfig.plugins.push(new SourcemapWebpackPlugin(posthogNextConfigComplete, options.isServer, options.nextRuntime, distDir));
19
- }
20
- return webpackConfig;
21
- }
16
+ webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir),
17
+ compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete)
22
18
  };
23
19
  };
24
20
  }
@@ -49,4 +45,35 @@ function resolvePostHogConfig(posthogProvidedConfig) {
49
45
  }
50
46
  };
51
47
  }
48
+ function withWebpackConfig(userWebpackConfig, posthogConfig, distDir) {
49
+ const defaultWebpackConfig = userWebpackConfig || ((config)=>config);
50
+ const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
51
+ return (config, options)=>{
52
+ const turbopackEnabled = isTurbopackEnabled();
53
+ const webpackConfig = defaultWebpackConfig(config, options);
54
+ if (sourceMapEnabled) {
55
+ if (options.isServer) webpackConfig.devtool = 'source-map';
56
+ if (!turbopackEnabled) {
57
+ webpackConfig.plugins = webpackConfig.plugins || [];
58
+ webpackConfig.plugins.push(new SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir));
59
+ }
60
+ }
61
+ return webpackConfig;
62
+ };
63
+ }
64
+ function withCompilerConfig(userCompilerConfig, posthogConfig) {
65
+ const sourceMapEnabled = posthogConfig.sourcemaps.enabled;
66
+ const turbopackEnabled = isTurbopackEnabled();
67
+ if (sourceMapEnabled && turbopackEnabled && hasCompilerHook()) {
68
+ const newConfig = userCompilerConfig || {};
69
+ const userCompilerHook = null == userCompilerConfig ? void 0 : userCompilerConfig.runAfterProductionCompile;
70
+ newConfig.runAfterProductionCompile = async (config)=>{
71
+ await (null == userCompilerHook ? void 0 : userCompilerHook(config));
72
+ posthogConfig.verbose && console.debug('Processing source maps from compilation hook...');
73
+ await processSourceMaps(posthogConfig, config.distDir);
74
+ };
75
+ return newConfig;
76
+ }
77
+ return userCompilerConfig;
78
+ }
52
79
  export { withPostHogConfig };
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import { PostHogNextConfigComplete } from './config';
1
2
  export declare function resolveBinaryPath(envPath: string, cwd: string, binName: string): string;
2
3
  export declare const buildLocalBinaryPaths: (cwd: string) => string[];
3
- export declare function callPosthogCli(args: string[], env: NodeJS.ProcessEnv, verbose: boolean): Promise<void>;
4
+ export declare function getNextJsVersion(): string;
5
+ export declare function hasCompilerHook(): boolean;
6
+ export declare function processSourceMaps(posthogOptions: PostHogNextConfigComplete, directory: string): Promise<void>;
7
+ export declare function isTurbopackEnabled(): boolean;
package/dist/utils.js CHANGED
@@ -33,15 +33,22 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
- buildLocalBinaryPaths: ()=>buildLocalBinaryPaths,
36
+ isTurbopackEnabled: ()=>isTurbopackEnabled,
37
+ hasCompilerHook: ()=>hasCompilerHook,
37
38
  resolveBinaryPath: ()=>resolveBinaryPath,
38
- callPosthogCli: ()=>callPosthogCli
39
+ buildLocalBinaryPaths: ()=>buildLocalBinaryPaths,
40
+ getNextJsVersion: ()=>getNextJsVersion,
41
+ processSourceMaps: ()=>processSourceMaps
39
42
  });
40
43
  const external_path_namespaceObject = require("path");
41
44
  var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
42
45
  const external_fs_namespaceObject = require("fs");
43
46
  var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject);
44
47
  const external_child_process_namespaceObject = require("child_process");
48
+ const package_json_namespaceObject = require("next/package.json");
49
+ var package_json_default = /*#__PURE__*/ __webpack_require__.n(package_json_namespaceObject);
50
+ const external_semver_namespaceObject = require("semver");
51
+ var external_semver_default = /*#__PURE__*/ __webpack_require__.n(external_semver_namespaceObject);
45
52
  function resolveBinaryPath(envPath, cwd, binName) {
46
53
  const envLocations = envPath.split(external_path_default().delimiter);
47
54
  const localLocations = buildLocalBinaryPaths(cwd);
@@ -72,6 +79,28 @@ const getLocalPaths = (startPath)=>{
72
79
  }
73
80
  return paths;
74
81
  };
82
+ function getNextJsVersion() {
83
+ return package_json_default().version;
84
+ }
85
+ function hasCompilerHook() {
86
+ const nextJsVersion = getNextJsVersion();
87
+ return external_semver_default().gte(nextJsVersion, '15.4.1');
88
+ }
89
+ async function processSourceMaps(posthogOptions, directory) {
90
+ const cliOptions = [];
91
+ if (posthogOptions.host) cliOptions.push('--host', posthogOptions.host);
92
+ cliOptions.push('sourcemap', 'process');
93
+ cliOptions.push('--directory', directory);
94
+ if (posthogOptions.sourcemaps.project) cliOptions.push('--project', posthogOptions.sourcemaps.project);
95
+ if (posthogOptions.sourcemaps.version) cliOptions.push('--version', posthogOptions.sourcemaps.version);
96
+ if (posthogOptions.sourcemaps.deleteAfterUpload) cliOptions.push('--delete-after');
97
+ const envVars = {
98
+ ...process.env,
99
+ POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
100
+ POSTHOG_CLI_ENV_ID: posthogOptions.envId
101
+ };
102
+ await callPosthogCli(cliOptions, envVars, posthogOptions.verbose);
103
+ }
75
104
  async function callPosthogCli(args, env, verbose) {
76
105
  let binaryLocation;
77
106
  try {
@@ -98,12 +127,21 @@ async function callPosthogCli(args, env, verbose) {
98
127
  });
99
128
  });
100
129
  }
130
+ function isTurbopackEnabled() {
131
+ return '1' === process.env.TURBOPACK;
132
+ }
101
133
  exports.buildLocalBinaryPaths = __webpack_exports__.buildLocalBinaryPaths;
102
- exports.callPosthogCli = __webpack_exports__.callPosthogCli;
134
+ exports.getNextJsVersion = __webpack_exports__.getNextJsVersion;
135
+ exports.hasCompilerHook = __webpack_exports__.hasCompilerHook;
136
+ exports.isTurbopackEnabled = __webpack_exports__.isTurbopackEnabled;
137
+ exports.processSourceMaps = __webpack_exports__.processSourceMaps;
103
138
  exports.resolveBinaryPath = __webpack_exports__.resolveBinaryPath;
104
139
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
105
140
  "buildLocalBinaryPaths",
106
- "callPosthogCli",
141
+ "getNextJsVersion",
142
+ "hasCompilerHook",
143
+ "isTurbopackEnabled",
144
+ "processSourceMaps",
107
145
  "resolveBinaryPath"
108
146
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
109
147
  Object.defineProperty(exports, '__esModule', {
package/dist/utils.mjs CHANGED
@@ -3,6 +3,10 @@ import { dirname as __webpack_dirname__ } from "node:path";
3
3
  import path from "path";
4
4
  import fs from "fs";
5
5
  import { spawn } from "child_process";
6
+ import next_package from "next/package.json" with {
7
+ type: "json"
8
+ };
9
+ import semver from "semver";
6
10
  var utils_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));
7
11
  function resolveBinaryPath(envPath, cwd, binName) {
8
12
  const envLocations = envPath.split(path.delimiter);
@@ -34,6 +38,28 @@ const getLocalPaths = (startPath)=>{
34
38
  }
35
39
  return paths;
36
40
  };
41
+ function getNextJsVersion() {
42
+ return next_package.version;
43
+ }
44
+ function hasCompilerHook() {
45
+ const nextJsVersion = getNextJsVersion();
46
+ return semver.gte(nextJsVersion, '15.4.1');
47
+ }
48
+ async function processSourceMaps(posthogOptions, directory) {
49
+ const cliOptions = [];
50
+ if (posthogOptions.host) cliOptions.push('--host', posthogOptions.host);
51
+ cliOptions.push('sourcemap', 'process');
52
+ cliOptions.push('--directory', directory);
53
+ if (posthogOptions.sourcemaps.project) cliOptions.push('--project', posthogOptions.sourcemaps.project);
54
+ if (posthogOptions.sourcemaps.version) cliOptions.push('--version', posthogOptions.sourcemaps.version);
55
+ if (posthogOptions.sourcemaps.deleteAfterUpload) cliOptions.push('--delete-after');
56
+ const envVars = {
57
+ ...process.env,
58
+ POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
59
+ POSTHOG_CLI_ENV_ID: posthogOptions.envId
60
+ };
61
+ await callPosthogCli(cliOptions, envVars, posthogOptions.verbose);
62
+ }
37
63
  async function callPosthogCli(args, env, verbose) {
38
64
  let binaryLocation;
39
65
  try {
@@ -60,4 +86,7 @@ async function callPosthogCli(args, env, verbose) {
60
86
  });
61
87
  });
62
88
  }
63
- export { buildLocalBinaryPaths, callPosthogCli, resolveBinaryPath };
89
+ function isTurbopackEnabled() {
90
+ return '1' === process.env.TURBOPACK;
91
+ }
92
+ export { buildLocalBinaryPaths, getNextJsVersion, hasCompilerHook, isTurbopackEnabled, processSourceMaps, resolveBinaryPath };
@@ -7,7 +7,5 @@ export declare class SourcemapWebpackPlugin {
7
7
  directory: string;
8
8
  constructor(posthogOptions: PostHogNextConfigComplete, isServer: boolean, nextRuntime: NextRuntime, distDir?: string);
9
9
  apply(compiler: any): void;
10
- runInject(): Promise<void>;
11
- runUpload(): Promise<void>;
12
10
  }
13
11
  export {};
@@ -44,8 +44,15 @@ class SourcemapWebpackPlugin {
44
44
  const onDone = async (_, callback)=>{
45
45
  callback = callback || (()=>{});
46
46
  try {
47
- await this.runInject();
48
- await this.runUpload();
47
+ this.posthogOptions.verbose && 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);
49
56
  } catch (error) {
50
57
  const errorMessage = error instanceof Error ? error.message : error;
51
58
  return console.error('Error running PostHog sourcemap plugin:', errorMessage);
@@ -55,26 +62,6 @@ class SourcemapWebpackPlugin {
55
62
  if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
56
63
  else compiler.plugin('done', onDone);
57
64
  }
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
65
  constructor(posthogOptions, isServer, nextRuntime, distDir){
79
66
  this.posthogOptions = posthogOptions;
80
67
  this.isServer = isServer;
@@ -1,13 +1,20 @@
1
1
  import path from "path";
2
- import { callPosthogCli } from "./utils.mjs";
2
+ import { processSourceMaps } from "./utils.mjs";
3
3
  class SourcemapWebpackPlugin {
4
4
  apply(compiler) {
5
5
  if ('edge' === this.nextRuntime) return;
6
6
  const onDone = async (_, callback)=>{
7
7
  callback = callback || (()=>{});
8
8
  try {
9
- await this.runInject();
10
- await this.runUpload();
9
+ this.posthogOptions.verbose && 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);
11
18
  } catch (error) {
12
19
  const errorMessage = error instanceof Error ? error.message : error;
13
20
  return console.error('Error running PostHog sourcemap plugin:', errorMessage);
@@ -17,26 +24,6 @@ class SourcemapWebpackPlugin {
17
24
  if (compiler.hooks) compiler.hooks.done.tapAsync('SourcemapWebpackPlugin', onDone);
18
25
  else compiler.plugin('done', onDone);
19
26
  }
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
27
  constructor(posthogOptions, isServer, nextRuntime, distDir){
41
28
  this.posthogOptions = posthogOptions;
42
29
  this.isServer = isServer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/nextjs-config",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "NextJS configuration helper for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,7 +32,8 @@
32
32
  },
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@posthog/cli": "^0.3.6"
35
+ "@posthog/cli": "^0.4.2",
36
+ "semver": "^7.7.2"
36
37
  },
37
38
  "keywords": [
38
39
  "posthog",
@@ -41,8 +42,9 @@
41
42
  "devDependencies": {
42
43
  "@rslib/core": "^0.10.5",
43
44
  "@types/node": "^22.15.23",
44
- "next": "^12.1.0",
45
+ "@types/semver": "^7.7.0",
45
46
  "jest": "^29.7.0",
47
+ "next": "^15.4.1",
46
48
  "ts-jest": "29.4.0",
47
49
  "@posthog-tooling/rollup-utils": "1.0.0",
48
50
  "@posthog-tooling/tsconfig-base": "1.0.0"