Refactor getRoutesAndModules function#76
Conversation
Fixed react router build time generation for sitemap.
Problem:When testing sitemap generation in a React Router project, running the build command fails with:
This happens because remix-sitemap currently imports @remix-run/dev/dist/config.js to read route configuration. That works for Remix projects, but React Router projects don’t ship with @remix-run/dev, so the module cannot be resolved. Additionally, in the current implementation we attempt to rebuild route modules at buildtime using:
This is unnecessary in React Router builds because the compiled route modules are already present in the generated build/server/index.js. Solution:Instead of pulling route definitions from @remix-run/dev, we can load them directly from the project’s build output. For example: From there, we can access the routes object directly. Since these routes already contain a module property, we don’t need to rebuild them with buildModule2 or require-from-string. This simplifies the logic and avoids the missing dependency issue for React Router projects. Summary of Changes:
|
| const buildRoute = path.join(remixRoot, '/build/server/index.js'); | ||
|
|
||
| const { routes } = await import(buildRoute); |
There was a problem hiding this comment.
This will fetch the routes from your project's build folder.
| const file = path.resolve(config.appDirectory, route.file); | ||
|
|
||
| const result = await buildModule(file); | ||
|
|
||
| const module = requireFromString(result.outputFiles[0].text); |
There was a problem hiding this comment.
Since we already have modules inside route fetched from build we don't need this anymore.
Example route ->
' <my-route>': { id: '<my-route>', parentId: '<parent-route>', path: '<route-path>', index: undefined, caseSensitive: undefined, module: [Object: null prototype] [Module] { action: [AsyncFunction: action], default: [Function: Wrapped], loader: [AsyncFunction: loader] } }
|
Hey @Aryan201903 thanks you for this pr, i had a long time without taking care of this project, really appreciated |
This can fix #75, using routes from the build folder.