diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 4002db18f295..de6d346f2b7c 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -147,12 +147,9 @@ export function constructWebpackConfigFunction( ); }; - const possibleMiddlewareLocations = ['js', 'jsx', 'ts', 'tsx'].map(middlewareFileEnding => { - return path.join(middlewareLocationFolder, `middleware.${middlewareFileEnding}`); - }); const isMiddlewareResource = (resourcePath: string): boolean => { const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath(resourcePath); - return possibleMiddlewareLocations.includes(normalizedAbsoluteResourcePath); + return normalizedAbsoluteResourcePath.startsWith(middlewareLocationFolder) && !!normalizedAbsoluteResourcePath.match(/[\\/]middleware.*\.(js|jsx|ts|tsx)$/) }; const isServerComponentResource = (resourcePath: string): boolean => { @@ -172,7 +169,7 @@ export function constructWebpackConfigFunction( return ( appDirPath !== undefined && normalizedAbsoluteResourcePath.startsWith(appDirPath + path.sep) && - !!normalizedAbsoluteResourcePath.match(/[\\/]route\.(js|jsx|ts|tsx)$/) + !!normalizedAbsoluteResourcePath.match(/[\\/]route.*\.(js|jsx|ts|tsx)$/) ); }; diff --git a/packages/nextjs/test/config/loaders.test.ts b/packages/nextjs/test/config/loaders.test.ts index c2aaf0c9a707..873ed500c92d 100644 --- a/packages/nextjs/test/config/loaders.test.ts +++ b/packages/nextjs/test/config/loaders.test.ts @@ -96,6 +96,10 @@ describe('webpack loaders', () => { resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.tsx', expectedWrappingTargetKind: 'page', }, + { + resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.custom.tsx', + expectedWrappingTargetKind: 'page', + }, { resourcePath: './src/pages/testPage.tsx', expectedWrappingTargetKind: 'page', @@ -133,6 +137,10 @@ describe('webpack loaders', () => { resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.js', expectedWrappingTargetKind: 'middleware', }, + { + resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.custom.js', + expectedWrappingTargetKind: 'middleware', + }, { resourcePath: './src/middleware.js', expectedWrappingTargetKind: 'middleware', @@ -162,6 +170,18 @@ describe('webpack loaders', () => { resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.js', expectedWrappingTargetKind: 'api-route', }, + { + resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.custom.js', + expectedWrappingTargetKind: 'api-route', + }, + { + resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.ts', + expectedWrappingTargetKind: 'route-handler', + }, + { + resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.custom.ts', + expectedWrappingTargetKind: 'route-handler', + }, { resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.js', expectedWrappingTargetKind: 'server-component',