Compilation.js 158 KB

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