Skip to content

Commit 94405b5

Browse files
committed
Add support for --target web
This commit adds support for the new `--web` flag in `wasm-bindgen` under the flag name `--target web`. To ensure that it was plubmed around the stringly-typed `target` type was switched to an `enum Target` to ensure that all cases it's looked at are handled appropriately for the new `web` target.
1 parent 0c45215 commit 94405b5

File tree

5 files changed

+105
-35
lines changed

5 files changed

+105
-35
lines changed

src/bindgen.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use binary_install::{Cache, Download};
44
use child;
5-
use command::build::BuildProfile;
5+
use command::build::{BuildProfile, Target};
66
use emoji;
77
use failure::{self, ResultExt};
88
use log::debug;
@@ -176,7 +176,7 @@ pub fn wasm_bindgen_build(
176176
bindgen: &Download,
177177
out_dir: &Path,
178178
disable_dts: bool,
179-
target: &str,
179+
target: &Target,
180180
profile: BuildProfile,
181181
step: &Step,
182182
) -> Result<(), failure::Error> {
@@ -203,9 +203,10 @@ pub fn wasm_bindgen_build(
203203
"--typescript"
204204
};
205205
let target_arg = match target {
206-
"nodejs" => "--nodejs",
207-
"no-modules" => "--no-modules",
208-
_ => "--browser",
206+
Target::Nodejs => "--nodejs",
207+
Target::NoModules => "--no-modules",
208+
Target::Web => "--web",
209+
Target::Bundler => "--browser",
209210
};
210211
let bindgen_path = bindgen.binary("wasm-bindgen")?;
211212
let mut cmd = Command::new(bindgen_path);

src/command/build.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Build {
2626
pub crate_data: manifest::CrateData,
2727
pub scope: Option<String>,
2828
pub disable_dts: bool,
29-
pub target: String,
29+
pub target: Target,
3030
pub profile: BuildProfile,
3131
pub mode: BuildMode,
3232
pub out_dir: PathBuf,
@@ -66,6 +66,44 @@ impl FromStr for BuildMode {
6666
}
6767
}
6868

69+
/// What sort of output we're going to be generating and flags we're invoking
70+
/// `wasm-bindgen` with.
71+
#[derive(Clone, Copy, Debug)]
72+
pub enum Target {
73+
/// Default output mode or `--target bundler`, indicates output will be
74+
/// used with a bundle in a later step.
75+
Bundler,
76+
/// Correspond to `--target web` where the output is natively usable as an
77+
/// ES module in a browser and the wasm is manually instantiated.
78+
Web,
79+
/// Correspond to `--target nodejs` where the output is natively usable as
80+
/// a Node.js module loaded with `require`.
81+
Nodejs,
82+
/// Correspond to `--target no-modules` where the output is natively usable
83+
/// in a browser but pollutes the global namespace and must be manually
84+
/// instantiated.
85+
NoModules,
86+
}
87+
88+
impl Default for Target {
89+
fn default() -> Target {
90+
Target::Bundler
91+
}
92+
}
93+
94+
impl FromStr for Target {
95+
type Err = Error;
96+
fn from_str(s: &str) -> Result<Self, Error> {
97+
match s {
98+
"bundler" | "browser" => Ok(Target::Bundler),
99+
"web" => Ok(Target::Web),
100+
"nodejs" => Ok(Target::Nodejs),
101+
"no-modules" => Ok(Target::NoModules),
102+
_ => bail!("Unknown target: {}", s),
103+
}
104+
}
105+
}
106+
69107
/// The build profile controls whether optimizations, debug info, and assertions
70108
/// are enabled or disabled.
71109
#[derive(Clone, Copy, Debug)]
@@ -99,8 +137,8 @@ pub struct BuildOptions {
99137
pub disable_dts: bool,
100138

101139
#[structopt(long = "target", short = "t", default_value = "browser")]
102-
/// Sets the target environment. [possible values: browser, nodejs, no-modules]
103-
pub target: String,
140+
/// Sets the target environment. [possible values: browser, nodejs, web, no-modules]
141+
pub target: Target,
104142

105143
#[structopt(long = "debug")]
106144
/// Deprecated. Renamed to `--dev`.
@@ -133,9 +171,9 @@ impl Default for BuildOptions {
133171
Self {
134172
path: None,
135173
scope: None,
136-
mode: BuildMode::Normal,
174+
mode: BuildMode::default(),
137175
disable_dts: false,
138-
target: String::new(),
176+
target: Target::default(),
139177
debug: false,
140178
dev: false,
141179
release: false,
@@ -165,12 +203,6 @@ impl Build {
165203
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
166204
};
167205

168-
// `possible_values` in clap isn't supported by `structopt`
169-
let possible_targets = ["browser", "nodejs", "no-modules"];
170-
if !possible_targets.contains(&build_opts.target.as_str()) {
171-
bail!("Supported targets: browser, nodejs, no-modules");
172-
}
173-
174206
Ok(Build {
175207
crate_path,
176208
crate_data,

src/command/publish/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
pub mod access;
33

44
use self::access::Access;
5-
use command::build::{Build, BuildOptions};
5+
use command::build::{Build, BuildOptions, Target};
66
use command::utils::{find_pkg_directory, set_crate_path};
77
use dialoguer::{Confirmation, Input, Select};
88
use failure::Error;
99
use log::info;
1010
use npm;
1111
use std::path::PathBuf;
1212
use std::result;
13+
use std::str::FromStr;
1314
use PBAR;
1415

1516
/// Creates a tarball from a 'pkg' directory
@@ -45,6 +46,7 @@ pub fn publish(
4546
.default(0)
4647
.interact()?
4748
.to_string();
49+
let target = Target::from_str(&target)?;
4850
let build_opts = BuildOptions {
4951
path: Some(crate_path.clone()),
5052
target,

src/manifest/mod.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use self::npm::{
1111
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
1212
};
1313
use cargo_metadata::Metadata;
14-
use command::build::BuildProfile;
14+
use command::build::{BuildProfile, Target};
1515
use emoji;
1616
use failure::{Error, ResultExt};
1717
use progressbar::Step;
@@ -377,19 +377,18 @@ impl CrateData {
377377
out_dir: &Path,
378378
scope: &Option<String>,
379379
disable_dts: bool,
380-
target: &str,
380+
target: &Target,
381381
step: &Step,
382382
) -> Result<(), Error> {
383383
let msg = format!("{}Writing a package.json...", emoji::MEMO);
384384

385385
PBAR.step(step, &msg);
386386
let pkg_file_path = out_dir.join("package.json");
387-
let npm_data = if target == "nodejs" {
388-
self.to_commonjs(scope, disable_dts, out_dir)
389-
} else if target == "no-modules" {
390-
self.to_nomodules(scope, disable_dts, out_dir)
391-
} else {
392-
self.to_esmodules(scope, disable_dts, out_dir)
387+
let npm_data = match target {
388+
Target::Nodejs => self.to_commonjs(scope, disable_dts, out_dir),
389+
Target::NoModules => self.to_nomodules(scope, disable_dts, out_dir),
390+
Target::Bundler => self.to_esmodules(scope, disable_dts, out_dir),
391+
Target::Web => self.to_web(scope, disable_dts, out_dir),
393392
};
394393

395394
let npm_json = serde_json::to_string_pretty(&npm_data)?;
@@ -522,6 +521,35 @@ impl CrateData {
522521
})
523522
}
524523

524+
fn to_web(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage {
525+
let data = self.npm_data(scope, false, disable_dts, out_dir);
526+
let pkg = &self.data.packages[self.current_idx];
527+
528+
self.check_optional_fields();
529+
530+
NpmPackage::ESModulesPackage(ESModulesPackage {
531+
name: data.name,
532+
collaborators: pkg.authors.clone(),
533+
description: self.manifest.package.description.clone(),
534+
version: pkg.version.clone(),
535+
license: self.license(),
536+
repository: self
537+
.manifest
538+
.package
539+
.repository
540+
.clone()
541+
.map(|repo_url| Repository {
542+
ty: "git".to_string(),
543+
url: repo_url,
544+
}),
545+
files: data.files,
546+
module: data.main,
547+
homepage: data.homepage,
548+
types: data.dts_file,
549+
side_effects: "false".to_string(),
550+
})
551+
}
552+
525553
fn to_nomodules(
526554
&self,
527555
scope: &Option<String>,

tests/all/manifest.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashSet;
33
use std::fs;
44
use std::path::PathBuf;
55
use utils::{self, fixture};
6+
use wasm_pack::command::build::Target;
67
use wasm_pack::{self, license, manifest};
78

89
#[test]
@@ -66,7 +67,7 @@ fn it_creates_a_package_json_default_path() {
6667
let step = wasm_pack::progressbar::Step::new(1);
6768
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
6869
assert!(crate_data
69-
.write_package_json(&out_dir, &None, false, "", &step)
70+
.write_package_json(&out_dir, &None, false, &Target::Bundler, &step)
7071
.is_ok());
7172
let package_json_path = &fixture.path.join("pkg").join("package.json");
7273
fs::metadata(package_json_path).unwrap();
@@ -102,7 +103,7 @@ fn it_creates_a_package_json_provided_path() {
102103
let step = wasm_pack::progressbar::Step::new(1);
103104
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
104105
assert!(crate_data
105-
.write_package_json(&out_dir, &None, false, "", &step)
106+
.write_package_json(&out_dir, &None, false, &Target::Bundler, &step)
106107
.is_ok());
107108
let package_json_path = &fixture.path.join("pkg").join("package.json");
108109
fs::metadata(package_json_path).unwrap();
@@ -131,7 +132,13 @@ fn it_creates_a_package_json_provided_path_with_scope() {
131132
let step = wasm_pack::progressbar::Step::new(1);
132133
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
133134
assert!(crate_data
134-
.write_package_json(&out_dir, &Some("test".to_string()), false, "", &step)
135+
.write_package_json(
136+
&out_dir,
137+
&Some("test".to_string()),
138+
false,
139+
&Target::Bundler,
140+
&step
141+
)
135142
.is_ok());
136143
let package_json_path = &fixture.path.join("pkg").join("package.json");
137144
fs::metadata(package_json_path).unwrap();
@@ -160,7 +167,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
160167
let step = wasm_pack::progressbar::Step::new(1);
161168
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
162169
assert!(crate_data
163-
.write_package_json(&out_dir, &None, false, "nodejs", &step)
170+
.write_package_json(&out_dir, &None, false, &Target::Nodejs, &step)
164171
.is_ok());
165172
let package_json_path = &out_dir.join("package.json");
166173
fs::metadata(package_json_path).unwrap();
@@ -196,7 +203,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
196203
let step = wasm_pack::progressbar::Step::new(1);
197204
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
198205
assert!(crate_data
199-
.write_package_json(&out_dir, &None, false, "no-modules", &step)
206+
.write_package_json(&out_dir, &None, false, &Target::NoModules, &step)
200207
.is_ok());
201208
let package_json_path = &out_dir.join("package.json");
202209
fs::metadata(package_json_path).unwrap();
@@ -231,7 +238,7 @@ fn it_creates_a_pkg_json_in_out_dir() {
231238
let step = wasm_pack::progressbar::Step::new(1);
232239
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
233240
assert!(crate_data
234-
.write_package_json(&out_dir, &None, false, "", &step)
241+
.write_package_json(&out_dir, &None, false, &Target::Bundler, &step)
235242
.is_ok());
236243

237244
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
@@ -247,7 +254,7 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
247254
let step = wasm_pack::progressbar::Step::new(1);
248255
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
249256
assert!(crate_data
250-
.write_package_json(&out_dir, &None, true, "", &step)
257+
.write_package_json(&out_dir, &None, true, &Target::Bundler, &step)
251258
.is_ok());
252259
let package_json_path = &out_dir.join("package.json");
253260
fs::metadata(package_json_path).unwrap();
@@ -310,7 +317,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
310317
let step = wasm_pack::progressbar::Step::new(2);
311318
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
312319
crate_data
313-
.write_package_json(&out_dir, &None, true, "", &step)
320+
.write_package_json(&out_dir, &None, true, &Target::Bundler, &step)
314321
.unwrap();
315322

316323
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -327,7 +334,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
327334
let step = wasm_pack::progressbar::Step::new(2);
328335
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
329336
crate_data
330-
.write_package_json(&out_dir, &None, true, "", &step)
337+
.write_package_json(&out_dir, &None, true, &Target::Bundler, &step)
331338
.unwrap();
332339

333340
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -430,7 +437,7 @@ fn it_lists_license_files_in_files_field_of_package_json() {
430437
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
431438
license::copy_from_crate(&crate_data, &fixture.path, &out_dir, &step).unwrap();
432439
crate_data
433-
.write_package_json(&out_dir, &None, false, "", &step)
440+
.write_package_json(&out_dir, &None, false, &Target::Bundler, &step)
434441
.unwrap();
435442

436443
let package_json_path = &fixture.path.join("pkg").join("package.json");

0 commit comments

Comments
 (0)