-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding in wrapper file to fix circular dependency with Webpack 5 #2110
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
Conversation
|
I am doing it by hand til now. So thanks for your help. Did you see the node dependencies problem too ? Webpack 5 doesn't include automatically node module. I think that typescript declaration file has to be changed too. |
Maybe you can only reexport relevant exports. Currently all helper methods are accessible from outside. This new extra layer can be used to create a facade. |
|
@sokra Sure, that's definitely possible, but I'd rather save that for a different PR, since that's a larger change. |
|
@BenoitDel This PR doesn't do anything with |
|
@Pauan about util, I can open an issue if you prefer. I checked and the util is required both with target bundler and browser. i am new to all of this so i can be wrong. With the current implementation (I mean without this PR) only name import from wasm module work. And the typescript declaration doesn't fit. I can also open another issue about this. Sorry if it was not the place to talk about wasm-bindgen and webpack compatibility in general. Just tell me what to do |
|
@BenoitDel You would use I'm not sure what you mean by the declaration not fitting, it seems fine to me. Could you file an issue for that? |
The ESM integration spec has changed so that bindings are no longer live (they are "snapshotted" during the linking phase). Normally this isn't a big deal, but it changes the behavior for circular dependencies.
Webpack 5 implements the new ESM integration spec, whereas Webpack 4 implements the old ESM integration spec. That means that wasm-bindgen works fine in Webpack 4 but is broken in Webpack 5.
To explain more clearly why the circular dependencies are now a problem:
The
index.jsfile importsindex_bg.wasmwhich in turn importsindex.js. This is necessary becauseindex_bg.wasmneeds to use the glue code defined inindex.js, andindex.jsneeds to export the functions defined inindex_bg.wasm.This situation is covered in detail in the ESM integration proposal.
The short explanation is that when
index_bg.wasmimportsindex.js, it immediately snapshots the bindings fromindex.jsbefore theindex.jsfile has been evaluated. That means the bindings are in a "temporal dead zone" which means they will error when they are used.The only way to fix this is to export function declarations (e.g.
export function foo() { ... }), which means thatexport const foo = ...;andexport class Foo { ... }will not work.The other option is to import the
index_bg.wasmfile first, in which case the situation is reversed: nowindex_bg.wasmcan correctly access all of theindex.jsexports, butindex.jscannot use any of theindex_bg.wasmexports at the top level.So this PR fixes the problem in the following way:
The
index.jsfile is renamed toindex_bg.js(andindex_bg.wasmimportsindex_bg.js).The
index_bg.jsfile no longer callswasm.__wbindgen_start()(because it cannot call anyindex_bg.wasmexports at the top level).A new
index.jsfile is created which imports theindex_bg.wasmfile first, then imports theindex_bg.jsfile, then callswasm.__wbindgen_start():The ordering is important: by importing
index_bg.wasmfirst, it gives us the correct circular ordering.So the end result is that we just needed a tiny
index.jswrapper file in order to force the correct evaluation order, so this was actually a very small change.