Implement transitive support for NPM dependencies

This commit implements [RFC 8], which enables transitive and transparent
dependencies on NPM. The `module` attribute, when seen and not part of a
local JS snippet, triggers detection of a `package.json` next to
`Cargo.toml`. If found it will cause the `wasm-bindgen` CLI tool to load
and parse the `package.json` within each crate and then create a merged
`package.json` at the end.

[RFC 8]: https://github.com/rustwasm/rfcs/pull/8
This commit is contained in:
Alex Crichton
2019-02-27 12:20:33 -08:00
parent 6522968fb6
commit 6edb40a807
6 changed files with 111 additions and 2 deletions

View File

@ -1,7 +1,7 @@
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]
use failure::{bail, Error, ResultExt};
use std::collections::BTreeSet;
use std::collections::{BTreeSet, BTreeMap};
use std::env;
use std::fs;
use std::mem;
@ -329,6 +329,8 @@ impl Bindgen {
start: None,
anyref: Default::default(),
snippet_offsets: Default::default(),
npm_dependencies: Default::default(),
package_json_read: Default::default(),
};
cx.anyref.enabled = self.anyref;
cx.anyref.prepare(cx.module)?;
@ -366,6 +368,16 @@ impl Bindgen {
.with_context(|_| format!("failed to write `{}`", path.display()))?;
}
if cx.npm_dependencies.len() > 0 {
let map = cx
.npm_dependencies
.iter()
.map(|(k, v)| (k, &v.1))
.collect::<BTreeMap<_, _>>();
let json = serde_json::to_string_pretty(&map)?;
fs::write(out_dir.join("package.json"), json)?;
}
cx.finalize(stem)?
};
@ -701,4 +713,11 @@ impl OutputMode {
_ => false,
}
}
fn bundler(&self) -> bool {
match self {
OutputMode::Bundler => true,
_ => false,
}
}
}