Skip to content

Commit 7975e6d

Browse files
committed
refactor: rename option to serverEnvironmentName
1 parent 8e28d18 commit 7975e6d

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

packages/plugin-rsc/examples/browser-mode/vite.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ export default defineConfig({
1313
vitePluginRscMinimal(),
1414
vitePluginUseClient({
1515
environment: {
16-
server: ['client'],
16+
serverEnvironmentName: 'client',
1717
},
1818
}),
1919
vitePluginUseServer({
2020
environment: {
21-
server: ['client'],
21+
serverEnvironmentName: 'client',
2222
},
2323
}),
2424
vitePluginDefineEncryptionKey({
2525
environment: {
26-
server: ['client'],
26+
serverEnvironmentName: 'client',
2727
},
2828
}),
2929
{

packages/plugin-rsc/src/plugin.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ export type RscPluginOptions = {
133133
useBuildAppHook?: boolean
134134

135135
/**
136-
* This allows configuring `react-server` condition environment.
136+
* Custom environment configuration
137137
* @experimental
138-
* @default { server: ['rsc'] }
138+
* @default { serverEnvironmentName: 'rsc' }
139139
*/
140140
environment?: {
141-
server?: string[]
141+
serverEnvironmentName?: string
142142
}
143143
}
144144

@@ -974,16 +974,14 @@ export function vitePluginUseClient(
974974
// https://github.com/vitejs/vite/blob/4bcf45863b5f46aa2b41f261283d08f12d3e8675/packages/vite/src/node/utils.ts#L175
975975
const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/
976976

977-
const serverEnvironments = useClientPluginOptions.environment?.server ?? [
978-
'rsc',
979-
]
980-
const isServer = (name: string) => serverEnvironments.includes(name)
977+
const serverEnvironmentName =
978+
useClientPluginOptions.environment?.serverEnvironmentName ?? 'rsc'
981979

982980
return [
983981
{
984982
name: 'rsc:use-client',
985983
async transform(code, id) {
986-
if (!isServer(this.environment.name)) return
984+
if (this.environment.name !== serverEnvironmentName) return
987985
if (!code.includes('use client')) return
988986

989987
const ast = await parseAstAsync(code)
@@ -1106,7 +1104,7 @@ export function vitePluginUseClient(
11061104
id.startsWith('\0virtual:vite-rsc/client-in-server-package-proxy/')
11071105
) {
11081106
assert.equal(this.environment.mode, 'dev')
1109-
assert(!isServer(this.environment.name))
1107+
assert(this.environment.name !== serverEnvironmentName)
11101108
id = decodeURIComponent(
11111109
id.slice(
11121110
'\0virtual:vite-rsc/client-in-server-package-proxy/'.length,
@@ -1126,7 +1124,10 @@ export function vitePluginUseClient(
11261124
resolveId: {
11271125
order: 'pre',
11281126
async handler(source, importer, options) {
1129-
if (isServer(this.environment.name) && bareImportRE.test(source)) {
1127+
if (
1128+
this.environment.name === serverEnvironmentName &&
1129+
bareImportRE.test(source)
1130+
) {
11301131
const resolved = await this.resolve(source, importer, options)
11311132
if (resolved && resolved.id.includes('/node_modules/')) {
11321133
packageSources.set(resolved.id, source)
@@ -1151,7 +1152,7 @@ export function vitePluginUseClient(
11511152
}
11521153
},
11531154
generateBundle(_options, bundle) {
1154-
if (!isServer(this.environment.name)) return
1155+
if (this.environment.name !== serverEnvironmentName) return
11551156

11561157
// track used exports of client references in rsc build
11571158
// to tree shake unused exports in browser and ssr build
@@ -1182,16 +1183,14 @@ export function vitePluginDefineEncryptionKey(
11821183
const KEY_PLACEHOLDER = '__vite_rsc_define_encryption_key'
11831184
const KEY_FILE = '__vite_rsc_encryption_key.js'
11841185

1185-
const serverEnvironments = useServerPluginOptions.environment?.server ?? [
1186-
'rsc',
1187-
]
1188-
const isServer = (name: string) => serverEnvironments.includes(name)
1186+
const serverEnvironmentName =
1187+
useServerPluginOptions.environment?.serverEnvironmentName ?? 'rsc'
11891188

11901189
return [
11911190
{
11921191
name: 'rsc:encryption-key',
11931192
async configEnvironment(name, _config, env) {
1194-
if (isServer(name) && !env.isPreview) {
1193+
if (name === serverEnvironmentName && !env.isPreview) {
11951194
defineEncryptionKey =
11961195
useServerPluginOptions.defineEncryptionKey ??
11971196
JSON.stringify(toBase64(await generateEncryptionKey()))
@@ -1245,10 +1244,8 @@ export function vitePluginUseServer(
12451244
'ignoredPackageWarnings' | 'enableActionEncryption' | 'environment'
12461245
>,
12471246
): Plugin[] {
1248-
const serverEnvironments = useServerPluginOptions.environment?.server ?? [
1249-
'rsc',
1250-
]
1251-
const isServer = (name: string) => serverEnvironments.includes(name)
1247+
const serverEnvironmentName =
1248+
useServerPluginOptions.environment?.serverEnvironmentName ?? 'rsc'
12521249

12531250
return [
12541251
{
@@ -1278,13 +1275,12 @@ export function vitePluginUseServer(
12781275
// module identity of `import(id)` like browser, so we simply strip it off.
12791276
id = id.split('?v=')[0]!
12801277
}
1281-
// TODO
1282-
normalizedId_ = normalizeReferenceId(id, serverEnvironments[0]!)
1278+
normalizedId_ = normalizeReferenceId(id, serverEnvironmentName)
12831279
}
12841280
return normalizedId_
12851281
}
12861282

1287-
if (isServer(this.environment.name)) {
1283+
if (this.environment.name === serverEnvironmentName) {
12881284
const transformServerActionServer_ = withRollupError(
12891285
this,
12901286
transformServerActionServer,

0 commit comments

Comments
 (0)