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 5, 2019
1 parent 1ff00b6 commit e7c8084
Show file tree
Hide file tree
Showing 5 changed files with 66 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
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("./src").then(module => {
module.greet();
});
22 changes: 22 additions & 0 deletions tests/fixtures/webpack_wasm_pack/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/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() {}
}
}
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, "src"),
}),
]
}

0 comments on commit e7c8084

Please sign in to comment.