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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Webpack error due to dynamic require #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mjhanninen
Copy link
Owner

@mjhanninen mjhanninen commented Oct 15, 2021

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:

@mjhanninen mjhanninen changed the title Fix Webpacks error due to dynamic require Fix Webpack error due to dynamic require Oct 15, 2021
@mjhanninen mjhanninen force-pushed the fix-webpack-dynamic-require-error branch from 05d9e8e to 7fe10bd Compare October 15, 2021 17:17
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.
@mjhanninen mjhanninen force-pushed the fix-webpack-dynamic-require-error branch from 7fe10bd to 8372c16 Compare October 15, 2021 18:21
Signed-off-by: Joe Richey <joerichey@google.com>
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