@posthog/nuxt 0.0.27 → 1.0.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/module.d.mts CHANGED
@@ -12,16 +12,24 @@ interface EnabledSourcemaps {
12
12
  version?: string;
13
13
  project?: string;
14
14
  verbose?: boolean;
15
+ deleteAfterUpload?: boolean;
15
16
  }
16
17
  interface ModuleOptions {
17
18
  host: string;
18
19
  publicKey: string;
19
20
  debug?: boolean;
20
- clientConfig?: Partial<PostHogConfig>;
21
- serverConfig?: PostHogOptions;
21
+ clientConfig?: PostHogClientConfig;
22
+ serverConfig?: PostHogServerConfig;
22
23
  sourcemaps: DisabledSourcemaps | EnabledSourcemaps | undefined;
23
24
  }
25
+ interface PostHogCommon {
26
+ publicKey: string;
27
+ host: string;
28
+ debug?: boolean;
29
+ }
30
+ type PostHogServerConfig = PostHogOptions;
31
+ type PostHogClientConfig = Partial<PostHogConfig>;
24
32
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
25
33
 
26
34
  export { _default as default };
27
- export type { ModuleOptions };
35
+ export type { ModuleOptions, PostHogClientConfig, PostHogCommon, PostHogServerConfig };
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.7.0"
6
6
  },
7
- "version": "0.0.27",
7
+ "version": "1.0.1",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,10 +1,8 @@
1
1
  import { defineNuxtModule, createResolver, addPlugin, addServerPlugin } from '@nuxt/kit';
2
2
  import { spawnLocal } from '@posthog/core/process';
3
3
  import { fileURLToPath } from 'node:url';
4
- import { dirname } from 'node:path';
4
+ import path, { dirname } from 'node:path';
5
5
 
6
- const VUE_OUTPUT_DIRECTORY = ".output/public";
7
- const NITRO_OUTPUT_DIRECTORY = ".output/server";
8
6
  const filename = fileURLToPath(import.meta.url);
9
7
  const resolvedDirname = dirname(filename);
10
8
  const module = defineNuxtModule({
@@ -25,85 +23,105 @@ const module = defineNuxtModule({
25
23
  const resolver = createResolver(import.meta.url);
26
24
  addPlugin(resolver.resolve("./runtime/vue-plugin"));
27
25
  addServerPlugin(resolver.resolve("./runtime/nitro-plugin"));
28
- nuxt.options.runtimeConfig.public.posthogPublicKey = nuxt.options.runtimeConfig.public.posthogPublicKey || options.publicKey;
29
- nuxt.options.runtimeConfig.public.posthogHost = nuxt.options.runtimeConfig.public.posthogHost || options.host;
30
- nuxt.options.runtimeConfig.public.posthogDebug = nuxt.options.runtimeConfig.public.posthogDebug || options.debug;
31
- nuxt.options.runtimeConfig.public.posthogClientConfig = nuxt.options.runtimeConfig.public.posthogClientConfig || options.clientConfig;
32
- nuxt.options.runtimeConfig.public.posthogServerConfig = nuxt.options.runtimeConfig.public.posthogServerConfig || options.serverConfig;
33
- if (!options.sourcemaps?.enabled || nuxt.options.dev) {
34
- return;
35
- }
26
+ Object.assign(nuxt.options.runtimeConfig.public, {
27
+ posthog: {
28
+ publicKey: options.publicKey,
29
+ host: options.host,
30
+ debug: options.debug
31
+ },
32
+ posthogClientConfig: options.clientConfig
33
+ });
34
+ Object.assign(nuxt.options.runtimeConfig, {
35
+ posthogServerConfig: options.serverConfig
36
+ });
36
37
  const sourcemapsConfig = options.sourcemaps;
37
- const cliEnv = {
38
- ...process.env,
39
- POSTHOG_CLI_ENV_ID: sourcemapsConfig.envId,
40
- POSTHOG_CLI_TOKEN: sourcemapsConfig.personalApiKey
41
- };
42
- const sharedInjectOptions = [
43
- "--host",
44
- options.host,
45
- "sourcemap",
46
- "inject",
47
- "--ignore",
48
- "**/node_modules/**"
49
- ];
50
- if (options.sourcemaps.project) {
51
- sharedInjectOptions.push("--project", options.sourcemaps.project);
52
- }
53
- if (options.sourcemaps.version) {
54
- sharedInjectOptions.push("--version", options.sourcemaps.version);
38
+ if (!sourcemapsConfig?.enabled || nuxt.options.dev) {
39
+ return;
55
40
  }
56
- nuxt.hooks.hook("nitro:build:public-assets", async () => {
57
- try {
58
- await spawnLocal("posthog-cli", [...sharedInjectOptions, "--directory", VUE_OUTPUT_DIRECTORY], {
59
- env: cliEnv,
60
- cwd: process.cwd(),
61
- resolveFrom: resolvedDirname,
62
- stdio: "inherit",
63
- onBinaryFound: () => {
64
- }
65
- });
66
- } catch (error) {
67
- console.error("PostHog sourcemap inject failed:", error);
68
- }
41
+ nuxt.hook("nitro:config", (nitroConfig) => {
42
+ nitroConfig.rollupConfig = {
43
+ ...nitroConfig.rollupConfig || {},
44
+ output: {
45
+ ...nitroConfig.rollupConfig?.output || {},
46
+ sourcemapExcludeSources: false
47
+ // Make sure to set it (otherwise server sourcemaps will not be generated)
48
+ }
49
+ };
50
+ });
51
+ nuxt.hook("build:before", () => {
52
+ nuxt.options.sourcemap = {
53
+ client: "hidden",
54
+ server: "hidden"
55
+ };
69
56
  });
70
- nuxt.hooks.hook("close", async () => {
57
+ const outputDir = getOutputDir(nuxt.options.nitro);
58
+ let isBuildProcess = false;
59
+ nuxt.hook("nitro:build:public-assets", async () => {
60
+ isBuildProcess = true;
71
61
  try {
72
- await spawnLocal("posthog-cli", [...sharedInjectOptions, "--directory", NITRO_OUTPUT_DIRECTORY], {
73
- env: cliEnv,
74
- cwd: process.cwd(),
75
- resolveFrom: resolvedDirname,
76
- stdio: "inherit",
77
- onBinaryFound: () => {
78
- }
79
- });
62
+ await runInject(path.join(outputDir, "public"), options.host, sourcemapsConfig);
80
63
  } catch (error) {
81
- console.error("PostHog sourcemap inject failed:", error);
64
+ console.error("Failed to process public sourcemaps:", error);
82
65
  }
83
- const uploadOptions = [
84
- "--host",
85
- options.host,
86
- "sourcemap",
87
- "upload",
88
- "--directory",
89
- ".output",
90
- "--ignore",
91
- "**/node_modules/**"
92
- ];
66
+ });
67
+ nuxt.hook("close", async () => {
68
+ if (!isBuildProcess) return;
93
69
  try {
94
- await spawnLocal("posthog-cli", uploadOptions, {
95
- env: cliEnv,
96
- cwd: process.cwd(),
97
- resolveFrom: resolvedDirname,
98
- stdio: "inherit",
99
- onBinaryFound: () => {
100
- }
101
- });
70
+ await runInject(path.join(outputDir, "server"), options.host, sourcemapsConfig);
71
+ await runUpload(outputDir, options.host, sourcemapsConfig);
102
72
  } catch (error) {
103
- console.error("PostHog sourcemap upload failed:", error);
73
+ console.error("Failed to process server sourcemaps:", error);
104
74
  }
105
75
  });
106
76
  }
107
77
  });
78
+ async function runInject(directory, host, sourcemapsConfig) {
79
+ const processOptions = ["--host", host, "sourcemap", "process", "--ignore", "**/node_modules/**"];
80
+ if (sourcemapsConfig.project) {
81
+ processOptions.push("--project", sourcemapsConfig.project);
82
+ }
83
+ if (sourcemapsConfig.version) {
84
+ processOptions.push("--version", sourcemapsConfig.version);
85
+ }
86
+ await spawnLocal("posthog-cli", [...processOptions, "--directory", directory], {
87
+ env: {
88
+ ...process.env,
89
+ POSTHOG_CLI_ENV_ID: sourcemapsConfig.envId,
90
+ POSTHOG_CLI_TOKEN: sourcemapsConfig.personalApiKey
91
+ },
92
+ cwd: process.cwd(),
93
+ resolveFrom: resolvedDirname,
94
+ stdio: "inherit",
95
+ onBinaryFound: () => {
96
+ }
97
+ });
98
+ }
99
+ async function runUpload(directory, host, sourcemapsConfig) {
100
+ const processOptions = ["--host", host, "sourcemap", "upload", "--ignore", "**/node_modules/**"];
101
+ if (sourcemapsConfig.deleteAfterUpload ?? true) {
102
+ processOptions.push("--delete-after");
103
+ }
104
+ await spawnLocal("posthog-cli", [...processOptions, "--directory", directory], {
105
+ env: {
106
+ ...process.env,
107
+ POSTHOG_CLI_ENV_ID: sourcemapsConfig.envId,
108
+ POSTHOG_CLI_TOKEN: sourcemapsConfig.personalApiKey
109
+ },
110
+ cwd: process.cwd(),
111
+ resolveFrom: resolvedDirname,
112
+ stdio: "inherit",
113
+ onBinaryFound: () => {
114
+ }
115
+ });
116
+ }
117
+ function getOutputDir(nitroConfig) {
118
+ if (nitroConfig.preset && nitroConfig.preset.includes("vercel")) {
119
+ return ".vercel/output";
120
+ }
121
+ if (nitroConfig.output && nitroConfig.output.dir) {
122
+ return nitroConfig.output.dir;
123
+ }
124
+ return ".output";
125
+ }
108
126
 
109
127
  export { module as default };
@@ -4,25 +4,28 @@ import { defineNitroPlugin } from "nitropack/runtime";
4
4
  import { useRuntimeConfig } from "#imports";
5
5
  export default defineNitroPlugin((nitroApp) => {
6
6
  const runtimeConfig = useRuntimeConfig();
7
- const host = runtimeConfig.public.posthogHost;
8
- const apiKey = runtimeConfig.public.posthogPublicKey;
9
- const configOverride = runtimeConfig.public.posthogServerConfig;
10
- const debug = runtimeConfig.public.posthogDebug;
11
- const { enableExceptionAutocapture, ...restOfConfig } = configOverride;
12
- const client = new PostHog(apiKey, {
13
- host,
14
- ...restOfConfig
7
+ const posthogCommon = runtimeConfig.public.posthog;
8
+ const posthogServerConfig = runtimeConfig.posthogServerConfig;
9
+ const debug = posthogCommon.debug;
10
+ const client = new PostHog(posthogCommon.publicKey, {
11
+ host: posthogCommon.host,
12
+ ...posthogServerConfig
15
13
  });
16
14
  if (debug) {
17
15
  client.debug(true);
18
16
  }
19
- if (configOverride.enableExceptionAutocapture) {
20
- nitroApp.hooks.hook("error", async (error, { event }) => {
21
- await client.captureException(error, uuidv7(), {
22
- $process_person_profile: false,
23
- path: event?.path,
24
- method: event?.method
25
- });
17
+ if (posthogServerConfig.enableExceptionAutocapture) {
18
+ nitroApp.hooks.hook("error", (error, { event }) => {
19
+ const props = {
20
+ $process_person_profile: false
21
+ };
22
+ if (event?.path) {
23
+ props.path = event.path;
24
+ }
25
+ if (event?.method) {
26
+ props.method = event.method;
27
+ }
28
+ client.captureException(error, uuidv7(), props);
26
29
  });
27
30
  }
28
31
  nitroApp.hooks.hook("close", async () => {
@@ -1,6 +1,6 @@
1
- declare const _default: import("#app").Plugin<{
1
+ declare const _default: import("nuxt/app").Plugin<{
2
2
  posthog: () => import("posthog-js").PostHog;
3
- }> & import("#app").ObjectPlugin<{
3
+ }> & import("nuxt/app").ObjectPlugin<{
4
4
  posthog: () => import("posthog-js").PostHog;
5
5
  }>;
6
6
  export default _default;
@@ -1,24 +1,33 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig } from "#app";
2
2
  import posthog from "posthog-js";
3
- export default defineNuxtPlugin(() => {
3
+ export default defineNuxtPlugin((nuxtApp) => {
4
4
  const runtimeConfig = useRuntimeConfig();
5
- const publicApiKey = runtimeConfig.public.posthogPublicKey;
6
- const host = runtimeConfig.public.posthogHost;
7
- const configOverride = runtimeConfig.public.posthogClientConfig;
8
- const debug = runtimeConfig.public.posthogDebug;
5
+ const posthogCommon = runtimeConfig.public.posthog;
6
+ const posthogClientConfig = runtimeConfig.public.posthogClientConfig;
9
7
  if (!window || posthog.__loaded) {
10
8
  return;
11
9
  }
12
- posthog.init(publicApiKey, {
13
- api_host: host,
14
- ...configOverride
10
+ posthog.init(posthogCommon.publicKey, {
11
+ api_host: posthogCommon.host,
12
+ ...posthogClientConfig
15
13
  });
16
- if (debug) {
14
+ if (posthogCommon.debug) {
17
15
  posthog.debug(true);
18
16
  }
17
+ if (autocaptureEnabled(posthogClientConfig)) {
18
+ nuxtApp.hook("vue:error", (error, info) => {
19
+ posthog.captureException(error, { info });
20
+ });
21
+ }
19
22
  return {
20
23
  provide: {
21
24
  posthog: () => posthog
22
25
  }
23
26
  };
24
27
  });
28
+ function autocaptureEnabled(config) {
29
+ if (!config) return false;
30
+ if (typeof config.capture_exceptions === "boolean") return config.capture_exceptions;
31
+ if (typeof config.capture_exceptions === "object") return config.capture_exceptions.capture_unhandled_errors === true;
32
+ return false;
33
+ }
package/dist/types.d.mts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default } from './module.mjs'
2
2
 
3
- export { type ModuleOptions } from './module.mjs'
3
+ export { type ModuleOptions, type PostHogClientConfig, type PostHogCommon, type PostHogServerConfig } from './module.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/nuxt",
3
- "version": "0.0.27",
3
+ "version": "1.0.1",
4
4
  "description": "Nuxt module for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,11 +20,11 @@
20
20
  "dist"
21
21
  ],
22
22
  "dependencies": {
23
- "@posthog/cli": "^0.5.2",
23
+ "@posthog/cli": "^0.5.7",
24
24
  "@nuxt/kit": ">=3.7.0",
25
- "posthog-node": "5.11.0",
26
- "@posthog/core": "1.5.0",
27
- "posthog-js": "1.285.1"
25
+ "posthog-node": "5.11.1",
26
+ "@posthog/core": "1.5.1",
27
+ "posthog-js": "1.286.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^20.0.0",