Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/plugin-rsc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
// ...
},
})
```

Expand Down
19 changes: 11 additions & 8 deletions packages/plugin-rsc/examples/ssg/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions packages/plugin-rsc/examples/starter-cf-single/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {},
},
})
25 changes: 22 additions & 3 deletions packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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',
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()]
: []),
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-rsc/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ declare global {
}
}

declare module 'vite' {
interface UserConfig {
/** Options for `@vitejs/plugin-rsc` */
rsc?: import('@vitejs/plugin-rsc').RscPluginOptions
}
}

export {}
Loading