Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Match loader files exactly #6013

Merged
merged 2 commits into from Oct 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/nextjs/src/config/webpack.ts
Expand Up @@ -86,18 +86,17 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less than l: I would remove the comment, don’t think that context is needed.

Suggested change
const pagesDirectory = shouldIncludeSrcDirectory // We're intentionally not including slashes in the paths because we wanne let node do the path resolution for Windows
const pagesDirectory = shouldIncludeSrcDirectory

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna leave it just so there's some awareness around windows in that file. That was the original reason I even left a comment.

? 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'];
const pageExtensionRegex = pageExtensions.map(escapeStringForRegex).join('|');

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'),
Expand Down