Merge pull request #132 from ashleygwilliams/djfarly-nodejs-flag

Djfarly nodejs flag
master
Michael Gattozzi 7 years ago committed by GitHub
commit b4c25b2ab7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      docs/init.md
  2. 14
      src/bindgen.rs
  3. 30
      src/command.rs

@ -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

@ -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(),
@ -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() {

@ -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")]
@ -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>,
},
}
@ -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 {
@ -113,6 +130,7 @@ fn init(
path: Option<String>,
scope: Option<String>,
disable_dts: bool,
target: String,
) -> result::Result<(), Error> {
let started = Instant::now();
@ -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,

Loading…
Cancel
Save