How to add crates in project for compiling wasm?

#1

Hello I am pretty new to Rust and am trying to compile some code into wasm for a JS project.
Here is a summary of the code:

extern crate syntect;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{Color, ThemeSet, Theme};
use syntect::html::highlighted_html_for_file;

#[no_mangle]

pub fn code_to_html(code: &str, theme_file: &str)->String{
    let ss = SyntaxSet::load_defaults_newlines();
    let theme = &ThemeSet::get_theme(theme_file).unwrap();

    let html = highlighted_html_for_file(code, &ss, theme);
    return html.unwrap();
}

However when I compile it using
rustc --target wasm32-unknown-unknown --crate-type=cdylib src/lib.rs -o syntect_usage.big.wasm

It returns the error:
cant find crate.

However I did keep the syntect dependency in Cargo.toml, so how do I do the above command with a dependency?

PS: note that
cargo build
works but not
cargo build --target wasm32-unknown-unknown

0 Likes

#2

Calling rustc will not pick up on dependencies in cargo.toml because that is cargo’s configuration file, it’s only used by cargo tooling.

That is the correct arch tuple. What error do you get?

0 Likes

#3

This is the error:

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:105:19
    |
105 | pub struct Handle(imp::Handle);
    |                   ^^^ use of undeclared type or module `imp`

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:142:9
    |
142 |         imp::Handle::from_path(p).map(Handle)
    |         ^^^ use of undeclared type or module `imp`

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:179:9
    |
179 |         imp::Handle::from_file(file).map(Handle)
    |         ^^^ use of undeclared type or module `imp`

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:245:9
    |
245 |         imp::Handle::stdin().map(Handle)
    |         ^^^ use of undeclared type or module `imp`

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:261:9
    |
261 |         imp::Handle::stdout().map(Handle)
    |         ^^^ use of undeclared type or module `imp`

error[E0433]: failed to resolve: use of undeclared type or module `imp`
   --> /home/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/same-file-1.0.4/src/lib.rs:277:9
    |
277 |         imp::Handle::stderr().map(Handle)
    |         ^^^ use of undeclared type or module `imp`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `same-file`.
warning: build failed, waiting for other jobs to finish...
error: build failed
0 Likes

#4

This doesn’t look like a problem in your code, but in the same-file crate that is indirectly included. I don’t recognize this specific problem, however, anything that relies on files is going to be problematic with wasm, as there is no file system abstraction in that architecture.
It might be possible to avoid it (say, with a feature flag), not sure, otherwise it’d need to be patched.

0 Likes