Skip to content

webpack-warning about dynamic require-expression  #822

@simonwep

Description

@simonwep

🐛 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions