From e7c808491bea591e6f1cbf4acd9ae4e0d7f882ae Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Tue, 5 Nov 2019 17:44:37 -0600 Subject: [PATCH] Add webpack_wasm_pack fixture --- src/commands/build/wranglerjs/mod.rs | 19 ++++++++++------ tests/fixtures/webpack_wasm_pack/index.js | 3 +++ tests/fixtures/webpack_wasm_pack/src/lib.rs | 22 +++++++++++++++++++ tests/fixtures/webpack_wasm_pack/src/utils.rs | 17 ++++++++++++++ .../webpack_wasm_pack/webpack.config.js | 12 ++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/webpack_wasm_pack/index.js create mode 100644 tests/fixtures/webpack_wasm_pack/src/lib.rs create mode 100644 tests/fixtures/webpack_wasm_pack/src/utils.rs create mode 100644 tests/fixtures/webpack_wasm_pack/webpack.config.js diff --git a/src/commands/build/wranglerjs/mod.rs b/src/commands/build/wranglerjs/mod.rs index af6919fa9..229bacc8c 100644 --- a/src/commands/build/wranglerjs/mod.rs +++ b/src/commands/build/wranglerjs/mod.rs @@ -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(); @@ -311,7 +315,7 @@ fn get_source_dir() -> PathBuf { } // Install {wranglerjs} from our GitHub releases -fn install() -> Result { +fn install_wranglerjs() -> Result { let wranglerjs_path = if install::target::DEBUG { let source_path = get_source_dir(); let wranglerjs_path = source_path.join("wranglerjs"); @@ -319,8 +323,9 @@ fn install() -> Result { 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() }; diff --git a/tests/fixtures/webpack_wasm_pack/index.js b/tests/fixtures/webpack_wasm_pack/index.js new file mode 100644 index 000000000..4c4084596 --- /dev/null +++ b/tests/fixtures/webpack_wasm_pack/index.js @@ -0,0 +1,3 @@ +import("./src").then(module => { + module.greet(); +}); \ No newline at end of file diff --git a/tests/fixtures/webpack_wasm_pack/src/lib.rs b/tests/fixtures/webpack_wasm_pack/src/lib.rs new file mode 100644 index 000000000..b17d4f947 --- /dev/null +++ b/tests/fixtures/webpack_wasm_pack/src/lib.rs @@ -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() +} diff --git a/tests/fixtures/webpack_wasm_pack/src/utils.rs b/tests/fixtures/webpack_wasm_pack/src/utils.rs new file mode 100644 index 000000000..2ffc954db --- /dev/null +++ b/tests/fixtures/webpack_wasm_pack/src/utils.rs @@ -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() {} + } +} diff --git a/tests/fixtures/webpack_wasm_pack/webpack.config.js b/tests/fixtures/webpack_wasm_pack/webpack.config.js new file mode 100644 index 000000000..e8ee7f578 --- /dev/null +++ b/tests/fixtures/webpack_wasm_pack/webpack.config.js @@ -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"), + }), + ] +} \ No newline at end of file