From e4061309c71207df84e090c250ad9ccf25bcec5a Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 7 Oct 2022 11:41:53 +0100 Subject: [PATCH] fix(remix): Rework dynamic imports of `react-router-dom` (#5897) 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). --- packages/remix/rollup.npm.config.js | 7 +++++++ packages/remix/src/utils/serverAdapters/express.ts | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/remix/rollup.npm.config.js b/packages/remix/rollup.npm.config.js index 4689937a652c..dc51f24c7bb2 100644 --- a/packages/remix/rollup.npm.config.js +++ b/packages/remix/rollup.npm.config.js @@ -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', + }, + }, }), ); diff --git a/packages/remix/src/utils/serverAdapters/express.ts b/packages/remix/src/utils/serverAdapters/express.ts index 76ef655236ff..1a4944c43aac 100644 --- a/packages/remix/src/utils/serverAdapters/express.ts +++ b/packages/remix/src/utils/serverAdapters/express.ts @@ -43,7 +43,15 @@ function wrapExpressRequestHandler( next: ExpressNextFunction, ): Promise { 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