Skip to content

Fix Webpack warning caused by dynamic require#234

Merged
josephlr merged 2 commits intorust-random:masterfrom
mjhanninen:fix-webpack-dynamic-require-error
Jan 14, 2022
Merged

Fix Webpack warning caused by dynamic require#234
josephlr merged 2 commits intorust-random:masterfrom
mjhanninen:fix-webpack-dynamic-require-error

Conversation

@mjhanninen
Copy link
Copy Markdown
Contributor

This PR reverts the changes in commit 120a1d7 partially . In particular, it changes the wasm-bindgen binding of module.requirement back to the older form. As a result the generated Javascript binding code looks now like:

 getObject(arg0).require(getStringFromWasm0(arg1, arg2))

As a consequence Webpack does not detect this as a package import and, thus, does not attempt to bundle it which solves problem. It is safe skip the bundling of the crypto package because (1) it is imported only on Node and (2) it comes with Node.

Relevant issues:

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 120a1d7 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 120a1d7 so that the generated
binding obfuscates the `require` call enough to hide it from Webpack
again.
Signed-off-by: Joe Richey <joerichey@google.com>
@josephlr
Copy link
Copy Markdown
Member

Sorry for the delay. I was hoping there would eventually be a better solution to #224 but nobody has chimed in, so I'll just merge this (as it does seem to work).

@josephlr josephlr merged commit e6e7dd6 into rust-random:master Jan 14, 2022
takumi-earth pushed a commit to earthlings-dev/getrandom that referenced this pull request Jan 27, 2026
…c-require-error

Fix Webpack warning caused by dynamic require
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

Successfully merging this pull request may close these issues.

2 participants