Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support no-modules #327

Merged
merged 5 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub fn wasm_bindgen_build(
};
let target_arg = match target {
"nodejs" => "--nodejs",
"no-modules" => "--no-modules",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this PR, but as a follow up issue, this seems like a good candidate for an enum with FromStr and Display impls like how we do with BuildMode and Access

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i was thinking about this when i ran into it, i'll file an issue for a refactor

_ => "--browser",
};
let bindgen_path = Path::new(&wasm_bindgen_path);
Expand Down
42 changes: 41 additions & 1 deletion src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use self::npm::{repository::Repository, CommonJSPackage, ESModulesPackage, NpmPackage};
use self::npm::{
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
};
use console::style;
use emoji;
use error::Error;
Expand Down Expand Up @@ -193,6 +195,42 @@ impl CargoManifest {
side_effects: "false".to_string(),
})
}

fn into_nomodules(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
let mut files = vec![wasm_file, js_file.clone()];

let dts_file = if disable_dts == false {
let file = format!("{}.d.ts", filename);
files.push(file.to_string());
Some(file)
} else {
None
};

if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
}

&self.package.check_optional_fields();

NpmPackage::NoModulesPackage(NoModulesPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
browser: js_file,
types: dts_file,
})
}
}

/// Generate a package.json file inside in `./pkg`.
Expand All @@ -212,6 +250,8 @@ pub fn write_package_json(
let crate_data = read_cargo_toml(path)?;
let npm_data = if target == "nodejs" {
crate_data.into_commonjs(scope, disable_dts)
} else if target == "no-modules" {
crate_data.into_nomodules(scope, disable_dts)
} else {
crate_data.into_esmodules(scope, disable_dts)
};
Expand Down
3 changes: 3 additions & 0 deletions src/manifest/npm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
mod commonjs;
mod esmodules;
mod nomodules;
pub mod repository;

pub use self::commonjs::CommonJSPackage;
pub use self::esmodules::ESModulesPackage;
pub use self::nomodules::NoModulesPackage;

#[derive(Serialize)]
#[serde(untagged)]
pub enum NpmPackage {
CommonJSPackage(CommonJSPackage),
ESModulesPackage(ESModulesPackage),
NoModulesPackage(NoModulesPackage),
}
20 changes: 20 additions & 0 deletions src/manifest/npm/nomodules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use manifest::npm::repository::Repository;

#[derive(Serialize)]
pub struct NoModulesPackage {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub browser: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}
35 changes: 35 additions & 0 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,41 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
assert_eq!(actual_files, expected_files);
}

#[test]
fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(
manifest::write_package_json(&fixture.path, &out_dir, &None, false, "no-modules", &step)
.is_ok()
);
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.browser, "js_hello_world.js");
assert_eq!(pkg.types, "js_hello_world.d.ts");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.js",
"js_hello_world.d.ts",
]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}

#[test]
fn it_creates_a_pkg_json_in_out_dir() {
let fixture = fixture::js_hello_world();
Expand Down
2 changes: 2 additions & 0 deletions tests/all/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct NpmPackage {
#[serde(default = "default_none")]
pub module: String,
#[serde(default = "default_none")]
pub browser: String,
#[serde(default = "default_none")]
pub types: String,
#[serde(default = "default_none", rename = "sideEffects")]
pub side_effects: String,
Expand Down