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
feat(remix): Add dynamic loading of @remix-run/router
module.
#10458
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { loadModule, logger } from '@sentry/utils'; | ||
import { cwd } from 'process'; | ||
import { DEBUG_BUILD } from './debug-build'; | ||
import type { ReactRouterDomPkg } from './vendor/types'; | ||
|
||
function hasMatchRoutes(pkg: ReactRouterDomPkg | undefined): boolean { | ||
return !!pkg && typeof pkg.matchRoutes === 'function'; | ||
} | ||
|
||
/** | ||
* Loads the router package that provides matchRoutes. | ||
* Tries loading @remix-run/router first, then falls back to react-router-dom. | ||
*/ | ||
export async function loadRemixRouterModule(): Promise<ReactRouterDomPkg | undefined> { | ||
// Try loading @remix-run/router first, then fall back to react-router-dom | ||
for (const moduleName of ['@remix-run/router', 'react-router-dom']) { | ||
const pkg = await tryLoadRouterModule(moduleName); | ||
|
||
if (pkg) { | ||
return pkg; | ||
} | ||
} | ||
|
||
DEBUG_BUILD && logger.warn('Could not find a router package that provides `matchRoutes`.'); | ||
|
||
return; | ||
} | ||
|
||
async function tryLoadRouterModule(moduleName: string): Promise<ReactRouterDomPkg | undefined> { | ||
let pkg: ReactRouterDomPkg | undefined; | ||
|
||
pkg = loadModule<ReactRouterDomPkg>(moduleName); | ||
|
||
if (hasMatchRoutes(pkg)) { | ||
return pkg; | ||
} | ||
|
||
try { | ||
pkg = await import(moduleName); | ||
} catch (e) { | ||
pkg = await import(`${cwd()}/node_modules/${moduleName}`); | ||
} | ||
|
||
if (hasMatchRoutes(pkg)) { | ||
return pkg; | ||
} else { | ||
DEBUG_BUILD && logger.warn(`Could not find ${moduleName} package.`); | ||
} | ||
|
||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't work in CJS right? Can you confirm that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done a little research, but could not produce a case where this breaks. It looks like it works fine on both CJS and ESM. Not in worker environments though.
But, while checking the previous discussions (#5897), the main reason we need this was our TS version and it's up to date now, so we can just try making
@remix-run/router
a dependency and stop doing this anyways? WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do that instead!! Less dynamic imports the better.