Skip to content

feat(vite): self-deduplicating Cloudflare plugin — no more env-var guard in vite configs - #1183

Open
sam-goodwin wants to merge 1 commit into
mainfrom
claude/vite-plugin-self-dedup
Open

sam-goodwin wants to merge 1 commit into
mainfrom
claude/vite-plugin-self-dedup

Conversation

@sam-goodwin

Copy link
Copy Markdown
Member

Follow-up to #1129: the Cloudflare Vite plugin now deduplicates itself, so apps that also build standalone can declare it unguarded — the ALCHEMY_CLOUDFLARE_VITE_INJECTED env-var ternary is no longer needed.

  // vite.config.ts
- process.env.ALCHEMY_CLOUDFLARE_VITE_INJECTED === "1" ? null : cloudflare({ ... })
+ cloudflare({ ... })

Orchestrated call sites (alchemy's viteDev/viteBuild, the Astro integration, the Waku cloudflare target) pass injected: true, which stamps a marker property on that instance's plugin objects. A non-injected instance carries an apply clause that scans the merged config's plugin tree for the marker and stands down:

plugin.apply = (config, env) =>
  !hasInjectedInstance(config.plugins ?? []) && originalApplies(apply, config, env);

Why this shape:

  • Vite evaluates apply before a plugin enters the pipeline — a standing-down instance runs no hooks, boots no workerd, and never appears in configResolved.plugins, so the name-based cross-plugin lookups (distilled-cloudflare:options) can only find the injected instance.
  • Detection rides on the plugin objects, not module state: the orchestrator's instance and the config-file instance may come from different copies of the module (app node_modules vs orchestrator's), so a module-level flag would not be shared between them.
  • The env var is still set for existing configs that guard on it — their ternary yields null, which is equally correct. It's now documented as deprecated for new configs.

Coverage: unit + vite.resolveConfig integration tests assert exactly one active plugin stack in every combination (plugin-dedup.test.ts), and the Website vite fixture's vite.config.ts now declares the plugin unguarded — the Vite, Vite.local, and ViteRoutePrefix suites (18 tests) only pass if the config-file instance stands down in both dev and live builds.

🤖 Generated with Claude Code

…stand down against the orchestrated instance

Apps that also build standalone declare `cloudflare({...})` in their own
vite.config.ts; alchemy-orchestrated runs inject a second, fully
resolved instance. The config-file instance previously had to guard
itself with the ALCHEMY_CLOUDFLARE_VITE_INJECTED env-var ternary or two
same-named plugin stacks would load (name-resolved cross-plugin lookups
hit the wrong instance; dev boots a bindings-less second workerd).

The plugin now dedups itself: orchestrated call sites (alchemy's
viteDev/viteBuild, the Astro integration, the Waku cloudflare target)
pass `injected: true`, which stamps a marker property on the instance's
plugin objects; a non-injected instance carries an `apply` clause that
scans the merged config's plugin tree for the marker and stands down.
Vite evaluates `apply` before a plugin enters the pipeline, so a
standing-down instance runs no hooks, boots no workerd, and never
appears in configResolved.plugins — name lookups can only find the
injected instance. Detection rides on the plugin objects (not module
state) because the two instances may come from different copies of the
module (app node_modules vs orchestrator).

The env var is still set for existing configs that guard on it (their
ternary yields null, equally correct); new configs need no guard — the
vite-fixture's config now declares the plugin unguarded to pin the
behavior end to end.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@alchemy-version-bot

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

Alchemy

alchemy

bun add https://pkg.ing/alchemy/5bc6eca

@alchemy.run/better-auth

bun add https://pkg.ing/@alchemy.run/better-auth/5bc6eca

@alchemy.run/cloudflare-runtime

bun add https://pkg.ing/@alchemy.run/cloudflare-runtime/5bc6eca

@alchemy.run/cloudflare-frameworks

bun add https://pkg.ing/@alchemy.run/cloudflare-frameworks/5bc6eca

@alchemy.run/pr-package

bun add https://pkg.ing/@alchemy.run/pr-package/5bc6eca

Distilled

@distilled.cloud/core

bun add https://pkg.ing/@distilled.cloud/core/26852f0

@distilled.cloud/aws

bun add https://pkg.ing/@distilled.cloud/aws/26852f0

@distilled.cloud/axiom

bun add https://pkg.ing/@distilled.cloud/axiom/26852f0

@distilled.cloud/cloudflare

bun add https://pkg.ing/@distilled.cloud/cloudflare/26852f0

@distilled.cloud/neon

bun add https://pkg.ing/@distilled.cloud/neon/26852f0

@distilled.cloud/planetscale

bun add https://pkg.ing/@distilled.cloud/planetscale/26852f0

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.

1 participant