11import { AsyncLocalStorage } from 'node:async_hooks' ;
22import { randomUUID } from 'node:crypto' ;
3- import type fs from 'node:fs' ;
43import { existsSync } from 'node:fs' ;
54import { mkdir , readFile , writeFile } from 'node:fs/promises' ;
65import { IncomingMessage } from 'node:http' ;
7- import { fileURLToPath } from 'node:url' ;
6+ import { fileURLToPath , pathToFileURL } from 'node:url' ;
87import type * as vite from 'vite' ;
9- import { normalizePath } from 'vite' ;
108import { toFallbackType } from '../core/app/common.js' ;
119import { toRoutingStrategy } from '../core/app/index.js' ;
12- import type {
13- RouteInfo ,
14- SerializedSSRManifest ,
15- SSRManifest ,
16- SSRManifestCSP ,
17- SSRManifestI18n ,
18- } from '../core/app/types.js' ;
10+ import type { SSRManifest , SSRManifestCSP , SSRManifestI18n } from '../core/app/types.js' ;
1911import {
2012 getAlgorithm ,
2113 getDirectives ,
@@ -26,18 +18,15 @@ import {
2618 getStyleResources ,
2719 shouldTrackCspHashes ,
2820} from '../core/csp/common.js' ;
29- import { warnMissingAdapter } from '../core/dev/adapter-validation.js' ;
30- import { createKey , encodeKey , getEnvironmentKey , hasEnvironmentKey } from '../core/encryption.js' ;
21+ import { createKey , getEnvironmentKey , hasEnvironmentKey } from '../core/encryption.js' ;
3122import { getViteErrorPayload } from '../core/errors/dev/index.js' ;
3223import { AstroError , AstroErrorData } from '../core/errors/index.js' ;
3324import { patchOverlay } from '../core/errors/overlay.js' ;
3425import type { Logger } from '../core/logger/core.js' ;
3526import { NOOP_MIDDLEWARE_FN } from '../core/middleware/noop-middleware.js' ;
3627import { createViteLoader } from '../core/module-loader/index.js' ;
37- import { getRoutePrerenderOption } from '../core/routing/manifest/prerender.js' ;
38- import { runHookRoutesResolved } from '../integrations/hooks.js' ;
39- import type { AstroSettings , RoutesList } from '../types/astro.js' ;
40- import { DevApp } from './app.js' ;
28+ import { viteID } from '../core/util.js' ;
29+ import type { AstroSettings } from '../types/astro.js' ;
4130import { baseMiddleware } from './base.js' ;
4231import { createController } from './controller.js' ;
4332import { recordServerError } from './error.js' ;
@@ -47,58 +36,24 @@ import { trailingSlashMiddleware } from './trailing-slash.js';
4736interface AstroPluginOptions {
4837 settings : AstroSettings ;
4938 logger : Logger ;
50- fs : typeof fs ;
5139}
5240
5341export default function createVitePluginAstroServer ( {
5442 settings,
5543 logger,
56- fs : fsMod ,
5744} : AstroPluginOptions ) : vite . Plugin {
5845 return {
5946 name : 'astro:server' ,
6047 async configureServer ( viteServer ) {
6148 const loader = createViteLoader ( viteServer ) ;
62- const { routes } = await loader . import ( 'astro:routes' ) ;
63- const routesList : RoutesList = { routes : routes . map ( ( r : RouteInfo ) => r . routeData ) } ;
64-
65- const app = await DevApp . create ( routesList , settings , logger , loader ) ;
66-
49+ // NOTE: this is temporary, we will use the configuration and proper loading later
50+ // @ts -expect-error
51+ const url = pathToFileURL ( import . meta. dirname + '/entrypoint.js' ) ;
52+ const createExports = await loader . import ( viteID ( url ) ) ;
6753 const controller = createController ( { loader } ) ;
68- const localStorage = new AsyncLocalStorage ( ) ;
54+ const { handler } = await createExports . default ( settings , controller , loader ) ;
6955
70- /** rebuild the route cache + manifest */
71- async function rebuildManifest ( path : string | null = null ) {
72- app . clearRouteCache ( ) ;
73-
74- // If a route changes, we check if it's part of the manifest and check for its prerender value
75- if ( path !== null ) {
76- const route = routesList . routes . find (
77- ( r ) =>
78- normalizePath ( path ) ===
79- normalizePath ( fileURLToPath ( new URL ( r . component , settings . config . root ) ) ) ,
80- ) ;
81- if ( ! route ) {
82- return ;
83- }
84- if ( route . type !== 'page' && route . type !== 'endpoint' ) return ;
85-
86- const routePath = fileURLToPath ( new URL ( route . component , settings . config . root ) ) ;
87- try {
88- const content = await fsMod . promises . readFile ( routePath , 'utf-8' ) ;
89- await getRoutePrerenderOption ( content , route , settings , logger ) ;
90- await runHookRoutesResolved ( { routes : routesList . routes , settings, logger } ) ;
91- } catch ( _ ) { }
92- }
93-
94- warnMissingAdapter ( logger , settings ) ;
95- app . setManifestData = routesList ;
96- }
97-
98- // Rebuild route manifest on file change
99- viteServer . watcher . on ( 'add' , rebuildManifest . bind ( null , null ) ) ;
100- viteServer . watcher . on ( 'unlink' , rebuildManifest . bind ( null , null ) ) ;
101- viteServer . watcher . on ( 'change' , rebuildManifest ) ;
56+ const localStorage = new AsyncLocalStorage ( ) ;
10257
10358 function handleUnhandledRejection ( rejection : any ) {
10459 const error = AstroError . is ( rejection )
@@ -186,11 +141,7 @@ export default function createVitePluginAstroServer({
186141 return ;
187142 }
188143 localStorage . run ( request , ( ) => {
189- app . handleRequest ( {
190- controller,
191- incomingRequest : request ,
192- incomingResponse : response ,
193- } ) ;
144+ handler ( request , response ) ;
194145 } ) ;
195146 } ) ;
196147 } ;
@@ -281,67 +232,3 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
281232 csp,
282233 } ;
283234}
284-
285- export async function createSerializedManifest (
286- settings : AstroSettings ,
287- ) : Promise < SerializedSSRManifest > {
288- let i18nManifest : SSRManifestI18n | undefined ;
289- let csp : SSRManifestCSP | undefined ;
290- if ( settings . config . i18n ) {
291- i18nManifest = {
292- fallback : settings . config . i18n . fallback ,
293- strategy : toRoutingStrategy ( settings . config . i18n . routing , settings . config . i18n . domains ) ,
294- defaultLocale : settings . config . i18n . defaultLocale ,
295- locales : settings . config . i18n . locales ,
296- domainLookupTable : { } ,
297- fallbackType : toFallbackType ( settings . config . i18n . routing ) ,
298- } ;
299- }
300-
301- if ( shouldTrackCspHashes ( settings . config . experimental . csp ) ) {
302- csp = {
303- cspDestination : settings . adapter ?. adapterFeatures ?. experimentalStaticHeaders
304- ? 'adapter'
305- : undefined ,
306- scriptHashes : getScriptHashes ( settings . config . experimental . csp ) ,
307- scriptResources : getScriptResources ( settings . config . experimental . csp ) ,
308- styleHashes : getStyleHashes ( settings . config . experimental . csp ) ,
309- styleResources : getStyleResources ( settings . config . experimental . csp ) ,
310- algorithm : getAlgorithm ( settings . config . experimental . csp ) ,
311- directives : getDirectives ( settings ) ,
312- isStrictDynamic : getStrictDynamic ( settings . config . experimental . csp ) ,
313- } ;
314- }
315-
316- return {
317- hrefRoot : settings . config . root . toString ( ) ,
318- srcDir : settings . config . srcDir ,
319- cacheDir : settings . config . cacheDir ,
320- outDir : settings . config . outDir ,
321- buildServerDir : settings . config . build . server ,
322- buildClientDir : settings . config . build . client ,
323- publicDir : settings . config . publicDir ,
324- trailingSlash : settings . config . trailingSlash ,
325- buildFormat : settings . config . build . format ,
326- compressHTML : settings . config . compressHTML ,
327- assets : [ ] ,
328- entryModules : { } ,
329- routes : [ ] ,
330- adapterName : settings ?. adapter ?. name ?? '' ,
331- clientDirectives : Array . from ( settings . clientDirectives . entries ( ) ) ,
332- renderers : [ ] ,
333- base : settings . config . base ,
334- userAssetsBase : settings . config ?. vite ?. base ,
335- assetsPrefix : settings . config . build . assetsPrefix ,
336- site : settings . config . site ,
337- componentMetadata : [ ] ,
338- inlinedScripts : [ ] ,
339- i18n : i18nManifest ,
340- checkOrigin :
341- ( settings . config . security ?. checkOrigin && settings . buildOutput === 'server' ) ?? false ,
342- key : await encodeKey ( hasEnvironmentKey ( ) ? await getEnvironmentKey ( ) : await createKey ( ) ) ,
343- sessionConfig : settings . config . session ,
344- csp,
345- serverIslandNameMap : [ ] ,
346- } ;
347- }
0 commit comments