Support Cloudflare Vite plugin v2 - #18119
jamesopstad wants to merge 11 commits into
Conversation
🦋 Changeset detectedLatest commit: e8b4c3f The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 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 |
3f1e57c to
c6357ef
Compare
| 'astro': patch | ||
| --- | ||
|
|
||
| Defers Vite manifest cleanup until all `buildApp` hooks have completed, allowing platform plugins to consume the manifests. |
There was a problem hiding this comment.
Unless this change fixes some user-facing problem, we don't need it. If so, it should be reworded. Here's a small guide https://contribute.docs.astro.build/docs-for-code-changes/changesets/#patch-updates
There was a problem hiding this comment.
Here's a guide to write breaking changes https://contribute.docs.astro.build/docs-for-code-changes/changesets/#breaking-changes
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Astro now treats Vite's resolved `outDir` values as the source of truth for build output. Generated assets, build hook `dir` values, and SSR manifest paths use the resolved client, server, and prerender directories. Custom prerenderer factories also receive these directories through a new context argument. |
There was a problem hiding this comment.
I would rewrite this changeset from the user point of view.
| const flags = compatibilityFlags ?? []; | ||
| if (flags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) { | ||
| return flags; | ||
| } | ||
| return [...flags, 'nodejs_als']; |
There was a problem hiding this comment.
| const flags = compatibilityFlags ?? []; | |
| if (flags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) { | |
| return flags; | |
| } | |
| return [...flags, 'nodejs_als']; | |
| if (!compatibilityFlags) { | |
| return ['nodejs_als'] | |
| } | |
| if (compatibilityFlags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) { | |
| return compatibilityFlags; | |
| } | |
| return compatibilityFlags.push('nodejs_als') |
Since in the if we return the original array, there's no need for the spread
There was a problem hiding this comment.
This code hasn't changed. It's only in the diff because the filename has changed.
| if (cloudflareOptions.configPath) { | ||
| addWatchFile(new URL(cloudflareOptions.configPath, config.root)); | ||
| } | ||
|
|
||
| addWatchFile(new URL('./wrangler.toml', config.root)); | ||
| addWatchFile(new URL('./wrangler.json', config.root)); | ||
| addWatchFile(new URL('./wrangler.jsonc', config.root)); |
There was a problem hiding this comment.
Don't we need to watch the new configuration file?
There was a problem hiding this comment.
Hmm, that depends on how Astro restarts interact with Vite restarts. We restart the Vite dev server when the config file or any of its dependencies change. Watching it here would be tricky because you don't know its dependencies.
There was a problem hiding this comment.
I would check to make sure restarting the dev server in Astro works that same as in vite dev, it might not.
There was a problem hiding this comment.
I got an agent to investigate this. Here is its reply:
The Cloudflare plugin already watches cloudflare.config.ts and its dependencies, so watching only the top-level file here would be incomplete and would cause both Astro and Cloudflare to initiate restarts. However, Matthew’s concern uncovered an Astro lifecycle issue: Vite replaces its watcher during a native restart, and Astro’s externally registered restart/content listeners are not reattached after a plugin-initiated restart. We should fix that listener lifecycle and add an astro dev regression test rather than restore addWatchFile() here.
| // Assign the Wrangler config's effective env (`vars` merged with | ||
| // `.dev.vars`/`.env` overrides) to process.env so astro:env can find | ||
| // these variables at build time. | ||
| loadWranglerEnv(config.root, cloudflareOptions.configPath, logger); |
There was a problem hiding this comment.
| "peerDependencies": { | ||
| "astro": "^7.2.0", | ||
| "wrangler": "^4.125.0" | ||
| "astro": "^7.4.0" |
There was a problem hiding this comment.
Was this change intentional? We usually don't bump it unless we need a specific API from astro
There was a problem hiding this comment.
This PR relies on #18112, which makes changes to Astro core, so I assumed that would require the bump?
There was a problem hiding this comment.
Oh I see. Makes sense then. So I suggest #18112 to main
| }) => { | ||
| const wranglerConfigPath = resolvePath(fileURLToPath(root), '.wrangler/deploy/config.json'); | ||
| if (!existsSync(wranglerConfigPath)) { | ||
| const buildOutputConfigPath = new URL('./.cloudflare/output/v0/config.json', root); |
There was a problem hiding this comment.
Is this v0 something that could change? and if so, is it under semver?
There was a problem hiding this comment.
It will change, but only in the beta period. We'll try to keep changes as non-breaking as possible. It will change to v1 after the beta.
|
|
||
| export function pluginPrerender(_opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { | ||
| export function pluginPrerender( | ||
| _opts: StaticBuildOptionsInput, |
There was a problem hiding this comment.
Remove it if it's not needed anymore
There was a problem hiding this comment.
Sure. I presumed it must be there for a reason.
Depends on #18112.
Until that PR lands, the focused diff is:
jamesopstad/astro@resolved-vite-output-dirs...support-cloudflare-vite-plugin-v2
Changes
Updates the adapter to use v2 of
@cloudflare/vite-pluginand support deployment withcf.Projects should replace Wrangler configuration with
cloudflare.config.ts, importing configuration utilities fromcf/config.cfshould also be used in place of Wrangler for deployment.The adapter’s
configPathoption has been removed. Configuration is always loaded from acloudflare.config.tsfile in the project root.wrangleris no longer a peer dependency of@astrojs/cloudflare.Testing
Tests updated as appropriate.
Docs
This will need a migration guide in the docs.