import * as path from 'path' ; import { useAppContext , createPlugin } from '@modern-js/core' ; import { createRuntimeExportsUtils } from '@modern-js/utils' ; export default createPlugin ( ( ) => { let bffExportsUtils ; const runtimeModulePath = path . resolve ( __dirname , '../runtime' ) ; return { config ( ) { const appContext = useAppContext ( ) ; const { appDirectory } = appContext ; bffExportsUtils = createRuntimeExportsUtils ( appContext . internalDirectory , 'server' ) ; const serverRuntimePath = bffExportsUtils . getPath ( ) ; let relativeRuntimePath = path . join ( '../' , path . relative ( appDirectory , serverRuntimePath ) ) ; if ( process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ) { relativeRuntimePath = `./${ path . relative ( appDirectory , serverRuntimePath ) }` ; } return { source : { alias : { '@modern-js/runtime/server' : relativeRuntimePath , } , } , } ; } , modifyEntryImports ( input ) { const { appDirectory } = useAppContext ( ) ; const runtimePath = require . resolve ( `@modern-js/runtime` , { paths : [ appDirectory ] , } ) ; const currentFile = bffExportsUtils . getPath ( ) ; const runtimeDir = path . dirname ( runtimePath ) ; const relativeBffPath = path . relative ( path . dirname ( currentFile ) , path . join ( runtimeDir , './exports/server' ) ) ; const relativeRuntimeModulePath = path . relative ( path . dirname ( currentFile ) , runtimeModulePath ) ; const relativeFramePath = path . relative ( path . dirname ( currentFile ) , require . resolve ( 'express' ) ) ; bffExportsUtils . addExport ( `const bffRuntime = require('${ relativeBffPath }');
           const pluginRuntime = require('${ relativeRuntimeModulePath }');
           const express = require('${ relativeFramePath }')
           module.exports = {
            express: express,
             ...bffRuntime,
             ...pluginRuntime
           }
          ` ) ; return input ; } , } ; } , { name : '@modern-js/plugin-express' } ) ;