@posthog/webpack-plugin 1.1.1 → 1.1.2

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,EAAE,MAAM,EAAgB,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAiB,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAC5E,OAAO,EAAE,WAAW,EAAS,MAAM,SAAS,CAAA;AAG5C,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,GAAG,GAAG,IAAI;IAsBpB,iBAAiB,CAAC,WAAW,EAAE,WAAW,EAAE,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CAqCjG"}
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,EAAE,WAAW,EAAS,MAAM,SAAS,CAAA;AAI5C,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,GAAG,GAAG,IAAI;IAmBpB,iBAAiB,CAAC,WAAW,EAAE,WAAW,EAAE,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CA+CjG"}
package/dist/index.js CHANGED
@@ -8,6 +8,9 @@ var __webpack_modules__ = {
8
8
  },
9
9
  "@posthog/core/process": function(module) {
10
10
  module.exports = require("@posthog/core/process");
11
+ },
12
+ path: function(module) {
13
+ module.exports = require("path");
11
14
  }
12
15
  };
13
16
  var __webpack_module_cache__ = {};
@@ -59,6 +62,8 @@ var __webpack_exports__ = {};
59
62
  var _posthog_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("@posthog/core");
60
63
  var _config__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./config");
61
64
  var _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("@posthog/core/process");
65
+ var path__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("path");
66
+ var path__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/ __webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_3__);
62
67
  var __WEBPACK_REEXPORT_OBJECT__ = {};
63
68
  for(var __WEBPACK_IMPORT_KEY__ in _config__WEBPACK_IMPORTED_MODULE_1__)if ([
64
69
  "default",
@@ -69,8 +74,6 @@ var __webpack_exports__ = {};
69
74
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
70
75
  class PosthogWebpackPlugin {
71
76
  apply(compiler) {
72
- var _compiler_options_devtool;
73
- compiler.options.devtool = null != (_compiler_options_devtool = compiler.options.devtool) ? _compiler_options_devtool : this.resolvedConfig.sourcemaps.enabled ? 'source-map' : void 0;
74
77
  const onDone = async (stats, callback)=>{
75
78
  callback = callback || (()=>{});
76
79
  try {
@@ -88,15 +91,17 @@ var __webpack_exports__ = {};
88
91
  const outputDirectory = compilation.outputOptions.path;
89
92
  const args = [
90
93
  'sourcemap',
91
- 'process',
92
- '--directory',
93
- outputDirectory
94
+ 'process'
94
95
  ];
95
- for (const chunk of compilation.chunks)for (const file of chunk.files)args.push('--include', `**/${file}`);
96
+ const chunkArray = Array.from(compilation.chunks);
97
+ if (0 == chunkArray.length) return;
98
+ const filePaths = chunkArray.flatMap((chunk)=>Array.from(chunk.files).map((file)=>path__WEBPACK_IMPORTED_MODULE_3___default().resolve(outputDirectory, file)));
99
+ const [commonDirectory, relativeFilePaths] = splitFilePaths(filePaths);
100
+ args.push('--directory', commonDirectory);
101
+ for (const chunkPath of relativeFilePaths)args.push('--include', `**/${chunkPath}`);
96
102
  if (config.sourcemaps.project) args.push('--project', config.sourcemaps.project);
97
103
  if (config.sourcemaps.version) args.push('--version', config.sourcemaps.version);
98
104
  if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
99
- this.logger.info(args);
100
105
  await (0, _posthog_core_process__WEBPACK_IMPORTED_MODULE_2__.spawnLocal)(config.cliBinaryPath, args, {
101
106
  cwd: process.cwd(),
102
107
  env: {
@@ -119,6 +124,27 @@ var __webpack_exports__ = {};
119
124
  function assertValue(value, message) {
120
125
  if (!value) throw new Error(message);
121
126
  }
127
+ function splitFilePaths(absolutePaths) {
128
+ if (!absolutePaths || 0 === absolutePaths.length) return [
129
+ process.cwd(),
130
+ []
131
+ ];
132
+ let commonDir = path__WEBPACK_IMPORTED_MODULE_3___default().dirname(absolutePaths[0]);
133
+ const isCommonAncestor = (candidate)=>absolutePaths.every((p)=>{
134
+ const rel = path__WEBPACK_IMPORTED_MODULE_3___default().relative(candidate, p);
135
+ return '' === rel || !rel.startsWith('..') && !path__WEBPACK_IMPORTED_MODULE_3___default().isAbsolute(rel);
136
+ });
137
+ while(!isCommonAncestor(commonDir)){
138
+ const parent = path__WEBPACK_IMPORTED_MODULE_3___default().dirname(commonDir);
139
+ if (parent === commonDir) break;
140
+ commonDir = parent;
141
+ }
142
+ const relativePaths = absolutePaths.map((p)=>path__WEBPACK_IMPORTED_MODULE_3___default().relative(commonDir, p).replace(/\\/g, '/'));
143
+ return [
144
+ commonDir,
145
+ relativePaths
146
+ ];
147
+ }
122
148
  })();
123
149
  exports.PosthogWebpackPlugin = __webpack_exports__.PosthogWebpackPlugin;
124
150
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
package/dist/index.mjs CHANGED
@@ -1,11 +1,10 @@
1
1
  import { createLogger } from "@posthog/core";
2
2
  import { resolveConfig } from "./config.mjs";
3
3
  import { spawnLocal } from "@posthog/core/process";
4
+ import path from "path";
4
5
  export * from "./config.mjs";
5
6
  class PosthogWebpackPlugin {
6
7
  apply(compiler) {
7
- var _compiler_options_devtool;
8
- compiler.options.devtool = null != (_compiler_options_devtool = compiler.options.devtool) ? _compiler_options_devtool : this.resolvedConfig.sourcemaps.enabled ? 'source-map' : void 0;
9
8
  const onDone = async (stats, callback)=>{
10
9
  callback = callback || (()=>{});
11
10
  try {
@@ -23,15 +22,17 @@ class PosthogWebpackPlugin {
23
22
  const outputDirectory = compilation.outputOptions.path;
24
23
  const args = [
25
24
  'sourcemap',
26
- 'process',
27
- '--directory',
28
- outputDirectory
25
+ 'process'
29
26
  ];
30
- for (const chunk of compilation.chunks)for (const file of chunk.files)args.push('--include', `**/${file}`);
27
+ const chunkArray = Array.from(compilation.chunks);
28
+ if (0 == chunkArray.length) return;
29
+ const filePaths = chunkArray.flatMap((chunk)=>Array.from(chunk.files).map((file)=>path.resolve(outputDirectory, file)));
30
+ const [commonDirectory, relativeFilePaths] = splitFilePaths(filePaths);
31
+ args.push('--directory', commonDirectory);
32
+ for (const chunkPath of relativeFilePaths)args.push('--include', `**/${chunkPath}`);
31
33
  if (config.sourcemaps.project) args.push('--project', config.sourcemaps.project);
32
34
  if (config.sourcemaps.version) args.push('--version', config.sourcemaps.version);
33
35
  if (config.sourcemaps.deleteAfterUpload) args.push('--delete-after');
34
- this.logger.info(args);
35
36
  await spawnLocal(config.cliBinaryPath, args, {
36
37
  cwd: process.cwd(),
37
38
  env: {
@@ -54,4 +55,25 @@ class PosthogWebpackPlugin {
54
55
  function assertValue(value, message) {
55
56
  if (!value) throw new Error(message);
56
57
  }
58
+ function splitFilePaths(absolutePaths) {
59
+ if (!absolutePaths || 0 === absolutePaths.length) return [
60
+ process.cwd(),
61
+ []
62
+ ];
63
+ let commonDir = path.dirname(absolutePaths[0]);
64
+ const isCommonAncestor = (candidate)=>absolutePaths.every((p)=>{
65
+ const rel = path.relative(candidate, p);
66
+ return '' === rel || !rel.startsWith('..') && !path.isAbsolute(rel);
67
+ });
68
+ while(!isCommonAncestor(commonDir)){
69
+ const parent = path.dirname(commonDir);
70
+ if (parent === commonDir) break;
71
+ commonDir = parent;
72
+ }
73
+ const relativePaths = absolutePaths.map((p)=>path.relative(commonDir, p).replace(/\\/g, '/'));
74
+ return [
75
+ commonDir,
76
+ relativePaths
77
+ ];
78
+ }
57
79
  export { PosthogWebpackPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/webpack-plugin",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Webpack plugin for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,7 +16,7 @@
16
16
  "module": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.ts",
18
18
  "dependencies": {
19
- "@posthog/cli": "~0.5.13",
19
+ "@posthog/cli": "~0.5.16",
20
20
  "@posthog/core": "1.6.0"
21
21
  },
22
22
  "files": [
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { Logger, createLogger } from '@posthog/core'
2
2
  import { PluginConfig, resolveConfig, ResolvedPluginConfig } from './config'
3
3
  import { Compilation, Stats } from 'webpack'
4
4
  import { spawnLocal } from '@posthog/core/process'
5
+ import path from 'path'
5
6
 
6
7
  export * from './config'
7
8
 
@@ -23,9 +24,6 @@ export class PosthogWebpackPlugin {
23
24
  }
24
25
 
25
26
  apply(compiler: any): void {
26
- compiler.options.devtool =
27
- compiler.options.devtool ?? (this.resolvedConfig.sourcemaps.enabled ? 'source-map' : undefined)
28
-
29
27
  const onDone = async (stats: Stats, callback: any): Promise<void> => {
30
28
  callback = callback || (() => {})
31
29
  try {
@@ -46,13 +44,25 @@ export class PosthogWebpackPlugin {
46
44
 
47
45
  async processSourceMaps(compilation: Compilation, config: ResolvedPluginConfig): Promise<void> {
48
46
  const outputDirectory = compilation.outputOptions.path
49
- const args = ['sourcemap', 'process', '--directory', outputDirectory]
50
47
 
51
- for (const chunk of compilation.chunks) {
52
- // chunk.files is a Set in webpack 5
53
- for (const file of chunk.files) {
54
- args.push('--include', `**/${file}`)
55
- }
48
+ // chunks are output outside of the output directory for server chunks
49
+ const args = ['sourcemap', 'process']
50
+ const chunkArray = Array.from(compilation.chunks)
51
+
52
+ if (chunkArray.length == 0) {
53
+ // No chunks generated, skipping sourcemap processing.
54
+ return
55
+ }
56
+
57
+ const filePaths = chunkArray.flatMap((chunk) =>
58
+ Array.from(chunk.files).map((file) => path.resolve(outputDirectory, file))
59
+ )
60
+ const [commonDirectory, relativeFilePaths] = splitFilePaths(filePaths)
61
+
62
+ args.push('--directory', commonDirectory)
63
+
64
+ for (const chunkPath of relativeFilePaths) {
65
+ args.push('--include', `**/${chunkPath}`)
56
66
  }
57
67
 
58
68
  if (config.sourcemaps.project) {
@@ -67,8 +77,6 @@ export class PosthogWebpackPlugin {
67
77
  args.push('--delete-after')
68
78
  }
69
79
 
70
- this.logger.info(args)
71
-
72
80
  await spawnLocal(config.cliBinaryPath, args, {
73
81
  cwd: process.cwd(),
74
82
  env: {
@@ -88,3 +96,36 @@ function assertValue(value: any, message: string): void {
88
96
  throw new Error(message)
89
97
  }
90
98
  }
99
+
100
+ // Convert a list of absolute file path to a common absolute directory ancestor path and relative path
101
+ function splitFilePaths(absolutePaths: string[]): [string, string[]] {
102
+ if (!absolutePaths || absolutePaths.length === 0) {
103
+ return [process.cwd(), []]
104
+ }
105
+
106
+ // Start with the directory of the first path and walk up until we find a common ancestor
107
+ let commonDir = path.dirname(absolutePaths[0])
108
+
109
+ const isCommonAncestor = (candidate: string): boolean => {
110
+ return absolutePaths.every((p) => {
111
+ const rel = path.relative(candidate, p)
112
+ // If rel starts with '..' or is absolute, p is not inside candidate
113
+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))
114
+ })
115
+ }
116
+
117
+ // If the first candidate isn't a common ancestor, walk up the directory tree
118
+ while (!isCommonAncestor(commonDir)) {
119
+ const parent = path.dirname(commonDir)
120
+ if (parent === commonDir) {
121
+ // reached filesystem root
122
+ break
123
+ }
124
+ commonDir = parent
125
+ }
126
+
127
+ // Compute relative paths from the common directory, normalize to forward slashes for globs
128
+ const relativePaths = absolutePaths.map((p) => path.relative(commonDir, p).replace(/\\/g, '/'))
129
+
130
+ return [commonDir, relativePaths]
131
+ }