Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Dec 5, 2025

It is now possible to access ctx.exports properties for the main (SELF) worker.

Due to the dynamic nature of Vitest the integration relies upon guessing what the exports of the main Worker are by statically analyzing the Worker source using esbuild. In cases where it is not possible to infer the exports (for example, a wildcard re-export of a virtual module) it is possible to declare these in the vitest-pool-workers config via the additionalExports setting.

A picture of a cute animal (not mandatory, but encouraged)

@changeset-bot
Copy link

changeset-bot bot commented Dec 5, 2025

🦋 Changeset detected

Latest commit: 0dc5ba5

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

@petebacondarwin petebacondarwin force-pushed the pbd/vitest/context-exports branch from b6b4f85 to a09f165 Compare December 5, 2025 17:40
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 5, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11533

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11533

miniflare

npm i https://pkg.pr.new/miniflare@11533

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11533

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11533

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11533

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11533

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11533

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11533

wrangler

npm i https://pkg.pr.new/wrangler@11533

commit: 8d0c798

@petebacondarwin petebacondarwin force-pushed the pbd/vitest/context-exports branch 6 times, most recently from 3aba7ce to 11eaa14 Compare December 9, 2025 17:16
@petebacondarwin petebacondarwin changed the title WIP - support ctx.exports in vitest support ctx.exports in vitest-pool-workers Dec 10, 2025
@petebacondarwin petebacondarwin marked this pull request as ready for review December 10, 2025 09:37
@petebacondarwin petebacondarwin requested a review from a team as a code owner December 10, 2025 09:37
@petebacondarwin petebacondarwin force-pushed the pbd/vitest/context-exports branch from 11eaa14 to 8d0c798 Compare December 10, 2025 10:41
): exports is Cloudflare.Exports {
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).Cloudflare?.compatibilityFlags.enable_ctx_exports &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this not in workers-types? Should we add it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently it is "internal"? It's not in our docs.
The only place I could find it being used was in the unenv preset code.

@@ -1,4 +1,5 @@
import assert from "node:assert";
import { exports } from "cloudflare:workers";
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's used on line 64 to add the exports onto the user generated ctx object.

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Dec 11, 2025
Copy link
Contributor

@jamesopstad jamesopstad left a comment

Choose a reason for hiding this comment

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

Very nice, @petebacondarwin! Great that you've managed to apply these changes so cleanly.

…OBJECT__` in order to reduce chance of collision with user code.

This is the approach used in the Vite plugin and reduces the complexity of our code since we can stop "prefixing" user exports.
@petebacondarwin petebacondarwin force-pushed the pbd/vitest/context-exports branch from b115efe to 0dc5ba5 Compare December 12, 2025 10:53
@petebacondarwin petebacondarwin merged commit 8d9003e into main Dec 12, 2025
37 of 39 checks passed
@petebacondarwin petebacondarwin deleted the pbd/vitest/context-exports branch December 12, 2025 12:16
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants