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 1 commit
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
4 changes: 2 additions & 2 deletions packages/nextjs/src/config/webpack.ts
Expand Up @@ -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})$`,
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 have to admit this feels a bit risky because of file path shenanigans (windows paths?). I am open to other suggestions.

Making it an exact match should be fine in combination with our query-string import approach because we don't want to process these files anyways. Maybe we can even remove the exit condition (if (this.resourceQuery.includes('__sentry_wrapped__')) return userCode;)

Copy link
Member

Choose a reason for hiding this comment

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

We should have two regex - one for windows and one for Unix if this one does not support windows imo

Copy link
Member

Choose a reason for hiding this comment

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

Ugh, right, I always forget about windows path separators. Good call, @AbhiPrasad.

I don't think we need to do it in two regexes, though. We actually already know the pages directory, so I claim we can do this:

image

Copy link
Member

Choose a reason for hiding this comment

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

Ah perfect, let’s do that then!

Copy link
Member Author

Choose a reason for hiding this comment

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

Katie that change gets rid of the solution for the src folder problematic: #5978

I like however that we build the path to the Next.js pages before we pass it to escapeStringForRegex() so I opted for a hybrid approach. 43f2823

Copy link
Member

Choose a reason for hiding this comment

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

Katie that change gets rid of the solution for the src folder problematic: #5978

If their pages folder is in a src folder, that should be included in pagesDir, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

But we shouldn't include both! It's either pages or src/pages.

Copy link
Member

Choose a reason for hiding this comment

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

No, I mean: If a user has their pages in /pages, then that will be reflected in the value of pagesDir. If a user has their pages in src/pages, that will also be reflected in the value of pagesDir. Either way, we don't need to recalculate the value.

If I do

console.log(pagesDir);
console.log(pagesDirectory);

depending on whether or not I'm using a src directory I get either

/Users/Katie/Documents/Sentry/test-apps/next-js-test/src/pages
/Users/Katie/Documents/Sentry/test-apps/next-js-test/src/pages

or

/Users/Katie/Documents/Sentry/test-apps/next-js-test/pages
/Users/Katie/Documents/Sentry/test-apps/next-js-test/pages

Update: Opened #6044 to remove the extra calculation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh sorry got confused and didn't realize that pagesDir was a thing. I'll approve that PR!

),
use: [
{
Expand Down