Skip to content

Djfarly nodejs flag #132

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

Merged
merged 4 commits into from
May 29, 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
16 changes: 16 additions & 0 deletions docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ wasm-pack init examples/js-hello-world
This path should point to a directory that contains a `Cargo.toml` file. If no
path is given, the `init` command will run in the current directory.

## Target

The init command accepts a `--target` argument. This will customize the output files
to align with a particular type of JS module. This allows wasm-pack to generate either
ES6 modules or CommonJS modules for use in browser and in NodeJS. Defaults to `browser`.
The options are:

```
wasm-pack init --target nodejs
```

| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------|
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. |
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. |

## Scope

The init command also accepts an optional `--scope` argument. This will scope
Expand Down
14 changes: 13 additions & 1 deletion src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
}
}

pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(), Error> {
pub fn wasm_bindgen_build(
path: &str,
name: &str,
disable_dts: bool,
target: String,
) -> Result<(), Error> {
let step = format!(
"{} {}Running WASM-bindgen...",
style("[7/7]").bold().dim(),
Expand All @@ -37,18 +42,25 @@ pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(
let pb = PBAR.message(&step);
let binary_name = name.replace("-", "_");
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);

let dts_arg = if disable_dts == false {
"--typescript"
} else {
"--no-typescript"
};

let target_arg = match target.as_str() {
"nodejs" => "--nodejs",
_ => "--browser",
};

let output = Command::new("wasm-bindgen")
.current_dir(path)
.arg(&wasm_path)
.arg("--out-dir")
.arg("./pkg")
.arg(dts_arg)
.arg(target_arg)
.output()?;
pb.finish();
if !output.status.success() {
Expand Down
30 changes: 24 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ pub enum Command {
/// 🐣 initialize a package.json based on your compiled wasm!
Init {
path: Option<String>,

#[structopt(long = "scope", short = "s")]
scope: Option<String>,

#[structopt(long = "no-typescript")]
/// By default a *.d.ts file is generated for the generated JS file, but
/// this flag will disable generating this TypeScript file.
disable_dts: bool,

#[structopt(long = "target", short = "t", default_value = "browser")]
/// Sets the target environment. [possible values: browser, nodejs]
target: String,
},

#[structopt(name = "pack")]
Expand All @@ -38,22 +46,30 @@ pub enum Command {
Login {
#[structopt(long = "registry", short = "r")]
/// Default: 'https://registry.npmjs.org/'.
/// 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.
/// 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.
registry: Option<String>,

#[structopt(long = "scope", short = "s")]
/// Default: none.
/// If specified, the user and login credentials given will be associated with the specified scope.
/// If specified, the user and login credentials given will be
/// associated with the specified scope.
scope: Option<String>,

#[structopt(long = "always-auth", short = "a")]
/// 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
/// 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
always_auth: bool,

#[structopt(long = "auth-type", short = "t")]
/// Default: 'legacy'.
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
/// 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.
/// 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.
auth_type: Option<String>,
},
}
Expand All @@ -66,7 +82,8 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
path,
scope,
disable_dts,
} => init(path, scope, disable_dts),
target,
} => init(path, scope, disable_dts, target),
Command::Pack { path } => pack(path),
Command::Publish { path } => publish(path),
Command::Login {
Expand Down Expand Up @@ -113,6 +130,7 @@ fn init(
path: Option<String>,
scope: Option<String>,
disable_dts: bool,
target: String,
) -> result::Result<(), Error> {
let started = Instant::now();

Expand All @@ -125,7 +143,7 @@ fn init(
readme::copy_from_crate(&crate_path)?;
bindgen::cargo_install_wasm_bindgen()?;
let name = manifest::get_crate_name(&crate_path)?;
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts)?;
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target)?;
PBAR.message(&format!(
"{} Done in {}",
emoji::SPARKLE,
Expand Down