import Webpack from 'webpack' ; import { DevServerOptions } from '../type' ; const { EntryPlugin } = Webpack ; export default class DevServerPlugin { private readonly options : DevServerOptions ; constructor ( options ) { this . options = options ; } apply ( compiler ) { const { options } = this ; const host = `&host=${ options . client . host || 'localhost' }` ; const path = `&path=${ options . client . path }` ; const port = `&port=${ options . client . port }` ; const clientEntry = `${ require . resolve ( '@modern-js/hmr-client' ) }?${ host }${ path }${ port }` ; const hotEntry = require . resolve ( 'webpack/hot/dev-server' ) ; const additionalEntries = [ clientEntry , hotEntry ] ; for ( const additionalEntry of additionalEntries ) { new EntryPlugin ( compiler . context , additionalEntry , { name : undefined , } ) . apply ( compiler ) ; } const compilerOptions = compiler . options ; compilerOptions . plugins = compilerOptions . plugins || [ ] ; if ( hotEntry && ! compilerOptions . plugins . find ( p => p . constructor === Webpack . HotModuleReplacementPlugin ) ) { const plugin = new Webpack . HotModuleReplacementPlugin ( ) ; plugin . apply ( compiler ) ; } } }