I am converting my react component library react-esri-leaflet over to typescript. The library requires the user to install react-leaflet as a peerDependency, and it provides support for react-leaflet version 3 (RLV3), as well as react-leaflet version 2 (RLV2). For the default components, I can simply do
import { createLayerComponent } from 'react-leaflet'
But if the user wants to use the components that are compatible for RLV2, they can import those components separately (feel free to look at the library docs to see how). Most of the RLV2 components start with something like
import { withLeaflet, MapControl } from 'react-leaflet'
While converting over to typescript, this throws an error. While developing this library, my install of react-leaflet installs the newest version. withLeaflet
and MapControl
no longer exist in that version.
To solve that problem, I followed the advice in this answer to "Two versions of same npm package in Node application". This allows me to treat RLV2 as a separate package in my source code by aliasing it to 'react-leaflet-v2'
, and I can do
import { withLeaflet, MapControl } from 'react-leaflet-v2'
Great, now typescript is happy, and I can import the appropriate version of react-leaflet to the appropriate component and the typings are correct. However, when I compile back to js using tsc
, the package name being used in my V2 components is still 'react-leaflet-v2'
.
The problem is that the end-user of the library is going to be using either RLV2 or RLV3, and its going to simply be called 'react-leaflet'
in their project. The compiled files of the library need to be able to find 'react-leaflet'
, not some alias, regardless of the version they're using.
How can I maintain type safety in development for both dependency versions, but allow my users to not worry about it, meaning just install the version of react-leaflet that they want to use, import the correct components from the library, and have the components find the 'react-leaflet'
dependency in their project? Is it possible to tell tsc
to rename 'react-leaflet-v2'
to 'react-leaflet'
when compiling? Is there another solution?