0

I am working on a crate which may target wasm, with some fewer features.

The Cargo.toml excludes some dependencies when targeting wasm:

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
foo = { path = "../crates/some/path" }

In my code, I want to only execute code involving foo if the target is not wasm. I have tried to achieve this like so:

#[cfg(not(target_arch = "wasm32"))]
use foo;

fn bar() {
    if cfg(not(target_arch = "wasm32")) {
        foo::Foo::bar()
    }
}

However, when I try to compile this code using wasm-pack, I get the following error:

use of undeclared type Foo

So I assume this is because of the reference to Foo within the bar function body.

How can I exclude that code completely in the case that the cfg condition is not met? I.e. the same way the C pre-processor would exclude any code within a #ifndef block?

markalex
  • 8,623
  • 2
  • 7
  • 32
sak
  • 2,612
  • 24
  • 55

0 Answers0