@posthog/webpack-plugin 1.2.27 → 1.3.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.
package/dist/config.d.ts CHANGED
@@ -1,37 +1,9 @@
1
- type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
2
- export interface PluginConfig {
3
- personalApiKey: string;
4
- /** @deprecated Use projectId instead */
5
- envId?: string;
6
- projectId?: string;
7
- host?: string;
8
- logLevel?: LogLevel;
9
- cliBinaryPath?: string;
10
- sourcemaps?: {
11
- enabled?: boolean;
12
- /** @deprecated Use releaseName instead */
13
- project?: string;
14
- releaseName?: string;
15
- /** @deprecated Use releaseVersion instead */
16
- version?: string;
17
- releaseVersion?: string;
18
- deleteAfterUpload?: boolean;
19
- batchSize?: number;
20
- };
21
- }
22
- export interface ResolvedPluginConfig extends Omit<PluginConfig, 'envId' | 'projectId'> {
23
- projectId: string;
24
- host: string;
25
- logLevel: LogLevel;
26
- cliBinaryPath: string;
27
- sourcemaps: {
28
- enabled: boolean;
29
- releaseName?: string;
30
- releaseVersion?: string;
31
- deleteAfterUpload: boolean;
32
- batchSize?: number;
33
- };
34
- }
35
- export declare function resolveConfig(options: PluginConfig): ResolvedPluginConfig;
36
- export {};
1
+ import { PluginConfig as CorePluginConfig, ResolvedPluginConfig as CoreResolvedPluginConfig, ResolveConfigOptions } from '@posthog/core/process';
2
+ export type PluginConfig = CorePluginConfig;
3
+ export type ResolvedPluginConfig = CoreResolvedPluginConfig;
4
+ /**
5
+ * Resolve plugin config with webpack-specific defaults.
6
+ * Webpack defaults sourcemaps.enabled to `process.env.NODE_ENV === 'production'`.
7
+ */
8
+ export declare function resolveConfig(options: PluginConfig, resolveOptions?: ResolveConfigOptions): ResolvedPluginConfig;
37
9
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAEA,KAAK,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAA;AAE9D,MAAM,WAAW,YAAY;IACzB,cAAc,EAAE,MAAM,CAAA;IACtB,wCAAwC;IACxC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,UAAU,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,0CAA0C;QAC1C,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,6CAA6C;QAC7C,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,cAAc,CAAC,EAAE,MAAM,CAAA;QACvB,iBAAiB,CAAC,EAAE,OAAO,CAAA;QAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;KACrB,CAAA;CACJ;AAED,MAAM,WAAW,oBAAqB,SAAQ,IAAI,CAAC,YAAY,EAAE,OAAO,GAAG,WAAW,CAAC;IACnF,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,QAAQ,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;IACrB,UAAU,EAAE;QACR,OAAO,EAAE,OAAO,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,cAAc,CAAC,EAAE,MAAM,CAAA;QACvB,iBAAiB,EAAE,OAAO,CAAA;QAC1B,SAAS,CAAC,EAAE,MAAM,CAAA;KACrB,CAAA;CACJ;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,oBAAoB,CA+BzE"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,IAAI,gBAAgB,EAChC,oBAAoB,IAAI,wBAAwB,EAEhD,oBAAoB,EACvB,MAAM,uBAAuB,CAAA;AAI9B,MAAM,MAAM,YAAY,GAAG,gBAAgB,CAAA;AAC3C,MAAM,MAAM,oBAAoB,GAAG,wBAAwB,CAAA;AAE3D;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,CAAC,EAAE,oBAAoB,GAAG,oBAAoB,CAMhH"}
package/dist/config.js CHANGED
@@ -27,36 +27,12 @@ __webpack_require__.d(__webpack_exports__, {
27
27
  resolveConfig: ()=>resolveConfig
28
28
  });
29
29
  const process_namespaceObject = require("@posthog/core/process");
30
- function resolveConfig(options) {
31
- var _options_projectId;
32
- const projectId = null != (_options_projectId = options.projectId) ? _options_projectId : options.envId;
33
- if (!projectId) throw new Error('projectId is required (envId is deprecated)');
34
- var _options_host;
35
- const host = null != (_options_host = options.host) ? _options_host : 'https://us.i.posthog.com';
36
- var _options_logLevel;
37
- const logLevel = null != (_options_logLevel = options.logLevel) ? _options_logLevel : 'info';
38
- var _process_env_PATH, _options_cliBinaryPath;
39
- const cliBinaryPath = null != (_options_cliBinaryPath = options.cliBinaryPath) ? _options_cliBinaryPath : (0, process_namespaceObject.resolveBinaryPath)('posthog-cli', {
40
- path: null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '',
41
- cwd: __dirname
30
+ function resolveConfig(options, resolveOptions) {
31
+ return (0, process_namespaceObject.resolveConfig)(options, {
32
+ defaultEnabled: 'production' === process.env.NODE_ENV,
33
+ cwd: __dirname,
34
+ ...resolveOptions
42
35
  });
43
- var _options_sourcemaps;
44
- const sourcemaps = null != (_options_sourcemaps = options.sourcemaps) ? _options_sourcemaps : {};
45
- var _sourcemaps_enabled, _sourcemaps_releaseName, _sourcemaps_releaseVersion, _sourcemaps_deleteAfterUpload;
46
- return {
47
- personalApiKey: options.personalApiKey,
48
- projectId,
49
- host,
50
- logLevel,
51
- cliBinaryPath,
52
- sourcemaps: {
53
- enabled: null != (_sourcemaps_enabled = sourcemaps.enabled) ? _sourcemaps_enabled : 'production' === process.env.NODE_ENV,
54
- releaseName: null != (_sourcemaps_releaseName = sourcemaps.releaseName) ? _sourcemaps_releaseName : sourcemaps.project,
55
- releaseVersion: null != (_sourcemaps_releaseVersion = sourcemaps.releaseVersion) ? _sourcemaps_releaseVersion : sourcemaps.version,
56
- deleteAfterUpload: null != (_sourcemaps_deleteAfterUpload = sourcemaps.deleteAfterUpload) ? _sourcemaps_deleteAfterUpload : true,
57
- batchSize: sourcemaps.batchSize
58
- }
59
- };
60
36
  }
61
37
  exports.resolveConfig = __webpack_exports__.resolveConfig;
62
38
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
package/dist/config.mjs CHANGED
@@ -1,36 +1,12 @@
1
1
  import { fileURLToPath as __webpack_fileURLToPath__ } from "node:url";
2
2
  import { dirname as __webpack_dirname__ } from "node:path";
3
- import { resolveBinaryPath } from "@posthog/core/process";
3
+ import { resolveConfig } from "@posthog/core/process";
4
4
  var config_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));
5
- function resolveConfig(options) {
6
- var _options_projectId;
7
- const projectId = null != (_options_projectId = options.projectId) ? _options_projectId : options.envId;
8
- if (!projectId) throw new Error('projectId is required (envId is deprecated)');
9
- var _options_host;
10
- const host = null != (_options_host = options.host) ? _options_host : 'https://us.i.posthog.com';
11
- var _options_logLevel;
12
- const logLevel = null != (_options_logLevel = options.logLevel) ? _options_logLevel : 'info';
13
- var _process_env_PATH, _options_cliBinaryPath;
14
- const cliBinaryPath = null != (_options_cliBinaryPath = options.cliBinaryPath) ? _options_cliBinaryPath : resolveBinaryPath('posthog-cli', {
15
- path: null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '',
16
- cwd: config_dirname
5
+ function config_resolveConfig(options, resolveOptions) {
6
+ return resolveConfig(options, {
7
+ defaultEnabled: 'production' === process.env.NODE_ENV,
8
+ cwd: config_dirname,
9
+ ...resolveOptions
17
10
  });
18
- var _options_sourcemaps;
19
- const sourcemaps = null != (_options_sourcemaps = options.sourcemaps) ? _options_sourcemaps : {};
20
- var _sourcemaps_enabled, _sourcemaps_releaseName, _sourcemaps_releaseVersion, _sourcemaps_deleteAfterUpload;
21
- return {
22
- personalApiKey: options.personalApiKey,
23
- projectId,
24
- host,
25
- logLevel,
26
- cliBinaryPath,
27
- sourcemaps: {
28
- enabled: null != (_sourcemaps_enabled = sourcemaps.enabled) ? _sourcemaps_enabled : 'production' === process.env.NODE_ENV,
29
- releaseName: null != (_sourcemaps_releaseName = sourcemaps.releaseName) ? _sourcemaps_releaseName : sourcemaps.project,
30
- releaseVersion: null != (_sourcemaps_releaseVersion = sourcemaps.releaseVersion) ? _sourcemaps_releaseVersion : sourcemaps.version,
31
- deleteAfterUpload: null != (_sourcemaps_deleteAfterUpload = sourcemaps.deleteAfterUpload) ? _sourcemaps_deleteAfterUpload : true,
32
- batchSize: sourcemaps.batchSize
33
- }
34
- };
35
11
  }
36
- export { resolveConfig };
12
+ export { config_resolveConfig as resolveConfig };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export declare class PosthogWebpackPlugin {
6
6
  resolvedConfig: ResolvedPluginConfig;
7
7
  logger: Logger;
8
8
  constructor(pluginConfig: PluginConfig);
9
+ constructor(pluginConfig: ResolvedPluginConfig, resolved: true);
9
10
  apply(compiler: webpack.Compiler): void;
10
11
  processSourceMaps(compilation: webpack.Compilation, config: ResolvedPluginConfig): Promise<void>;
11
12
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAgB,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAiB,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAC5E,OAAO,OAAO,MAAM,SAAS,CAAA;AAI7B,cAAc,UAAU,CAAA;AAExB,qBAAa,oBAAoB;IAC7B,cAAc,EAAE,oBAAoB,CAAA;IACpC,MAAM,EAAE,MAAM,CAAA;gBAEF,YAAY,EAAE,YAAY;IAatC,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;IA0BjC,iBAAiB,CAAC,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CAiDzG"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAgB,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAiB,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAE5E,OAAO,OAAO,MAAM,SAAS,CAAA;AAG7B,cAAc,UAAU,CAAA;AAExB,qBAAa,oBAAoB;IAC7B,cAAc,EAAE,oBAAoB,CAAA;IACpC,MAAM,EAAE,MAAM,CAAA;gBAEF,YAAY,EAAE,YAAY;gBAC1B,YAAY,EAAE,oBAAoB,EAAE,QAAQ,EAAE,IAAI;IAQ9D,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;IA4BjC,iBAAiB,CAAC,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CAqBzG"}
package/dist/index.js CHANGED
@@ -74,7 +74,7 @@ var __webpack_exports__ = {};
74
74
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
75
75
  class PosthogWebpackPlugin {
76
76
  apply(compiler) {
77
- new compiler.webpack.SourceMapDevToolPlugin({
77
+ if (this.resolvedConfig.sourcemaps.enabled) new compiler.webpack.SourceMapDevToolPlugin({
78
78
  filename: '[file].map',
79
79
  noSources: false,
80
80
  moduleFilenameTemplate: '[resource-path]',
@@ -94,41 +94,24 @@ var __webpack_exports__ = {};
94
94
  else throw new Error('PosthogWebpackPlugin is not compatible with webpack version < 5');
95
95
  }
96
96
  async processSourceMaps(compilation, config) {
97
+ if (!config.sourcemaps.enabled) return;
97
98
  const outputDirectory = compilation.outputOptions.path;
98
- const args = [];
99
- args.push('sourcemap', 'process');
100
99
  const chunkArray = Array.from(compilation.chunks);
101
100
  if (0 == chunkArray.length) return;
101
+ const filePaths = [];
102
102
  chunkArray.forEach((chunk)=>chunk.files.forEach((file)=>{
103
103
  const chunkPath = path__WEBPACK_IMPORTED_MODULE_3___default().resolve(outputDirectory, file);
104
- args.push('--file', chunkPath);
104
+ filePaths.push(chunkPath);
105
105
  }));
106
- if (config.sourcemaps.releaseName) args.push('--release-name', config.sourcemaps.releaseName);
107
- if (config.sourcemaps.releaseVersion) args.push('--release-version', config.sourcemaps.releaseVersion);
108
- if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
109
- if (config.sourcemaps.batchSize) args.push('--batch-size', config.sourcemaps.batchSize.toString());
110
- await (0, _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__.spawnLocal)(config.cliBinaryPath, args, {
111
- cwd: process.cwd(),
112
- env: {
113
- RUST_LOG: `posthog_cli=${config.logLevel}`,
114
- ...process.env,
115
- POSTHOG_CLI_HOST: config.host,
116
- POSTHOG_CLI_API_KEY: config.personalApiKey,
117
- POSTHOG_CLI_PROJECT_ID: config.projectId
118
- },
119
- stdio: 'inherit'
106
+ await (0, _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__.runSourcemapCli)(config, {
107
+ filePaths
120
108
  });
121
109
  }
122
- constructor(pluginConfig){
110
+ constructor(pluginConfig, resolved){
123
111
  this.logger = (0, _posthog_core__WEBPACK_IMPORTED_MODULE_0__.createLogger)('[PostHog Webpack]');
124
- this.resolvedConfig = (0, _config__WEBPACK_IMPORTED_MODULE_1__.resolveConfig)(pluginConfig);
125
- assertValue(this.resolvedConfig.personalApiKey, "Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config");
126
- assertValue(this.resolvedConfig.projectId, "projectId (or deprecated envId) not provided. If you are using turbo, make sure to add env variables to your turbo config");
112
+ this.resolvedConfig = resolved ? pluginConfig : (0, _config__WEBPACK_IMPORTED_MODULE_1__.resolveConfig)(pluginConfig);
127
113
  }
128
114
  }
129
- function assertValue(value, message) {
130
- if (!value) throw new Error(message);
131
- }
132
115
  })();
133
116
  exports.PosthogWebpackPlugin = __webpack_exports__.PosthogWebpackPlugin;
134
117
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
package/dist/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import { createLogger } from "@posthog/core";
2
2
  import { resolveConfig } from "./config.mjs";
3
- import { spawnLocal } from "@posthog/core/process";
3
+ import { runSourcemapCli } from "@posthog/core/process";
4
4
  import path from "path";
5
5
  export * from "./config.mjs";
6
6
  class PosthogWebpackPlugin {
7
7
  apply(compiler) {
8
- new compiler.webpack.SourceMapDevToolPlugin({
8
+ if (this.resolvedConfig.sourcemaps.enabled) new compiler.webpack.SourceMapDevToolPlugin({
9
9
  filename: '[file].map',
10
10
  noSources: false,
11
11
  moduleFilenameTemplate: '[resource-path]',
@@ -25,39 +25,22 @@ class PosthogWebpackPlugin {
25
25
  else throw new Error('PosthogWebpackPlugin is not compatible with webpack version < 5');
26
26
  }
27
27
  async processSourceMaps(compilation, config) {
28
+ if (!config.sourcemaps.enabled) return;
28
29
  const outputDirectory = compilation.outputOptions.path;
29
- const args = [];
30
- args.push('sourcemap', 'process');
31
30
  const chunkArray = Array.from(compilation.chunks);
32
31
  if (0 == chunkArray.length) return;
32
+ const filePaths = [];
33
33
  chunkArray.forEach((chunk)=>chunk.files.forEach((file)=>{
34
34
  const chunkPath = path.resolve(outputDirectory, file);
35
- args.push('--file', chunkPath);
35
+ filePaths.push(chunkPath);
36
36
  }));
37
- if (config.sourcemaps.releaseName) args.push('--release-name', config.sourcemaps.releaseName);
38
- if (config.sourcemaps.releaseVersion) args.push('--release-version', config.sourcemaps.releaseVersion);
39
- if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
40
- if (config.sourcemaps.batchSize) args.push('--batch-size', config.sourcemaps.batchSize.toString());
41
- await spawnLocal(config.cliBinaryPath, args, {
42
- cwd: process.cwd(),
43
- env: {
44
- RUST_LOG: `posthog_cli=${config.logLevel}`,
45
- ...process.env,
46
- POSTHOG_CLI_HOST: config.host,
47
- POSTHOG_CLI_API_KEY: config.personalApiKey,
48
- POSTHOG_CLI_PROJECT_ID: config.projectId
49
- },
50
- stdio: 'inherit'
37
+ await runSourcemapCli(config, {
38
+ filePaths
51
39
  });
52
40
  }
53
- constructor(pluginConfig){
41
+ constructor(pluginConfig, resolved){
54
42
  this.logger = createLogger('[PostHog Webpack]');
55
- this.resolvedConfig = resolveConfig(pluginConfig);
56
- assertValue(this.resolvedConfig.personalApiKey, "Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config");
57
- assertValue(this.resolvedConfig.projectId, "projectId (or deprecated envId) not provided. If you are using turbo, make sure to add env variables to your turbo config");
43
+ this.resolvedConfig = resolved ? pluginConfig : resolveConfig(pluginConfig);
58
44
  }
59
45
  }
60
- function assertValue(value, message) {
61
- if (!value) throw new Error(message);
62
- }
63
46
  export { PosthogWebpackPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/webpack-plugin",
3
- "version": "1.2.27",
3
+ "version": "1.3.1",
4
4
  "description": "Webpack plugin for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,8 +16,8 @@
16
16
  "module": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.ts",
18
18
  "dependencies": {
19
- "@posthog/cli": "~0.5.29",
20
- "@posthog/core": "1.23.4"
19
+ "@posthog/cli": "~0.7.3",
20
+ "@posthog/core": "1.24.1"
21
21
  },
22
22
  "files": [
23
23
  "dist",
package/src/config.ts CHANGED
@@ -1,71 +1,23 @@
1
- import { resolveBinaryPath } from '@posthog/core/process'
1
+ import {
2
+ PluginConfig as CorePluginConfig,
3
+ ResolvedPluginConfig as CoreResolvedPluginConfig,
4
+ resolveConfig as coreResolveConfig,
5
+ ResolveConfigOptions,
6
+ } from '@posthog/core/process'
2
7
 
3
- type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'
8
+ // Re-export types for backward compatibility consumers importing from @posthog/webpack-plugin
9
+ // will continue to get the same types.
10
+ export type PluginConfig = CorePluginConfig
11
+ export type ResolvedPluginConfig = CoreResolvedPluginConfig
4
12
 
5
- export interface PluginConfig {
6
- personalApiKey: string
7
- /** @deprecated Use projectId instead */
8
- envId?: string
9
- projectId?: string
10
- host?: string
11
- logLevel?: LogLevel
12
- cliBinaryPath?: string
13
- sourcemaps?: {
14
- enabled?: boolean
15
- /** @deprecated Use releaseName instead */
16
- project?: string
17
- releaseName?: string
18
- /** @deprecated Use releaseVersion instead */
19
- version?: string
20
- releaseVersion?: string
21
- deleteAfterUpload?: boolean
22
- batchSize?: number
23
- }
24
- }
25
-
26
- export interface ResolvedPluginConfig extends Omit<PluginConfig, 'envId' | 'projectId'> {
27
- projectId: string
28
- host: string
29
- logLevel: LogLevel
30
- cliBinaryPath: string
31
- sourcemaps: {
32
- enabled: boolean
33
- releaseName?: string
34
- releaseVersion?: string
35
- deleteAfterUpload: boolean
36
- batchSize?: number
37
- }
38
- }
39
-
40
- export function resolveConfig(options: PluginConfig): ResolvedPluginConfig {
41
- const projectId = options.projectId ?? options.envId
42
- if (!projectId) {
43
- throw new Error('projectId is required (envId is deprecated)')
44
- }
45
-
46
- const host = options.host ?? 'https://us.i.posthog.com'
47
- const logLevel = options.logLevel ?? 'info'
48
- const cliBinaryPath =
49
- options.cliBinaryPath ??
50
- resolveBinaryPath('posthog-cli', {
51
- path: process.env.PATH ?? '',
52
- cwd: __dirname,
53
- })
54
-
55
- const sourcemaps = options.sourcemaps ?? {}
56
-
57
- return {
58
- personalApiKey: options.personalApiKey,
59
- projectId,
60
- host,
61
- logLevel,
62
- cliBinaryPath,
63
- sourcemaps: {
64
- enabled: sourcemaps.enabled ?? process.env.NODE_ENV === 'production',
65
- releaseName: sourcemaps.releaseName ?? sourcemaps.project,
66
- releaseVersion: sourcemaps.releaseVersion ?? sourcemaps.version,
67
- deleteAfterUpload: sourcemaps.deleteAfterUpload ?? true,
68
- batchSize: sourcemaps.batchSize,
69
- },
70
- }
13
+ /**
14
+ * Resolve plugin config with webpack-specific defaults.
15
+ * Webpack defaults sourcemaps.enabled to `process.env.NODE_ENV === 'production'`.
16
+ */
17
+ export function resolveConfig(options: PluginConfig, resolveOptions?: ResolveConfigOptions): ResolvedPluginConfig {
18
+ return coreResolveConfig(options, {
19
+ defaultEnabled: process.env.NODE_ENV === 'production',
20
+ cwd: __dirname,
21
+ ...resolveOptions,
22
+ })
71
23
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Logger, createLogger } from '@posthog/core'
2
2
  import { PluginConfig, resolveConfig, ResolvedPluginConfig } from './config'
3
+ import { runSourcemapCli } from '@posthog/core/process'
3
4
  import webpack from 'webpack'
4
- import { spawnLocal } from '@posthog/core/process'
5
5
  import path from 'path'
6
6
 
7
7
  export * from './config'
@@ -10,26 +10,24 @@ export class PosthogWebpackPlugin {
10
10
  resolvedConfig: ResolvedPluginConfig
11
11
  logger: Logger
12
12
 
13
- constructor(pluginConfig: PluginConfig) {
13
+ constructor(pluginConfig: PluginConfig)
14
+ constructor(pluginConfig: ResolvedPluginConfig, resolved: true)
15
+ constructor(pluginConfig: PluginConfig | ResolvedPluginConfig, resolved?: boolean) {
14
16
  this.logger = createLogger('[PostHog Webpack]')
15
- this.resolvedConfig = resolveConfig(pluginConfig)
16
- assertValue(
17
- this.resolvedConfig.personalApiKey,
18
- `Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config`
19
- )
20
- assertValue(
21
- this.resolvedConfig.projectId,
22
- `projectId (or deprecated envId) not provided. If you are using turbo, make sure to add env variables to your turbo config`
23
- )
17
+ this.resolvedConfig = resolved
18
+ ? (pluginConfig as ResolvedPluginConfig)
19
+ : resolveConfig(pluginConfig as PluginConfig)
24
20
  }
25
21
 
26
22
  apply(compiler: webpack.Compiler): void {
27
- new compiler.webpack.SourceMapDevToolPlugin({
28
- filename: '[file].map',
29
- noSources: false,
30
- moduleFilenameTemplate: '[resource-path]',
31
- append: this.resolvedConfig.sourcemaps.deleteAfterUpload ? false : undefined,
32
- }).apply(compiler)
23
+ if (this.resolvedConfig.sourcemaps.enabled) {
24
+ new compiler.webpack.SourceMapDevToolPlugin({
25
+ filename: '[file].map',
26
+ noSources: false,
27
+ moduleFilenameTemplate: '[resource-path]',
28
+ append: this.resolvedConfig.sourcemaps.deleteAfterUpload ? false : undefined,
29
+ }).apply(compiler)
30
+ }
33
31
 
34
32
  const onDone = async (stats: webpack.Stats, callback: any): Promise<void> => {
35
33
  callback = callback || (() => {})
@@ -50,12 +48,9 @@ export class PosthogWebpackPlugin {
50
48
  }
51
49
 
52
50
  async processSourceMaps(compilation: webpack.Compilation, config: ResolvedPluginConfig): Promise<void> {
53
- const outputDirectory = compilation.outputOptions.path
54
- const args = []
55
-
56
- // chunks are output outside of the output directory for server chunks
57
- args.push('sourcemap', 'process')
51
+ if (!config.sourcemaps.enabled) return
58
52
 
53
+ const outputDirectory = compilation.outputOptions.path
59
54
  const chunkArray = Array.from(compilation.chunks)
60
55
 
61
56
  if (chunkArray.length == 0) {
@@ -63,45 +58,14 @@ export class PosthogWebpackPlugin {
63
58
  return
64
59
  }
65
60
 
61
+ const filePaths: string[] = []
66
62
  chunkArray.forEach((chunk) =>
67
63
  chunk.files.forEach((file) => {
68
64
  const chunkPath = path.resolve(outputDirectory, file)
69
- args.push('--file', chunkPath)
65
+ filePaths.push(chunkPath)
70
66
  })
71
67
  )
72
68
 
73
- if (config.sourcemaps.releaseName) {
74
- args.push('--release-name', config.sourcemaps.releaseName)
75
- }
76
-
77
- if (config.sourcemaps.releaseVersion) {
78
- args.push('--release-version', config.sourcemaps.releaseVersion)
79
- }
80
-
81
- if (config.sourcemaps.deleteAfterUpload) {
82
- args.push('--delete-after')
83
- }
84
-
85
- if (config.sourcemaps.batchSize) {
86
- args.push('--batch-size', config.sourcemaps.batchSize.toString())
87
- }
88
-
89
- await spawnLocal(config.cliBinaryPath, args, {
90
- cwd: process.cwd(),
91
- env: {
92
- RUST_LOG: `posthog_cli=${config.logLevel}`,
93
- ...process.env,
94
- POSTHOG_CLI_HOST: config.host,
95
- POSTHOG_CLI_API_KEY: config.personalApiKey,
96
- POSTHOG_CLI_PROJECT_ID: config.projectId,
97
- },
98
- stdio: 'inherit',
99
- })
100
- }
101
- }
102
-
103
- function assertValue(value: any, message: string): void {
104
- if (!value) {
105
- throw new Error(message)
69
+ await runSourcemapCli(config, { filePaths })
106
70
  }
107
71
  }