feat(cloudflare): enable native node.js compatibility #3064
+444
−29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enables
cloudflare_pages
andcloudflare_module
(with recent compatibility date) to leverage from Native Node.js APIs implemented in workerd instead of using unenv mocks.Due to versioning issues, breaking changes of unenv v2, and different implementations of
@cloudflare/unenv-preset
(WIP), this PR duplicates hybrid modules and overlay preset for now.Utils related to direct fetch and
node:http
emulation is still used from unenv v1.Workerd nodejs_compatibility can now be safely enabled using generated
compatibility_flags
thanks to #2949 and work by @petebacondarwin ❤️Current issue: Hybrid modules need(workaround with an alternative method for hybrid handling) -- we miss some benefits of cloudflare/workerd#2147 (globals still injected from native)process.getBuiltinModule
which needsnodejs_compat_v2
compatibility flag being passed toworkerd
however, it also affectswrangler
behavior to do additional transforms which are incompatible with nitro. There isno_nodejs_compat_v2
however, it is mutually exclusive andnodejs_compat
alone does not enablesgetBuiltinModule
🚀 Bundle size improvement
As we can use workerd native built-ins, a minimal project size reduces from 132 kB (41.1 kB gzip) to 98.1 kB (32.3 kB gzip) (-33.9KB (8.8kB gz) / -25.6% (21.4% gz))
📦 Modules
Hybrid modules:
node:async_hooks
(missingasyncWrapProviders
)node:crypto
(missinghash
)node:util
/node:sys
(missingparseEnv
andstyleText
)(incompatible with unenv v1)node:module
(incompatible with unenv v1)node:console
(incompatible with unenv v1)node:process
Native modules: (unenv polyfill will be replaced by workerd)