From 8372c165afcda48aade40750943eefcf12a483f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matti=20H=C3=A4nninen?= Date: Fri, 15 Oct 2021 21:00:21 +0300 Subject: [PATCH 1/2] Fix Webpack warning caused by dynamic require Webpack supports dynamic importing only for some special cases in which it is able to narrow down the set of packages to bundled. In the general case it just produces an empty (Webpack) context plus the warning stating that "the request of a dependency is an expression." Apparently the commit 120a1d7f changed the Javascript generated by wasm-bindgen so that the binding for the `require` became: ``` module.require(getStringFromWasm0(arg0, arg1)) ``` when it used to be: ``` getObject(arg0).require(getStringFromWasm0(arg1, arg2)) ``` In the latter case Webpack did not even realize that this code imported a package and, hence, did not try to bundle it. The new code triggers the bundling and because the dependency is fully dynamic Webpack has problems with it. This commit reverts partially the commit 120a1d7f so that the generated binding obfuscates the `require` call enough to hide it from Webpack again. --- src/js.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/js.rs b/src/js.rs index 6b4c8cc2..84f6ba7d 100644 --- a/src/js.rs +++ b/src/js.rs @@ -59,7 +59,9 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { fn getrandom_init() -> Result { let global: Global = global().unchecked_into(); if is_node(&global) { - let crypto = require("crypto").map_err(|_| Error::NODE_CRYPTO)?; + let crypto = NODE_MODULE + .require("crypto") + .map_err(|_| Error::NODE_CRYPTO)?; return Ok(RngSource::Node(crypto)); } @@ -102,9 +104,12 @@ extern "C" { #[wasm_bindgen(method, js_name = getRandomValues, catch)] fn get_random_values(this: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>; + type NodeModule; + #[wasm_bindgen(js_name = module)] + static NODE_MODULE: NodeModule; // Node JS crypto module (https://nodejs.org/api/crypto.html) - #[wasm_bindgen(catch, js_name = "module.require")] - fn require(s: &str) -> Result; + #[wasm_bindgen(method, catch)] + fn require(this: &NodeModule, s: &str) -> Result; type NodeCrypto; #[wasm_bindgen(method, js_name = randomFillSync, catch)] fn random_fill_sync(this: &NodeCrypto, buf: &mut [u8]) -> Result<(), JsValue>; From 8fad7c5ab4014ac755e6f95fb2bdeef0f674faa2 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 13 Jan 2022 18:48:09 -0800 Subject: [PATCH 2/2] js: Add comment explaining why we do this hack Signed-off-by: Joe Richey --- src/js.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/js.rs b/src/js.rs index 84f6ba7d..e910f2bc 100644 --- a/src/js.rs +++ b/src/js.rs @@ -104,6 +104,9 @@ extern "C" { #[wasm_bindgen(method, js_name = getRandomValues, catch)] fn get_random_values(this: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>; + // We use a "module" object here instead of just annotating require() with + // js_name = "module.require", so that Webpack doesn't give a warning. See: + // https://github.com/rust-random/getrandom/issues/224 type NodeModule; #[wasm_bindgen(js_name = module)] static NODE_MODULE: NodeModule;