-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Adding in wrapper file to fix circular dependency with Webpack 5 #2110
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.js
file importsindex_bg.wasm
which in turn importsindex.js
. This is necessary becauseindex_bg.wasm
needs to use the glue code defined inindex.js
, andindex.js
needs 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.wasm
importsindex.js
, it immediately snapshots the bindings fromindex.js
before theindex.js
file 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.wasm
file first, in which case the situation is reversed: nowindex_bg.wasm
can correctly access all of theindex.js
exports, butindex.js
cannot use any of theindex_bg.wasm
exports at the top level.So this PR fixes the problem in the following way:
The
index.js
file is renamed toindex_bg.js
(andindex_bg.wasm
importsindex_bg.js
).The
index_bg.js
file no longer callswasm.__wbindgen_start()
(because it cannot call anyindex_bg.wasm
exports at the top level).A new
index.js
file is created which imports theindex_bg.wasm
file first, then imports theindex_bg.js
file, then callswasm.__wbindgen_start()
:The ordering is important: by importing
index_bg.wasm
first, it gives us the correct circular ordering.So the end result is that we just needed a tiny
index.js
wrapper file in order to force the correct evaluation order, so this was actually a very small change.