import { ConfigEnv , Plugin , UserConfig , ViteDevServer } from 'vite' ; import virtual from '@rollup/plugin-virtual' ; import { prodRemotePlugin } from './prod/remote-production' ; import { VitePluginFederationOptions } from '../types' ; import { builderInfo , DEFAULT_ENTRY_FILENAME , parsedOptions } from './public' ; import { PluginHooks } from '../types/pluginHooks' ; import { ModuleInfo } from 'rollup' ; import { prodSharedPlugin } from './prod/shared-production' ; import { prodExposePlugin } from './prod/expose-production' ; import { devSharedPlugin } from './dev/shared-development' ; import { devRemotePlugin } from './dev/remote-development' ; import { devExposePlugin } from './dev/expose-development' ; export default function federation ( options ) { options . filename = options . filename ? options . filename : DEFAULT_ENTRY_FILENAME ; let pluginList = [ ] ; let virtualMod ; let registerCount = 0 ; function registerPlugins ( mode ) { if ( mode === 'development' ) { pluginList = [ devSharedPlugin ( options ) , devExposePlugin ( options ) , devRemotePlugin ( options ) ] ; } else if ( mode === 'production' ) { pluginList = [ prodSharedPlugin ( options ) , prodExposePlugin ( options ) , prodRemotePlugin ( options ) ] ; } else { pluginList = [ ] ; } builderInfo . isHost = ! ! ( parsedOptions . prodRemote . length || parsedOptions . devRemote . length ) ; builderInfo . isRemote = ! ! ( parsedOptions . prodExpose . length || parsedOptions . devExpose . length ) ; builderInfo . isShared = ! ! ( parsedOptions . prodShared . length || parsedOptions . devShared . length ) ; let virtualFiles = { } ; pluginList . forEach ( ( plugin ) => { if ( plugin . virtualFile ) { virtualFiles = Object . assign ( virtualFiles , plugin . virtualFile ) ; } } ) ; virtualMod = virtual ( virtualFiles ) ; } return { name : 'originjs:federation' , enforce : 'post' , options ( _options ) { if ( ! registerCount ++ ) { registerPlugins ( ( options . mode = options . mode ?? 'production' ) ) ; } if ( typeof _options . input === 'string' ) { _options . input = { index : _options . input } ; } _options . external = _options . external || [ ] ; if ( ! Array . isArray ( _options . external ) ) { _options . external = [ _options . external as string ] ; } for ( const pluginHook of pluginList ) { pluginHook . options ?. call ( this , _options ) ; } return _options ; } , config ( config , env ) { options . mode = env . mode ; registerPlugins ( options . mode ) ; registerCount ++ ; for ( const pluginHook of pluginList ) { pluginHook . config ?. call ( this , config , env ) ; } builderInfo . builder = 'vite' ; builderInfo . assetsDir = config ?. build ?. assetsDir ?? 'assets' ; } , configureServer ( server ) { for ( const pluginHook of pluginList ) { pluginHook . configureServer ?. call ( this , server ) ; } } , buildStart ( inputOptions ) { for ( const pluginHook of pluginList ) { pluginHook . buildStart ?. call ( this , inputOptions ) ; } } , resolveId ( ... args ) { const v = virtualMod . resolveId . call ( this , ... args ) ; if ( v ) { return v ; } return null ; } , load ( ... args ) { const v = virtualMod . load . call ( this , ... args ) ; if ( v ) { return v ; } return null ; } , transform ( code , id ) { for ( const pluginHook of pluginList ) { const result = pluginHook . transform ?. call ( this , code , id ) ; if ( result ) { return result ; } } return code ; } , moduleParsed ( moduleInfo ) { for ( const pluginHook of pluginList ) { pluginHook . moduleParsed ?. call ( this , moduleInfo ) ; } } , outputOptions ( outputOptions ) { outputOptions . manualChunks = outputOptions . manualChunks || { } ; for ( const pluginHook of pluginList ) { pluginHook . outputOptions ?. call ( this , outputOptions ) ; } return outputOptions ; } , renderChunk ( code , chunkInfo , _options ) { for ( const pluginHook of pluginList ) { const result = pluginHook . renderChunk ?. call ( this , code , chunkInfo , _options ) ; if ( result ) { return result ; } } return null ; } , generateBundle : function ( _options , bundle , isWrite ) { for ( const pluginHook of pluginList ) { pluginHook . generateBundle ?. call ( this , _options , bundle , isWrite ) ; } } } ; }