Skip to content

Commit

Permalink
fix(remix): Rework dynamic imports of react-router-dom (#5897)
Browse files Browse the repository at this point in the history
Added import by name as the first priority (which will work in monorepos) and relative imports as the second if the former fails. Logging error at the end if the latter fails too, without breaking the whole application. This implementation is similar to the loadModule implementation below, only the priorities are swapped. (The other way works too).
  • Loading branch information
onurtemizkan authored Oct 7, 2022
1 parent 1d5c610 commit e406130
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/remix/rollup.npm.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'
export default makeNPMConfigVariants(
makeBaseNPMConfig({
entrypoints: ['src/index.server.ts', 'src/index.client.tsx'],
packageSpecificConfig: {
external: ['react-router', 'react-router-dom'],
output: {
// make it so Rollup calms down about the fact that we're combining default and named exports
exports: 'named',
},
},
}),
);
10 changes: 9 additions & 1 deletion packages/remix/src/utils/serverAdapters/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ function wrapExpressRequestHandler(
next: ExpressNextFunction,
): Promise<void> {
if (!pkg) {
pkg = await import(`${cwd()}/node_modules/react-router-dom`);
try {
pkg = await import('react-router-dom');
} catch (e) {
pkg = await import(`${cwd()}/node_modules/react-router-dom`);
} finally {
if (!pkg) {
__DEBUG_BUILD__ && logger.error('Could not find `react-router-dom` package.');
}
}
}

// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down

0 comments on commit e406130

Please sign in to comment.