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

feat(@astrojs/cloudflare): Add support for wasm module imports #8542

Merged
merged 5 commits into from
Sep 22, 2023

Conversation

adrianlyjak
Copy link
Contributor

@adrianlyjak adrianlyjak commented Sep 13, 2023

Changes

Testing

  • automated tests were added that imports web assembly modules from the 3 different possible directory structures
  • manual testing can be done be importing a simple .wasm file and using it. Wasm files should be able to be imported from node_modules as well.
  • Refactors cloudflare wrangler tests to be more reliable by dynamically rotating ports, and retrying timeouts. Removes kill-port dependency. I attempted to removes skips for flaky startup (I wasn't encountering any flakiness after the port fix locally), however the windows and node 20 environments seem to still consistently fail on 1 or 2 wrangler cli tests. Instead I refactored the skip behavior to be a little simpler to opt-in or out.

Docs

Updated README for cloudflare adapter with small section on wasm modules.

@changeset-bot
Copy link

changeset-bot bot commented Sep 13, 2023

🦋 Changeset detected

Latest commit: 6a806ef

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Sep 13, 2023
@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Sep 14, 2023
@adrianlyjak adrianlyjak marked this pull request as ready for review September 14, 2023 03:22
@adrianlyjak adrianlyjak requested a review from a team as a code owner September 14, 2023 03:22
},
});
//
// Sadly, this needs to build esbuild for each depth of routes/entrypoints independently so that relative
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas how this could be improved for functionPerRoute mode? The problem is that the esbuild is effectively creating the exact same bundle and duplicating, and now the bundle needs to have relative imports, so the bundles need to be different depending on how deep they are in the tree

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO wasm should be an opt-in functionality

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexanderniebuhr could you explain why, please? I'm curious :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add my 2 cents. It would be nice if this was zero configuration. Its not really "changing" behavior like other configs, rather its adding behavior that the user would expect to work given what works in cloudflare.

That being said, this multiple esbundle depending on depth of the entrypoint is a bit of a hack, and if it stays that way, adding a configuration to opt-in to wasm loading for at least the function per route case makes sense to me.

@alexanderniebuhr were your reservations around this hack or something else?

About the hack, In my development, I wasn't able to specify a different import pattern for the different entrypoints using its onResolve callback. But it seems like there must be some other better way to do it. I was just hoping that someone might have better insight. Looking deeper at the esbuild plugin documentation, I might be able to do something with the onEnd callback to modify the imports within each entry

Copy link
Member

@alexanderniebuhr alexanderniebuhr Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ematipico @adrianlyjak let me explain!

I'm genuinely excited about this feature and would love to see it enabled by default in the long run. 🚀

However, I've got a few thoughts:

  • I'm a bit hesitant about the entryPathsGroupedByDepth solution. I believe there might be a more efficient approach out there. I'm already thinking about it but need some more time, to fully understand the requirements.
  • However I don't think it should block this PR - we should iterate on it in a future PR!
  • Given that wasm isn't a typical use-case for majority of users, my suggestion would be to make this feature opt-in for now. This way, we ensure we don't inadvertently degrade the experience for users without wasm - which we test for, but it still feels a bit hacky.
  • I also haven't dived deeper into a thorough code review yet. So I don't really know if the current solution is that hacky or fine. I was just going by my initial gut feeling and the comment provided by @adrianlyjak.
  • Once we've tested this with real-world use cases & optimized the adapter to reduce code branches (I'm working on that), I would consider making the default true, but still leave the flag intact (for user choice).

This mirrors how I usually approach runtime support: opt-in first & switching to default once I'm sure it works for 95% of users.

If we decide to invest the time to find a better solution for the depth issue and include it in this PR, I would love to see it enabled by default. I was just going to suggest: "Ship this first - deal with improvements later."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrianlyjak I'm keen to assist and offer a fresh perspective. While I haven't fully grasped all the issues yet, I'll delve deeper into your implementation to better understand the challenges you've described. I believe there might be some hooks we can leverage to streamline the process. To reiterate my earlier point, I feel we should merge this PR as it stands and focus on refining it in subsequent iterations.

What are your thoughts, @ematipico?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only further idea to support both would be to use base64 in the vite build always, and then somehow re-interpret an wasm import within the worker esbuild.

I ended up doing this. It seems to work well and not be too complicated. Main complaint is that there's loosely coupled and hard to follow assumptions between the 2 build stages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the long discussion and the technical details that surfaced, I think we should keep the feature opt-in, so we can discuss them offline (discord, github issue, etc.) I want @adrianlyjak 's contribution to be merged ASAP, but we shouldn't rush into "enabled by default".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the feature is opt-in. What's the process for merging? Or do you want further code review? I see this is starting to accumulate conflicts, and I'm hoping to not have to keep up with those. I'll rebase this for the time being (excited about the node compatibility that's conflicting here 😄 )

Copy link
Member

@alexanderniebuhr alexanderniebuhr Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me, amazing work! 🚀

In general the process is some more reviews, including one of docs team (maybe uncomment the review request from the PR template.) Once they all approve, merging will be handled by one of the maintainers :)

@adrianlyjak adrianlyjak changed the title fix(cloudflare): Add support for wasm module imports for #8541 fix(cloudflare): Add support for wasm module imports Sep 14, 2023
@adrianlyjak adrianlyjak changed the title fix(cloudflare): Add support for wasm module imports feat(@astrojs/cloudflare): Add support for wasm module imports Sep 17, 2023
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for these docs contributions, @adrianlyjak! I found them super easy to follow and I think they'll be helpful to people using the integration! 🙌

I just had some suggestions for your consideration to adjust them a tiny bit so they would be more in line with how we'd normally write our Astro documentation. See what you think and as I always have to say, I'm just editing for style and structure. It's your job to make the statements true! 😉

packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
packages/integrations/cloudflare/README.md Show resolved Hide resolved
packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good @adrianlyjak ! Just two tiny suggestions, then we're good to go!

packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to approve great docs! 🥳

@ematipico ematipico merged commit faeead4 into withastro:main Sep 22, 2023
13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cloudflare adapter doesn't support WASM, despite natively supporting WASM modules
5 participants