Description
Describe the Bug
There have already been some good discussions on the challenges of circular dependencies (#32) but I'm wondering if there is anything else we can do to solve a specific subset of the problem.
My specific problem is that when attempt_direct_import@crates/cli-support/src/js/mod.rs
is used we generate js that looks like this:
export const __wbg_clearTimeout_f42b256aef985387 = typeof clearTimeout == 'function' ? clearTimeout : notDefined('clearTimeout');
It seems that in most cases bundlers don't have issues with circular dependencies because:
#[wasm_bindgen(start)]
seems to force a workable ordering between_bg.js
and_bg.wasm
- js functions can be referenced before they're defined
However in the case of __wbg_clearTimeout_f42b256aef985387
we're exporting a const which can't be referenced before use, so webpack will silently throw this exception:
ReferenceError: can't access lexical declaration '__wbg_clearTimeout_f42b256aef985387' before initialization
__wbg_clearTimeout_f42b256aef985387 webpack://rust-webpack-template/./pkg/index_bg.js?:4
<anonymous> webpack://rust-webpack-template/./pkg/index_bg.wasm?:15
a https://zero.lan:9000/index.js:102
<anonymous> webpack://rust-webpack-template/./pkg/index_bg.wasm?:8
wasm https://zero.lan:9000/pkg_index_js.js:38
__webpack_require__ https://zero.lan:9000/index.js:42
<anonymous> webpack://rust-webpack-template/./pkg/index_bg.js?:7
a https://zero.lan:9000/index.js:102
<anonymous> webpack://rust-webpack-template/./pkg/index_bg.js?:1
js https://zero.lan:9000/pkg_index_js.js:28
__webpack_require__ https://zero.lan:9000/index.js:42
<anonymous> webpack://rust-webpack-template/./pkg/index.js?:7
a https://zero.lan:9000/index.js:102
<anonymous> webpack://rust-webpack-template/./pkg/index.js?:1
js https://zero.lan:9000/pkg_index_js.js:18
__webpack_require__ https://zero.lan:9000/index.js:42
promise callback* webpack://rust-webpack-template/./js/index.js?:7
js https://zero.lan:9000/index.js:18
__webpack_require__ https://zero.lan:9000/index.js:42
<anonymous> https://zero.lan:9000/index.js:359
<anonymous> https://zero.lan:9000/index.js:361
index.js:116:34
(Retrieved by adding console.log(err)
to the error handler in the body
call in __webpack_require__.a
)
As I mentioned one workaround is to add a start function with #[wasm_bindgen(start)]
however this is less than ideal.
Steps to Reproduce
I created a sample repo with the problem here
You can see the bug by running npm run start
and noticing that our wasm never loads.
If you would like to see the workaround in action you can uncomment this block
If you would like to debug the webpack exception for yourself you can run npm run build
and insert the relevant console.log
statements in the generated output.
Additional Context
I'm wondering if the easiest answer is to just change attempt_direct_import
to return a function instead of a const?