I've been working on developing a library that enforces the use of specific strategies for each target. The current structure I have in place is as follows:
[Application] -> contains -> [player] -> contains -> [renderer]
In the current setup, Renderer
is an interface that needs to be swapped out for different platforms:
- Mobile -> uses MobileRenderer
- Web -> uses WebRenderer
I have flexibility in choosing bundlers - currently utilizing Webpack for the app and Rollup for the lib - and here's what I've managed to achieve:
My library exports a Player
interface and a createPlayer
function which returns a PlayerInterface
. On the application side, there's a Webpack alias set up to resolve the correct platform library based on the build input.
For example:
import { createPlayer } from "my-lib";
const player = createPlayer()
We then run the application build with
npm run build --platform=web
Webpack will then convert the my-lib
import into my-lib/platforms/web
, which includes the exported createPlayer
function utilizing the right renderer.
My query is, how can we ensure, from the application's perspective, that we are importing the correct renderer per platform during build time while still enabling tree-shaking (and only including necessary sources)? Using the build system for this purpose seems somewhat convoluted, as it lacks transparency into the process.
Is there a more efficient approach to handle this?
Regards,