Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack-warning about dynamic require-expression #822

Closed
simonwep opened this issue Mar 30, 2020 · 2 comments
Closed

webpack-warning about dynamic require-expression #822

simonwep opened this issue Mar 30, 2020 · 2 comments

Comments

@simonwep
Copy link

馃悰 Bug description

I'm getting the following warning from webpack:

 WARNING in ./crate/pkg/index.js 195:14-53
    Critical dependency: the request of a dependency is an expression

The problem is this line:

// [...]
export const __wbg_require_604837428532a733 = function(arg0, arg1) {
    var ret = require(getStringFromWasm0(arg0, arg1)); // << dynamic require expression
    return addHeapObject(ret);
};
// [...]

馃 Expected Behavior

There should be no warning I guess.

馃憻 Steps to reproduce

Command I used to build my crate:

wasm-pack build "<path>" --release  --out-name index

My Cargo.toml:

[package]
name = "..."
version = "0.0.0"
authors = ["..."]
edition = "2018"

[lib]
name = "life"
path = "src/lib.rs"
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.59"
getrandom = { version="0.1.14", features = ["wasm-bindgen"]}

# 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 = "0.1.6"

# `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.
wee_alloc = "0.4.5"

[profile.release]
lto = true
opt-level = "s"

Rust file:

use wasm_bindgen::prelude::*;
use getrandom::Error;

#[wasm_bindgen]
pub fn random_bool(limit: f32) -> bool {
    match random() {
        Ok(val) => val > limit,
        Err(_) => false,
    }
}

pub fn random() -> Result<f32, Error> {
    let mut buf = [0u8; 1];
    getrandom::getrandom(&mut buf)?;
    Ok(buf[0] as f32 / 255.0)
}

The issue is somewhere related to getrandom as it works (e.g. the dynamic require disappears) when I remove the dependency / related code.

馃實 Your environment

Include the relevant details of your environment.
wasm-pack version: wasm-pack 0.9.1
rustc version: rustc 1.41.0 (5e1a79984 2020-01-27)

@Pauan
Copy link
Contributor

Pauan commented Mar 30, 2020

This is really an issue with Webpack, not wasm-pack or getrandom.

There aren't any great solutions: you can use the browser field in package.json to disable the crypto module, or maybe getrandom can be changed so that it uses inline_js to do the require call (though that has some other issues of its own).

Or maybe getrandom could use this solution, which actually seems like the most viable option.

@Pauan
Copy link
Contributor

Pauan commented Apr 27, 2020

This has been fixed in getrandom, thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants