123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486 |
- "use strict";
- const os = require("os");
- const path = require("path");
- const url = require("url");
- const util = require("util");
- const fs = require("graceful-fs");
- const ipaddr = require("ipaddr.js");
- const { validate } = require("schema-utils");
- const schema = require("./options.json");
- /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
- /** @typedef {import("webpack").Compiler} Compiler */
- /** @typedef {import("webpack").MultiCompiler} MultiCompiler */
- /** @typedef {import("webpack").Configuration} WebpackConfiguration */
- /** @typedef {import("webpack").StatsOptions} StatsOptions */
- /** @typedef {import("webpack").StatsCompilation} StatsCompilation */
- /** @typedef {import("webpack").Stats} Stats */
- /** @typedef {import("webpack").MultiStats} MultiStats */
- /** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
- /** @typedef {import("chokidar").WatchOptions} WatchOptions */
- /** @typedef {import("chokidar").FSWatcher} FSWatcher */
- /** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
- /** @typedef {import("bonjour-service").Bonjour} Bonjour */
- /** @typedef {import("bonjour-service").Service} BonjourOptions */
- /** @typedef {import("http-proxy-middleware").RequestHandler} RequestHandler */
- /** @typedef {import("http-proxy-middleware").Options} HttpProxyMiddlewareOptions */
- /** @typedef {import("http-proxy-middleware").Filter} HttpProxyMiddlewareOptionsFilter */
- /** @typedef {import("serve-index").Options} ServeIndexOptions */
- /** @typedef {import("serve-static").ServeStaticOptions} ServeStaticOptions */
- /** @typedef {import("ipaddr.js").IPv4} IPv4 */
- /** @typedef {import("ipaddr.js").IPv6} IPv6 */
- /** @typedef {import("net").Socket} Socket */
- /** @typedef {import("http").Server} HTTPServer*/
- /** @typedef {import("http").IncomingMessage} IncomingMessage */
- /** @typedef {import("http").ServerResponse} ServerResponse */
- /** @typedef {import("open").Options} OpenOptions */
- /** @typedef {import("express").Application} ExpressApplication */
- /** @typedef {import("express").RequestHandler} ExpressRequestHandler */
- /** @typedef {import("express").ErrorRequestHandler} ExpressErrorRequestHandler */
- /** @typedef {import("express").Request} ExpressRequest */
- /** @typedef {import("express").Response} ExpressResponse */
- /** @typedef {(err?: any) => void} NextFunction */
- /** @typedef {(req: IncomingMessage, res: ServerResponse) => void} SimpleHandleFunction */
- /** @typedef {(req: IncomingMessage, res: ServerResponse, next: NextFunction) => void} NextHandleFunction */
- /** @typedef {(err: any, req: IncomingMessage, res: ServerResponse, next: NextFunction) => void} ErrorHandleFunction */
- /** @typedef {SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction} HandleFunction */
- /** @typedef {import("https").ServerOptions & { spdy?: { plain?: boolean | undefined, ssl?: boolean | undefined, 'x-forwarded-for'?: string | undefined, protocol?: string | undefined, protocols?: string[] | undefined }}} ServerOptions */
- /**
- * @template {BasicApplication} [T=ExpressApplication]
- * @typedef {T extends ExpressApplication ? ExpressRequest : IncomingMessage} Request
- */
- /**
- * @template {BasicApplication} [T=ExpressApplication]
- * @typedef {T extends ExpressApplication ? ExpressResponse : ServerResponse} Response
- */
- /**
- * @template {Request} T
- * @template {Response} U
- * @typedef {import("webpack-dev-middleware").Options<T, U>} DevMiddlewareOptions
- */
- /**
- * @template {Request} T
- * @template {Response} U
- * @typedef {import("webpack-dev-middleware").Context<T, U>} DevMiddlewareContext
- */
- /**
- * @typedef {"local-ip" | "local-ipv4" | "local-ipv6" | string} Host
- */
- /**
- * @typedef {number | string | "auto"} Port
- */
- /**
- * @typedef {Object} WatchFiles
- * @property {string | string[]} paths
- * @property {WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} [options]
- */
- /**
- * @typedef {Object} Static
- * @property {string} [directory]
- * @property {string | string[]} [publicPath]
- * @property {boolean | ServeIndexOptions} [serveIndex]
- * @property {ServeStaticOptions} [staticOptions]
- * @property {boolean | WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} [watch]
- */
- /**
- * @typedef {Object} NormalizedStatic
- * @property {string} directory
- * @property {string[]} publicPath
- * @property {false | ServeIndexOptions} serveIndex
- * @property {ServeStaticOptions} staticOptions
- * @property {false | WatchOptions} watch
- */
- /**
- * @template {BasicApplication} [A=ExpressApplication]
- * @template {BasicServer} [S=import("http").Server]
- * @typedef {"http" | "https" | "spdy" | "http2" | string | function(ServerOptions, A): S} ServerType
- */
- /**
- * @template {BasicApplication} [A=ExpressApplication]
- * @template {BasicServer} [S=import("http").Server]
- * @typedef {Object} ServerConfiguration
- * @property {ServerType<A, S>} [type]
- * @property {ServerOptions} [options]
- */
- /**
- * @typedef {Object} WebSocketServerConfiguration
- * @property {"sockjs" | "ws" | string | Function} [type]
- * @property {Record<string, any>} [options]
- */
- /**
- * @typedef {(import("ws").WebSocket | import("sockjs").Connection & { send: import("ws").WebSocket["send"], terminate: import("ws").WebSocket["terminate"], ping: import("ws").WebSocket["ping"] }) & { isAlive?: boolean }} ClientConnection
- */
- /**
- * @typedef {import("ws").WebSocketServer | import("sockjs").Server & { close: import("ws").WebSocketServer["close"] }} WebSocketServer
- */
- /**
- * @typedef {{ implementation: WebSocketServer, clients: ClientConnection[] }} WebSocketServerImplementation
- */
- /**
- * @callback ByPass
- * @param {Request} req
- * @param {Response} res
- * @param {ProxyConfigArrayItem} proxyConfig
- */
- /**
- * @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
- */
- /**
- * @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
- */
- /**
- * @typedef {Object} OpenApp
- * @property {string} [name]
- * @property {string[]} [arguments]
- */
- /**
- * @typedef {Object} Open
- * @property {string | string[] | OpenApp} [app]
- * @property {string | string[]} [target]
- */
- /**
- * @typedef {Object} NormalizedOpen
- * @property {string} target
- * @property {import("open").Options} options
- */
- /**
- * @typedef {Object} WebSocketURL
- * @property {string} [hostname]
- * @property {string} [password]
- * @property {string} [pathname]
- * @property {number | string} [port]
- * @property {string} [protocol]
- * @property {string} [username]
- */
- /**
- * @typedef {boolean | ((error: Error) => void)} OverlayMessageOptions
- */
- /**
- * @typedef {Object} ClientConfiguration
- * @property {"log" | "info" | "warn" | "error" | "none" | "verbose"} [logging]
- * @property {boolean | { warnings?: OverlayMessageOptions, errors?: OverlayMessageOptions, runtimeErrors?: OverlayMessageOptions }} [overlay]
- * @property {boolean} [progress]
- * @property {boolean | number} [reconnect]
- * @property {"ws" | "sockjs" | string} [webSocketTransport]
- * @property {string | WebSocketURL} [webSocketURL]
- */
- /**
- * @typedef {Array<{ key: string; value: string }> | Record<string, string | string[]>} Headers
- */
- /**
- * @template {BasicApplication} [T=ExpressApplication]
- * @typedef {T extends ExpressApplication ? ExpressRequestHandler | ExpressErrorRequestHandler : HandleFunction} MiddlewareHandler
- */
- /**
- * @typedef {{ name?: string, path?: string, middleware: MiddlewareHandler }} MiddlewareObject
- */
- /**
- * @typedef {MiddlewareObject | MiddlewareHandler } Middleware
- */
- /** @typedef {import("net").Server | import("tls").Server} BasicServer */
- /**
- * @template {BasicApplication} [A=ExpressApplication]
- * @template {BasicServer} [S=import("http").Server]
- * @typedef {Object} Configuration
- * @property {boolean | string} [ipc]
- * @property {Host} [host]
- * @property {Port} [port]
- * @property {boolean | "only"} [hot]
- * @property {boolean} [liveReload]
- * @property {DevMiddlewareOptions<Request, Response>} [devMiddleware]
- * @property {boolean} [compress]
- * @property {"auto" | "all" | string | string[]} [allowedHosts]
- * @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
- * @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
- * @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
- * @property {boolean | string | Static | Array<string | Static>} [static]
- * @property {ServerType<A, S> | ServerConfiguration<A, S>} [server]
- * @property {() => Promise<A>} [app]
- * @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
- * @property {ProxyConfigArray} [proxy]
- * @property {boolean | string | Open | Array<string | Open>} [open]
- * @property {boolean} [setupExitSignals]
- * @property {boolean | ClientConfiguration} [client]
- * @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response> | undefined) => Headers)} [headers]
- * @property {(devServer: Server<A, S>) => void} [onListening]
- * @property {(middlewares: Middleware[], devServer: Server<A, S>) => Middleware[]} [setupMiddlewares]
- */
- if (!process.env.WEBPACK_SERVE) {
- process.env.WEBPACK_SERVE = "true";
- }
- /**
- * @template T
- * @param fn {(function(): any) | undefined}
- * @returns {function(): T}
- */
- const memoize = (fn) => {
- let cache = false;
- /** @type {T} */
- let result;
- return () => {
- if (cache) {
- return result;
- }
- result = /** @type {function(): any} */ (fn)();
- cache = true;
- // Allow to clean up memory for fn
- // and all dependent resources
- // eslint-disable-next-line no-undefined
- fn = undefined;
- return result;
- };
- };
- const getExpress = memoize(() => require("express"));
- /**
- *
- * @param {OverlayMessageOptions} [setting]
- * @returns
- */
- const encodeOverlaySettings = (setting) =>
- typeof setting === "function"
- ? encodeURIComponent(setting.toString())
- : setting;
- // Working for overload, because typescript doesn't support this yes
- /**
- * @overload
- * @param {NextHandleFunction} fn
- * @returns {BasicApplication}
- */
- /**
- * @overload
- * @param {HandleFunction} fn
- * @returns {BasicApplication}
- */
- /**
- * @overload
- * @param {string} route
- * @param {NextHandleFunction} fn
- * @returns {BasicApplication}
- */
- /**
- * @param {string} route
- * @param {HandleFunction} fn
- * @returns {BasicApplication}
- */
- // eslint-disable-next-line no-unused-vars
- function useFn(route, fn) {
- return /** @type {BasicApplication} */ ({});
- }
- /**
- * @typedef {Object} BasicApplication
- * @property {typeof useFn} use
- */
- /**
- * @template {BasicApplication} [A=ExpressApplication]
- * @template {BasicServer} [S=HTTPServer]
- */
- class Server {
- /**
- * @param {Configuration<A, S>} options
- * @param {Compiler | MultiCompiler} compiler
- */
- constructor(options = {}, compiler) {
- validate(/** @type {Schema} */ (schema), options, {
- name: "Dev Server",
- baseDataPath: "options",
- });
- this.compiler = compiler;
- /**
- * @type {ReturnType<Compiler["getInfrastructureLogger"]>}
- * */
- this.logger = this.compiler.getInfrastructureLogger("webpack-dev-server");
- this.options = options;
- /**
- * @type {FSWatcher[]}
- */
- this.staticWatchers = [];
- /**
- * @private
- * @type {{ name: string | symbol, listener: (...args: any[]) => void}[] }}
- */
- this.listeners = [];
- // Keep track of websocket proxies for external websocket upgrade.
- /**
- * @private
- * @type {RequestHandler[]}
- */
- this.webSocketProxies = [];
- /**
- * @type {Socket[]}
- */
- this.sockets = [];
- /**
- * @private
- * @type {string | undefined}
- */
- // eslint-disable-next-line no-undefined
- this.currentHash = undefined;
- }
- static get schema() {
- return schema;
- }
- /**
- * @private
- * @returns {StatsOptions}
- * @constructor
- */
- static get DEFAULT_STATS() {
- return {
- all: false,
- hash: true,
- warnings: true,
- errors: true,
- errorDetails: false,
- };
- }
- /**
- * @param {string} URL
- * @returns {boolean}
- */
- static isAbsoluteURL(URL) {
- // Don't match Windows paths `c:\`
- if (/^[a-zA-Z]:\\/.test(URL)) {
- return false;
- }
- // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
- // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
- return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
- }
- /**
- * @param {string} gatewayOrFamily or family
- * @param {boolean} [isInternal] ip should be internal
- * @returns {string | undefined}
- */
- static findIp(gatewayOrFamily, isInternal) {
- if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
- let host;
- const networks = Object.values(os.networkInterfaces())
- // eslint-disable-next-line no-shadow
- .flatMap((networks) => networks ?? [])
- .filter((network) => {
- if (!network || !network.address) {
- return false;
- }
- if (network.family !== `IP${gatewayOrFamily}`) {
- return false;
- }
- if (
- typeof isInternal !== "undefined" &&
- network.internal !== isInternal
- ) {
- return false;
- }
- if (gatewayOrFamily === "v6") {
- const range = ipaddr.parse(network.address).range();
- if (
- range !== "ipv4Mapped" &&
- range !== "uniqueLocal" &&
- range !== "loopback"
- ) {
- return false;
- }
- }
- return network.address;
- });
- for (const network of networks) {
- host = network.address;
- if (host.includes(":")) {
- host = `[${host}]`;
- }
- }
- return host;
- }
- const gatewayIp = ipaddr.parse(gatewayOrFamily);
- // Look for the matching interface in all local interfaces.
- for (const addresses of Object.values(os.networkInterfaces())) {
- for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
- addresses
- )) {
- const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));
- if (
- net[0] &&
- net[0].kind() === gatewayIp.kind() &&
- gatewayIp.match(net)
- ) {
- return net[0].toString();
- }
- }
- }
- }
- // TODO remove me in the next major release, we have `findIp`
- /**
- * @param {"v4" | "v6"} family
- * @returns {Promise<string | undefined>}
- */
- static async internalIP(family) {
- return Server.findIp(family, false);
- }
- // TODO remove me in the next major release, we have `findIp`
- /**
- * @param {"v4" | "v6"} family
- * @returns {string | undefined}
- */
- static internalIPSync(family) {
- return Server.findIp(family, false);
- }
- /**
- * @param {Host} hostname
- * @returns {Promise<string>}
- */
- static async getHostname(hostname) {
- if (hostname === "local-ip") {
- return (
- Server.findIp("v4", false) || Server.findIp("v6", false) || "0.0.0.0"
- );
- } else if (hostname === "local-ipv4") {
- return Server.findIp("v4", false) || "0.0.0.0";
- } else if (hostname === "local-ipv6") {
- return Server.findIp("v6", false) || "::";
- }
- return hostname;
- }
- /**
- * @param {Port} port
- * @param {string} host
- * @returns {Promise<number | string>}
- */
- static async getFreePort(port, host) {
- if (typeof port !== "undefined" && port !== null && port !== "auto") {
- return port;
- }
- const pRetry = (await import("p-retry")).default;
- const getPort = require("./getPort");
- const basePort =
- typeof process.env.WEBPACK_DEV_SERVER_BASE_PORT !== "undefined"
- ? parseInt(process.env.WEBPACK_DEV_SERVER_BASE_PORT, 10)
- : 8080;
- // Try to find unused port and listen on it for 3 times,
- // if port is not specified in options.
- const defaultPortRetry =
- typeof process.env.WEBPACK_DEV_SERVER_PORT_RETRY !== "undefined"
- ? parseInt(process.env.WEBPACK_DEV_SERVER_PORT_RETRY, 10)
- : 3;
- return pRetry(() => getPort(basePort, host), {
- retries: defaultPortRetry,
- });
- }
- /**
- * @returns {string}
- */
- static findCacheDir() {
- const cwd = process.cwd();
- /**
- * @type {string | undefined}
- */
- let dir = cwd;
- for (;;) {
- try {
- if (fs.statSync(path.join(dir, "package.json")).isFile()) break;
- // eslint-disable-next-line no-empty
- } catch (e) {}
- const parent = path.dirname(dir);
- if (dir === parent) {
- // eslint-disable-next-line no-undefined
- dir = undefined;
- break;
- }
- dir = parent;
- }
- if (!dir) {
- return path.resolve(cwd, ".cache/webpack-dev-server");
- } else if (process.versions.pnp === "1") {
- return path.resolve(dir, ".pnp/.cache/webpack-dev-server");
- } else if (process.versions.pnp === "3") {
- return path.resolve(dir, ".yarn/.cache/webpack-dev-server");
- }
- return path.resolve(dir, "node_modules/.cache/webpack-dev-server");
- }
- /**
- * @private
- * @param {Compiler} compiler
- * @returns bool
- */
- static isWebTarget(compiler) {
- if (compiler.platform && compiler.platform.web) {
- return compiler.platform.web;
- }
- // TODO improve for the next major version and keep only `webTargets` to fallback for old versions
- if (
- compiler.options.externalsPresets &&
- compiler.options.externalsPresets.web
- ) {
- return true;
- }
- if (
- compiler.options.resolve.conditionNames &&
- compiler.options.resolve.conditionNames.includes("browser")
- ) {
- return true;
- }
- const webTargets = [
- "web",
- "webworker",
- "electron-preload",
- "electron-renderer",
- "nwjs",
- "node-webkit",
- // eslint-disable-next-line no-undefined
- undefined,
- null,
- ];
- if (Array.isArray(compiler.options.target)) {
- return compiler.options.target.some((r) => webTargets.includes(r));
- }
- return webTargets.includes(/** @type {string} */ (compiler.options.target));
- }
- /**
- * @private
- * @param {Compiler} compiler
- */
- addAdditionalEntries(compiler) {
- /**
- * @type {string[]}
- */
- const additionalEntries = [];
- const isWebTarget = Server.isWebTarget(compiler);
- // TODO maybe empty client
- if (this.options.client && isWebTarget) {
- let webSocketURLStr = "";
- if (this.options.webSocketServer) {
- const webSocketURL =
- /** @type {WebSocketURL} */
- (
- /** @type {ClientConfiguration} */
- (this.options.client).webSocketURL
- );
- const webSocketServer =
- /** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
- (this.options.webSocketServer);
- const searchParams = new URLSearchParams();
- /** @type {string} */
- let protocol;
- // We are proxying dev server and need to specify custom `hostname`
- if (typeof webSocketURL.protocol !== "undefined") {
- protocol = webSocketURL.protocol;
- } else {
- protocol = this.isTlsServer ? "wss:" : "ws:";
- }
- searchParams.set("protocol", protocol);
- if (typeof webSocketURL.username !== "undefined") {
- searchParams.set("username", webSocketURL.username);
- }
- if (typeof webSocketURL.password !== "undefined") {
- searchParams.set("password", webSocketURL.password);
- }
- /** @type {string} */
- let hostname;
- // SockJS is not supported server mode, so `hostname` and `port` can't specified, let's ignore them
- const isSockJSType = webSocketServer.type === "sockjs";
- const isWebSocketServerHostDefined =
- typeof webSocketServer.options.host !== "undefined";
- const isWebSocketServerPortDefined =
- typeof webSocketServer.options.port !== "undefined";
- if (
- isSockJSType &&
- (isWebSocketServerHostDefined || isWebSocketServerPortDefined)
- ) {
- this.logger.warn(
- "SockJS only supports client mode and does not support custom hostname and port options. Please consider using 'ws' if you need to customize these options.",
- );
- }
- // We are proxying dev server and need to specify custom `hostname`
- if (typeof webSocketURL.hostname !== "undefined") {
- hostname = webSocketURL.hostname;
- }
- // Web socket server works on custom `hostname`, only for `ws` because `sock-js` is not support custom `hostname`
- else if (isWebSocketServerHostDefined && !isSockJSType) {
- hostname = webSocketServer.options.host;
- }
- // The `host` option is specified
- else if (typeof this.options.host !== "undefined") {
- hostname = this.options.host;
- }
- // The `port` option is not specified
- else {
- hostname = "0.0.0.0";
- }
- searchParams.set("hostname", hostname);
- /** @type {number | string} */
- let port;
- // We are proxying dev server and need to specify custom `port`
- if (typeof webSocketURL.port !== "undefined") {
- port = webSocketURL.port;
- }
- // Web socket server works on custom `port`, only for `ws` because `sock-js` is not support custom `port`
- else if (isWebSocketServerPortDefined && !isSockJSType) {
- port = webSocketServer.options.port;
- }
- // The `port` option is specified
- else if (typeof this.options.port === "number") {
- port = this.options.port;
- }
- // The `port` option is specified using `string`
- else if (
- typeof this.options.port === "string" &&
- this.options.port !== "auto"
- ) {
- port = Number(this.options.port);
- }
- // The `port` option is not specified or set to `auto`
- else {
- port = "0";
- }
- searchParams.set("port", String(port));
- /** @type {string} */
- let pathname = "";
- // We are proxying dev server and need to specify custom `pathname`
- if (typeof webSocketURL.pathname !== "undefined") {
- pathname = webSocketURL.pathname;
- }
- // Web socket server works on custom `path`
- else if (
- typeof webSocketServer.options.prefix !== "undefined" ||
- typeof webSocketServer.options.path !== "undefined"
- ) {
- pathname =
- webSocketServer.options.prefix || webSocketServer.options.path;
- }
- searchParams.set("pathname", pathname);
- const client = /** @type {ClientConfiguration} */ (this.options.client);
- if (typeof client.logging !== "undefined") {
- searchParams.set("logging", client.logging);
- }
- if (typeof client.progress !== "undefined") {
- searchParams.set("progress", String(client.progress));
- }
- if (typeof client.overlay !== "undefined") {
- const overlayString =
- typeof client.overlay === "boolean"
- ? String(client.overlay)
- : JSON.stringify({
- ...client.overlay,
- errors: encodeOverlaySettings(client.overlay.errors),
- warnings: encodeOverlaySettings(client.overlay.warnings),
- runtimeErrors: encodeOverlaySettings(
- client.overlay.runtimeErrors,
- ),
- });
- searchParams.set("overlay", overlayString);
- }
- if (typeof client.reconnect !== "undefined") {
- searchParams.set(
- "reconnect",
- typeof client.reconnect === "number"
- ? String(client.reconnect)
- : "10",
- );
- }
- if (typeof this.options.hot !== "undefined") {
- searchParams.set("hot", String(this.options.hot));
- }
- if (typeof this.options.liveReload !== "undefined") {
- searchParams.set("live-reload", String(this.options.liveReload));
- }
- webSocketURLStr = searchParams.toString();
- }
- additionalEntries.push(`${this.getClientEntry()}?${webSocketURLStr}`);
- }
- const clientHotEntry = this.getClientHotEntry();
- if (clientHotEntry) {
- additionalEntries.push(clientHotEntry);
- }
- const webpack = compiler.webpack || require("webpack");
- // use a hook to add entries if available
- for (const additionalEntry of additionalEntries) {
- new webpack.EntryPlugin(compiler.context, additionalEntry, {
- // eslint-disable-next-line no-undefined
- name: undefined,
- }).apply(compiler);
- }
- }
- /**
- * @private
- * @returns {Compiler["options"]}
- */
- getCompilerOptions() {
- if (
- typeof (/** @type {MultiCompiler} */ (this.compiler).compilers) !==
- "undefined"
- ) {
- if (/** @type {MultiCompiler} */ (this.compiler).compilers.length === 1) {
- return (
- /** @type {MultiCompiler} */
- (this.compiler).compilers[0].options
- );
- }
- // Configuration with the `devServer` options
- const compilerWithDevServer =
- /** @type {MultiCompiler} */
- (this.compiler).compilers.find((config) => config.options.devServer);
- if (compilerWithDevServer) {
- return compilerWithDevServer.options;
- }
- // Configuration with `web` preset
- const compilerWithWebPreset =
- /** @type {MultiCompiler} */
- (this.compiler).compilers.find(
- (config) =>
- (config.options.externalsPresets &&
- config.options.externalsPresets.web) ||
- [
- "web",
- "webworker",
- "electron-preload",
- "electron-renderer",
- "node-webkit",
- // eslint-disable-next-line no-undefined
- undefined,
- null,
- ].includes(/** @type {string} */ (config.options.target)),
- );
- if (compilerWithWebPreset) {
- return compilerWithWebPreset.options;
- }
- // Fallback
- return /** @type {MultiCompiler} */ (this.compiler).compilers[0].options;
- }
- return /** @type {Compiler} */ (this.compiler).options;
- }
- /**
- * @private
- * @returns {Promise<void>}
- */
- async normalizeOptions() {
- const { options } = this;
- const compilerOptions = this.getCompilerOptions();
- const compilerWatchOptions = compilerOptions.watchOptions;
- /**
- * @param {WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} watchOptions
- * @returns {WatchOptions}
- */
- const getWatchOptions = (watchOptions = {}) => {
- const getPolling = () => {
- if (typeof watchOptions.usePolling !== "undefined") {
- return watchOptions.usePolling;
- }
- if (typeof watchOptions.poll !== "undefined") {
- return Boolean(watchOptions.poll);
- }
- if (typeof compilerWatchOptions.poll !== "undefined") {
- return Boolean(compilerWatchOptions.poll);
- }
- return false;
- };
- const getInterval = () => {
- if (typeof watchOptions.interval !== "undefined") {
- return watchOptions.interval;
- }
- if (typeof watchOptions.poll === "number") {
- return watchOptions.poll;
- }
- if (typeof compilerWatchOptions.poll === "number") {
- return compilerWatchOptions.poll;
- }
- };
- const usePolling = getPolling();
- const interval = getInterval();
- const { poll, ...rest } = watchOptions;
- return {
- ignoreInitial: true,
- persistent: true,
- followSymlinks: false,
- atomic: false,
- alwaysStat: true,
- ignorePermissionErrors: true,
- // Respect options from compiler watchOptions
- usePolling,
- interval,
- ignored: watchOptions.ignored,
- // TODO: we respect these options for all watch options and allow developers to pass them to chokidar, but chokidar doesn't have these options maybe we need revisit that in future
- ...rest,
- };
- };
- /**
- * @param {string | Static | undefined} [optionsForStatic]
- * @returns {NormalizedStatic}
- */
- const getStaticItem = (optionsForStatic) => {
- const getDefaultStaticOptions = () => {
- return {
- directory: path.join(process.cwd(), "public"),
- staticOptions: {},
- publicPath: ["/"],
- serveIndex: { icons: true },
- watch: getWatchOptions(),
- };
- };
- /** @type {NormalizedStatic} */
- let item;
- if (typeof optionsForStatic === "undefined") {
- item = getDefaultStaticOptions();
- } else if (typeof optionsForStatic === "string") {
- item = {
- ...getDefaultStaticOptions(),
- directory: optionsForStatic,
- };
- } else {
- const def = getDefaultStaticOptions();
- item = {
- directory:
- typeof optionsForStatic.directory !== "undefined"
- ? optionsForStatic.directory
- : def.directory,
- staticOptions:
- typeof optionsForStatic.staticOptions !== "undefined"
- ? { ...def.staticOptions, ...optionsForStatic.staticOptions }
- : def.staticOptions,
- publicPath:
- // eslint-disable-next-line no-nested-ternary
- typeof optionsForStatic.publicPath !== "undefined"
- ? Array.isArray(optionsForStatic.publicPath)
- ? optionsForStatic.publicPath
- : [optionsForStatic.publicPath]
- : def.publicPath,
- serveIndex:
- // Check if 'serveIndex' property is defined in 'optionsForStatic'
- // If 'serveIndex' is a boolean and true, use default 'serveIndex'
- // If 'serveIndex' is an object, merge its properties with default 'serveIndex'
- // If 'serveIndex' is neither a boolean true nor an object, use it as-is
- // If 'serveIndex' is not defined in 'optionsForStatic', use default 'serveIndex'
- // eslint-disable-next-line no-nested-ternary
- typeof optionsForStatic.serveIndex !== "undefined"
- ? // eslint-disable-next-line no-nested-ternary
- typeof optionsForStatic.serveIndex === "boolean" &&
- optionsForStatic.serveIndex
- ? def.serveIndex
- : typeof optionsForStatic.serveIndex === "object"
- ? { ...def.serveIndex, ...optionsForStatic.serveIndex }
- : optionsForStatic.serveIndex
- : def.serveIndex,
- watch:
- // eslint-disable-next-line no-nested-ternary
- typeof optionsForStatic.watch !== "undefined"
- ? // eslint-disable-next-line no-nested-ternary
- typeof optionsForStatic.watch === "boolean"
- ? optionsForStatic.watch
- ? def.watch
- : false
- : getWatchOptions(optionsForStatic.watch)
- : def.watch,
- };
- }
- if (Server.isAbsoluteURL(item.directory)) {
- throw new Error("Using a URL as static.directory is not supported");
- }
- return item;
- };
- if (typeof options.allowedHosts === "undefined") {
- // AllowedHosts allows some default hosts picked from `options.host` or `webSocketURL.hostname` and `localhost`
- options.allowedHosts = "auto";
- }
- // We store allowedHosts as array when supplied as string
- else if (
- typeof options.allowedHosts === "string" &&
- options.allowedHosts !== "auto" &&
- options.allowedHosts !== "all"
- ) {
- options.allowedHosts = [options.allowedHosts];
- }
- // CLI pass options as array, we should normalize them
- else if (
- Array.isArray(options.allowedHosts) &&
- options.allowedHosts.includes("all")
- ) {
- options.allowedHosts = "all";
- }
- if (typeof options.bonjour === "undefined") {
- options.bonjour = false;
- } else if (typeof options.bonjour === "boolean") {
- options.bonjour = options.bonjour ? {} : false;
- }
- if (
- typeof options.client === "undefined" ||
- (typeof options.client === "object" && options.client !== null)
- ) {
- if (!options.client) {
- options.client = {};
- }
- if (typeof options.client.webSocketURL === "undefined") {
- options.client.webSocketURL = {};
- } else if (typeof options.client.webSocketURL === "string") {
- const parsedURL = new URL(options.client.webSocketURL);
- options.client.webSocketURL = {
- protocol: parsedURL.protocol,
- hostname: parsedURL.hostname,
- port: parsedURL.port.length > 0 ? Number(parsedURL.port) : "",
- pathname: parsedURL.pathname,
- username: parsedURL.username,
- password: parsedURL.password,
- };
- } else if (typeof options.client.webSocketURL.port === "string") {
- options.client.webSocketURL.port = Number(
- options.client.webSocketURL.port,
- );
- }
- // Enable client overlay by default
- if (typeof options.client.overlay === "undefined") {
- options.client.overlay = true;
- } else if (typeof options.client.overlay !== "boolean") {
- options.client.overlay = {
- errors: true,
- warnings: true,
- ...options.client.overlay,
- };
- }
- if (typeof options.client.reconnect === "undefined") {
- options.client.reconnect = 10;
- } else if (options.client.reconnect === true) {
- options.client.reconnect = Infinity;
- } else if (options.client.reconnect === false) {
- options.client.reconnect = 0;
- }
- // Respect infrastructureLogging.level
- if (typeof options.client.logging === "undefined") {
- options.client.logging = compilerOptions.infrastructureLogging
- ? compilerOptions.infrastructureLogging.level
- : "info";
- }
- }
- if (typeof options.compress === "undefined") {
- options.compress = true;
- }
- if (typeof options.devMiddleware === "undefined") {
- options.devMiddleware = {};
- }
- // No need to normalize `headers`
- if (typeof options.historyApiFallback === "undefined") {
- options.historyApiFallback = false;
- } else if (
- typeof options.historyApiFallback === "boolean" &&
- options.historyApiFallback
- ) {
- options.historyApiFallback = {};
- }
- // No need to normalize `host`
- options.hot =
- typeof options.hot === "boolean" || options.hot === "only"
- ? options.hot
- : true;
- if (
- typeof options.server === "function" ||
- typeof options.server === "string"
- ) {
- options.server = {
- type: options.server,
- options: {},
- };
- } else {
- const serverOptions =
- /** @type {ServerConfiguration<A, S>} */
- (options.server || {});
- options.server = {
- type: serverOptions.type || "http",
- options: { ...serverOptions.options },
- };
- }
- const serverOptions = /** @type {ServerOptions} */ (options.server.options);
- if (
- options.server.type === "spdy" &&
- typeof serverOptions.spdy === "undefined"
- ) {
- serverOptions.spdy = { protocols: ["h2", "http/1.1"] };
- }
- if (
- options.server.type === "https" ||
- options.server.type === "http2" ||
- options.server.type === "spdy"
- ) {
- if (typeof serverOptions.requestCert === "undefined") {
- serverOptions.requestCert = false;
- }
- const httpsProperties =
- /** @type {Array<keyof ServerOptions>} */
- (["ca", "cert", "crl", "key", "pfx"]);
- for (const property of httpsProperties) {
- if (typeof serverOptions[property] === "undefined") {
- // eslint-disable-next-line no-continue
- continue;
- }
- /** @type {any} */
- const value = serverOptions[property];
- /**
- * @param {string | Buffer | undefined} item
- * @returns {string | Buffer | undefined}
- */
- const readFile = (item) => {
- if (
- Buffer.isBuffer(item) ||
- (typeof item === "object" && item !== null && !Array.isArray(item))
- ) {
- return item;
- }
- if (item) {
- let stats = null;
- try {
- stats = fs.lstatSync(fs.realpathSync(item)).isFile();
- } catch (error) {
- // Ignore error
- }
- // It is a file
- return stats ? fs.readFileSync(item) : item;
- }
- };
- /** @type {any} */
- (serverOptions)[property] = Array.isArray(value)
- ? value.map((item) => readFile(item))
- : readFile(value);
- }
- let fakeCert;
- if (!serverOptions.key || !serverOptions.cert) {
- const certificateDir = Server.findCacheDir();
- const certificatePath = path.join(certificateDir, "server.pem");
- let certificateExists;
- try {
- const certificate = await fs.promises.stat(certificatePath);
- certificateExists = certificate.isFile();
- } catch {
- certificateExists = false;
- }
- if (certificateExists) {
- const certificateTtl = 1000 * 60 * 60 * 24;
- const certificateStat = await fs.promises.stat(certificatePath);
- const now = Number(new Date());
- // cert is more than 30 days old, kill it with fire
- if ((now - Number(certificateStat.ctime)) / certificateTtl > 30) {
- this.logger.info(
- "SSL certificate is more than 30 days old. Removing...",
- );
- await fs.promises.rm(certificatePath, { recursive: true });
- certificateExists = false;
- }
- }
- if (!certificateExists) {
- this.logger.info("Generating SSL certificate...");
- const selfsigned = require("selfsigned");
- const attributes = [{ name: "commonName", value: "localhost" }];
- const pems = selfsigned.generate(attributes, {
- algorithm: "sha256",
- days: 30,
- keySize: 2048,
- extensions: [
- {
- name: "basicConstraints",
- cA: true,
- },
- {
- name: "keyUsage",
- keyCertSign: true,
- digitalSignature: true,
- nonRepudiation: true,
- keyEncipherment: true,
- dataEncipherment: true,
- },
- {
- name: "extKeyUsage",
- serverAuth: true,
- clientAuth: true,
- codeSigning: true,
- timeStamping: true,
- },
- {
- name: "subjectAltName",
- altNames: [
- {
- // type 2 is DNS
- type: 2,
- value: "localhost",
- },
- {
- type: 2,
- value: "localhost.localdomain",
- },
- {
- type: 2,
- value: "lvh.me",
- },
- {
- type: 2,
- value: "*.lvh.me",
- },
- {
- type: 2,
- value: "[::1]",
- },
- {
- // type 7 is IP
- type: 7,
- ip: "127.0.0.1",
- },
- {
- type: 7,
- ip: "fe80::1",
- },
- ],
- },
- ],
- });
- await fs.promises.mkdir(certificateDir, { recursive: true });
- await fs.promises.writeFile(
- certificatePath,
- pems.private + pems.cert,
- {
- encoding: "utf8",
- },
- );
- }
- fakeCert = await fs.promises.readFile(certificatePath);
- this.logger.info(`SSL certificate: ${certificatePath}`);
- }
- serverOptions.key = serverOptions.key || fakeCert;
- serverOptions.cert = serverOptions.cert || fakeCert;
- }
- if (typeof options.ipc === "boolean") {
- const isWindows = process.platform === "win32";
- const pipePrefix = isWindows ? "\\\\.\\pipe\\" : os.tmpdir();
- const pipeName = "webpack-dev-server.sock";
- options.ipc = path.join(pipePrefix, pipeName);
- }
- options.liveReload =
- typeof options.liveReload !== "undefined" ? options.liveReload : true;
- // https://github.com/webpack/webpack-dev-server/issues/1990
- const defaultOpenOptions = { wait: false };
- /**
- * @param {any} target
- * @returns {NormalizedOpen[]}
- */
- const getOpenItemsFromObject = ({ target, ...rest }) => {
- const normalizedOptions = { ...defaultOpenOptions, ...rest };
- if (typeof normalizedOptions.app === "string") {
- normalizedOptions.app = {
- name: normalizedOptions.app,
- };
- }
- const normalizedTarget = typeof target === "undefined" ? "<url>" : target;
- if (Array.isArray(normalizedTarget)) {
- return normalizedTarget.map((singleTarget) => {
- return { target: singleTarget, options: normalizedOptions };
- });
- }
- return [{ target: normalizedTarget, options: normalizedOptions }];
- };
- if (typeof options.open === "undefined") {
- /** @type {NormalizedOpen[]} */
- (options.open) = [];
- } else if (typeof options.open === "boolean") {
- /** @type {NormalizedOpen[]} */
- (options.open) = options.open
- ? [
- {
- target: "<url>",
- options: /** @type {OpenOptions} */ (defaultOpenOptions),
- },
- ]
- : [];
- } else if (typeof options.open === "string") {
- /** @type {NormalizedOpen[]} */
- (options.open) = [{ target: options.open, options: defaultOpenOptions }];
- } else if (Array.isArray(options.open)) {
- /**
- * @type {NormalizedOpen[]}
- */
- const result = [];
- for (const item of options.open) {
- if (typeof item === "string") {
- result.push({ target: item, options: defaultOpenOptions });
- // eslint-disable-next-line no-continue
- continue;
- }
- result.push(...getOpenItemsFromObject(item));
- }
- /** @type {NormalizedOpen[]} */
- (options.open) = result;
- } else {
- /** @type {NormalizedOpen[]} */
- (options.open) = [...getOpenItemsFromObject(options.open)];
- }
- if (typeof options.port === "string" && options.port !== "auto") {
- options.port = Number(options.port);
- }
- /**
- * Assume a proxy configuration specified as:
- * proxy: {
- * 'context': { options }
- * }
- * OR
- * proxy: {
- * 'context': 'target'
- * }
- */
- if (typeof options.proxy !== "undefined") {
- options.proxy = options.proxy.map((item) => {
- if (typeof item === "function") {
- return item;
- }
- /**
- * @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
- * @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
- */
- const getLogLevelForProxy = (level) => {
- if (level === "none") {
- return "silent";
- }
- if (level === "log") {
- return "info";
- }
- if (level === "verbose") {
- return "debug";
- }
- return level;
- };
- if (typeof item.logLevel === "undefined") {
- item.logLevel = getLogLevelForProxy(
- compilerOptions.infrastructureLogging
- ? compilerOptions.infrastructureLogging.level
- : "info",
- );
- }
- if (typeof item.logProvider === "undefined") {
- item.logProvider = () => this.logger;
- }
- return item;
- });
- }
- if (typeof options.setupExitSignals === "undefined") {
- options.setupExitSignals = true;
- }
- if (typeof options.static === "undefined") {
- options.static = [getStaticItem()];
- } else if (typeof options.static === "boolean") {
- options.static = options.static ? [getStaticItem()] : false;
- } else if (typeof options.static === "string") {
- options.static = [getStaticItem(options.static)];
- } else if (Array.isArray(options.static)) {
- options.static = options.static.map((item) => getStaticItem(item));
- } else {
- options.static = [getStaticItem(options.static)];
- }
- if (typeof options.watchFiles === "string") {
- options.watchFiles = [
- { paths: options.watchFiles, options: getWatchOptions() },
- ];
- } else if (
- typeof options.watchFiles === "object" &&
- options.watchFiles !== null &&
- !Array.isArray(options.watchFiles)
- ) {
- options.watchFiles = [
- {
- paths: options.watchFiles.paths,
- options: getWatchOptions(options.watchFiles.options || {}),
- },
- ];
- } else if (Array.isArray(options.watchFiles)) {
- options.watchFiles = options.watchFiles.map((item) => {
- if (typeof item === "string") {
- return { paths: item, options: getWatchOptions() };
- }
- return {
- paths: item.paths,
- options: getWatchOptions(item.options || {}),
- };
- });
- } else {
- options.watchFiles = [];
- }
- const defaultWebSocketServerType = "ws";
- const defaultWebSocketServerOptions = { path: "/ws" };
- if (typeof options.webSocketServer === "undefined") {
- options.webSocketServer = {
- type: defaultWebSocketServerType,
- options: defaultWebSocketServerOptions,
- };
- } else if (
- typeof options.webSocketServer === "boolean" &&
- !options.webSocketServer
- ) {
- options.webSocketServer = false;
- } else if (
- typeof options.webSocketServer === "string" ||
- typeof options.webSocketServer === "function"
- ) {
- options.webSocketServer = {
- type: options.webSocketServer,
- options: defaultWebSocketServerOptions,
- };
- } else {
- options.webSocketServer = {
- type:
- /** @type {WebSocketServerConfiguration} */
- (options.webSocketServer).type || defaultWebSocketServerType,
- options: {
- ...defaultWebSocketServerOptions,
- .../** @type {WebSocketServerConfiguration} */
- (options.webSocketServer).options,
- },
- };
- const webSocketServer =
- /** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
- (options.webSocketServer);
- if (typeof webSocketServer.options.port === "string") {
- webSocketServer.options.port = Number(webSocketServer.options.port);
- }
- }
- }
- /**
- * @private
- * @returns {string}
- */
- getClientTransport() {
- let clientImplementation;
- let clientImplementationFound = true;
- const isKnownWebSocketServerImplementation =
- this.options.webSocketServer &&
- typeof (
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type
- ) === "string" &&
- // @ts-ignore
- (this.options.webSocketServer.type === "ws" ||
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type === "sockjs");
- let clientTransport;
- if (this.options.client) {
- if (
- typeof (
- /** @type {ClientConfiguration} */
- (this.options.client).webSocketTransport
- ) !== "undefined"
- ) {
- clientTransport =
- /** @type {ClientConfiguration} */
- (this.options.client).webSocketTransport;
- } else if (isKnownWebSocketServerImplementation) {
- clientTransport =
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type;
- } else {
- clientTransport = "ws";
- }
- } else {
- clientTransport = "ws";
- }
- switch (typeof clientTransport) {
- case "string":
- // could be 'sockjs', 'ws', or a path that should be required
- if (clientTransport === "sockjs") {
- clientImplementation = require.resolve(
- "../client/clients/SockJSClient",
- );
- } else if (clientTransport === "ws") {
- clientImplementation = require.resolve(
- "../client/clients/WebSocketClient",
- );
- } else {
- try {
- clientImplementation = require.resolve(clientTransport);
- } catch (e) {
- clientImplementationFound = false;
- }
- }
- break;
- default:
- clientImplementationFound = false;
- }
- if (!clientImplementationFound) {
- throw new Error(
- `${
- !isKnownWebSocketServerImplementation
- ? "When you use custom web socket implementation you must explicitly specify client.webSocketTransport. "
- : ""
- }client.webSocketTransport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class `,
- );
- }
- return /** @type {string} */ (clientImplementation);
- }
- /**
- * @template T
- * @private
- * @returns {T}
- */
- getServerTransport() {
- let implementation;
- let implementationFound = true;
- switch (
- typeof (
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type
- )
- ) {
- case "string":
- // Could be 'sockjs', in the future 'ws', or a path that should be required
- if (
- /** @type {WebSocketServerConfiguration} */ (
- this.options.webSocketServer
- ).type === "sockjs"
- ) {
- implementation = require("./servers/SockJSServer");
- } else if (
- /** @type {WebSocketServerConfiguration} */ (
- this.options.webSocketServer
- ).type === "ws"
- ) {
- implementation = require("./servers/WebsocketServer");
- } else {
- try {
- // eslint-disable-next-line import/no-dynamic-require
- implementation = require(
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type,
- );
- } catch (error) {
- implementationFound = false;
- }
- }
- break;
- case "function":
- implementation =
- /** @type {WebSocketServerConfiguration} */
- (this.options.webSocketServer).type;
- break;
- default:
- implementationFound = false;
- }
- if (!implementationFound) {
- throw new Error(
- "webSocketServer (webSocketServer.type) must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to " +
- "a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer.js) " +
- "via require.resolve(...), or the class itself which extends BaseServer",
- );
- }
- return implementation;
- }
- /**
- * @returns {string}
- */
- // eslint-disable-next-line class-methods-use-this
- getClientEntry() {
- return require.resolve("../client/index.js");
- }
- /**
- * @returns {string | void}
- */
- getClientHotEntry() {
- if (this.options.hot === "only") {
- return require.resolve("webpack/hot/only-dev-server");
- } else if (this.options.hot) {
- return require.resolve("webpack/hot/dev-server");
- }
- }
- /**
- * @private
- * @returns {void}
- */
- setupProgressPlugin() {
- const { ProgressPlugin } =
- /** @type {MultiCompiler}*/
- (this.compiler).compilers
- ? /** @type {MultiCompiler}*/ (this.compiler).compilers[0].webpack
- : /** @type {Compiler}*/ (this.compiler).webpack;
- new ProgressPlugin(
- /**
- * @param {number} percent
- * @param {string} msg
- * @param {string} addInfo
- * @param {string} pluginName
- */
- (percent, msg, addInfo, pluginName) => {
- percent = Math.floor(percent * 100);
- if (percent === 100) {
- msg = "Compilation completed";
- }
- if (addInfo) {
- msg = `${msg} (${addInfo})`;
- }
- if (this.webSocketServer) {
- this.sendMessage(this.webSocketServer.clients, "progress-update", {
- percent,
- msg,
- pluginName,
- });
- }
- if (this.server) {
- this.server.emit("progress-update", { percent, msg, pluginName });
- }
- },
- ).apply(this.compiler);
- }
- /**
- * @private
- * @returns {Promise<void>}
- */
- async initialize() {
- this.setupHooks();
- await this.setupApp();
- await this.createServer();
- if (this.options.webSocketServer) {
- const compilers =
- /** @type {MultiCompiler} */
- (this.compiler).compilers || [this.compiler];
- for (const compiler of compilers) {
- if (compiler.options.devServer === false) {
- // eslint-disable-next-line no-continue
- continue;
- }
- this.addAdditionalEntries(compiler);
- const webpack = compiler.webpack || require("webpack");
- new webpack.ProvidePlugin({
- __webpack_dev_server_client__: this.getClientTransport(),
- }).apply(compiler);
- if (this.options.hot) {
- const HMRPluginExists = compiler.options.plugins.find(
- (p) => p && p.constructor === webpack.HotModuleReplacementPlugin,
- );
- if (HMRPluginExists) {
- this.logger.warn(
- `"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`,
- );
- } else {
- // Apply the HMR plugin
- const plugin = new webpack.HotModuleReplacementPlugin();
- plugin.apply(compiler);
- }
- }
- }
- if (
- this.options.client &&
- /** @type {ClientConfiguration} */ (this.options.client).progress
- ) {
- this.setupProgressPlugin();
- }
- }
- this.setupWatchFiles();
- this.setupWatchStaticFiles();
- this.setupMiddlewares();
- if (this.options.setupExitSignals) {
- const signals = ["SIGINT", "SIGTERM"];
- let needForceShutdown = false;
- signals.forEach((signal) => {
- const listener = () => {
- if (needForceShutdown) {
- process.exit();
- }
- this.logger.info(
- "Gracefully shutting down. To force exit, press ^C again. Please wait...",
- );
- needForceShutdown = true;
- this.stopCallback(() => {
- if (typeof this.compiler.close === "function") {
- this.compiler.close(() => {
- process.exit();
- });
- } else {
- process.exit();
- }
- });
- };
- this.listeners.push({ name: signal, listener });
- process.on(signal, listener);
- });
- }
- // Proxy WebSocket without the initial http request
- // https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
- const webSocketProxies =
- /** @type {RequestHandler[]} */
- (this.webSocketProxies);
- for (const webSocketProxy of webSocketProxies) {
- /** @type {S} */
- (this.server).on(
- "upgrade",
- /** @type {RequestHandler & { upgrade: NonNullable<RequestHandler["upgrade"]> }} */
- (webSocketProxy).upgrade,
- );
- }
- }
- /**
- * @private
- * @returns {Promise<void>}
- */
- async setupApp() {
- /** @type {A | undefined}*/
- this.app =
- typeof this.options.app === "function"
- ? await this.options.app()
- : getExpress()();
- }
- /**
- * @private
- * @param {Stats | MultiStats} statsObj
- * @returns {StatsCompilation}
- */
- getStats(statsObj) {
- const stats = Server.DEFAULT_STATS;
- const compilerOptions = this.getCompilerOptions();
- // @ts-ignore
- if (compilerOptions.stats && compilerOptions.stats.warningsFilter) {
- // @ts-ignore
- stats.warningsFilter = compilerOptions.stats.warningsFilter;
- }
- return statsObj.toJson(stats);
- }
- /**
- * @private
- * @returns {void}
- */
- setupHooks() {
- this.compiler.hooks.invalid.tap("webpack-dev-server", () => {
- if (this.webSocketServer) {
- this.sendMessage(this.webSocketServer.clients, "invalid");
- }
- });
- this.compiler.hooks.done.tap(
- "webpack-dev-server",
- /**
- * @param {Stats | MultiStats} stats
- */
- (stats) => {
- if (this.webSocketServer) {
- this.sendStats(this.webSocketServer.clients, this.getStats(stats));
- }
- /**
- * @private
- * @type {Stats | MultiStats}
- */
- this.stats = stats;
- },
- );
- }
- /**
- * @private
- * @returns {void}
- */
- setupWatchStaticFiles() {
- const watchFiles = /** @type {NormalizedStatic[]} */ (this.options.static);
- if (watchFiles.length > 0) {
- for (const item of watchFiles) {
- if (item.watch) {
- this.watchFiles(item.directory, item.watch);
- }
- }
- }
- }
- /**
- * @private
- * @returns {void}
- */
- setupWatchFiles() {
- const watchFiles = /** @type {WatchFiles[]} */ (this.options.watchFiles);
- if (watchFiles.length > 0) {
- for (const item of watchFiles) {
- this.watchFiles(item.paths, item.options);
- }
- }
- }
- /**
- * @private
- * @returns {void}
- */
- setupMiddlewares() {
- /**
- * @type {Array<Middleware>}
- */
- let middlewares = [];
- // Register setup host header check for security
- middlewares.push({
- name: "host-header-check",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- const headers =
- /** @type {{ [key: string]: string | undefined }} */
- (req.headers);
- const headerName = headers[":authority"] ? ":authority" : "host";
- if (this.checkHeader(headers, headerName)) {
- next();
- return;
- }
- res.statusCode = 403;
- res.end("Invalid Host header");
- },
- });
- const isHTTP2 =
- /** @type {ServerConfiguration<A, S>} */ (this.options.server).type ===
- "http2";
- if (isHTTP2) {
- // TODO patch for https://github.com/pillarjs/finalhandler/pull/45, need remove then will be resolved
- middlewares.push({
- name: "http2-status-message-patch",
- middleware:
- /** @type {NextHandleFunction} */
- (_req, res, next) => {
- Object.defineProperty(res, "statusMessage", {
- get() {
- return "";
- },
- set() {},
- });
- next();
- },
- });
- }
- // compress is placed last and uses unshift so that it will be the first middleware used
- if (this.options.compress && !isHTTP2) {
- const compression = require("compression");
- middlewares.push({ name: "compression", middleware: compression() });
- }
- if (typeof this.options.headers !== "undefined") {
- middlewares.push({
- name: "set-headers",
- middleware: this.setHeaders.bind(this),
- });
- }
- middlewares.push({
- name: "webpack-dev-middleware",
- middleware: /** @type {MiddlewareHandler} */ (this.middleware),
- });
- // Should be after `webpack-dev-middleware`, otherwise other middlewares might rewrite response
- middlewares.push({
- name: "webpack-dev-server-sockjs-bundle",
- path: "/__webpack_dev_server__/sockjs.bundle.js",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- if (req.method !== "GET" && req.method !== "HEAD") {
- next();
- return;
- }
- const clientPath = path.join(
- __dirname,
- "..",
- "client/modules/sockjs-client/index.js",
- );
- // Express send Etag and other headers by default, so let's keep them for compatibility reasons
- if (typeof res.sendFile === "function") {
- res.sendFile(clientPath);
- return;
- }
- let stats;
- try {
- // TODO implement `inputFileSystem.createReadStream` in webpack
- stats = fs.statSync(clientPath);
- } catch (err) {
- next();
- return;
- }
- res.setHeader("Content-Type", "application/javascript; charset=UTF-8");
- res.setHeader("Content-Length", stats.size);
- if (req.method === "HEAD") {
- res.end();
- return;
- }
- fs.createReadStream(clientPath).pipe(res);
- },
- });
- middlewares.push({
- name: "webpack-dev-server-invalidate",
- path: "/webpack-dev-server/invalidate",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- if (req.method !== "GET" && req.method !== "HEAD") {
- next();
- return;
- }
- this.invalidate();
- res.end();
- },
- });
- middlewares.push({
- name: "webpack-dev-server-open-editor",
- path: "/webpack-dev-server/open-editor",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- if (req.method !== "GET" && req.method !== "HEAD") {
- next();
- return;
- }
- if (!req.url) {
- next();
- return;
- }
- const resolveUrl = new URL(req.url, `http://${req.headers.host}`);
- const params = new URLSearchParams(resolveUrl.search);
- const fileName = params.get("fileName");
- if (typeof fileName === "string") {
- // @ts-ignore
- const launchEditor = require("launch-editor");
- launchEditor(fileName);
- }
- res.end();
- },
- });
- middlewares.push({
- name: "webpack-dev-server-assets",
- path: "/webpack-dev-server",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- if (req.method !== "GET" && req.method !== "HEAD") {
- next();
- return;
- }
- if (!this.middleware) {
- next();
- return;
- }
- this.middleware.waitUntilValid((stats) => {
- res.setHeader("Content-Type", "text/html; charset=utf-8");
- // HEAD requests should not return body content
- if (req.method === "HEAD") {
- res.end();
- return;
- }
- res.write(
- '<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
- );
- /**
- * @type {StatsCompilation[]}
- */
- const statsForPrint =
- typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
- ? /** @type {NonNullable<StatsCompilation["children"]>} */
- (/** @type {MultiStats} */ (stats).toJson().children)
- : [/** @type {Stats} */ (stats).toJson()];
- res.write(`<h1>Assets Report:</h1>`);
- for (const [index, item] of statsForPrint.entries()) {
- res.write("<div>");
- const name =
- // eslint-disable-next-line no-nested-ternary
- typeof item.name !== "undefined"
- ? item.name
- : /** @type {MultiStats} */ (stats).stats
- ? `unnamed[${index}]`
- : "unnamed";
- res.write(`<h2>Compilation: ${name}</h2>`);
- res.write("<ul>");
- const publicPath =
- item.publicPath === "auto" ? "" : item.publicPath;
- const assets =
- /** @type {NonNullable<StatsCompilation["assets"]>} */
- (item.assets);
- for (const asset of assets) {
- const assetName = asset.name;
- const assetURL = `${publicPath}${assetName}`;
- res.write(
- `<li>
- <strong><a href="${assetURL}" target="_blank">${assetName}</a></strong>
- </li>`,
- );
- }
- res.write("</ul>");
- res.write("</div>");
- }
- res.end("</body></html>");
- });
- },
- });
- if (this.options.proxy) {
- const { createProxyMiddleware } = require("http-proxy-middleware");
- /**
- * @param {ProxyConfigArrayItem} proxyConfig
- * @returns {RequestHandler | undefined}
- */
- const getProxyMiddleware = (proxyConfig) => {
- // It is possible to use the `bypass` method without a `target` or `router`.
- // However, the proxy middleware has no use in this case, and will fail to instantiate.
- if (proxyConfig.target) {
- const context = proxyConfig.context || proxyConfig.path;
- return createProxyMiddleware(
- /** @type {string} */ (context),
- proxyConfig,
- );
- }
- if (proxyConfig.router) {
- return createProxyMiddleware(proxyConfig);
- }
- // TODO improve me after drop `bypass` to always generate error when configuration is bad
- if (!proxyConfig.bypass) {
- util.deprecate(
- () => {},
- `Invalid proxy configuration:\n\n${JSON.stringify(proxyConfig, null, 2)}\n\nThe use of proxy object notation as proxy routes has been removed.\nPlease use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options`,
- "DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT",
- )();
- }
- };
- /**
- * Assume a proxy configuration specified as:
- * proxy: [
- * {
- * context: "value",
- * ...options,
- * },
- * // or:
- * function() {
- * return {
- * context: "context",
- * ...options,
- * };
- * }
- * ]
- */
- this.options.proxy.forEach((proxyConfigOrCallback) => {
- /**
- * @type {RequestHandler}
- */
- let proxyMiddleware;
- let proxyConfig =
- typeof proxyConfigOrCallback === "function"
- ? proxyConfigOrCallback()
- : proxyConfigOrCallback;
- proxyMiddleware =
- /** @type {RequestHandler} */
- (getProxyMiddleware(proxyConfig));
- if (proxyConfig.ws) {
- this.webSocketProxies.push(proxyMiddleware);
- }
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {Promise<void>}
- */
- const handler = async (req, res, next) => {
- if (typeof proxyConfigOrCallback === "function") {
- const newProxyConfig = proxyConfigOrCallback(req, res, next);
- if (newProxyConfig !== proxyConfig) {
- proxyConfig = newProxyConfig;
- const socket = req.socket != null ? req.socket : req.connection;
- // @ts-ignore
- const server = socket != null ? socket.server : null;
- if (server) {
- server.removeAllListeners("close");
- }
- proxyMiddleware =
- /** @type {RequestHandler} */
- (getProxyMiddleware(proxyConfig));
- }
- }
- // - Check if we have a bypass function defined
- // - In case the bypass function is defined we'll retrieve the
- // bypassUrl from it otherwise bypassUrl would be null
- // TODO remove in the next major in favor `context` and `router` options
- const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
- if (isByPassFuncDefined) {
- util.deprecate(
- () => {},
- "Using the 'bypass' option is deprecated. Please use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options",
- "DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT",
- )();
- }
- const bypassUrl = isByPassFuncDefined
- ? await /** @type {ByPass} */ (proxyConfig.bypass)(
- req,
- res,
- proxyConfig,
- )
- : null;
- if (typeof bypassUrl === "boolean") {
- // skip the proxy
- res.statusCode = 404;
- req.url = "";
- next();
- } else if (typeof bypassUrl === "string") {
- // byPass to that url
- req.url = bypassUrl;
- next();
- } else if (proxyMiddleware) {
- return proxyMiddleware(req, res, next);
- } else {
- next();
- }
- };
- middlewares.push({
- name: "http-proxy-middleware",
- middleware: handler,
- });
- // Also forward error requests to the proxy so it can handle them.
- middlewares.push({
- name: "http-proxy-middleware-error-handler",
- middleware:
- /**
- * @param {Error} error
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {any}
- */
- (error, req, res, next) => handler(req, res, next),
- });
- });
- middlewares.push({
- name: "webpack-dev-middleware",
- middleware: /** @type {MiddlewareHandler} */ (this.middleware),
- });
- }
- const staticOptions =
- /** @type {NormalizedStatic[]} */
- (this.options.static);
- if (staticOptions.length > 0) {
- for (const staticOption of staticOptions) {
- for (const publicPath of staticOption.publicPath) {
- middlewares.push({
- name: "express-static",
- path: publicPath,
- middleware: getExpress().static(
- staticOption.directory,
- staticOption.staticOptions,
- ),
- });
- }
- }
- }
- if (this.options.historyApiFallback) {
- const connectHistoryApiFallback = require("connect-history-api-fallback");
- const { historyApiFallback } = this.options;
- if (
- typeof (
- /** @type {ConnectHistoryApiFallbackOptions} */
- (historyApiFallback).logger
- ) === "undefined" &&
- !(
- /** @type {ConnectHistoryApiFallbackOptions} */
- (historyApiFallback).verbose
- )
- ) {
- // @ts-ignore
- historyApiFallback.logger = this.logger.log.bind(
- this.logger,
- "[connect-history-api-fallback]",
- );
- }
- // Fall back to /index.html if nothing else matches.
- middlewares.push({
- name: "connect-history-api-fallback",
- middleware: connectHistoryApiFallback(
- /** @type {ConnectHistoryApiFallbackOptions} */
- (historyApiFallback),
- ),
- });
- // include our middleware to ensure
- // it is able to handle '/index.html' request after redirect
- middlewares.push({
- name: "webpack-dev-middleware",
- middleware: /** @type {MiddlewareHandler} */ (this.middleware),
- });
- if (staticOptions.length > 0) {
- for (const staticOption of staticOptions) {
- for (const publicPath of staticOption.publicPath) {
- middlewares.push({
- name: "express-static",
- path: publicPath,
- middleware: getExpress().static(
- staticOption.directory,
- staticOption.staticOptions,
- ),
- });
- }
- }
- }
- }
- if (staticOptions.length > 0) {
- const serveIndex = require("serve-index");
- for (const staticOption of staticOptions) {
- for (const publicPath of staticOption.publicPath) {
- if (staticOption.serveIndex) {
- middlewares.push({
- name: "serve-index",
- path: publicPath,
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- // serve-index doesn't fallthrough non-get/head request to next middleware
- if (req.method !== "GET" && req.method !== "HEAD") {
- return next();
- }
- serveIndex(
- staticOption.directory,
- /** @type {ServeIndexOptions} */
- (staticOption.serveIndex),
- )(req, res, next);
- },
- });
- }
- }
- }
- }
- // Register this middleware always as the last one so that it's only used as a
- // fallback when no other middleware responses.
- middlewares.push({
- name: "options-middleware",
- /**
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- * @returns {void}
- */
- middleware: (req, res, next) => {
- if (req.method === "OPTIONS") {
- res.statusCode = 204;
- res.setHeader("Content-Length", "0");
- res.end();
- return;
- }
- next();
- },
- });
- if (typeof this.options.setupMiddlewares === "function") {
- middlewares = this.options.setupMiddlewares(middlewares, this);
- }
- // Lazy init webpack dev middleware
- const lazyInitDevMiddleware = () => {
- if (!this.middleware) {
- const webpackDevMiddleware = require("webpack-dev-middleware");
- // middleware for serving webpack bundle
- /** @type {import("webpack-dev-middleware").API<Request, Response>} */
- this.middleware = webpackDevMiddleware(
- this.compiler,
- this.options.devMiddleware,
- );
- }
- return this.middleware;
- };
- for (const i of middlewares) {
- if (i.name === "webpack-dev-middleware") {
- const item = /** @type {MiddlewareObject} */ (i);
- if (typeof item.middleware === "undefined") {
- item.middleware = lazyInitDevMiddleware();
- }
- }
- }
- for (const middleware of middlewares) {
- if (typeof middleware === "function") {
- /** @type {A} */
- (this.app).use(
- /** @type {NextHandleFunction | HandleFunction} */
- (middleware),
- );
- } else if (typeof middleware.path !== "undefined") {
- /** @type {A} */
- (this.app).use(
- middleware.path,
- /** @type {SimpleHandleFunction | NextHandleFunction} */
- (middleware.middleware),
- );
- } else {
- /** @type {A} */
- (this.app).use(
- /** @type {NextHandleFunction | HandleFunction} */
- (middleware.middleware),
- );
- }
- }
- }
- /**
- * @private
- * @returns {Promise<void>}
- */
- async createServer() {
- const { type, options } =
- /** @type {ServerConfiguration<A, S>} */
- (this.options.server);
- if (typeof type === "function") {
- /** @type {S | undefined}*/
- this.server = await type(
- /** @type {ServerOptions} */
- (options),
- /** @type {A} */
- (this.app),
- );
- } else {
- // eslint-disable-next-line import/no-dynamic-require
- const serverType = require(/** @type {string} */ (type));
- /** @type {S | undefined}*/
- this.server =
- type === "http2"
- ? serverType.createSecureServer(
- { ...options, allowHTTP1: true },
- this.app,
- )
- : serverType.createServer(options, this.app);
- }
- this.isTlsServer =
- typeof (
- /** @type {import("tls").Server} */ (this.server).setSecureContext
- ) !== "undefined";
- /** @type {S} */
- (this.server).on(
- "connection",
- /**
- * @param {Socket} socket
- */
- (socket) => {
- // Add socket to list
- this.sockets.push(socket);
- socket.once("close", () => {
- // Remove socket from list
- this.sockets.splice(this.sockets.indexOf(socket), 1);
- });
- },
- );
- /** @type {S} */
- (this.server).on(
- "error",
- /**
- * @param {Error} error
- */
- (error) => {
- throw error;
- },
- );
- }
- /**
- * @private
- * @returns {void}
- */
- createWebSocketServer() {
- /** @type {WebSocketServerImplementation | undefined | null} */
- this.webSocketServer = new (this.getServerTransport())(this);
- /** @type {WebSocketServerImplementation} */
- (this.webSocketServer).implementation.on(
- "connection",
- /**
- * @param {ClientConnection} client
- * @param {IncomingMessage} request
- */
- (client, request) => {
- /** @type {{ [key: string]: string | undefined } | undefined} */
- const headers =
- // eslint-disable-next-line no-nested-ternary
- typeof request !== "undefined"
- ? /** @type {{ [key: string]: string | undefined }} */
- (request.headers)
- : typeof (
- /** @type {import("sockjs").Connection} */ (client).headers
- ) !== "undefined"
- ? /** @type {import("sockjs").Connection} */ (client).headers
- : // eslint-disable-next-line no-undefined
- undefined;
- if (!headers) {
- this.logger.warn(
- 'webSocketServer implementation must pass headers for the "connection" event',
- );
- }
- if (
- !headers ||
- !this.checkHeader(headers, "host") ||
- !this.checkHeader(headers, "origin")
- ) {
- this.sendMessage([client], "error", "Invalid Host/Origin header");
- // With https enabled, the sendMessage above is encrypted asynchronously so not yet sent
- // Terminate would prevent it sending, so use close to allow it to be sent
- client.close();
- return;
- }
- if (this.options.hot === true || this.options.hot === "only") {
- this.sendMessage([client], "hot");
- }
- if (this.options.liveReload) {
- this.sendMessage([client], "liveReload");
- }
- if (
- this.options.client &&
- /** @type {ClientConfiguration} */
- (this.options.client).progress
- ) {
- this.sendMessage(
- [client],
- "progress",
- /** @type {ClientConfiguration} */
- (this.options.client).progress,
- );
- }
- if (
- this.options.client &&
- /** @type {ClientConfiguration} */ (this.options.client).reconnect
- ) {
- this.sendMessage(
- [client],
- "reconnect",
- /** @type {ClientConfiguration} */
- (this.options.client).reconnect,
- );
- }
- if (
- this.options.client &&
- /** @type {ClientConfiguration} */
- (this.options.client).overlay
- ) {
- const overlayConfig = /** @type {ClientConfiguration} */ (
- this.options.client
- ).overlay;
- this.sendMessage(
- [client],
- "overlay",
- typeof overlayConfig === "object"
- ? {
- ...overlayConfig,
- errors:
- overlayConfig.errors &&
- encodeOverlaySettings(overlayConfig.errors),
- warnings:
- overlayConfig.warnings &&
- encodeOverlaySettings(overlayConfig.warnings),
- runtimeErrors:
- overlayConfig.runtimeErrors &&
- encodeOverlaySettings(overlayConfig.runtimeErrors),
- }
- : overlayConfig,
- );
- }
- if (!this.stats) {
- return;
- }
- this.sendStats([client], this.getStats(this.stats), true);
- },
- );
- }
- /**
- * @private
- * @param {string} defaultOpenTarget
- * @returns {Promise<void>}
- */
- async openBrowser(defaultOpenTarget) {
- const open = (await import("open")).default;
- Promise.all(
- /** @type {NormalizedOpen[]} */
- (this.options.open).map((item) => {
- /**
- * @type {string}
- */
- let openTarget;
- if (item.target === "<url>") {
- openTarget = defaultOpenTarget;
- } else {
- openTarget = Server.isAbsoluteURL(item.target)
- ? item.target
- : new URL(item.target, defaultOpenTarget).toString();
- }
- return open(openTarget, item.options).catch(() => {
- this.logger.warn(
- `Unable to open "${openTarget}" page${
- item.options.app
- ? ` in "${
- /** @type {import("open").App} */
- (item.options.app).name
- }" app${
- /** @type {import("open").App} */
- (item.options.app).arguments
- ? ` with "${
- /** @type {import("open").App} */
- (item.options.app).arguments.join(" ")
- }" arguments`
- : ""
- }`
- : ""
- }. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app-name".`,
- );
- });
- }),
- );
- }
- /**
- * @private
- * @returns {void}
- */
- runBonjour() {
- const { Bonjour } = require("bonjour-service");
- const type = this.isTlsServer ? "https" : "http";
- /**
- * @private
- * @type {Bonjour | undefined}
- */
- this.bonjour = new Bonjour();
- this.bonjour.publish({
- name: `Webpack Dev Server ${os.hostname()}:${this.options.port}`,
- port: /** @type {number} */ (this.options.port),
- type,
- subtypes: ["webpack"],
- .../** @type {Partial<BonjourOptions>} */ (this.options.bonjour),
- });
- }
- /**
- * @private
- * @returns {void}
- */
- stopBonjour(callback = () => {}) {
- /** @type {Bonjour} */
- (this.bonjour).unpublishAll(() => {
- /** @type {Bonjour} */
- (this.bonjour).destroy();
- if (callback) {
- callback();
- }
- });
- }
- /**
- * @private
- * @returns {Promise<void>}
- */
- async logStatus() {
- const { isColorSupported, cyan, red } = require("colorette");
- /**
- * @param {Compiler["options"]} compilerOptions
- * @returns {boolean}
- */
- const getColorsOption = (compilerOptions) => {
- /**
- * @type {boolean}
- */
- let colorsEnabled;
- if (
- compilerOptions.stats &&
- typeof (/** @type {StatsOptions} */ (compilerOptions.stats).colors) !==
- "undefined"
- ) {
- colorsEnabled =
- /** @type {boolean} */
- (/** @type {StatsOptions} */ (compilerOptions.stats).colors);
- } else {
- colorsEnabled = isColorSupported;
- }
- return colorsEnabled;
- };
- const colors = {
- /**
- * @param {boolean} useColor
- * @param {string} msg
- * @returns {string}
- */
- info(useColor, msg) {
- if (useColor) {
- return cyan(msg);
- }
- return msg;
- },
- /**
- * @param {boolean} useColor
- * @param {string} msg
- * @returns {string}
- */
- error(useColor, msg) {
- if (useColor) {
- return red(msg);
- }
- return msg;
- },
- };
- const useColor = getColorsOption(this.getCompilerOptions());
- const server = /** @type {S} */ (this.server);
- if (this.options.ipc) {
- this.logger.info(`Project is running at: "${server.address()}"`);
- } else {
- const protocol = this.isTlsServer ? "https" : "http";
- const { address, port } =
- /** @type {import("net").AddressInfo} */
- (server.address());
- /**
- * @param {string} newHostname
- * @returns {string}
- */
- const prettyPrintURL = (newHostname) =>
- url.format({ protocol, hostname: newHostname, port, pathname: "/" });
- let host;
- let localhost;
- let loopbackIPv4;
- let loopbackIPv6;
- let networkUrlIPv4;
- let networkUrlIPv6;
- if (this.options.host) {
- if (this.options.host === "localhost") {
- localhost = prettyPrintURL("localhost");
- } else {
- let isIP;
- try {
- isIP = ipaddr.parse(this.options.host);
- } catch (error) {
- // Ignore
- }
- if (!isIP) {
- host = prettyPrintURL(this.options.host);
- }
- }
- }
- const parsedIP = ipaddr.parse(address);
- if (parsedIP.range() === "unspecified") {
- localhost = prettyPrintURL("localhost");
- loopbackIPv6 = prettyPrintURL("::1");
- const networkIPv4 = Server.findIp("v4", false);
- if (networkIPv4) {
- networkUrlIPv4 = prettyPrintURL(networkIPv4);
- }
- const networkIPv6 = Server.findIp("v6", false);
- if (networkIPv6) {
- networkUrlIPv6 = prettyPrintURL(networkIPv6);
- }
- } else if (parsedIP.range() === "loopback") {
- if (parsedIP.kind() === "ipv4") {
- loopbackIPv4 = prettyPrintURL(parsedIP.toString());
- } else if (parsedIP.kind() === "ipv6") {
- loopbackIPv6 = prettyPrintURL(parsedIP.toString());
- }
- } else {
- networkUrlIPv4 =
- parsedIP.kind() === "ipv6" &&
- /** @type {IPv6} */
- (parsedIP).isIPv4MappedAddress()
- ? prettyPrintURL(
- /** @type {IPv6} */
- (parsedIP).toIPv4Address().toString(),
- )
- : prettyPrintURL(address);
- if (parsedIP.kind() === "ipv6") {
- networkUrlIPv6 = prettyPrintURL(address);
- }
- }
- this.logger.info("Project is running at:");
- if (host) {
- this.logger.info(`Server: ${colors.info(useColor, host)}`);
- }
- if (localhost || loopbackIPv4 || loopbackIPv6) {
- const loopbacks = [];
- if (localhost) {
- loopbacks.push([colors.info(useColor, localhost)]);
- }
- if (loopbackIPv4) {
- loopbacks.push([colors.info(useColor, loopbackIPv4)]);
- }
- if (loopbackIPv6) {
- loopbacks.push([colors.info(useColor, loopbackIPv6)]);
- }
- this.logger.info(`Loopback: ${loopbacks.join(", ")}`);
- }
- if (networkUrlIPv4) {
- this.logger.info(
- `On Your Network (IPv4): ${colors.info(useColor, networkUrlIPv4)}`,
- );
- }
- if (networkUrlIPv6) {
- this.logger.info(
- `On Your Network (IPv6): ${colors.info(useColor, networkUrlIPv6)}`,
- );
- }
- if (/** @type {NormalizedOpen[]} */ (this.options.open).length > 0) {
- const openTarget = prettyPrintURL(
- !this.options.host ||
- this.options.host === "0.0.0.0" ||
- this.options.host === "::"
- ? "localhost"
- : this.options.host,
- );
- await this.openBrowser(openTarget);
- }
- }
- if (/** @type {NormalizedStatic[]} */ (this.options.static).length > 0) {
- this.logger.info(
- `Content not from webpack is served from '${colors.info(
- useColor,
- /** @type {NormalizedStatic[]} */
- (this.options.static)
- .map((staticOption) => staticOption.directory)
- .join(", "),
- )}' directory`,
- );
- }
- if (this.options.historyApiFallback) {
- this.logger.info(
- `404s will fallback to '${colors.info(
- useColor,
- /** @type {ConnectHistoryApiFallbackOptions} */ (
- this.options.historyApiFallback
- ).index || "/index.html",
- )}'`,
- );
- }
- if (this.options.bonjour) {
- const bonjourProtocol =
- /** @type {BonjourOptions} */
- (this.options.bonjour).type || this.isTlsServer ? "https" : "http";
- this.logger.info(
- `Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
- );
- }
- }
- /**
- * @private
- * @param {Request} req
- * @param {Response} res
- * @param {NextFunction} next
- */
- setHeaders(req, res, next) {
- let { headers } = this.options;
- if (headers) {
- if (typeof headers === "function") {
- headers = headers(
- req,
- res,
- // eslint-disable-next-line no-undefined
- this.middleware ? this.middleware.context : undefined,
- );
- }
- /**
- * @type {{key: string, value: string}[]}
- */
- const allHeaders = [];
- allHeaders.push({ key: "X_TEST", value: "TEST" });
- if (!Array.isArray(headers)) {
- // eslint-disable-next-line guard-for-in
- for (const name in headers) {
- // @ts-ignore
- allHeaders.push({ key: name, value: headers[name] });
- }
- headers = allHeaders;
- }
- for (const { key, value } of headers) {
- res.setHeader(key, value);
- }
- }
- next();
- }
- /**
- * @private
- * @param {{ [key: string]: string | undefined }} headers
- * @param {string} headerToCheck
- * @returns {boolean}
- */
- checkHeader(headers, headerToCheck) {
- // allow user to opt out of this security check, at their own risk
- // by explicitly enabling allowedHosts
- if (this.options.allowedHosts === "all") {
- return true;
- }
- // get the Host header and extract hostname
- // we don't care about port not matching
- const hostHeader = headers[headerToCheck];
- if (!hostHeader) {
- return false;
- }
- if (/^(file|.+-extension):/i.test(hostHeader)) {
- return true;
- }
- // use the node url-parser to retrieve the hostname from the host-header.
- const hostname = url.parse(
- // if hostHeader doesn't have scheme, add // for parsing.
- /^(.+:)?\/\//.test(hostHeader) ? hostHeader : `//${hostHeader}`,
- false,
- true,
- ).hostname;
- // always allow requests with explicit IPv4 or IPv6-address.
- // A note on IPv6 addresses:
- // hostHeader will always contain the brackets denoting
- // an IPv6-address in URLs,
- // these are removed from the hostname in url.parse(),
- // so we have the pure IPv6-address in hostname.
- // For convenience, always allow localhost (hostname === 'localhost')
- // and its subdomains (hostname.endsWith(".localhost")).
- // allow hostname of listening address (hostname === this.options.host)
- const isValidHostname =
- (hostname !== null && ipaddr.IPv4.isValid(hostname)) ||
- (hostname !== null && ipaddr.IPv6.isValid(hostname)) ||
- hostname === "localhost" ||
- (hostname !== null && hostname.endsWith(".localhost")) ||
- hostname === this.options.host;
- if (isValidHostname) {
- return true;
- }
- const { allowedHosts } = this.options;
- // always allow localhost host, for convenience
- // allow if hostname is in allowedHosts
- if (Array.isArray(allowedHosts) && allowedHosts.length > 0) {
- for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
- /** @type {string} */
- const allowedHost = allowedHosts[hostIdx];
- if (allowedHost === hostname) {
- return true;
- }
- // support "." as a subdomain wildcard
- // e.g. ".example.com" will allow "example.com", "www.example.com", "subdomain.example.com", etc
- if (allowedHost[0] === ".") {
- // "example.com" (hostname === allowedHost.substring(1))
- // "*.example.com" (hostname.endsWith(allowedHost))
- if (
- hostname === allowedHost.substring(1) ||
- /** @type {string} */ (hostname).endsWith(allowedHost)
- ) {
- return true;
- }
- }
- }
- }
- // Also allow if `client.webSocketURL.hostname` provided
- if (
- this.options.client &&
- typeof (
- /** @type {ClientConfiguration} */ (this.options.client).webSocketURL
- ) !== "undefined"
- ) {
- return (
- /** @type {WebSocketURL} */
- (/** @type {ClientConfiguration} */ (this.options.client).webSocketURL)
- .hostname === hostname
- );
- }
- // disallow
- return false;
- }
- /**
- * @param {ClientConnection[]} clients
- * @param {string} type
- * @param {any} [data]
- * @param {any} [params]
- */
- // eslint-disable-next-line class-methods-use-this
- sendMessage(clients, type, data, params) {
- for (const client of clients) {
- // `sockjs` uses `1` to indicate client is ready to accept data
- // `ws` uses `WebSocket.OPEN`, but it is mean `1` too
- if (client.readyState === 1) {
- client.send(JSON.stringify({ type, data, params }));
- }
- }
- }
- // Send stats to a socket or multiple sockets
- /**
- * @private
- * @param {ClientConnection[]} clients
- * @param {StatsCompilation} stats
- * @param {boolean} [force]
- */
- sendStats(clients, stats, force) {
- const shouldEmit =
- !force &&
- stats &&
- (!stats.errors || stats.errors.length === 0) &&
- (!stats.warnings || stats.warnings.length === 0) &&
- this.currentHash === stats.hash;
- if (shouldEmit) {
- this.sendMessage(clients, "still-ok");
- return;
- }
- this.currentHash = stats.hash;
- this.sendMessage(clients, "hash", stats.hash);
- if (
- /** @type {NonNullable<StatsCompilation["errors"]>} */
- (stats.errors).length > 0 ||
- /** @type {NonNullable<StatsCompilation["warnings"]>} */
- (stats.warnings).length > 0
- ) {
- const hasErrors =
- /** @type {NonNullable<StatsCompilation["errors"]>} */
- (stats.errors).length > 0;
- if (
- /** @type {NonNullable<StatsCompilation["warnings"]>} */
- (stats.warnings).length > 0
- ) {
- let params;
- if (hasErrors) {
- params = { preventReloading: true };
- }
- this.sendMessage(clients, "warnings", stats.warnings, params);
- }
- if (
- /** @type {NonNullable<StatsCompilation["errors"]>} */ (stats.errors)
- .length > 0
- ) {
- this.sendMessage(clients, "errors", stats.errors);
- }
- } else {
- this.sendMessage(clients, "ok");
- }
- }
- /**
- * @param {string | string[]} watchPath
- * @param {WatchOptions} [watchOptions]
- */
- watchFiles(watchPath, watchOptions) {
- const chokidar = require("chokidar");
- const watcher = chokidar.watch(watchPath, watchOptions);
- // disabling refreshing on changing the content
- if (this.options.liveReload) {
- watcher.on("change", (item) => {
- if (this.webSocketServer) {
- this.sendMessage(
- this.webSocketServer.clients,
- "static-changed",
- item,
- );
- }
- });
- }
- this.staticWatchers.push(watcher);
- }
- /**
- * @param {import("webpack-dev-middleware").Callback} [callback]
- */
- invalidate(callback = () => {}) {
- if (this.middleware) {
- this.middleware.invalidate(callback);
- }
- }
- /**
- * @returns {Promise<void>}
- */
- async start() {
- await this.normalizeOptions();
- if (this.options.ipc) {
- await /** @type {Promise<void>} */ (
- new Promise((resolve, reject) => {
- const net = require("net");
- const socket = new net.Socket();
- socket.on(
- "error",
- /**
- * @param {Error & { code?: string }} error
- */
- (error) => {
- if (error.code === "ECONNREFUSED") {
- // No other server listening on this socket, so it can be safely removed
- fs.unlinkSync(/** @type {string} */ (this.options.ipc));
- resolve();
- return;
- } else if (error.code === "ENOENT") {
- resolve();
- return;
- }
- reject(error);
- },
- );
- socket.connect(
- { path: /** @type {string} */ (this.options.ipc) },
- () => {
- throw new Error(`IPC "${this.options.ipc}" is already used`);
- },
- );
- })
- );
- } else {
- this.options.host = await Server.getHostname(
- /** @type {Host} */ (this.options.host),
- );
- this.options.port = await Server.getFreePort(
- /** @type {Port} */ (this.options.port),
- this.options.host,
- );
- }
- await this.initialize();
- const listenOptions = this.options.ipc
- ? { path: this.options.ipc }
- : { host: this.options.host, port: this.options.port };
- await /** @type {Promise<void>} */ (
- new Promise((resolve) => {
- /** @type {S} */
- (this.server).listen(listenOptions, () => {
- resolve();
- });
- })
- );
- if (this.options.ipc) {
- // chmod 666 (rw rw rw)
- const READ_WRITE = 438;
- await fs.promises.chmod(
- /** @type {string} */ (this.options.ipc),
- READ_WRITE,
- );
- }
- if (this.options.webSocketServer) {
- this.createWebSocketServer();
- }
- if (this.options.bonjour) {
- this.runBonjour();
- }
- await this.logStatus();
- if (typeof this.options.onListening === "function") {
- this.options.onListening(this);
- }
- }
- /**
- * @param {(err?: Error) => void} [callback]
- */
- startCallback(callback = () => {}) {
- this.start()
- .then(() => callback(), callback)
- .catch(callback);
- }
- /**
- * @returns {Promise<void>}
- */
- async stop() {
- if (this.bonjour) {
- await /** @type {Promise<void>} */ (
- new Promise((resolve) => {
- this.stopBonjour(() => {
- resolve();
- });
- })
- );
- }
- this.webSocketProxies = [];
- await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
- this.staticWatchers = [];
- if (this.webSocketServer) {
- await /** @type {Promise<void>} */ (
- new Promise((resolve) => {
- /** @type {WebSocketServerImplementation} */
- (this.webSocketServer).implementation.close(() => {
- this.webSocketServer = null;
- resolve();
- });
- for (const client of /** @type {WebSocketServerImplementation} */ (
- this.webSocketServer
- ).clients) {
- client.terminate();
- }
- /** @type {WebSocketServerImplementation} */
- (this.webSocketServer).clients = [];
- })
- );
- }
- if (this.server) {
- await /** @type {Promise<void>} */ (
- new Promise((resolve) => {
- /** @type {S} */
- (this.server).close(() => {
- // eslint-disable-next-line no-undefined
- this.server = undefined;
- resolve();
- });
- for (const socket of this.sockets) {
- socket.destroy();
- }
- this.sockets = [];
- })
- );
- if (this.middleware) {
- await /** @type {Promise<void>} */ (
- new Promise((resolve, reject) => {
- /** @type {import("webpack-dev-middleware").API<Request, Response>}*/
- (this.middleware).close((error) => {
- if (error) {
- reject(error);
- return;
- }
- resolve();
- });
- })
- );
- // eslint-disable-next-line no-undefined
- this.middleware = undefined;
- }
- }
- // We add listeners to signals when creating a new Server instance
- // So ensure they are removed to prevent EventEmitter memory leak warnings
- for (const item of this.listeners) {
- process.removeListener(item.name, item.listener);
- }
- }
- /**
- * @param {(err?: Error) => void} [callback]
- */
- stopCallback(callback = () => {}) {
- this.stop()
- .then(() => callback(), callback)
- .catch(callback);
- }
- }
- module.exports = Server;
|