|
1 | 1 | import { defineConfig, type Plugin } from 'vite' |
2 | 2 | import react from '@vitejs/plugin-react' |
3 | 3 | import rsc from '@vitejs/plugin-rsc' |
4 | | -import fsp from 'node:fs/promises' |
5 | 4 |
|
6 | 5 | export default defineConfig({ |
| 6 | + appType: 'spa', |
7 | 7 | plugins: [ |
8 | | - spaPlugin(), |
9 | 8 | react(), |
10 | 9 | rsc({ |
| 10 | + serverHandler: false, |
11 | 11 | entries: { |
12 | 12 | rsc: './src/framework/entry.rsc.tsx', |
13 | 13 | }, |
14 | 14 | }), |
15 | 15 | rscBrowserMode2Plugin(), |
| 16 | + { |
| 17 | + name: 'patch-rsc', |
| 18 | + configResolved(config) { |
| 19 | + const plugin = config.plugins.find( |
| 20 | + (p) => p.name === 'rsc:inject-async-local-storage', |
| 21 | + ) |
| 22 | + delete plugin!.transform |
| 23 | + }, |
| 24 | + }, |
16 | 25 | ], |
17 | | - define: { |
18 | | - 'process.env.NODE_ENV': JSON.stringify('development'), |
19 | | - }, |
20 | 26 | environments: { |
21 | 27 | rsc: { |
| 28 | + keepProcessEnv: false, |
22 | 29 | resolve: { |
23 | 30 | noExternal: true, |
24 | | - alias: { |
25 | | - 'node:async_hooks': '/src/framework/async-hooks-polyfill.js', |
26 | | - }, |
27 | 31 | }, |
28 | 32 | 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 | 33 | esbuildOptions: { |
39 | | - platform: 'browser', |
| 34 | + platform: 'neutral', |
40 | 35 | }, |
41 | 36 | }, |
42 | 37 | }, |
43 | 38 | }, |
44 | 39 | }) |
45 | 40 |
|
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 | | - |
95 | 41 | function rscBrowserMode2Plugin(): Plugin[] { |
96 | 42 | return [ |
97 | 43 | { |
|
0 commit comments