@@ -17,7 +17,6 @@ import type {
1717 IntegrationResolvedRoute ,
1818} from 'astro' ;
1919import type { PluginOption } from 'vite' ;
20- import { defaultClientConditions } from 'vite' ;
2120import { cloudflareModuleLoader } from './utils/cloudflare-module-loader.js' ;
2221import { createRoutesFile , getParts } from './utils/generate-routes-json.js' ;
2322import { type ImageService , setImageConfig } from './utils/image-config.js' ;
@@ -211,20 +210,13 @@ export default function createIntegration(args?: Options): AstroIntegration {
211210 } ,
212211 session,
213212 vite : {
214- ssr : {
215- optimizeDeps : {
216- // Disabled to prevent "prebundle" errors on first dev
217- // This can be removed when the issue is resolved with Cloudflare
218- noDiscovery : true ,
219- } ,
220- } ,
221213 plugins : [
222214 cfVitePlugin ( cfPluginConfig ) ,
223215 // https://developers.cloudflare.com/pages/functions/module-support/
224216 // Allows imports of '.wasm', '.bin', and '.txt' file types
225217 cloudflareModulePlugin ,
226218 {
227- name : 'vite :cf-imports' ,
219+ name : '@astrojs/cloudflare :cf-imports' ,
228220 enforce : 'pre' ,
229221 resolveId ( source ) {
230222 if ( source . startsWith ( 'cloudflare:' ) ) {
@@ -233,9 +225,21 @@ export default function createIntegration(args?: Options): AstroIntegration {
233225 return null ;
234226 } ,
235227 } ,
228+ {
229+ name : '@astrojs/cloudflare:environment' ,
230+ configEnvironment ( environmentName , _options ) {
231+ if ( environmentName === 'ssr' && _options . optimizeDeps ?. noDiscovery === false ) {
232+ return {
233+ optimizeDeps : {
234+ exclude : [ 'unstorage/drivers/cloudflare-kv-binding' ] ,
235+ } ,
236+ } ;
237+ }
238+ } ,
239+ } ,
236240 {
237241 enforce : 'post' ,
238- name : 'vite :cf-externals' ,
242+ name : '@astrojs/cloudflare :cf-externals' ,
239243 applyToEnvironment : ( environment ) => environment . name === 'ssr' ,
240244 config ( conf ) {
241245 if ( conf . ssr ) {
@@ -256,7 +260,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
256260 addWatchFile ( new URL ( './wrangler.toml' , config . root ) ) ;
257261 addWatchFile ( new URL ( './wrangler.json' , config . root ) ) ;
258262 addWatchFile ( new URL ( './wrangler.jsonc' , config . root ) ) ;
259-
260263 } ,
261264 'astro:routes:resolved' : ( { routes } ) => {
262265 _routes = routes ;
@@ -314,49 +317,28 @@ export default function createIntegration(args?: Options): AstroIntegration {
314317 const parsed = parse ( data ) ;
315318 Object . assign ( process . env , parsed ) ;
316319 } catch {
317- logger . error ( `Unable to parse .dev.vars, variables will not be available to your application.` ) ;
320+ logger . error (
321+ `Unable to parse .dev.vars, variables will not be available to your application.` ,
322+ ) ;
318323 }
319324 }
320325 } ,
321326 'astro:build:setup' : ( { vite, target } ) => {
322327 if ( target === 'server' ) {
323328 vite . resolve ||= { } ;
324329 vite . resolve . alias ||= { } ;
325-
326- const aliases = [
327- {
328- find : 'react-dom/server' ,
329- replacement : 'react-dom/server.browser' ,
330- } ,
331- ] ;
332-
333- if ( Array . isArray ( vite . resolve . alias ) ) {
334- vite . resolve . alias = [ ...vite . resolve . alias , ...aliases ] ;
335- } else {
336- for ( const alias of aliases ) {
337- ( vite . resolve . alias as Record < string , string > ) [ alias . find ] = alias . replacement ;
338- }
339- }
340-
341- // Support `workerd` and `worker` conditions for the ssr environment
342- // (previously supported in esbuild instead: https://github.com/withastro/astro/pull/7092)
343330 vite . ssr ||= { } ;
344- vite . ssr . resolve ||= { } ;
345- vite . ssr . resolve . conditions ||= [ ...defaultClientConditions ] ;
346- vite . ssr . resolve . conditions . push ( 'workerd' , 'worker' ) ;
347-
348- vite . ssr . target = 'webworker' ;
349331 vite . ssr . noExternal = true ;
350332
351333 vite . build ||= { } ;
352334 vite . build . rollupOptions ||= { } ;
353335 vite . build . rollupOptions . output ||= { } ;
336+ vite . build . rollupOptions . external = [ 'sharp' ] ;
337+
354338 // @ts -expect-error
355339 vite . build . rollupOptions . output . banner ||=
356340 'globalThis.process ??= {}; globalThis.process.env ??= {};' ;
357341
358- vite . build . rollupOptions . external = [ 'sharp' ] ;
359-
360342 // Cloudflare env is only available per request. This isn't feasible for code that access env vars
361343 // in a global way, so we shim their access as `process.env.*`. This is not the recommended way for users to access environment variables. But we'll add this for compatibility for chosen variables. Mainly to support `@astrojs/db`
362344 vite . define = {
0 commit comments