import { SettingsPlugin , BuildSettings , DevServerSettings } from '@reskript/settings' ; import { Request , Response } from 'webpack-dev-server' ; import { Options } from './interface' ; import htmlEntry from './htmlEntry' ; import runtimeEntry from './runtimeEntry' ; export default ( appName , options ? ) : SettingsPlugin => { const finalizeBuild = config => { config . output = { ... config . output , library : `${ appName }-[name]` , libraryTarget : 'umd' , globalObject : 'window' , } ; return config ; } ; const finalizeDevServer = config => { const { setupMiddlewares } = config ; config . setupMiddlewares = ( middlewares , devServer ) => { devServer . app ?. get ( '/__qiankun_entry__.js' , async ( req , res ) => { res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ; res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE, PATCH, OPTIONS' ) ; res . setHeader ( 'Access-Control-Allow-Headers' , 'X-Requested-With, Content-Type, Authorization' ) ; const script = await runtimeEntry ( appName ) ; res . type ( 'js' ) . end ( script ) ; } ) ; return [ ... setupMiddlewares ?. ( middlewares , devServer ) ?? middlewares , { name : 'qiankun-entry-html' , path : '/__qiankun__.html' , middleware : async ( req , res ) => { const html = await htmlEntry ( appName , options ) ; res . type ( 'html' ) . end ( html ) ; } , } , ] ; } ; config . historyApiFallback = { index : '/__qiankun__.html' , disableDotRule : true , } ; return config ; } ; return settings => { return { ... settings , build : { ... settings . build , finalize : ( config , env , internals ) => { const previous = settings . build . finalize ?. ( config , env , internals ) ; return finalizeBuild ( previous , env , internals ) ; } , } , devServer : options ?. setupDevServer === false ? settings . devServer : { ... settings . devServer , finalize : ( config , env ) => { const previous = settings . devServer . finalize ( config , env ) ; return finalizeDevServer ( previous , env ) ; } , } , } ; } ; } ;