Skip to content

Commit eda9685

Browse files
committed
feat(build): add optional package_name parameter
This commit adds optional `package_name` parameter, which defaults to Rust crate name.
1 parent 32e52ca commit eda9685

File tree

4 files changed

+104
-25
lines changed

4 files changed

+104
-25
lines changed

docs/src/commands/build.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ the npm documentation [here][npm-scope-documentation].
115115

116116
[npm-scope-documentation]: https://docs.npmjs.com/misc/scope
117117

118+
## Name
119+
120+
The `build` command also accepts an optional `--package-name` argument. This will override package name, which defaults to crate name. For example:
121+
122+
```
123+
wasm-pack build examples/js-hello-world --package-name hello-world
124+
```
125+
126+
This command would create a `package.json` file for a package called
127+
`hello-world`. For more information about scoping, you can refer to
128+
the npm documentation [here][npm-name-documentation].
129+
130+
[npm-name-documentation]: https://docs.npmjs.com/cli/v11/configuring-npm/package-json#name
131+
118132
## Mode
119133

120134
The `build` command accepts an optional `--mode` argument.

src/command/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Build {
2828
pub crate_path: PathBuf,
2929
pub crate_data: manifest::CrateData,
3030
pub scope: Option<String>,
31+
pub package_name: Option<String>,
3132
pub disable_dts: bool,
3233
pub weak_refs: bool,
3334
pub reference_types: bool,
@@ -124,6 +125,10 @@ pub struct BuildOptions {
124125
#[clap(long = "scope", short = 's')]
125126
pub scope: Option<String>,
126127

128+
/// The npm package name to use in package.json, if any.
129+
#[clap(long = "package-name", short = 'p')]
130+
pub package_name: Option<String>,
131+
127132
#[clap(long = "mode", short = 'm', default_value = "normal")]
128133
/// Sets steps to be run. [possible values: no-install, normal, force]
129134
pub mode: InstallMode,
@@ -191,6 +196,7 @@ impl Default for BuildOptions {
191196
Self {
192197
path: None,
193198
scope: None,
199+
package_name: None,
194200
mode: InstallMode::default(),
195201
disable_dts: false,
196202
weak_refs: false,
@@ -247,6 +253,7 @@ impl Build {
247253
crate_path,
248254
crate_data,
249255
scope: build_opts.scope,
256+
package_name: build_opts.package_name,
250257
disable_dts: build_opts.disable_dts,
251258
weak_refs: build_opts.weak_refs,
252259
reference_types: build_opts.reference_types,
@@ -392,6 +399,7 @@ impl Build {
392399
self.crate_data.write_package_json(
393400
&self.out_dir,
394401
&self.scope,
402+
&self.package_name,
395403
self.disable_dts,
396404
self.target,
397405
)?;

src/manifest/mod.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use self::npm::{
1616
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
1717
};
1818
use crate::command::build::{BuildProfile, Target};
19+
use crate::emoji::PACKAGE;
1920
use crate::PBAR;
2021
use cargo_metadata::Metadata;
2122
use chrono::offset;
@@ -621,6 +622,7 @@ impl CrateData {
621622
&self,
622623
out_dir: &Path,
623624
scope: &Option<String>,
625+
package_name: &Option<String>,
624626
disable_dts: bool,
625627
target: Target,
626628
) -> Result<()> {
@@ -636,10 +638,16 @@ impl CrateData {
636638
None
637639
};
638640
let npm_data = match target {
639-
Target::Nodejs => self.to_commonjs(scope, disable_dts, existing_deps, out_dir),
640-
Target::NoModules => self.to_nomodules(scope, disable_dts, existing_deps, out_dir),
641-
Target::Bundler => self.to_esmodules(scope, disable_dts, existing_deps, out_dir),
642-
Target::Web => self.to_web(scope, disable_dts, existing_deps, out_dir),
641+
Target::Nodejs => {
642+
self.to_commonjs(scope, package_name, disable_dts, existing_deps, out_dir)
643+
}
644+
Target::NoModules => {
645+
self.to_nomodules(scope, package_name, disable_dts, existing_deps, out_dir)
646+
}
647+
Target::Bundler => {
648+
self.to_esmodules(scope, package_name, disable_dts, existing_deps, out_dir)
649+
}
650+
Target::Web => self.to_web(scope, package_name, disable_dts, existing_deps, out_dir),
643651
// Deno does not need package.json
644652
Target::Deno => return Ok(()),
645653
};
@@ -654,6 +662,7 @@ impl CrateData {
654662
fn npm_data(
655663
&self,
656664
scope: &Option<String>,
665+
package_name: &Option<String>,
657666
add_js_bg_to_package_json: bool,
658667
disable_dts: bool,
659668
out_dir: &Path,
@@ -670,9 +679,11 @@ impl CrateData {
670679
}
671680

672681
let pkg = &self.data.packages[self.current_idx];
673-
let npm_name = match scope {
674-
Some(s) => format!("@{}/{}", s, pkg.name),
675-
None => pkg.name.clone(),
682+
let npm_name = match (scope, package_name) {
683+
(Some(s), Some(name)) => format!("@{}/{}", s, name),
684+
(Some(s), None) => format!("@{}/{}", s, pkg.name.clone()),
685+
(None, Some(name)) => name.clone(),
686+
(None, None) => pkg.name.clone(),
676687
};
677688

678689
let dts_file = if !disable_dts {
@@ -723,11 +734,12 @@ impl CrateData {
723734
fn to_commonjs(
724735
&self,
725736
scope: &Option<String>,
737+
package_name: &Option<String>,
726738
disable_dts: bool,
727739
dependencies: Option<HashMap<String, String>>,
728740
out_dir: &Path,
729741
) -> NpmPackage {
730-
let data = self.npm_data(scope, false, disable_dts, out_dir);
742+
let data = self.npm_data(scope, package_name, false, disable_dts, out_dir);
731743
let pkg = &self.data.packages[self.current_idx];
732744

733745
self.check_optional_fields();
@@ -754,11 +766,12 @@ impl CrateData {
754766
fn to_esmodules(
755767
&self,
756768
scope: &Option<String>,
769+
package_name: &Option<String>,
757770
disable_dts: bool,
758771
dependencies: Option<HashMap<String, String>>,
759772
out_dir: &Path,
760773
) -> NpmPackage {
761-
let data = self.npm_data(scope, true, disable_dts, out_dir);
774+
let data = self.npm_data(scope, package_name, true, disable_dts, out_dir);
762775
let pkg = &self.data.packages[self.current_idx];
763776

764777
self.check_optional_fields();
@@ -787,11 +800,12 @@ impl CrateData {
787800
fn to_web(
788801
&self,
789802
scope: &Option<String>,
803+
package_name: &Option<String>,
790804
disable_dts: bool,
791805
dependencies: Option<HashMap<String, String>>,
792806
out_dir: &Path,
793807
) -> NpmPackage {
794-
let data = self.npm_data(scope, false, disable_dts, out_dir);
808+
let data = self.npm_data(scope, package_name, false, disable_dts, out_dir);
795809
let pkg = &self.data.packages[self.current_idx];
796810

797811
self.check_optional_fields();
@@ -820,11 +834,12 @@ impl CrateData {
820834
fn to_nomodules(
821835
&self,
822836
scope: &Option<String>,
837+
package_name: &Option<String>,
823838
disable_dts: bool,
824839
dependencies: Option<HashMap<String, String>>,
825840
out_dir: &Path,
826841
) -> NpmPackage {
827-
let data = self.npm_data(scope, false, disable_dts, out_dir);
842+
let data = self.npm_data(scope, package_name, false, disable_dts, out_dir);
828843
let pkg = &self.data.packages[self.current_idx];
829844

830845
self.check_optional_fields();

tests/all/manifest.rs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn it_creates_a_package_json_default_path() {
7979
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
8080
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
8181
assert!(crate_data
82-
.write_package_json(&out_dir, &None, false, Target::Bundler)
82+
.write_package_json(&out_dir, &None, &None, false, Target::Bundler)
8383
.is_ok());
8484
let package_json_path = &fixture.path.join("pkg").join("package.json");
8585
fs::metadata(package_json_path).unwrap();
@@ -119,7 +119,7 @@ fn it_creates_a_package_json_provided_path() {
119119
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
120120
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
121121
assert!(crate_data
122-
.write_package_json(&out_dir, &None, false, Target::Bundler)
122+
.write_package_json(&out_dir, &None, &None, false, Target::Bundler)
123123
.is_ok());
124124
let package_json_path = &fixture.path.join("pkg").join("package.json");
125125
fs::metadata(package_json_path).unwrap();
@@ -149,7 +149,13 @@ fn it_creates_a_package_json_provided_path_with_scope() {
149149
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
150150
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
151151
assert!(crate_data
152-
.write_package_json(&out_dir, &Some("test".to_string()), false, Target::Bundler,)
152+
.write_package_json(
153+
&out_dir,
154+
&Some("test".to_string()),
155+
&None,
156+
false,
157+
Target::Bundler,
158+
)
153159
.is_ok());
154160
let package_json_path = &fixture.path.join("pkg").join("package.json");
155161
fs::metadata(package_json_path).unwrap();
@@ -172,14 +178,50 @@ fn it_creates_a_package_json_provided_path_with_scope() {
172178
assert_eq!(actual_files, expected_files);
173179
}
174180

181+
#[test]
182+
fn it_creates_a_package_json_provided_path_with_package_name() {
183+
let fixture = fixture::js_hello_world();
184+
let out_dir = fixture.path.join("pkg");
185+
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
186+
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
187+
assert!(crate_data
188+
.write_package_json(
189+
&out_dir,
190+
&None,
191+
&Some("hello-world".to_string()),
192+
false,
193+
Target::Bundler,
194+
)
195+
.is_ok());
196+
let package_json_path = &fixture.path.join("pkg").join("package.json");
197+
fs::metadata(package_json_path).unwrap();
198+
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
199+
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
200+
assert_eq!(pkg.name, "hello-world");
201+
assert_eq!(pkg.ty, "module");
202+
assert_eq!(pkg.main, "js_hello_world.js");
203+
204+
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
205+
let expected_files: HashSet<String> = [
206+
"js_hello_world.d.ts",
207+
"js_hello_world_bg.js",
208+
"js_hello_world_bg.wasm",
209+
"js_hello_world.js",
210+
]
211+
.iter()
212+
.map(|&s| String::from(s))
213+
.collect();
214+
assert_eq!(actual_files, expected_files);
215+
}
216+
175217
#[test]
176218
fn it_creates_a_pkg_json_with_correct_files_on_node() {
177219
let fixture = fixture::js_hello_world();
178220
let out_dir = fixture.path.join("pkg");
179221
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
180222
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
181223
assert!(crate_data
182-
.write_package_json(&out_dir, &None, false, Target::Nodejs)
224+
.write_package_json(&out_dir, &None, &None, false, Target::Nodejs)
183225
.is_ok());
184226
let package_json_path = &out_dir.join("package.json");
185227
fs::metadata(package_json_path).unwrap();
@@ -213,7 +255,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
213255
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
214256
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
215257
assert!(crate_data
216-
.write_package_json(&out_dir, &None, false, Target::NoModules)
258+
.write_package_json(&out_dir, &None, &None, false, Target::NoModules)
217259
.is_ok());
218260
let package_json_path = &out_dir.join("package.json");
219261
fs::metadata(package_json_path).unwrap();
@@ -247,7 +289,7 @@ fn it_creates_a_package_json_with_correct_files_when_out_name_is_provided() {
247289
let crate_data = manifest::CrateData::new(&fixture.path, Some("index".to_owned())).unwrap();
248290
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
249291
assert!(crate_data
250-
.write_package_json(&out_dir, &None, false, Target::Bundler)
292+
.write_package_json(&out_dir, &None, &None, false, Target::Bundler)
251293
.is_ok());
252294
let package_json_path = &fixture.path.join("pkg").join("package.json");
253295
fs::metadata(package_json_path).unwrap();
@@ -280,7 +322,7 @@ fn it_creates_a_pkg_json_in_out_dir() {
280322
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
281323
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
282324
assert!(crate_data
283-
.write_package_json(&out_dir, &None, false, Target::Bundler)
325+
.write_package_json(&out_dir, &None, &None, false, Target::Bundler)
284326
.is_ok());
285327

286328
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
@@ -295,7 +337,7 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
295337
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();
296338
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
297339
assert!(crate_data
298-
.write_package_json(&out_dir, &None, true, Target::Bundler)
340+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
299341
.is_ok());
300342
let package_json_path = &out_dir.join("package.json");
301343
fs::metadata(package_json_path).unwrap();
@@ -337,7 +379,7 @@ fn it_creates_a_package_json_with_npm_dependencies_provided_by_wasm_bindgen() {
337379
)
338380
.unwrap();
339381
assert!(crate_data
340-
.write_package_json(&out_dir, &None, true, Target::Bundler)
382+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
341383
.is_ok());
342384
let package_json_path = &out_dir.join("package.json");
343385
fs::metadata(package_json_path).unwrap();
@@ -409,7 +451,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
409451

410452
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
411453
crate_data
412-
.write_package_json(&out_dir, &None, true, Target::Bundler)
454+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
413455
.unwrap();
414456

415457
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -425,7 +467,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
425467

426468
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
427469
crate_data
428-
.write_package_json(&out_dir, &None, true, Target::Bundler)
470+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
429471
.unwrap();
430472

431473
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -464,7 +506,7 @@ fn it_sets_keywords_field_if_available_in_cargo_toml() {
464506

465507
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
466508
crate_data
467-
.write_package_json(&out_dir, &None, true, Target::Bundler)
509+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
468510
.unwrap();
469511

470512
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -482,7 +524,7 @@ fn it_sets_keywords_field_if_available_in_cargo_toml() {
482524

483525
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
484526
crate_data
485-
.write_package_json(&out_dir, &None, true, Target::Bundler)
527+
.write_package_json(&out_dir, &None, &None, true, Target::Bundler)
486528
.unwrap();
487529

488530
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
@@ -581,7 +623,7 @@ fn it_lists_license_files_in_files_field_of_package_json() {
581623
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
582624
license::copy_from_crate(&crate_data, &fixture.path, &out_dir).unwrap();
583625
crate_data
584-
.write_package_json(&out_dir, &None, false, Target::Bundler)
626+
.write_package_json(&out_dir, &None, &None, false, Target::Bundler)
585627
.unwrap();
586628

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

0 commit comments

Comments
 (0)