10
0

Compilation.js 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const {
  8. HookMap,
  9. SyncHook,
  10. SyncBailHook,
  11. SyncWaterfallHook,
  12. AsyncSeriesHook,
  13. AsyncSeriesBailHook,
  14. AsyncParallelHook
  15. } = require("tapable");
  16. const util = require("util");
  17. const { CachedSource } = require("webpack-sources");
  18. const { MultiItemCache } = require("./CacheFacade");
  19. const Chunk = require("./Chunk");
  20. const ChunkGraph = require("./ChunkGraph");
  21. const ChunkGroup = require("./ChunkGroup");
  22. const ChunkRenderError = require("./ChunkRenderError");
  23. const ChunkTemplate = require("./ChunkTemplate");
  24. const CodeGenerationError = require("./CodeGenerationError");
  25. const CodeGenerationResults = require("./CodeGenerationResults");
  26. const Dependency = require("./Dependency");
  27. const DependencyTemplates = require("./DependencyTemplates");
  28. const Entrypoint = require("./Entrypoint");
  29. const ErrorHelpers = require("./ErrorHelpers");
  30. const FileSystemInfo = require("./FileSystemInfo");
  31. const {
  32. connectChunkGroupAndChunk,
  33. connectChunkGroupParentAndChild
  34. } = require("./GraphHelpers");
  35. const {
  36. makeWebpackError,
  37. tryRunOrWebpackError
  38. } = require("./HookWebpackError");
  39. const MainTemplate = require("./MainTemplate");
  40. const Module = require("./Module");
  41. const ModuleDependencyError = require("./ModuleDependencyError");
  42. const ModuleDependencyWarning = require("./ModuleDependencyWarning");
  43. const ModuleGraph = require("./ModuleGraph");
  44. const ModuleHashingError = require("./ModuleHashingError");
  45. const ModuleNotFoundError = require("./ModuleNotFoundError");
  46. const ModuleProfile = require("./ModuleProfile");
  47. const ModuleRestoreError = require("./ModuleRestoreError");
  48. const ModuleStoreError = require("./ModuleStoreError");
  49. const ModuleTemplate = require("./ModuleTemplate");
  50. const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
  51. const RuntimeGlobals = require("./RuntimeGlobals");
  52. const RuntimeTemplate = require("./RuntimeTemplate");
  53. const Stats = require("./Stats");
  54. const WebpackError = require("./WebpackError");
  55. const buildChunkGraph = require("./buildChunkGraph");
  56. const BuildCycleError = require("./errors/BuildCycleError");
  57. const { Logger, LogType } = require("./logging/Logger");
  58. const StatsFactory = require("./stats/StatsFactory");
  59. const StatsPrinter = require("./stats/StatsPrinter");
  60. const { equals: arrayEquals } = require("./util/ArrayHelpers");
  61. const AsyncQueue = require("./util/AsyncQueue");
  62. const LazySet = require("./util/LazySet");
  63. const { getOrInsert } = require("./util/MapHelpers");
  64. const WeakTupleMap = require("./util/WeakTupleMap");
  65. const { cachedCleverMerge } = require("./util/cleverMerge");
  66. const {
  67. compareLocations,
  68. concatComparators,
  69. compareSelect,
  70. compareIds,
  71. compareStringsNumeric,
  72. compareModulesByIdentifier
  73. } = require("./util/comparators");
  74. const createHash = require("./util/createHash");
  75. const {
  76. arrayToSetDeprecation,
  77. soonFrozenObjectDeprecation,
  78. createFakeHook
  79. } = require("./util/deprecation");
  80. const processAsyncTree = require("./util/processAsyncTree");
  81. const { getRuntimeKey } = require("./util/runtime");
  82. const { isSourceEqual } = require("./util/source");
  83. /** @template T @typedef {import("tapable").AsArray<T>} AsArray<T> */
  84. /** @typedef {import("webpack-sources").Source} Source */
  85. /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
  86. /** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputOptions */
  87. /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
  88. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  89. /** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
  90. /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
  91. /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
  92. /** @typedef {import("./Cache")} Cache */
  93. /** @typedef {import("./CacheFacade")} CacheFacade */
  94. /** @typedef {import("./Chunk").ChunkId} ChunkId */
  95. /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
  96. /** @typedef {import("./Compiler")} Compiler */
  97. /** @typedef {import("./Compiler").CompilationParams} CompilationParams */
  98. /** @typedef {import("./Compiler").ModuleMemCachesItem} ModuleMemCachesItem */
  99. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  100. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  101. /** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
  102. /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
  103. /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
  104. /** @typedef {import("./Module").BuildInfo} BuildInfo */
  105. /** @typedef {import("./Module").ValueCacheVersions} ValueCacheVersions */
  106. /** @typedef {import("./NormalModule").NormalModuleCompilationHooks} NormalModuleCompilationHooks */
  107. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  108. /** @typedef {import("./ModuleFactory")} ModuleFactory */
  109. /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
  110. /** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
  111. /** @typedef {import("./ModuleFactory").ModuleFactoryCreateDataContextInfo} ModuleFactoryCreateDataContextInfo */
  112. /** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
  113. /** @typedef {import("./RequestShortener")} RequestShortener */
  114. /** @typedef {import("./RuntimeModule")} RuntimeModule */
  115. /** @typedef {import("./Template").RenderManifestEntry} RenderManifestEntry */
  116. /** @typedef {import("./Template").RenderManifestOptions} RenderManifestOptions */
  117. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
  118. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsError} StatsError */
  119. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModule} StatsModule */
  120. /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
  121. /** @typedef {import("./util/Hash")} Hash */
  122. /** @typedef {import("./util/createHash").Algorithm} Algorithm */
  123. /**
  124. * @template T
  125. * @typedef {import("./util/deprecation").FakeHook<T>} FakeHook<T>
  126. */
  127. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  128. /** @typedef {WeakMap<Dependency, Module>} References */
  129. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  130. /**
  131. * @callback Callback
  132. * @param {(WebpackError | null)=} err
  133. * @returns {void}
  134. */
  135. /**
  136. * @callback ModuleCallback
  137. * @param {(WebpackError | null)=} err
  138. * @param {(Module | null)=} result
  139. * @returns {void}
  140. */
  141. /**
  142. * @callback ModuleFactoryResultCallback
  143. * @param {(WebpackError | null)=} err
  144. * @param {ModuleFactoryResult=} result
  145. * @returns {void}
  146. */
  147. /**
  148. * @callback ModuleOrFactoryResultCallback
  149. * @param {(WebpackError | null)=} err
  150. * @param {Module | ModuleFactoryResult=} result
  151. * @returns {void}
  152. */
  153. /**
  154. * @callback ExecuteModuleCallback
  155. * @param {WebpackError | null} err
  156. * @param {ExecuteModuleResult=} result
  157. * @returns {void}
  158. */
  159. /**
  160. * @callback DepBlockVarDependenciesCallback
  161. * @param {Dependency} dependency
  162. * @returns {any}
  163. */
  164. /** @typedef {new (...args: any[]) => Dependency} DepConstructor */
  165. /** @typedef {Record<string, Source>} CompilationAssets */
  166. /**
  167. * @typedef {object} AvailableModulesChunkGroupMapping
  168. * @property {ChunkGroup} chunkGroup
  169. * @property {Set<Module>} availableModules
  170. * @property {boolean} needCopy
  171. */
  172. /**
  173. * @typedef {object} DependenciesBlockLike
  174. * @property {Dependency[]} dependencies
  175. * @property {AsyncDependenciesBlock[]} blocks
  176. */
  177. /**
  178. * @typedef {object} ChunkPathData
  179. * @property {string|number} id
  180. * @property {string=} name
  181. * @property {string} hash
  182. * @property {function(number): string=} hashWithLength
  183. * @property {(Record<string, string>)=} contentHash
  184. * @property {(Record<string, (length: number) => string>)=} contentHashWithLength
  185. */
  186. /**
  187. * @typedef {object} ChunkHashContext
  188. * @property {CodeGenerationResults} codeGenerationResults results of code generation
  189. * @property {RuntimeTemplate} runtimeTemplate the runtime template
  190. * @property {ModuleGraph} moduleGraph the module graph
  191. * @property {ChunkGraph} chunkGraph the chunk graph
  192. */
  193. /**
  194. * @typedef {object} RuntimeRequirementsContext
  195. * @property {ChunkGraph} chunkGraph the chunk graph
  196. * @property {CodeGenerationResults} codeGenerationResults the code generation results
  197. */
  198. /**
  199. * @typedef {object} ExecuteModuleOptions
  200. * @property {EntryOptions=} entryOptions
  201. */
  202. /**
  203. * @typedef {object} ExecuteModuleResult
  204. * @property {any} exports
  205. * @property {boolean} cacheable
  206. * @property {Map<string, { source: Source, info: AssetInfo }>} assets
  207. * @property {LazySet<string>} fileDependencies
  208. * @property {LazySet<string>} contextDependencies
  209. * @property {LazySet<string>} missingDependencies
  210. * @property {LazySet<string>} buildDependencies
  211. */
  212. /**
  213. * @typedef {{ id: string, exports: any, loaded: boolean }} ModuleObject
  214. *
  215. * /**
  216. * @typedef {object} ExecuteModuleArgument
  217. * @property {Module} module
  218. * @property {ModuleObject=} moduleObject
  219. * @property {any} preparedInfo
  220. * @property {CodeGenerationResult} codeGenerationResult
  221. */
  222. /**
  223. * @typedef {object} ExecuteModuleContext
  224. * @property {Map<string, { source: Source, info: AssetInfo }>} assets
  225. * @property {Chunk} chunk
  226. * @property {ChunkGraph} chunkGraph
  227. * @property {function(string): any=} __webpack_require__
  228. */
  229. /**
  230. * @typedef {object} EntryData
  231. * @property {Dependency[]} dependencies dependencies of the entrypoint that should be evaluated at startup
  232. * @property {Dependency[]} includeDependencies dependencies of the entrypoint that should be included but not evaluated
  233. * @property {EntryOptions} options options of the entrypoint
  234. */
  235. /**
  236. * @typedef {object} LogEntry
  237. * @property {string} type
  238. * @property {any[]=} args
  239. * @property {number} time
  240. * @property {string[]=} trace
  241. */
  242. /**
  243. * @typedef {object} KnownAssetInfo
  244. * @property {boolean=} immutable true, if the asset can be long term cached forever (contains a hash)
  245. * @property {boolean=} minimized whether the asset is minimized
  246. * @property {string | string[]=} fullhash the value(s) of the full hash used for this asset
  247. * @property {string | string[]=} chunkhash the value(s) of the chunk hash used for this asset
  248. * @property {string | string[]=} modulehash the value(s) of the module hash used for this asset
  249. * @property {string | string[]=} contenthash the value(s) of the content hash used for this asset
  250. * @property {string=} sourceFilename when asset was created from a source file (potentially transformed), the original filename relative to compilation context
  251. * @property {number=} size size in bytes, only set after asset has been emitted
  252. * @property {boolean=} development true, when asset is only used for development and doesn't count towards user-facing assets
  253. * @property {boolean=} hotModuleReplacement true, when asset ships data for updating an existing application (HMR)
  254. * @property {boolean=} javascriptModule true, when asset is javascript and an ESM
  255. * @property {Record<string, string | string[]>=} related object of pointers to other assets, keyed by type of relation (only points from parent to child)
  256. */
  257. /** @typedef {KnownAssetInfo & Record<string, any>} AssetInfo */
  258. /** @typedef {{ path: string, info: AssetInfo }} InterpolatedPathAndAssetInfo */
  259. /**
  260. * @typedef {object} Asset
  261. * @property {string} name the filename of the asset
  262. * @property {Source} source source of the asset
  263. * @property {AssetInfo} info info about the asset
  264. */
  265. /**
  266. * @typedef {object} ModulePathData
  267. * @property {string|number} id
  268. * @property {string} hash
  269. * @property {function(number): string=} hashWithLength
  270. */
  271. /**
  272. * @typedef {object} PathData
  273. * @property {ChunkGraph=} chunkGraph
  274. * @property {string=} hash
  275. * @property {function(number): string=} hashWithLength
  276. * @property {(Chunk|ChunkPathData)=} chunk
  277. * @property {(Module|ModulePathData)=} module
  278. * @property {RuntimeSpec=} runtime
  279. * @property {string=} filename
  280. * @property {string=} basename
  281. * @property {string=} query
  282. * @property {string=} contentHashType
  283. * @property {string=} contentHash
  284. * @property {function(number): string=} contentHashWithLength
  285. * @property {boolean=} noChunkHash
  286. * @property {string=} url
  287. */
  288. /**
  289. * @typedef {object} KnownNormalizedStatsOptions
  290. * @property {string} context
  291. * @property {RequestShortener} requestShortener
  292. * @property {string} chunksSort
  293. * @property {string} modulesSort
  294. * @property {string} chunkModulesSort
  295. * @property {string} nestedModulesSort
  296. * @property {string} assetsSort
  297. * @property {boolean} ids
  298. * @property {boolean} cachedAssets
  299. * @property {boolean} groupAssetsByEmitStatus
  300. * @property {boolean} groupAssetsByPath
  301. * @property {boolean} groupAssetsByExtension
  302. * @property {number} assetsSpace
  303. * @property {((value: string, asset: StatsAsset) => boolean)[]} excludeAssets
  304. * @property {((name: string, module: StatsModule, type: "module" | "chunk" | "root-of-chunk" | "nested") => boolean)[]} excludeModules
  305. * @property {((warning: StatsError, textValue: string) => boolean)[]} warningsFilter
  306. * @property {boolean} cachedModules
  307. * @property {boolean} orphanModules
  308. * @property {boolean} dependentModules
  309. * @property {boolean} runtimeModules
  310. * @property {boolean} groupModulesByCacheStatus
  311. * @property {boolean} groupModulesByLayer
  312. * @property {boolean} groupModulesByAttributes
  313. * @property {boolean} groupModulesByPath
  314. * @property {boolean} groupModulesByExtension
  315. * @property {boolean} groupModulesByType
  316. * @property {boolean | "auto"} entrypoints
  317. * @property {boolean} chunkGroups
  318. * @property {boolean} chunkGroupAuxiliary
  319. * @property {boolean} chunkGroupChildren
  320. * @property {number} chunkGroupMaxAssets
  321. * @property {number} modulesSpace
  322. * @property {number} chunkModulesSpace
  323. * @property {number} nestedModulesSpace
  324. * @property {false|"none"|"error"|"warn"|"info"|"log"|"verbose"} logging
  325. * @property {((value: string) => boolean)[]} loggingDebug
  326. * @property {boolean} loggingTrace
  327. * @property {any} _env
  328. */
  329. /** @typedef {KnownNormalizedStatsOptions & Omit<StatsOptions, keyof KnownNormalizedStatsOptions> & Record<string, any>} NormalizedStatsOptions */
  330. /**
  331. * @typedef {object} KnownCreateStatsOptionsContext
  332. * @property {boolean=} forToString
  333. */
  334. /** @typedef {Record<string, any> & KnownCreateStatsOptionsContext} CreateStatsOptionsContext */
  335. /** @typedef {{module: Module, hash: string, runtime: RuntimeSpec, runtimes: RuntimeSpec[]}[]} CodeGenerationJobs */
  336. /** @typedef {{javascript: ModuleTemplate}} ModuleTemplates */
  337. /** @typedef {Set<Module>} NotCodeGeneratedModules */
  338. /** @type {AssetInfo} */
  339. const EMPTY_ASSET_INFO = Object.freeze({});
  340. const esmDependencyCategory = "esm";
  341. // TODO webpack 6: remove
  342. const deprecatedNormalModuleLoaderHook = util.deprecate(
  343. /**
  344. * @param {Compilation} compilation compilation
  345. * @returns {NormalModuleCompilationHooks["loader"]} hooks
  346. */
  347. compilation =>
  348. require("./NormalModule").getCompilationHooks(compilation).loader,
  349. "Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation).loader",
  350. "DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK"
  351. );
  352. // TODO webpack 6: remove
  353. /**
  354. * @param {ModuleTemplates | undefined} moduleTemplates module templates
  355. */
  356. const defineRemovedModuleTemplates = moduleTemplates => {
  357. Object.defineProperties(moduleTemplates, {
  358. asset: {
  359. enumerable: false,
  360. configurable: false,
  361. get: () => {
  362. throw new WebpackError(
  363. "Compilation.moduleTemplates.asset has been removed"
  364. );
  365. }
  366. },
  367. webassembly: {
  368. enumerable: false,
  369. configurable: false,
  370. get: () => {
  371. throw new WebpackError(
  372. "Compilation.moduleTemplates.webassembly has been removed"
  373. );
  374. }
  375. }
  376. });
  377. moduleTemplates = undefined;
  378. };
  379. const byId = compareSelect(c => c.id, compareIds);
  380. const byNameOrHash = concatComparators(
  381. compareSelect(c => c.name, compareIds),
  382. compareSelect(c => c.fullHash, compareIds)
  383. );
  384. const byMessage = compareSelect(err => `${err.message}`, compareStringsNumeric);
  385. const byModule = compareSelect(
  386. err => (err.module && err.module.identifier()) || "",
  387. compareStringsNumeric
  388. );
  389. const byLocation = compareSelect(err => err.loc, compareLocations);
  390. const compareErrors = concatComparators(byModule, byLocation, byMessage);
  391. /** @type {WeakMap<Dependency, Module & { restoreFromUnsafeCache: Function } | null>} */
  392. const unsafeCacheDependencies = new WeakMap();
  393. /** @type {WeakMap<Module & { restoreFromUnsafeCache: Function }, object>} */
  394. const unsafeCacheData = new WeakMap();
  395. class Compilation {
  396. /**
  397. * Creates an instance of Compilation.
  398. * @param {Compiler} compiler the compiler which created the compilation
  399. * @param {CompilationParams} params the compilation parameters
  400. */
  401. constructor(compiler, params) {
  402. this._backCompat = compiler._backCompat;
  403. const getNormalModuleLoader = () => deprecatedNormalModuleLoaderHook(this);
  404. /** @typedef {{ additionalAssets?: true | Function }} ProcessAssetsAdditionalOptions */
  405. /** @type {AsyncSeriesHook<[CompilationAssets], ProcessAssetsAdditionalOptions>} */
  406. const processAssetsHook = new AsyncSeriesHook(["assets"]);
  407. let savedAssets = new Set();
  408. /**
  409. * @param {CompilationAssets} assets assets
  410. * @returns {CompilationAssets} new assets
  411. */
  412. const popNewAssets = assets => {
  413. let newAssets;
  414. for (const file of Object.keys(assets)) {
  415. if (savedAssets.has(file)) continue;
  416. if (newAssets === undefined) {
  417. newAssets = Object.create(null);
  418. }
  419. newAssets[file] = assets[file];
  420. savedAssets.add(file);
  421. }
  422. return newAssets;
  423. };
  424. processAssetsHook.intercept({
  425. name: "Compilation",
  426. call: () => {
  427. savedAssets = new Set(Object.keys(this.assets));
  428. },
  429. register: tap => {
  430. const { type, name } = tap;
  431. const { fn, additionalAssets, ...remainingTap } = tap;
  432. const additionalAssetsFn =
  433. additionalAssets === true ? fn : additionalAssets;
  434. const processedAssets = additionalAssetsFn ? new WeakSet() : undefined;
  435. switch (type) {
  436. case "sync":
  437. if (additionalAssetsFn) {
  438. this.hooks.processAdditionalAssets.tap(name, assets => {
  439. if (processedAssets.has(this.assets))
  440. additionalAssetsFn(assets);
  441. });
  442. }
  443. return {
  444. ...remainingTap,
  445. type: "async",
  446. fn: (assets, callback) => {
  447. try {
  448. fn(assets);
  449. } catch (err) {
  450. return callback(err);
  451. }
  452. if (processedAssets !== undefined)
  453. processedAssets.add(this.assets);
  454. const newAssets = popNewAssets(assets);
  455. if (newAssets !== undefined) {
  456. this.hooks.processAdditionalAssets.callAsync(
  457. newAssets,
  458. callback
  459. );
  460. return;
  461. }
  462. callback();
  463. }
  464. };
  465. case "async":
  466. if (additionalAssetsFn) {
  467. this.hooks.processAdditionalAssets.tapAsync(
  468. name,
  469. (assets, callback) => {
  470. if (processedAssets.has(this.assets))
  471. return additionalAssetsFn(assets, callback);
  472. callback();
  473. }
  474. );
  475. }
  476. return {
  477. ...remainingTap,
  478. fn: (assets, callback) => {
  479. fn(assets, err => {
  480. if (err) return callback(err);
  481. if (processedAssets !== undefined)
  482. processedAssets.add(this.assets);
  483. const newAssets = popNewAssets(assets);
  484. if (newAssets !== undefined) {
  485. this.hooks.processAdditionalAssets.callAsync(
  486. newAssets,
  487. callback
  488. );
  489. return;
  490. }
  491. callback();
  492. });
  493. }
  494. };
  495. case "promise":
  496. if (additionalAssetsFn) {
  497. this.hooks.processAdditionalAssets.tapPromise(name, assets => {
  498. if (processedAssets.has(this.assets))
  499. return additionalAssetsFn(assets);
  500. return Promise.resolve();
  501. });
  502. }
  503. return {
  504. ...remainingTap,
  505. fn: assets => {
  506. const p = fn(assets);
  507. if (!p || !p.then) return p;
  508. return p.then(() => {
  509. if (processedAssets !== undefined)
  510. processedAssets.add(this.assets);
  511. const newAssets = popNewAssets(assets);
  512. if (newAssets !== undefined) {
  513. return this.hooks.processAdditionalAssets.promise(
  514. newAssets
  515. );
  516. }
  517. });
  518. }
  519. };
  520. }
  521. }
  522. });
  523. /** @type {SyncHook<[CompilationAssets]>} */
  524. const afterProcessAssetsHook = new SyncHook(["assets"]);
  525. /**
  526. * @template T
  527. * @param {string} name name of the hook
  528. * @param {number} stage new stage
  529. * @param {function(): AsArray<T>} getArgs get old hook function args
  530. * @param {string=} code deprecation code (not deprecated when unset)
  531. * @returns {FakeHook<Pick<AsyncSeriesHook<T>, "tap" | "tapAsync" | "tapPromise" | "name">>} fake hook which redirects
  532. */
  533. const createProcessAssetsHook = (name, stage, getArgs, code) => {
  534. if (!this._backCompat && code) return;
  535. /**
  536. * @param {string} reason reason
  537. * @returns {string} error message
  538. */
  539. const errorMessage =
  540. reason => `Can't automatically convert plugin using Compilation.hooks.${name} to Compilation.hooks.processAssets because ${reason}.
  541. BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a single Compilation.hooks.processAssets hook.`;
  542. const getOptions = options => {
  543. if (typeof options === "string") options = { name: options };
  544. if (options.stage) {
  545. throw new Error(errorMessage("it's using the 'stage' option"));
  546. }
  547. return { ...options, stage };
  548. };
  549. return createFakeHook(
  550. {
  551. name,
  552. /** @type {AsyncSeriesHook<T>["intercept"]} */
  553. intercept(interceptor) {
  554. throw new Error(errorMessage("it's using 'intercept'"));
  555. },
  556. /** @type {AsyncSeriesHook<T>["tap"]} */
  557. tap: (options, fn) => {
  558. processAssetsHook.tap(getOptions(options), () => fn(...getArgs()));
  559. },
  560. /** @type {AsyncSeriesHook<T>["tapAsync"]} */
  561. tapAsync: (options, fn) => {
  562. processAssetsHook.tapAsync(
  563. getOptions(options),
  564. (assets, callback) =>
  565. /** @type {any} */ (fn)(...getArgs(), callback)
  566. );
  567. },
  568. /** @type {AsyncSeriesHook<T>["tapPromise"]} */
  569. tapPromise: (options, fn) => {
  570. processAssetsHook.tapPromise(getOptions(options), () =>
  571. fn(...getArgs())
  572. );
  573. }
  574. },
  575. `${name} is deprecated (use Compilation.hooks.processAssets instead and use one of Compilation.PROCESS_ASSETS_STAGE_* as stage option)`,
  576. code
  577. );
  578. };
  579. this.hooks = Object.freeze({
  580. /** @type {SyncHook<[Module]>} */
  581. buildModule: new SyncHook(["module"]),
  582. /** @type {SyncHook<[Module]>} */
  583. rebuildModule: new SyncHook(["module"]),
  584. /** @type {SyncHook<[Module, WebpackError]>} */
  585. failedModule: new SyncHook(["module", "error"]),
  586. /** @type {SyncHook<[Module]>} */
  587. succeedModule: new SyncHook(["module"]),
  588. /** @type {SyncHook<[Module]>} */
  589. stillValidModule: new SyncHook(["module"]),
  590. /** @type {SyncHook<[Dependency, EntryOptions]>} */
  591. addEntry: new SyncHook(["entry", "options"]),
  592. /** @type {SyncHook<[Dependency, EntryOptions, Error]>} */
  593. failedEntry: new SyncHook(["entry", "options", "error"]),
  594. /** @type {SyncHook<[Dependency, EntryOptions, Module]>} */
  595. succeedEntry: new SyncHook(["entry", "options", "module"]),
  596. /** @type {SyncWaterfallHook<[(string[] | ReferencedExport)[], Dependency, RuntimeSpec]>} */
  597. dependencyReferencedExports: new SyncWaterfallHook([
  598. "referencedExports",
  599. "dependency",
  600. "runtime"
  601. ]),
  602. /** @type {SyncHook<[ExecuteModuleArgument, ExecuteModuleContext]>} */
  603. executeModule: new SyncHook(["options", "context"]),
  604. /** @type {AsyncParallelHook<[ExecuteModuleArgument, ExecuteModuleContext]>} */
  605. prepareModuleExecution: new AsyncParallelHook(["options", "context"]),
  606. /** @type {AsyncSeriesHook<[Iterable<Module>]>} */
  607. finishModules: new AsyncSeriesHook(["modules"]),
  608. /** @type {AsyncSeriesHook<[Module]>} */
  609. finishRebuildingModule: new AsyncSeriesHook(["module"]),
  610. /** @type {SyncHook<[]>} */
  611. unseal: new SyncHook([]),
  612. /** @type {SyncHook<[]>} */
  613. seal: new SyncHook([]),
  614. /** @type {SyncHook<[]>} */
  615. beforeChunks: new SyncHook([]),
  616. /**
  617. * The `afterChunks` hook is called directly after the chunks and module graph have
  618. * been created and before the chunks and modules have been optimized. This hook is useful to
  619. * inspect, analyze, and/or modify the chunk graph.
  620. * @type {SyncHook<[Iterable<Chunk>]>}
  621. */
  622. afterChunks: new SyncHook(["chunks"]),
  623. /** @type {SyncBailHook<[Iterable<Module>], boolean | void>} */
  624. optimizeDependencies: new SyncBailHook(["modules"]),
  625. /** @type {SyncHook<[Iterable<Module>]>} */
  626. afterOptimizeDependencies: new SyncHook(["modules"]),
  627. /** @type {SyncHook<[]>} */
  628. optimize: new SyncHook([]),
  629. /** @type {SyncBailHook<[Iterable<Module>], boolean | void>} */
  630. optimizeModules: new SyncBailHook(["modules"]),
  631. /** @type {SyncHook<[Iterable<Module>]>} */
  632. afterOptimizeModules: new SyncHook(["modules"]),
  633. /** @type {SyncBailHook<[Iterable<Chunk>, ChunkGroup[]], boolean | void>} */
  634. optimizeChunks: new SyncBailHook(["chunks", "chunkGroups"]),
  635. /** @type {SyncHook<[Iterable<Chunk>, ChunkGroup[]]>} */
  636. afterOptimizeChunks: new SyncHook(["chunks", "chunkGroups"]),
  637. /** @type {AsyncSeriesHook<[Iterable<Chunk>, Iterable<Module>]>} */
  638. optimizeTree: new AsyncSeriesHook(["chunks", "modules"]),
  639. /** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
  640. afterOptimizeTree: new SyncHook(["chunks", "modules"]),
  641. /** @type {AsyncSeriesBailHook<[Iterable<Chunk>, Iterable<Module>], void>} */
  642. optimizeChunkModules: new AsyncSeriesBailHook(["chunks", "modules"]),
  643. /** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
  644. afterOptimizeChunkModules: new SyncHook(["chunks", "modules"]),
  645. /** @type {SyncBailHook<[], boolean | void>} */
  646. shouldRecord: new SyncBailHook([]),
  647. /** @type {SyncHook<[Chunk, Set<string>, RuntimeRequirementsContext]>} */
  648. additionalChunkRuntimeRequirements: new SyncHook([
  649. "chunk",
  650. "runtimeRequirements",
  651. "context"
  652. ]),
  653. /** @type {HookMap<SyncBailHook<[Chunk, Set<string>, RuntimeRequirementsContext], void>>} */
  654. runtimeRequirementInChunk: new HookMap(
  655. () => new SyncBailHook(["chunk", "runtimeRequirements", "context"])
  656. ),
  657. /** @type {SyncHook<[Module, Set<string>, RuntimeRequirementsContext]>} */
  658. additionalModuleRuntimeRequirements: new SyncHook([
  659. "module",
  660. "runtimeRequirements",
  661. "context"
  662. ]),
  663. /** @type {HookMap<SyncBailHook<[Module, Set<string>, RuntimeRequirementsContext], void>>} */
  664. runtimeRequirementInModule: new HookMap(
  665. () => new SyncBailHook(["module", "runtimeRequirements", "context"])
  666. ),
  667. /** @type {SyncHook<[Chunk, Set<string>, RuntimeRequirementsContext]>} */
  668. additionalTreeRuntimeRequirements: new SyncHook([
  669. "chunk",
  670. "runtimeRequirements",
  671. "context"
  672. ]),
  673. /** @type {HookMap<SyncBailHook<[Chunk, Set<string>, RuntimeRequirementsContext], void>>} */
  674. runtimeRequirementInTree: new HookMap(
  675. () => new SyncBailHook(["chunk", "runtimeRequirements", "context"])
  676. ),
  677. /** @type {SyncHook<[RuntimeModule, Chunk]>} */
  678. runtimeModule: new SyncHook(["module", "chunk"]),
  679. /** @type {SyncHook<[Iterable<Module>, any]>} */
  680. reviveModules: new SyncHook(["modules", "records"]),
  681. /** @type {SyncHook<[Iterable<Module>]>} */
  682. beforeModuleIds: new SyncHook(["modules"]),
  683. /** @type {SyncHook<[Iterable<Module>]>} */
  684. moduleIds: new SyncHook(["modules"]),
  685. /** @type {SyncHook<[Iterable<Module>]>} */
  686. optimizeModuleIds: new SyncHook(["modules"]),
  687. /** @type {SyncHook<[Iterable<Module>]>} */
  688. afterOptimizeModuleIds: new SyncHook(["modules"]),
  689. /** @type {SyncHook<[Iterable<Chunk>, any]>} */
  690. reviveChunks: new SyncHook(["chunks", "records"]),
  691. /** @type {SyncHook<[Iterable<Chunk>]>} */
  692. beforeChunkIds: new SyncHook(["chunks"]),
  693. /** @type {SyncHook<[Iterable<Chunk>]>} */
  694. chunkIds: new SyncHook(["chunks"]),
  695. /** @type {SyncHook<[Iterable<Chunk>]>} */
  696. optimizeChunkIds: new SyncHook(["chunks"]),
  697. /** @type {SyncHook<[Iterable<Chunk>]>} */
  698. afterOptimizeChunkIds: new SyncHook(["chunks"]),
  699. /** @type {SyncHook<[Iterable<Module>, any]>} */
  700. recordModules: new SyncHook(["modules", "records"]),
  701. /** @type {SyncHook<[Iterable<Chunk>, any]>} */
  702. recordChunks: new SyncHook(["chunks", "records"]),
  703. /** @type {SyncHook<[Iterable<Module>]>} */
  704. optimizeCodeGeneration: new SyncHook(["modules"]),
  705. /** @type {SyncHook<[]>} */
  706. beforeModuleHash: new SyncHook([]),
  707. /** @type {SyncHook<[]>} */
  708. afterModuleHash: new SyncHook([]),
  709. /** @type {SyncHook<[]>} */
  710. beforeCodeGeneration: new SyncHook([]),
  711. /** @type {SyncHook<[]>} */
  712. afterCodeGeneration: new SyncHook([]),
  713. /** @type {SyncHook<[]>} */
  714. beforeRuntimeRequirements: new SyncHook([]),
  715. /** @type {SyncHook<[]>} */
  716. afterRuntimeRequirements: new SyncHook([]),
  717. /** @type {SyncHook<[]>} */
  718. beforeHash: new SyncHook([]),
  719. /** @type {SyncHook<[Chunk]>} */
  720. contentHash: new SyncHook(["chunk"]),
  721. /** @type {SyncHook<[]>} */
  722. afterHash: new SyncHook([]),
  723. /** @type {SyncHook<[any]>} */
  724. recordHash: new SyncHook(["records"]),
  725. /** @type {SyncHook<[Compilation, any]>} */
  726. record: new SyncHook(["compilation", "records"]),
  727. /** @type {SyncHook<[]>} */
  728. beforeModuleAssets: new SyncHook([]),
  729. /** @type {SyncBailHook<[], boolean | void>} */
  730. shouldGenerateChunkAssets: new SyncBailHook([]),
  731. /** @type {SyncHook<[]>} */
  732. beforeChunkAssets: new SyncHook([]),
  733. // TODO webpack 6 remove
  734. /** @deprecated */
  735. additionalChunkAssets: createProcessAssetsHook(
  736. "additionalChunkAssets",
  737. Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
  738. () => [this.chunks],
  739. "DEP_WEBPACK_COMPILATION_ADDITIONAL_CHUNK_ASSETS"
  740. ),
  741. // TODO webpack 6 deprecate
  742. /** @deprecated */
  743. additionalAssets: createProcessAssetsHook(
  744. "additionalAssets",
  745. Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
  746. () => []
  747. ),
  748. // TODO webpack 6 remove
  749. /** @deprecated */
  750. optimizeChunkAssets: createProcessAssetsHook(
  751. "optimizeChunkAssets",
  752. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
  753. () => [this.chunks],
  754. "DEP_WEBPACK_COMPILATION_OPTIMIZE_CHUNK_ASSETS"
  755. ),
  756. // TODO webpack 6 remove
  757. /** @deprecated */
  758. afterOptimizeChunkAssets: createProcessAssetsHook(
  759. "afterOptimizeChunkAssets",
  760. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE + 1,
  761. () => [this.chunks],
  762. "DEP_WEBPACK_COMPILATION_AFTER_OPTIMIZE_CHUNK_ASSETS"
  763. ),
  764. // TODO webpack 6 deprecate
  765. /** @deprecated */
  766. optimizeAssets: processAssetsHook,
  767. // TODO webpack 6 deprecate
  768. /** @deprecated */
  769. afterOptimizeAssets: afterProcessAssetsHook,
  770. processAssets: processAssetsHook,
  771. afterProcessAssets: afterProcessAssetsHook,
  772. /** @type {AsyncSeriesHook<[CompilationAssets]>} */
  773. processAdditionalAssets: new AsyncSeriesHook(["assets"]),
  774. /** @type {SyncBailHook<[], boolean | void>} */
  775. needAdditionalSeal: new SyncBailHook([]),
  776. /** @type {AsyncSeriesHook<[]>} */
  777. afterSeal: new AsyncSeriesHook([]),
  778. /** @type {SyncWaterfallHook<[RenderManifestEntry[], RenderManifestOptions]>} */
  779. renderManifest: new SyncWaterfallHook(["result", "options"]),
  780. /** @type {SyncHook<[Hash]>} */
  781. fullHash: new SyncHook(["hash"]),
  782. /** @type {SyncHook<[Chunk, Hash, ChunkHashContext]>} */
  783. chunkHash: new SyncHook(["chunk", "chunkHash", "ChunkHashContext"]),
  784. /** @type {SyncHook<[Module, string]>} */
  785. moduleAsset: new SyncHook(["module", "filename"]),
  786. /** @type {SyncHook<[Chunk, string]>} */
  787. chunkAsset: new SyncHook(["chunk", "filename"]),
  788. /** @type {SyncWaterfallHook<[string, object, AssetInfo | undefined]>} */
  789. assetPath: new SyncWaterfallHook(["path", "options", "assetInfo"]),
  790. /** @type {SyncBailHook<[], boolean | void>} */
  791. needAdditionalPass: new SyncBailHook([]),
  792. /** @type {SyncHook<[Compiler, string, number]>} */
  793. childCompiler: new SyncHook([
  794. "childCompiler",
  795. "compilerName",
  796. "compilerIndex"
  797. ]),
  798. /** @type {SyncBailHook<[string, LogEntry], boolean | void>} */
  799. log: new SyncBailHook(["origin", "logEntry"]),
  800. /** @type {SyncWaterfallHook<[WebpackError[]]>} */
  801. processWarnings: new SyncWaterfallHook(["warnings"]),
  802. /** @type {SyncWaterfallHook<[WebpackError[]]>} */
  803. processErrors: new SyncWaterfallHook(["errors"]),
  804. /** @type {HookMap<SyncHook<[Partial<NormalizedStatsOptions>, CreateStatsOptionsContext]>>} */
  805. statsPreset: new HookMap(() => new SyncHook(["options", "context"])),
  806. /** @type {SyncHook<[Partial<NormalizedStatsOptions>, CreateStatsOptionsContext]>} */
  807. statsNormalize: new SyncHook(["options", "context"]),
  808. /** @type {SyncHook<[StatsFactory, NormalizedStatsOptions]>} */
  809. statsFactory: new SyncHook(["statsFactory", "options"]),
  810. /** @type {SyncHook<[StatsPrinter, NormalizedStatsOptions]>} */
  811. statsPrinter: new SyncHook(["statsPrinter", "options"]),
  812. get normalModuleLoader() {
  813. return getNormalModuleLoader();
  814. }
  815. });
  816. /** @type {string=} */
  817. this.name = undefined;
  818. /** @type {number | undefined} */
  819. this.startTime = undefined;
  820. /** @type {number | undefined} */
  821. this.endTime = undefined;
  822. /** @type {Compiler} */
  823. this.compiler = compiler;
  824. this.resolverFactory = compiler.resolverFactory;
  825. /** @type {InputFileSystem} */
  826. this.inputFileSystem =
  827. /** @type {InputFileSystem} */
  828. (compiler.inputFileSystem);
  829. this.fileSystemInfo = new FileSystemInfo(this.inputFileSystem, {
  830. unmanagedPaths: compiler.unmanagedPaths,
  831. managedPaths: compiler.managedPaths,
  832. immutablePaths: compiler.immutablePaths,
  833. logger: this.getLogger("webpack.FileSystemInfo"),
  834. hashFunction: compiler.options.output.hashFunction
  835. });
  836. if (compiler.fileTimestamps) {
  837. this.fileSystemInfo.addFileTimestamps(compiler.fileTimestamps, true);
  838. }
  839. if (compiler.contextTimestamps) {
  840. this.fileSystemInfo.addContextTimestamps(
  841. compiler.contextTimestamps,
  842. true
  843. );
  844. }
  845. /** @type {ValueCacheVersions} */
  846. this.valueCacheVersions = new Map();
  847. this.requestShortener = compiler.requestShortener;
  848. this.compilerPath = compiler.compilerPath;
  849. this.logger = this.getLogger("webpack.Compilation");
  850. const options = /** @type {WebpackOptions} */ (compiler.options);
  851. this.options = options;
  852. this.outputOptions = options && options.output;
  853. /** @type {boolean} */
  854. this.bail = (options && options.bail) || false;
  855. /** @type {boolean} */
  856. this.profile = (options && options.profile) || false;
  857. this.params = params;
  858. this.mainTemplate = new MainTemplate(this.outputOptions, this);
  859. this.chunkTemplate = new ChunkTemplate(this.outputOptions, this);
  860. this.runtimeTemplate = new RuntimeTemplate(
  861. this,
  862. this.outputOptions,
  863. this.requestShortener
  864. );
  865. /** @type {ModuleTemplates} */
  866. this.moduleTemplates = {
  867. javascript: new ModuleTemplate(this.runtimeTemplate, this)
  868. };
  869. defineRemovedModuleTemplates(this.moduleTemplates);
  870. /** @type {Map<Module, WeakTupleMap<any, any>> | undefined} */
  871. this.moduleMemCaches = undefined;
  872. /** @type {Map<Module, WeakTupleMap<any, any>> | undefined} */
  873. this.moduleMemCaches2 = undefined;
  874. this.moduleGraph = new ModuleGraph();
  875. /** @type {ChunkGraph} */
  876. this.chunkGraph = undefined;
  877. /** @type {CodeGenerationResults} */
  878. this.codeGenerationResults = undefined;
  879. /** @type {AsyncQueue<Module, Module, Module>} */
  880. this.processDependenciesQueue = new AsyncQueue({
  881. name: "processDependencies",
  882. parallelism: options.parallelism || 100,
  883. processor: this._processModuleDependencies.bind(this)
  884. });
  885. /** @type {AsyncQueue<Module, string, Module>} */
  886. this.addModuleQueue = new AsyncQueue({
  887. name: "addModule",
  888. parent: this.processDependenciesQueue,
  889. getKey: module => module.identifier(),
  890. processor: this._addModule.bind(this)
  891. });
  892. /** @type {AsyncQueue<FactorizeModuleOptions, string, Module | ModuleFactoryResult>} */
  893. this.factorizeQueue = new AsyncQueue({
  894. name: "factorize",
  895. parent: this.addModuleQueue,
  896. processor: this._factorizeModule.bind(this)
  897. });
  898. /** @type {AsyncQueue<Module, Module, Module>} */
  899. this.buildQueue = new AsyncQueue({
  900. name: "build",
  901. parent: this.factorizeQueue,
  902. processor: this._buildModule.bind(this)
  903. });
  904. /** @type {AsyncQueue<Module, Module, Module>} */
  905. this.rebuildQueue = new AsyncQueue({
  906. name: "rebuild",
  907. parallelism: options.parallelism || 100,
  908. processor: this._rebuildModule.bind(this)
  909. });
  910. /**
  911. * Modules in value are building during the build of Module in key.
  912. * Means value blocking key from finishing.
  913. * Needed to detect build cycles.
  914. * @type {WeakMap<Module, Set<Module>>}
  915. */
  916. this.creatingModuleDuringBuild = new WeakMap();
  917. /** @type {Map<string, EntryData>} */
  918. this.entries = new Map();
  919. /** @type {EntryData} */
  920. this.globalEntry = {
  921. dependencies: [],
  922. includeDependencies: [],
  923. options: {
  924. name: undefined
  925. }
  926. };
  927. /** @type {Map<string, Entrypoint>} */
  928. this.entrypoints = new Map();
  929. /** @type {Entrypoint[]} */
  930. this.asyncEntrypoints = [];
  931. /** @type {Set<Chunk>} */
  932. this.chunks = new Set();
  933. /** @type {ChunkGroup[]} */
  934. this.chunkGroups = [];
  935. /** @type {Map<string, ChunkGroup>} */
  936. this.namedChunkGroups = new Map();
  937. /** @type {Map<string, Chunk>} */
  938. this.namedChunks = new Map();
  939. /** @type {Set<Module>} */
  940. this.modules = new Set();
  941. if (this._backCompat) {
  942. arrayToSetDeprecation(this.chunks, "Compilation.chunks");
  943. arrayToSetDeprecation(this.modules, "Compilation.modules");
  944. }
  945. /**
  946. * @private
  947. * @type {Map<string, Module>}
  948. */
  949. this._modules = new Map();
  950. this.records = null;
  951. /** @type {string[]} */
  952. this.additionalChunkAssets = [];
  953. /** @type {CompilationAssets} */
  954. this.assets = {};
  955. /** @type {Map<string, AssetInfo>} */
  956. this.assetsInfo = new Map();
  957. /** @type {Map<string, Map<string, Set<string>>>} */
  958. this._assetsRelatedIn = new Map();
  959. /** @type {WebpackError[]} */
  960. this.errors = [];
  961. /** @type {WebpackError[]} */
  962. this.warnings = [];
  963. /** @type {Compilation[]} */
  964. this.children = [];
  965. /** @type {Map<string, LogEntry[]>} */
  966. this.logging = new Map();
  967. /** @type {Map<DepConstructor, ModuleFactory>} */
  968. this.dependencyFactories = new Map();
  969. /** @type {DependencyTemplates} */
  970. this.dependencyTemplates = new DependencyTemplates(
  971. this.outputOptions.hashFunction
  972. );
  973. /** @type {Record<string, number>} */
  974. this.childrenCounters = {};
  975. /** @type {Set<number|string>} */
  976. this.usedChunkIds = null;
  977. /** @type {Set<number>} */
  978. this.usedModuleIds = null;
  979. /** @type {boolean} */
  980. this.needAdditionalPass = false;
  981. /** @type {Set<Module & { restoreFromUnsafeCache: Function }>} */
  982. this._restoredUnsafeCacheModuleEntries = new Set();
  983. /** @type {Map<string, Module & { restoreFromUnsafeCache: Function }>} */
  984. this._restoredUnsafeCacheEntries = new Map();
  985. /** @type {WeakSet<Module>} */
  986. this.builtModules = new WeakSet();
  987. /** @type {WeakSet<Module>} */
  988. this.codeGeneratedModules = new WeakSet();
  989. /** @type {WeakSet<Module>} */
  990. this.buildTimeExecutedModules = new WeakSet();
  991. /** @type {Set<string>} */
  992. this.emittedAssets = new Set();
  993. /** @type {Set<string>} */
  994. this.comparedForEmitAssets = new Set();
  995. /** @type {LazySet<string>} */
  996. this.fileDependencies = new LazySet();
  997. /** @type {LazySet<string>} */
  998. this.contextDependencies = new LazySet();
  999. /** @type {LazySet<string>} */
  1000. this.missingDependencies = new LazySet();
  1001. /** @type {LazySet<string>} */
  1002. this.buildDependencies = new LazySet();
  1003. // TODO webpack 6 remove
  1004. this.compilationDependencies = {
  1005. add: util.deprecate(
  1006. /**
  1007. * @param {string} item item
  1008. * @returns {LazySet<string>} file dependencies
  1009. */
  1010. item => this.fileDependencies.add(item),
  1011. "Compilation.compilationDependencies is deprecated (used Compilation.fileDependencies instead)",
  1012. "DEP_WEBPACK_COMPILATION_COMPILATION_DEPENDENCIES"
  1013. )
  1014. };
  1015. this._modulesCache = this.getCache("Compilation/modules");
  1016. this._assetsCache = this.getCache("Compilation/assets");
  1017. this._codeGenerationCache = this.getCache("Compilation/codeGeneration");
  1018. const unsafeCache = options.module.unsafeCache;
  1019. this._unsafeCache = Boolean(unsafeCache);
  1020. this._unsafeCachePredicate =
  1021. typeof unsafeCache === "function" ? unsafeCache : () => true;
  1022. }
  1023. getStats() {
  1024. return new Stats(this);
  1025. }
  1026. /**
  1027. * @param {string | boolean | StatsOptions | undefined} optionsOrPreset stats option value
  1028. * @param {CreateStatsOptionsContext=} context context
  1029. * @returns {NormalizedStatsOptions} normalized options
  1030. */
  1031. createStatsOptions(optionsOrPreset, context = {}) {
  1032. if (typeof optionsOrPreset === "boolean") {
  1033. optionsOrPreset = {
  1034. preset: optionsOrPreset === false ? "none" : "normal"
  1035. };
  1036. } else if (typeof optionsOrPreset === "string") {
  1037. optionsOrPreset = { preset: optionsOrPreset };
  1038. }
  1039. if (typeof optionsOrPreset === "object" && optionsOrPreset !== null) {
  1040. // We use this method of shallow cloning this object to include
  1041. // properties in the prototype chain
  1042. /** @type {Partial<NormalizedStatsOptions>} */
  1043. const options = {};
  1044. // eslint-disable-next-line guard-for-in
  1045. for (const key in optionsOrPreset) {
  1046. options[key] = optionsOrPreset[/** @type {keyof StatsOptions} */ (key)];
  1047. }
  1048. if (options.preset !== undefined) {
  1049. this.hooks.statsPreset.for(options.preset).call(options, context);
  1050. }
  1051. this.hooks.statsNormalize.call(options, context);
  1052. return /** @type {NormalizedStatsOptions} */ (options);
  1053. }
  1054. /** @type {Partial<NormalizedStatsOptions>} */
  1055. const options = {};
  1056. this.hooks.statsNormalize.call(options, context);
  1057. return /** @type {NormalizedStatsOptions} */ (options);
  1058. }
  1059. /**
  1060. * @param {NormalizedStatsOptions} options options
  1061. * @returns {StatsFactory} the stats factory
  1062. */
  1063. createStatsFactory(options) {
  1064. const statsFactory = new StatsFactory();
  1065. this.hooks.statsFactory.call(statsFactory, options);
  1066. return statsFactory;
  1067. }
  1068. /**
  1069. * @param {NormalizedStatsOptions} options options
  1070. * @returns {StatsPrinter} the stats printer
  1071. */
  1072. createStatsPrinter(options) {
  1073. const statsPrinter = new StatsPrinter();
  1074. this.hooks.statsPrinter.call(statsPrinter, options);
  1075. return statsPrinter;
  1076. }
  1077. /**
  1078. * @param {string} name cache name
  1079. * @returns {CacheFacade} the cache facade instance
  1080. */
  1081. getCache(name) {
  1082. return this.compiler.getCache(name);
  1083. }
  1084. /**
  1085. * @param {string | (function(): string)} name name of the logger, or function called once to get the logger name
  1086. * @returns {Logger} a logger with that name
  1087. */
  1088. getLogger(name) {
  1089. if (!name) {
  1090. throw new TypeError("Compilation.getLogger(name) called without a name");
  1091. }
  1092. /** @type {LogEntry[] | undefined} */
  1093. let logEntries;
  1094. return new Logger(
  1095. (type, args) => {
  1096. if (typeof name === "function") {
  1097. name = name();
  1098. if (!name) {
  1099. throw new TypeError(
  1100. "Compilation.getLogger(name) called with a function not returning a name"
  1101. );
  1102. }
  1103. }
  1104. let trace;
  1105. switch (type) {
  1106. case LogType.warn:
  1107. case LogType.error:
  1108. case LogType.trace:
  1109. trace = ErrorHelpers.cutOffLoaderExecution(
  1110. /** @type {string} */ (new Error("Trace").stack)
  1111. )
  1112. .split("\n")
  1113. .slice(3);
  1114. break;
  1115. }
  1116. /** @type {LogEntry} */
  1117. const logEntry = {
  1118. time: Date.now(),
  1119. type,
  1120. args,
  1121. trace
  1122. };
  1123. if (this.hooks.log.call(name, logEntry) === undefined) {
  1124. if (
  1125. logEntry.type === LogType.profileEnd &&
  1126. typeof console.profileEnd === "function"
  1127. ) {
  1128. console.profileEnd(
  1129. `[${name}] ${/** @type {NonNullable<LogEntry["args"]>} */ (logEntry.args)[0]}`
  1130. );
  1131. }
  1132. if (logEntries === undefined) {
  1133. logEntries = this.logging.get(name);
  1134. if (logEntries === undefined) {
  1135. logEntries = [];
  1136. this.logging.set(name, logEntries);
  1137. }
  1138. }
  1139. logEntries.push(logEntry);
  1140. if (
  1141. logEntry.type === LogType.profile &&
  1142. typeof console.profile === "function"
  1143. ) {
  1144. console.profile(
  1145. `[${name}] ${
  1146. /** @type {NonNullable<LogEntry["args"]>} */
  1147. (logEntry.args)[0]
  1148. }`
  1149. );
  1150. }
  1151. }
  1152. },
  1153. childName => {
  1154. if (typeof name === "function") {
  1155. if (typeof childName === "function") {
  1156. return this.getLogger(() => {
  1157. if (typeof name === "function") {
  1158. name = name();
  1159. if (!name) {
  1160. throw new TypeError(
  1161. "Compilation.getLogger(name) called with a function not returning a name"
  1162. );
  1163. }
  1164. }
  1165. if (typeof childName === "function") {
  1166. childName = childName();
  1167. if (!childName) {
  1168. throw new TypeError(
  1169. "Logger.getChildLogger(name) called with a function not returning a name"
  1170. );
  1171. }
  1172. }
  1173. return `${name}/${childName}`;
  1174. });
  1175. }
  1176. return this.getLogger(() => {
  1177. if (typeof name === "function") {
  1178. name = name();
  1179. if (!name) {
  1180. throw new TypeError(
  1181. "Compilation.getLogger(name) called with a function not returning a name"
  1182. );
  1183. }
  1184. }
  1185. return `${name}/${childName}`;
  1186. });
  1187. }
  1188. if (typeof childName === "function") {
  1189. return this.getLogger(() => {
  1190. if (typeof childName === "function") {
  1191. childName = childName();
  1192. if (!childName) {
  1193. throw new TypeError(
  1194. "Logger.getChildLogger(name) called with a function not returning a name"
  1195. );
  1196. }
  1197. }
  1198. return `${name}/${childName}`;
  1199. });
  1200. }
  1201. return this.getLogger(`${name}/${childName}`);
  1202. }
  1203. );
  1204. }
  1205. /**
  1206. * @param {Module} module module to be added that was created
  1207. * @param {ModuleCallback} callback returns the module in the compilation,
  1208. * it could be the passed one (if new), or an already existing in the compilation
  1209. * @returns {void}
  1210. */
  1211. addModule(module, callback) {
  1212. this.addModuleQueue.add(module, callback);
  1213. }
  1214. /**
  1215. * @param {Module} module module to be added that was created
  1216. * @param {ModuleCallback} callback returns the module in the compilation,
  1217. * it could be the passed one (if new), or an already existing in the compilation
  1218. * @returns {void}
  1219. */
  1220. _addModule(module, callback) {
  1221. const identifier = module.identifier();
  1222. const alreadyAddedModule = this._modules.get(identifier);
  1223. if (alreadyAddedModule) {
  1224. return callback(null, alreadyAddedModule);
  1225. }
  1226. const currentProfile = this.profile
  1227. ? this.moduleGraph.getProfile(module)
  1228. : undefined;
  1229. if (currentProfile !== undefined) {
  1230. currentProfile.markRestoringStart();
  1231. }
  1232. this._modulesCache.get(identifier, null, (err, cacheModule) => {
  1233. if (err) return callback(new ModuleRestoreError(module, err));
  1234. if (currentProfile !== undefined) {
  1235. currentProfile.markRestoringEnd();
  1236. currentProfile.markIntegrationStart();
  1237. }
  1238. if (cacheModule) {
  1239. cacheModule.updateCacheModule(module);
  1240. module = cacheModule;
  1241. }
  1242. this._modules.set(identifier, module);
  1243. this.modules.add(module);
  1244. if (this._backCompat)
  1245. ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
  1246. if (currentProfile !== undefined) {
  1247. currentProfile.markIntegrationEnd();
  1248. }
  1249. callback(null, module);
  1250. });
  1251. }
  1252. /**
  1253. * Fetches a module from a compilation by its identifier
  1254. * @param {Module} module the module provided
  1255. * @returns {Module} the module requested
  1256. */
  1257. getModule(module) {
  1258. const identifier = module.identifier();
  1259. return /** @type {Module} */ (this._modules.get(identifier));
  1260. }
  1261. /**
  1262. * Attempts to search for a module by its identifier
  1263. * @param {string} identifier identifier (usually path) for module
  1264. * @returns {Module|undefined} attempt to search for module and return it, else undefined
  1265. */
  1266. findModule(identifier) {
  1267. return this._modules.get(identifier);
  1268. }
  1269. /**
  1270. * Schedules a build of the module object
  1271. * @param {Module} module module to be built
  1272. * @param {ModuleCallback} callback the callback
  1273. * @returns {void}
  1274. */
  1275. buildModule(module, callback) {
  1276. this.buildQueue.add(module, callback);
  1277. }
  1278. /**
  1279. * Builds the module object
  1280. * @param {Module} module module to be built
  1281. * @param {ModuleCallback} callback the callback
  1282. * @returns {void}
  1283. */
  1284. _buildModule(module, callback) {
  1285. const currentProfile = this.profile
  1286. ? this.moduleGraph.getProfile(module)
  1287. : undefined;
  1288. if (currentProfile !== undefined) {
  1289. currentProfile.markBuildingStart();
  1290. }
  1291. module.needBuild(
  1292. {
  1293. compilation: this,
  1294. fileSystemInfo: this.fileSystemInfo,
  1295. valueCacheVersions: this.valueCacheVersions
  1296. },
  1297. (err, needBuild) => {
  1298. if (err) return callback(err);
  1299. if (!needBuild) {
  1300. if (currentProfile !== undefined) {
  1301. currentProfile.markBuildingEnd();
  1302. }
  1303. this.hooks.stillValidModule.call(module);
  1304. return callback();
  1305. }
  1306. this.hooks.buildModule.call(module);
  1307. this.builtModules.add(module);
  1308. module.build(
  1309. this.options,
  1310. this,
  1311. this.resolverFactory.get("normal", module.resolveOptions),
  1312. /** @type {InputFileSystem} */ (this.inputFileSystem),
  1313. err => {
  1314. if (currentProfile !== undefined) {
  1315. currentProfile.markBuildingEnd();
  1316. }
  1317. if (err) {
  1318. this.hooks.failedModule.call(module, err);
  1319. return callback(err);
  1320. }
  1321. if (currentProfile !== undefined) {
  1322. currentProfile.markStoringStart();
  1323. }
  1324. this._modulesCache.store(module.identifier(), null, module, err => {
  1325. if (currentProfile !== undefined) {
  1326. currentProfile.markStoringEnd();
  1327. }
  1328. if (err) {
  1329. this.hooks.failedModule.call(
  1330. module,
  1331. /** @type {WebpackError} */ (err)
  1332. );
  1333. return callback(new ModuleStoreError(module, err));
  1334. }
  1335. this.hooks.succeedModule.call(module);
  1336. return callback();
  1337. });
  1338. }
  1339. );
  1340. }
  1341. );
  1342. }
  1343. /**
  1344. * @param {Module} module to be processed for deps
  1345. * @param {ModuleCallback} callback callback to be triggered
  1346. * @returns {void}
  1347. */
  1348. processModuleDependencies(module, callback) {
  1349. this.processDependenciesQueue.add(module, callback);
  1350. }
  1351. /**
  1352. * @param {Module} module to be processed for deps
  1353. * @returns {void}
  1354. */
  1355. processModuleDependenciesNonRecursive(module) {
  1356. /**
  1357. * @param {DependenciesBlock} block block
  1358. */
  1359. const processDependenciesBlock = block => {
  1360. if (block.dependencies) {
  1361. let i = 0;
  1362. for (const dep of block.dependencies) {
  1363. this.moduleGraph.setParents(dep, block, module, i++);
  1364. }
  1365. }
  1366. if (block.blocks) {
  1367. for (const b of block.blocks) processDependenciesBlock(b);
  1368. }
  1369. };
  1370. processDependenciesBlock(module);
  1371. }
  1372. /**
  1373. * @param {Module} module to be processed for deps
  1374. * @param {ModuleCallback} callback callback to be triggered
  1375. * @returns {void}
  1376. */
  1377. _processModuleDependencies(module, callback) {
  1378. /** @type {Array<{factory: ModuleFactory, dependencies: Dependency[], context: string|undefined, originModule: Module|null}>} */
  1379. const sortedDependencies = [];
  1380. /** @type {DependenciesBlock} */
  1381. let currentBlock;
  1382. /** @type {Map<ModuleFactory, Map<string, Dependency[]>>} */
  1383. let dependencies;
  1384. /** @type {DepConstructor} */
  1385. let factoryCacheKey;
  1386. /** @type {ModuleFactory} */
  1387. let factoryCacheKey2;
  1388. /** @typedef {Map<string, Dependency[]>} FactoryCacheValue */
  1389. /** @type {FactoryCacheValue | undefined} */
  1390. let factoryCacheValue;
  1391. /** @type {string} */
  1392. let listCacheKey1;
  1393. /** @type {string} */
  1394. let listCacheKey2;
  1395. /** @type {Dependency[]} */
  1396. let listCacheValue;
  1397. let inProgressSorting = 1;
  1398. let inProgressTransitive = 1;
  1399. /**
  1400. * @param {WebpackError=} err error
  1401. * @returns {void}
  1402. */
  1403. const onDependenciesSorted = err => {
  1404. if (err) return callback(err);
  1405. // early exit without changing parallelism back and forth
  1406. if (sortedDependencies.length === 0 && inProgressTransitive === 1) {
  1407. return callback();
  1408. }
  1409. // This is nested so we need to allow one additional task
  1410. this.processDependenciesQueue.increaseParallelism();
  1411. for (const item of sortedDependencies) {
  1412. inProgressTransitive++;
  1413. // eslint-disable-next-line no-loop-func
  1414. this.handleModuleCreation(item, err => {
  1415. // In V8, the Error objects keep a reference to the functions on the stack. These warnings &
  1416. // errors are created inside closures that keep a reference to the Compilation, so errors are
  1417. // leaking the Compilation object.
  1418. if (err && this.bail) {
  1419. if (inProgressTransitive <= 0) return;
  1420. inProgressTransitive = -1;
  1421. // eslint-disable-next-line no-self-assign
  1422. err.stack = err.stack;
  1423. onTransitiveTasksFinished(err);
  1424. return;
  1425. }
  1426. if (--inProgressTransitive === 0) onTransitiveTasksFinished();
  1427. });
  1428. }
  1429. if (--inProgressTransitive === 0) onTransitiveTasksFinished();
  1430. };
  1431. /**
  1432. * @param {WebpackError=} err error
  1433. * @returns {void}
  1434. */
  1435. const onTransitiveTasksFinished = err => {
  1436. if (err) return callback(err);
  1437. this.processDependenciesQueue.decreaseParallelism();
  1438. return callback();
  1439. };
  1440. /**
  1441. * @param {Dependency} dep dependency
  1442. * @param {number} index index in block
  1443. * @returns {void}
  1444. */
  1445. const processDependency = (dep, index) => {
  1446. this.moduleGraph.setParents(dep, currentBlock, module, index);
  1447. if (this._unsafeCache) {
  1448. try {
  1449. const unsafeCachedModule = unsafeCacheDependencies.get(dep);
  1450. if (unsafeCachedModule === null) return;
  1451. if (unsafeCachedModule !== undefined) {
  1452. if (
  1453. this._restoredUnsafeCacheModuleEntries.has(unsafeCachedModule)
  1454. ) {
  1455. this._handleExistingModuleFromUnsafeCache(
  1456. module,
  1457. dep,
  1458. unsafeCachedModule
  1459. );
  1460. return;
  1461. }
  1462. const identifier = unsafeCachedModule.identifier();
  1463. const cachedModule =
  1464. this._restoredUnsafeCacheEntries.get(identifier);
  1465. if (cachedModule !== undefined) {
  1466. // update unsafe cache to new module
  1467. unsafeCacheDependencies.set(dep, cachedModule);
  1468. this._handleExistingModuleFromUnsafeCache(
  1469. module,
  1470. dep,
  1471. cachedModule
  1472. );
  1473. return;
  1474. }
  1475. inProgressSorting++;
  1476. this._modulesCache.get(identifier, null, (err, cachedModule) => {
  1477. if (err) {
  1478. if (inProgressSorting <= 0) return;
  1479. inProgressSorting = -1;
  1480. onDependenciesSorted(/** @type {WebpackError} */ (err));
  1481. return;
  1482. }
  1483. try {
  1484. if (!this._restoredUnsafeCacheEntries.has(identifier)) {
  1485. const data = unsafeCacheData.get(cachedModule);
  1486. if (data === undefined) {
  1487. processDependencyForResolving(dep);
  1488. if (--inProgressSorting === 0) onDependenciesSorted();
  1489. return;
  1490. }
  1491. if (cachedModule !== unsafeCachedModule) {
  1492. unsafeCacheDependencies.set(dep, cachedModule);
  1493. }
  1494. cachedModule.restoreFromUnsafeCache(
  1495. data,
  1496. this.params.normalModuleFactory,
  1497. this.params
  1498. );
  1499. this._restoredUnsafeCacheEntries.set(
  1500. identifier,
  1501. cachedModule
  1502. );
  1503. this._restoredUnsafeCacheModuleEntries.add(cachedModule);
  1504. if (!this.modules.has(cachedModule)) {
  1505. inProgressTransitive++;
  1506. this._handleNewModuleFromUnsafeCache(
  1507. module,
  1508. dep,
  1509. cachedModule,
  1510. err => {
  1511. if (err) {
  1512. if (inProgressTransitive <= 0) return;
  1513. inProgressTransitive = -1;
  1514. onTransitiveTasksFinished(err);
  1515. }
  1516. if (--inProgressTransitive === 0)
  1517. return onTransitiveTasksFinished();
  1518. }
  1519. );
  1520. if (--inProgressSorting === 0) onDependenciesSorted();
  1521. return;
  1522. }
  1523. }
  1524. if (unsafeCachedModule !== cachedModule) {
  1525. unsafeCacheDependencies.set(dep, cachedModule);
  1526. }
  1527. this._handleExistingModuleFromUnsafeCache(
  1528. module,
  1529. dep,
  1530. cachedModule
  1531. ); // a3
  1532. } catch (err) {
  1533. if (inProgressSorting <= 0) return;
  1534. inProgressSorting = -1;
  1535. onDependenciesSorted(/** @type {WebpackError} */ (err));
  1536. return;
  1537. }
  1538. if (--inProgressSorting === 0) onDependenciesSorted();
  1539. });
  1540. return;
  1541. }
  1542. } catch (err) {
  1543. console.error(err);
  1544. }
  1545. }
  1546. processDependencyForResolving(dep);
  1547. };
  1548. /**
  1549. * @param {Dependency} dep dependency
  1550. * @returns {void}
  1551. */
  1552. const processDependencyForResolving = dep => {
  1553. const resourceIdent = dep.getResourceIdentifier();
  1554. if (resourceIdent !== undefined && resourceIdent !== null) {
  1555. const category = dep.category;
  1556. const constructor = /** @type {DepConstructor} */ (dep.constructor);
  1557. if (factoryCacheKey === constructor) {
  1558. // Fast path 1: same constructor as prev item
  1559. if (listCacheKey1 === category && listCacheKey2 === resourceIdent) {
  1560. // Super fast path 1: also same resource
  1561. listCacheValue.push(dep);
  1562. return;
  1563. }
  1564. } else {
  1565. const factory = this.dependencyFactories.get(constructor);
  1566. if (factory === undefined) {
  1567. throw new Error(
  1568. `No module factory available for dependency type: ${constructor.name}`
  1569. );
  1570. }
  1571. if (factoryCacheKey2 === factory) {
  1572. // Fast path 2: same factory as prev item
  1573. factoryCacheKey = constructor;
  1574. if (listCacheKey1 === category && listCacheKey2 === resourceIdent) {
  1575. // Super fast path 2: also same resource
  1576. listCacheValue.push(dep);
  1577. return;
  1578. }
  1579. } else {
  1580. // Slow path
  1581. if (factoryCacheKey2 !== undefined) {
  1582. // Archive last cache entry
  1583. if (dependencies === undefined) dependencies = new Map();
  1584. dependencies.set(
  1585. factoryCacheKey2,
  1586. /** @type {FactoryCacheValue} */ (factoryCacheValue)
  1587. );
  1588. factoryCacheValue = dependencies.get(factory);
  1589. if (factoryCacheValue === undefined) {
  1590. factoryCacheValue = new Map();
  1591. }
  1592. } else {
  1593. factoryCacheValue = new Map();
  1594. }
  1595. factoryCacheKey = constructor;
  1596. factoryCacheKey2 = factory;
  1597. }
  1598. }
  1599. // Here webpack is using heuristic that assumes
  1600. // mostly esm dependencies would be used
  1601. // so we don't allocate extra string for them
  1602. const cacheKey =
  1603. category === esmDependencyCategory
  1604. ? resourceIdent
  1605. : `${category}${resourceIdent}`;
  1606. let list = /** @type {FactoryCacheValue} */ (factoryCacheValue).get(
  1607. cacheKey
  1608. );
  1609. if (list === undefined) {
  1610. /** @type {FactoryCacheValue} */
  1611. (factoryCacheValue).set(cacheKey, (list = []));
  1612. sortedDependencies.push({
  1613. factory: factoryCacheKey2,
  1614. dependencies: list,
  1615. context: dep.getContext(),
  1616. originModule: module
  1617. });
  1618. }
  1619. list.push(dep);
  1620. listCacheKey1 = category;
  1621. listCacheKey2 = resourceIdent;
  1622. listCacheValue = list;
  1623. }
  1624. };
  1625. try {
  1626. /** @type {DependenciesBlock[]} */
  1627. const queue = [module];
  1628. do {
  1629. const block = /** @type {DependenciesBlock} */ (queue.pop());
  1630. if (block.dependencies) {
  1631. currentBlock = block;
  1632. let i = 0;
  1633. for (const dep of block.dependencies) processDependency(dep, i++);
  1634. }
  1635. if (block.blocks) {
  1636. for (const b of block.blocks) queue.push(b);
  1637. }
  1638. } while (queue.length !== 0);
  1639. } catch (err) {
  1640. return callback(/** @type {WebpackError} */ (err));
  1641. }
  1642. if (--inProgressSorting === 0) onDependenciesSorted();
  1643. }
  1644. /**
  1645. * @private
  1646. * @param {Module} originModule original module
  1647. * @param {Dependency} dependency dependency
  1648. * @param {Module} module cached module
  1649. * @param {Callback} callback callback
  1650. */
  1651. _handleNewModuleFromUnsafeCache(originModule, dependency, module, callback) {
  1652. const moduleGraph = this.moduleGraph;
  1653. moduleGraph.setResolvedModule(originModule, dependency, module);
  1654. moduleGraph.setIssuerIfUnset(
  1655. module,
  1656. originModule !== undefined ? originModule : null
  1657. );
  1658. this._modules.set(module.identifier(), module);
  1659. this.modules.add(module);
  1660. if (this._backCompat)
  1661. ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
  1662. this._handleModuleBuildAndDependencies(
  1663. originModule,
  1664. module,
  1665. true,
  1666. false,
  1667. callback
  1668. );
  1669. }
  1670. /**
  1671. * @private
  1672. * @param {Module} originModule original modules
  1673. * @param {Dependency} dependency dependency
  1674. * @param {Module} module cached module
  1675. */
  1676. _handleExistingModuleFromUnsafeCache(originModule, dependency, module) {
  1677. const moduleGraph = this.moduleGraph;
  1678. moduleGraph.setResolvedModule(originModule, dependency, module);
  1679. }
  1680. /**
  1681. * @typedef {object} HandleModuleCreationOptions
  1682. * @property {ModuleFactory} factory
  1683. * @property {Dependency[]} dependencies
  1684. * @property {Module | null} originModule
  1685. * @property {Partial<ModuleFactoryCreateDataContextInfo>=} contextInfo
  1686. * @property {string=} context
  1687. * @property {boolean=} recursive recurse into dependencies of the created module
  1688. * @property {boolean=} connectOrigin connect the resolved module with the origin module
  1689. * @property {boolean=} checkCycle check the cycle dependencies of the created module
  1690. */
  1691. /**
  1692. * @param {HandleModuleCreationOptions} options options object
  1693. * @param {ModuleCallback} callback callback
  1694. * @returns {void}
  1695. */
  1696. handleModuleCreation(
  1697. {
  1698. factory,
  1699. dependencies,
  1700. originModule,
  1701. contextInfo,
  1702. context,
  1703. recursive = true,
  1704. connectOrigin = recursive,
  1705. checkCycle = !recursive
  1706. },
  1707. callback
  1708. ) {
  1709. const moduleGraph = this.moduleGraph;
  1710. const currentProfile = this.profile ? new ModuleProfile() : undefined;
  1711. this.factorizeModule(
  1712. {
  1713. currentProfile,
  1714. factory,
  1715. dependencies,
  1716. factoryResult: true,
  1717. originModule,
  1718. contextInfo,
  1719. context
  1720. },
  1721. (err, factoryResult) => {
  1722. const applyFactoryResultDependencies = () => {
  1723. const { fileDependencies, contextDependencies, missingDependencies } =
  1724. /** @type {ModuleFactoryResult} */ (factoryResult);
  1725. if (fileDependencies) {
  1726. this.fileDependencies.addAll(fileDependencies);
  1727. }
  1728. if (contextDependencies) {
  1729. this.contextDependencies.addAll(contextDependencies);
  1730. }
  1731. if (missingDependencies) {
  1732. this.missingDependencies.addAll(missingDependencies);
  1733. }
  1734. };
  1735. if (err) {
  1736. if (factoryResult) applyFactoryResultDependencies();
  1737. if (dependencies.every(d => d.optional)) {
  1738. this.warnings.push(err);
  1739. return callback();
  1740. }
  1741. this.errors.push(err);
  1742. return callback(err);
  1743. }
  1744. const newModule =
  1745. /** @type {ModuleFactoryResult} */
  1746. (factoryResult).module;
  1747. if (!newModule) {
  1748. applyFactoryResultDependencies();
  1749. return callback();
  1750. }
  1751. if (currentProfile !== undefined) {
  1752. moduleGraph.setProfile(newModule, currentProfile);
  1753. }
  1754. this.addModule(newModule, (err, _module) => {
  1755. if (err) {
  1756. applyFactoryResultDependencies();
  1757. if (!err.module) {
  1758. err.module = _module;
  1759. }
  1760. this.errors.push(err);
  1761. return callback(err);
  1762. }
  1763. const module =
  1764. /** @type {Module & { restoreFromUnsafeCache?: Function }} */
  1765. (_module);
  1766. if (
  1767. this._unsafeCache &&
  1768. /** @type {ModuleFactoryResult} */
  1769. (factoryResult).cacheable !== false &&
  1770. module.restoreFromUnsafeCache &&
  1771. this._unsafeCachePredicate(module)
  1772. ) {
  1773. const unsafeCacheableModule =
  1774. /** @type {Module & { restoreFromUnsafeCache: Function }} */
  1775. (module);
  1776. for (let i = 0; i < dependencies.length; i++) {
  1777. const dependency = dependencies[i];
  1778. moduleGraph.setResolvedModule(
  1779. connectOrigin ? originModule : null,
  1780. dependency,
  1781. unsafeCacheableModule
  1782. );
  1783. unsafeCacheDependencies.set(dependency, unsafeCacheableModule);
  1784. }
  1785. if (!unsafeCacheData.has(unsafeCacheableModule)) {
  1786. unsafeCacheData.set(
  1787. unsafeCacheableModule,
  1788. unsafeCacheableModule.getUnsafeCacheData()
  1789. );
  1790. }
  1791. } else {
  1792. applyFactoryResultDependencies();
  1793. for (let i = 0; i < dependencies.length; i++) {
  1794. const dependency = dependencies[i];
  1795. moduleGraph.setResolvedModule(
  1796. connectOrigin ? originModule : null,
  1797. dependency,
  1798. module
  1799. );
  1800. }
  1801. }
  1802. moduleGraph.setIssuerIfUnset(
  1803. module,
  1804. originModule !== undefined ? originModule : null
  1805. );
  1806. if (module !== newModule && currentProfile !== undefined) {
  1807. const otherProfile = moduleGraph.getProfile(module);
  1808. if (otherProfile !== undefined) {
  1809. currentProfile.mergeInto(otherProfile);
  1810. } else {
  1811. moduleGraph.setProfile(module, currentProfile);
  1812. }
  1813. }
  1814. this._handleModuleBuildAndDependencies(
  1815. originModule,
  1816. module,
  1817. recursive,
  1818. checkCycle,
  1819. callback
  1820. );
  1821. });
  1822. }
  1823. );
  1824. }
  1825. /**
  1826. * @private
  1827. * @param {Module} originModule original module
  1828. * @param {Module} module module
  1829. * @param {boolean} recursive true if make it recursive, otherwise false
  1830. * @param {boolean} checkCycle true if need to check cycle, otherwise false
  1831. * @param {ModuleCallback} callback callback
  1832. * @returns {void}
  1833. */
  1834. _handleModuleBuildAndDependencies(
  1835. originModule,
  1836. module,
  1837. recursive,
  1838. checkCycle,
  1839. callback
  1840. ) {
  1841. // Check for cycles when build is trigger inside another build
  1842. /** @type {Set<Module> | undefined} */
  1843. let creatingModuleDuringBuildSet;
  1844. if (checkCycle && this.buildQueue.isProcessing(originModule)) {
  1845. // Track build dependency
  1846. creatingModuleDuringBuildSet =
  1847. this.creatingModuleDuringBuild.get(originModule);
  1848. if (creatingModuleDuringBuildSet === undefined) {
  1849. creatingModuleDuringBuildSet = new Set();
  1850. this.creatingModuleDuringBuild.set(
  1851. originModule,
  1852. creatingModuleDuringBuildSet
  1853. );
  1854. }
  1855. creatingModuleDuringBuildSet.add(module);
  1856. // When building is blocked by another module
  1857. // search for a cycle, cancel the cycle by throwing
  1858. // an error (otherwise this would deadlock)
  1859. const blockReasons = this.creatingModuleDuringBuild.get(module);
  1860. if (blockReasons !== undefined) {
  1861. const set = new Set(blockReasons);
  1862. for (const item of set) {
  1863. const blockReasons = this.creatingModuleDuringBuild.get(item);
  1864. if (blockReasons !== undefined) {
  1865. for (const m of blockReasons) {
  1866. if (m === module) {
  1867. return callback(new BuildCycleError(module));
  1868. }
  1869. set.add(m);
  1870. }
  1871. }
  1872. }
  1873. }
  1874. }
  1875. this.buildModule(module, err => {
  1876. if (creatingModuleDuringBuildSet !== undefined) {
  1877. creatingModuleDuringBuildSet.delete(module);
  1878. }
  1879. if (err) {
  1880. if (!err.module) {
  1881. err.module = module;
  1882. }
  1883. this.errors.push(err);
  1884. return callback(err);
  1885. }
  1886. if (!recursive) {
  1887. this.processModuleDependenciesNonRecursive(module);
  1888. callback(null, module);
  1889. return;
  1890. }
  1891. // This avoids deadlocks for circular dependencies
  1892. if (this.processDependenciesQueue.isProcessing(module)) {
  1893. return callback(null, module);
  1894. }
  1895. this.processModuleDependencies(module, err => {
  1896. if (err) {
  1897. return callback(err);
  1898. }
  1899. callback(null, module);
  1900. });
  1901. });
  1902. }
  1903. /**
  1904. * @param {FactorizeModuleOptions} options options object
  1905. * @param {ModuleOrFactoryResultCallback} callback callback
  1906. * @returns {void}
  1907. */
  1908. _factorizeModule(
  1909. {
  1910. currentProfile,
  1911. factory,
  1912. dependencies,
  1913. originModule,
  1914. factoryResult,
  1915. contextInfo,
  1916. context
  1917. },
  1918. callback
  1919. ) {
  1920. if (currentProfile !== undefined) {
  1921. currentProfile.markFactoryStart();
  1922. }
  1923. factory.create(
  1924. {
  1925. contextInfo: {
  1926. issuer: originModule ? originModule.nameForCondition() : "",
  1927. issuerLayer: originModule ? originModule.layer : null,
  1928. compiler: this.compiler.name,
  1929. ...contextInfo
  1930. },
  1931. resolveOptions: originModule ? originModule.resolveOptions : undefined,
  1932. context:
  1933. context ||
  1934. (originModule ? originModule.context : this.compiler.context),
  1935. dependencies
  1936. },
  1937. (err, result) => {
  1938. if (result) {
  1939. // TODO webpack 6: remove
  1940. // For backward-compat
  1941. if (result.module === undefined && result instanceof Module) {
  1942. result = {
  1943. module: result
  1944. };
  1945. }
  1946. if (!factoryResult) {
  1947. const {
  1948. fileDependencies,
  1949. contextDependencies,
  1950. missingDependencies
  1951. } = result;
  1952. if (fileDependencies) {
  1953. this.fileDependencies.addAll(fileDependencies);
  1954. }
  1955. if (contextDependencies) {
  1956. this.contextDependencies.addAll(contextDependencies);
  1957. }
  1958. if (missingDependencies) {
  1959. this.missingDependencies.addAll(missingDependencies);
  1960. }
  1961. }
  1962. }
  1963. if (err) {
  1964. const notFoundError = new ModuleNotFoundError(
  1965. originModule,
  1966. err,
  1967. /** @type {DependencyLocation} */
  1968. (dependencies.map(d => d.loc).find(Boolean))
  1969. );
  1970. return callback(notFoundError, factoryResult ? result : undefined);
  1971. }
  1972. if (!result) {
  1973. return callback();
  1974. }
  1975. if (currentProfile !== undefined) {
  1976. currentProfile.markFactoryEnd();
  1977. }
  1978. callback(null, factoryResult ? result : result.module);
  1979. }
  1980. );
  1981. }
  1982. /**
  1983. * @param {string} context context string path
  1984. * @param {Dependency} dependency dependency used to create Module chain
  1985. * @param {ModuleCallback} callback callback for when module chain is complete
  1986. * @returns {void} will throw if dependency instance is not a valid Dependency
  1987. */
  1988. addModuleChain(context, dependency, callback) {
  1989. return this.addModuleTree({ context, dependency }, callback);
  1990. }
  1991. /**
  1992. * @param {object} options options
  1993. * @param {string} options.context context string path
  1994. * @param {Dependency} options.dependency dependency used to create Module chain
  1995. * @param {Partial<ModuleFactoryCreateDataContextInfo>=} options.contextInfo additional context info for the root module
  1996. * @param {ModuleCallback} callback callback for when module chain is complete
  1997. * @returns {void} will throw if dependency instance is not a valid Dependency
  1998. */
  1999. addModuleTree({ context, dependency, contextInfo }, callback) {
  2000. if (
  2001. typeof dependency !== "object" ||
  2002. dependency === null ||
  2003. !dependency.constructor
  2004. ) {
  2005. return callback(
  2006. new WebpackError("Parameter 'dependency' must be a Dependency")
  2007. );
  2008. }
  2009. const Dep = /** @type {DepConstructor} */ (dependency.constructor);
  2010. const moduleFactory = this.dependencyFactories.get(Dep);
  2011. if (!moduleFactory) {
  2012. return callback(
  2013. new WebpackError(
  2014. `No dependency factory available for this dependency type: ${dependency.constructor.name}`
  2015. )
  2016. );
  2017. }
  2018. this.handleModuleCreation(
  2019. {
  2020. factory: moduleFactory,
  2021. dependencies: [dependency],
  2022. originModule: null,
  2023. contextInfo,
  2024. context
  2025. },
  2026. (err, result) => {
  2027. if (err && this.bail) {
  2028. callback(err);
  2029. this.buildQueue.stop();
  2030. this.rebuildQueue.stop();
  2031. this.processDependenciesQueue.stop();
  2032. this.factorizeQueue.stop();
  2033. } else if (!err && result) {
  2034. callback(null, result);
  2035. } else {
  2036. callback();
  2037. }
  2038. }
  2039. );
  2040. }
  2041. /**
  2042. * @param {string} context context path for entry
  2043. * @param {Dependency} entry entry dependency that should be followed
  2044. * @param {string | EntryOptions} optionsOrName options or deprecated name of entry
  2045. * @param {ModuleCallback} callback callback function
  2046. * @returns {void} returns
  2047. */
  2048. addEntry(context, entry, optionsOrName, callback) {
  2049. // TODO webpack 6 remove
  2050. const options =
  2051. typeof optionsOrName === "object"
  2052. ? optionsOrName
  2053. : { name: optionsOrName };
  2054. this._addEntryItem(context, entry, "dependencies", options, callback);
  2055. }
  2056. /**
  2057. * @param {string} context context path for entry
  2058. * @param {Dependency} dependency dependency that should be followed
  2059. * @param {EntryOptions} options options
  2060. * @param {ModuleCallback} callback callback function
  2061. * @returns {void} returns
  2062. */
  2063. addInclude(context, dependency, options, callback) {
  2064. this._addEntryItem(
  2065. context,
  2066. dependency,
  2067. "includeDependencies",
  2068. options,
  2069. callback
  2070. );
  2071. }
  2072. /**
  2073. * @param {string} context context path for entry
  2074. * @param {Dependency} entry entry dependency that should be followed
  2075. * @param {"dependencies" | "includeDependencies"} target type of entry
  2076. * @param {EntryOptions} options options
  2077. * @param {ModuleCallback} callback callback function
  2078. * @returns {void} returns
  2079. */
  2080. _addEntryItem(context, entry, target, options, callback) {
  2081. const { name } = options;
  2082. let entryData =
  2083. name !== undefined ? this.entries.get(name) : this.globalEntry;
  2084. if (entryData === undefined) {
  2085. entryData = {
  2086. dependencies: [],
  2087. includeDependencies: [],
  2088. options: {
  2089. name: undefined,
  2090. ...options
  2091. }
  2092. };
  2093. entryData[target].push(entry);
  2094. this.entries.set(
  2095. /** @type {NonNullable<EntryOptions["name"]>} */ (name),
  2096. entryData
  2097. );
  2098. } else {
  2099. entryData[target].push(entry);
  2100. for (const key of Object.keys(options)) {
  2101. if (options[key] === undefined) continue;
  2102. if (entryData.options[key] === options[key]) continue;
  2103. if (
  2104. Array.isArray(entryData.options[key]) &&
  2105. Array.isArray(options[key]) &&
  2106. arrayEquals(entryData.options[key], options[key])
  2107. ) {
  2108. continue;
  2109. }
  2110. if (entryData.options[key] === undefined) {
  2111. entryData.options[key] = options[key];
  2112. } else {
  2113. return callback(
  2114. new WebpackError(
  2115. `Conflicting entry option ${key} = ${entryData.options[key]} vs ${options[key]}`
  2116. )
  2117. );
  2118. }
  2119. }
  2120. }
  2121. this.hooks.addEntry.call(entry, options);
  2122. this.addModuleTree(
  2123. {
  2124. context,
  2125. dependency: entry,
  2126. contextInfo: entryData.options.layer
  2127. ? { issuerLayer: entryData.options.layer }
  2128. : undefined
  2129. },
  2130. (err, module) => {
  2131. if (err) {
  2132. this.hooks.failedEntry.call(entry, options, err);
  2133. return callback(err);
  2134. }
  2135. this.hooks.succeedEntry.call(entry, options, module);
  2136. return callback(null, module);
  2137. }
  2138. );
  2139. }
  2140. /**
  2141. * @param {Module} module module to be rebuilt
  2142. * @param {ModuleCallback} callback callback when module finishes rebuilding
  2143. * @returns {void}
  2144. */
  2145. rebuildModule(module, callback) {
  2146. this.rebuildQueue.add(module, callback);
  2147. }
  2148. /**
  2149. * @param {Module} module module to be rebuilt
  2150. * @param {ModuleCallback} callback callback when module finishes rebuilding
  2151. * @returns {void}
  2152. */
  2153. _rebuildModule(module, callback) {
  2154. this.hooks.rebuildModule.call(module);
  2155. const oldDependencies = module.dependencies.slice();
  2156. const oldBlocks = module.blocks.slice();
  2157. module.invalidateBuild();
  2158. this.buildQueue.invalidate(module);
  2159. this.buildModule(module, err => {
  2160. if (err) {
  2161. return this.hooks.finishRebuildingModule.callAsync(module, err2 => {
  2162. if (err2) {
  2163. callback(
  2164. makeWebpackError(err2, "Compilation.hooks.finishRebuildingModule")
  2165. );
  2166. return;
  2167. }
  2168. callback(err);
  2169. });
  2170. }
  2171. this.processDependenciesQueue.invalidate(module);
  2172. this.moduleGraph.unfreeze();
  2173. this.processModuleDependencies(module, err => {
  2174. if (err) return callback(err);
  2175. this.removeReasonsOfDependencyBlock(module, {
  2176. dependencies: oldDependencies,
  2177. blocks: oldBlocks
  2178. });
  2179. this.hooks.finishRebuildingModule.callAsync(module, err2 => {
  2180. if (err2) {
  2181. callback(
  2182. makeWebpackError(err2, "Compilation.hooks.finishRebuildingModule")
  2183. );
  2184. return;
  2185. }
  2186. callback(null, module);
  2187. });
  2188. });
  2189. });
  2190. }
  2191. /**
  2192. * @private
  2193. * @param {Set<Module>} modules modules
  2194. */
  2195. _computeAffectedModules(modules) {
  2196. const moduleMemCacheCache = this.compiler.moduleMemCaches;
  2197. if (!moduleMemCacheCache) return;
  2198. if (!this.moduleMemCaches) {
  2199. this.moduleMemCaches = new Map();
  2200. this.moduleGraph.setModuleMemCaches(this.moduleMemCaches);
  2201. }
  2202. const { moduleGraph, moduleMemCaches } = this;
  2203. const affectedModules = new Set();
  2204. const infectedModules = new Set();
  2205. let statNew = 0;
  2206. let statChanged = 0;
  2207. let statUnchanged = 0;
  2208. let statReferencesChanged = 0;
  2209. let statWithoutBuild = 0;
  2210. /**
  2211. * @param {Module} module module
  2212. * @returns {References | undefined} references
  2213. */
  2214. const computeReferences = module => {
  2215. /** @type {References | undefined} */
  2216. let references;
  2217. for (const connection of moduleGraph.getOutgoingConnections(module)) {
  2218. const d = connection.dependency;
  2219. const m = connection.module;
  2220. if (!d || !m || unsafeCacheDependencies.has(d)) continue;
  2221. if (references === undefined) references = new WeakMap();
  2222. references.set(d, m);
  2223. }
  2224. return references;
  2225. };
  2226. /**
  2227. * @param {Module} module the module
  2228. * @param {References | undefined} references references
  2229. * @returns {boolean} true, when the references differ
  2230. */
  2231. const compareReferences = (module, references) => {
  2232. if (references === undefined) return true;
  2233. for (const connection of moduleGraph.getOutgoingConnections(module)) {
  2234. const d = connection.dependency;
  2235. if (!d) continue;
  2236. const entry = references.get(d);
  2237. if (entry === undefined) continue;
  2238. if (entry !== connection.module) return false;
  2239. }
  2240. return true;
  2241. };
  2242. const modulesWithoutCache = new Set(modules);
  2243. for (const [module, cachedMemCache] of moduleMemCacheCache) {
  2244. if (modulesWithoutCache.has(module)) {
  2245. const buildInfo = module.buildInfo;
  2246. if (buildInfo) {
  2247. if (cachedMemCache.buildInfo !== buildInfo) {
  2248. // use a new one
  2249. const memCache = new WeakTupleMap();
  2250. moduleMemCaches.set(module, memCache);
  2251. affectedModules.add(module);
  2252. cachedMemCache.buildInfo = buildInfo;
  2253. cachedMemCache.references = computeReferences(module);
  2254. cachedMemCache.memCache = memCache;
  2255. statChanged++;
  2256. } else if (!compareReferences(module, cachedMemCache.references)) {
  2257. // use a new one
  2258. const memCache = new WeakTupleMap();
  2259. moduleMemCaches.set(module, memCache);
  2260. affectedModules.add(module);
  2261. cachedMemCache.references = computeReferences(module);
  2262. cachedMemCache.memCache = memCache;
  2263. statReferencesChanged++;
  2264. } else {
  2265. // keep the old mem cache
  2266. moduleMemCaches.set(module, cachedMemCache.memCache);
  2267. statUnchanged++;
  2268. }
  2269. } else {
  2270. infectedModules.add(module);
  2271. moduleMemCacheCache.delete(module);
  2272. statWithoutBuild++;
  2273. }
  2274. modulesWithoutCache.delete(module);
  2275. } else {
  2276. moduleMemCacheCache.delete(module);
  2277. }
  2278. }
  2279. for (const module of modulesWithoutCache) {
  2280. const buildInfo = module.buildInfo;
  2281. if (buildInfo) {
  2282. // create a new entry
  2283. const memCache = new WeakTupleMap();
  2284. moduleMemCacheCache.set(module, {
  2285. buildInfo,
  2286. references: computeReferences(module),
  2287. memCache
  2288. });
  2289. moduleMemCaches.set(module, memCache);
  2290. affectedModules.add(module);
  2291. statNew++;
  2292. } else {
  2293. infectedModules.add(module);
  2294. statWithoutBuild++;
  2295. }
  2296. }
  2297. /**
  2298. * @param {readonly ModuleGraphConnection[]} connections connections
  2299. * @returns {symbol|boolean} result
  2300. */
  2301. const reduceAffectType = connections => {
  2302. let affected = false;
  2303. for (const { dependency } of connections) {
  2304. if (!dependency) continue;
  2305. const type = dependency.couldAffectReferencingModule();
  2306. if (type === Dependency.TRANSITIVE) return Dependency.TRANSITIVE;
  2307. if (type === false) continue;
  2308. affected = true;
  2309. }
  2310. return affected;
  2311. };
  2312. const directOnlyInfectedModules = new Set();
  2313. for (const module of infectedModules) {
  2314. for (const [
  2315. referencingModule,
  2316. connections
  2317. ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
  2318. if (!referencingModule) continue;
  2319. if (infectedModules.has(referencingModule)) continue;
  2320. const type = reduceAffectType(connections);
  2321. if (!type) continue;
  2322. if (type === true) {
  2323. directOnlyInfectedModules.add(referencingModule);
  2324. } else {
  2325. infectedModules.add(referencingModule);
  2326. }
  2327. }
  2328. }
  2329. for (const module of directOnlyInfectedModules) infectedModules.add(module);
  2330. const directOnlyAffectModules = new Set();
  2331. for (const module of affectedModules) {
  2332. for (const [
  2333. referencingModule,
  2334. connections
  2335. ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
  2336. if (!referencingModule) continue;
  2337. if (infectedModules.has(referencingModule)) continue;
  2338. if (affectedModules.has(referencingModule)) continue;
  2339. const type = reduceAffectType(connections);
  2340. if (!type) continue;
  2341. if (type === true) {
  2342. directOnlyAffectModules.add(referencingModule);
  2343. } else {
  2344. affectedModules.add(referencingModule);
  2345. }
  2346. const memCache = new WeakTupleMap();
  2347. const cache =
  2348. /** @type {ModuleMemCachesItem} */
  2349. (moduleMemCacheCache.get(referencingModule));
  2350. cache.memCache = memCache;
  2351. moduleMemCaches.set(referencingModule, memCache);
  2352. }
  2353. }
  2354. for (const module of directOnlyAffectModules) affectedModules.add(module);
  2355. this.logger.log(
  2356. `${Math.round(
  2357. (100 * (affectedModules.size + infectedModules.size)) /
  2358. this.modules.size
  2359. )}% (${affectedModules.size} affected + ${
  2360. infectedModules.size
  2361. } infected of ${
  2362. this.modules.size
  2363. }) modules flagged as affected (${statNew} new modules, ${statChanged} changed, ${statReferencesChanged} references changed, ${statUnchanged} unchanged, ${statWithoutBuild} were not built)`
  2364. );
  2365. }
  2366. _computeAffectedModulesWithChunkGraph() {
  2367. const { moduleMemCaches } = this;
  2368. if (!moduleMemCaches) return;
  2369. const moduleMemCaches2 = (this.moduleMemCaches2 = new Map());
  2370. const { moduleGraph, chunkGraph } = this;
  2371. const key = "memCache2";
  2372. let statUnchanged = 0;
  2373. let statChanged = 0;
  2374. let statNew = 0;
  2375. /**
  2376. * @param {Module} module module
  2377. * @returns {{ id: ModuleId, modules?: Map<Module, string | number | undefined>, blocks?: (string | number | null)[] }} references
  2378. */
  2379. const computeReferences = module => {
  2380. const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  2381. /** @type {Map<Module, string | number | undefined> | undefined} */
  2382. let modules;
  2383. /** @type {(string | number | null)[] | undefined} */
  2384. let blocks;
  2385. const outgoing = moduleGraph.getOutgoingConnectionsByModule(module);
  2386. if (outgoing !== undefined) {
  2387. for (const m of outgoing.keys()) {
  2388. if (!m) continue;
  2389. if (modules === undefined) modules = new Map();
  2390. modules.set(m, /** @type {ModuleId} */ (chunkGraph.getModuleId(m)));
  2391. }
  2392. }
  2393. if (module.blocks.length > 0) {
  2394. blocks = [];
  2395. const queue = Array.from(module.blocks);
  2396. for (const block of queue) {
  2397. const chunkGroup = chunkGraph.getBlockChunkGroup(block);
  2398. if (chunkGroup) {
  2399. for (const chunk of chunkGroup.chunks) {
  2400. blocks.push(chunk.id);
  2401. }
  2402. } else {
  2403. blocks.push(null);
  2404. }
  2405. // eslint-disable-next-line prefer-spread
  2406. queue.push.apply(queue, block.blocks);
  2407. }
  2408. }
  2409. return { id, modules, blocks };
  2410. };
  2411. /**
  2412. * @param {Module} module module
  2413. * @param {object} references references
  2414. * @param {string | number} references.id id
  2415. * @param {Map<Module, string | number | undefined>=} references.modules modules
  2416. * @param {(string | number | null)[]=} references.blocks blocks
  2417. * @returns {boolean} ok?
  2418. */
  2419. const compareReferences = (module, { id, modules, blocks }) => {
  2420. if (id !== chunkGraph.getModuleId(module)) return false;
  2421. if (modules !== undefined) {
  2422. for (const [module, id] of modules) {
  2423. if (chunkGraph.getModuleId(module) !== id) return false;
  2424. }
  2425. }
  2426. if (blocks !== undefined) {
  2427. const queue = Array.from(module.blocks);
  2428. let i = 0;
  2429. for (const block of queue) {
  2430. const chunkGroup = chunkGraph.getBlockChunkGroup(block);
  2431. if (chunkGroup) {
  2432. for (const chunk of chunkGroup.chunks) {
  2433. if (i >= blocks.length || blocks[i++] !== chunk.id) return false;
  2434. }
  2435. } else if (i >= blocks.length || blocks[i++] !== null) {
  2436. return false;
  2437. }
  2438. // eslint-disable-next-line prefer-spread
  2439. queue.push.apply(queue, block.blocks);
  2440. }
  2441. if (i !== blocks.length) return false;
  2442. }
  2443. return true;
  2444. };
  2445. for (const [module, memCache] of moduleMemCaches) {
  2446. /** @type {{ references: { id: string | number, modules?: Map<Module, string | number | undefined>, blocks?: (string | number | null)[]}, memCache: WeakTupleMap<any[], any> }} */
  2447. const cache = memCache.get(key);
  2448. if (cache === undefined) {
  2449. const memCache2 = new WeakTupleMap();
  2450. memCache.set(key, {
  2451. references: computeReferences(module),
  2452. memCache: memCache2
  2453. });
  2454. moduleMemCaches2.set(module, memCache2);
  2455. statNew++;
  2456. } else if (!compareReferences(module, cache.references)) {
  2457. const memCache = new WeakTupleMap();
  2458. cache.references = computeReferences(module);
  2459. cache.memCache = memCache;
  2460. moduleMemCaches2.set(module, memCache);
  2461. statChanged++;
  2462. } else {
  2463. moduleMemCaches2.set(module, cache.memCache);
  2464. statUnchanged++;
  2465. }
  2466. }
  2467. this.logger.log(
  2468. `${Math.round(
  2469. (100 * statChanged) / (statNew + statChanged + statUnchanged)
  2470. )}% modules flagged as affected by chunk graph (${statNew} new modules, ${statChanged} changed, ${statUnchanged} unchanged)`
  2471. );
  2472. }
  2473. /**
  2474. * @param {Callback} callback callback
  2475. */
  2476. finish(callback) {
  2477. this.factorizeQueue.clear();
  2478. if (this.profile) {
  2479. this.logger.time("finish module profiles");
  2480. const ParallelismFactorCalculator = require("./util/ParallelismFactorCalculator");
  2481. const p = new ParallelismFactorCalculator();
  2482. const moduleGraph = this.moduleGraph;
  2483. /** @type {Map<Module, ModuleProfile>} */
  2484. const modulesWithProfiles = new Map();
  2485. for (const module of this.modules) {
  2486. const profile = moduleGraph.getProfile(module);
  2487. if (!profile) continue;
  2488. modulesWithProfiles.set(module, profile);
  2489. p.range(
  2490. profile.buildingStartTime,
  2491. profile.buildingEndTime,
  2492. f => (profile.buildingParallelismFactor = f)
  2493. );
  2494. p.range(
  2495. profile.factoryStartTime,
  2496. profile.factoryEndTime,
  2497. f => (profile.factoryParallelismFactor = f)
  2498. );
  2499. p.range(
  2500. profile.integrationStartTime,
  2501. profile.integrationEndTime,
  2502. f => (profile.integrationParallelismFactor = f)
  2503. );
  2504. p.range(
  2505. profile.storingStartTime,
  2506. profile.storingEndTime,
  2507. f => (profile.storingParallelismFactor = f)
  2508. );
  2509. p.range(
  2510. profile.restoringStartTime,
  2511. profile.restoringEndTime,
  2512. f => (profile.restoringParallelismFactor = f)
  2513. );
  2514. if (profile.additionalFactoryTimes) {
  2515. for (const { start, end } of profile.additionalFactoryTimes) {
  2516. const influence = (end - start) / profile.additionalFactories;
  2517. p.range(
  2518. start,
  2519. end,
  2520. f =>
  2521. (profile.additionalFactoriesParallelismFactor += f * influence)
  2522. );
  2523. }
  2524. }
  2525. }
  2526. p.calculate();
  2527. const logger = this.getLogger("webpack.Compilation.ModuleProfile");
  2528. // Avoid coverage problems due indirect changes
  2529. /**
  2530. * @param {number} value value
  2531. * @param {string} msg message
  2532. */
  2533. /* istanbul ignore next */
  2534. const logByValue = (value, msg) => {
  2535. if (value > 1000) {
  2536. logger.error(msg);
  2537. } else if (value > 500) {
  2538. logger.warn(msg);
  2539. } else if (value > 200) {
  2540. logger.info(msg);
  2541. } else if (value > 30) {
  2542. logger.log(msg);
  2543. } else {
  2544. logger.debug(msg);
  2545. }
  2546. };
  2547. /**
  2548. * @param {string} category a category
  2549. * @param {(profile: ModuleProfile) => number} getDuration get duration callback
  2550. * @param {(profile: ModuleProfile) => number} getParallelism get parallelism callback
  2551. */
  2552. const logNormalSummary = (category, getDuration, getParallelism) => {
  2553. let sum = 0;
  2554. let max = 0;
  2555. for (const [module, profile] of modulesWithProfiles) {
  2556. const p = getParallelism(profile);
  2557. const d = getDuration(profile);
  2558. if (d === 0 || p === 0) continue;
  2559. const t = d / p;
  2560. sum += t;
  2561. if (t <= 10) continue;
  2562. logByValue(
  2563. t,
  2564. ` | ${Math.round(t)} ms${
  2565. p >= 1.1 ? ` (parallelism ${Math.round(p * 10) / 10})` : ""
  2566. } ${category} > ${module.readableIdentifier(this.requestShortener)}`
  2567. );
  2568. max = Math.max(max, t);
  2569. }
  2570. if (sum <= 10) return;
  2571. logByValue(
  2572. Math.max(sum / 10, max),
  2573. `${Math.round(sum)} ms ${category}`
  2574. );
  2575. };
  2576. /**
  2577. * @param {string} category a category
  2578. * @param {(profile: ModuleProfile) => number} getDuration get duration callback
  2579. * @param {(profile: ModuleProfile) => number} getParallelism get parallelism callback
  2580. */
  2581. const logByLoadersSummary = (category, getDuration, getParallelism) => {
  2582. const map = new Map();
  2583. for (const [module, profile] of modulesWithProfiles) {
  2584. const list = getOrInsert(
  2585. map,
  2586. `${module.type}!${module.identifier().replace(/(!|^)[^!]*$/, "")}`,
  2587. () => []
  2588. );
  2589. list.push({ module, profile });
  2590. }
  2591. let sum = 0;
  2592. let max = 0;
  2593. for (const [key, modules] of map) {
  2594. let innerSum = 0;
  2595. let innerMax = 0;
  2596. for (const { module, profile } of modules) {
  2597. const p = getParallelism(profile);
  2598. const d = getDuration(profile);
  2599. if (d === 0 || p === 0) continue;
  2600. const t = d / p;
  2601. innerSum += t;
  2602. if (t <= 10) continue;
  2603. logByValue(
  2604. t,
  2605. ` | | ${Math.round(t)} ms${
  2606. p >= 1.1 ? ` (parallelism ${Math.round(p * 10) / 10})` : ""
  2607. } ${category} > ${module.readableIdentifier(
  2608. this.requestShortener
  2609. )}`
  2610. );
  2611. innerMax = Math.max(innerMax, t);
  2612. }
  2613. sum += innerSum;
  2614. if (innerSum <= 10) continue;
  2615. const idx = key.indexOf("!");
  2616. const loaders = key.slice(idx + 1);
  2617. const moduleType = key.slice(0, idx);
  2618. const t = Math.max(innerSum / 10, innerMax);
  2619. logByValue(
  2620. t,
  2621. ` | ${Math.round(innerSum)} ms ${category} > ${
  2622. loaders
  2623. ? `${
  2624. modules.length
  2625. } x ${moduleType} with ${this.requestShortener.shorten(
  2626. loaders
  2627. )}`
  2628. : `${modules.length} x ${moduleType}`
  2629. }`
  2630. );
  2631. max = Math.max(max, t);
  2632. }
  2633. if (sum <= 10) return;
  2634. logByValue(
  2635. Math.max(sum / 10, max),
  2636. `${Math.round(sum)} ms ${category}`
  2637. );
  2638. };
  2639. logNormalSummary(
  2640. "resolve to new modules",
  2641. p => p.factory,
  2642. p => p.factoryParallelismFactor
  2643. );
  2644. logNormalSummary(
  2645. "resolve to existing modules",
  2646. p => p.additionalFactories,
  2647. p => p.additionalFactoriesParallelismFactor
  2648. );
  2649. logNormalSummary(
  2650. "integrate modules",
  2651. p => p.restoring,
  2652. p => p.restoringParallelismFactor
  2653. );
  2654. logByLoadersSummary(
  2655. "build modules",
  2656. p => p.building,
  2657. p => p.buildingParallelismFactor
  2658. );
  2659. logNormalSummary(
  2660. "store modules",
  2661. p => p.storing,
  2662. p => p.storingParallelismFactor
  2663. );
  2664. logNormalSummary(
  2665. "restore modules",
  2666. p => p.restoring,
  2667. p => p.restoringParallelismFactor
  2668. );
  2669. this.logger.timeEnd("finish module profiles");
  2670. }
  2671. this.logger.time("compute affected modules");
  2672. this._computeAffectedModules(this.modules);
  2673. this.logger.timeEnd("compute affected modules");
  2674. this.logger.time("finish modules");
  2675. const { modules, moduleMemCaches } = this;
  2676. this.hooks.finishModules.callAsync(modules, err => {
  2677. this.logger.timeEnd("finish modules");
  2678. if (err) return callback(/** @type {WebpackError} */ (err));
  2679. // extract warnings and errors from modules
  2680. this.moduleGraph.freeze("dependency errors");
  2681. // TODO keep a cacheToken (= {}) for each module in the graph
  2682. // create a new one per compilation and flag all updated files
  2683. // and parents with it
  2684. this.logger.time("report dependency errors and warnings");
  2685. for (const module of modules) {
  2686. // TODO only run for modules with changed cacheToken
  2687. // global WeakMap<CacheToken, WeakSet<Module>> to keep modules without errors/warnings
  2688. const memCache = moduleMemCaches && moduleMemCaches.get(module);
  2689. if (memCache && memCache.get("noWarningsOrErrors")) continue;
  2690. let hasProblems = this.reportDependencyErrorsAndWarnings(module, [
  2691. module
  2692. ]);
  2693. const errors = module.getErrors();
  2694. if (errors !== undefined) {
  2695. for (const error of errors) {
  2696. if (!error.module) {
  2697. error.module = module;
  2698. }
  2699. this.errors.push(error);
  2700. hasProblems = true;
  2701. }
  2702. }
  2703. const warnings = module.getWarnings();
  2704. if (warnings !== undefined) {
  2705. for (const warning of warnings) {
  2706. if (!warning.module) {
  2707. warning.module = module;
  2708. }
  2709. this.warnings.push(warning);
  2710. hasProblems = true;
  2711. }
  2712. }
  2713. if (!hasProblems && memCache) memCache.set("noWarningsOrErrors", true);
  2714. }
  2715. this.moduleGraph.unfreeze();
  2716. this.logger.timeEnd("report dependency errors and warnings");
  2717. callback();
  2718. });
  2719. }
  2720. unseal() {
  2721. this.hooks.unseal.call();
  2722. this.chunks.clear();
  2723. this.chunkGroups.length = 0;
  2724. this.namedChunks.clear();
  2725. this.namedChunkGroups.clear();
  2726. this.entrypoints.clear();
  2727. this.additionalChunkAssets.length = 0;
  2728. this.assets = {};
  2729. this.assetsInfo.clear();
  2730. this.moduleGraph.removeAllModuleAttributes();
  2731. this.moduleGraph.unfreeze();
  2732. this.moduleMemCaches2 = undefined;
  2733. }
  2734. /**
  2735. * @param {Callback} callback signals when the call finishes
  2736. * @returns {void}
  2737. */
  2738. seal(callback) {
  2739. /**
  2740. * @param {WebpackError=} err err
  2741. * @returns {void}
  2742. */
  2743. const finalCallback = err => {
  2744. this.factorizeQueue.clear();
  2745. this.buildQueue.clear();
  2746. this.rebuildQueue.clear();
  2747. this.processDependenciesQueue.clear();
  2748. this.addModuleQueue.clear();
  2749. return callback(err);
  2750. };
  2751. const chunkGraph = new ChunkGraph(
  2752. this.moduleGraph,
  2753. this.outputOptions.hashFunction
  2754. );
  2755. this.chunkGraph = chunkGraph;
  2756. if (this._backCompat) {
  2757. for (const module of this.modules) {
  2758. ChunkGraph.setChunkGraphForModule(module, chunkGraph);
  2759. }
  2760. }
  2761. this.hooks.seal.call();
  2762. this.logger.time("optimize dependencies");
  2763. while (this.hooks.optimizeDependencies.call(this.modules)) {
  2764. /* empty */
  2765. }
  2766. this.hooks.afterOptimizeDependencies.call(this.modules);
  2767. this.logger.timeEnd("optimize dependencies");
  2768. this.logger.time("create chunks");
  2769. this.hooks.beforeChunks.call();
  2770. this.moduleGraph.freeze("seal");
  2771. /** @type {Map<Entrypoint, Module[]>} */
  2772. const chunkGraphInit = new Map();
  2773. for (const [name, { dependencies, includeDependencies, options }] of this
  2774. .entries) {
  2775. const chunk = this.addChunk(name);
  2776. if (options.filename) {
  2777. chunk.filenameTemplate = options.filename;
  2778. }
  2779. const entrypoint = new Entrypoint(options);
  2780. if (!options.dependOn && !options.runtime) {
  2781. entrypoint.setRuntimeChunk(chunk);
  2782. }
  2783. entrypoint.setEntrypointChunk(chunk);
  2784. this.namedChunkGroups.set(name, entrypoint);
  2785. this.entrypoints.set(name, entrypoint);
  2786. this.chunkGroups.push(entrypoint);
  2787. connectChunkGroupAndChunk(entrypoint, chunk);
  2788. const entryModules = new Set();
  2789. for (const dep of [...this.globalEntry.dependencies, ...dependencies]) {
  2790. entrypoint.addOrigin(null, { name }, /** @type {any} */ (dep).request);
  2791. const module = this.moduleGraph.getModule(dep);
  2792. if (module) {
  2793. chunkGraph.connectChunkAndEntryModule(chunk, module, entrypoint);
  2794. entryModules.add(module);
  2795. const modulesList = chunkGraphInit.get(entrypoint);
  2796. if (modulesList === undefined) {
  2797. chunkGraphInit.set(entrypoint, [module]);
  2798. } else {
  2799. modulesList.push(module);
  2800. }
  2801. }
  2802. }
  2803. this.assignDepths(entryModules);
  2804. /**
  2805. * @param {Dependency[]} deps deps
  2806. * @returns {Module[]} sorted deps
  2807. */
  2808. const mapAndSort = deps =>
  2809. /** @type {Module[]} */
  2810. (deps.map(dep => this.moduleGraph.getModule(dep)).filter(Boolean)).sort(
  2811. compareModulesByIdentifier
  2812. );
  2813. const includedModules = [
  2814. ...mapAndSort(this.globalEntry.includeDependencies),
  2815. ...mapAndSort(includeDependencies)
  2816. ];
  2817. let modulesList = chunkGraphInit.get(entrypoint);
  2818. if (modulesList === undefined) {
  2819. chunkGraphInit.set(entrypoint, (modulesList = []));
  2820. }
  2821. for (const module of includedModules) {
  2822. this.assignDepth(module);
  2823. modulesList.push(module);
  2824. }
  2825. }
  2826. const runtimeChunks = new Set();
  2827. outer: for (const [
  2828. name,
  2829. {
  2830. options: { dependOn, runtime }
  2831. }
  2832. ] of this.entries) {
  2833. if (dependOn && runtime) {
  2834. const err =
  2835. new WebpackError(`Entrypoint '${name}' has 'dependOn' and 'runtime' specified. This is not valid.
  2836. Entrypoints that depend on other entrypoints do not have their own runtime.
  2837. They will use the runtime(s) from referenced entrypoints instead.
  2838. Remove the 'runtime' option from the entrypoint.`);
  2839. const entry = /** @type {Entrypoint} */ (this.entrypoints.get(name));
  2840. err.chunk = entry.getEntrypointChunk();
  2841. this.errors.push(err);
  2842. }
  2843. if (dependOn) {
  2844. const entry = /** @type {Entrypoint} */ (this.entrypoints.get(name));
  2845. const referencedChunks = entry
  2846. .getEntrypointChunk()
  2847. .getAllReferencedChunks();
  2848. const dependOnEntries = [];
  2849. for (const dep of dependOn) {
  2850. const dependency = this.entrypoints.get(dep);
  2851. if (!dependency) {
  2852. throw new Error(
  2853. `Entry ${name} depends on ${dep}, but this entry was not found`
  2854. );
  2855. }
  2856. if (referencedChunks.has(dependency.getEntrypointChunk())) {
  2857. const err = new WebpackError(
  2858. `Entrypoints '${name}' and '${dep}' use 'dependOn' to depend on each other in a circular way.`
  2859. );
  2860. const entryChunk = entry.getEntrypointChunk();
  2861. err.chunk = entryChunk;
  2862. this.errors.push(err);
  2863. entry.setRuntimeChunk(entryChunk);
  2864. continue outer;
  2865. }
  2866. dependOnEntries.push(dependency);
  2867. }
  2868. for (const dependency of dependOnEntries) {
  2869. connectChunkGroupParentAndChild(dependency, entry);
  2870. }
  2871. } else if (runtime) {
  2872. const entry = /** @type {Entrypoint} */ (this.entrypoints.get(name));
  2873. let chunk = this.namedChunks.get(runtime);
  2874. if (chunk) {
  2875. if (!runtimeChunks.has(chunk)) {
  2876. const err =
  2877. new WebpackError(`Entrypoint '${name}' has a 'runtime' option which points to another entrypoint named '${runtime}'.
  2878. It's not valid to use other entrypoints as runtime chunk.
  2879. Did you mean to use 'dependOn: ${JSON.stringify(
  2880. runtime
  2881. )}' instead to allow using entrypoint '${name}' within the runtime of entrypoint '${runtime}'? For this '${runtime}' must always be loaded when '${name}' is used.
  2882. Or do you want to use the entrypoints '${name}' and '${runtime}' independently on the same page with a shared runtime? In this case give them both the same value for the 'runtime' option. It must be a name not already used by an entrypoint.`);
  2883. const entryChunk =
  2884. /** @type {Chunk} */
  2885. (entry.getEntrypointChunk());
  2886. err.chunk = entryChunk;
  2887. this.errors.push(err);
  2888. entry.setRuntimeChunk(entryChunk);
  2889. continue;
  2890. }
  2891. } else {
  2892. chunk = this.addChunk(runtime);
  2893. chunk.preventIntegration = true;
  2894. runtimeChunks.add(chunk);
  2895. }
  2896. entry.unshiftChunk(chunk);
  2897. chunk.addGroup(entry);
  2898. entry.setRuntimeChunk(chunk);
  2899. }
  2900. }
  2901. buildChunkGraph(this, chunkGraphInit);
  2902. this.hooks.afterChunks.call(this.chunks);
  2903. this.logger.timeEnd("create chunks");
  2904. this.logger.time("optimize");
  2905. this.hooks.optimize.call();
  2906. while (this.hooks.optimizeModules.call(this.modules)) {
  2907. /* empty */
  2908. }
  2909. this.hooks.afterOptimizeModules.call(this.modules);
  2910. while (this.hooks.optimizeChunks.call(this.chunks, this.chunkGroups)) {
  2911. /* empty */
  2912. }
  2913. this.hooks.afterOptimizeChunks.call(this.chunks, this.chunkGroups);
  2914. this.hooks.optimizeTree.callAsync(this.chunks, this.modules, err => {
  2915. if (err) {
  2916. return finalCallback(
  2917. makeWebpackError(err, "Compilation.hooks.optimizeTree")
  2918. );
  2919. }
  2920. this.hooks.afterOptimizeTree.call(this.chunks, this.modules);
  2921. this.hooks.optimizeChunkModules.callAsync(
  2922. this.chunks,
  2923. this.modules,
  2924. err => {
  2925. if (err) {
  2926. return finalCallback(
  2927. makeWebpackError(err, "Compilation.hooks.optimizeChunkModules")
  2928. );
  2929. }
  2930. this.hooks.afterOptimizeChunkModules.call(this.chunks, this.modules);
  2931. const shouldRecord = this.hooks.shouldRecord.call() !== false;
  2932. this.hooks.reviveModules.call(this.modules, this.records);
  2933. this.hooks.beforeModuleIds.call(this.modules);
  2934. this.hooks.moduleIds.call(this.modules);
  2935. this.hooks.optimizeModuleIds.call(this.modules);
  2936. this.hooks.afterOptimizeModuleIds.call(this.modules);
  2937. this.hooks.reviveChunks.call(this.chunks, this.records);
  2938. this.hooks.beforeChunkIds.call(this.chunks);
  2939. this.hooks.chunkIds.call(this.chunks);
  2940. this.hooks.optimizeChunkIds.call(this.chunks);
  2941. this.hooks.afterOptimizeChunkIds.call(this.chunks);
  2942. this.assignRuntimeIds();
  2943. this.logger.time("compute affected modules with chunk graph");
  2944. this._computeAffectedModulesWithChunkGraph();
  2945. this.logger.timeEnd("compute affected modules with chunk graph");
  2946. this.sortItemsWithChunkIds();
  2947. if (shouldRecord) {
  2948. this.hooks.recordModules.call(this.modules, this.records);
  2949. this.hooks.recordChunks.call(this.chunks, this.records);
  2950. }
  2951. this.hooks.optimizeCodeGeneration.call(this.modules);
  2952. this.logger.timeEnd("optimize");
  2953. this.logger.time("module hashing");
  2954. this.hooks.beforeModuleHash.call();
  2955. this.createModuleHashes();
  2956. this.hooks.afterModuleHash.call();
  2957. this.logger.timeEnd("module hashing");
  2958. this.logger.time("code generation");
  2959. this.hooks.beforeCodeGeneration.call();
  2960. this.codeGeneration(err => {
  2961. if (err) {
  2962. return finalCallback(err);
  2963. }
  2964. this.hooks.afterCodeGeneration.call();
  2965. this.logger.timeEnd("code generation");
  2966. this.logger.time("runtime requirements");
  2967. this.hooks.beforeRuntimeRequirements.call();
  2968. this.processRuntimeRequirements();
  2969. this.hooks.afterRuntimeRequirements.call();
  2970. this.logger.timeEnd("runtime requirements");
  2971. this.logger.time("hashing");
  2972. this.hooks.beforeHash.call();
  2973. const codeGenerationJobs = this.createHash();
  2974. this.hooks.afterHash.call();
  2975. this.logger.timeEnd("hashing");
  2976. this._runCodeGenerationJobs(codeGenerationJobs, err => {
  2977. if (err) {
  2978. return finalCallback(err);
  2979. }
  2980. if (shouldRecord) {
  2981. this.logger.time("record hash");
  2982. this.hooks.recordHash.call(this.records);
  2983. this.logger.timeEnd("record hash");
  2984. }
  2985. this.logger.time("module assets");
  2986. this.clearAssets();
  2987. this.hooks.beforeModuleAssets.call();
  2988. this.createModuleAssets();
  2989. this.logger.timeEnd("module assets");
  2990. const cont = () => {
  2991. this.logger.time("process assets");
  2992. this.hooks.processAssets.callAsync(this.assets, err => {
  2993. if (err) {
  2994. return finalCallback(
  2995. makeWebpackError(err, "Compilation.hooks.processAssets")
  2996. );
  2997. }
  2998. this.hooks.afterProcessAssets.call(this.assets);
  2999. this.logger.timeEnd("process assets");
  3000. this.assets = /** @type {CompilationAssets} */ (
  3001. this._backCompat
  3002. ? soonFrozenObjectDeprecation(
  3003. this.assets,
  3004. "Compilation.assets",
  3005. "DEP_WEBPACK_COMPILATION_ASSETS",
  3006. `BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
  3007. Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
  3008. Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.`
  3009. )
  3010. : Object.freeze(this.assets)
  3011. );
  3012. this.summarizeDependencies();
  3013. if (shouldRecord) {
  3014. this.hooks.record.call(this, this.records);
  3015. }
  3016. if (this.hooks.needAdditionalSeal.call()) {
  3017. this.unseal();
  3018. return this.seal(callback);
  3019. }
  3020. return this.hooks.afterSeal.callAsync(err => {
  3021. if (err) {
  3022. return finalCallback(
  3023. makeWebpackError(err, "Compilation.hooks.afterSeal")
  3024. );
  3025. }
  3026. this.fileSystemInfo.logStatistics();
  3027. finalCallback();
  3028. });
  3029. });
  3030. };
  3031. this.logger.time("create chunk assets");
  3032. if (this.hooks.shouldGenerateChunkAssets.call() !== false) {
  3033. this.hooks.beforeChunkAssets.call();
  3034. this.createChunkAssets(err => {
  3035. this.logger.timeEnd("create chunk assets");
  3036. if (err) {
  3037. return finalCallback(err);
  3038. }
  3039. cont();
  3040. });
  3041. } else {
  3042. this.logger.timeEnd("create chunk assets");
  3043. cont();
  3044. }
  3045. });
  3046. });
  3047. }
  3048. );
  3049. });
  3050. }
  3051. /**
  3052. * @param {Module} module module to report from
  3053. * @param {DependenciesBlock[]} blocks blocks to report from
  3054. * @returns {boolean} true, when it has warnings or errors
  3055. */
  3056. reportDependencyErrorsAndWarnings(module, blocks) {
  3057. let hasProblems = false;
  3058. for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
  3059. const block = blocks[indexBlock];
  3060. const dependencies = block.dependencies;
  3061. for (let indexDep = 0; indexDep < dependencies.length; indexDep++) {
  3062. const d = dependencies[indexDep];
  3063. const warnings = d.getWarnings(this.moduleGraph);
  3064. if (warnings) {
  3065. for (let indexWar = 0; indexWar < warnings.length; indexWar++) {
  3066. const w = warnings[indexWar];
  3067. const warning = new ModuleDependencyWarning(module, w, d.loc);
  3068. this.warnings.push(warning);
  3069. hasProblems = true;
  3070. }
  3071. }
  3072. const errors = d.getErrors(this.moduleGraph);
  3073. if (errors) {
  3074. for (let indexErr = 0; indexErr < errors.length; indexErr++) {
  3075. const e = errors[indexErr];
  3076. const error = new ModuleDependencyError(module, e, d.loc);
  3077. this.errors.push(error);
  3078. hasProblems = true;
  3079. }
  3080. }
  3081. }
  3082. if (this.reportDependencyErrorsAndWarnings(module, block.blocks))
  3083. hasProblems = true;
  3084. }
  3085. return hasProblems;
  3086. }
  3087. /**
  3088. * @param {Callback} callback callback
  3089. */
  3090. codeGeneration(callback) {
  3091. const { chunkGraph } = this;
  3092. this.codeGenerationResults = new CodeGenerationResults(
  3093. this.outputOptions.hashFunction
  3094. );
  3095. /** @type {CodeGenerationJobs} */
  3096. const jobs = [];
  3097. for (const module of this.modules) {
  3098. const runtimes = chunkGraph.getModuleRuntimes(module);
  3099. if (runtimes.size === 1) {
  3100. for (const runtime of runtimes) {
  3101. const hash = chunkGraph.getModuleHash(module, runtime);
  3102. jobs.push({ module, hash, runtime, runtimes: [runtime] });
  3103. }
  3104. } else if (runtimes.size > 1) {
  3105. /** @type {Map<string, { runtimes: RuntimeSpec[] }>} */
  3106. const map = new Map();
  3107. for (const runtime of runtimes) {
  3108. const hash = chunkGraph.getModuleHash(module, runtime);
  3109. const job = map.get(hash);
  3110. if (job === undefined) {
  3111. const newJob = { module, hash, runtime, runtimes: [runtime] };
  3112. jobs.push(newJob);
  3113. map.set(hash, newJob);
  3114. } else {
  3115. job.runtimes.push(runtime);
  3116. }
  3117. }
  3118. }
  3119. }
  3120. this._runCodeGenerationJobs(jobs, callback);
  3121. }
  3122. /**
  3123. * @private
  3124. * @param {CodeGenerationJobs} jobs code generation jobs
  3125. * @param {Callback} callback callback
  3126. * @returns {void}
  3127. */
  3128. _runCodeGenerationJobs(jobs, callback) {
  3129. if (jobs.length === 0) {
  3130. return callback();
  3131. }
  3132. let statModulesFromCache = 0;
  3133. let statModulesGenerated = 0;
  3134. const { chunkGraph, moduleGraph, dependencyTemplates, runtimeTemplate } =
  3135. this;
  3136. const results = this.codeGenerationResults;
  3137. /** @type {WebpackError[]} */
  3138. const errors = [];
  3139. /** @type {NotCodeGeneratedModules | undefined} */
  3140. let notCodeGeneratedModules;
  3141. const runIteration = () => {
  3142. /** @type {CodeGenerationJobs} */
  3143. let delayedJobs = [];
  3144. let delayedModules = new Set();
  3145. asyncLib.eachLimit(
  3146. jobs,
  3147. /** @type {number} */
  3148. (this.options.parallelism),
  3149. (job, callback) => {
  3150. const { module } = job;
  3151. const { codeGenerationDependencies } = module;
  3152. if (
  3153. codeGenerationDependencies !== undefined &&
  3154. (notCodeGeneratedModules === undefined ||
  3155. codeGenerationDependencies.some(dep => {
  3156. const referencedModule = /** @type {Module} */ (
  3157. moduleGraph.getModule(dep)
  3158. );
  3159. return /** @type {NotCodeGeneratedModules} */ (
  3160. notCodeGeneratedModules
  3161. ).has(referencedModule);
  3162. }))
  3163. ) {
  3164. delayedJobs.push(job);
  3165. delayedModules.add(module);
  3166. return callback();
  3167. }
  3168. const { hash, runtime, runtimes } = job;
  3169. this._codeGenerationModule(
  3170. module,
  3171. runtime,
  3172. runtimes,
  3173. hash,
  3174. dependencyTemplates,
  3175. chunkGraph,
  3176. moduleGraph,
  3177. runtimeTemplate,
  3178. errors,
  3179. results,
  3180. (err, codeGenerated) => {
  3181. if (codeGenerated) statModulesGenerated++;
  3182. else statModulesFromCache++;
  3183. callback(err);
  3184. }
  3185. );
  3186. },
  3187. err => {
  3188. if (err) return callback(err);
  3189. if (delayedJobs.length > 0) {
  3190. if (delayedJobs.length === jobs.length) {
  3191. return callback(
  3192. /** @type {WebpackError} */ (
  3193. new Error(
  3194. `Unable to make progress during code generation because of circular code generation dependency: ${Array.from(
  3195. delayedModules,
  3196. m => m.identifier()
  3197. ).join(", ")}`
  3198. )
  3199. )
  3200. );
  3201. }
  3202. jobs = delayedJobs;
  3203. delayedJobs = [];
  3204. notCodeGeneratedModules = delayedModules;
  3205. delayedModules = new Set();
  3206. return runIteration();
  3207. }
  3208. if (errors.length > 0) {
  3209. errors.sort(
  3210. compareSelect(err => err.module, compareModulesByIdentifier)
  3211. );
  3212. for (const error of errors) {
  3213. this.errors.push(error);
  3214. }
  3215. }
  3216. this.logger.log(
  3217. `${Math.round(
  3218. (100 * statModulesGenerated) /
  3219. (statModulesGenerated + statModulesFromCache)
  3220. )}% code generated (${statModulesGenerated} generated, ${statModulesFromCache} from cache)`
  3221. );
  3222. callback();
  3223. }
  3224. );
  3225. };
  3226. runIteration();
  3227. }
  3228. /**
  3229. * @param {Module} module module
  3230. * @param {RuntimeSpec} runtime runtime
  3231. * @param {RuntimeSpec[]} runtimes runtimes
  3232. * @param {string} hash hash
  3233. * @param {DependencyTemplates} dependencyTemplates dependencyTemplates
  3234. * @param {ChunkGraph} chunkGraph chunkGraph
  3235. * @param {ModuleGraph} moduleGraph moduleGraph
  3236. * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
  3237. * @param {WebpackError[]} errors errors
  3238. * @param {CodeGenerationResults} results results
  3239. * @param {function((WebpackError | null)=, boolean=): void} callback callback
  3240. */
  3241. _codeGenerationModule(
  3242. module,
  3243. runtime,
  3244. runtimes,
  3245. hash,
  3246. dependencyTemplates,
  3247. chunkGraph,
  3248. moduleGraph,
  3249. runtimeTemplate,
  3250. errors,
  3251. results,
  3252. callback
  3253. ) {
  3254. let codeGenerated = false;
  3255. const cache = new MultiItemCache(
  3256. runtimes.map(runtime =>
  3257. this._codeGenerationCache.getItemCache(
  3258. `${module.identifier()}|${getRuntimeKey(runtime)}`,
  3259. `${hash}|${dependencyTemplates.getHash()}`
  3260. )
  3261. )
  3262. );
  3263. cache.get((err, cachedResult) => {
  3264. if (err) return callback(/** @type {WebpackError} */ (err));
  3265. let result;
  3266. if (!cachedResult) {
  3267. try {
  3268. codeGenerated = true;
  3269. this.codeGeneratedModules.add(module);
  3270. result = module.codeGeneration({
  3271. chunkGraph,
  3272. moduleGraph,
  3273. dependencyTemplates,
  3274. runtimeTemplate,
  3275. runtime,
  3276. codeGenerationResults: results,
  3277. compilation: this
  3278. });
  3279. } catch (err) {
  3280. errors.push(
  3281. new CodeGenerationError(module, /** @type {Error} */ (err))
  3282. );
  3283. result = cachedResult = {
  3284. sources: new Map(),
  3285. runtimeRequirements: null
  3286. };
  3287. }
  3288. } else {
  3289. result = cachedResult;
  3290. }
  3291. for (const runtime of runtimes) {
  3292. results.add(module, runtime, result);
  3293. }
  3294. if (!cachedResult) {
  3295. cache.store(result, err =>
  3296. callback(/** @type {WebpackError} */ (err), codeGenerated)
  3297. );
  3298. } else {
  3299. callback(null, codeGenerated);
  3300. }
  3301. });
  3302. }
  3303. _getChunkGraphEntries() {
  3304. /** @type {Set<Chunk>} */
  3305. const treeEntries = new Set();
  3306. for (const ep of this.entrypoints.values()) {
  3307. const chunk = ep.getRuntimeChunk();
  3308. if (chunk) treeEntries.add(chunk);
  3309. }
  3310. for (const ep of this.asyncEntrypoints) {
  3311. const chunk = ep.getRuntimeChunk();
  3312. if (chunk) treeEntries.add(chunk);
  3313. }
  3314. return treeEntries;
  3315. }
  3316. /**
  3317. * @param {object} options options
  3318. * @param {ChunkGraph=} options.chunkGraph the chunk graph
  3319. * @param {Iterable<Module>=} options.modules modules
  3320. * @param {Iterable<Chunk>=} options.chunks chunks
  3321. * @param {CodeGenerationResults=} options.codeGenerationResults codeGenerationResults
  3322. * @param {Iterable<Chunk>=} options.chunkGraphEntries chunkGraphEntries
  3323. * @returns {void}
  3324. */
  3325. processRuntimeRequirements({
  3326. chunkGraph = this.chunkGraph,
  3327. modules = this.modules,
  3328. chunks = this.chunks,
  3329. codeGenerationResults = this.codeGenerationResults,
  3330. chunkGraphEntries = this._getChunkGraphEntries()
  3331. } = {}) {
  3332. const context = { chunkGraph, codeGenerationResults };
  3333. const { moduleMemCaches2 } = this;
  3334. this.logger.time("runtime requirements.modules");
  3335. const additionalModuleRuntimeRequirements =
  3336. this.hooks.additionalModuleRuntimeRequirements;
  3337. const runtimeRequirementInModule = this.hooks.runtimeRequirementInModule;
  3338. for (const module of modules) {
  3339. if (chunkGraph.getNumberOfModuleChunks(module) > 0) {
  3340. const memCache = moduleMemCaches2 && moduleMemCaches2.get(module);
  3341. for (const runtime of chunkGraph.getModuleRuntimes(module)) {
  3342. if (memCache) {
  3343. const cached = memCache.get(
  3344. `moduleRuntimeRequirements-${getRuntimeKey(runtime)}`
  3345. );
  3346. if (cached !== undefined) {
  3347. if (cached !== null) {
  3348. chunkGraph.addModuleRuntimeRequirements(
  3349. module,
  3350. runtime,
  3351. cached,
  3352. false
  3353. );
  3354. }
  3355. continue;
  3356. }
  3357. }
  3358. let set;
  3359. const runtimeRequirements =
  3360. codeGenerationResults.getRuntimeRequirements(module, runtime);
  3361. if (runtimeRequirements && runtimeRequirements.size > 0) {
  3362. set = new Set(runtimeRequirements);
  3363. } else if (additionalModuleRuntimeRequirements.isUsed()) {
  3364. set = new Set();
  3365. } else {
  3366. if (memCache) {
  3367. memCache.set(
  3368. `moduleRuntimeRequirements-${getRuntimeKey(runtime)}`,
  3369. null
  3370. );
  3371. }
  3372. continue;
  3373. }
  3374. additionalModuleRuntimeRequirements.call(module, set, context);
  3375. for (const r of set) {
  3376. const hook = runtimeRequirementInModule.get(r);
  3377. if (hook !== undefined) hook.call(module, set, context);
  3378. }
  3379. if (set.size === 0) {
  3380. if (memCache) {
  3381. memCache.set(
  3382. `moduleRuntimeRequirements-${getRuntimeKey(runtime)}`,
  3383. null
  3384. );
  3385. }
  3386. } else if (memCache) {
  3387. memCache.set(
  3388. `moduleRuntimeRequirements-${getRuntimeKey(runtime)}`,
  3389. set
  3390. );
  3391. chunkGraph.addModuleRuntimeRequirements(
  3392. module,
  3393. runtime,
  3394. set,
  3395. false
  3396. );
  3397. } else {
  3398. chunkGraph.addModuleRuntimeRequirements(module, runtime, set);
  3399. }
  3400. }
  3401. }
  3402. }
  3403. this.logger.timeEnd("runtime requirements.modules");
  3404. this.logger.time("runtime requirements.chunks");
  3405. for (const chunk of chunks) {
  3406. const set = new Set();
  3407. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  3408. const runtimeRequirements = chunkGraph.getModuleRuntimeRequirements(
  3409. module,
  3410. chunk.runtime
  3411. );
  3412. for (const r of runtimeRequirements) set.add(r);
  3413. }
  3414. this.hooks.additionalChunkRuntimeRequirements.call(chunk, set, context);
  3415. for (const r of set) {
  3416. this.hooks.runtimeRequirementInChunk.for(r).call(chunk, set, context);
  3417. }
  3418. chunkGraph.addChunkRuntimeRequirements(chunk, set);
  3419. }
  3420. this.logger.timeEnd("runtime requirements.chunks");
  3421. this.logger.time("runtime requirements.entries");
  3422. for (const treeEntry of chunkGraphEntries) {
  3423. const set = new Set();
  3424. for (const chunk of treeEntry.getAllReferencedChunks()) {
  3425. const runtimeRequirements =
  3426. chunkGraph.getChunkRuntimeRequirements(chunk);
  3427. for (const r of runtimeRequirements) set.add(r);
  3428. }
  3429. this.hooks.additionalTreeRuntimeRequirements.call(
  3430. treeEntry,
  3431. set,
  3432. context
  3433. );
  3434. for (const r of set) {
  3435. this.hooks.runtimeRequirementInTree
  3436. .for(r)
  3437. .call(treeEntry, set, context);
  3438. }
  3439. chunkGraph.addTreeRuntimeRequirements(treeEntry, set);
  3440. }
  3441. this.logger.timeEnd("runtime requirements.entries");
  3442. }
  3443. // TODO webpack 6 make chunkGraph argument non-optional
  3444. /**
  3445. * @param {Chunk} chunk target chunk
  3446. * @param {RuntimeModule} module runtime module
  3447. * @param {ChunkGraph} chunkGraph the chunk graph
  3448. * @returns {void}
  3449. */
  3450. addRuntimeModule(chunk, module, chunkGraph = this.chunkGraph) {
  3451. // Deprecated ModuleGraph association
  3452. if (this._backCompat)
  3453. ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
  3454. // add it to the list
  3455. this.modules.add(module);
  3456. this._modules.set(module.identifier(), module);
  3457. // connect to the chunk graph
  3458. chunkGraph.connectChunkAndModule(chunk, module);
  3459. chunkGraph.connectChunkAndRuntimeModule(chunk, module);
  3460. if (module.fullHash) {
  3461. chunkGraph.addFullHashModuleToChunk(chunk, module);
  3462. } else if (module.dependentHash) {
  3463. chunkGraph.addDependentHashModuleToChunk(chunk, module);
  3464. }
  3465. // attach runtime module
  3466. module.attach(this, chunk, chunkGraph);
  3467. // Setup internals
  3468. const exportsInfo = this.moduleGraph.getExportsInfo(module);
  3469. exportsInfo.setHasProvideInfo();
  3470. if (typeof chunk.runtime === "string") {
  3471. exportsInfo.setUsedForSideEffectsOnly(chunk.runtime);
  3472. } else if (chunk.runtime === undefined) {
  3473. exportsInfo.setUsedForSideEffectsOnly(undefined);
  3474. } else {
  3475. for (const runtime of chunk.runtime) {
  3476. exportsInfo.setUsedForSideEffectsOnly(runtime);
  3477. }
  3478. }
  3479. chunkGraph.addModuleRuntimeRequirements(
  3480. module,
  3481. chunk.runtime,
  3482. new Set([RuntimeGlobals.requireScope])
  3483. );
  3484. // runtime modules don't need ids
  3485. chunkGraph.setModuleId(module, "");
  3486. // Call hook
  3487. this.hooks.runtimeModule.call(module, chunk);
  3488. }
  3489. /**
  3490. * If `module` is passed, `loc` and `request` must also be passed.
  3491. * @param {string | ChunkGroupOptions} groupOptions options for the chunk group
  3492. * @param {Module=} module the module the references the chunk group
  3493. * @param {DependencyLocation=} loc the location from with the chunk group is referenced (inside of module)
  3494. * @param {string=} request the request from which the the chunk group is referenced
  3495. * @returns {ChunkGroup} the new or existing chunk group
  3496. */
  3497. addChunkInGroup(groupOptions, module, loc, request) {
  3498. if (typeof groupOptions === "string") {
  3499. groupOptions = { name: groupOptions };
  3500. }
  3501. const name = groupOptions.name;
  3502. if (name) {
  3503. const chunkGroup = this.namedChunkGroups.get(name);
  3504. if (chunkGroup !== undefined) {
  3505. if (module) {
  3506. chunkGroup.addOrigin(
  3507. module,
  3508. /** @type {DependencyLocation} */
  3509. (loc),
  3510. request
  3511. );
  3512. }
  3513. return chunkGroup;
  3514. }
  3515. }
  3516. const chunkGroup = new ChunkGroup(groupOptions);
  3517. if (module)
  3518. chunkGroup.addOrigin(
  3519. module,
  3520. /** @type {DependencyLocation} */
  3521. (loc),
  3522. request
  3523. );
  3524. const chunk = this.addChunk(name);
  3525. connectChunkGroupAndChunk(chunkGroup, chunk);
  3526. this.chunkGroups.push(chunkGroup);
  3527. if (name) {
  3528. this.namedChunkGroups.set(name, chunkGroup);
  3529. }
  3530. return chunkGroup;
  3531. }
  3532. /**
  3533. * @param {EntryOptions} options options for the entrypoint
  3534. * @param {Module} module the module the references the chunk group
  3535. * @param {DependencyLocation} loc the location from with the chunk group is referenced (inside of module)
  3536. * @param {string} request the request from which the the chunk group is referenced
  3537. * @returns {Entrypoint} the new or existing entrypoint
  3538. */
  3539. addAsyncEntrypoint(options, module, loc, request) {
  3540. const name = options.name;
  3541. if (name) {
  3542. const entrypoint = this.namedChunkGroups.get(name);
  3543. if (entrypoint instanceof Entrypoint) {
  3544. if (entrypoint !== undefined) {
  3545. if (module) {
  3546. entrypoint.addOrigin(module, loc, request);
  3547. }
  3548. return entrypoint;
  3549. }
  3550. } else if (entrypoint) {
  3551. throw new Error(
  3552. `Cannot add an async entrypoint with the name '${name}', because there is already an chunk group with this name`
  3553. );
  3554. }
  3555. }
  3556. const chunk = this.addChunk(name);
  3557. if (options.filename) {
  3558. chunk.filenameTemplate = options.filename;
  3559. }
  3560. const entrypoint = new Entrypoint(options, false);
  3561. entrypoint.setRuntimeChunk(chunk);
  3562. entrypoint.setEntrypointChunk(chunk);
  3563. if (name) {
  3564. this.namedChunkGroups.set(name, entrypoint);
  3565. }
  3566. this.chunkGroups.push(entrypoint);
  3567. this.asyncEntrypoints.push(entrypoint);
  3568. connectChunkGroupAndChunk(entrypoint, chunk);
  3569. if (module) {
  3570. entrypoint.addOrigin(module, loc, request);
  3571. }
  3572. return entrypoint;
  3573. }
  3574. /**
  3575. * This method first looks to see if a name is provided for a new chunk,
  3576. * and first looks to see if any named chunks already exist and reuse that chunk instead.
  3577. * @param {string=} name optional chunk name to be provided
  3578. * @returns {Chunk} create a chunk (invoked during seal event)
  3579. */
  3580. addChunk(name) {
  3581. if (name) {
  3582. const chunk = this.namedChunks.get(name);
  3583. if (chunk !== undefined) {
  3584. return chunk;
  3585. }
  3586. }
  3587. const chunk = new Chunk(name, this._backCompat);
  3588. this.chunks.add(chunk);
  3589. if (this._backCompat)
  3590. ChunkGraph.setChunkGraphForChunk(chunk, this.chunkGraph);
  3591. if (name) {
  3592. this.namedChunks.set(name, chunk);
  3593. }
  3594. return chunk;
  3595. }
  3596. /**
  3597. * @deprecated
  3598. * @param {Module} module module to assign depth
  3599. * @returns {void}
  3600. */
  3601. assignDepth(module) {
  3602. const moduleGraph = this.moduleGraph;
  3603. const queue = new Set([module]);
  3604. /** @type {number} */
  3605. let depth;
  3606. moduleGraph.setDepth(module, 0);
  3607. /**
  3608. * @param {Module} module module for processing
  3609. * @returns {void}
  3610. */
  3611. const processModule = module => {
  3612. if (!moduleGraph.setDepthIfLower(module, depth)) return;
  3613. queue.add(module);
  3614. };
  3615. for (module of queue) {
  3616. queue.delete(module);
  3617. depth = /** @type {number} */ (moduleGraph.getDepth(module)) + 1;
  3618. for (const connection of moduleGraph.getOutgoingConnections(module)) {
  3619. const refModule = connection.module;
  3620. if (refModule) {
  3621. processModule(refModule);
  3622. }
  3623. }
  3624. }
  3625. }
  3626. /**
  3627. * @param {Set<Module>} modules module to assign depth
  3628. * @returns {void}
  3629. */
  3630. assignDepths(modules) {
  3631. const moduleGraph = this.moduleGraph;
  3632. /** @type {Set<Module | number>} */
  3633. const queue = new Set(modules);
  3634. queue.add(1);
  3635. let depth = 0;
  3636. let i = 0;
  3637. for (const module of queue) {
  3638. i++;
  3639. if (typeof module === "number") {
  3640. depth = module;
  3641. if (queue.size === i) return;
  3642. queue.add(depth + 1);
  3643. } else {
  3644. moduleGraph.setDepth(module, depth);
  3645. for (const { module: refModule } of moduleGraph.getOutgoingConnections(
  3646. module
  3647. )) {
  3648. if (refModule) {
  3649. queue.add(refModule);
  3650. }
  3651. }
  3652. }
  3653. }
  3654. }
  3655. /**
  3656. * @param {Dependency} dependency the dependency
  3657. * @param {RuntimeSpec} runtime the runtime
  3658. * @returns {(string[] | ReferencedExport)[]} referenced exports
  3659. */
  3660. getDependencyReferencedExports(dependency, runtime) {
  3661. const referencedExports = dependency.getReferencedExports(
  3662. this.moduleGraph,
  3663. runtime
  3664. );
  3665. return this.hooks.dependencyReferencedExports.call(
  3666. referencedExports,
  3667. dependency,
  3668. runtime
  3669. );
  3670. }
  3671. /**
  3672. * @param {Module} module module relationship for removal
  3673. * @param {DependenciesBlockLike} block //TODO: good description
  3674. * @returns {void}
  3675. */
  3676. removeReasonsOfDependencyBlock(module, block) {
  3677. if (block.blocks) {
  3678. for (const b of block.blocks) {
  3679. this.removeReasonsOfDependencyBlock(module, b);
  3680. }
  3681. }
  3682. if (block.dependencies) {
  3683. for (const dep of block.dependencies) {
  3684. const originalModule = this.moduleGraph.getModule(dep);
  3685. if (originalModule) {
  3686. this.moduleGraph.removeConnection(dep);
  3687. if (this.chunkGraph) {
  3688. for (const chunk of this.chunkGraph.getModuleChunks(
  3689. originalModule
  3690. )) {
  3691. this.patchChunksAfterReasonRemoval(originalModule, chunk);
  3692. }
  3693. }
  3694. }
  3695. }
  3696. }
  3697. }
  3698. /**
  3699. * @param {Module} module module to patch tie
  3700. * @param {Chunk} chunk chunk to patch tie
  3701. * @returns {void}
  3702. */
  3703. patchChunksAfterReasonRemoval(module, chunk) {
  3704. if (!module.hasReasons(this.moduleGraph, chunk.runtime)) {
  3705. this.removeReasonsOfDependencyBlock(module, module);
  3706. }
  3707. if (
  3708. !module.hasReasonForChunk(chunk, this.moduleGraph, this.chunkGraph) &&
  3709. this.chunkGraph.isModuleInChunk(module, chunk)
  3710. ) {
  3711. this.chunkGraph.disconnectChunkAndModule(chunk, module);
  3712. this.removeChunkFromDependencies(module, chunk);
  3713. }
  3714. }
  3715. /**
  3716. * @param {DependenciesBlock} block block tie for Chunk
  3717. * @param {Chunk} chunk chunk to remove from dep
  3718. * @returns {void}
  3719. */
  3720. removeChunkFromDependencies(block, chunk) {
  3721. /**
  3722. * @param {Dependency} d dependency to (maybe) patch up
  3723. */
  3724. const iteratorDependency = d => {
  3725. const depModule = this.moduleGraph.getModule(d);
  3726. if (!depModule) {
  3727. return;
  3728. }
  3729. this.patchChunksAfterReasonRemoval(depModule, chunk);
  3730. };
  3731. const blocks = block.blocks;
  3732. for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
  3733. const asyncBlock = blocks[indexBlock];
  3734. const chunkGroup =
  3735. /** @type {ChunkGroup} */
  3736. (this.chunkGraph.getBlockChunkGroup(asyncBlock));
  3737. // Grab all chunks from the first Block's AsyncDepBlock
  3738. const chunks = chunkGroup.chunks;
  3739. // For each chunk in chunkGroup
  3740. for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
  3741. const iteratedChunk = chunks[indexChunk];
  3742. chunkGroup.removeChunk(iteratedChunk);
  3743. // Recurse
  3744. this.removeChunkFromDependencies(block, iteratedChunk);
  3745. }
  3746. }
  3747. if (block.dependencies) {
  3748. for (const dep of block.dependencies) iteratorDependency(dep);
  3749. }
  3750. }
  3751. assignRuntimeIds() {
  3752. const { chunkGraph } = this;
  3753. /**
  3754. * @param {Entrypoint} ep an entrypoint
  3755. */
  3756. const processEntrypoint = ep => {
  3757. const runtime = /** @type {string} */ (ep.options.runtime || ep.name);
  3758. const chunk = /** @type {Chunk} */ (ep.getRuntimeChunk());
  3759. chunkGraph.setRuntimeId(runtime, /** @type {ChunkId} */ (chunk.id));
  3760. };
  3761. for (const ep of this.entrypoints.values()) {
  3762. processEntrypoint(ep);
  3763. }
  3764. for (const ep of this.asyncEntrypoints) {
  3765. processEntrypoint(ep);
  3766. }
  3767. }
  3768. sortItemsWithChunkIds() {
  3769. for (const chunkGroup of this.chunkGroups) {
  3770. chunkGroup.sortItems();
  3771. }
  3772. this.errors.sort(compareErrors);
  3773. this.warnings.sort(compareErrors);
  3774. this.children.sort(byNameOrHash);
  3775. }
  3776. summarizeDependencies() {
  3777. for (
  3778. let indexChildren = 0;
  3779. indexChildren < this.children.length;
  3780. indexChildren++
  3781. ) {
  3782. const child = this.children[indexChildren];
  3783. this.fileDependencies.addAll(child.fileDependencies);
  3784. this.contextDependencies.addAll(child.contextDependencies);
  3785. this.missingDependencies.addAll(child.missingDependencies);
  3786. this.buildDependencies.addAll(child.buildDependencies);
  3787. }
  3788. for (const module of this.modules) {
  3789. module.addCacheDependencies(
  3790. this.fileDependencies,
  3791. this.contextDependencies,
  3792. this.missingDependencies,
  3793. this.buildDependencies
  3794. );
  3795. }
  3796. }
  3797. createModuleHashes() {
  3798. let statModulesHashed = 0;
  3799. let statModulesFromCache = 0;
  3800. const { chunkGraph, runtimeTemplate, moduleMemCaches2 } = this;
  3801. const { hashFunction, hashDigest, hashDigestLength } = this.outputOptions;
  3802. /** @type {WebpackError[]} */
  3803. const errors = [];
  3804. for (const module of this.modules) {
  3805. const memCache = moduleMemCaches2 && moduleMemCaches2.get(module);
  3806. for (const runtime of chunkGraph.getModuleRuntimes(module)) {
  3807. if (memCache) {
  3808. const digest = memCache.get(`moduleHash-${getRuntimeKey(runtime)}`);
  3809. if (digest !== undefined) {
  3810. chunkGraph.setModuleHashes(
  3811. module,
  3812. runtime,
  3813. digest,
  3814. digest.slice(0, hashDigestLength)
  3815. );
  3816. statModulesFromCache++;
  3817. continue;
  3818. }
  3819. }
  3820. statModulesHashed++;
  3821. const digest = this._createModuleHash(
  3822. module,
  3823. chunkGraph,
  3824. runtime,
  3825. hashFunction,
  3826. runtimeTemplate,
  3827. hashDigest,
  3828. hashDigestLength,
  3829. errors
  3830. );
  3831. if (memCache) {
  3832. memCache.set(`moduleHash-${getRuntimeKey(runtime)}`, digest);
  3833. }
  3834. }
  3835. }
  3836. if (errors.length > 0) {
  3837. errors.sort(compareSelect(err => err.module, compareModulesByIdentifier));
  3838. for (const error of errors) {
  3839. this.errors.push(error);
  3840. }
  3841. }
  3842. this.logger.log(
  3843. `${statModulesHashed} modules hashed, ${statModulesFromCache} from cache (${
  3844. Math.round(
  3845. (100 * (statModulesHashed + statModulesFromCache)) / this.modules.size
  3846. ) / 100
  3847. } variants per module in average)`
  3848. );
  3849. }
  3850. /**
  3851. * @private
  3852. * @param {Module} module module
  3853. * @param {ChunkGraph} chunkGraph the chunk graph
  3854. * @param {RuntimeSpec} runtime runtime
  3855. * @param {OutputOptions["hashFunction"]} hashFunction hash function
  3856. * @param {RuntimeTemplate} runtimeTemplate runtime template
  3857. * @param {OutputOptions["hashDigest"]} hashDigest hash digest
  3858. * @param {OutputOptions["hashDigestLength"]} hashDigestLength hash digest length
  3859. * @param {WebpackError[]} errors errors
  3860. * @returns {string} module hash digest
  3861. */
  3862. _createModuleHash(
  3863. module,
  3864. chunkGraph,
  3865. runtime,
  3866. hashFunction,
  3867. runtimeTemplate,
  3868. hashDigest,
  3869. hashDigestLength,
  3870. errors
  3871. ) {
  3872. let moduleHashDigest;
  3873. try {
  3874. const moduleHash = createHash(/** @type {Algorithm} */ (hashFunction));
  3875. module.updateHash(moduleHash, {
  3876. chunkGraph,
  3877. runtime,
  3878. runtimeTemplate
  3879. });
  3880. moduleHashDigest = /** @type {string} */ (moduleHash.digest(hashDigest));
  3881. } catch (err) {
  3882. errors.push(new ModuleHashingError(module, /** @type {Error} */ (err)));
  3883. moduleHashDigest = "XXXXXX";
  3884. }
  3885. chunkGraph.setModuleHashes(
  3886. module,
  3887. runtime,
  3888. moduleHashDigest,
  3889. moduleHashDigest.slice(0, hashDigestLength)
  3890. );
  3891. return moduleHashDigest;
  3892. }
  3893. createHash() {
  3894. this.logger.time("hashing: initialize hash");
  3895. const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
  3896. const runtimeTemplate = this.runtimeTemplate;
  3897. const outputOptions = this.outputOptions;
  3898. const hashFunction = outputOptions.hashFunction;
  3899. const hashDigest = outputOptions.hashDigest;
  3900. const hashDigestLength = outputOptions.hashDigestLength;
  3901. const hash = createHash(/** @type {Algorithm} */ (hashFunction));
  3902. if (outputOptions.hashSalt) {
  3903. hash.update(outputOptions.hashSalt);
  3904. }
  3905. this.logger.timeEnd("hashing: initialize hash");
  3906. if (this.children.length > 0) {
  3907. this.logger.time("hashing: hash child compilations");
  3908. for (const child of this.children) {
  3909. hash.update(/** @type {string} */ (child.hash));
  3910. }
  3911. this.logger.timeEnd("hashing: hash child compilations");
  3912. }
  3913. if (this.warnings.length > 0) {
  3914. this.logger.time("hashing: hash warnings");
  3915. for (const warning of this.warnings) {
  3916. hash.update(`${warning.message}`);
  3917. }
  3918. this.logger.timeEnd("hashing: hash warnings");
  3919. }
  3920. if (this.errors.length > 0) {
  3921. this.logger.time("hashing: hash errors");
  3922. for (const error of this.errors) {
  3923. hash.update(`${error.message}`);
  3924. }
  3925. this.logger.timeEnd("hashing: hash errors");
  3926. }
  3927. this.logger.time("hashing: sort chunks");
  3928. /*
  3929. * all non-runtime chunks need to be hashes first,
  3930. * since runtime chunk might use their hashes.
  3931. * runtime chunks need to be hashed in the correct order
  3932. * since they may depend on each other (for async entrypoints).
  3933. * So we put all non-runtime chunks first and hash them in any order.
  3934. * And order runtime chunks according to referenced between each other.
  3935. * Chunks need to be in deterministic order since we add hashes to full chunk
  3936. * during these hashing.
  3937. */
  3938. /** @type {Chunk[]} */
  3939. const unorderedRuntimeChunks = [];
  3940. /** @type {Chunk[]} */
  3941. const otherChunks = [];
  3942. for (const c of this.chunks) {
  3943. if (c.hasRuntime()) {
  3944. unorderedRuntimeChunks.push(c);
  3945. } else {
  3946. otherChunks.push(c);
  3947. }
  3948. }
  3949. unorderedRuntimeChunks.sort(byId);
  3950. otherChunks.sort(byId);
  3951. /** @typedef {{ chunk: Chunk, referencedBy: RuntimeChunkInfo[], remaining: number }} RuntimeChunkInfo */
  3952. /** @type {Map<Chunk, RuntimeChunkInfo>} */
  3953. const runtimeChunksMap = new Map();
  3954. for (const chunk of unorderedRuntimeChunks) {
  3955. runtimeChunksMap.set(chunk, {
  3956. chunk,
  3957. referencedBy: [],
  3958. remaining: 0
  3959. });
  3960. }
  3961. let remaining = 0;
  3962. for (const info of runtimeChunksMap.values()) {
  3963. for (const other of new Set(
  3964. Array.from(info.chunk.getAllReferencedAsyncEntrypoints()).map(
  3965. e => e.chunks[e.chunks.length - 1]
  3966. )
  3967. )) {
  3968. const otherInfo =
  3969. /** @type {RuntimeChunkInfo} */
  3970. (runtimeChunksMap.get(other));
  3971. otherInfo.referencedBy.push(info);
  3972. info.remaining++;
  3973. remaining++;
  3974. }
  3975. }
  3976. /** @type {Chunk[]} */
  3977. const runtimeChunks = [];
  3978. for (const info of runtimeChunksMap.values()) {
  3979. if (info.remaining === 0) {
  3980. runtimeChunks.push(info.chunk);
  3981. }
  3982. }
  3983. // If there are any references between chunks
  3984. // make sure to follow these chains
  3985. if (remaining > 0) {
  3986. const readyChunks = [];
  3987. for (const chunk of runtimeChunks) {
  3988. const hasFullHashModules =
  3989. chunkGraph.getNumberOfChunkFullHashModules(chunk) !== 0;
  3990. const info =
  3991. /** @type {RuntimeChunkInfo} */
  3992. (runtimeChunksMap.get(chunk));
  3993. for (const otherInfo of info.referencedBy) {
  3994. if (hasFullHashModules) {
  3995. chunkGraph.upgradeDependentToFullHashModules(otherInfo.chunk);
  3996. }
  3997. remaining--;
  3998. if (--otherInfo.remaining === 0) {
  3999. readyChunks.push(otherInfo.chunk);
  4000. }
  4001. }
  4002. if (readyChunks.length > 0) {
  4003. // This ensures deterministic ordering, since referencedBy is non-deterministic
  4004. readyChunks.sort(byId);
  4005. for (const c of readyChunks) runtimeChunks.push(c);
  4006. readyChunks.length = 0;
  4007. }
  4008. }
  4009. }
  4010. // If there are still remaining references we have cycles and want to create a warning
  4011. if (remaining > 0) {
  4012. const circularRuntimeChunkInfo = [];
  4013. for (const info of runtimeChunksMap.values()) {
  4014. if (info.remaining !== 0) {
  4015. circularRuntimeChunkInfo.push(info);
  4016. }
  4017. }
  4018. circularRuntimeChunkInfo.sort(compareSelect(i => i.chunk, byId));
  4019. const err =
  4020. new WebpackError(`Circular dependency between chunks with runtime (${Array.from(
  4021. circularRuntimeChunkInfo,
  4022. c => c.chunk.name || c.chunk.id
  4023. ).join(", ")})
  4024. This prevents using hashes of each other and should be avoided.`);
  4025. err.chunk = circularRuntimeChunkInfo[0].chunk;
  4026. this.warnings.push(err);
  4027. for (const i of circularRuntimeChunkInfo) runtimeChunks.push(i.chunk);
  4028. }
  4029. this.logger.timeEnd("hashing: sort chunks");
  4030. const fullHashChunks = new Set();
  4031. /** @type {{module: Module, hash: string, runtime: RuntimeSpec, runtimes: RuntimeSpec[]}[]} */
  4032. const codeGenerationJobs = [];
  4033. /** @type {Map<string, Map<Module, {module: Module, hash: string, runtime: RuntimeSpec, runtimes: RuntimeSpec[]}>>} */
  4034. const codeGenerationJobsMap = new Map();
  4035. /** @type {WebpackError[]} */
  4036. const errors = [];
  4037. /**
  4038. * @param {Chunk} chunk chunk
  4039. */
  4040. const processChunk = chunk => {
  4041. // Last minute module hash generation for modules that depend on chunk hashes
  4042. this.logger.time("hashing: hash runtime modules");
  4043. const runtime = chunk.runtime;
  4044. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  4045. if (!chunkGraph.hasModuleHashes(module, runtime)) {
  4046. const hash = this._createModuleHash(
  4047. module,
  4048. chunkGraph,
  4049. runtime,
  4050. hashFunction,
  4051. runtimeTemplate,
  4052. hashDigest,
  4053. hashDigestLength,
  4054. errors
  4055. );
  4056. let hashMap = codeGenerationJobsMap.get(hash);
  4057. if (hashMap) {
  4058. const moduleJob = hashMap.get(module);
  4059. if (moduleJob) {
  4060. moduleJob.runtimes.push(runtime);
  4061. continue;
  4062. }
  4063. } else {
  4064. hashMap = new Map();
  4065. codeGenerationJobsMap.set(hash, hashMap);
  4066. }
  4067. const job = {
  4068. module,
  4069. hash,
  4070. runtime,
  4071. runtimes: [runtime]
  4072. };
  4073. hashMap.set(module, job);
  4074. codeGenerationJobs.push(job);
  4075. }
  4076. }
  4077. this.logger.timeAggregate("hashing: hash runtime modules");
  4078. try {
  4079. this.logger.time("hashing: hash chunks");
  4080. const chunkHash = createHash(/** @type {Algorithm} */ (hashFunction));
  4081. if (outputOptions.hashSalt) {
  4082. chunkHash.update(outputOptions.hashSalt);
  4083. }
  4084. chunk.updateHash(chunkHash, chunkGraph);
  4085. this.hooks.chunkHash.call(chunk, chunkHash, {
  4086. chunkGraph,
  4087. codeGenerationResults: this.codeGenerationResults,
  4088. moduleGraph: this.moduleGraph,
  4089. runtimeTemplate: this.runtimeTemplate
  4090. });
  4091. const chunkHashDigest = /** @type {string} */ (
  4092. chunkHash.digest(hashDigest)
  4093. );
  4094. hash.update(chunkHashDigest);
  4095. chunk.hash = chunkHashDigest;
  4096. chunk.renderedHash = chunk.hash.slice(0, hashDigestLength);
  4097. const fullHashModules =
  4098. chunkGraph.getChunkFullHashModulesIterable(chunk);
  4099. if (fullHashModules) {
  4100. fullHashChunks.add(chunk);
  4101. } else {
  4102. this.hooks.contentHash.call(chunk);
  4103. }
  4104. } catch (err) {
  4105. this.errors.push(
  4106. new ChunkRenderError(chunk, "", /** @type {Error} */ (err))
  4107. );
  4108. }
  4109. this.logger.timeAggregate("hashing: hash chunks");
  4110. };
  4111. for (const chunk of otherChunks) processChunk(chunk);
  4112. for (const chunk of runtimeChunks) processChunk(chunk);
  4113. if (errors.length > 0) {
  4114. errors.sort(compareSelect(err => err.module, compareModulesByIdentifier));
  4115. for (const error of errors) {
  4116. this.errors.push(error);
  4117. }
  4118. }
  4119. this.logger.timeAggregateEnd("hashing: hash runtime modules");
  4120. this.logger.timeAggregateEnd("hashing: hash chunks");
  4121. this.logger.time("hashing: hash digest");
  4122. this.hooks.fullHash.call(hash);
  4123. this.fullHash = /** @type {string} */ (hash.digest(hashDigest));
  4124. this.hash = this.fullHash.slice(0, hashDigestLength);
  4125. this.logger.timeEnd("hashing: hash digest");
  4126. this.logger.time("hashing: process full hash modules");
  4127. for (const chunk of fullHashChunks) {
  4128. for (const module of /** @type {Iterable<RuntimeModule>} */ (
  4129. chunkGraph.getChunkFullHashModulesIterable(chunk)
  4130. )) {
  4131. const moduleHash = createHash(/** @type {Algorithm} */ (hashFunction));
  4132. module.updateHash(moduleHash, {
  4133. chunkGraph,
  4134. runtime: chunk.runtime,
  4135. runtimeTemplate
  4136. });
  4137. const moduleHashDigest = /** @type {string} */ (
  4138. moduleHash.digest(hashDigest)
  4139. );
  4140. const oldHash = chunkGraph.getModuleHash(module, chunk.runtime);
  4141. chunkGraph.setModuleHashes(
  4142. module,
  4143. chunk.runtime,
  4144. moduleHashDigest,
  4145. moduleHashDigest.slice(0, hashDigestLength)
  4146. );
  4147. codeGenerationJobsMap.get(oldHash).get(module).hash = moduleHashDigest;
  4148. }
  4149. const chunkHash = createHash(/** @type {Algorithm} */ (hashFunction));
  4150. chunkHash.update(chunk.hash);
  4151. chunkHash.update(this.hash);
  4152. const chunkHashDigest =
  4153. /** @type {string} */
  4154. (chunkHash.digest(hashDigest));
  4155. chunk.hash = chunkHashDigest;
  4156. chunk.renderedHash = chunk.hash.slice(0, hashDigestLength);
  4157. this.hooks.contentHash.call(chunk);
  4158. }
  4159. this.logger.timeEnd("hashing: process full hash modules");
  4160. return codeGenerationJobs;
  4161. }
  4162. /**
  4163. * @param {string} file file name
  4164. * @param {Source} source asset source
  4165. * @param {AssetInfo} assetInfo extra asset information
  4166. * @returns {void}
  4167. */
  4168. emitAsset(file, source, assetInfo = {}) {
  4169. if (this.assets[file]) {
  4170. if (!isSourceEqual(this.assets[file], source)) {
  4171. this.errors.push(
  4172. new WebpackError(
  4173. `Conflict: Multiple assets emit different content to the same filename ${file}${
  4174. assetInfo.sourceFilename
  4175. ? `. Original source ${assetInfo.sourceFilename}`
  4176. : ""
  4177. }`
  4178. )
  4179. );
  4180. this.assets[file] = source;
  4181. this._setAssetInfo(file, assetInfo);
  4182. return;
  4183. }
  4184. const oldInfo = this.assetsInfo.get(file);
  4185. const newInfo = { ...oldInfo, ...assetInfo };
  4186. this._setAssetInfo(file, newInfo, oldInfo);
  4187. return;
  4188. }
  4189. this.assets[file] = source;
  4190. this._setAssetInfo(file, assetInfo, undefined);
  4191. }
  4192. /**
  4193. * @private
  4194. * @param {string} file file name
  4195. * @param {AssetInfo} newInfo new asset information
  4196. * @param {AssetInfo=} oldInfo old asset information
  4197. */
  4198. _setAssetInfo(file, newInfo, oldInfo = this.assetsInfo.get(file)) {
  4199. if (newInfo === undefined) {
  4200. this.assetsInfo.delete(file);
  4201. } else {
  4202. this.assetsInfo.set(file, newInfo);
  4203. }
  4204. const oldRelated = oldInfo && oldInfo.related;
  4205. const newRelated = newInfo && newInfo.related;
  4206. if (oldRelated) {
  4207. for (const key of Object.keys(oldRelated)) {
  4208. /**
  4209. * @param {string} name name
  4210. */
  4211. const remove = name => {
  4212. const relatedIn = this._assetsRelatedIn.get(name);
  4213. if (relatedIn === undefined) return;
  4214. const entry = relatedIn.get(key);
  4215. if (entry === undefined) return;
  4216. entry.delete(file);
  4217. if (entry.size !== 0) return;
  4218. relatedIn.delete(key);
  4219. if (relatedIn.size === 0) this._assetsRelatedIn.delete(name);
  4220. };
  4221. const entry = oldRelated[key];
  4222. if (Array.isArray(entry)) {
  4223. for (const name of entry) {
  4224. remove(name);
  4225. }
  4226. } else if (entry) {
  4227. remove(entry);
  4228. }
  4229. }
  4230. }
  4231. if (newRelated) {
  4232. for (const key of Object.keys(newRelated)) {
  4233. /**
  4234. * @param {string} name name
  4235. */
  4236. const add = name => {
  4237. let relatedIn = this._assetsRelatedIn.get(name);
  4238. if (relatedIn === undefined) {
  4239. this._assetsRelatedIn.set(name, (relatedIn = new Map()));
  4240. }
  4241. let entry = relatedIn.get(key);
  4242. if (entry === undefined) {
  4243. relatedIn.set(key, (entry = new Set()));
  4244. }
  4245. entry.add(file);
  4246. };
  4247. const entry = newRelated[key];
  4248. if (Array.isArray(entry)) {
  4249. for (const name of entry) {
  4250. add(name);
  4251. }
  4252. } else if (entry) {
  4253. add(entry);
  4254. }
  4255. }
  4256. }
  4257. }
  4258. /**
  4259. * @param {string} file file name
  4260. * @param {Source | function(Source): Source} newSourceOrFunction new asset source or function converting old to new
  4261. * @param {(AssetInfo | function(AssetInfo | undefined): AssetInfo) | undefined} assetInfoUpdateOrFunction new asset info or function converting old to new
  4262. */
  4263. updateAsset(
  4264. file,
  4265. newSourceOrFunction,
  4266. assetInfoUpdateOrFunction = undefined
  4267. ) {
  4268. if (!this.assets[file]) {
  4269. throw new Error(
  4270. `Called Compilation.updateAsset for not existing filename ${file}`
  4271. );
  4272. }
  4273. this.assets[file] =
  4274. typeof newSourceOrFunction === "function"
  4275. ? newSourceOrFunction(this.assets[file])
  4276. : newSourceOrFunction;
  4277. if (assetInfoUpdateOrFunction !== undefined) {
  4278. const oldInfo = this.assetsInfo.get(file) || EMPTY_ASSET_INFO;
  4279. if (typeof assetInfoUpdateOrFunction === "function") {
  4280. this._setAssetInfo(file, assetInfoUpdateOrFunction(oldInfo), oldInfo);
  4281. } else {
  4282. this._setAssetInfo(
  4283. file,
  4284. cachedCleverMerge(oldInfo, assetInfoUpdateOrFunction),
  4285. oldInfo
  4286. );
  4287. }
  4288. }
  4289. }
  4290. /**
  4291. * @param {string} file file name
  4292. * @param {string} newFile the new name of file
  4293. */
  4294. renameAsset(file, newFile) {
  4295. const source = this.assets[file];
  4296. if (!source) {
  4297. throw new Error(
  4298. `Called Compilation.renameAsset for not existing filename ${file}`
  4299. );
  4300. }
  4301. if (this.assets[newFile] && !isSourceEqual(this.assets[file], source)) {
  4302. this.errors.push(
  4303. new WebpackError(
  4304. `Conflict: Called Compilation.renameAsset for already existing filename ${newFile} with different content`
  4305. )
  4306. );
  4307. }
  4308. const assetInfo = this.assetsInfo.get(file);
  4309. // Update related in all other assets
  4310. const relatedInInfo = this._assetsRelatedIn.get(file);
  4311. if (relatedInInfo) {
  4312. for (const [key, assets] of relatedInInfo) {
  4313. for (const name of assets) {
  4314. const info = this.assetsInfo.get(name);
  4315. if (!info) continue;
  4316. const related = info.related;
  4317. if (!related) continue;
  4318. const entry = related[key];
  4319. let newEntry;
  4320. if (Array.isArray(entry)) {
  4321. newEntry = entry.map(x => (x === file ? newFile : x));
  4322. } else if (entry === file) {
  4323. newEntry = newFile;
  4324. } else continue;
  4325. this.assetsInfo.set(name, {
  4326. ...info,
  4327. related: {
  4328. ...related,
  4329. [key]: newEntry
  4330. }
  4331. });
  4332. }
  4333. }
  4334. }
  4335. this._setAssetInfo(file, undefined, assetInfo);
  4336. this._setAssetInfo(newFile, assetInfo);
  4337. delete this.assets[file];
  4338. this.assets[newFile] = source;
  4339. for (const chunk of this.chunks) {
  4340. {
  4341. const size = chunk.files.size;
  4342. chunk.files.delete(file);
  4343. if (size !== chunk.files.size) {
  4344. chunk.files.add(newFile);
  4345. }
  4346. }
  4347. {
  4348. const size = chunk.auxiliaryFiles.size;
  4349. chunk.auxiliaryFiles.delete(file);
  4350. if (size !== chunk.auxiliaryFiles.size) {
  4351. chunk.auxiliaryFiles.add(newFile);
  4352. }
  4353. }
  4354. }
  4355. }
  4356. /**
  4357. * @param {string} file file name
  4358. */
  4359. deleteAsset(file) {
  4360. if (!this.assets[file]) {
  4361. return;
  4362. }
  4363. delete this.assets[file];
  4364. const assetInfo = this.assetsInfo.get(file);
  4365. this._setAssetInfo(file, undefined, assetInfo);
  4366. const related = assetInfo && assetInfo.related;
  4367. if (related) {
  4368. for (const key of Object.keys(related)) {
  4369. /**
  4370. * @param {string} file file
  4371. */
  4372. const checkUsedAndDelete = file => {
  4373. if (!this._assetsRelatedIn.has(file)) {
  4374. this.deleteAsset(file);
  4375. }
  4376. };
  4377. const items = related[key];
  4378. if (Array.isArray(items)) {
  4379. for (const file of items) {
  4380. checkUsedAndDelete(file);
  4381. }
  4382. } else if (items) {
  4383. checkUsedAndDelete(items);
  4384. }
  4385. }
  4386. }
  4387. // TODO If this becomes a performance problem
  4388. // store a reverse mapping from asset to chunk
  4389. for (const chunk of this.chunks) {
  4390. chunk.files.delete(file);
  4391. chunk.auxiliaryFiles.delete(file);
  4392. }
  4393. }
  4394. getAssets() {
  4395. /** @type {Readonly<Asset>[]} */
  4396. const array = [];
  4397. for (const assetName of Object.keys(this.assets)) {
  4398. if (Object.prototype.hasOwnProperty.call(this.assets, assetName)) {
  4399. array.push({
  4400. name: assetName,
  4401. source: this.assets[assetName],
  4402. info: this.assetsInfo.get(assetName) || EMPTY_ASSET_INFO
  4403. });
  4404. }
  4405. }
  4406. return array;
  4407. }
  4408. /**
  4409. * @param {string} name the name of the asset
  4410. * @returns {Readonly<Asset> | undefined} the asset or undefined when not found
  4411. */
  4412. getAsset(name) {
  4413. if (!Object.prototype.hasOwnProperty.call(this.assets, name)) return;
  4414. return {
  4415. name,
  4416. source: this.assets[name],
  4417. info: this.assetsInfo.get(name) || EMPTY_ASSET_INFO
  4418. };
  4419. }
  4420. clearAssets() {
  4421. for (const chunk of this.chunks) {
  4422. chunk.files.clear();
  4423. chunk.auxiliaryFiles.clear();
  4424. }
  4425. }
  4426. createModuleAssets() {
  4427. const { chunkGraph } = this;
  4428. for (const module of this.modules) {
  4429. const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
  4430. if (buildInfo.assets) {
  4431. const assetsInfo = buildInfo.assetsInfo;
  4432. for (const assetName of Object.keys(buildInfo.assets)) {
  4433. const fileName = this.getPath(assetName, {
  4434. chunkGraph: this.chunkGraph,
  4435. module
  4436. });
  4437. for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
  4438. chunk.auxiliaryFiles.add(fileName);
  4439. }
  4440. this.emitAsset(
  4441. fileName,
  4442. buildInfo.assets[assetName],
  4443. assetsInfo ? assetsInfo.get(assetName) : undefined
  4444. );
  4445. this.hooks.moduleAsset.call(module, fileName);
  4446. }
  4447. }
  4448. }
  4449. }
  4450. /**
  4451. * @param {RenderManifestOptions} options options object
  4452. * @returns {RenderManifestEntry[]} manifest entries
  4453. */
  4454. getRenderManifest(options) {
  4455. return this.hooks.renderManifest.call([], options);
  4456. }
  4457. /**
  4458. * @param {Callback} callback signals when the call finishes
  4459. * @returns {void}
  4460. */
  4461. createChunkAssets(callback) {
  4462. const outputOptions = this.outputOptions;
  4463. const cachedSourceMap = new WeakMap();
  4464. /** @type {Map<string, {hash: string, source: Source, chunk: Chunk}>} */
  4465. const alreadyWrittenFiles = new Map();
  4466. asyncLib.forEachLimit(
  4467. this.chunks,
  4468. 15,
  4469. (chunk, callback) => {
  4470. /** @type {RenderManifestEntry[]} */
  4471. let manifest;
  4472. try {
  4473. manifest = this.getRenderManifest({
  4474. chunk,
  4475. hash: /** @type {string} */ (this.hash),
  4476. fullHash: /** @type {string} */ (this.fullHash),
  4477. outputOptions,
  4478. codeGenerationResults: this.codeGenerationResults,
  4479. moduleTemplates: this.moduleTemplates,
  4480. dependencyTemplates: this.dependencyTemplates,
  4481. chunkGraph: this.chunkGraph,
  4482. moduleGraph: this.moduleGraph,
  4483. runtimeTemplate: this.runtimeTemplate
  4484. });
  4485. } catch (err) {
  4486. this.errors.push(
  4487. new ChunkRenderError(chunk, "", /** @type {Error} */ (err))
  4488. );
  4489. return callback();
  4490. }
  4491. asyncLib.each(
  4492. manifest,
  4493. (fileManifest, callback) => {
  4494. const ident = fileManifest.identifier;
  4495. const usedHash = fileManifest.hash;
  4496. const assetCacheItem = this._assetsCache.getItemCache(
  4497. ident,
  4498. usedHash
  4499. );
  4500. assetCacheItem.get((err, sourceFromCache) => {
  4501. /** @type {TemplatePath} */
  4502. let filenameTemplate;
  4503. /** @type {string} */
  4504. let file;
  4505. /** @type {AssetInfo} */
  4506. let assetInfo;
  4507. let inTry = true;
  4508. /**
  4509. * @param {Error} err error
  4510. * @returns {void}
  4511. */
  4512. const errorAndCallback = err => {
  4513. const filename =
  4514. file ||
  4515. (typeof file === "string"
  4516. ? file
  4517. : typeof filenameTemplate === "string"
  4518. ? filenameTemplate
  4519. : "");
  4520. this.errors.push(new ChunkRenderError(chunk, filename, err));
  4521. inTry = false;
  4522. return callback();
  4523. };
  4524. try {
  4525. if ("filename" in fileManifest) {
  4526. file = fileManifest.filename;
  4527. assetInfo = fileManifest.info;
  4528. } else {
  4529. filenameTemplate = fileManifest.filenameTemplate;
  4530. const pathAndInfo = this.getPathWithInfo(
  4531. filenameTemplate,
  4532. fileManifest.pathOptions
  4533. );
  4534. file = pathAndInfo.path;
  4535. assetInfo = fileManifest.info
  4536. ? {
  4537. ...pathAndInfo.info,
  4538. ...fileManifest.info
  4539. }
  4540. : pathAndInfo.info;
  4541. }
  4542. if (err) {
  4543. return errorAndCallback(err);
  4544. }
  4545. let source = sourceFromCache;
  4546. // check if the same filename was already written by another chunk
  4547. const alreadyWritten = alreadyWrittenFiles.get(file);
  4548. if (alreadyWritten !== undefined) {
  4549. if (alreadyWritten.hash !== usedHash) {
  4550. inTry = false;
  4551. return callback(
  4552. new WebpackError(
  4553. `Conflict: Multiple chunks emit assets to the same filename ${file}` +
  4554. ` (chunks ${alreadyWritten.chunk.id} and ${chunk.id})`
  4555. )
  4556. );
  4557. }
  4558. source = alreadyWritten.source;
  4559. } else if (!source) {
  4560. // render the asset
  4561. source = fileManifest.render();
  4562. // Ensure that source is a cached source to avoid additional cost because of repeated access
  4563. if (!(source instanceof CachedSource)) {
  4564. const cacheEntry = cachedSourceMap.get(source);
  4565. if (cacheEntry) {
  4566. source = cacheEntry;
  4567. } else {
  4568. const cachedSource = new CachedSource(source);
  4569. cachedSourceMap.set(source, cachedSource);
  4570. source = cachedSource;
  4571. }
  4572. }
  4573. }
  4574. this.emitAsset(file, source, assetInfo);
  4575. if (fileManifest.auxiliary) {
  4576. chunk.auxiliaryFiles.add(file);
  4577. } else {
  4578. chunk.files.add(file);
  4579. }
  4580. this.hooks.chunkAsset.call(chunk, file);
  4581. alreadyWrittenFiles.set(file, {
  4582. hash: usedHash,
  4583. source,
  4584. chunk
  4585. });
  4586. if (source !== sourceFromCache) {
  4587. assetCacheItem.store(source, err => {
  4588. if (err) return errorAndCallback(err);
  4589. inTry = false;
  4590. return callback();
  4591. });
  4592. } else {
  4593. inTry = false;
  4594. callback();
  4595. }
  4596. } catch (err) {
  4597. if (!inTry) throw err;
  4598. errorAndCallback(/** @type {Error} */ (err));
  4599. }
  4600. });
  4601. },
  4602. callback
  4603. );
  4604. },
  4605. callback
  4606. );
  4607. }
  4608. /**
  4609. * @param {TemplatePath} filename used to get asset path with hash
  4610. * @param {PathData} data context data
  4611. * @returns {string} interpolated path
  4612. */
  4613. getPath(filename, data = {}) {
  4614. if (!data.hash) {
  4615. data = {
  4616. hash: this.hash,
  4617. ...data
  4618. };
  4619. }
  4620. return this.getAssetPath(filename, data);
  4621. }
  4622. /**
  4623. * @param {TemplatePath} filename used to get asset path with hash
  4624. * @param {PathData} data context data
  4625. * @returns {InterpolatedPathAndAssetInfo} interpolated path and asset info
  4626. */
  4627. getPathWithInfo(filename, data = {}) {
  4628. if (!data.hash) {
  4629. data = {
  4630. hash: this.hash,
  4631. ...data
  4632. };
  4633. }
  4634. return this.getAssetPathWithInfo(filename, data);
  4635. }
  4636. /**
  4637. * @param {TemplatePath} filename used to get asset path with hash
  4638. * @param {PathData} data context data
  4639. * @returns {string} interpolated path
  4640. */
  4641. getAssetPath(filename, data) {
  4642. return this.hooks.assetPath.call(
  4643. typeof filename === "function" ? filename(data) : filename,
  4644. data,
  4645. undefined
  4646. );
  4647. }
  4648. /**
  4649. * @param {TemplatePath} filename used to get asset path with hash
  4650. * @param {PathData} data context data
  4651. * @returns {InterpolatedPathAndAssetInfo} interpolated path and asset info
  4652. */
  4653. getAssetPathWithInfo(filename, data) {
  4654. const assetInfo = {};
  4655. // TODO webpack 5: refactor assetPath hook to receive { path, info } object
  4656. const newPath = this.hooks.assetPath.call(
  4657. typeof filename === "function" ? filename(data, assetInfo) : filename,
  4658. data,
  4659. assetInfo
  4660. );
  4661. return { path: newPath, info: assetInfo };
  4662. }
  4663. getWarnings() {
  4664. return this.hooks.processWarnings.call(this.warnings);
  4665. }
  4666. getErrors() {
  4667. return this.hooks.processErrors.call(this.errors);
  4668. }
  4669. /**
  4670. * This function allows you to run another instance of webpack inside of webpack however as
  4671. * a child with different settings and configurations (if desired) applied. It copies all hooks, plugins
  4672. * from parent (or top level compiler) and creates a child Compilation
  4673. * @param {string} name name of the child compiler
  4674. * @param {Partial<OutputOptions>=} outputOptions // Need to convert config schema to types for this
  4675. * @param {Array<WebpackPluginInstance | WebpackPluginFunction>=} plugins webpack plugins that will be applied
  4676. * @returns {Compiler} creates a child Compiler instance
  4677. */
  4678. createChildCompiler(name, outputOptions, plugins) {
  4679. const idx = this.childrenCounters[name] || 0;
  4680. this.childrenCounters[name] = idx + 1;
  4681. return this.compiler.createChildCompiler(
  4682. this,
  4683. name,
  4684. idx,
  4685. outputOptions,
  4686. plugins
  4687. );
  4688. }
  4689. /**
  4690. * @param {Module} module the module
  4691. * @param {ExecuteModuleOptions} options options
  4692. * @param {ExecuteModuleCallback} callback callback
  4693. */
  4694. executeModule(module, options, callback) {
  4695. // Aggregate all referenced modules and ensure they are ready
  4696. const modules = new Set([module]);
  4697. processAsyncTree(
  4698. modules,
  4699. 10,
  4700. (module, push, callback) => {
  4701. this.buildQueue.waitFor(module, err => {
  4702. if (err) return callback(err);
  4703. this.processDependenciesQueue.waitFor(module, err => {
  4704. if (err) return callback(err);
  4705. for (const { module: m } of this.moduleGraph.getOutgoingConnections(
  4706. module
  4707. )) {
  4708. const size = modules.size;
  4709. modules.add(m);
  4710. if (modules.size !== size) push(m);
  4711. }
  4712. callback();
  4713. });
  4714. });
  4715. },
  4716. err => {
  4717. if (err) return callback(/** @type {WebpackError} */ (err));
  4718. // Create new chunk graph, chunk and entrypoint for the build time execution
  4719. const chunkGraph = new ChunkGraph(
  4720. this.moduleGraph,
  4721. this.outputOptions.hashFunction
  4722. );
  4723. const runtime = "build time";
  4724. const { hashFunction, hashDigest, hashDigestLength } =
  4725. this.outputOptions;
  4726. const runtimeTemplate = this.runtimeTemplate;
  4727. const chunk = new Chunk("build time chunk", this._backCompat);
  4728. chunk.id = /** @type {ChunkId} */ (chunk.name);
  4729. chunk.ids = [chunk.id];
  4730. chunk.runtime = runtime;
  4731. const entrypoint = new Entrypoint({
  4732. runtime,
  4733. chunkLoading: false,
  4734. ...options.entryOptions
  4735. });
  4736. chunkGraph.connectChunkAndEntryModule(chunk, module, entrypoint);
  4737. connectChunkGroupAndChunk(entrypoint, chunk);
  4738. entrypoint.setRuntimeChunk(chunk);
  4739. entrypoint.setEntrypointChunk(chunk);
  4740. const chunks = new Set([chunk]);
  4741. // Assign ids to modules and modules to the chunk
  4742. for (const module of modules) {
  4743. const id = module.identifier();
  4744. chunkGraph.setModuleId(module, id);
  4745. chunkGraph.connectChunkAndModule(chunk, module);
  4746. }
  4747. /** @type {WebpackError[]} */
  4748. const errors = [];
  4749. // Hash modules
  4750. for (const module of modules) {
  4751. this._createModuleHash(
  4752. module,
  4753. chunkGraph,
  4754. runtime,
  4755. hashFunction,
  4756. runtimeTemplate,
  4757. hashDigest,
  4758. hashDigestLength,
  4759. errors
  4760. );
  4761. }
  4762. const codeGenerationResults = new CodeGenerationResults(
  4763. this.outputOptions.hashFunction
  4764. );
  4765. /**
  4766. * @param {Module} module the module
  4767. * @param {Callback} callback callback
  4768. * @returns {void}
  4769. */
  4770. const codeGen = (module, callback) => {
  4771. this._codeGenerationModule(
  4772. module,
  4773. runtime,
  4774. [runtime],
  4775. chunkGraph.getModuleHash(module, runtime),
  4776. this.dependencyTemplates,
  4777. chunkGraph,
  4778. this.moduleGraph,
  4779. runtimeTemplate,
  4780. errors,
  4781. codeGenerationResults,
  4782. (err, codeGenerated) => {
  4783. callback(err);
  4784. }
  4785. );
  4786. };
  4787. const reportErrors = () => {
  4788. if (errors.length > 0) {
  4789. errors.sort(
  4790. compareSelect(err => err.module, compareModulesByIdentifier)
  4791. );
  4792. for (const error of errors) {
  4793. this.errors.push(error);
  4794. }
  4795. errors.length = 0;
  4796. }
  4797. };
  4798. // Generate code for all aggregated modules
  4799. asyncLib.eachLimit(modules, 10, codeGen, err => {
  4800. if (err) return callback(err);
  4801. reportErrors();
  4802. // for backward-compat temporary set the chunk graph
  4803. // TODO webpack 6
  4804. const old = this.chunkGraph;
  4805. this.chunkGraph = chunkGraph;
  4806. this.processRuntimeRequirements({
  4807. chunkGraph,
  4808. modules,
  4809. chunks,
  4810. codeGenerationResults,
  4811. chunkGraphEntries: chunks
  4812. });
  4813. this.chunkGraph = old;
  4814. const runtimeModules =
  4815. chunkGraph.getChunkRuntimeModulesIterable(chunk);
  4816. // Hash runtime modules
  4817. for (const module of runtimeModules) {
  4818. modules.add(module);
  4819. this._createModuleHash(
  4820. module,
  4821. chunkGraph,
  4822. runtime,
  4823. hashFunction,
  4824. runtimeTemplate,
  4825. hashDigest,
  4826. hashDigestLength,
  4827. errors
  4828. );
  4829. }
  4830. // Generate code for all runtime modules
  4831. asyncLib.eachLimit(runtimeModules, 10, codeGen, err => {
  4832. if (err) return callback(err);
  4833. reportErrors();
  4834. /** @type {Map<Module, ExecuteModuleArgument>} */
  4835. const moduleArgumentsMap = new Map();
  4836. /** @type {Map<string, ExecuteModuleArgument>} */
  4837. const moduleArgumentsById = new Map();
  4838. /** @type {ExecuteModuleResult["fileDependencies"]} */
  4839. const fileDependencies = new LazySet();
  4840. /** @type {ExecuteModuleResult["contextDependencies"]} */
  4841. const contextDependencies = new LazySet();
  4842. /** @type {ExecuteModuleResult["missingDependencies"]} */
  4843. const missingDependencies = new LazySet();
  4844. /** @type {ExecuteModuleResult["buildDependencies"]} */
  4845. const buildDependencies = new LazySet();
  4846. /** @type {ExecuteModuleResult["assets"]} */
  4847. const assets = new Map();
  4848. let cacheable = true;
  4849. /** @type {ExecuteModuleContext} */
  4850. const context = {
  4851. assets,
  4852. __webpack_require__: undefined,
  4853. chunk,
  4854. chunkGraph
  4855. };
  4856. // Prepare execution
  4857. asyncLib.eachLimit(
  4858. modules,
  4859. 10,
  4860. (module, callback) => {
  4861. const codeGenerationResult = codeGenerationResults.get(
  4862. module,
  4863. runtime
  4864. );
  4865. /** @type {ExecuteModuleArgument} */
  4866. const moduleArgument = {
  4867. module,
  4868. codeGenerationResult,
  4869. preparedInfo: undefined,
  4870. moduleObject: undefined
  4871. };
  4872. moduleArgumentsMap.set(module, moduleArgument);
  4873. moduleArgumentsById.set(module.identifier(), moduleArgument);
  4874. module.addCacheDependencies(
  4875. fileDependencies,
  4876. contextDependencies,
  4877. missingDependencies,
  4878. buildDependencies
  4879. );
  4880. if (
  4881. /** @type {BuildInfo} */ (module.buildInfo).cacheable ===
  4882. false
  4883. ) {
  4884. cacheable = false;
  4885. }
  4886. if (module.buildInfo && module.buildInfo.assets) {
  4887. const { assets: moduleAssets, assetsInfo } = module.buildInfo;
  4888. for (const assetName of Object.keys(moduleAssets)) {
  4889. assets.set(assetName, {
  4890. source: moduleAssets[assetName],
  4891. info: assetsInfo ? assetsInfo.get(assetName) : undefined
  4892. });
  4893. }
  4894. }
  4895. this.hooks.prepareModuleExecution.callAsync(
  4896. moduleArgument,
  4897. context,
  4898. callback
  4899. );
  4900. },
  4901. err => {
  4902. if (err) return callback(err);
  4903. let exports;
  4904. try {
  4905. const {
  4906. strictModuleErrorHandling,
  4907. strictModuleExceptionHandling
  4908. } = this.outputOptions;
  4909. const __webpack_require__ = id => {
  4910. const cached = moduleCache[id];
  4911. if (cached !== undefined) {
  4912. if (cached.error) throw cached.error;
  4913. return cached.exports;
  4914. }
  4915. const moduleArgument = moduleArgumentsById.get(id);
  4916. return __webpack_require_module__(moduleArgument, id);
  4917. };
  4918. const interceptModuleExecution = (__webpack_require__[
  4919. RuntimeGlobals.interceptModuleExecution.replace(
  4920. `${RuntimeGlobals.require}.`,
  4921. ""
  4922. )
  4923. ] = []);
  4924. const moduleCache = (__webpack_require__[
  4925. RuntimeGlobals.moduleCache.replace(
  4926. `${RuntimeGlobals.require}.`,
  4927. ""
  4928. )
  4929. ] = {});
  4930. context.__webpack_require__ = __webpack_require__;
  4931. /**
  4932. * @param {ExecuteModuleArgument} moduleArgument the module argument
  4933. * @param {string=} id id
  4934. * @returns {any} exports
  4935. */
  4936. const __webpack_require_module__ = (moduleArgument, id) => {
  4937. const execOptions = {
  4938. id,
  4939. module: {
  4940. id,
  4941. exports: {},
  4942. loaded: false,
  4943. error: undefined
  4944. },
  4945. require: __webpack_require__
  4946. };
  4947. for (const handler of interceptModuleExecution) {
  4948. handler(execOptions);
  4949. }
  4950. const module = moduleArgument.module;
  4951. this.buildTimeExecutedModules.add(module);
  4952. const moduleObject = execOptions.module;
  4953. moduleArgument.moduleObject = moduleObject;
  4954. try {
  4955. if (id) moduleCache[id] = moduleObject;
  4956. tryRunOrWebpackError(
  4957. () =>
  4958. this.hooks.executeModule.call(
  4959. moduleArgument,
  4960. context
  4961. ),
  4962. "Compilation.hooks.executeModule"
  4963. );
  4964. moduleObject.loaded = true;
  4965. return moduleObject.exports;
  4966. } catch (execErr) {
  4967. if (strictModuleExceptionHandling) {
  4968. if (id) delete moduleCache[id];
  4969. } else if (strictModuleErrorHandling) {
  4970. moduleObject.error = execErr;
  4971. }
  4972. if (!execErr.module) execErr.module = module;
  4973. throw execErr;
  4974. }
  4975. };
  4976. for (const runtimeModule of chunkGraph.getChunkRuntimeModulesInOrder(
  4977. chunk
  4978. )) {
  4979. __webpack_require_module__(
  4980. /** @type {ExecuteModuleArgument} */
  4981. (moduleArgumentsMap.get(runtimeModule))
  4982. );
  4983. }
  4984. exports = __webpack_require__(module.identifier());
  4985. } catch (execErr) {
  4986. const err = new WebpackError(
  4987. `Execution of module code from module graph (${module.readableIdentifier(
  4988. this.requestShortener
  4989. )}) failed: ${execErr.message}`
  4990. );
  4991. err.stack = execErr.stack;
  4992. err.module = execErr.module;
  4993. return callback(err);
  4994. }
  4995. callback(null, {
  4996. exports,
  4997. assets,
  4998. cacheable,
  4999. fileDependencies,
  5000. contextDependencies,
  5001. missingDependencies,
  5002. buildDependencies
  5003. });
  5004. }
  5005. );
  5006. });
  5007. });
  5008. }
  5009. );
  5010. }
  5011. checkConstraints() {
  5012. const chunkGraph = this.chunkGraph;
  5013. /** @type {Set<number|string>} */
  5014. const usedIds = new Set();
  5015. for (const module of this.modules) {
  5016. if (module.type === WEBPACK_MODULE_TYPE_RUNTIME) continue;
  5017. const moduleId = chunkGraph.getModuleId(module);
  5018. if (moduleId === null) continue;
  5019. if (usedIds.has(moduleId)) {
  5020. throw new Error(`checkConstraints: duplicate module id ${moduleId}`);
  5021. }
  5022. usedIds.add(moduleId);
  5023. }
  5024. for (const chunk of this.chunks) {
  5025. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  5026. if (!this.modules.has(module)) {
  5027. throw new Error(
  5028. "checkConstraints: module in chunk but not in compilation " +
  5029. ` ${chunk.debugId} ${module.debugId}`
  5030. );
  5031. }
  5032. }
  5033. for (const module of chunkGraph.getChunkEntryModulesIterable(chunk)) {
  5034. if (!this.modules.has(module)) {
  5035. throw new Error(
  5036. "checkConstraints: entry module in chunk but not in compilation " +
  5037. ` ${chunk.debugId} ${module.debugId}`
  5038. );
  5039. }
  5040. }
  5041. }
  5042. for (const chunkGroup of this.chunkGroups) {
  5043. chunkGroup.checkConstraints();
  5044. }
  5045. }
  5046. }
  5047. /**
  5048. * @typedef {object} FactorizeModuleOptions
  5049. * @property {ModuleProfile=} currentProfile
  5050. * @property {ModuleFactory} factory
  5051. * @property {Dependency[]} dependencies
  5052. * @property {boolean=} factoryResult return full ModuleFactoryResult instead of only module
  5053. * @property {Module | null} originModule
  5054. * @property {Partial<ModuleFactoryCreateDataContextInfo>=} contextInfo
  5055. * @property {string=} context
  5056. */
  5057. /**
  5058. * @param {FactorizeModuleOptions} options options object
  5059. * @param {ModuleCallback | ModuleFactoryResultCallback} callback callback
  5060. * @returns {void}
  5061. */
  5062. // Workaround for typescript as it doesn't support function overloading in jsdoc within a class
  5063. /* eslint-disable jsdoc/require-asterisk-prefix */
  5064. Compilation.prototype.factorizeModule = /**
  5065. @type {{
  5066. (options: FactorizeModuleOptions & { factoryResult?: false }, callback: ModuleCallback): void;
  5067. (options: FactorizeModuleOptions & { factoryResult: true }, callback: ModuleFactoryResultCallback): void;
  5068. }} */ (
  5069. function (options, callback) {
  5070. this.factorizeQueue.add(options, callback);
  5071. }
  5072. );
  5073. /* eslint-enable jsdoc/require-asterisk-prefix */
  5074. // Hide from typescript
  5075. const compilationPrototype = Compilation.prototype;
  5076. // TODO webpack 6 remove
  5077. Object.defineProperty(compilationPrototype, "modifyHash", {
  5078. writable: false,
  5079. enumerable: false,
  5080. configurable: false,
  5081. value: () => {
  5082. throw new Error(
  5083. "Compilation.modifyHash was removed in favor of Compilation.hooks.fullHash"
  5084. );
  5085. }
  5086. });
  5087. // TODO webpack 6 remove
  5088. Object.defineProperty(compilationPrototype, "cache", {
  5089. enumerable: false,
  5090. configurable: false,
  5091. get: util.deprecate(
  5092. /**
  5093. * @this {Compilation} the compilation
  5094. * @returns {Cache} the cache
  5095. */
  5096. function () {
  5097. return this.compiler.cache;
  5098. },
  5099. "Compilation.cache was removed in favor of Compilation.getCache()",
  5100. "DEP_WEBPACK_COMPILATION_CACHE"
  5101. ),
  5102. set: util.deprecate(
  5103. /**
  5104. * @param {any} v value
  5105. */
  5106. v => {},
  5107. "Compilation.cache was removed in favor of Compilation.getCache()",
  5108. "DEP_WEBPACK_COMPILATION_CACHE"
  5109. )
  5110. });
  5111. /**
  5112. * Add additional assets to the compilation.
  5113. */
  5114. Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL = -2000;
  5115. /**
  5116. * Basic preprocessing of assets.
  5117. */
  5118. Compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS = -1000;
  5119. /**
  5120. * Derive new assets from existing assets.
  5121. * Existing assets should not be treated as complete.
  5122. */
  5123. Compilation.PROCESS_ASSETS_STAGE_DERIVED = -200;
  5124. /**
  5125. * Add additional sections to existing assets, like a banner or initialization code.
  5126. */
  5127. Compilation.PROCESS_ASSETS_STAGE_ADDITIONS = -100;
  5128. /**
  5129. * Optimize existing assets in a general way.
  5130. */
  5131. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE = 100;
  5132. /**
  5133. * Optimize the count of existing assets, e. g. by merging them.
  5134. * Only assets of the same type should be merged.
  5135. * For assets of different types see PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE.
  5136. */
  5137. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT = 200;
  5138. /**
  5139. * Optimize the compatibility of existing assets, e. g. add polyfills or vendor-prefixes.
  5140. */
  5141. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY = 300;
  5142. /**
  5143. * Optimize the size of existing assets, e. g. by minimizing or omitting whitespace.
  5144. */
  5145. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE = 400;
  5146. /**
  5147. * Add development tooling to assets, e. g. by extracting a SourceMap.
  5148. */
  5149. Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING = 500;
  5150. /**
  5151. * Optimize the count of existing assets, e. g. by inlining assets of into other assets.
  5152. * Only assets of different types should be inlined.
  5153. * For assets of the same type see PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT.
  5154. */
  5155. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE = 700;
  5156. /**
  5157. * Summarize the list of existing assets
  5158. * e. g. creating an assets manifest of Service Workers.
  5159. */
  5160. Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE = 1000;
  5161. /**
  5162. * Optimize the hashes of the assets, e. g. by generating real hashes of the asset content.
  5163. */
  5164. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH = 2500;
  5165. /**
  5166. * Optimize the transfer of existing assets, e. g. by preparing a compressed (gzip) file as separate asset.
  5167. */
  5168. Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER = 3000;
  5169. /**
  5170. * Analyse existing assets.
  5171. */
  5172. Compilation.PROCESS_ASSETS_STAGE_ANALYSE = 4000;
  5173. /**
  5174. * Creating assets for reporting purposes.
  5175. */
  5176. Compilation.PROCESS_ASSETS_STAGE_REPORT = 5000;
  5177. module.exports = Compilation;