70 %

Enabling features in Rust crates installed from GitHub like SWC

Some libraries don't end up on crates.io. SWC is one such crate. Thus to install it we need to get the crate from GitHub. We can use the same git repo for all of our dependencies defined in this Cargo workspace on GitHub, as seen below (all swc_* crates are in the workspace).

toml
[package]
name = "toast"
version = "0.1.0"
authors = ["Christopher Biscardi <chris@christopherbiscardi.com>"]
edition = "2018"
[dependencies]
structopt = { version = "0.3.15" }
swc = { git = "https://github.com/swc-project/swc" }
swc_atoms= { git = "https://github.com/swc-project/swc" }
swc_ecma_ast= { git = "https://github.com/swc-project/swc" }
swc_ecma_codegen= { git = "https://github.com/swc-project/swc" }
swc_ecma_parser= { git = "https://github.com/swc-project/swc" }
swc_ecma_preset_env= { git = "https://github.com/swc-project/swc" }
swc_ecma_transforms= { git = "https://github.com/swc-project/swc" }
swc_ecma_visit= { git = "https://github.com/swc-project/swc" }
swc_visit= { git = "https://github.com/swc-project/swc" }
[dependencies.swc_common]
git = "https://github.com/swc-project/swc"
features = ["tty-emitter"]

In swc_common there is a specific function I want to use called with_tty_emitter. I care about this function because the example code shows it:

rust
use std::{path::Path, sync::Arc};
use swc::{self, config::Options};
use swc_common::{
errors::{ColorConfig, Handler},
SourceMap,
};
fn main() {
let cm = Arc::<SourceMap>::default();
let handler = Arc::new(Handler::with_tty_emitter(
ColorConfig::Auto,
true,
false,
Some(cm.clone()),
));
let c = swc::Compiler::new(cm.clone(), handler.clone());
let fm = cm
.load_file(Path::new("foo.js"))
.expect("failed to load file");
c.process_js_file(
fm,
&Options {
..Default::default()
},
)
.expect("failed to process file");
}

Because the function is guarded by the conditional compilation macro #[cfg(feature = "tty-emitter")], we need to specify the feature in Config.toml to use it. We can do this by pulling swc_common out into its own declaration and adding the features key.

toml
[dependencies.swc_common]
git = "https://github.com/swc-project/swc"
features = ["tty-emitter"]