-
-
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
Add support for linked modules #3069
Conversation
Apologies but I personally do not have the time to adequately review new major features like this. Others may be able to but I will be unable to provide review or issue support for this if it lands. |
Requesting my own review so I don't forget. This is something I've experimented with and can be very useful when it comes to lazy loading modules |
fde73ce
to
157d50c
Compare
I needed to rebase because of #3073. Because the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks fine to me. It's worth noting that I'm not too familiar with this part of the code base so I could've missed something important.
A couple of things are missing here. Can you add tests and documentation for this feature?
It's not really possible to update the book1 so adding rustdoc comments (including an example) to the macro would be fine.
Footnotes
157d50c
to
74bec76
Compare
64b65fc
to
5e6d12a
Compare
5e6d12a
to
b8b1363
Compare
287b461
to
2f3da4a
Compare
12d6449
to
5ea1515
Compare
5ea1515
to
7697dea
Compare
d9dde32
to
3b8542b
Compare
62a172a
to
da157a5
Compare
da157a5
to
1edb30d
Compare
Other bundlers are a valid concern. Luckily, it can be solved at bindgen time, because the generated JS import code can choose to return a data URI. It took me some time though to figure out embedding the content in @Liamolucko, I'm sorry for the delay. Do you accept this approach? If so, I'll have to add tests with embedding probably. Is there anything else missing (I don't think so)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to put it behind a flag! I think that works out well; by default, everything 'just works', and then if you want the stricter CSP and/or lazy loading, you can manually configure your bundler to make things work and turn on the flag.
cb591f3
to
e2f942c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I just have some nitpicks about the phrasing of the docs.
66f7b71
to
4445006
Compare
Co-authored-by: Liam Murphy <liampm32@gmail.com>
@Liamolucko, I have overlooked the other suggestion. It is committed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for all of your work on this!
Like imported modules, linked modules can be added by any crate with the
wasm_bindgen::link_to!
macro and the existingmodule
,raw_module
orinline_js
attributes. In contrast to imported modules, no imports are added to the JS shim generated by wasm-bindgen. Instead, the macro expands to an expression of typeString
that contains a link. This link may be relative and is suitable for creating workers or worklets, or dynamic imports.Internally, the macro invocation
wasm_bindgen::link_to!(module = "/src/task/worker.js")
expands to code like (simplified):This link function is implemented in the JS shim:
The main reason for this approach is that it works both without bundlers and with bundlers (like webpack >= 5) that detect the
new URL(…, import.meta.url)
syntax, bundle and minify the linked file, and change the link. Depending on the target, the correct alternative forimport.meta.url
is used.This is a useful part of #3034. In contrast to #3034, importing the JS shim is not supported, so this PR can only be used for pure JS workers or worklets (not considering workarounds).
Fixes #2335 (Just create a script element or dynamically import your linked module before using JS classes from Rust)