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

Loading Multiple WASM bundles in the same Page #2700

Closed
fosskers opened this issue Oct 15, 2021 · 5 comments
Closed

Loading Multiple WASM bundles in the same Page #2700

fosskers opened this issue Oct 15, 2021 · 5 comments
Labels

Comments

@fosskers
Copy link

Summary

Lately I've been developing a browser extension entirely in Rust/WASM. Today for the first time I attempted to load multiple Content Scripts into the same page. Normally this is not a problem, so long as the the following is kept in mind:

There is only one global scope per frame, per extension. This means that variables from one content script can directly be accessed by another content script, regardless of how the content script was loaded.

When I noticed that the second script failed to load, I wondered why. Then I found my browser complaining to me that:

Uncaught SyntaxError: Identifier 'wasm_bindgen' has already been declared

Citing:

let wasm_bindgen;

that appears in the JS output of wasm-pack. Since I'm using two WASM bundles, and thus two JS shims, these symbols are thus clashing in the same namespace.

Question: Is there a way to obfuscate that symbol name at compile-time in order to avoid the symbol clash? Or are there deeper concerns within the wasm-bindgen world that require that symbol to be named exactly wasm_bindgen? Or perhaps am I overlooking something obvious in how I should be loading these bundles? Otherwise, it doesn't seem possible to load more than one WASM bundle per page, which doesn't sound right.

Please and thank you.

Additional Details

Note that I am compiling with --no-modules for technical reasons. I am also loading the WASM manually in my own shim script like:

// Fire up the WASM portion of the Seed app.
const { start } = wasm_bindgen;
wasm_bindgen(wasm_url).then(start);

because inlined JS (i.e. <script> tags) is not allowed in HTML of browser extensions. Presumably I'd have to use the obfuscated name here too.

@alexcrichton
Copy link
Contributor

I think the best solution here would be to get ES modules working since they'd contain their global state, but if not workable I think that the --no-modules-global might suffice here? (IIRC it simply renames the wasm_bindgen global variable)

@fosskers
Copy link
Author

Thank you, I suspect that flag will do the trick. I will be testing that tomorrow.

@fosskers
Copy link
Author

Debugging out loud:

[cargo-make] INFO - Execute Command: "wasm-pack" "build" "--target" "no-modules" "--no-modules-global" "wasm_bindgen_gmail" "--out-name" "integ-gmail" "--dev"
error: Found argument '--no-modules-global' which wasn't expected, or isn't valid in this context

USAGE:
    wasm-pack build --target <target>

For more information try --help

@alexcrichton
Copy link
Contributor

The --no-modules-global flag is for wasm-bindgen, not wasm-pack, so that's the issue there I think.

@fosskers
Copy link
Author

That was indeed the fix, I just needed a way to access that flag through wasm-pack (see the above PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants