Compilation.js 157 KB

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