React flow can now be imported normally into remix, see xyflow/xyflow#1953 (comment)
The fix can be still useful for other packages
Run it with npm run dev
-
esm-module.ts
stores the dynamic imports of the ESM modules -
app/moduleLoader.tsx
exports a wrapper element calledModuleLoader
and a hook calleduseModuleLoader
-
app/components/flow.tsx
is React Flow first example, modified withuseModuleLoader
-
app/components/initial-elements.tsx
is also from the React Flow example, note thatinitialEdges
have been modified to receive theReactMarker
enum since it can't fetch it itself
const OverviewFlow = () => {
const {
default: ReactFlow,
addEdge,
MiniMap,
Controls,
Background,
useNodesState,
useEdgesState,
MarkerType,
} = useModuleLoader("react-flow-renderer") as any;
// ...
}
A component using useModuleLoader
should be wrapped in a ModuleLoader
The string used as index will be used as the argument for useModuleLoader
<ModuleLoader imports={{ "react-flow-renderer": getReactFlowRenderer }}>
<Flow />
</ModuleLoader>
From app/routes/index.tsx
module.exports = {
getReactFlowRenderer: async () => await import("react-flow-renderer"),
};
From esm-module.ts
I did not manage to make the types work so typescript and your IDE will have no informations on the import.
However, you can import types using a regular static import statement without crash, if :
- you are not importing the same type with
useModuleLoader
- you are using the imported type only for type checking with typescript
Example in app/components/flow.tsx
import type { ReactFlowInstance, OnConnect, Edge, Node } from "react-flow-renderer";
// ...
const onConnect: OnConnect = (params) =>
setEdges((eds: Edge[]) => addEdge(params, eds));