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(cloudflare): introduce hybrid polyfills for the cloudflare preset #224

Merged
merged 8 commits into from
May 31, 2024

Conversation

IgorMinar
Copy link
Collaborator

This preset provides hybrid polyfills (#181) but requires special workerd flags.

Due to potential for breaking changes and extra requirements, we created a new preset rather than updating the existing one.

In the next major version of unenv, we should remove the cloudflare preset and rename cloudflareV2 to cloudflare.

🔗 Linked issue

#181

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@IgorMinar IgorMinar requested a review from pi0 as a code owner May 30, 2024 19:29
@IgorMinar IgorMinar requested a review from jculvey May 30, 2024 19:30
@IgorMinar
Copy link
Collaborator Author

@pi0 this one is a bit controversial as it introduces a new preset.

We could modify the existing cloudflare preset, but that would most likely be a breaking change for existing nitro apps, so we need to be careful here.

I'm really hoping that this extra preset is just a temporary thing we need to do to avoid a breaking change, but once nitro/nuxt is updated, we should drop the old preset and keep just this new one.

I called the preset cloudflareV2 but we could call it cloudflareExperimental or similar, because using that preset does currently require extra handholding.

What do you think?

@IgorMinar IgorMinar force-pushed the feat/workerd-hybrid-buffer branch 3 times, most recently from 0046dba to 3e58051 Compare May 31, 2024 07:29
@pi0
Copy link
Member

pi0 commented May 31, 2024

Thanks for the PR!

Currently unenv's cloudflare preset is not used by any version of Nuxt and Nitro i mainly made it few months ago to initiate an experiment but we hit the wrangler.toml automation and canceled so i think we can safely merge them. (Plus, i think considering risk of current changes, next version of unenv would be v2 with an explicit upgrade in current Nuxt, Nitro and H3 releases) 👍🏼

@pi0
Copy link
Member

pi0 commented May 31, 2024

@IgorMinar Just to double check is process.getBuiltinModule GA available or expected soon to be available?

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to adopt a convention like $cloudflare or $workerd for file naming to clearly indicate they are platform-specific overrides, really nice idea btw we could extend this convention later

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm good with $cloudflare. I'll update the PR.

@IgorMinar
Copy link
Collaborator Author

@IgorMinar Just to double check is process.getBuiltinModule GA available or expected soon to be available?

it will be. we'll need to coordinate the GA with [email protected]

@IgorMinar
Copy link
Collaborator Author

Thanks for the PR!

Currently unenv's cloudflare preset is not used by any version of Nuxt and Nitro i mainly made it few months ago to initiate an experiment but we hit the wrangler.toml automation and canceled so i think we can safely merge them. (Plus, i think considering risk of current changes, next version of unenv would be v2 with an explicit upgrade in current Nuxt, Nitro and H3 releases) 👍🏼

Excellent! I wasn't sure about the existing usage of the preset. I also agree that v2 will make getting this out easier.

In that case, I will go ahead and merge the cloudflare and cloudflareV2 presents into one.

@IgorMinar IgorMinar changed the title feat: add experimental cloudflareV2 preset feat(cloudflare): introduce hybrid polyfills for the cloudflare preset May 31, 2024
@IgorMinar
Copy link
Collaborator Author

IgorMinar commented May 31, 2024

@pi0 I addressed all your review feedback. PTAL

scryptSync,
sign,
verify,
} = unenvCrypto;
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense that we use export { ... } from './index' to improve the tree-shaking possibility (in case of ESM imports)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe that this will tree-shake just fine because everything is statically analyzable, but I'll verify.

Copy link
Member

@pi0 pi0 May 31, 2024

Choose a reason for hiding this comment

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

I think bundler would be confused because we first import as namespace then use (runtime) restructure. At least it cannot be a safe treeshake for it because namespace import can have side-effects as well as destructure

Copy link
Collaborator Author

@IgorMinar IgorMinar May 31, 2024

Choose a reason for hiding this comment

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

there is indeed a problem with tree-shaking. take a look at this...

Destructured import usage:

Namespaced import usage:

So the last pattern is the way to go even though it's the most verbose. Oh well...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems like the kind of optimization that rollup could handle automatically. Maybe one day. I got too spoiled by the capabilities of Closure Compiler at Google.

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Other than two suggestions seems nice upgrade!

Copy link
Collaborator

@jculvey jculvey left a comment

Choose a reason for hiding this comment

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

Changes LGTM.

Quick sanity check: it seems like there's quite a few more modules that need similar treatment: diagnostics_check, async_hooks, path, process, stream...etc

Are we going to tackle those in a followup PR?

@IgorMinar
Copy link
Collaborator Author

@jculvey those will be in separate PRs. I don't want to expand the scope of this one any more. I just wanted to implement a few to prove that the technique works for many modules.

@pi0
Copy link
Member

pi0 commented May 31, 2024

Feel free to merge whenever ready @IgorMinar i'm fine with treeshaking being deferred.

@pi0
Copy link
Member

pi0 commented May 31, 2024

In #242 i'm moving node internals to /internal dirs. Not sure if makes sense but we could also use /cloudflare namespaces if it makes sense (current $ seems also nice..)

@IgorMinar
Copy link
Collaborator Author

Feel free to merge whenever ready @IgorMinar i'm fine with treeshaking being deferred.

@pi0 I just finished the refactoring and pushed. It turned out to be a slightly bigger refactor but nothing too surprising: f239524

If CI is also green, I'll merge.

@IgorMinar IgorMinar merged commit 3931166 into unjs:main May 31, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants