@@ -98,26 +98,31 @@ export function constructWebpackConfigFunction(
98
98
] ,
99
99
} ) ;
100
100
101
- let pagesDirPath : string ;
101
+ let pagesDirPath : string | undefined ;
102
102
const maybePagesDirPath = path . join ( projectDir , 'pages' ) ;
103
+ const maybeSrcPagesDirPath = path . join ( projectDir , 'src' , 'pages' ) ;
103
104
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 ;
107
108
}
108
109
109
- let appDirPath : string ;
110
+ let appDirPath : string | undefined ;
110
111
const maybeAppDirPath = path . join ( projectDir , 'app' ) ;
112
+ const maybeSrcAppDirPath = path . join ( projectDir , 'src' , 'app' ) ;
111
113
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 ;
115
117
}
116
118
117
- const apiRoutesPath = path . join ( pagesDirPath , 'api' ) ;
119
+ const apiRoutesPath = pagesDirPath ? path . join ( pagesDirPath , 'api' ) : undefined ;
118
120
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 ;
121
126
122
127
// Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
123
128
const pageExtensions = userNextConfig . pageExtensions || [ 'tsx' , 'ts' , 'jsx' , 'js' ] ;
@@ -151,6 +156,7 @@ export function constructWebpackConfigFunction(
151
156
const isPageResource = ( resourcePath : string ) : boolean => {
152
157
const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
153
158
return (
159
+ pagesDirPath !== undefined &&
154
160
normalizedAbsoluteResourcePath . startsWith ( pagesDirPath + path . sep ) &&
155
161
! normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
156
162
dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
@@ -167,7 +173,10 @@ export function constructWebpackConfigFunction(
167
173
168
174
const isMiddlewareResource = ( resourcePath : string ) : boolean => {
169
175
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
+ ) ;
171
180
} ;
172
181
173
182
const isServerComponentResource = ( resourcePath : string ) : boolean => {
@@ -176,6 +185,7 @@ export function constructWebpackConfigFunction(
176
185
// ".js, .jsx, or .tsx file extensions can be used for Pages"
177
186
// 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.
178
187
return (
188
+ appDirPath !== undefined &&
179
189
normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
180
190
! ! 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 ) $ / )
181
191
) ;
@@ -184,6 +194,7 @@ export function constructWebpackConfigFunction(
184
194
const isRouteHandlerResource = ( resourcePath : string ) : boolean => {
185
195
const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
186
196
return (
197
+ appDirPath !== undefined &&
187
198
normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
188
199
! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] r o u t e \. ( j s | j s x | t s | t s x ) $ / )
189
200
) ;
0 commit comments