Skip to content
This repository was archived by the owner on Sep 30, 2025. It is now read-only.

Refactor getRoutesAndModules function#76

Merged
fedeya merged 1 commit into
fedeya:canaryfrom
Aryan201903:fixes/react-router-build-generation
Sep 29, 2025
Merged

Refactor getRoutesAndModules function#76
fedeya merged 1 commit into
fedeya:canaryfrom
Aryan201903:fixes/react-router-build-generation

Conversation

@Aryan201903
Copy link
Copy Markdown
Contributor

@Aryan201903 Aryan201903 commented Sep 29, 2025

This can fix #75, using routes from the build folder.

Fixed react router build time generation for sitemap.
@Aryan201903
Copy link
Copy Markdown
Contributor Author

Aryan201903 commented Sep 29, 2025

Problem:

When testing sitemap generation in a React Router project, running the build command fails with:

Error: Cannot find module '@remix-run/dev/dist/config.js'

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:

const file = import_path2.default.resolve(config.appDirectory, route.file); const result = await buildModule2(file); const module2 = (0, import_require_from_string.default)(result.outputFiles[0].text); modules[key] = module2;

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:
const buildRoute = import_path.default.join(remixRoot, "build/server/index.js");

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:

  1. Removed dependency on @remix-run/dev/dist/config.js for route resolution.

  2. Updated route module handling to use the precompiled routes object from build/server/index.js.

  3. Skipped unnecessary rebuilding of route modules via buildModule2.

Comment thread src/bin/routes.ts
Comment on lines +11 to +13
const buildRoute = path.join(remixRoot, '/build/server/index.js');

const { routes } = await import(buildRoute);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This will fetch the routes from your project's build folder.

Comment thread src/bin/routes.ts
Comment on lines -28 to -32
const file = path.resolve(config.appDirectory, route.file);

const result = await buildModule(file);

const module = requireFromString(result.outputFiles[0].text);
Copy link
Copy Markdown
Contributor Author

@Aryan201903 Aryan201903 Sep 29, 2025

Choose a reason for hiding this comment

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

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] } }

@fedeya
Copy link
Copy Markdown
Owner

fedeya commented Sep 29, 2025

Hey @Aryan201903 thanks you for this pr, i had a long time without taking care of this project, really appreciated

@fedeya fedeya merged commit bc6f8f4 into fedeya:canary Sep 29, 2025
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Any chance this can be updated to support react router 7?

2 participants