-
Notifications
You must be signed in to change notification settings - Fork 785
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
fix(hybrid-nodejs-compat): inject modules only once #8191
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 9addcea The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-wrangler-8191 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/8191/npm-package-wrangler-8191 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-wrangler-8191 dev path/to/script.js Additional artifacts:cloudflare-workers-bindings-extension: wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-workers-bindings-extension-8191 -O ./cloudflare-workers-bindings-extension.0.0.0-v070d28207.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v070d28207.vsix create-cloudflare: npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-create-cloudflare-8191 --no-auto-update @cloudflare/kv-asset-handler: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-kv-asset-handler-8191 miniflare: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-miniflare-8191 @cloudflare/pages-shared: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-pages-shared-8191 @cloudflare/unenv-preset: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-unenv-preset-8191 @cloudflare/vite-plugin: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-vite-plugin-8191 @cloudflare/vitest-pool-workers: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-vitest-pool-workers-8191 @cloudflare/workers-editor-shared: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-workers-editor-shared-8191 @cloudflare/workers-shared: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-workers-shared-8191 @cloudflare/workflows-shared: npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13443167933/npm-package-cloudflare-workflows-shared-8191 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
This PR makes sure that modules are injected only once before injected all the related values.
You can see the effect for (from the Cloudflare preset):
The generated code before this PR:
and after this PR:
So the first thing is that the code size is reduced - as a data point
init_virtual_unenv_global_polyfill_unenv_runtime_node_console_cloudflare(); / init_virtual_unenv_global_polyfill_unenv_runtime_node_timers_cloudflare();
is repeated 106 times in thenodejs-hybrid-app
test.Another benefit (which actually is the reason for which I created this PR) is that you can inject multiple export from the same file when they have a dependency.
For examples, we would like to inject
performance
andPerformance
fromperf_hooks
. Howeverperformance
needs to instantiate aPerformance
so there is no way we could initialize with the current code.Let's say
Performance
is injected first fromperf_hooks
. Soperf_hooks
will be imported. The first thing that module will do (seeinit_utils
above) would be to inject all the values. It will then try to injectperformance
which importsperf_hooks
too which result in ESBuild doing nothing (because of the circular dependency) and then it can instantiatePerformance
because it is undefined.One last benefit is that we would stop using the polyfills (i.e.
init_virtual_unenv_global_polyfill_unenv_runtime_polyfill_performance();
) as I think the preferred way is to useinject
(the performance polyfill directly adds props onglobalThis
)