-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
module.ts
83 lines (69 loc) · 2.74 KB
/
module.ts
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
import * as fs from 'fs';
import * as path from 'path';
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import type { SentryNuxtModuleOptions } from './common/types';
import { setupSourceMaps } from './vite/sourceMaps';
export type ModuleOptions = SentryNuxtModuleOptions;
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@sentry/nuxt/module',
configKey: 'sentry',
compatibility: {
nuxt: '^3.0.0',
},
},
defaults: {},
setup(moduleOptions, nuxt) {
const moduleDirResolver = createResolver(import.meta.url);
const buildDirResolver = createResolver(nuxt.options.buildDir);
const clientConfigFile = findDefaultSdkInitFile('client');
if (clientConfigFile) {
// Inject the client-side Sentry config file with a side effect import
addPluginTemplate({
mode: 'client',
filename: 'sentry-client-config.mjs',
// 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}`)}")
}
});`,
});
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'), mode: 'client' });
}
const serverConfigFile = findDefaultSdkInitFile('server');
if (serverConfigFile) {
// Inject the server-side Sentry config file with a side effect import
addPluginTemplate({
mode: 'server',
filename: 'sentry-server-config.mjs',
getContents: () =>
`import "${buildDirResolver.resolve(`/${serverConfigFile}`)}"\n` +
'import { defineNuxtPlugin } from "#imports"\n' +
'export default defineNuxtPlugin(() => {})',
});
addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
}
if (clientConfigFile || serverConfigFile) {
setupSourceMaps(moduleOptions, nuxt);
}
},
});
function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined {
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'];
const cwd = process.cwd();
const filePath = possibleFileExtensions
.map(e =>
path.resolve(
type === 'server'
? path.join(cwd, 'public', `instrument.${type}.${e}`)
: path.join(cwd, `sentry.${type}.config.${e}`),
),
)
.find(filename => fs.existsSync(filename));
return filePath ? path.basename(filePath) : undefined;
}