Skip to content

Commit 07a64c2

Browse files
authored
feat(rsc): support UserConfig.rsc: RscPluginOptions (#810)
1 parent 6e4dfa7 commit 07a64c2

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

packages/plugin-rsc/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ import.meta.hot.on('rsc:update', async () => {
355355

356356
### `@vitejs/plugin-rsc`
357357

358+
- Type: `rsc: (options?: RscPluginOptions) => Plugin[]`;
359+
358360
```js
359361
import rsc from '@vitejs/plugin-rsc'
360362
import { defineConfig } from 'vite'
@@ -390,8 +392,15 @@ export default defineConfig({
390392
// for example, to obtain a key through environment variable during runtime.
391393
// cf. https://nextjs.org/docs/app/guides/data-security#overwriting-encryption-keys-advanced
392394
defineEncryptionKey: 'process.env.MY_ENCRYPTION_KEY',
395+
396+
// see `RscPluginOptions` for full options ...
393397
}),
394398
],
399+
// the same options can be also specified via top-level `rsc` property.
400+
// this allows other plugin to set options via `config` hook.
401+
rsc: {
402+
// ...
403+
},
395404
})
396405
```
397406

packages/plugin-rsc/examples/ssg/vite.config.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { type Plugin, type ResolvedConfig, defineConfig } from 'vite'
99
// import inspect from 'vite-plugin-inspect'
1010
import { RSC_POSTFIX } from './src/framework/shared'
1111

12-
export default defineConfig((env) => ({
12+
export default defineConfig({
1313
plugins: [
1414
// inspect(),
1515
mdx(),
@@ -20,23 +20,26 @@ export default defineConfig((env) => ({
2020
rsc: './src/framework/entry.rsc.tsx',
2121
ssr: './src/framework/entry.ssr.tsx',
2222
},
23-
serverHandler: env.isPreview ? false : undefined,
24-
useBuildAppHook: true,
2523
}),
2624
rscSsgPlugin(),
2725
],
28-
}))
26+
})
2927

3028
function rscSsgPlugin(): Plugin[] {
3129
return [
3230
{
3331
name: 'rsc-ssg',
34-
config(_config, env) {
35-
if (env.isPreview) {
32+
config: {
33+
order: 'pre',
34+
handler(_config, env) {
3635
return {
37-
appType: 'mpa',
36+
appType: env.isPreview ? 'mpa' : undefined,
37+
rsc: {
38+
useBuildAppHook: true,
39+
serverHandler: env.isPreview ? false : undefined,
40+
},
3841
}
39-
}
42+
},
4043
},
4144
buildApp: {
4245
async handler(builder) {

packages/plugin-rsc/examples/starter-cf-single/vite.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,4 @@ export default defineConfig({
5050
},
5151
},
5252
},
53-
builder: {
54-
// empty buildApp to disable cloudflare's buildApp
55-
// https://github.com/cloudflare/workers-sdk/blob/19e2aab1d68594c7289d0aa16474544919fd5b9b/packages/vite-plugin-cloudflare/src/index.ts#L183-L186
56-
buildApp: async () => {},
57-
},
5853
})

packages/plugin-rsc/src/plugin.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ export default function vitePluginRsc(
309309
{
310310
name: 'rsc',
311311
async config(config, env) {
312+
if (config.rsc) {
313+
// mutate `rscPluginOptions` since internally this object is passed around
314+
Object.assign(
315+
rscPluginOptions,
316+
// not sure which should win. for now plugin constructor wins.
317+
vite.mergeConfig(config.rsc, rscPluginOptions),
318+
)
319+
}
312320
// crawl packages with "react" in "peerDependencies" to bundle react deps on server
313321
// see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101
314322
const result = await crawlFrameworkPkgs({
@@ -332,7 +340,7 @@ export default function vitePluginRsc(
332340
]
333341

334342
return {
335-
appType: 'custom',
343+
appType: config.appType ?? 'custom',
336344
define: {
337345
'import.meta.env.__vite_rsc_build__': JSON.stringify(
338346
env.command === 'build',
@@ -412,11 +420,21 @@ export default function vitePluginRsc(
412420
builder: {
413421
sharedPlugins: true,
414422
sharedConfigBuild: true,
415-
buildApp: rscPluginOptions.useBuildAppHook ? undefined : buildApp,
423+
async buildApp(builder) {
424+
if (!rscPluginOptions.useBuildAppHook) {
425+
await buildApp(builder)
426+
}
427+
},
416428
},
417429
}
418430
},
419-
buildApp: rscPluginOptions.useBuildAppHook ? buildApp : undefined,
431+
buildApp: {
432+
async handler(builder) {
433+
if (rscPluginOptions.useBuildAppHook) {
434+
await buildApp(builder)
435+
}
436+
},
437+
},
420438
configureServer(server) {
421439
;(globalThis as any).__viteRscDevServer = server
422440

@@ -989,6 +1007,7 @@ import.meta.hot.on("rsc:update", () => {
9891007
...vitePluginRscMinimal(rscPluginOptions, manager),
9901008
...vitePluginFindSourceMapURL(),
9911009
...vitePluginRscCss(rscPluginOptions, manager),
1010+
// TODO: delay validateImports option check after config
9921011
...(rscPluginOptions.validateImports !== false
9931012
? [validateImportPlugin()]
9941013
: []),

packages/plugin-rsc/types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ declare global {
1616
}
1717
}
1818

19+
declare module 'vite' {
20+
interface UserConfig {
21+
/** Options for `@vitejs/plugin-rsc` */
22+
rsc?: import('@vitejs/plugin-rsc').RscPluginOptions
23+
}
24+
}
25+
1926
export {}

0 commit comments

Comments
 (0)