Optional shard required or not

There isn’t really a direct way to do this. Best work around would be to like check if a type from that shard exists. E.g.

{% if @top_level.has_type?("MyShard") %}
  # `MyShard` is required
{% end %}

The catch is you’d have to pick a type that is for sure only going to exist in the shard, and not re-opened in your lib as it would otherwise trick the logic into thinking that shard was actually installed.

Another option could be using a compile time flag. This would deff be more specific and probably less brittle, but also a bit more verbose. E.g. crystal build -Dwith_my_shard src/app.cr, then check with {% if flag? :with_my_shard %}. An ENV var could also work but like {% if env("WITH_MY_SHARD") == "1" %} or something along those lines.

Related: Check whether a required file / shard exists on Compilation time?