@@ -4,11 +4,12 @@ import MagicString from 'magic-string'
44import { stripLiteral } from 'strip-literal'
55import type { Plugin } from 'vite'
66import type { RscPluginManager } from '../plugin'
7+ import { createVirtualPlugin , normalizeRollupOpitonsInput } from './utils'
78import { evalValue } from './vite-utils'
89
910export const ENV_IMPORTS_MANIFEST_NAME = '__vite_rsc_env_imports_manifest.js'
1011export const ENV_IMPORTS_ENTRY_FALLBACK =
11- 'virtual:vite-rsc/env-imports-entry-fallbacks '
12+ 'virtual:vite-rsc/env-imports-entry-fallback '
1213
1314export 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
0 commit comments