@@ -98,26 +98,31 @@ export function constructWebpackConfigFunction(
9898 ] ,
9999 } ) ;
100100
101- let pagesDirPath : string ;
101+ let pagesDirPath : string | undefined ;
102102 const maybePagesDirPath = path . join ( projectDir , 'pages' ) ;
103+ const maybeSrcPagesDirPath = path . join ( projectDir , 'src' , 'pages' ) ;
103104 if ( fs . existsSync ( maybePagesDirPath ) && fs . lstatSync ( maybePagesDirPath ) . isDirectory ( ) ) {
104- pagesDirPath = path . join ( projectDir , 'pages' ) ;
105- } else {
106- pagesDirPath = path . join ( projectDir , 'src' , 'pages' ) ;
105+ pagesDirPath = maybePagesDirPath ;
106+ } else if ( fs . existsSync ( maybeSrcPagesDirPath ) && fs . lstatSync ( maybeSrcPagesDirPath ) . isDirectory ( ) ) {
107+ pagesDirPath = maybeSrcPagesDirPath ;
107108 }
108109
109- let appDirPath : string ;
110+ let appDirPath : string | undefined ;
110111 const maybeAppDirPath = path . join ( projectDir , 'app' ) ;
112+ const maybeSrcAppDirPath = path . join ( projectDir , 'src' , 'app' ) ;
111113 if ( fs . existsSync ( maybeAppDirPath ) && fs . lstatSync ( maybeAppDirPath ) . isDirectory ( ) ) {
112- appDirPath = path . join ( projectDir , 'app' ) ;
113- } else {
114- appDirPath = path . join ( projectDir , 'src' , 'app' ) ;
114+ appDirPath = maybeAppDirPath ;
115+ } else if ( fs . existsSync ( maybeSrcAppDirPath ) && fs . lstatSync ( maybeSrcAppDirPath ) . isDirectory ( ) ) {
116+ appDirPath = maybeSrcAppDirPath ;
115117 }
116118
117- const apiRoutesPath = path . join ( pagesDirPath , 'api' ) ;
119+ const apiRoutesPath = pagesDirPath ? path . join ( pagesDirPath , 'api' ) : undefined ;
118120
119- const middlewareJsPath = path . join ( pagesDirPath , '..' , 'middleware.js' ) ;
120- const middlewareTsPath = path . join ( pagesDirPath , '..' , 'middleware.ts' ) ;
121+ const middlewareLocationFolder = pagesDirPath
122+ ? path . join ( pagesDirPath , '..' )
123+ : appDirPath
124+ ? path . join ( appDirPath , '..' )
125+ : projectDir ;
121126
122127 // Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
123128 const pageExtensions = userNextConfig . pageExtensions || [ 'tsx' , 'ts' , 'jsx' , 'js' ] ;
@@ -151,6 +156,7 @@ export function constructWebpackConfigFunction(
151156 const isPageResource = ( resourcePath : string ) : boolean => {
152157 const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
153158 return (
159+ pagesDirPath !== undefined &&
154160 normalizedAbsoluteResourcePath . startsWith ( pagesDirPath + path . sep ) &&
155161 ! normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
156162 dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
@@ -167,7 +173,10 @@ export function constructWebpackConfigFunction(
167173
168174 const isMiddlewareResource = ( resourcePath : string ) : boolean => {
169175 const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
170- return normalizedAbsoluteResourcePath === middlewareJsPath || normalizedAbsoluteResourcePath === middlewareTsPath ;
176+ return (
177+ normalizedAbsoluteResourcePath . startsWith ( middlewareLocationFolder + path . sep ) &&
178+ ! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] m i d d l e w a r e \. ( j s | j s x | t s | t s x ) $ / )
179+ ) ;
171180 } ;
172181
173182 const isServerComponentResource = ( resourcePath : string ) : boolean => {
@@ -176,6 +185,7 @@ export function constructWebpackConfigFunction(
176185 // ".js, .jsx, or .tsx file extensions can be used for Pages"
177186 // https://beta.nextjs.org/docs/routing/pages-and-layouts#pages:~:text=.js%2C%20.jsx%2C%20or%20.tsx%20file%20extensions%20can%20be%20used%20for%20Pages.
178187 return (
188+ appDirPath !== undefined &&
179189 normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
180190 ! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] ( p a g e | l a y o u t | l o a d i n g | h e a d | n o t - f o u n d ) \. ( j s | j s x | t s x ) $ / )
181191 ) ;
@@ -184,6 +194,7 @@ export function constructWebpackConfigFunction(
184194 const isRouteHandlerResource = ( resourcePath : string ) : boolean => {
185195 const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
186196 return (
197+ appDirPath !== undefined &&
187198 normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
188199 ! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] r o u t e \. ( j s | j s x | t s | t s x ) $ / )
189200 ) ;
0 commit comments