11import * as fs from 'fs' ;
22import * as path from 'path' ;
3- import { type Resolver , addPlugin , createResolver , defineNuxtModule } from '@nuxt/kit' ;
4- import { addImportStatement , buildSdkInitFileImportSnippet } from './common/snippets' ;
3+ import { addPlugin , addPluginTemplate , createResolver , defineNuxtModule } from '@nuxt/kit' ;
54import type { SentryNuxtOptions } from './common/types' ;
65
76export type ModuleOptions = SentryNuxtOptions ;
@@ -16,34 +15,46 @@ export default defineNuxtModule<ModuleOptions>({
1615 } ,
1716 defaults : { } ,
1817 setup ( _moduleOptions , nuxt ) {
19- const resolver : Resolver = createResolver ( import . meta. url ) ;
20-
21- const pathToClientInit = findDefaultSdkInitFile ( 'client' ) ;
22-
23- if ( pathToClientInit ) {
24- nuxt . hook ( 'app:templates' , nuxtApp => {
25- if ( nuxtApp . rootComponent ) {
26- try {
27- addImportStatement ( nuxtApp . rootComponent , buildSdkInitFileImportSnippet ( pathToClientInit ) ) ;
28- } catch ( err ) {
29- // eslint-disable-next-line no-console
30- console . error ( `[Sentry] Could not add import statement to root component. ${ err } ` ) ;
31- }
32- }
18+ const moduleDirResolver = createResolver ( import . meta. url ) ;
19+ const buildDirResolver = createResolver ( nuxt . options . buildDir ) ;
20+
21+ const clientConfigFile = findDefaultSdkInitFile ( 'client' ) ;
22+
23+ if ( clientConfigFile ) {
24+ // Inject the client-side Sentry config file with a side effect import
25+ addPluginTemplate ( {
26+ mode : 'client' ,
27+ filename : 'sentry-client-config.mjs' ,
28+ getContents : ( ) =>
29+ `import "${ buildDirResolver . resolve ( `/${ clientConfigFile } ` ) } "\n` +
30+ 'export default defineNuxtPlugin(() => {})' ,
3331 } ) ;
32+
33+ addPlugin ( { src : moduleDirResolver . resolve ( './runtime/plugins/sentry.client' ) , mode : 'client' } ) ;
3434 }
3535
36- if ( resolver ) {
37- addPlugin ( resolver . resolve ( './runtime/plugins/sentry.client' ) ) ;
36+ const serverConfigFile = findDefaultSdkInitFile ( 'server' ) ;
37+
38+ if ( serverConfigFile ) {
39+ // Inject the server-side Sentry config file with a side effect import
40+ addPluginTemplate ( {
41+ mode : 'server' ,
42+ filename : 'sentry-server-config.mjs' ,
43+ getContents : ( ) =>
44+ `import "${ buildDirResolver . resolve ( `/${ serverConfigFile } ` ) } "\n` +
45+ 'export default defineNuxtPlugin(() => {})' ,
46+ } ) ;
3847 }
3948 } ,
4049} ) ;
4150
42- function findDefaultSdkInitFile ( type : /* 'server' | */ 'client' ) : string | undefined {
51+ function findDefaultSdkInitFile ( type : 'server' | 'client' ) : string | undefined {
4352 const possibleFileExtensions = [ 'ts' , 'js' , 'mjs' , 'cjs' , 'mts' , 'cts' ] ;
4453
4554 const cwd = process . cwd ( ) ;
46- return possibleFileExtensions
55+ const filePath = possibleFileExtensions
4756 . map ( e => path . resolve ( path . join ( cwd , `sentry.${ type } .config.${ e } ` ) ) )
4857 . find ( filename => fs . existsSync ( filename ) ) ;
58+
59+ return filePath ? path . basename ( filePath ) : undefined ;
4960}
0 commit comments