|
| 1 | +import { rmSync } from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { normalizePath, type Plugin } from 'vite' |
| 4 | + |
| 5 | +export default function vitePluginRscBrowser(): Plugin[] { |
| 6 | + return [ |
| 7 | + { |
| 8 | + name: 'rsc-browser', |
| 9 | + config() { |
| 10 | + return { |
| 11 | + appType: 'spa', |
| 12 | + environments: { |
| 13 | + client: { |
| 14 | + build: { |
| 15 | + emptyOutDir: false, |
| 16 | + }, |
| 17 | + }, |
| 18 | + // TODO: server build is not hashed |
| 19 | + rsc: { |
| 20 | + build: { |
| 21 | + outDir: 'dist/client/__server', |
| 22 | + }, |
| 23 | + keepProcessEnv: false, |
| 24 | + resolve: { |
| 25 | + noExternal: true, |
| 26 | + }, |
| 27 | + optimizeDeps: { |
| 28 | + esbuildOptions: { |
| 29 | + platform: 'neutral', |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + rsc: { |
| 35 | + serverHandler: false, |
| 36 | + }, |
| 37 | + } |
| 38 | + }, |
| 39 | + configResolved(config) { |
| 40 | + // avoid globalThis.AsyncLocalStorage injection in browser mode |
| 41 | + const plugin = config.plugins.find( |
| 42 | + (p) => p.name === 'rsc:inject-async-local-storage', |
| 43 | + ) |
| 44 | + delete plugin!.transform |
| 45 | + }, |
| 46 | + buildApp: { |
| 47 | + order: 'pre', |
| 48 | + async handler() { |
| 49 | + // clean up nested outDir |
| 50 | + rmSync('./dist', { recursive: true, force: true }) |
| 51 | + }, |
| 52 | + }, |
| 53 | + configureServer(server) { |
| 54 | + server.middlewares.use(async (req, res, next) => { |
| 55 | + const url = new URL(req.url ?? '/', 'https://any.local') |
| 56 | + if (url.pathname === '/@vite/invoke-rsc') { |
| 57 | + const payload = JSON.parse(url.searchParams.get('data')!) |
| 58 | + const result = |
| 59 | + await server.environments['rsc']!.hot.handleInvoke(payload) |
| 60 | + res.setHeader('Content-Type', 'application/json') |
| 61 | + res.end(JSON.stringify(result)) |
| 62 | + return |
| 63 | + } |
| 64 | + next() |
| 65 | + }) |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'rsc-browser:load-rsc', |
| 70 | + resolveId(source) { |
| 71 | + if (source === 'virtual:vite-rsc-browser/load-rsc') { |
| 72 | + if (this.environment.mode === 'dev') { |
| 73 | + return this.resolve('/lib/dev-proxy') |
| 74 | + } |
| 75 | + return { id: source, external: true } |
| 76 | + } |
| 77 | + }, |
| 78 | + renderChunk(code, chunk) { |
| 79 | + if (code.includes('virtual:vite-rsc-browser/load-rsc')) { |
| 80 | + const config = this.environment.getTopLevelConfig() |
| 81 | + const replacement = normalizeRelativePath( |
| 82 | + path.relative( |
| 83 | + path.join( |
| 84 | + config.environments.client.build.outDir, |
| 85 | + chunk.fileName, |
| 86 | + '..', |
| 87 | + ), |
| 88 | + path.join(config.environments.rsc.build.outDir, 'index.js'), |
| 89 | + ), |
| 90 | + ) |
| 91 | + code = code.replaceAll( |
| 92 | + 'virtual:vite-rsc-browser/load-rsc', |
| 93 | + () => replacement, |
| 94 | + ) |
| 95 | + return { code } |
| 96 | + } |
| 97 | + }, |
| 98 | + }, |
| 99 | + ] |
| 100 | +} |
| 101 | + |
| 102 | +function normalizeRelativePath(s: string): string { |
| 103 | + s = normalizePath(s) |
| 104 | + return s[0] === '.' ? s : './' + s |
| 105 | +} |
0 commit comments