|
|
|
@ -8,8 +8,8 @@ |
|
|
|
|
|
|
|
|
|
mod npm; |
|
|
|
|
|
|
|
|
|
use std::fs; |
|
|
|
|
use std::path::Path; |
|
|
|
|
use std::{collections::HashMap, fs}; |
|
|
|
|
|
|
|
|
|
use self::npm::{ |
|
|
|
|
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage, |
|
|
|
@ -575,14 +575,25 @@ impl CrateData { |
|
|
|
|
target: Target, |
|
|
|
|
) -> Result<(), Error> { |
|
|
|
|
let pkg_file_path = out_dir.join("package.json"); |
|
|
|
|
// Check if a `package.json` was already generated by wasm-bindgen, if so
|
|
|
|
|
// we merge the NPM dependencies already specified in it.
|
|
|
|
|
let existing_deps = if pkg_file_path.exists() { |
|
|
|
|
// It's just a map of dependency names to versions
|
|
|
|
|
Some(serde_json::from_str::<HashMap<String, String>>( |
|
|
|
|
&fs::read_to_string(&pkg_file_path)?, |
|
|
|
|
)?) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
|
let npm_data = match target { |
|
|
|
|
Target::Nodejs => self.to_commonjs(scope, disable_dts, out_dir), |
|
|
|
|
Target::NoModules => self.to_nomodules(scope, disable_dts, out_dir), |
|
|
|
|
Target::Bundler => self.to_esmodules(scope, disable_dts, out_dir), |
|
|
|
|
Target::Web => self.to_web(scope, disable_dts, out_dir), |
|
|
|
|
Target::Nodejs => self.to_commonjs(scope, disable_dts, existing_deps, out_dir), |
|
|
|
|
Target::NoModules => self.to_nomodules(scope, disable_dts, existing_deps, out_dir), |
|
|
|
|
Target::Bundler => self.to_esmodules(scope, disable_dts, existing_deps, out_dir), |
|
|
|
|
Target::Web => self.to_web(scope, disable_dts, existing_deps, out_dir), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let npm_json = serde_json::to_string_pretty(&npm_data)?; |
|
|
|
|
|
|
|
|
|
fs::write(&pkg_file_path, npm_json) |
|
|
|
|
.with_context(|_| format!("failed to write: {}", pkg_file_path.display()))?; |
|
|
|
|
Ok(()) |
|
|
|
@ -657,7 +668,13 @@ impl CrateData { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn to_commonjs(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage { |
|
|
|
|
fn to_commonjs( |
|
|
|
|
&self, |
|
|
|
|
scope: &Option<String>, |
|
|
|
|
disable_dts: bool, |
|
|
|
|
dependencies: Option<HashMap<String, String>>, |
|
|
|
|
out_dir: &Path, |
|
|
|
|
) -> NpmPackage { |
|
|
|
|
let data = self.npm_data(scope, false, disable_dts, out_dir); |
|
|
|
|
let pkg = &self.data.packages[self.current_idx]; |
|
|
|
|
|
|
|
|
@ -683,6 +700,7 @@ impl CrateData { |
|
|
|
|
homepage: data.homepage, |
|
|
|
|
types: data.dts_file, |
|
|
|
|
keywords: data.keywords, |
|
|
|
|
dependencies, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -690,6 +708,7 @@ impl CrateData { |
|
|
|
|
&self, |
|
|
|
|
scope: &Option<String>, |
|
|
|
|
disable_dts: bool, |
|
|
|
|
dependencies: Option<HashMap<String, String>>, |
|
|
|
|
out_dir: &Path, |
|
|
|
|
) -> NpmPackage { |
|
|
|
|
let data = self.npm_data(scope, true, disable_dts, out_dir); |
|
|
|
@ -718,10 +737,17 @@ impl CrateData { |
|
|
|
|
types: data.dts_file, |
|
|
|
|
side_effects: false, |
|
|
|
|
keywords: data.keywords, |
|
|
|
|
dependencies, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn to_web(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage { |
|
|
|
|
fn to_web( |
|
|
|
|
&self, |
|
|
|
|
scope: &Option<String>, |
|
|
|
|
disable_dts: bool, |
|
|
|
|
dependencies: Option<HashMap<String, String>>, |
|
|
|
|
out_dir: &Path, |
|
|
|
|
) -> NpmPackage { |
|
|
|
|
let data = self.npm_data(scope, false, disable_dts, out_dir); |
|
|
|
|
let pkg = &self.data.packages[self.current_idx]; |
|
|
|
|
|
|
|
|
@ -748,6 +774,7 @@ impl CrateData { |
|
|
|
|
types: data.dts_file, |
|
|
|
|
side_effects: false, |
|
|
|
|
keywords: data.keywords, |
|
|
|
|
dependencies, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -755,6 +782,7 @@ impl CrateData { |
|
|
|
|
&self, |
|
|
|
|
scope: &Option<String>, |
|
|
|
|
disable_dts: bool, |
|
|
|
|
dependencies: Option<HashMap<String, String>>, |
|
|
|
|
out_dir: &Path, |
|
|
|
|
) -> NpmPackage { |
|
|
|
|
let data = self.npm_data(scope, false, disable_dts, out_dir); |
|
|
|
@ -782,6 +810,7 @@ impl CrateData { |
|
|
|
|
homepage: data.homepage, |
|
|
|
|
types: data.dts_file, |
|
|
|
|
keywords: data.keywords, |
|
|
|
|
dependencies, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|