Skip to content

Commit de75327

Browse files
committed
refactor: wip
1 parent a1332f9 commit de75327

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class RscPluginManager {
126126
serverReferenceMetaMap: Record<string, ServerRerferenceMeta> = {}
127127
serverResourcesMetaMap: Record<string, { key: string }> = {}
128128
environmentImportMetaMap: Record<string, EnvironmentImportMeta> = {}
129+
// TODO: per-environment (can we merge to environmentImportMetaMap?)
129130
// Maps resolvedId to output fileName (populated in generateBundle)
130131
environmentImportOutputMap: Record<string, string> = {}
131132

packages/plugin-rsc/src/plugins/import-environment.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import MagicString from 'magic-string'
44
import { stripLiteral } from 'strip-literal'
55
import type { Plugin } from 'vite'
66
import type { RscPluginManager } from '../plugin'
7+
import { createVirtualPlugin, normalizeRollupOpitonsInput } from './utils'
78
import { evalValue } from './vite-utils'
89

910
export const ENV_IMPORTS_MANIFEST_NAME = '__vite_rsc_env_imports_manifest.js'
1011
export const ENV_IMPORTS_ENTRY_FALLBACK =
11-
'virtual:vite-rsc/env-imports-entry-fallbacks'
12+
'virtual:vite-rsc/env-imports-entry-fallback'
1213

1314
export type EnvironmentImportMeta = {
1415
resolvedId: string
@@ -27,19 +28,21 @@ export function vitePluginImportEnvironment(
2728
configEnvironment: {
2829
order: 'post',
2930
handler(name, config, _env) {
30-
if (name === 'ssr' || name === 'rsc') {
31-
// ensure at least one entry since otherwise rollup build fails
32-
if (!config.build?.rollupOptions?.input) {
33-
return {
34-
build: {
35-
rollupOptions: {
36-
input: {
37-
__vite_rsc_env_imports_entry_fallback:
38-
ENV_IMPORTS_ENTRY_FALLBACK,
39-
},
31+
if (name === 'client') return
32+
// ensure at least one entry since otherwise rollup build fails
33+
const input = normalizeRollupOpitonsInput(
34+
config.build?.rollupOptions?.input,
35+
)
36+
if (Object.keys(input).length === 0) {
37+
return {
38+
build: {
39+
rollupOptions: {
40+
input: {
41+
__vite_rsc_env_imports_entry_fallback:
42+
ENV_IMPORTS_ENTRY_FALLBACK,
4043
},
4144
},
42-
}
45+
},
4346
}
4447
}
4548
},
@@ -54,16 +57,6 @@ export function vitePluginImportEnvironment(
5457
// TODO: relativity should be enforced via another renderChunk patch
5558
return { id: './' + ENV_IMPORTS_MANIFEST_NAME, external: true }
5659
}
57-
if (source === ENV_IMPORTS_ENTRY_FALLBACK) {
58-
return '\0' + ENV_IMPORTS_ENTRY_FALLBACK
59-
}
60-
},
61-
load(id) {
62-
// TODO: how to avoid warning?
63-
// > Generated an empty chunk: "__vite_rsc_env_imports_entry_fallback".
64-
if (id === '\0' + ENV_IMPORTS_ENTRY_FALLBACK) {
65-
return 'export default "__vite_rsc_env_imports_entry_fallback"'
66-
}
6760
},
6861
buildStart() {
6962
// Emit discovered entries during build
@@ -138,6 +131,7 @@ export function vitePluginImportEnvironment(
138131

139132
// Track discovered entry
140133
manager.environmentImportMetaMap[resolvedId] = {
134+
// TODO: relative-ize resolveId
141135
resolvedId,
142136
targetEnv: environmentName,
143137
sourceEnv: this.environment.name,
@@ -168,6 +162,8 @@ export function vitePluginImportEnvironment(
168162
},
169163

170164
generateBundle(_options, bundle) {
165+
if (this.environment.name === 'client') return
166+
171167
// Track output filenames for discovered environment imports
172168
// This runs in both RSC and SSR builds to capture all outputs
173169
for (const [fileName, chunk] of Object.entries(bundle)) {
@@ -180,6 +176,11 @@ export function vitePluginImportEnvironment(
180176
}
181177
},
182178
},
179+
createVirtualPlugin(ENV_IMPORTS_ENTRY_FALLBACK, () => {
180+
// TODO: how to avoid warning?
181+
// > Generated an empty chunk: "__vite_rsc_env_imports_entry_fallback".
182+
return `export default console.log("__vite_rsc_env_imports_entry_fallback");`
183+
}),
183184
]
184185
}
185186

packages/plugin-rsc/src/plugins/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function getFallbackRollupEntry(
100100
// normalize to object form
101101
// https://rollupjs.org/configuration-options/#input
102102
// https://rollupjs.org/configuration-options/#output-entryfilenames
103-
function normalizeRollupOpitonsInput(
103+
export function normalizeRollupOpitonsInput(
104104
input: Rollup.InputOptions['input'] = {},
105105
): Record<string, string> {
106106
if (typeof input === 'string') {

0 commit comments

Comments
 (0)