Skip to content

Commit b4c25b2

Browse files
authored
Merge pull request #132 from ashleygwilliams/djfarly-nodejs-flag
Djfarly nodejs flag
2 parents 7a29377 + db37920 commit b4c25b2

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

docs/init.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ wasm-pack init examples/js-hello-world
1616
This path should point to a directory that contains a `Cargo.toml` file. If no
1717
path is given, the `init` command will run in the current directory.
1818

19+
## Target
20+
21+
The init command accepts a `--target` argument. This will customize the output files
22+
to align with a particular type of JS module. This allows wasm-pack to generate either
23+
ES6 modules or CommonJS modules for use in browser and in NodeJS. Defaults to `browser`.
24+
The options are:
25+
26+
```
27+
wasm-pack init --target nodejs
28+
```
29+
30+
| Option | Description |
31+
|-----------|-----------------------------------------------------------------------------------------------------------------|
32+
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. |
33+
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. |
34+
1935
## Scope
2036

2137
The init command also accepts an optional `--scope` argument. This will scope

src/bindgen.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
2828
}
2929
}
3030

31-
pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(), Error> {
31+
pub fn wasm_bindgen_build(
32+
path: &str,
33+
name: &str,
34+
disable_dts: bool,
35+
target: String,
36+
) -> Result<(), Error> {
3237
let step = format!(
3338
"{} {}Running WASM-bindgen...",
3439
style("[7/7]").bold().dim(),
@@ -37,18 +42,25 @@ pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(
3742
let pb = PBAR.message(&step);
3843
let binary_name = name.replace("-", "_");
3944
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);
45+
4046
let dts_arg = if disable_dts == false {
4147
"--typescript"
4248
} else {
4349
"--no-typescript"
4450
};
4551

52+
let target_arg = match target.as_str() {
53+
"nodejs" => "--nodejs",
54+
_ => "--browser",
55+
};
56+
4657
let output = Command::new("wasm-bindgen")
4758
.current_dir(path)
4859
.arg(&wasm_path)
4960
.arg("--out-dir")
5061
.arg("./pkg")
5162
.arg(dts_arg)
63+
.arg(target_arg)
5264
.output()?;
5365
pb.finish();
5466
if !output.status.success() {

src/command.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ pub enum Command {
1919
/// 🐣 initialize a package.json based on your compiled wasm!
2020
Init {
2121
path: Option<String>,
22+
2223
#[structopt(long = "scope", short = "s")]
2324
scope: Option<String>,
25+
2426
#[structopt(long = "no-typescript")]
27+
/// By default a *.d.ts file is generated for the generated JS file, but
28+
/// this flag will disable generating this TypeScript file.
2529
disable_dts: bool,
30+
31+
#[structopt(long = "target", short = "t", default_value = "browser")]
32+
/// Sets the target environment. [possible values: browser, nodejs]
33+
target: String,
2634
},
2735

2836
#[structopt(name = "pack")]
@@ -38,22 +46,30 @@ pub enum Command {
3846
Login {
3947
#[structopt(long = "registry", short = "r")]
4048
/// Default: 'https://registry.npmjs.org/'.
41-
/// The base URL of the npm package registry. If scope is also specified, this registry will only be used for packages with that scope. scope defaults to the scope of the project directory you're currently in, if any.
49+
/// The base URL of the npm package registry. If scope is also
50+
/// specified, this registry will only be used for packages with that
51+
/// scope. scope defaults to the scope of the project directory you're
52+
/// currently in, if any.
4253
registry: Option<String>,
4354

4455
#[structopt(long = "scope", short = "s")]
4556
/// Default: none.
46-
/// If specified, the user and login credentials given will be associated with the specified scope.
57+
/// If specified, the user and login credentials given will be
58+
/// associated with the specified scope.
4759
scope: Option<String>,
4860

4961
#[structopt(long = "always-auth", short = "a")]
50-
/// If specified, save configuration indicating that all requests to the given registry should include authorization information. Useful for private registries. Can be used with --registry and / or --scope
62+
/// If specified, save configuration indicating that all requests to the
63+
/// given registry should include authorization information. Useful for
64+
/// private registries. Can be used with --registry and / or --scope
5165
always_auth: bool,
5266

5367
#[structopt(long = "auth-type", short = "t")]
5468
/// Default: 'legacy'.
5569
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
56-
/// What authentication strategy to use with adduser/login. Some npm registries (for example, npmE) might support alternative auth strategies besides classic username/password entry in legacy npm.
70+
/// What authentication strategy to use with adduser/login. Some npm
71+
/// registries (for example, npmE) might support alternative auth
72+
/// strategies besides classic username/password entry in legacy npm.
5773
auth_type: Option<String>,
5874
},
5975
}
@@ -66,7 +82,8 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
6682
path,
6783
scope,
6884
disable_dts,
69-
} => init(path, scope, disable_dts),
85+
target,
86+
} => init(path, scope, disable_dts, target),
7087
Command::Pack { path } => pack(path),
7188
Command::Publish { path } => publish(path),
7289
Command::Login {
@@ -113,6 +130,7 @@ fn init(
113130
path: Option<String>,
114131
scope: Option<String>,
115132
disable_dts: bool,
133+
target: String,
116134
) -> result::Result<(), Error> {
117135
let started = Instant::now();
118136

@@ -125,7 +143,7 @@ fn init(
125143
readme::copy_from_crate(&crate_path)?;
126144
bindgen::cargo_install_wasm_bindgen()?;
127145
let name = manifest::get_crate_name(&crate_path)?;
128-
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts)?;
146+
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target)?;
129147
PBAR.message(&format!(
130148
"{} Done in {}",
131149
emoji::SPARKLE,

0 commit comments

Comments
 (0)