Repository navigation
Expand file tree
/
Copy pathmodule.ts
More file actions
149 lines (126 loc) · 5.67 KB
/
Copy pathmodule.ts
File metadata and controls
149 lines (126 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { addPlugin, addPluginTemplate, addTemplate, addVitePlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
// Needed to make TS evaluate the augmentation of Nitro types (https://github.com/nuxt/nuxt/pull/34039)
import type {} from '@nuxt/nitro-server';
import * as path from 'path';
import type { SentryNuxtModuleOptions } from './common/types';
import { setupNitroServer } from './vite/nitroServer';
import { setupOrchestrion } from './vite/orchestrion';
import { setupSourceMaps } from './vite/sourceMaps';
import { findDefaultSdkInitFile, resolveServerApi } from './vite/utils';
export type ModuleOptions = SentryNuxtModuleOptions;
type NuxtPageSubset = { file?: string; path: string };
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@sentry/nuxt/module',
configKey: 'sentry',
compatibility: {
nuxt: '>=3.7.0',
},
},
defaults: {},
async setup(moduleOptionsParam, nuxt) {
if (moduleOptionsParam?.enabled === false) {
return;
}
const serverApi = resolveServerApi();
const moduleOptions = {
...moduleOptionsParam,
// oxlint-disable-next-line typescript/no-deprecated -- supported until removal
autoInjectServerSentry: moduleOptionsParam.autoInjectServerSentry,
// oxlint-disable-next-line typescript/no-deprecated -- supported until removal
experimental_entrypointWrappedFunctions: moduleOptionsParam.experimental_entrypointWrappedFunctions || [
'default',
'handler',
'server',
],
};
const moduleDirResolver = createResolver(import.meta.url);
const buildDirResolver = createResolver(nuxt.options.buildDir);
const clientConfigFile = await findDefaultSdkInitFile('client', nuxt, moduleOptions);
if (clientConfigFile) {
// Inject the client-side Sentry config file with a side effect import
addPluginTemplate({
mode: 'client',
filename: 'sentry-client-config.mjs',
order: 0,
// Dynamic import of config file to wrap it within a Nuxt context (here: defineNuxtPlugin)
// Makes it possible to call useRuntimeConfig() in the user-defined sentry config file
getContents: () => `
import { defineNuxtPlugin } from "#imports";
export default defineNuxtPlugin({
name: 'sentry-client-config',
async setup() {
await import("${buildDirResolver.resolve(`/${clientConfigFile}`)}")
}
});`,
});
// Add the plugin which loads client integrations etc. -
// this must run after the sentry-client-config plugin has run, and the client is initialized!
addPlugin({
src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'),
mode: 'client',
order: 1,
});
}
const serverConfigFile = await findDefaultSdkInitFile('server', nuxt, moduleOptions);
const nuxtMajor = parseInt((nuxt as unknown as { _version: string })._version?.split('.')[0] ?? '3', 10);
const isMinNuxtV4 = nuxtMajor >= 4;
// Orchestrion runs on both the Node path (gated on a server config file) and the Cloudflare path
// (which has no server config file — the SDK is set up via `sentryCloudflareNitroPlugin`). The
// Cloudflare detection happens inside, keyed off the resolved Nitro preset.
setupOrchestrion(nuxt, !!serverConfigFile, moduleOptions.buildTimeInstrumentation);
if (serverConfigFile) {
setupNitroServer(nuxt, serverApi, serverConfigFile, moduleOptions);
if (isMinNuxtV4) {
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector.server'), mode: 'server' });
} else {
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' });
}
}
if (clientConfigFile || serverConfigFile) {
setupSourceMaps(moduleOptions, nuxt, addVitePlugin);
}
let pagesData: NuxtPageSubset[] = [];
nuxt.hooks.hook('pages:extend', pages => {
pagesData = pages
.map(page => ({ file: page.file, path: page.path }))
.filter(page => {
// Check for dynamic parameter (e.g., :userId or [userId])
return page.path.includes(':') || page?.file?.includes('[');
});
});
if (isMinNuxtV4) {
const pagesDataVirtualModuleId = '#sentry/nuxt-pages-data.mjs';
// Vite virtual plugin (for the Vite SSR build, where addPlugin mode:'server' plugins are bundled)
addVitePlugin({
name: 'sentry-nuxt-pages-data-virtual',
resolveId: (id: string) => (id === pagesDataVirtualModuleId ? `\0${pagesDataVirtualModuleId}` : null),
load: (id: string) =>
id === `\0${pagesDataVirtualModuleId}` ? `export default ${JSON.stringify(pagesData, null, 2)};` : undefined,
});
} else {
// Nuxt v3: register as a build template (accessible via #build/)
addTemplate({
filename: 'sentry--nuxt-pages-data.mjs',
getContents: () => `export default ${JSON.stringify(pagesData, null, 2)};`,
});
}
// Add the sentry config file to the include array
nuxt.hook('prepare:types', options => {
const tsConfig = options.tsConfig as { include?: string[] };
if (!tsConfig.include) {
tsConfig.include = [];
}
// Add type references for useRuntimeConfig in root files for nuxt v4
// Should be relative to `root/.nuxt`
if (clientConfigFile) {
const relativePath = path.relative(nuxt.options.buildDir, clientConfigFile);
tsConfig.include.push(relativePath);
}
if (serverConfigFile) {
const relativePath = path.relative(nuxt.options.buildDir, serverConfigFile);
tsConfig.include.push(relativePath);
}
});
},
});