diff --git a/packages/plugin-rsc/README.md b/packages/plugin-rsc/README.md index a0574c065..cb5bcb6e1 100644 --- a/packages/plugin-rsc/README.md +++ b/packages/plugin-rsc/README.md @@ -355,6 +355,8 @@ import.meta.hot.on('rsc:update', async () => { ### `@vitejs/plugin-rsc` +- Type: `rsc: (options?: RscPluginOptions) => Plugin[]`; + ```js import rsc from '@vitejs/plugin-rsc' import { defineConfig } from 'vite' @@ -390,8 +392,15 @@ export default defineConfig({ // for example, to obtain a key through environment variable during runtime. // cf. https://nextjs.org/docs/app/guides/data-security#overwriting-encryption-keys-advanced defineEncryptionKey: 'process.env.MY_ENCRYPTION_KEY', + + // see `RscPluginOptions` for full options ... }), ], + // the same options can be also specified via top-level `rsc` property. + // this allows other plugin to set options via `config` hook. + rsc: { + // ... + }, }) ``` diff --git a/packages/plugin-rsc/examples/ssg/vite.config.ts b/packages/plugin-rsc/examples/ssg/vite.config.ts index 7c0fb059a..f8d92f9f9 100644 --- a/packages/plugin-rsc/examples/ssg/vite.config.ts +++ b/packages/plugin-rsc/examples/ssg/vite.config.ts @@ -9,7 +9,7 @@ import { type Plugin, type ResolvedConfig, defineConfig } from 'vite' // import inspect from 'vite-plugin-inspect' import { RSC_POSTFIX } from './src/framework/shared' -export default defineConfig((env) => ({ +export default defineConfig({ plugins: [ // inspect(), mdx(), @@ -20,23 +20,26 @@ export default defineConfig((env) => ({ rsc: './src/framework/entry.rsc.tsx', ssr: './src/framework/entry.ssr.tsx', }, - serverHandler: env.isPreview ? false : undefined, - useBuildAppHook: true, }), rscSsgPlugin(), ], -})) +}) function rscSsgPlugin(): Plugin[] { return [ { name: 'rsc-ssg', - config(_config, env) { - if (env.isPreview) { + config: { + order: 'pre', + handler(_config, env) { return { - appType: 'mpa', + appType: env.isPreview ? 'mpa' : undefined, + rsc: { + useBuildAppHook: true, + serverHandler: env.isPreview ? false : undefined, + }, } - } + }, }, buildApp: { async handler(builder) { diff --git a/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts b/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts index afb3648a8..0b9bf89b9 100644 --- a/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts +++ b/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts @@ -50,9 +50,4 @@ export default defineConfig({ }, }, }, - builder: { - // empty buildApp to disable cloudflare's buildApp - // https://github.com/cloudflare/workers-sdk/blob/19e2aab1d68594c7289d0aa16474544919fd5b9b/packages/vite-plugin-cloudflare/src/index.ts#L183-L186 - buildApp: async () => {}, - }, }) diff --git a/packages/plugin-rsc/src/plugin.ts b/packages/plugin-rsc/src/plugin.ts index d00db1adb..d129c1c4b 100644 --- a/packages/plugin-rsc/src/plugin.ts +++ b/packages/plugin-rsc/src/plugin.ts @@ -309,6 +309,14 @@ export default function vitePluginRsc( { name: 'rsc', async config(config, env) { + if (config.rsc) { + // mutate `rscPluginOptions` since internally this object is passed around + Object.assign( + rscPluginOptions, + // not sure which should win. for now plugin constructor wins. + vite.mergeConfig(config.rsc, rscPluginOptions), + ) + } // crawl packages with "react" in "peerDependencies" to bundle react deps on server // see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101 const result = await crawlFrameworkPkgs({ @@ -332,7 +340,7 @@ export default function vitePluginRsc( ] return { - appType: 'custom', + appType: config.appType ?? 'custom', define: { 'import.meta.env.__vite_rsc_build__': JSON.stringify( env.command === 'build', @@ -412,11 +420,21 @@ export default function vitePluginRsc( builder: { sharedPlugins: true, sharedConfigBuild: true, - buildApp: rscPluginOptions.useBuildAppHook ? undefined : buildApp, + async buildApp(builder) { + if (!rscPluginOptions.useBuildAppHook) { + await buildApp(builder) + } + }, }, } }, - buildApp: rscPluginOptions.useBuildAppHook ? buildApp : undefined, + buildApp: { + async handler(builder) { + if (rscPluginOptions.useBuildAppHook) { + await buildApp(builder) + } + }, + }, configureServer(server) { ;(globalThis as any).__viteRscDevServer = server @@ -989,6 +1007,7 @@ import.meta.hot.on("rsc:update", () => { ...vitePluginRscMinimal(rscPluginOptions, manager), ...vitePluginFindSourceMapURL(), ...vitePluginRscCss(rscPluginOptions, manager), + // TODO: delay validateImports option check after config ...(rscPluginOptions.validateImports !== false ? [validateImportPlugin()] : []), diff --git a/packages/plugin-rsc/types/index.d.ts b/packages/plugin-rsc/types/index.d.ts index dfd4de56b..ea900dcc0 100644 --- a/packages/plugin-rsc/types/index.d.ts +++ b/packages/plugin-rsc/types/index.d.ts @@ -16,4 +16,11 @@ declare global { } } +declare module 'vite' { + interface UserConfig { + /** Options for `@vitejs/plugin-rsc` */ + rsc?: import('@vitejs/plugin-rsc').RscPluginOptions + } +} + export {}