Compilation.js 157 KB

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