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: update flat-route module check for file colocation #5034

Merged
merged 7 commits into from
Jan 10, 2023

Conversation

mcansh
Copy link
Collaborator

@mcansh mcansh commented Jan 9, 2023

closes #5028

before:

<Routes>
  <Route file="root.tsx">
    <Route index file="routes/_index.tsx" />
    <Route path="help" file="routes/help.tsx">
      <Route index file="routes/help._index.tsx" />
      <Route path="bam" file="routes/help.bam.tsx" />
    </Route>
    <Route path="app" index file="routes/app/_index.tsx" />
    <Route path="app/footer" file="routes/app/footer.tsx" />
    <Route path="app/primary-nav" file="routes/app/primary-nav.tsx" />
    <Route path="something" index file="routes/something/_index.tsx" />
  </Route>
</Routes>

after:

<Routes>
  <Route file="root.tsx">
    <Route index file="routes/_index.tsx" />
    <Route path="help" file="routes/help.tsx">
      <Route index file="routes/help._index.tsx" />
      <Route path="bam" file="routes/help.bam.tsx" />
    </Route>
    <Route path="app" index file="routes/app/_index.tsx" />
    <Route path="something" index file="routes/something/_index.tsx" />
  </Route>
</Routes>
  • Docs
  • Tests

Testing Strategy:

@changeset-bot
Copy link

changeset-bot bot commented Jan 9, 2023

🦋 Changeset detected

Latest commit: 558a448

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
remix Patch
@remix-run/dev Patch
create-remix Patch
@remix-run/css-bundle Patch
@remix-run/architect Patch
@remix-run/cloudflare Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/deno Patch
@remix-run/eslint-config Patch
@remix-run/express Patch
@remix-run/netlify Patch
@remix-run/node Patch
@remix-run/react Patch
@remix-run/serve Patch
@remix-run/server-runtime Patch
@remix-run/testing Patch
@remix-run/vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mcansh mcansh force-pushed the logan/flat-routes-colocation branch from 81c34cd to a3bc9e6 Compare January 9, 2023 18:06
@mcansh mcansh marked this pull request as ready for review January 9, 2023 18:29
@mcansh mcansh changed the title fix: update route module check for file colocation fix: update flat-route module check for file colocation Jan 9, 2023
mcansh added 5 commits January 9, 2023 16:46
Signed-off-by: Logan McAnsh <[email protected]>

chore: update regex

Signed-off-by: Logan McAnsh <[email protected]>

test: add "path" to all tests

Signed-off-by: Logan McAnsh <[email protected]>

chore: update regex

Signed-off-by: Logan McAnsh <[email protected]>
Signed-off-by: Logan McAnsh <[email protected]>
Signed-off-by: Logan McAnsh <[email protected]>

chore: update regex

Signed-off-by: Logan McAnsh <[email protected]>
Signed-off-by: Logan McAnsh <[email protected]>

test: add explicit id to tests

Signed-off-by: Logan McAnsh <[email protected]>
@mcansh mcansh force-pushed the logan/flat-routes-colocation branch from 0ab2f88 to f3eda39 Compare January 9, 2023 21:47
Comment on lines 324 to 325
let routeRegex =
/(([+][/\\][^/\\:?*]+)|[/\\]((_index)|([^/\\:?*])))\.(tsx?|jsx?|mdx?)$$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

This regex is too complex for me to understand at a glance. Could we break this down into simpler pieces somehow?

For example, this regex matches /a.js, but not /abc.js nor a.js. I'm not really sure if that's intentional or not.
It also seems to only match after the final / so /abc/d.js is only matched on /d.js. If we only want to match after the final path separator, I think it'd be better to use path utilities to get the base filename first and then match against regexes.

In any case, I think this deserves at least a comment explaining it, but probably warrants doing it a different way to keep things readable/understandable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, the regex is complex. Even more so in my package as it handles more conventions. Unfortunately, JavaScript doesn't support the x flag (ignore whitespace) to make the regex easier to read.

Since routes can be determined as either a filename a.b.c.tsx or a folder a.b.c./_index.tsx, I don't think you can just get the base filename.

The filepath in isRouteModuleFile will be the full path after routes folder. So will never start with /.

To match flat-files convention, this is the regex: ^[^\/\\:?*]+\.(tsx?|jsx?|mdx?)$

  • Start of path ^
  • Followed by one or more non-path characters [^\/\\:?*]+
  • Ending with .tsx, etc \.(tsx?|jsx?|mdx?)$

image

To match flat-folders convention, this is the regex: [\/\\]_index\.(tsx?|jsx?|mdx?)$

  • Path ends with /_index.tsx
  • So it should ignore colocated files like users/component.tsx

image

Put them together and you get: ((^[^\/\\:?*]+)|([\/\\]_index))\.(tsx?|jsx?|mdx?)$

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

instead of using a regex would using isIndexRoute be sufficient for non flat files?

Copy link
Collaborator

@kiliman kiliman Jan 10, 2023

Choose a reason for hiding this comment

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

Probably, considering the new core feature is a subset of the remix-flat-routes package. You don't need to handle quite as many cases.

But if you end up expanding the naming conventions, you'll most likely need to use regex at some point.

Signed-off-by: Logan McAnsh <[email protected]>
@mcansh mcansh merged commit 3a28769 into dev Jan 10, 2023
@mcansh mcansh deleted the logan/flat-routes-colocation branch January 10, 2023 18:30
@github-actions github-actions bot added the awaiting release This issue has been fixed and will be released soon label Jan 10, 2023
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version v0.0.0-nightly-f5475db-20230111 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.10.1-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions github-actions bot removed the awaiting release This issue has been fixed and will be released soon label Jan 11, 2023
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.10.1 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants