1
1
import commonjs from '@rollup/plugin-commonjs' ;
2
2
import { stringMatchesSomePattern } from '@sentry/utils' ;
3
+ import * as chalk from 'chalk' ;
3
4
import * as fs from 'fs' ;
4
5
import * as path from 'path' ;
5
6
import { rollup } from 'rollup' ;
6
7
7
8
import type { LoaderThis } from './types' ;
8
9
10
+ // Just a simple placeholder to make referencing module consistent
11
+ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module' ;
12
+
13
+ // Needs to end in .cjs in order for the `commonjs` plugin to pick it up
14
+ const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs' ;
15
+
16
+ // Non-public API. Can be found here: https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
17
+ const NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH = 'next/dist/client/components/request-async-storagee' ;
18
+
9
19
const apiWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'apiWrapperTemplate.js' ) ;
10
20
const apiWrapperTemplateCode = fs . readFileSync ( apiWrapperTemplatePath , { encoding : 'utf8' } ) ;
11
21
@@ -15,6 +25,10 @@ const pageWrapperTemplateCode = fs.readFileSync(pageWrapperTemplatePath, { encod
15
25
const middlewareWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'middlewareWrapperTemplate.js' ) ;
16
26
const middlewareWrapperTemplateCode = fs . readFileSync ( middlewareWrapperTemplatePath , { encoding : 'utf8' } ) ;
17
27
28
+ const requestAsyncStorageShimPath = path . resolve ( __dirname , '..' , 'templates' , 'requestAsyncStorageShim.js' ) ;
29
+ const requestAsyncStorageModuleExists = moduleExists ( NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH ) ;
30
+ let showedMissingAsyncStorageModuleWarning = false ;
31
+
18
32
const serverComponentWrapperTemplatePath = path . resolve (
19
33
__dirname ,
20
34
'..' ,
@@ -23,12 +37,6 @@ const serverComponentWrapperTemplatePath = path.resolve(
23
37
) ;
24
38
const serverComponentWrapperTemplateCode = fs . readFileSync ( serverComponentWrapperTemplatePath , { encoding : 'utf8' } ) ;
25
39
26
- // Just a simple placeholder to make referencing module consistent
27
- const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module' ;
28
-
29
- // Needs to end in .cjs in order for the `commonjs` plugin to pick it up
30
- const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs' ;
31
-
32
40
type LoaderOptions = {
33
41
pagesDir : string ;
34
42
appDir : string ;
@@ -37,6 +45,15 @@ type LoaderOptions = {
37
45
wrappingTargetKind : 'page' | 'api-route' | 'middleware' | 'server-component' ;
38
46
} ;
39
47
48
+ function moduleExists ( id : string ) : boolean {
49
+ try {
50
+ require . resolve ( id ) ;
51
+ return true ;
52
+ } catch ( e ) {
53
+ return false ;
54
+ }
55
+ }
56
+
40
57
/**
41
58
* Replace the loaded file with a wrapped version the original file. In the wrapped version, the original file is loaded,
42
59
* any data-fetching functions (`getInitialProps`, `getStaticProps`, and `getServerSideProps`) or API routes it contains
@@ -126,6 +143,24 @@ export default function wrappingLoader(
126
143
127
144
templateCode = serverComponentWrapperTemplateCode ;
128
145
146
+ if ( requestAsyncStorageModuleExists ) {
147
+ templateCode = templateCode . replace (
148
+ / _ _ S E N T R Y _ N E X T J S _ R E Q U E S T _ A S Y N C _ S T O R A G E _ S H I M _ _ / g,
149
+ NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH ,
150
+ ) ;
151
+ } else {
152
+ if ( ! showedMissingAsyncStorageModuleWarning ) {
153
+ // eslint-disable-next-line no-console
154
+ console . warn (
155
+ `${ chalk . yellow ( 'warn' ) } - The Sentry SDK could not access the ${ chalk . bold . cyan (
156
+ 'RequestAsyncStorage' ,
157
+ ) } module. Certain features may not work. There is nothing you can do to fix this yourself, but future SDK updates may resolve this.\n`,
158
+ ) ;
159
+ showedMissingAsyncStorageModuleWarning = true ;
160
+ }
161
+ templateCode = templateCode . replace ( / _ _ S E N T R Y _ N E X T J S _ R E Q U E S T _ A S Y N C _ S T O R A G E _ S H I M _ _ / g, requestAsyncStorageShimPath ) ;
162
+ }
163
+
129
164
templateCode = templateCode . replace ( / _ _ R O U T E _ _ / g, parameterizedPagesRoute . replace ( / \\ / g, '\\\\' ) ) ;
130
165
131
166
const componentTypeMatch = path . posix
0 commit comments