@posthog/rollup-plugin 1.1.11 → 1.1.13

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAA2C,MAAM,QAAQ,CAAA;AAC7E,OAAO,EAAiC,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAG/E,MAAM,WAAW,0BAA0B;IACvC,cAAc,EAAE,MAAM,CAAA;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,UAAU,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,OAAO,CAAA;QAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;KACrB,CAAA;CACJ;AAiBD,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,WAAW,EAAE,0BAA0B,GAqD1E,MAAM,CACd"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAA2C,MAAM,QAAQ,CAAA;AAC7E,OAAO,EAAiC,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAI/E,MAAM,WAAW,0BAA0B;IACvC,cAAc,EAAE,MAAM,CAAA;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,UAAU,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,OAAO,CAAA;QAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;KACrB,CAAA;CACJ;AAiBD,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,WAAW,EAAE,0BAA0B,GA4E1E,MAAM,CACd"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { resolveBinaryPath, spawnLocal } from "@posthog/core/process";
2
2
  import path from "path";
3
+ import promises from "fs/promises";
3
4
  function posthogRollupPlugin(userOptions) {
4
5
  const posthogOptions = resolveOptions(userOptions);
5
6
  return {
@@ -9,43 +10,53 @@ function posthogRollupPlugin(userOptions) {
9
10
  handler (options) {
10
11
  return {
11
12
  ...options,
12
- sourcemap: true
13
+ sourcemap: posthogOptions.sourcemaps.deleteAfterUpload ? 'hidden' : true
13
14
  };
14
15
  }
15
16
  },
16
- async writeBundle (options, bundle) {
17
- if (!posthogOptions.sourcemaps.enabled) return;
18
- const args = [
19
- 'sourcemap',
20
- 'process'
21
- ];
22
- const cliPath = posthogOptions.cliBinaryPath;
23
- if (options.dir) for(const fileName in bundle){
24
- const chunk = bundle[fileName];
25
- if ('chunk' === chunk.type) {
26
- const chunkPath = path.resolve(options.dir, fileName);
27
- args.push('--file', chunkPath);
17
+ writeBundle: {
18
+ sequential: true,
19
+ async handler (options, bundle) {
20
+ if (!posthogOptions.sourcemaps.enabled) return;
21
+ const args = [
22
+ 'sourcemap',
23
+ 'process'
24
+ ];
25
+ const cliPath = posthogOptions.cliBinaryPath;
26
+ const chunks = {};
27
+ const basePaths = [];
28
+ if (options.dir) basePaths.push(options.dir);
29
+ if (options.file) basePaths.push(path.dirname(options.file));
30
+ for(const fileName in bundle){
31
+ const chunk = bundle[fileName];
32
+ if ('chunk' === chunk.type) {
33
+ const chunkPath = path.resolve(...basePaths, fileName);
34
+ chunks[chunkPath] = chunk;
35
+ args.push('--file', chunkPath);
36
+ }
28
37
  }
38
+ if (posthogOptions.sourcemaps.project) args.push('--project', posthogOptions.sourcemaps.project);
39
+ if (posthogOptions.sourcemaps.version) args.push('--version', posthogOptions.sourcemaps.version);
40
+ if (posthogOptions.sourcemaps.deleteAfterUpload) args.push('--delete-after');
41
+ if (posthogOptions.sourcemaps.batchSize) args.push('--batch-size', posthogOptions.sourcemaps.batchSize.toString());
42
+ await spawnLocal(cliPath, args, {
43
+ env: {
44
+ ...process.env,
45
+ RUST_LOG: `posthog_cli=${posthogOptions.logLevel}`,
46
+ POSTHOG_CLI_HOST: posthogOptions.host,
47
+ POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
48
+ POSTHOG_CLI_ENV_ID: posthogOptions.envId
49
+ },
50
+ stdio: 'inherit',
51
+ cwd: process.cwd()
52
+ });
53
+ await Promise.all(Object.entries(chunks).map((param)=>{
54
+ let [chunkPath, chunk] = param;
55
+ return promises.readFile(chunkPath, 'utf8').then((content)=>{
56
+ chunk.code = content;
57
+ });
58
+ }));
29
59
  }
30
- else if (options.file) {
31
- const filePath = path.resolve(options.file);
32
- args.push('--file', filePath);
33
- }
34
- if (posthogOptions.sourcemaps.project) args.push('--project', posthogOptions.sourcemaps.project);
35
- if (posthogOptions.sourcemaps.version) args.push('--version', posthogOptions.sourcemaps.version);
36
- if (posthogOptions.sourcemaps.deleteAfterUpload) args.push('--delete-after');
37
- if (posthogOptions.sourcemaps.batchSize) args.push('--batch-size', posthogOptions.sourcemaps.batchSize.toString());
38
- await spawnLocal(cliPath, args, {
39
- env: {
40
- ...process.env,
41
- RUST_LOG: `posthog_cli=${posthogOptions.logLevel}`,
42
- POSTHOG_CLI_HOST: posthogOptions.host,
43
- POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
44
- POSTHOG_CLI_ENV_ID: posthogOptions.envId
45
- },
46
- stdio: 'inherit',
47
- cwd: process.cwd()
48
- });
49
60
  }
50
61
  };
51
62
  }
@@ -68,7 +79,9 @@ function resolveOptions(userOptions) {
68
79
  sourcemaps: {
69
80
  enabled: null != (_userSourcemaps_enabled = userSourcemaps.enabled) ? _userSourcemaps_enabled : true,
70
81
  deleteAfterUpload: null != (_userSourcemaps_deleteAfterUpload = userSourcemaps.deleteAfterUpload) ? _userSourcemaps_deleteAfterUpload : true,
71
- batchSize: userSourcemaps.batchSize
82
+ batchSize: userSourcemaps.batchSize,
83
+ project: userSourcemaps.project,
84
+ version: userSourcemaps.version
72
85
  }
73
86
  };
74
87
  return posthogOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/rollup-plugin",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "PostHog Rollup plugin",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,9 +23,9 @@
23
23
  },
24
24
  "devDependencies": {
25
25
  "@rslib/core": "0.10.6",
26
- "rollup": "~4.53.2",
27
- "jest": "29.7.0",
28
26
  "@types/jest": "^29.5.14",
27
+ "jest": "29.7.0",
28
+ "rollup": "~4.53.2",
29
29
  "@posthog-tooling/tsconfig-base": "1.1.1"
30
30
  },
31
31
  "scripts": {
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Plugin, OutputOptions, OutputAsset, OutputChunk } from 'rollup'
2
2
  import { spawnLocal, resolveBinaryPath, LogLevel } from '@posthog/core/process'
3
3
  import path from 'node:path'
4
+ import fs from 'node:fs/promises'
4
5
 
5
6
  export interface PostHogRollupPluginOptions {
6
7
  personalApiKey: string
@@ -36,54 +37,77 @@ export default function posthogRollupPlugin(userOptions: PostHogRollupPluginOpti
36
37
  const posthogOptions = resolveOptions(userOptions)
37
38
  return {
38
39
  name: 'posthog-rollup-plugin',
40
+
39
41
  outputOptions: {
40
42
  order: 'post',
41
- handler(options) {
43
+ handler(options: OutputOptions) {
42
44
  return {
43
45
  ...options,
44
- sourcemap: true,
46
+ sourcemap: posthogOptions.sourcemaps.deleteAfterUpload ? 'hidden' : true,
45
47
  }
46
48
  },
47
49
  },
48
- async writeBundle(options: OutputOptions, bundle: { [fileName: string]: OutputAsset | OutputChunk }) {
49
- if (!posthogOptions.sourcemaps.enabled) return
50
- const args = ['sourcemap', 'process']
51
- const cliPath = posthogOptions.cliBinaryPath
52
- if (options.dir) {
50
+
51
+ writeBundle: {
52
+ // Write bundle is executed in parallel, make it sequential to ensure correct order
53
+ sequential: true,
54
+ async handler(options: OutputOptions, bundle: { [fileName: string]: OutputAsset | OutputChunk }) {
55
+ if (!posthogOptions.sourcemaps.enabled) return
56
+ const args = ['sourcemap', 'process']
57
+ const cliPath = posthogOptions.cliBinaryPath
58
+ const chunks: { [fileName: string]: OutputChunk } = {}
59
+ const basePaths = []
60
+
61
+ if (options.dir) {
62
+ basePaths.push(options.dir)
63
+ }
64
+
65
+ if (options.file) {
66
+ basePaths.push(path.dirname(options.file))
67
+ }
68
+
53
69
  for (const fileName in bundle) {
54
70
  const chunk = bundle[fileName]
55
71
  if (chunk.type === 'chunk') {
56
- const chunkPath = path.resolve(options.dir, fileName)
72
+ const chunkPath = path.resolve(...basePaths, fileName)
73
+ chunks[chunkPath] = chunk
57
74
  args.push('--file', chunkPath)
58
75
  }
59
76
  }
60
- } else if (options.file) {
61
- const filePath = path.resolve(options.file)
62
- args.push('--file', filePath)
63
- }
64
- if (posthogOptions.sourcemaps.project) {
65
- args.push('--project', posthogOptions.sourcemaps.project)
66
- }
67
- if (posthogOptions.sourcemaps.version) {
68
- args.push('--version', posthogOptions.sourcemaps.version)
69
- }
70
- if (posthogOptions.sourcemaps.deleteAfterUpload) {
71
- args.push('--delete-after')
72
- }
73
- if (posthogOptions.sourcemaps.batchSize) {
74
- args.push('--batch-size', posthogOptions.sourcemaps.batchSize.toString())
75
- }
76
- await spawnLocal(cliPath, args, {
77
- env: {
78
- ...process.env,
79
- RUST_LOG: `posthog_cli=${posthogOptions.logLevel}`,
80
- POSTHOG_CLI_HOST: posthogOptions.host,
81
- POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
82
- POSTHOG_CLI_ENV_ID: posthogOptions.envId,
83
- },
84
- stdio: 'inherit',
85
- cwd: process.cwd(),
86
- })
77
+
78
+ if (posthogOptions.sourcemaps.project) {
79
+ args.push('--project', posthogOptions.sourcemaps.project)
80
+ }
81
+ if (posthogOptions.sourcemaps.version) {
82
+ args.push('--version', posthogOptions.sourcemaps.version)
83
+ }
84
+ if (posthogOptions.sourcemaps.deleteAfterUpload) {
85
+ args.push('--delete-after')
86
+ }
87
+ if (posthogOptions.sourcemaps.batchSize) {
88
+ args.push('--batch-size', posthogOptions.sourcemaps.batchSize.toString())
89
+ }
90
+ await spawnLocal(cliPath, args, {
91
+ env: {
92
+ ...process.env,
93
+ RUST_LOG: `posthog_cli=${posthogOptions.logLevel}`,
94
+ POSTHOG_CLI_HOST: posthogOptions.host,
95
+ POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey,
96
+ POSTHOG_CLI_ENV_ID: posthogOptions.envId,
97
+ },
98
+ stdio: 'inherit',
99
+ cwd: process.cwd(),
100
+ })
101
+
102
+ // we need to update code for others plugins to work
103
+ await Promise.all(
104
+ Object.entries(chunks).map(([chunkPath, chunk]) =>
105
+ fs.readFile(chunkPath, 'utf8').then((content) => {
106
+ chunk.code = content
107
+ })
108
+ )
109
+ )
110
+ },
87
111
  },
88
112
  } as Plugin
89
113
  }
@@ -110,6 +134,8 @@ function resolveOptions(userOptions: PostHogRollupPluginOptions): ResolvedPostHo
110
134
  enabled: userSourcemaps.enabled ?? true,
111
135
  deleteAfterUpload: userSourcemaps.deleteAfterUpload ?? true,
112
136
  batchSize: userSourcemaps.batchSize,
137
+ project: userSourcemaps.project,
138
+ version: userSourcemaps.version,
113
139
  },
114
140
  }
115
141
  return posthogOptions