11import { defineConfig , type Plugin } from 'vite'
22import react from '@vitejs/plugin-react'
33import rsc from '@vitejs/plugin-rsc'
4+ import fsp from 'node:fs/promises'
45
56export default defineConfig ( {
67 plugins : [
8+ spaPlugin ( ) ,
79 react ( ) ,
810 rsc ( {
911 entries : {
@@ -12,8 +14,84 @@ export default defineConfig({
1214 } ) ,
1315 rscBrowserMode2Plugin ( ) ,
1416 ] ,
17+ define : {
18+ 'process.env.NODE_ENV' : JSON . stringify ( 'development' ) ,
19+ } ,
20+ environments : {
21+ rsc : {
22+ resolve : {
23+ noExternal : true ,
24+ alias : {
25+ 'node:async_hooks' : '/src/framework/async-hooks-polyfill.js' ,
26+ } ,
27+ } ,
28+ optimizeDeps : {
29+ include : [
30+ 'react' ,
31+ 'react-dom' ,
32+ 'react/jsx-runtime' ,
33+ 'react/jsx-dev-runtime' ,
34+ '@vitejs/plugin-rsc/vendor/react-server-dom/server.edge' ,
35+ '@vitejs/plugin-rsc/vendor/react-server-dom/client.edge' ,
36+ ] ,
37+ exclude : [ '@vitejs/plugin-rsc' ] ,
38+ esbuildOptions : {
39+ platform : 'browser' ,
40+ } ,
41+ } ,
42+ } ,
43+ } ,
1544} )
1645
46+ function spaPlugin ( ) : Plugin [ ] {
47+ // serve index.html before rsc server
48+ return [
49+ {
50+ name : 'serve-spa' ,
51+ configureServer ( server ) {
52+ return ( ) => {
53+ server . middlewares . use ( async ( req , res , next ) => {
54+ try {
55+ if ( req . headers . accept ?. includes ( 'text/html' ) ) {
56+ const html = await fsp . readFile ( 'index.html' , 'utf-8' )
57+ const transformed = await server . transformIndexHtml ( '/' , html )
58+ res . setHeader ( 'Content-type' , 'text/html' )
59+ res . setHeader ( 'Vary' , 'accept' )
60+ res . end ( transformed )
61+ return
62+ }
63+ } catch ( error ) {
64+ next ( error )
65+ return
66+ }
67+ next ( )
68+ } )
69+ }
70+ } ,
71+ configurePreviewServer ( server ) {
72+ return ( ) => {
73+ server . middlewares . use ( async ( req , res , next ) => {
74+ try {
75+ if ( req . headers . accept ?. includes ( 'text/html' ) ) {
76+ const html = await fsp . readFile (
77+ 'dist/client/index.html' ,
78+ 'utf-8' ,
79+ )
80+ res . end ( html )
81+ return
82+ }
83+ } catch ( error ) {
84+ next ( error )
85+ return
86+ }
87+ next ( )
88+ } )
89+ }
90+ } ,
91+ } ,
92+ ]
93+ }
94+
1795function rscBrowserMode2Plugin ( ) : Plugin [ ] {
1896 return [
1997 {
0 commit comments