@posthog/webpack-plugin 1.3.0 → 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;CAgDzG"}
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,12 +94,8 @@ 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
- 'sourcemap',
100
- 'process',
101
- '--stdin'
102
- ];
103
99
  const chunkArray = Array.from(compilation.chunks);
104
100
  if (0 == chunkArray.length) return;
105
101
  const filePaths = [];
@@ -107,33 +103,15 @@ var __webpack_exports__ = {};
107
103
  const chunkPath = path__WEBPACK_IMPORTED_MODULE_3___default().resolve(outputDirectory, file);
108
104
  filePaths.push(chunkPath);
109
105
  }));
110
- if (config.sourcemaps.releaseName) args.push('--release-name', config.sourcemaps.releaseName);
111
- if (config.sourcemaps.releaseVersion) args.push('--release-version', config.sourcemaps.releaseVersion);
112
- if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
113
- if (config.sourcemaps.batchSize) args.push('--batch-size', config.sourcemaps.batchSize.toString());
114
- await (0, _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__.spawnLocal)(config.cliBinaryPath, args, {
115
- cwd: process.cwd(),
116
- env: {
117
- RUST_LOG: `posthog_cli=${config.logLevel}`,
118
- ...process.env,
119
- POSTHOG_CLI_HOST: config.host,
120
- POSTHOG_CLI_API_KEY: config.personalApiKey,
121
- POSTHOG_CLI_PROJECT_ID: config.projectId
122
- },
123
- stdio: 'inherit',
124
- stdin: filePaths.join('\n') + '\n'
106
+ await (0, _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__.runSourcemapCli)(config, {
107
+ filePaths
125
108
  });
126
109
  }
127
- constructor(pluginConfig){
110
+ constructor(pluginConfig, resolved){
128
111
  this.logger = (0, _posthog_core__WEBPACK_IMPORTED_MODULE_0__.createLogger)('[PostHog Webpack]');
129
- this.resolvedConfig = (0, _config__WEBPACK_IMPORTED_MODULE_1__.resolveConfig)(pluginConfig);
130
- assertValue(this.resolvedConfig.personalApiKey, "Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config");
131
- 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);
132
113
  }
133
114
  }
134
- function assertValue(value, message) {
135
- if (!value) throw new Error(message);
136
- }
137
115
  })();
138
116
  exports.PosthogWebpackPlugin = __webpack_exports__.PosthogWebpackPlugin;
139
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,12 +25,8 @@ 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
- 'sourcemap',
31
- 'process',
32
- '--stdin'
33
- ];
34
30
  const chunkArray = Array.from(compilation.chunks);
35
31
  if (0 == chunkArray.length) return;
36
32
  const filePaths = [];
@@ -38,31 +34,13 @@ class PosthogWebpackPlugin {
38
34
  const chunkPath = path.resolve(outputDirectory, file);
39
35
  filePaths.push(chunkPath);
40
36
  }));
41
- if (config.sourcemaps.releaseName) args.push('--release-name', config.sourcemaps.releaseName);
42
- if (config.sourcemaps.releaseVersion) args.push('--release-version', config.sourcemaps.releaseVersion);
43
- if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
44
- if (config.sourcemaps.batchSize) args.push('--batch-size', config.sourcemaps.batchSize.toString());
45
- await spawnLocal(config.cliBinaryPath, args, {
46
- cwd: process.cwd(),
47
- env: {
48
- RUST_LOG: `posthog_cli=${config.logLevel}`,
49
- ...process.env,
50
- POSTHOG_CLI_HOST: config.host,
51
- POSTHOG_CLI_API_KEY: config.personalApiKey,
52
- POSTHOG_CLI_PROJECT_ID: config.projectId
53
- },
54
- stdio: 'inherit',
55
- stdin: filePaths.join('\n') + '\n'
37
+ await runSourcemapCli(config, {
38
+ filePaths
56
39
  });
57
40
  }
58
- constructor(pluginConfig){
41
+ constructor(pluginConfig, resolved){
59
42
  this.logger = createLogger('[PostHog Webpack]');
60
- this.resolvedConfig = resolveConfig(pluginConfig);
61
- assertValue(this.resolvedConfig.personalApiKey, "Personal API key not provided. If you are using turbo, make sure to add env variables to your turbo config");
62
- 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);
63
44
  }
64
45
  }
65
- function assertValue(value, message) {
66
- if (!value) throw new Error(message);
67
- }
68
46
  export { PosthogWebpackPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/webpack-plugin",
3
- "version": "1.3.0",
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.7.2",
20
- "@posthog/core": "1.24.0"
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,9 +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 = ['sourcemap', 'process', '--stdin']
51
+ if (!config.sourcemaps.enabled) return
55
52
 
53
+ const outputDirectory = compilation.outputOptions.path
56
54
  const chunkArray = Array.from(compilation.chunks)
57
55
 
58
56
  if (chunkArray.length == 0) {
@@ -68,39 +66,6 @@ export class PosthogWebpackPlugin {
68
66
  })
69
67
  )
70
68
 
71
- if (config.sourcemaps.releaseName) {
72
- args.push('--release-name', config.sourcemaps.releaseName)
73
- }
74
-
75
- if (config.sourcemaps.releaseVersion) {
76
- args.push('--release-version', config.sourcemaps.releaseVersion)
77
- }
78
-
79
- if (config.sourcemaps.deleteAfterUpload) {
80
- args.push('--delete-after')
81
- }
82
-
83
- if (config.sourcemaps.batchSize) {
84
- args.push('--batch-size', config.sourcemaps.batchSize.toString())
85
- }
86
-
87
- await spawnLocal(config.cliBinaryPath, args, {
88
- cwd: process.cwd(),
89
- env: {
90
- RUST_LOG: `posthog_cli=${config.logLevel}`,
91
- ...process.env,
92
- POSTHOG_CLI_HOST: config.host,
93
- POSTHOG_CLI_API_KEY: config.personalApiKey,
94
- POSTHOG_CLI_PROJECT_ID: config.projectId,
95
- },
96
- stdio: 'inherit',
97
- stdin: filePaths.join('\n') + '\n',
98
- })
99
- }
100
- }
101
-
102
- function assertValue(value: any, message: string): void {
103
- if (!value) {
104
- throw new Error(message)
69
+ await runSourcemapCli(config, { filePaths })
105
70
  }
106
71
  }