Skip to content

Fixing Webpack require warning when using wasm-bindgen #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/wasm32_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn getrandom_init() -> Result<RngSource, Error> {
return Ok(RngSource::Browser(crypto));
}

return Ok(RngSource::Node(node_require("crypto")));
return Ok(RngSource::Node(MODULE.require("crypto")));
}

#[wasm_bindgen]
Expand All @@ -102,12 +102,17 @@ extern "C" {
#[wasm_bindgen(method, js_name = getRandomValues, structural)]
fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]);

#[wasm_bindgen(js_name = require)]
fn node_require(s: &str) -> NodeCrypto;

#[derive(Clone, Debug)]
type NodeCrypto;

#[wasm_bindgen(method, js_name = randomFillSync, structural)]
fn random_fill_sync(me: &NodeCrypto, buf: &mut [u8]);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting nit: could we just put all of the Node.js declarations in their own block. Something like:

#[wasm_bindgen]
extern "C" {
    type NodeModule;
    #[wasm_bindgen(js_name = module)]
    static MODULE: NodeModule;

    #[derive(Clone, Debug)]
    type NodeCrypto;
    #[wasm_bindgen(method)]
    fn require(this: &NodeModule, s: &str) -> NodeCrypto;

    #[wasm_bindgen(method, js_name = randomFillSync)]
    fn random_fill_sync(this: &NodeCrypto, buf: &mut [u8]);
}

type NodeModule;

#[wasm_bindgen(js_name = module)]
static MODULE: NodeModule;

#[wasm_bindgen(method)]
fn require(this: &NodeModule, s: &str) -> NodeCrypto;
}