Features or multiple packages?

I am looking to write a package. I am debating whether I should be making a single package with feature flags, or if I should be creating multiple packages.

To give a couple specifics, I am looking to write a package that works with websockets. It should be able to be used as a native library (probably via the websocket crate), or as a web client package via stdweb. Should I be creating a single package, or create foo and foo-web packages?

1 Like

If you think you can make the same API / interface for both versions, then I'd go with a feature flag. If they'll need to be considerably different interfaces, then two packages would be better.

If the interface can be exactly the same in both, have you considered using a specific package with platform-specific conditional compilation? It would let the user just depend on "foo" with no features, and then it could use the right backend for whatever was being targeted.

In Cargo.toml, you'd do something like:

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
websocket = ...
[target.'cfg(target_arch = "wasm32")'.dependencies]
websocket = ...

Then in the code instead of #[cfg(feature = ...)], you could use #[cfg(target_arch = "wasm32")]. If you can make the interface exactly the same between the backends, I think this would offer the best user experience.

3 Likes

I completely forgot about conditional target_arch. Thank you for the idea and I will see where I end up with this.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.