Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Add webpack_wasm_pack fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Nov 6, 2019
1 parent 1ff00b6 commit d2ce52a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E

let node = which::which("node").unwrap();
let mut command = Command::new(node);
let wranglerjs_path = install().expect("could not install wranglerjs");
let wranglerjs_path = install_wranglerjs().expect("could not install wranglerjs");
command.arg(wranglerjs_path);

//put path to our wasm_pack as env variable so wasm-pack-plugin can utilize it
let wasm_pack_path = install::install("wasm-pack", "rustwasm")?.binary("wasm-pack")?;
command.env("WASM_PACK_PATH", wasm_pack_path);
let has_wasm_pack_plugin = true;
if has_wasm_pack_plugin {
//put path to our wasm_pack as env variable so wasm-pack-plugin can utilize it
let tool_name = "wasm-pack";
let author = "rustwasm";
let wasm_pack_path = install::install(tool_name, author)?.binary(tool_name)?;
command.env("WASM_PACK_PATH", wasm_pack_path);
}

// create a temp file for IPC with the wranglerjs process
let mut temp_file = env::temp_dir();
Expand Down Expand Up @@ -311,16 +315,17 @@ fn get_source_dir() -> PathBuf {
}

// Install {wranglerjs} from our GitHub releases
fn install() -> Result<PathBuf, failure::Error> {
fn install_wranglerjs() -> Result<PathBuf, failure::Error> {
let wranglerjs_path = if install::target::DEBUG {
let source_path = get_source_dir();
let wranglerjs_path = source_path.join("wranglerjs");
info!("wranglerjs at: {:?}", wranglerjs_path);
wranglerjs_path
} else {
let tool_name = "wranglerjs";
let author = "cloudflare";
let version = env!("CARGO_PKG_VERSION");
let wranglerjs_path = install::install_artifact(tool_name, "cloudflare", version)?;
let wranglerjs_path = install::install_artifact(tool_name, author, version)?;
info!("wranglerjs downloaded at: {:?}", wranglerjs_path.path());
wranglerjs_path.path()
};
Expand Down
18 changes: 18 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod utils;

use assert_cmd::prelude::*;

use std::env;
use std::fs::File;
use std::io::Write;
use std::process::Command;
Expand Down Expand Up @@ -244,6 +245,23 @@ fn it_builds_with_webpack_target_webworker() {
utils::cleanup(fixture);
}

#[test]
fn it_builds_with_webpack_wasm_pack() {
let fixture = "webpack_wasm_pack";
utils::create_temporary_copy(fixture);

single_env_settings! {fixture, r#"
type = "webpack"
webpack_config = "webpack.config.js"
"#};

// make sure wrangler overrides WASM_PACK_PATH
env::set_var("WASM_PACK_PATH", "invalid_wasm_pack_path");

build(fixture);
utils::cleanup(fixture);
}

fn build(fixture: &str) {
// Lock to avoid having concurrent builds
let _g = BUILD_LOCK.lock().unwrap();
Expand Down
35 changes: 35 additions & 0 deletions tests/fixtures/webpack_wasm_pack/crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "worker"
version = "0.1.0"
authors = ["Avery Harnish <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.2"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
22 changes: 22 additions & 0 deletions tests/fixtures/webpack_wasm_pack/crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate cfg_if;
extern crate wasm_bindgen;

mod utils;

use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;

cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}

#[wasm_bindgen]
pub fn greet() -> String {
"Hello, wasm-worker!".to_string()
}
17 changes: 17 additions & 0 deletions tests/fixtures/webpack_wasm_pack/crate/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cfg_if::cfg_if;

cfg_if! {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/webpack_wasm_pack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import("./crate/pkg/index.js").then(module => {
module.greet();
});
14 changes: 14 additions & 0 deletions tests/fixtures/webpack_wasm_pack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "webpack_wasm_pack",
"version": "1.0.0",
"description": "",
"main": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.0.1"
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/webpack_wasm_pack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require("path");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = {
"entry": "./index.js",
"target": "webworker",
plugins: [
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, "crate"),
}),
]
}

0 comments on commit d2ce52a

Please sign in to comment.