@@ -6,6 +6,7 @@ import type SentryCliPlugin from '@sentry/webpack-plugin';
66import * as chalk from 'chalk' ;
77import * as fs from 'fs' ;
88import * as path from 'path' ;
9+ import { sync as resolveSync } from 'resolve' ;
910
1011import type { VercelCronsConfig } from '../common/types' ;
1112// 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(
126127 pageExtensionRegex,
127128 excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
128129 sentryConfigFilePath : getUserConfigFilePath ( projectDir , runtime ) ,
129- nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
130+ nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation (
131+ projectDir ,
132+ rawNewConfig . resolve ?. modules ,
133+ ) ,
130134 } ;
131135
132136 const normalizeLoaderResourcePath = ( resourcePath : string ) : string => {
@@ -977,39 +981,47 @@ function addValueInjectionLoader(
977981 ) ;
978982}
979983
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+
9801002function getRequestAsyncStorageModuleLocation (
1003+ webpackContextDir : string ,
9811004 webpackResolvableModuleLocations : string [ ] | undefined ,
9821005) : string | undefined {
9831006 if ( webpackResolvableModuleLocations === undefined ) {
9841007 return undefined ;
9851008 }
9861009
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+ ) ;
10041013
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+ }
10131025
1014- return potentialRequestAsyncStorageLocations . find ( potentialLocation => moduleIsWebpackResolvable ( potentialLocation ) ) ;
1026+ return undefined ;
10151027}
0 commit comments