From 7c967fd4dd9c42a2ffb2e596acd67205d22b2a93 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 21 Oct 2022 11:33:51 +0000 Subject: [PATCH 1/2] fix(nextjs): Match loader files exactly --- packages/nextjs/src/config/webpack.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 9cdfe2753e51..38561effff05 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -94,9 +94,9 @@ export function constructWebpackConfigFunction( newConfig.module.rules.push({ // Nextjs allows the `pages` folder to optionally live inside a `src` folder test: new RegExp( - `${escapeStringForRegex(projectDir)}${ + `^${escapeStringForRegex(projectDir)}${ shouldIncludeSrcDirectory ? '/src' : '' - }/pages/.*\\.(${pageExtensionRegex})`, + }/pages/.*\\.(${pageExtensionRegex})$`, ), use: [ { From 43f2823b8a2011d07d2a45b06135852e426dc7e8 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Oct 2022 08:46:48 +0000 Subject: [PATCH 2/2] Account for windows users --- packages/nextjs/src/config/webpack.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 38561effff05..0574b4af03c0 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -86,6 +86,9 @@ export function constructWebpackConfigFunction( // if `pages` is present in the root directory" // - https://nextjs.org/docs/advanced-features/src-directory const shouldIncludeSrcDirectory = !fs.existsSync(path.resolve(projectDir, 'pages')); + const pagesDirectory = shouldIncludeSrcDirectory // We're intentionally not including slashes in the paths because we wanne let node do the path resolution for Windows + ? path.resolve(projectDir, 'src', 'pages') + : path.resolve(projectDir, 'pages'); // Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161 const pageExtensions = userNextConfig.pageExtensions || ['tsx', 'ts', 'jsx', 'js']; @@ -93,11 +96,7 @@ export function constructWebpackConfigFunction( newConfig.module.rules.push({ // Nextjs allows the `pages` folder to optionally live inside a `src` folder - test: new RegExp( - `^${escapeStringForRegex(projectDir)}${ - shouldIncludeSrcDirectory ? '/src' : '' - }/pages/.*\\.(${pageExtensionRegex})$`, - ), + test: new RegExp(`^${escapeStringForRegex(pagesDirectory)}.*\\.(${pageExtensionRegex})$`), use: [ { loader: path.resolve(__dirname, 'loaders/proxyLoader.js'),