@@ -3,7 +3,7 @@ import fs from 'node:fs'
33import path from 'node:path'
44import MagicString from 'magic-string'
55import { stripLiteral } from 'strip-literal'
6- import type { Plugin } from 'vite'
6+ import type { Plugin , ResolvedConfig } from 'vite'
77import type { RscPluginManager } from '../plugin'
88import {
99 createVirtualPlugin ,
@@ -24,34 +24,31 @@ export type EnvironmentImportMeta = {
2424 specifier : string
2525}
2626
27+ // ensure at least one entry since otherwise rollup build fails
28+ export function ensureEnvironmentImportsEntryFallback ( {
29+ environments,
30+ } : ResolvedConfig ) : void {
31+ for ( const [ name , config ] of Object . entries ( environments ) ) {
32+ if ( name === 'client' ) continue
33+ const input = normalizeRollupOpitonsInput (
34+ config . build ?. rollupOptions ?. input ,
35+ )
36+ if ( Object . keys ( input ) . length === 0 ) {
37+ config . build = config . build || { }
38+ config . build . rollupOptions = config . build . rollupOptions || { }
39+ config . build . rollupOptions . input = {
40+ __vite_rsc_env_imports_entry_fallback : ENV_IMPORTS_ENTRY_FALLBACK ,
41+ }
42+ }
43+ }
44+ }
45+
2746export function vitePluginImportEnvironment (
2847 manager : RscPluginManager ,
2948) : Plugin [ ] {
3049 return [
3150 {
3251 name : 'rsc:import-environment' ,
33- configEnvironment : {
34- order : 'post' ,
35- handler ( name , config , _env ) {
36- if ( name === 'client' ) return
37- // ensure at least one entry since otherwise rollup build fails
38- const input = normalizeRollupOpitonsInput (
39- config . build ?. rollupOptions ?. input ,
40- )
41- if ( Object . keys ( input ) . length === 0 ) {
42- return {
43- build : {
44- rollupOptions : {
45- input : {
46- __vite_rsc_env_imports_entry_fallback :
47- ENV_IMPORTS_ENTRY_FALLBACK ,
48- } ,
49- } ,
50- } ,
51- }
52- }
53- } ,
54- } ,
5552 resolveId ( source ) {
5653 // Use placeholder as external, renderChunk will replace with correct relative path
5754 if ( source === ENV_IMPORTS_MANIFEST_PLACEHOLDER ) {
0 commit comments