@@ -6,6 +6,7 @@ import type SentryCliPlugin from '@sentry/webpack-plugin';
6
6
import * as chalk from 'chalk' ;
7
7
import * as fs from 'fs' ;
8
8
import * as path from 'path' ;
9
+ import { sync as resolveSync } from 'resolve' ;
9
10
10
11
import type { VercelCronsConfig } from '../common/types' ;
11
12
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
@@ -126,7 +127,10 @@ export function constructWebpackConfigFunction(
126
127
pageExtensionRegex,
127
128
excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
128
129
sentryConfigFilePath : getUserConfigFilePath ( projectDir , runtime ) ,
129
- nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
130
+ nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation (
131
+ projectDir ,
132
+ rawNewConfig . resolve ?. modules ,
133
+ ) ,
130
134
} ;
131
135
132
136
const normalizeLoaderResourcePath = ( resourcePath : string ) : string => {
@@ -977,39 +981,47 @@ function addValueInjectionLoader(
977
981
) ;
978
982
}
979
983
984
+ function resolveNextPackageDirFromDirectory ( basedir : string ) : string | undefined {
985
+ try {
986
+ return path . dirname ( resolveSync ( 'next/package.json' , { basedir } ) ) ;
987
+ } catch {
988
+ // Should not happen in theory
989
+ return undefined ;
990
+ }
991
+ }
992
+
993
+ const POTENTIAL_REQUEST_ASNYC_STORAGE_LOCATIONS = [
994
+ // Original location of RequestAsyncStorage
995
+ // https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
996
+ 'next/dist/client/components/request-async-storage.js' ,
997
+ // Introduced in Next.js 13.4.20
998
+ // https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
999
+ 'next/dist/client/components/request-async-storage.external.js' ,
1000
+ ] ;
1001
+
980
1002
function getRequestAsyncStorageModuleLocation (
1003
+ webpackContextDir : string ,
981
1004
webpackResolvableModuleLocations : string [ ] | undefined ,
982
1005
) : string | undefined {
983
1006
if ( webpackResolvableModuleLocations === undefined ) {
984
1007
return undefined ;
985
1008
}
986
1009
987
- const absoluteWebpackResolvableModuleLocations = webpackResolvableModuleLocations . map ( m => path . resolve ( m ) ) ;
988
- const moduleIsWebpackResolvable = ( moduleId : string ) : boolean => {
989
- let requireResolveLocation : string ;
990
- try {
991
- // This will throw if the location is not resolvable at all.
992
- // We provide a `paths` filter in order to maximally limit the potential locations to the locations webpack would check.
993
- requireResolveLocation = require . resolve ( moduleId , { paths : webpackResolvableModuleLocations } ) ;
994
- } catch {
995
- return false ;
996
- }
997
-
998
- // Since the require.resolve approach still looks in "global" node_modules locations like for example "/user/lib/node"
999
- // we further need to filter by locations that start with the locations that webpack would check for.
1000
- return absoluteWebpackResolvableModuleLocations . some ( resolvableModuleLocation =>
1001
- requireResolveLocation . startsWith ( resolvableModuleLocation ) ,
1002
- ) ;
1003
- } ;
1010
+ const absoluteWebpackResolvableModuleLocations = webpackResolvableModuleLocations . map ( loc =>
1011
+ path . resolve ( webpackContextDir , loc ) ,
1012
+ ) ;
1004
1013
1005
- const potentialRequestAsyncStorageLocations = [
1006
- // Original location of RequestAsyncStorage
1007
- // https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
1008
- 'next/dist/client/components/request-async-storage' ,
1009
- // Introduced in Next.js 13.4.20
1010
- // https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
1011
- 'next/dist/client/components/request-async-storage.external' ,
1012
- ] ;
1014
+ for ( const webpackResolvableLocation of absoluteWebpackResolvableModuleLocations ) {
1015
+ const nextPackageDir = resolveNextPackageDirFromDirectory ( webpackResolvableLocation ) ;
1016
+ if ( nextPackageDir ) {
1017
+ const asyncLocalStorageLocation = POTENTIAL_REQUEST_ASNYC_STORAGE_LOCATIONS . find ( loc =>
1018
+ fs . existsSync ( path . join ( nextPackageDir , '..' , loc ) ) ,
1019
+ ) ;
1020
+ if ( asyncLocalStorageLocation ) {
1021
+ return asyncLocalStorageLocation ;
1022
+ }
1023
+ }
1024
+ }
1013
1025
1014
- return potentialRequestAsyncStorageLocations . find ( potentialLocation => moduleIsWebpackResolvable ( potentialLocation ) ) ;
1026
+ return undefined ;
1015
1027
}
0 commit comments