dep-DkOS1hkm.js 2.2 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795
  1. import fs$l, { promises as promises$1 } from 'node:fs';
  2. import fsp from 'node:fs/promises';
  3. import path$o, { posix as posix$1, isAbsolute as isAbsolute$2, join as join$2, extname as extname$1, dirname as dirname$2, relative as relative$2, basename as basename$2 } from 'node:path';
  4. import { fileURLToPath, URL as URL$3, parse as parse$i, pathToFileURL } from 'node:url';
  5. import { promisify as promisify$4, format as format$2, inspect } from 'node:util';
  6. import { performance } from 'node:perf_hooks';
  7. import { createRequire as createRequire$1, builtinModules } from 'node:module';
  8. import require$$0$3 from 'tty';
  9. import require$$0$4, { win32, posix, isAbsolute as isAbsolute$1, resolve as resolve$3, relative as relative$1, basename as basename$1, extname, dirname as dirname$1, join as join$1, sep, normalize } from 'path';
  10. import esbuild, { transform as transform$1, formatMessages, build as build$3 } from 'esbuild';
  11. import { CLIENT_ENTRY, OPTIMIZABLE_ENTRY_RE, wildcardHosts, loopbackHosts, FS_PREFIX, CLIENT_PUBLIC_PATH, ENV_PUBLIC_PATH, DEFAULT_ASSETS_INLINE_LIMIT, CSS_LANGS_RE, ESBUILD_MODULES_TARGET, SPECIAL_QUERY_RE, ENV_ENTRY, DEP_VERSION_RE, DEFAULT_MAIN_FIELDS, DEFAULT_EXTENSIONS, KNOWN_ASSET_TYPES, JS_TYPES_RE, METADATA_FILENAME, VITE_PACKAGE_DIR, DEFAULT_DEV_PORT, CLIENT_DIR, VERSION, DEFAULT_PREVIEW_PORT, DEFAULT_ASSETS_RE, DEFAULT_CONFIG_FILES } from '../constants.js';
  12. import * as require$$0$2 from 'fs';
  13. import require$$0__default, { existsSync, readFileSync, statSync as statSync$1, readdirSync } from 'fs';
  14. import require$$0$5 from 'events';
  15. import require$$5 from 'assert';
  16. import { exec, execSync } from 'node:child_process';
  17. import { createServer as createServer$3, STATUS_CODES, get as get$2 } from 'node:http';
  18. import { createServer as createServer$2, get as get$1 } from 'node:https';
  19. import require$$0$6 from 'util';
  20. import require$$4$1 from 'net';
  21. import require$$0$9 from 'url';
  22. import require$$1 from 'http';
  23. import require$$0$7 from 'stream';
  24. import require$$2 from 'os';
  25. import require$$2$1 from 'child_process';
  26. import os$4 from 'node:os';
  27. import { createHash as createHash$2 } from 'node:crypto';
  28. import { promises } from 'node:dns';
  29. import require$$3$1 from 'crypto';
  30. import require$$0$8, { createRequire as createRequire$2 } from 'module';
  31. import assert$1 from 'node:assert';
  32. import v8 from 'node:v8';
  33. import { Worker as Worker$1 } from 'node:worker_threads';
  34. import { Buffer as Buffer$1 } from 'node:buffer';
  35. import { EventEmitter as EventEmitter$4 } from 'node:events';
  36. import { parseAst, parseAstAsync } from 'rollup/parseAst';
  37. import * as qs from 'querystring';
  38. import readline from 'node:readline';
  39. import zlib$1 from 'zlib';
  40. import require$$0$a from 'buffer';
  41. import require$$1$1 from 'https';
  42. import require$$4$2 from 'tls';
  43. import { gzip } from 'node:zlib';
  44. import { fileURLToPath as __cjs_fileURLToPath } from 'node:url';
  45. import { dirname as __cjs_dirname } from 'node:path';
  46. import { createRequire as __cjs_createRequire } from 'node:module';
  47. const __filename = __cjs_fileURLToPath(import.meta.url);
  48. const __dirname = __cjs_dirname(__filename);
  49. const require = __cjs_createRequire(import.meta.url);
  50. const __require = require;
  51. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  52. function getDefaultExportFromCjs (x) {
  53. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  54. }
  55. function getAugmentedNamespace(n) {
  56. if (n.__esModule) return n;
  57. var f = n.default;
  58. if (typeof f == "function") {
  59. var a = function a () {
  60. if (this instanceof a) {
  61. return Reflect.construct(f, arguments, this.constructor);
  62. }
  63. return f.apply(this, arguments);
  64. };
  65. a.prototype = f.prototype;
  66. } else a = {};
  67. Object.defineProperty(a, '__esModule', {value: true});
  68. Object.keys(n).forEach(function (k) {
  69. var d = Object.getOwnPropertyDescriptor(n, k);
  70. Object.defineProperty(a, k, d.get ? d : {
  71. enumerable: true,
  72. get: function () {
  73. return n[k];
  74. }
  75. });
  76. });
  77. return a;
  78. }
  79. var picocolors = {exports: {}};
  80. let tty = require$$0$3;
  81. let isColorSupported =
  82. !("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
  83. ("FORCE_COLOR" in process.env ||
  84. process.argv.includes("--color") ||
  85. process.platform === "win32" ||
  86. (tty.isatty(1) && process.env.TERM !== "dumb") ||
  87. "CI" in process.env);
  88. let formatter =
  89. (open, close, replace = open) =>
  90. input => {
  91. let string = "" + input;
  92. let index = string.indexOf(close, open.length);
  93. return ~index
  94. ? open + replaceClose(string, close, replace, index) + close
  95. : open + string + close
  96. };
  97. let replaceClose = (string, close, replace, index) => {
  98. let start = string.substring(0, index) + replace;
  99. let end = string.substring(index + close.length);
  100. let nextIndex = end.indexOf(close);
  101. return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
  102. };
  103. let createColors = (enabled = isColorSupported) => ({
  104. isColorSupported: enabled,
  105. reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String,
  106. bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
  107. dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
  108. italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
  109. underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
  110. inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
  111. hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
  112. strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
  113. black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
  114. red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
  115. green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
  116. yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
  117. blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
  118. magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
  119. cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
  120. white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
  121. gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
  122. bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
  123. bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
  124. bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
  125. bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
  126. bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
  127. bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
  128. bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
  129. bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
  130. });
  131. picocolors.exports = createColors();
  132. picocolors.exports.createColors = createColors;
  133. var picocolorsExports = picocolors.exports;
  134. var colors$1 = /*@__PURE__*/getDefaultExportFromCjs(picocolorsExports);
  135. function matches$1(pattern, importee) {
  136. if (pattern instanceof RegExp) {
  137. return pattern.test(importee);
  138. }
  139. if (importee.length < pattern.length) {
  140. return false;
  141. }
  142. if (importee === pattern) {
  143. return true;
  144. }
  145. // eslint-disable-next-line prefer-template
  146. return importee.startsWith(pattern + '/');
  147. }
  148. function getEntries({ entries, customResolver }) {
  149. if (!entries) {
  150. return [];
  151. }
  152. const resolverFunctionFromOptions = resolveCustomResolver(customResolver);
  153. if (Array.isArray(entries)) {
  154. return entries.map((entry) => {
  155. return {
  156. find: entry.find,
  157. replacement: entry.replacement,
  158. resolverFunction: resolveCustomResolver(entry.customResolver) || resolverFunctionFromOptions
  159. };
  160. });
  161. }
  162. return Object.entries(entries).map(([key, value]) => {
  163. return { find: key, replacement: value, resolverFunction: resolverFunctionFromOptions };
  164. });
  165. }
  166. function getHookFunction(hook) {
  167. if (typeof hook === 'function') {
  168. return hook;
  169. }
  170. if (hook && 'handler' in hook && typeof hook.handler === 'function') {
  171. return hook.handler;
  172. }
  173. return null;
  174. }
  175. function resolveCustomResolver(customResolver) {
  176. if (typeof customResolver === 'function') {
  177. return customResolver;
  178. }
  179. if (customResolver) {
  180. return getHookFunction(customResolver.resolveId);
  181. }
  182. return null;
  183. }
  184. function alias$1(options = {}) {
  185. const entries = getEntries(options);
  186. if (entries.length === 0) {
  187. return {
  188. name: 'alias',
  189. resolveId: () => null
  190. };
  191. }
  192. return {
  193. name: 'alias',
  194. async buildStart(inputOptions) {
  195. await Promise.all([...(Array.isArray(options.entries) ? options.entries : []), options].map(({ customResolver }) => { var _a; return customResolver && ((_a = getHookFunction(customResolver.buildStart)) === null || _a === void 0 ? void 0 : _a.call(this, inputOptions)); }));
  196. },
  197. resolveId(importee, importer, resolveOptions) {
  198. // First match is supposed to be the correct one
  199. const matchedEntry = entries.find((entry) => matches$1(entry.find, importee));
  200. if (!matchedEntry) {
  201. return null;
  202. }
  203. const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement);
  204. if (matchedEntry.resolverFunction) {
  205. return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions);
  206. }
  207. return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => {
  208. if (resolved)
  209. return resolved;
  210. if (!require$$0$4.isAbsolute(updatedId)) {
  211. this.warn(`rewrote ${importee} to ${updatedId} but was not an abolute path and was not handled by other plugins. ` +
  212. `This will lead to duplicated modules for the same path. ` +
  213. `To avoid duplicating modules, you should resolve to an absolute path.`);
  214. }
  215. return { id: updatedId };
  216. });
  217. }
  218. };
  219. }
  220. /**
  221. * Prefix for resolved Ids that are not valid browser import specifiers
  222. */
  223. const VALID_ID_PREFIX = `/@id/`;
  224. /**
  225. * Plugins that use 'virtual modules' (e.g. for helper functions), prefix the
  226. * module ID with `\0`, a convention from the rollup ecosystem.
  227. * This prevents other plugins from trying to process the id (like node resolution),
  228. * and core features like sourcemaps can use this info to differentiate between
  229. * virtual modules and regular files.
  230. * `\0` is not a permitted char in import URLs so we have to replace them during
  231. * import analysis. The id will be decoded back before entering the plugins pipeline.
  232. * These encoded virtual ids are also prefixed by the VALID_ID_PREFIX, so virtual
  233. * modules in the browser end up encoded as `/@id/__x00__{id}`
  234. */
  235. const NULL_BYTE_PLACEHOLDER = `__x00__`;
  236. let SOURCEMAPPING_URL = 'sourceMa';
  237. SOURCEMAPPING_URL += 'ppingURL';
  238. const VITE_RUNTIME_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime';
  239. const isWindows$5 = typeof process !== 'undefined' && process.platform === 'win32';
  240. /**
  241. * Prepend `/@id/` and replace null byte so the id is URL-safe.
  242. * This is prepended to resolved ids that are not valid browser
  243. * import specifiers by the importAnalysis plugin.
  244. */
  245. function wrapId$1(id) {
  246. return id.startsWith(VALID_ID_PREFIX)
  247. ? id
  248. : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER);
  249. }
  250. /**
  251. * Undo {@link wrapId}'s `/@id/` and null byte replacements.
  252. */
  253. function unwrapId$1(id) {
  254. return id.startsWith(VALID_ID_PREFIX)
  255. ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
  256. : id;
  257. }
  258. const windowsSlashRE = /\\/g;
  259. function slash$1(p) {
  260. return p.replace(windowsSlashRE, '/');
  261. }
  262. const postfixRE = /[?#].*$/;
  263. function cleanUrl(url) {
  264. return url.replace(postfixRE, '');
  265. }
  266. function withTrailingSlash(path) {
  267. if (path[path.length - 1] !== '/') {
  268. return `${path}/`;
  269. }
  270. return path;
  271. }
  272. // eslint-disable-next-line @typescript-eslint/no-empty-function
  273. const AsyncFunction = async function () { }.constructor;
  274. // https://github.com/nodejs/node/issues/43047#issuecomment-1564068099
  275. const asyncFunctionDeclarationPaddingLineCount =
  276. /** #__PURE__ */ (() => {
  277. const body = '/*code*/';
  278. const source = new AsyncFunction('a', 'b', body).toString();
  279. return source.slice(0, source.indexOf(body)).split('\n').length - 1;
  280. })();
  281. // @ts-check
  282. /** @typedef { import('estree').BaseNode} BaseNode */
  283. /** @typedef {{
  284. skip: () => void;
  285. remove: () => void;
  286. replace: (node: BaseNode) => void;
  287. }} WalkerContext */
  288. let WalkerBase$1 = class WalkerBase {
  289. constructor() {
  290. /** @type {boolean} */
  291. this.should_skip = false;
  292. /** @type {boolean} */
  293. this.should_remove = false;
  294. /** @type {BaseNode | null} */
  295. this.replacement = null;
  296. /** @type {WalkerContext} */
  297. this.context = {
  298. skip: () => (this.should_skip = true),
  299. remove: () => (this.should_remove = true),
  300. replace: (node) => (this.replacement = node)
  301. };
  302. }
  303. /**
  304. *
  305. * @param {any} parent
  306. * @param {string} prop
  307. * @param {number} index
  308. * @param {BaseNode} node
  309. */
  310. replace(parent, prop, index, node) {
  311. if (parent) {
  312. if (index !== null) {
  313. parent[prop][index] = node;
  314. } else {
  315. parent[prop] = node;
  316. }
  317. }
  318. }
  319. /**
  320. *
  321. * @param {any} parent
  322. * @param {string} prop
  323. * @param {number} index
  324. */
  325. remove(parent, prop, index) {
  326. if (parent) {
  327. if (index !== null) {
  328. parent[prop].splice(index, 1);
  329. } else {
  330. delete parent[prop];
  331. }
  332. }
  333. }
  334. };
  335. // @ts-check
  336. /** @typedef { import('estree').BaseNode} BaseNode */
  337. /** @typedef { import('./walker.js').WalkerContext} WalkerContext */
  338. /** @typedef {(
  339. * this: WalkerContext,
  340. * node: BaseNode,
  341. * parent: BaseNode,
  342. * key: string,
  343. * index: number
  344. * ) => void} SyncHandler */
  345. let SyncWalker$1 = class SyncWalker extends WalkerBase$1 {
  346. /**
  347. *
  348. * @param {SyncHandler} enter
  349. * @param {SyncHandler} leave
  350. */
  351. constructor(enter, leave) {
  352. super();
  353. /** @type {SyncHandler} */
  354. this.enter = enter;
  355. /** @type {SyncHandler} */
  356. this.leave = leave;
  357. }
  358. /**
  359. *
  360. * @param {BaseNode} node
  361. * @param {BaseNode} parent
  362. * @param {string} [prop]
  363. * @param {number} [index]
  364. * @returns {BaseNode}
  365. */
  366. visit(node, parent, prop, index) {
  367. if (node) {
  368. if (this.enter) {
  369. const _should_skip = this.should_skip;
  370. const _should_remove = this.should_remove;
  371. const _replacement = this.replacement;
  372. this.should_skip = false;
  373. this.should_remove = false;
  374. this.replacement = null;
  375. this.enter.call(this.context, node, parent, prop, index);
  376. if (this.replacement) {
  377. node = this.replacement;
  378. this.replace(parent, prop, index, node);
  379. }
  380. if (this.should_remove) {
  381. this.remove(parent, prop, index);
  382. }
  383. const skipped = this.should_skip;
  384. const removed = this.should_remove;
  385. this.should_skip = _should_skip;
  386. this.should_remove = _should_remove;
  387. this.replacement = _replacement;
  388. if (skipped) return node;
  389. if (removed) return null;
  390. }
  391. for (const key in node) {
  392. const value = node[key];
  393. if (typeof value !== "object") {
  394. continue;
  395. } else if (Array.isArray(value)) {
  396. for (let i = 0; i < value.length; i += 1) {
  397. if (value[i] !== null && typeof value[i].type === 'string') {
  398. if (!this.visit(value[i], node, key, i)) {
  399. // removed
  400. i--;
  401. }
  402. }
  403. }
  404. } else if (value !== null && typeof value.type === "string") {
  405. this.visit(value, node, key, null);
  406. }
  407. }
  408. if (this.leave) {
  409. const _replacement = this.replacement;
  410. const _should_remove = this.should_remove;
  411. this.replacement = null;
  412. this.should_remove = false;
  413. this.leave.call(this.context, node, parent, prop, index);
  414. if (this.replacement) {
  415. node = this.replacement;
  416. this.replace(parent, prop, index, node);
  417. }
  418. if (this.should_remove) {
  419. this.remove(parent, prop, index);
  420. }
  421. const removed = this.should_remove;
  422. this.replacement = _replacement;
  423. this.should_remove = _should_remove;
  424. if (removed) return null;
  425. }
  426. }
  427. return node;
  428. }
  429. };
  430. // @ts-check
  431. /** @typedef { import('estree').BaseNode} BaseNode */
  432. /** @typedef { import('./sync.js').SyncHandler} SyncHandler */
  433. /** @typedef { import('./async.js').AsyncHandler} AsyncHandler */
  434. /**
  435. *
  436. * @param {BaseNode} ast
  437. * @param {{
  438. * enter?: SyncHandler
  439. * leave?: SyncHandler
  440. * }} walker
  441. * @returns {BaseNode}
  442. */
  443. function walk$3(ast, { enter, leave }) {
  444. const instance = new SyncWalker$1(enter, leave);
  445. return instance.visit(ast, null);
  446. }
  447. var utils$k = {};
  448. const path$n = require$$0$4;
  449. const WIN_SLASH = '\\\\/';
  450. const WIN_NO_SLASH = `[^${WIN_SLASH}]`;
  451. /**
  452. * Posix glob regex
  453. */
  454. const DOT_LITERAL = '\\.';
  455. const PLUS_LITERAL = '\\+';
  456. const QMARK_LITERAL = '\\?';
  457. const SLASH_LITERAL = '\\/';
  458. const ONE_CHAR = '(?=.)';
  459. const QMARK = '[^/]';
  460. const END_ANCHOR = `(?:${SLASH_LITERAL}|$)`;
  461. const START_ANCHOR = `(?:^|${SLASH_LITERAL})`;
  462. const DOTS_SLASH = `${DOT_LITERAL}{1,2}${END_ANCHOR}`;
  463. const NO_DOT = `(?!${DOT_LITERAL})`;
  464. const NO_DOTS = `(?!${START_ANCHOR}${DOTS_SLASH})`;
  465. const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
  466. const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
  467. const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
  468. const STAR$1 = `${QMARK}*?`;
  469. const POSIX_CHARS = {
  470. DOT_LITERAL,
  471. PLUS_LITERAL,
  472. QMARK_LITERAL,
  473. SLASH_LITERAL,
  474. ONE_CHAR,
  475. QMARK,
  476. END_ANCHOR,
  477. DOTS_SLASH,
  478. NO_DOT,
  479. NO_DOTS,
  480. NO_DOT_SLASH,
  481. NO_DOTS_SLASH,
  482. QMARK_NO_DOT,
  483. STAR: STAR$1,
  484. START_ANCHOR
  485. };
  486. /**
  487. * Windows glob regex
  488. */
  489. const WINDOWS_CHARS = {
  490. ...POSIX_CHARS,
  491. SLASH_LITERAL: `[${WIN_SLASH}]`,
  492. QMARK: WIN_NO_SLASH,
  493. STAR: `${WIN_NO_SLASH}*?`,
  494. DOTS_SLASH: `${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$)`,
  495. NO_DOT: `(?!${DOT_LITERAL})`,
  496. NO_DOTS: `(?!(?:^|[${WIN_SLASH}])${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  497. NO_DOT_SLASH: `(?!${DOT_LITERAL}{0,1}(?:[${WIN_SLASH}]|$))`,
  498. NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  499. QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
  500. START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
  501. END_ANCHOR: `(?:[${WIN_SLASH}]|$)`
  502. };
  503. /**
  504. * POSIX Bracket Regex
  505. */
  506. const POSIX_REGEX_SOURCE$1 = {
  507. alnum: 'a-zA-Z0-9',
  508. alpha: 'a-zA-Z',
  509. ascii: '\\x00-\\x7F',
  510. blank: ' \\t',
  511. cntrl: '\\x00-\\x1F\\x7F',
  512. digit: '0-9',
  513. graph: '\\x21-\\x7E',
  514. lower: 'a-z',
  515. print: '\\x20-\\x7E ',
  516. punct: '\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
  517. space: ' \\t\\r\\n\\v\\f',
  518. upper: 'A-Z',
  519. word: 'A-Za-z0-9_',
  520. xdigit: 'A-Fa-f0-9'
  521. };
  522. var constants$6 = {
  523. MAX_LENGTH: 1024 * 64,
  524. POSIX_REGEX_SOURCE: POSIX_REGEX_SOURCE$1,
  525. // regular expressions
  526. REGEX_BACKSLASH: /\\(?![*+?^${}(|)[\]])/g,
  527. REGEX_NON_SPECIAL_CHARS: /^[^@![\].,$*+?^{}()|\\/]+/,
  528. REGEX_SPECIAL_CHARS: /[-*+?.^${}(|)[\]]/,
  529. REGEX_SPECIAL_CHARS_BACKREF: /(\\?)((\W)(\3*))/g,
  530. REGEX_SPECIAL_CHARS_GLOBAL: /([-*+?.^${}(|)[\]])/g,
  531. REGEX_REMOVE_BACKSLASH: /(?:\[.*?[^\\]\]|\\(?=.))/g,
  532. // Replace globs with equivalent patterns to reduce parsing time.
  533. REPLACEMENTS: {
  534. '***': '*',
  535. '**/**': '**',
  536. '**/**/**': '**'
  537. },
  538. // Digits
  539. CHAR_0: 48, /* 0 */
  540. CHAR_9: 57, /* 9 */
  541. // Alphabet chars.
  542. CHAR_UPPERCASE_A: 65, /* A */
  543. CHAR_LOWERCASE_A: 97, /* a */
  544. CHAR_UPPERCASE_Z: 90, /* Z */
  545. CHAR_LOWERCASE_Z: 122, /* z */
  546. CHAR_LEFT_PARENTHESES: 40, /* ( */
  547. CHAR_RIGHT_PARENTHESES: 41, /* ) */
  548. CHAR_ASTERISK: 42, /* * */
  549. // Non-alphabetic chars.
  550. CHAR_AMPERSAND: 38, /* & */
  551. CHAR_AT: 64, /* @ */
  552. CHAR_BACKWARD_SLASH: 92, /* \ */
  553. CHAR_CARRIAGE_RETURN: 13, /* \r */
  554. CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
  555. CHAR_COLON: 58, /* : */
  556. CHAR_COMMA: 44, /* , */
  557. CHAR_DOT: 46, /* . */
  558. CHAR_DOUBLE_QUOTE: 34, /* " */
  559. CHAR_EQUAL: 61, /* = */
  560. CHAR_EXCLAMATION_MARK: 33, /* ! */
  561. CHAR_FORM_FEED: 12, /* \f */
  562. CHAR_FORWARD_SLASH: 47, /* / */
  563. CHAR_GRAVE_ACCENT: 96, /* ` */
  564. CHAR_HASH: 35, /* # */
  565. CHAR_HYPHEN_MINUS: 45, /* - */
  566. CHAR_LEFT_ANGLE_BRACKET: 60, /* < */
  567. CHAR_LEFT_CURLY_BRACE: 123, /* { */
  568. CHAR_LEFT_SQUARE_BRACKET: 91, /* [ */
  569. CHAR_LINE_FEED: 10, /* \n */
  570. CHAR_NO_BREAK_SPACE: 160, /* \u00A0 */
  571. CHAR_PERCENT: 37, /* % */
  572. CHAR_PLUS: 43, /* + */
  573. CHAR_QUESTION_MARK: 63, /* ? */
  574. CHAR_RIGHT_ANGLE_BRACKET: 62, /* > */
  575. CHAR_RIGHT_CURLY_BRACE: 125, /* } */
  576. CHAR_RIGHT_SQUARE_BRACKET: 93, /* ] */
  577. CHAR_SEMICOLON: 59, /* ; */
  578. CHAR_SINGLE_QUOTE: 39, /* ' */
  579. CHAR_SPACE: 32, /* */
  580. CHAR_TAB: 9, /* \t */
  581. CHAR_UNDERSCORE: 95, /* _ */
  582. CHAR_VERTICAL_LINE: 124, /* | */
  583. CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */
  584. SEP: path$n.sep,
  585. /**
  586. * Create EXTGLOB_CHARS
  587. */
  588. extglobChars(chars) {
  589. return {
  590. '!': { type: 'negate', open: '(?:(?!(?:', close: `))${chars.STAR})` },
  591. '?': { type: 'qmark', open: '(?:', close: ')?' },
  592. '+': { type: 'plus', open: '(?:', close: ')+' },
  593. '*': { type: 'star', open: '(?:', close: ')*' },
  594. '@': { type: 'at', open: '(?:', close: ')' }
  595. };
  596. },
  597. /**
  598. * Create GLOB_CHARS
  599. */
  600. globChars(win32) {
  601. return win32 === true ? WINDOWS_CHARS : POSIX_CHARS;
  602. }
  603. };
  604. (function (exports) {
  605. const path = require$$0$4;
  606. const win32 = process.platform === 'win32';
  607. const {
  608. REGEX_BACKSLASH,
  609. REGEX_REMOVE_BACKSLASH,
  610. REGEX_SPECIAL_CHARS,
  611. REGEX_SPECIAL_CHARS_GLOBAL
  612. } = constants$6;
  613. exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  614. exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str);
  615. exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str);
  616. exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1');
  617. exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/');
  618. exports.removeBackslashes = str => {
  619. return str.replace(REGEX_REMOVE_BACKSLASH, match => {
  620. return match === '\\' ? '' : match;
  621. });
  622. };
  623. exports.supportsLookbehinds = () => {
  624. const segs = process.version.slice(1).split('.').map(Number);
  625. if (segs.length === 3 && segs[0] >= 9 || (segs[0] === 8 && segs[1] >= 10)) {
  626. return true;
  627. }
  628. return false;
  629. };
  630. exports.isWindows = options => {
  631. if (options && typeof options.windows === 'boolean') {
  632. return options.windows;
  633. }
  634. return win32 === true || path.sep === '\\';
  635. };
  636. exports.escapeLast = (input, char, lastIdx) => {
  637. const idx = input.lastIndexOf(char, lastIdx);
  638. if (idx === -1) return input;
  639. if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
  640. return `${input.slice(0, idx)}\\${input.slice(idx)}`;
  641. };
  642. exports.removePrefix = (input, state = {}) => {
  643. let output = input;
  644. if (output.startsWith('./')) {
  645. output = output.slice(2);
  646. state.prefix = './';
  647. }
  648. return output;
  649. };
  650. exports.wrapOutput = (input, state = {}, options = {}) => {
  651. const prepend = options.contains ? '' : '^';
  652. const append = options.contains ? '' : '$';
  653. let output = `${prepend}(?:${input})${append}`;
  654. if (state.negated === true) {
  655. output = `(?:^(?!${output}).*$)`;
  656. }
  657. return output;
  658. };
  659. } (utils$k));
  660. const utils$j = utils$k;
  661. const {
  662. CHAR_ASTERISK, /* * */
  663. CHAR_AT, /* @ */
  664. CHAR_BACKWARD_SLASH, /* \ */
  665. CHAR_COMMA: CHAR_COMMA$1, /* , */
  666. CHAR_DOT: CHAR_DOT$1, /* . */
  667. CHAR_EXCLAMATION_MARK, /* ! */
  668. CHAR_FORWARD_SLASH, /* / */
  669. CHAR_LEFT_CURLY_BRACE: CHAR_LEFT_CURLY_BRACE$1, /* { */
  670. CHAR_LEFT_PARENTHESES: CHAR_LEFT_PARENTHESES$1, /* ( */
  671. CHAR_LEFT_SQUARE_BRACKET: CHAR_LEFT_SQUARE_BRACKET$1, /* [ */
  672. CHAR_PLUS, /* + */
  673. CHAR_QUESTION_MARK, /* ? */
  674. CHAR_RIGHT_CURLY_BRACE: CHAR_RIGHT_CURLY_BRACE$1, /* } */
  675. CHAR_RIGHT_PARENTHESES: CHAR_RIGHT_PARENTHESES$1, /* ) */
  676. CHAR_RIGHT_SQUARE_BRACKET: CHAR_RIGHT_SQUARE_BRACKET$1 /* ] */
  677. } = constants$6;
  678. const isPathSeparator = code => {
  679. return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
  680. };
  681. const depth = token => {
  682. if (token.isPrefix !== true) {
  683. token.depth = token.isGlobstar ? Infinity : 1;
  684. }
  685. };
  686. /**
  687. * Quickly scans a glob pattern and returns an object with a handful of
  688. * useful properties, like `isGlob`, `path` (the leading non-glob, if it exists),
  689. * `glob` (the actual pattern), `negated` (true if the path starts with `!` but not
  690. * with `!(`) and `negatedExtglob` (true if the path starts with `!(`).
  691. *
  692. * ```js
  693. * const pm = require('picomatch');
  694. * console.log(pm.scan('foo/bar/*.js'));
  695. * { isGlob: true, input: 'foo/bar/*.js', base: 'foo/bar', glob: '*.js' }
  696. * ```
  697. * @param {String} `str`
  698. * @param {Object} `options`
  699. * @return {Object} Returns an object with tokens and regex source string.
  700. * @api public
  701. */
  702. const scan$2 = (input, options) => {
  703. const opts = options || {};
  704. const length = input.length - 1;
  705. const scanToEnd = opts.parts === true || opts.scanToEnd === true;
  706. const slashes = [];
  707. const tokens = [];
  708. const parts = [];
  709. let str = input;
  710. let index = -1;
  711. let start = 0;
  712. let lastIndex = 0;
  713. let isBrace = false;
  714. let isBracket = false;
  715. let isGlob = false;
  716. let isExtglob = false;
  717. let isGlobstar = false;
  718. let braceEscaped = false;
  719. let backslashes = false;
  720. let negated = false;
  721. let negatedExtglob = false;
  722. let finished = false;
  723. let braces = 0;
  724. let prev;
  725. let code;
  726. let token = { value: '', depth: 0, isGlob: false };
  727. const eos = () => index >= length;
  728. const peek = () => str.charCodeAt(index + 1);
  729. const advance = () => {
  730. prev = code;
  731. return str.charCodeAt(++index);
  732. };
  733. while (index < length) {
  734. code = advance();
  735. let next;
  736. if (code === CHAR_BACKWARD_SLASH) {
  737. backslashes = token.backslashes = true;
  738. code = advance();
  739. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  740. braceEscaped = true;
  741. }
  742. continue;
  743. }
  744. if (braceEscaped === true || code === CHAR_LEFT_CURLY_BRACE$1) {
  745. braces++;
  746. while (eos() !== true && (code = advance())) {
  747. if (code === CHAR_BACKWARD_SLASH) {
  748. backslashes = token.backslashes = true;
  749. advance();
  750. continue;
  751. }
  752. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  753. braces++;
  754. continue;
  755. }
  756. if (braceEscaped !== true && code === CHAR_DOT$1 && (code = advance()) === CHAR_DOT$1) {
  757. isBrace = token.isBrace = true;
  758. isGlob = token.isGlob = true;
  759. finished = true;
  760. if (scanToEnd === true) {
  761. continue;
  762. }
  763. break;
  764. }
  765. if (braceEscaped !== true && code === CHAR_COMMA$1) {
  766. isBrace = token.isBrace = true;
  767. isGlob = token.isGlob = true;
  768. finished = true;
  769. if (scanToEnd === true) {
  770. continue;
  771. }
  772. break;
  773. }
  774. if (code === CHAR_RIGHT_CURLY_BRACE$1) {
  775. braces--;
  776. if (braces === 0) {
  777. braceEscaped = false;
  778. isBrace = token.isBrace = true;
  779. finished = true;
  780. break;
  781. }
  782. }
  783. }
  784. if (scanToEnd === true) {
  785. continue;
  786. }
  787. break;
  788. }
  789. if (code === CHAR_FORWARD_SLASH) {
  790. slashes.push(index);
  791. tokens.push(token);
  792. token = { value: '', depth: 0, isGlob: false };
  793. if (finished === true) continue;
  794. if (prev === CHAR_DOT$1 && index === (start + 1)) {
  795. start += 2;
  796. continue;
  797. }
  798. lastIndex = index + 1;
  799. continue;
  800. }
  801. if (opts.noext !== true) {
  802. const isExtglobChar = code === CHAR_PLUS
  803. || code === CHAR_AT
  804. || code === CHAR_ASTERISK
  805. || code === CHAR_QUESTION_MARK
  806. || code === CHAR_EXCLAMATION_MARK;
  807. if (isExtglobChar === true && peek() === CHAR_LEFT_PARENTHESES$1) {
  808. isGlob = token.isGlob = true;
  809. isExtglob = token.isExtglob = true;
  810. finished = true;
  811. if (code === CHAR_EXCLAMATION_MARK && index === start) {
  812. negatedExtglob = true;
  813. }
  814. if (scanToEnd === true) {
  815. while (eos() !== true && (code = advance())) {
  816. if (code === CHAR_BACKWARD_SLASH) {
  817. backslashes = token.backslashes = true;
  818. code = advance();
  819. continue;
  820. }
  821. if (code === CHAR_RIGHT_PARENTHESES$1) {
  822. isGlob = token.isGlob = true;
  823. finished = true;
  824. break;
  825. }
  826. }
  827. continue;
  828. }
  829. break;
  830. }
  831. }
  832. if (code === CHAR_ASTERISK) {
  833. if (prev === CHAR_ASTERISK) isGlobstar = token.isGlobstar = true;
  834. isGlob = token.isGlob = true;
  835. finished = true;
  836. if (scanToEnd === true) {
  837. continue;
  838. }
  839. break;
  840. }
  841. if (code === CHAR_QUESTION_MARK) {
  842. isGlob = token.isGlob = true;
  843. finished = true;
  844. if (scanToEnd === true) {
  845. continue;
  846. }
  847. break;
  848. }
  849. if (code === CHAR_LEFT_SQUARE_BRACKET$1) {
  850. while (eos() !== true && (next = advance())) {
  851. if (next === CHAR_BACKWARD_SLASH) {
  852. backslashes = token.backslashes = true;
  853. advance();
  854. continue;
  855. }
  856. if (next === CHAR_RIGHT_SQUARE_BRACKET$1) {
  857. isBracket = token.isBracket = true;
  858. isGlob = token.isGlob = true;
  859. finished = true;
  860. break;
  861. }
  862. }
  863. if (scanToEnd === true) {
  864. continue;
  865. }
  866. break;
  867. }
  868. if (opts.nonegate !== true && code === CHAR_EXCLAMATION_MARK && index === start) {
  869. negated = token.negated = true;
  870. start++;
  871. continue;
  872. }
  873. if (opts.noparen !== true && code === CHAR_LEFT_PARENTHESES$1) {
  874. isGlob = token.isGlob = true;
  875. if (scanToEnd === true) {
  876. while (eos() !== true && (code = advance())) {
  877. if (code === CHAR_LEFT_PARENTHESES$1) {
  878. backslashes = token.backslashes = true;
  879. code = advance();
  880. continue;
  881. }
  882. if (code === CHAR_RIGHT_PARENTHESES$1) {
  883. finished = true;
  884. break;
  885. }
  886. }
  887. continue;
  888. }
  889. break;
  890. }
  891. if (isGlob === true) {
  892. finished = true;
  893. if (scanToEnd === true) {
  894. continue;
  895. }
  896. break;
  897. }
  898. }
  899. if (opts.noext === true) {
  900. isExtglob = false;
  901. isGlob = false;
  902. }
  903. let base = str;
  904. let prefix = '';
  905. let glob = '';
  906. if (start > 0) {
  907. prefix = str.slice(0, start);
  908. str = str.slice(start);
  909. lastIndex -= start;
  910. }
  911. if (base && isGlob === true && lastIndex > 0) {
  912. base = str.slice(0, lastIndex);
  913. glob = str.slice(lastIndex);
  914. } else if (isGlob === true) {
  915. base = '';
  916. glob = str;
  917. } else {
  918. base = str;
  919. }
  920. if (base && base !== '' && base !== '/' && base !== str) {
  921. if (isPathSeparator(base.charCodeAt(base.length - 1))) {
  922. base = base.slice(0, -1);
  923. }
  924. }
  925. if (opts.unescape === true) {
  926. if (glob) glob = utils$j.removeBackslashes(glob);
  927. if (base && backslashes === true) {
  928. base = utils$j.removeBackslashes(base);
  929. }
  930. }
  931. const state = {
  932. prefix,
  933. input,
  934. start,
  935. base,
  936. glob,
  937. isBrace,
  938. isBracket,
  939. isGlob,
  940. isExtglob,
  941. isGlobstar,
  942. negated,
  943. negatedExtglob
  944. };
  945. if (opts.tokens === true) {
  946. state.maxDepth = 0;
  947. if (!isPathSeparator(code)) {
  948. tokens.push(token);
  949. }
  950. state.tokens = tokens;
  951. }
  952. if (opts.parts === true || opts.tokens === true) {
  953. let prevIndex;
  954. for (let idx = 0; idx < slashes.length; idx++) {
  955. const n = prevIndex ? prevIndex + 1 : start;
  956. const i = slashes[idx];
  957. const value = input.slice(n, i);
  958. if (opts.tokens) {
  959. if (idx === 0 && start !== 0) {
  960. tokens[idx].isPrefix = true;
  961. tokens[idx].value = prefix;
  962. } else {
  963. tokens[idx].value = value;
  964. }
  965. depth(tokens[idx]);
  966. state.maxDepth += tokens[idx].depth;
  967. }
  968. if (idx !== 0 || value !== '') {
  969. parts.push(value);
  970. }
  971. prevIndex = i;
  972. }
  973. if (prevIndex && prevIndex + 1 < input.length) {
  974. const value = input.slice(prevIndex + 1);
  975. parts.push(value);
  976. if (opts.tokens) {
  977. tokens[tokens.length - 1].value = value;
  978. depth(tokens[tokens.length - 1]);
  979. state.maxDepth += tokens[tokens.length - 1].depth;
  980. }
  981. }
  982. state.slashes = slashes;
  983. state.parts = parts;
  984. }
  985. return state;
  986. };
  987. var scan_1 = scan$2;
  988. const constants$5 = constants$6;
  989. const utils$i = utils$k;
  990. /**
  991. * Constants
  992. */
  993. const {
  994. MAX_LENGTH: MAX_LENGTH$1,
  995. POSIX_REGEX_SOURCE,
  996. REGEX_NON_SPECIAL_CHARS,
  997. REGEX_SPECIAL_CHARS_BACKREF,
  998. REPLACEMENTS
  999. } = constants$5;
  1000. /**
  1001. * Helpers
  1002. */
  1003. const expandRange = (args, options) => {
  1004. if (typeof options.expandRange === 'function') {
  1005. return options.expandRange(...args, options);
  1006. }
  1007. args.sort();
  1008. const value = `[${args.join('-')}]`;
  1009. return value;
  1010. };
  1011. /**
  1012. * Create the message for a syntax error
  1013. */
  1014. const syntaxError = (type, char) => {
  1015. return `Missing ${type}: "${char}" - use "\\\\${char}" to match literal characters`;
  1016. };
  1017. /**
  1018. * Parse the given input string.
  1019. * @param {String} input
  1020. * @param {Object} options
  1021. * @return {Object}
  1022. */
  1023. const parse$h = (input, options) => {
  1024. if (typeof input !== 'string') {
  1025. throw new TypeError('Expected a string');
  1026. }
  1027. input = REPLACEMENTS[input] || input;
  1028. const opts = { ...options };
  1029. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1030. let len = input.length;
  1031. if (len > max) {
  1032. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1033. }
  1034. const bos = { type: 'bos', value: '', output: opts.prepend || '' };
  1035. const tokens = [bos];
  1036. const capture = opts.capture ? '' : '?:';
  1037. const win32 = utils$i.isWindows(options);
  1038. // create constants based on platform, for windows or posix
  1039. const PLATFORM_CHARS = constants$5.globChars(win32);
  1040. const EXTGLOB_CHARS = constants$5.extglobChars(PLATFORM_CHARS);
  1041. const {
  1042. DOT_LITERAL,
  1043. PLUS_LITERAL,
  1044. SLASH_LITERAL,
  1045. ONE_CHAR,
  1046. DOTS_SLASH,
  1047. NO_DOT,
  1048. NO_DOT_SLASH,
  1049. NO_DOTS_SLASH,
  1050. QMARK,
  1051. QMARK_NO_DOT,
  1052. STAR,
  1053. START_ANCHOR
  1054. } = PLATFORM_CHARS;
  1055. const globstar = opts => {
  1056. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1057. };
  1058. const nodot = opts.dot ? '' : NO_DOT;
  1059. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  1060. let star = opts.bash === true ? globstar(opts) : STAR;
  1061. if (opts.capture) {
  1062. star = `(${star})`;
  1063. }
  1064. // minimatch options support
  1065. if (typeof opts.noext === 'boolean') {
  1066. opts.noextglob = opts.noext;
  1067. }
  1068. const state = {
  1069. input,
  1070. index: -1,
  1071. start: 0,
  1072. dot: opts.dot === true,
  1073. consumed: '',
  1074. output: '',
  1075. prefix: '',
  1076. backtrack: false,
  1077. negated: false,
  1078. brackets: 0,
  1079. braces: 0,
  1080. parens: 0,
  1081. quotes: 0,
  1082. globstar: false,
  1083. tokens
  1084. };
  1085. input = utils$i.removePrefix(input, state);
  1086. len = input.length;
  1087. const extglobs = [];
  1088. const braces = [];
  1089. const stack = [];
  1090. let prev = bos;
  1091. let value;
  1092. /**
  1093. * Tokenizing helpers
  1094. */
  1095. const eos = () => state.index === len - 1;
  1096. const peek = state.peek = (n = 1) => input[state.index + n];
  1097. const advance = state.advance = () => input[++state.index] || '';
  1098. const remaining = () => input.slice(state.index + 1);
  1099. const consume = (value = '', num = 0) => {
  1100. state.consumed += value;
  1101. state.index += num;
  1102. };
  1103. const append = token => {
  1104. state.output += token.output != null ? token.output : token.value;
  1105. consume(token.value);
  1106. };
  1107. const negate = () => {
  1108. let count = 1;
  1109. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  1110. advance();
  1111. state.start++;
  1112. count++;
  1113. }
  1114. if (count % 2 === 0) {
  1115. return false;
  1116. }
  1117. state.negated = true;
  1118. state.start++;
  1119. return true;
  1120. };
  1121. const increment = type => {
  1122. state[type]++;
  1123. stack.push(type);
  1124. };
  1125. const decrement = type => {
  1126. state[type]--;
  1127. stack.pop();
  1128. };
  1129. /**
  1130. * Push tokens onto the tokens array. This helper speeds up
  1131. * tokenizing by 1) helping us avoid backtracking as much as possible,
  1132. * and 2) helping us avoid creating extra tokens when consecutive
  1133. * characters are plain text. This improves performance and simplifies
  1134. * lookbehinds.
  1135. */
  1136. const push = tok => {
  1137. if (prev.type === 'globstar') {
  1138. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  1139. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  1140. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  1141. state.output = state.output.slice(0, -prev.output.length);
  1142. prev.type = 'star';
  1143. prev.value = '*';
  1144. prev.output = star;
  1145. state.output += prev.output;
  1146. }
  1147. }
  1148. if (extglobs.length && tok.type !== 'paren') {
  1149. extglobs[extglobs.length - 1].inner += tok.value;
  1150. }
  1151. if (tok.value || tok.output) append(tok);
  1152. if (prev && prev.type === 'text' && tok.type === 'text') {
  1153. prev.value += tok.value;
  1154. prev.output = (prev.output || '') + tok.value;
  1155. return;
  1156. }
  1157. tok.prev = prev;
  1158. tokens.push(tok);
  1159. prev = tok;
  1160. };
  1161. const extglobOpen = (type, value) => {
  1162. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  1163. token.prev = prev;
  1164. token.parens = state.parens;
  1165. token.output = state.output;
  1166. const output = (opts.capture ? '(' : '') + token.open;
  1167. increment('parens');
  1168. push({ type, value, output: state.output ? '' : ONE_CHAR });
  1169. push({ type: 'paren', extglob: true, value: advance(), output });
  1170. extglobs.push(token);
  1171. };
  1172. const extglobClose = token => {
  1173. let output = token.close + (opts.capture ? ')' : '');
  1174. let rest;
  1175. if (token.type === 'negate') {
  1176. let extglobStar = star;
  1177. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  1178. extglobStar = globstar(opts);
  1179. }
  1180. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  1181. output = token.close = `)$))${extglobStar}`;
  1182. }
  1183. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  1184. // Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
  1185. // In this case, we need to parse the string and use it in the output of the original pattern.
  1186. // Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
  1187. //
  1188. // Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
  1189. const expression = parse$h(rest, { ...options, fastpaths: false }).output;
  1190. output = token.close = `)${expression})${extglobStar})`;
  1191. }
  1192. if (token.prev.type === 'bos') {
  1193. state.negatedExtglob = true;
  1194. }
  1195. }
  1196. push({ type: 'paren', extglob: true, value, output });
  1197. decrement('parens');
  1198. };
  1199. /**
  1200. * Fast paths
  1201. */
  1202. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  1203. let backslashes = false;
  1204. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  1205. if (first === '\\') {
  1206. backslashes = true;
  1207. return m;
  1208. }
  1209. if (first === '?') {
  1210. if (esc) {
  1211. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  1212. }
  1213. if (index === 0) {
  1214. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  1215. }
  1216. return QMARK.repeat(chars.length);
  1217. }
  1218. if (first === '.') {
  1219. return DOT_LITERAL.repeat(chars.length);
  1220. }
  1221. if (first === '*') {
  1222. if (esc) {
  1223. return esc + first + (rest ? star : '');
  1224. }
  1225. return star;
  1226. }
  1227. return esc ? m : `\\${m}`;
  1228. });
  1229. if (backslashes === true) {
  1230. if (opts.unescape === true) {
  1231. output = output.replace(/\\/g, '');
  1232. } else {
  1233. output = output.replace(/\\+/g, m => {
  1234. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  1235. });
  1236. }
  1237. }
  1238. if (output === input && opts.contains === true) {
  1239. state.output = input;
  1240. return state;
  1241. }
  1242. state.output = utils$i.wrapOutput(output, state, options);
  1243. return state;
  1244. }
  1245. /**
  1246. * Tokenize input until we reach end-of-string
  1247. */
  1248. while (!eos()) {
  1249. value = advance();
  1250. if (value === '\u0000') {
  1251. continue;
  1252. }
  1253. /**
  1254. * Escaped characters
  1255. */
  1256. if (value === '\\') {
  1257. const next = peek();
  1258. if (next === '/' && opts.bash !== true) {
  1259. continue;
  1260. }
  1261. if (next === '.' || next === ';') {
  1262. continue;
  1263. }
  1264. if (!next) {
  1265. value += '\\';
  1266. push({ type: 'text', value });
  1267. continue;
  1268. }
  1269. // collapse slashes to reduce potential for exploits
  1270. const match = /^\\+/.exec(remaining());
  1271. let slashes = 0;
  1272. if (match && match[0].length > 2) {
  1273. slashes = match[0].length;
  1274. state.index += slashes;
  1275. if (slashes % 2 !== 0) {
  1276. value += '\\';
  1277. }
  1278. }
  1279. if (opts.unescape === true) {
  1280. value = advance();
  1281. } else {
  1282. value += advance();
  1283. }
  1284. if (state.brackets === 0) {
  1285. push({ type: 'text', value });
  1286. continue;
  1287. }
  1288. }
  1289. /**
  1290. * If we're inside a regex character class, continue
  1291. * until we reach the closing bracket.
  1292. */
  1293. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  1294. if (opts.posix !== false && value === ':') {
  1295. const inner = prev.value.slice(1);
  1296. if (inner.includes('[')) {
  1297. prev.posix = true;
  1298. if (inner.includes(':')) {
  1299. const idx = prev.value.lastIndexOf('[');
  1300. const pre = prev.value.slice(0, idx);
  1301. const rest = prev.value.slice(idx + 2);
  1302. const posix = POSIX_REGEX_SOURCE[rest];
  1303. if (posix) {
  1304. prev.value = pre + posix;
  1305. state.backtrack = true;
  1306. advance();
  1307. if (!bos.output && tokens.indexOf(prev) === 1) {
  1308. bos.output = ONE_CHAR;
  1309. }
  1310. continue;
  1311. }
  1312. }
  1313. }
  1314. }
  1315. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  1316. value = `\\${value}`;
  1317. }
  1318. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  1319. value = `\\${value}`;
  1320. }
  1321. if (opts.posix === true && value === '!' && prev.value === '[') {
  1322. value = '^';
  1323. }
  1324. prev.value += value;
  1325. append({ value });
  1326. continue;
  1327. }
  1328. /**
  1329. * If we're inside a quoted string, continue
  1330. * until we reach the closing double quote.
  1331. */
  1332. if (state.quotes === 1 && value !== '"') {
  1333. value = utils$i.escapeRegex(value);
  1334. prev.value += value;
  1335. append({ value });
  1336. continue;
  1337. }
  1338. /**
  1339. * Double quotes
  1340. */
  1341. if (value === '"') {
  1342. state.quotes = state.quotes === 1 ? 0 : 1;
  1343. if (opts.keepQuotes === true) {
  1344. push({ type: 'text', value });
  1345. }
  1346. continue;
  1347. }
  1348. /**
  1349. * Parentheses
  1350. */
  1351. if (value === '(') {
  1352. increment('parens');
  1353. push({ type: 'paren', value });
  1354. continue;
  1355. }
  1356. if (value === ')') {
  1357. if (state.parens === 0 && opts.strictBrackets === true) {
  1358. throw new SyntaxError(syntaxError('opening', '('));
  1359. }
  1360. const extglob = extglobs[extglobs.length - 1];
  1361. if (extglob && state.parens === extglob.parens + 1) {
  1362. extglobClose(extglobs.pop());
  1363. continue;
  1364. }
  1365. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  1366. decrement('parens');
  1367. continue;
  1368. }
  1369. /**
  1370. * Square brackets
  1371. */
  1372. if (value === '[') {
  1373. if (opts.nobracket === true || !remaining().includes(']')) {
  1374. if (opts.nobracket !== true && opts.strictBrackets === true) {
  1375. throw new SyntaxError(syntaxError('closing', ']'));
  1376. }
  1377. value = `\\${value}`;
  1378. } else {
  1379. increment('brackets');
  1380. }
  1381. push({ type: 'bracket', value });
  1382. continue;
  1383. }
  1384. if (value === ']') {
  1385. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  1386. push({ type: 'text', value, output: `\\${value}` });
  1387. continue;
  1388. }
  1389. if (state.brackets === 0) {
  1390. if (opts.strictBrackets === true) {
  1391. throw new SyntaxError(syntaxError('opening', '['));
  1392. }
  1393. push({ type: 'text', value, output: `\\${value}` });
  1394. continue;
  1395. }
  1396. decrement('brackets');
  1397. const prevValue = prev.value.slice(1);
  1398. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  1399. value = `/${value}`;
  1400. }
  1401. prev.value += value;
  1402. append({ value });
  1403. // when literal brackets are explicitly disabled
  1404. // assume we should match with a regex character class
  1405. if (opts.literalBrackets === false || utils$i.hasRegexChars(prevValue)) {
  1406. continue;
  1407. }
  1408. const escaped = utils$i.escapeRegex(prev.value);
  1409. state.output = state.output.slice(0, -prev.value.length);
  1410. // when literal brackets are explicitly enabled
  1411. // assume we should escape the brackets to match literal characters
  1412. if (opts.literalBrackets === true) {
  1413. state.output += escaped;
  1414. prev.value = escaped;
  1415. continue;
  1416. }
  1417. // when the user specifies nothing, try to match both
  1418. prev.value = `(${capture}${escaped}|${prev.value})`;
  1419. state.output += prev.value;
  1420. continue;
  1421. }
  1422. /**
  1423. * Braces
  1424. */
  1425. if (value === '{' && opts.nobrace !== true) {
  1426. increment('braces');
  1427. const open = {
  1428. type: 'brace',
  1429. value,
  1430. output: '(',
  1431. outputIndex: state.output.length,
  1432. tokensIndex: state.tokens.length
  1433. };
  1434. braces.push(open);
  1435. push(open);
  1436. continue;
  1437. }
  1438. if (value === '}') {
  1439. const brace = braces[braces.length - 1];
  1440. if (opts.nobrace === true || !brace) {
  1441. push({ type: 'text', value, output: value });
  1442. continue;
  1443. }
  1444. let output = ')';
  1445. if (brace.dots === true) {
  1446. const arr = tokens.slice();
  1447. const range = [];
  1448. for (let i = arr.length - 1; i >= 0; i--) {
  1449. tokens.pop();
  1450. if (arr[i].type === 'brace') {
  1451. break;
  1452. }
  1453. if (arr[i].type !== 'dots') {
  1454. range.unshift(arr[i].value);
  1455. }
  1456. }
  1457. output = expandRange(range, opts);
  1458. state.backtrack = true;
  1459. }
  1460. if (brace.comma !== true && brace.dots !== true) {
  1461. const out = state.output.slice(0, brace.outputIndex);
  1462. const toks = state.tokens.slice(brace.tokensIndex);
  1463. brace.value = brace.output = '\\{';
  1464. value = output = '\\}';
  1465. state.output = out;
  1466. for (const t of toks) {
  1467. state.output += (t.output || t.value);
  1468. }
  1469. }
  1470. push({ type: 'brace', value, output });
  1471. decrement('braces');
  1472. braces.pop();
  1473. continue;
  1474. }
  1475. /**
  1476. * Pipes
  1477. */
  1478. if (value === '|') {
  1479. if (extglobs.length > 0) {
  1480. extglobs[extglobs.length - 1].conditions++;
  1481. }
  1482. push({ type: 'text', value });
  1483. continue;
  1484. }
  1485. /**
  1486. * Commas
  1487. */
  1488. if (value === ',') {
  1489. let output = value;
  1490. const brace = braces[braces.length - 1];
  1491. if (brace && stack[stack.length - 1] === 'braces') {
  1492. brace.comma = true;
  1493. output = '|';
  1494. }
  1495. push({ type: 'comma', value, output });
  1496. continue;
  1497. }
  1498. /**
  1499. * Slashes
  1500. */
  1501. if (value === '/') {
  1502. // if the beginning of the glob is "./", advance the start
  1503. // to the current index, and don't add the "./" characters
  1504. // to the state. This greatly simplifies lookbehinds when
  1505. // checking for BOS characters like "!" and "." (not "./")
  1506. if (prev.type === 'dot' && state.index === state.start + 1) {
  1507. state.start = state.index + 1;
  1508. state.consumed = '';
  1509. state.output = '';
  1510. tokens.pop();
  1511. prev = bos; // reset "prev" to the first token
  1512. continue;
  1513. }
  1514. push({ type: 'slash', value, output: SLASH_LITERAL });
  1515. continue;
  1516. }
  1517. /**
  1518. * Dots
  1519. */
  1520. if (value === '.') {
  1521. if (state.braces > 0 && prev.type === 'dot') {
  1522. if (prev.value === '.') prev.output = DOT_LITERAL;
  1523. const brace = braces[braces.length - 1];
  1524. prev.type = 'dots';
  1525. prev.output += value;
  1526. prev.value += value;
  1527. brace.dots = true;
  1528. continue;
  1529. }
  1530. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  1531. push({ type: 'text', value, output: DOT_LITERAL });
  1532. continue;
  1533. }
  1534. push({ type: 'dot', value, output: DOT_LITERAL });
  1535. continue;
  1536. }
  1537. /**
  1538. * Question marks
  1539. */
  1540. if (value === '?') {
  1541. const isGroup = prev && prev.value === '(';
  1542. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1543. extglobOpen('qmark', value);
  1544. continue;
  1545. }
  1546. if (prev && prev.type === 'paren') {
  1547. const next = peek();
  1548. let output = value;
  1549. if (next === '<' && !utils$i.supportsLookbehinds()) {
  1550. throw new Error('Node.js v10 or higher is required for regex lookbehinds');
  1551. }
  1552. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  1553. output = `\\${value}`;
  1554. }
  1555. push({ type: 'text', value, output });
  1556. continue;
  1557. }
  1558. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  1559. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  1560. continue;
  1561. }
  1562. push({ type: 'qmark', value, output: QMARK });
  1563. continue;
  1564. }
  1565. /**
  1566. * Exclamation
  1567. */
  1568. if (value === '!') {
  1569. if (opts.noextglob !== true && peek() === '(') {
  1570. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  1571. extglobOpen('negate', value);
  1572. continue;
  1573. }
  1574. }
  1575. if (opts.nonegate !== true && state.index === 0) {
  1576. negate();
  1577. continue;
  1578. }
  1579. }
  1580. /**
  1581. * Plus
  1582. */
  1583. if (value === '+') {
  1584. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1585. extglobOpen('plus', value);
  1586. continue;
  1587. }
  1588. if ((prev && prev.value === '(') || opts.regex === false) {
  1589. push({ type: 'plus', value, output: PLUS_LITERAL });
  1590. continue;
  1591. }
  1592. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  1593. push({ type: 'plus', value });
  1594. continue;
  1595. }
  1596. push({ type: 'plus', value: PLUS_LITERAL });
  1597. continue;
  1598. }
  1599. /**
  1600. * Plain text
  1601. */
  1602. if (value === '@') {
  1603. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1604. push({ type: 'at', extglob: true, value, output: '' });
  1605. continue;
  1606. }
  1607. push({ type: 'text', value });
  1608. continue;
  1609. }
  1610. /**
  1611. * Plain text
  1612. */
  1613. if (value !== '*') {
  1614. if (value === '$' || value === '^') {
  1615. value = `\\${value}`;
  1616. }
  1617. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  1618. if (match) {
  1619. value += match[0];
  1620. state.index += match[0].length;
  1621. }
  1622. push({ type: 'text', value });
  1623. continue;
  1624. }
  1625. /**
  1626. * Stars
  1627. */
  1628. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  1629. prev.type = 'star';
  1630. prev.star = true;
  1631. prev.value += value;
  1632. prev.output = star;
  1633. state.backtrack = true;
  1634. state.globstar = true;
  1635. consume(value);
  1636. continue;
  1637. }
  1638. let rest = remaining();
  1639. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  1640. extglobOpen('star', value);
  1641. continue;
  1642. }
  1643. if (prev.type === 'star') {
  1644. if (opts.noglobstar === true) {
  1645. consume(value);
  1646. continue;
  1647. }
  1648. const prior = prev.prev;
  1649. const before = prior.prev;
  1650. const isStart = prior.type === 'slash' || prior.type === 'bos';
  1651. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  1652. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  1653. push({ type: 'star', value, output: '' });
  1654. continue;
  1655. }
  1656. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  1657. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  1658. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  1659. push({ type: 'star', value, output: '' });
  1660. continue;
  1661. }
  1662. // strip consecutive `/**/`
  1663. while (rest.slice(0, 3) === '/**') {
  1664. const after = input[state.index + 4];
  1665. if (after && after !== '/') {
  1666. break;
  1667. }
  1668. rest = rest.slice(3);
  1669. consume('/**', 3);
  1670. }
  1671. if (prior.type === 'bos' && eos()) {
  1672. prev.type = 'globstar';
  1673. prev.value += value;
  1674. prev.output = globstar(opts);
  1675. state.output = prev.output;
  1676. state.globstar = true;
  1677. consume(value);
  1678. continue;
  1679. }
  1680. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  1681. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1682. prior.output = `(?:${prior.output}`;
  1683. prev.type = 'globstar';
  1684. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  1685. prev.value += value;
  1686. state.globstar = true;
  1687. state.output += prior.output + prev.output;
  1688. consume(value);
  1689. continue;
  1690. }
  1691. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  1692. const end = rest[1] !== void 0 ? '|$' : '';
  1693. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1694. prior.output = `(?:${prior.output}`;
  1695. prev.type = 'globstar';
  1696. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  1697. prev.value += value;
  1698. state.output += prior.output + prev.output;
  1699. state.globstar = true;
  1700. consume(value + advance());
  1701. push({ type: 'slash', value: '/', output: '' });
  1702. continue;
  1703. }
  1704. if (prior.type === 'bos' && rest[0] === '/') {
  1705. prev.type = 'globstar';
  1706. prev.value += value;
  1707. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  1708. state.output = prev.output;
  1709. state.globstar = true;
  1710. consume(value + advance());
  1711. push({ type: 'slash', value: '/', output: '' });
  1712. continue;
  1713. }
  1714. // remove single star from output
  1715. state.output = state.output.slice(0, -prev.output.length);
  1716. // reset previous token to globstar
  1717. prev.type = 'globstar';
  1718. prev.output = globstar(opts);
  1719. prev.value += value;
  1720. // reset output with globstar
  1721. state.output += prev.output;
  1722. state.globstar = true;
  1723. consume(value);
  1724. continue;
  1725. }
  1726. const token = { type: 'star', value, output: star };
  1727. if (opts.bash === true) {
  1728. token.output = '.*?';
  1729. if (prev.type === 'bos' || prev.type === 'slash') {
  1730. token.output = nodot + token.output;
  1731. }
  1732. push(token);
  1733. continue;
  1734. }
  1735. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  1736. token.output = value;
  1737. push(token);
  1738. continue;
  1739. }
  1740. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  1741. if (prev.type === 'dot') {
  1742. state.output += NO_DOT_SLASH;
  1743. prev.output += NO_DOT_SLASH;
  1744. } else if (opts.dot === true) {
  1745. state.output += NO_DOTS_SLASH;
  1746. prev.output += NO_DOTS_SLASH;
  1747. } else {
  1748. state.output += nodot;
  1749. prev.output += nodot;
  1750. }
  1751. if (peek() !== '*') {
  1752. state.output += ONE_CHAR;
  1753. prev.output += ONE_CHAR;
  1754. }
  1755. }
  1756. push(token);
  1757. }
  1758. while (state.brackets > 0) {
  1759. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  1760. state.output = utils$i.escapeLast(state.output, '[');
  1761. decrement('brackets');
  1762. }
  1763. while (state.parens > 0) {
  1764. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  1765. state.output = utils$i.escapeLast(state.output, '(');
  1766. decrement('parens');
  1767. }
  1768. while (state.braces > 0) {
  1769. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  1770. state.output = utils$i.escapeLast(state.output, '{');
  1771. decrement('braces');
  1772. }
  1773. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  1774. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  1775. }
  1776. // rebuild the output if we had to backtrack at any point
  1777. if (state.backtrack === true) {
  1778. state.output = '';
  1779. for (const token of state.tokens) {
  1780. state.output += token.output != null ? token.output : token.value;
  1781. if (token.suffix) {
  1782. state.output += token.suffix;
  1783. }
  1784. }
  1785. }
  1786. return state;
  1787. };
  1788. /**
  1789. * Fast paths for creating regular expressions for common glob patterns.
  1790. * This can significantly speed up processing and has very little downside
  1791. * impact when none of the fast paths match.
  1792. */
  1793. parse$h.fastpaths = (input, options) => {
  1794. const opts = { ...options };
  1795. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1796. const len = input.length;
  1797. if (len > max) {
  1798. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1799. }
  1800. input = REPLACEMENTS[input] || input;
  1801. const win32 = utils$i.isWindows(options);
  1802. // create constants based on platform, for windows or posix
  1803. const {
  1804. DOT_LITERAL,
  1805. SLASH_LITERAL,
  1806. ONE_CHAR,
  1807. DOTS_SLASH,
  1808. NO_DOT,
  1809. NO_DOTS,
  1810. NO_DOTS_SLASH,
  1811. STAR,
  1812. START_ANCHOR
  1813. } = constants$5.globChars(win32);
  1814. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  1815. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  1816. const capture = opts.capture ? '' : '?:';
  1817. const state = { negated: false, prefix: '' };
  1818. let star = opts.bash === true ? '.*?' : STAR;
  1819. if (opts.capture) {
  1820. star = `(${star})`;
  1821. }
  1822. const globstar = opts => {
  1823. if (opts.noglobstar === true) return star;
  1824. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1825. };
  1826. const create = str => {
  1827. switch (str) {
  1828. case '*':
  1829. return `${nodot}${ONE_CHAR}${star}`;
  1830. case '.*':
  1831. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  1832. case '*.*':
  1833. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1834. case '*/*':
  1835. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  1836. case '**':
  1837. return nodot + globstar(opts);
  1838. case '**/*':
  1839. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  1840. case '**/*.*':
  1841. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1842. case '**/.*':
  1843. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  1844. default: {
  1845. const match = /^(.*?)\.(\w+)$/.exec(str);
  1846. if (!match) return;
  1847. const source = create(match[1]);
  1848. if (!source) return;
  1849. return source + DOT_LITERAL + match[2];
  1850. }
  1851. }
  1852. };
  1853. const output = utils$i.removePrefix(input, state);
  1854. let source = create(output);
  1855. if (source && opts.strictSlashes !== true) {
  1856. source += `${SLASH_LITERAL}?`;
  1857. }
  1858. return source;
  1859. };
  1860. var parse_1$3 = parse$h;
  1861. const path$m = require$$0$4;
  1862. const scan$1 = scan_1;
  1863. const parse$g = parse_1$3;
  1864. const utils$h = utils$k;
  1865. const constants$4 = constants$6;
  1866. const isObject$3 = val => val && typeof val === 'object' && !Array.isArray(val);
  1867. /**
  1868. * Creates a matcher function from one or more glob patterns. The
  1869. * returned function takes a string to match as its first argument,
  1870. * and returns true if the string is a match. The returned matcher
  1871. * function also takes a boolean as the second argument that, when true,
  1872. * returns an object with additional information.
  1873. *
  1874. * ```js
  1875. * const picomatch = require('picomatch');
  1876. * // picomatch(glob[, options]);
  1877. *
  1878. * const isMatch = picomatch('*.!(*a)');
  1879. * console.log(isMatch('a.a')); //=> false
  1880. * console.log(isMatch('a.b')); //=> true
  1881. * ```
  1882. * @name picomatch
  1883. * @param {String|Array} `globs` One or more glob patterns.
  1884. * @param {Object=} `options`
  1885. * @return {Function=} Returns a matcher function.
  1886. * @api public
  1887. */
  1888. const picomatch$5 = (glob, options, returnState = false) => {
  1889. if (Array.isArray(glob)) {
  1890. const fns = glob.map(input => picomatch$5(input, options, returnState));
  1891. const arrayMatcher = str => {
  1892. for (const isMatch of fns) {
  1893. const state = isMatch(str);
  1894. if (state) return state;
  1895. }
  1896. return false;
  1897. };
  1898. return arrayMatcher;
  1899. }
  1900. const isState = isObject$3(glob) && glob.tokens && glob.input;
  1901. if (glob === '' || (typeof glob !== 'string' && !isState)) {
  1902. throw new TypeError('Expected pattern to be a non-empty string');
  1903. }
  1904. const opts = options || {};
  1905. const posix = utils$h.isWindows(options);
  1906. const regex = isState
  1907. ? picomatch$5.compileRe(glob, options)
  1908. : picomatch$5.makeRe(glob, options, false, true);
  1909. const state = regex.state;
  1910. delete regex.state;
  1911. let isIgnored = () => false;
  1912. if (opts.ignore) {
  1913. const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
  1914. isIgnored = picomatch$5(opts.ignore, ignoreOpts, returnState);
  1915. }
  1916. const matcher = (input, returnObject = false) => {
  1917. const { isMatch, match, output } = picomatch$5.test(input, regex, options, { glob, posix });
  1918. const result = { glob, state, regex, posix, input, output, match, isMatch };
  1919. if (typeof opts.onResult === 'function') {
  1920. opts.onResult(result);
  1921. }
  1922. if (isMatch === false) {
  1923. result.isMatch = false;
  1924. return returnObject ? result : false;
  1925. }
  1926. if (isIgnored(input)) {
  1927. if (typeof opts.onIgnore === 'function') {
  1928. opts.onIgnore(result);
  1929. }
  1930. result.isMatch = false;
  1931. return returnObject ? result : false;
  1932. }
  1933. if (typeof opts.onMatch === 'function') {
  1934. opts.onMatch(result);
  1935. }
  1936. return returnObject ? result : true;
  1937. };
  1938. if (returnState) {
  1939. matcher.state = state;
  1940. }
  1941. return matcher;
  1942. };
  1943. /**
  1944. * Test `input` with the given `regex`. This is used by the main
  1945. * `picomatch()` function to test the input string.
  1946. *
  1947. * ```js
  1948. * const picomatch = require('picomatch');
  1949. * // picomatch.test(input, regex[, options]);
  1950. *
  1951. * console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
  1952. * // { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' }
  1953. * ```
  1954. * @param {String} `input` String to test.
  1955. * @param {RegExp} `regex`
  1956. * @return {Object} Returns an object with matching info.
  1957. * @api public
  1958. */
  1959. picomatch$5.test = (input, regex, options, { glob, posix } = {}) => {
  1960. if (typeof input !== 'string') {
  1961. throw new TypeError('Expected input to be a string');
  1962. }
  1963. if (input === '') {
  1964. return { isMatch: false, output: '' };
  1965. }
  1966. const opts = options || {};
  1967. const format = opts.format || (posix ? utils$h.toPosixSlashes : null);
  1968. let match = input === glob;
  1969. let output = (match && format) ? format(input) : input;
  1970. if (match === false) {
  1971. output = format ? format(input) : input;
  1972. match = output === glob;
  1973. }
  1974. if (match === false || opts.capture === true) {
  1975. if (opts.matchBase === true || opts.basename === true) {
  1976. match = picomatch$5.matchBase(input, regex, options, posix);
  1977. } else {
  1978. match = regex.exec(output);
  1979. }
  1980. }
  1981. return { isMatch: Boolean(match), match, output };
  1982. };
  1983. /**
  1984. * Match the basename of a filepath.
  1985. *
  1986. * ```js
  1987. * const picomatch = require('picomatch');
  1988. * // picomatch.matchBase(input, glob[, options]);
  1989. * console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true
  1990. * ```
  1991. * @param {String} `input` String to test.
  1992. * @param {RegExp|String} `glob` Glob pattern or regex created by [.makeRe](#makeRe).
  1993. * @return {Boolean}
  1994. * @api public
  1995. */
  1996. picomatch$5.matchBase = (input, glob, options, posix = utils$h.isWindows(options)) => {
  1997. const regex = glob instanceof RegExp ? glob : picomatch$5.makeRe(glob, options);
  1998. return regex.test(path$m.basename(input));
  1999. };
  2000. /**
  2001. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  2002. *
  2003. * ```js
  2004. * const picomatch = require('picomatch');
  2005. * // picomatch.isMatch(string, patterns[, options]);
  2006. *
  2007. * console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
  2008. * console.log(picomatch.isMatch('a.a', 'b.*')); //=> false
  2009. * ```
  2010. * @param {String|Array} str The string to test.
  2011. * @param {String|Array} patterns One or more glob patterns to use for matching.
  2012. * @param {Object} [options] See available [options](#options).
  2013. * @return {Boolean} Returns true if any patterns match `str`
  2014. * @api public
  2015. */
  2016. picomatch$5.isMatch = (str, patterns, options) => picomatch$5(patterns, options)(str);
  2017. /**
  2018. * Parse a glob pattern to create the source string for a regular
  2019. * expression.
  2020. *
  2021. * ```js
  2022. * const picomatch = require('picomatch');
  2023. * const result = picomatch.parse(pattern[, options]);
  2024. * ```
  2025. * @param {String} `pattern`
  2026. * @param {Object} `options`
  2027. * @return {Object} Returns an object with useful properties and output to be used as a regex source string.
  2028. * @api public
  2029. */
  2030. picomatch$5.parse = (pattern, options) => {
  2031. if (Array.isArray(pattern)) return pattern.map(p => picomatch$5.parse(p, options));
  2032. return parse$g(pattern, { ...options, fastpaths: false });
  2033. };
  2034. /**
  2035. * Scan a glob pattern to separate the pattern into segments.
  2036. *
  2037. * ```js
  2038. * const picomatch = require('picomatch');
  2039. * // picomatch.scan(input[, options]);
  2040. *
  2041. * const result = picomatch.scan('!./foo/*.js');
  2042. * console.log(result);
  2043. * { prefix: '!./',
  2044. * input: '!./foo/*.js',
  2045. * start: 3,
  2046. * base: 'foo',
  2047. * glob: '*.js',
  2048. * isBrace: false,
  2049. * isBracket: false,
  2050. * isGlob: true,
  2051. * isExtglob: false,
  2052. * isGlobstar: false,
  2053. * negated: true }
  2054. * ```
  2055. * @param {String} `input` Glob pattern to scan.
  2056. * @param {Object} `options`
  2057. * @return {Object} Returns an object with
  2058. * @api public
  2059. */
  2060. picomatch$5.scan = (input, options) => scan$1(input, options);
  2061. /**
  2062. * Compile a regular expression from the `state` object returned by the
  2063. * [parse()](#parse) method.
  2064. *
  2065. * @param {Object} `state`
  2066. * @param {Object} `options`
  2067. * @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser.
  2068. * @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging.
  2069. * @return {RegExp}
  2070. * @api public
  2071. */
  2072. picomatch$5.compileRe = (state, options, returnOutput = false, returnState = false) => {
  2073. if (returnOutput === true) {
  2074. return state.output;
  2075. }
  2076. const opts = options || {};
  2077. const prepend = opts.contains ? '' : '^';
  2078. const append = opts.contains ? '' : '$';
  2079. let source = `${prepend}(?:${state.output})${append}`;
  2080. if (state && state.negated === true) {
  2081. source = `^(?!${source}).*$`;
  2082. }
  2083. const regex = picomatch$5.toRegex(source, options);
  2084. if (returnState === true) {
  2085. regex.state = state;
  2086. }
  2087. return regex;
  2088. };
  2089. /**
  2090. * Create a regular expression from a parsed glob pattern.
  2091. *
  2092. * ```js
  2093. * const picomatch = require('picomatch');
  2094. * const state = picomatch.parse('*.js');
  2095. * // picomatch.compileRe(state[, options]);
  2096. *
  2097. * console.log(picomatch.compileRe(state));
  2098. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2099. * ```
  2100. * @param {String} `state` The object returned from the `.parse` method.
  2101. * @param {Object} `options`
  2102. * @param {Boolean} `returnOutput` Implementors may use this argument to return the compiled output, instead of a regular expression. This is not exposed on the options to prevent end-users from mutating the result.
  2103. * @param {Boolean} `returnState` Implementors may use this argument to return the state from the parsed glob with the returned regular expression.
  2104. * @return {RegExp} Returns a regex created from the given pattern.
  2105. * @api public
  2106. */
  2107. picomatch$5.makeRe = (input, options = {}, returnOutput = false, returnState = false) => {
  2108. if (!input || typeof input !== 'string') {
  2109. throw new TypeError('Expected a non-empty string');
  2110. }
  2111. let parsed = { negated: false, fastpaths: true };
  2112. if (options.fastpaths !== false && (input[0] === '.' || input[0] === '*')) {
  2113. parsed.output = parse$g.fastpaths(input, options);
  2114. }
  2115. if (!parsed.output) {
  2116. parsed = parse$g(input, options);
  2117. }
  2118. return picomatch$5.compileRe(parsed, options, returnOutput, returnState);
  2119. };
  2120. /**
  2121. * Create a regular expression from the given regex source string.
  2122. *
  2123. * ```js
  2124. * const picomatch = require('picomatch');
  2125. * // picomatch.toRegex(source[, options]);
  2126. *
  2127. * const { output } = picomatch.parse('*.js');
  2128. * console.log(picomatch.toRegex(output));
  2129. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2130. * ```
  2131. * @param {String} `source` Regular expression source string.
  2132. * @param {Object} `options`
  2133. * @return {RegExp}
  2134. * @api public
  2135. */
  2136. picomatch$5.toRegex = (source, options) => {
  2137. try {
  2138. const opts = options || {};
  2139. return new RegExp(source, opts.flags || (opts.nocase ? 'i' : ''));
  2140. } catch (err) {
  2141. if (options && options.debug === true) throw err;
  2142. return /$^/;
  2143. }
  2144. };
  2145. /**
  2146. * Picomatch constants.
  2147. * @return {Object}
  2148. */
  2149. picomatch$5.constants = constants$4;
  2150. /**
  2151. * Expose "picomatch"
  2152. */
  2153. var picomatch_1 = picomatch$5;
  2154. var picomatch$3 = picomatch_1;
  2155. var picomatch$4 = /*@__PURE__*/getDefaultExportFromCjs(picomatch$3);
  2156. const extractors = {
  2157. ArrayPattern(names, param) {
  2158. for (const element of param.elements) {
  2159. if (element)
  2160. extractors[element.type](names, element);
  2161. }
  2162. },
  2163. AssignmentPattern(names, param) {
  2164. extractors[param.left.type](names, param.left);
  2165. },
  2166. Identifier(names, param) {
  2167. names.push(param.name);
  2168. },
  2169. MemberExpression() { },
  2170. ObjectPattern(names, param) {
  2171. for (const prop of param.properties) {
  2172. // @ts-ignore Typescript reports that this is not a valid type
  2173. if (prop.type === 'RestElement') {
  2174. extractors.RestElement(names, prop);
  2175. }
  2176. else {
  2177. extractors[prop.value.type](names, prop.value);
  2178. }
  2179. }
  2180. },
  2181. RestElement(names, param) {
  2182. extractors[param.argument.type](names, param.argument);
  2183. }
  2184. };
  2185. const extractAssignedNames = function extractAssignedNames(param) {
  2186. const names = [];
  2187. extractors[param.type](names, param);
  2188. return names;
  2189. };
  2190. const blockDeclarations = {
  2191. const: true,
  2192. let: true
  2193. };
  2194. let Scope$1 = class Scope {
  2195. constructor(options = {}) {
  2196. this.parent = options.parent;
  2197. this.isBlockScope = !!options.block;
  2198. this.declarations = Object.create(null);
  2199. if (options.params) {
  2200. options.params.forEach((param) => {
  2201. extractAssignedNames(param).forEach((name) => {
  2202. this.declarations[name] = true;
  2203. });
  2204. });
  2205. }
  2206. }
  2207. addDeclaration(node, isBlockDeclaration, isVar) {
  2208. if (!isBlockDeclaration && this.isBlockScope) {
  2209. // it's a `var` or function node, and this
  2210. // is a block scope, so we need to go up
  2211. this.parent.addDeclaration(node, isBlockDeclaration, isVar);
  2212. }
  2213. else if (node.id) {
  2214. extractAssignedNames(node.id).forEach((name) => {
  2215. this.declarations[name] = true;
  2216. });
  2217. }
  2218. }
  2219. contains(name) {
  2220. return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
  2221. }
  2222. };
  2223. const attachScopes = function attachScopes(ast, propertyName = 'scope') {
  2224. let scope = new Scope$1();
  2225. walk$3(ast, {
  2226. enter(n, parent) {
  2227. const node = n;
  2228. // function foo () {...}
  2229. // class Foo {...}
  2230. if (/(Function|Class)Declaration/.test(node.type)) {
  2231. scope.addDeclaration(node, false, false);
  2232. }
  2233. // var foo = 1
  2234. if (node.type === 'VariableDeclaration') {
  2235. const { kind } = node;
  2236. const isBlockDeclaration = blockDeclarations[kind];
  2237. node.declarations.forEach((declaration) => {
  2238. scope.addDeclaration(declaration, isBlockDeclaration, true);
  2239. });
  2240. }
  2241. let newScope;
  2242. // create new function scope
  2243. if (/Function/.test(node.type)) {
  2244. const func = node;
  2245. newScope = new Scope$1({
  2246. parent: scope,
  2247. block: false,
  2248. params: func.params
  2249. });
  2250. // named function expressions - the name is considered
  2251. // part of the function's scope
  2252. if (func.type === 'FunctionExpression' && func.id) {
  2253. newScope.addDeclaration(func, false, false);
  2254. }
  2255. }
  2256. // create new for scope
  2257. if (/For(In|Of)?Statement/.test(node.type)) {
  2258. newScope = new Scope$1({
  2259. parent: scope,
  2260. block: true
  2261. });
  2262. }
  2263. // create new block scope
  2264. if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
  2265. newScope = new Scope$1({
  2266. parent: scope,
  2267. block: true
  2268. });
  2269. }
  2270. // catch clause has its own block scope
  2271. if (node.type === 'CatchClause') {
  2272. newScope = new Scope$1({
  2273. parent: scope,
  2274. params: node.param ? [node.param] : [],
  2275. block: true
  2276. });
  2277. }
  2278. if (newScope) {
  2279. Object.defineProperty(node, propertyName, {
  2280. value: newScope,
  2281. configurable: true
  2282. });
  2283. scope = newScope;
  2284. }
  2285. },
  2286. leave(n) {
  2287. const node = n;
  2288. if (node[propertyName])
  2289. scope = scope.parent;
  2290. }
  2291. });
  2292. return scope;
  2293. };
  2294. // Helper since Typescript can't detect readonly arrays with Array.isArray
  2295. function isArray$1(arg) {
  2296. return Array.isArray(arg);
  2297. }
  2298. function ensureArray(thing) {
  2299. if (isArray$1(thing))
  2300. return thing;
  2301. if (thing == null)
  2302. return [];
  2303. return [thing];
  2304. }
  2305. const normalizePath$5 = function normalizePath(filename) {
  2306. return filename.split(win32.sep).join(posix.sep);
  2307. };
  2308. function getMatcherString(id, resolutionBase) {
  2309. if (resolutionBase === false || isAbsolute$1(id) || id.startsWith('**')) {
  2310. return normalizePath$5(id);
  2311. }
  2312. // resolve('') is valid and will default to process.cwd()
  2313. const basePath = normalizePath$5(resolve$3(resolutionBase || ''))
  2314. // escape all possible (posix + win) path characters that might interfere with regex
  2315. .replace(/[-^$*+?.()|[\]{}]/g, '\\$&');
  2316. // Note that we use posix.join because:
  2317. // 1. the basePath has been normalized to use /
  2318. // 2. the incoming glob (id) matcher, also uses /
  2319. // otherwise Node will force backslash (\) on windows
  2320. return posix.join(basePath, normalizePath$5(id));
  2321. }
  2322. const createFilter$1 = function createFilter(include, exclude, options) {
  2323. const resolutionBase = options && options.resolve;
  2324. const getMatcher = (id) => id instanceof RegExp
  2325. ? id
  2326. : {
  2327. test: (what) => {
  2328. // this refactor is a tad overly verbose but makes for easy debugging
  2329. const pattern = getMatcherString(id, resolutionBase);
  2330. const fn = picomatch$4(pattern, { dot: true });
  2331. const result = fn(what);
  2332. return result;
  2333. }
  2334. };
  2335. const includeMatchers = ensureArray(include).map(getMatcher);
  2336. const excludeMatchers = ensureArray(exclude).map(getMatcher);
  2337. return function result(id) {
  2338. if (typeof id !== 'string')
  2339. return false;
  2340. if (/\0/.test(id))
  2341. return false;
  2342. const pathId = normalizePath$5(id);
  2343. for (let i = 0; i < excludeMatchers.length; ++i) {
  2344. const matcher = excludeMatchers[i];
  2345. if (matcher.test(pathId))
  2346. return false;
  2347. }
  2348. for (let i = 0; i < includeMatchers.length; ++i) {
  2349. const matcher = includeMatchers[i];
  2350. if (matcher.test(pathId))
  2351. return true;
  2352. }
  2353. return !includeMatchers.length;
  2354. };
  2355. };
  2356. const reservedWords$1 = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
  2357. const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
  2358. const forbiddenIdentifiers = new Set(`${reservedWords$1} ${builtins}`.split(' '));
  2359. forbiddenIdentifiers.add('');
  2360. const makeLegalIdentifier = function makeLegalIdentifier(str) {
  2361. let identifier = str
  2362. .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
  2363. .replace(/[^$_a-zA-Z0-9]/g, '_');
  2364. if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
  2365. identifier = `_${identifier}`;
  2366. }
  2367. return identifier || '_';
  2368. };
  2369. function stringify$8(obj) {
  2370. return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, (char) => `\\u${`000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
  2371. }
  2372. function serializeArray(arr, indent, baseIndent) {
  2373. let output = '[';
  2374. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2375. for (let i = 0; i < arr.length; i++) {
  2376. const key = arr[i];
  2377. output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`;
  2378. }
  2379. return `${output}${indent ? `\n${baseIndent}` : ''}]`;
  2380. }
  2381. function serializeObject(obj, indent, baseIndent) {
  2382. let output = '{';
  2383. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2384. const entries = Object.entries(obj);
  2385. for (let i = 0; i < entries.length; i++) {
  2386. const [key, value] = entries[i];
  2387. const stringKey = makeLegalIdentifier(key) === key ? key : stringify$8(key);
  2388. output += `${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize(value, indent, baseIndent + indent)}`;
  2389. }
  2390. return `${output}${indent ? `\n${baseIndent}` : ''}}`;
  2391. }
  2392. function serialize(obj, indent, baseIndent) {
  2393. if (typeof obj === 'object' && obj !== null) {
  2394. if (Array.isArray(obj))
  2395. return serializeArray(obj, indent, baseIndent);
  2396. if (obj instanceof Date)
  2397. return `new Date(${obj.getTime()})`;
  2398. if (obj instanceof RegExp)
  2399. return obj.toString();
  2400. return serializeObject(obj, indent, baseIndent);
  2401. }
  2402. if (typeof obj === 'number') {
  2403. if (obj === Infinity)
  2404. return 'Infinity';
  2405. if (obj === -Infinity)
  2406. return '-Infinity';
  2407. if (obj === 0)
  2408. return 1 / obj === Infinity ? '0' : '-0';
  2409. if (obj !== obj)
  2410. return 'NaN'; // eslint-disable-line no-self-compare
  2411. }
  2412. if (typeof obj === 'symbol') {
  2413. const key = Symbol.keyFor(obj);
  2414. // eslint-disable-next-line no-undefined
  2415. if (key !== undefined)
  2416. return `Symbol.for(${stringify$8(key)})`;
  2417. }
  2418. if (typeof obj === 'bigint')
  2419. return `${obj}n`;
  2420. return stringify$8(obj);
  2421. }
  2422. // isWellFormed exists from Node.js 20
  2423. const hasStringIsWellFormed = 'isWellFormed' in String.prototype;
  2424. function isWellFormedString(input) {
  2425. // @ts-expect-error String::isWellFormed exists from ES2024. tsconfig lib is set to ES6
  2426. if (hasStringIsWellFormed)
  2427. return input.isWellFormed();
  2428. // https://github.com/tc39/proposal-is-usv-string/blob/main/README.md#algorithm
  2429. return !/\p{Surrogate}/u.test(input);
  2430. }
  2431. const dataToEsm = function dataToEsm(data, options = {}) {
  2432. var _a, _b;
  2433. const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
  2434. const _ = options.compact ? '' : ' ';
  2435. const n = options.compact ? '' : '\n';
  2436. const declarationType = options.preferConst ? 'const' : 'var';
  2437. if (options.namedExports === false ||
  2438. typeof data !== 'object' ||
  2439. Array.isArray(data) ||
  2440. data instanceof Date ||
  2441. data instanceof RegExp ||
  2442. data === null) {
  2443. const code = serialize(data, options.compact ? null : t, '');
  2444. const magic = _ || (/^[{[\-\/]/.test(code) ? '' : ' '); // eslint-disable-line no-useless-escape
  2445. return `export default${magic}${code};`;
  2446. }
  2447. let maxUnderbarPrefixLength = 0;
  2448. for (const key of Object.keys(data)) {
  2449. const underbarPrefixLength = (_b = (_a = key.match(/^(_+)/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
  2450. if (underbarPrefixLength > maxUnderbarPrefixLength) {
  2451. maxUnderbarPrefixLength = underbarPrefixLength;
  2452. }
  2453. }
  2454. const arbitraryNamePrefix = `${'_'.repeat(maxUnderbarPrefixLength + 1)}arbitrary`;
  2455. let namedExportCode = '';
  2456. const defaultExportRows = [];
  2457. const arbitraryNameExportRows = [];
  2458. for (const [key, value] of Object.entries(data)) {
  2459. if (key === makeLegalIdentifier(key)) {
  2460. if (options.objectShorthand)
  2461. defaultExportRows.push(key);
  2462. else
  2463. defaultExportRows.push(`${key}:${_}${key}`);
  2464. namedExportCode += `export ${declarationType} ${key}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2465. }
  2466. else {
  2467. defaultExportRows.push(`${stringify$8(key)}:${_}${serialize(value, options.compact ? null : t, '')}`);
  2468. if (options.includeArbitraryNames && isWellFormedString(key)) {
  2469. const variableName = `${arbitraryNamePrefix}${arbitraryNameExportRows.length}`;
  2470. namedExportCode += `${declarationType} ${variableName}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2471. arbitraryNameExportRows.push(`${variableName} as ${JSON.stringify(key)}`);
  2472. }
  2473. }
  2474. }
  2475. const arbitraryExportCode = arbitraryNameExportRows.length > 0
  2476. ? `export${_}{${n}${t}${arbitraryNameExportRows.join(`,${n}${t}`)}${n}};${n}`
  2477. : '';
  2478. const defaultExportCode = `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`;
  2479. return `${namedExportCode}${arbitraryExportCode}${defaultExportCode}`;
  2480. };
  2481. var path$l = require$$0$4;
  2482. var commondir = function (basedir, relfiles) {
  2483. if (relfiles) {
  2484. var files = relfiles.map(function (r) {
  2485. return path$l.resolve(basedir, r);
  2486. });
  2487. }
  2488. else {
  2489. var files = basedir;
  2490. }
  2491. var res = files.slice(1).reduce(function (ps, file) {
  2492. if (!file.match(/^([A-Za-z]:)?\/|\\/)) {
  2493. throw new Error('relative path without a basedir');
  2494. }
  2495. var xs = file.split(/\/+|\\+/);
  2496. for (
  2497. var i = 0;
  2498. ps[i] === xs[i] && i < Math.min(ps.length, xs.length);
  2499. i++
  2500. );
  2501. return ps.slice(0, i);
  2502. }, files[0].split(/\/+|\\+/));
  2503. // Windows correctly handles paths with forward-slashes
  2504. return res.length > 1 ? res.join('/') : '/'
  2505. };
  2506. var getCommonDir = /*@__PURE__*/getDefaultExportFromCjs(commondir);
  2507. var old$1 = {};
  2508. // Copyright Joyent, Inc. and other Node contributors.
  2509. //
  2510. // Permission is hereby granted, free of charge, to any person obtaining a
  2511. // copy of this software and associated documentation files (the
  2512. // "Software"), to deal in the Software without restriction, including
  2513. // without limitation the rights to use, copy, modify, merge, publish,
  2514. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2515. // persons to whom the Software is furnished to do so, subject to the
  2516. // following conditions:
  2517. //
  2518. // The above copyright notice and this permission notice shall be included
  2519. // in all copies or substantial portions of the Software.
  2520. //
  2521. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2522. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2523. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2524. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2525. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2526. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2527. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2528. var pathModule = require$$0$4;
  2529. var isWindows$4 = process.platform === 'win32';
  2530. var fs$k = require$$0__default;
  2531. // JavaScript implementation of realpath, ported from node pre-v6
  2532. var DEBUG$1 = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
  2533. function rethrow() {
  2534. // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
  2535. // is fairly slow to generate.
  2536. var callback;
  2537. if (DEBUG$1) {
  2538. var backtrace = new Error;
  2539. callback = debugCallback;
  2540. } else
  2541. callback = missingCallback;
  2542. return callback;
  2543. function debugCallback(err) {
  2544. if (err) {
  2545. backtrace.message = err.message;
  2546. err = backtrace;
  2547. missingCallback(err);
  2548. }
  2549. }
  2550. function missingCallback(err) {
  2551. if (err) {
  2552. if (process.throwDeprecation)
  2553. throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
  2554. else if (!process.noDeprecation) {
  2555. var msg = 'fs: missing callback ' + (err.stack || err.message);
  2556. if (process.traceDeprecation)
  2557. console.trace(msg);
  2558. else
  2559. console.error(msg);
  2560. }
  2561. }
  2562. }
  2563. }
  2564. function maybeCallback(cb) {
  2565. return typeof cb === 'function' ? cb : rethrow();
  2566. }
  2567. // Regexp that finds the next partion of a (partial) path
  2568. // result is [base_with_slash, base], e.g. ['somedir/', 'somedir']
  2569. if (isWindows$4) {
  2570. var nextPartRe = /(.*?)(?:[\/\\]+|$)/g;
  2571. } else {
  2572. var nextPartRe = /(.*?)(?:[\/]+|$)/g;
  2573. }
  2574. // Regex to find the device root, including trailing slash. E.g. 'c:\\'.
  2575. if (isWindows$4) {
  2576. var splitRootRe = /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/;
  2577. } else {
  2578. var splitRootRe = /^[\/]*/;
  2579. }
  2580. old$1.realpathSync = function realpathSync(p, cache) {
  2581. // make p is absolute
  2582. p = pathModule.resolve(p);
  2583. if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
  2584. return cache[p];
  2585. }
  2586. var original = p,
  2587. seenLinks = {},
  2588. knownHard = {};
  2589. // current character position in p
  2590. var pos;
  2591. // the partial path so far, including a trailing slash if any
  2592. var current;
  2593. // the partial path without a trailing slash (except when pointing at a root)
  2594. var base;
  2595. // the partial path scanned in the previous round, with slash
  2596. var previous;
  2597. start();
  2598. function start() {
  2599. // Skip over roots
  2600. var m = splitRootRe.exec(p);
  2601. pos = m[0].length;
  2602. current = m[0];
  2603. base = m[0];
  2604. previous = '';
  2605. // On windows, check that the root exists. On unix there is no need.
  2606. if (isWindows$4 && !knownHard[base]) {
  2607. fs$k.lstatSync(base);
  2608. knownHard[base] = true;
  2609. }
  2610. }
  2611. // walk down the path, swapping out linked pathparts for their real
  2612. // values
  2613. // NB: p.length changes.
  2614. while (pos < p.length) {
  2615. // find the next part
  2616. nextPartRe.lastIndex = pos;
  2617. var result = nextPartRe.exec(p);
  2618. previous = current;
  2619. current += result[0];
  2620. base = previous + result[1];
  2621. pos = nextPartRe.lastIndex;
  2622. // continue if not a symlink
  2623. if (knownHard[base] || (cache && cache[base] === base)) {
  2624. continue;
  2625. }
  2626. var resolvedLink;
  2627. if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
  2628. // some known symbolic link. no need to stat again.
  2629. resolvedLink = cache[base];
  2630. } else {
  2631. var stat = fs$k.lstatSync(base);
  2632. if (!stat.isSymbolicLink()) {
  2633. knownHard[base] = true;
  2634. if (cache) cache[base] = base;
  2635. continue;
  2636. }
  2637. // read the link if it wasn't read before
  2638. // dev/ino always return 0 on windows, so skip the check.
  2639. var linkTarget = null;
  2640. if (!isWindows$4) {
  2641. var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
  2642. if (seenLinks.hasOwnProperty(id)) {
  2643. linkTarget = seenLinks[id];
  2644. }
  2645. }
  2646. if (linkTarget === null) {
  2647. fs$k.statSync(base);
  2648. linkTarget = fs$k.readlinkSync(base);
  2649. }
  2650. resolvedLink = pathModule.resolve(previous, linkTarget);
  2651. // track this, if given a cache.
  2652. if (cache) cache[base] = resolvedLink;
  2653. if (!isWindows$4) seenLinks[id] = linkTarget;
  2654. }
  2655. // resolve the link, then start over
  2656. p = pathModule.resolve(resolvedLink, p.slice(pos));
  2657. start();
  2658. }
  2659. if (cache) cache[original] = p;
  2660. return p;
  2661. };
  2662. old$1.realpath = function realpath(p, cache, cb) {
  2663. if (typeof cb !== 'function') {
  2664. cb = maybeCallback(cache);
  2665. cache = null;
  2666. }
  2667. // make p is absolute
  2668. p = pathModule.resolve(p);
  2669. if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
  2670. return process.nextTick(cb.bind(null, null, cache[p]));
  2671. }
  2672. var original = p,
  2673. seenLinks = {},
  2674. knownHard = {};
  2675. // current character position in p
  2676. var pos;
  2677. // the partial path so far, including a trailing slash if any
  2678. var current;
  2679. // the partial path without a trailing slash (except when pointing at a root)
  2680. var base;
  2681. // the partial path scanned in the previous round, with slash
  2682. var previous;
  2683. start();
  2684. function start() {
  2685. // Skip over roots
  2686. var m = splitRootRe.exec(p);
  2687. pos = m[0].length;
  2688. current = m[0];
  2689. base = m[0];
  2690. previous = '';
  2691. // On windows, check that the root exists. On unix there is no need.
  2692. if (isWindows$4 && !knownHard[base]) {
  2693. fs$k.lstat(base, function(err) {
  2694. if (err) return cb(err);
  2695. knownHard[base] = true;
  2696. LOOP();
  2697. });
  2698. } else {
  2699. process.nextTick(LOOP);
  2700. }
  2701. }
  2702. // walk down the path, swapping out linked pathparts for their real
  2703. // values
  2704. function LOOP() {
  2705. // stop if scanned past end of path
  2706. if (pos >= p.length) {
  2707. if (cache) cache[original] = p;
  2708. return cb(null, p);
  2709. }
  2710. // find the next part
  2711. nextPartRe.lastIndex = pos;
  2712. var result = nextPartRe.exec(p);
  2713. previous = current;
  2714. current += result[0];
  2715. base = previous + result[1];
  2716. pos = nextPartRe.lastIndex;
  2717. // continue if not a symlink
  2718. if (knownHard[base] || (cache && cache[base] === base)) {
  2719. return process.nextTick(LOOP);
  2720. }
  2721. if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
  2722. // known symbolic link. no need to stat again.
  2723. return gotResolvedLink(cache[base]);
  2724. }
  2725. return fs$k.lstat(base, gotStat);
  2726. }
  2727. function gotStat(err, stat) {
  2728. if (err) return cb(err);
  2729. // if not a symlink, skip to the next path part
  2730. if (!stat.isSymbolicLink()) {
  2731. knownHard[base] = true;
  2732. if (cache) cache[base] = base;
  2733. return process.nextTick(LOOP);
  2734. }
  2735. // stat & read the link if not read before
  2736. // call gotTarget as soon as the link target is known
  2737. // dev/ino always return 0 on windows, so skip the check.
  2738. if (!isWindows$4) {
  2739. var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
  2740. if (seenLinks.hasOwnProperty(id)) {
  2741. return gotTarget(null, seenLinks[id], base);
  2742. }
  2743. }
  2744. fs$k.stat(base, function(err) {
  2745. if (err) return cb(err);
  2746. fs$k.readlink(base, function(err, target) {
  2747. if (!isWindows$4) seenLinks[id] = target;
  2748. gotTarget(err, target);
  2749. });
  2750. });
  2751. }
  2752. function gotTarget(err, target, base) {
  2753. if (err) return cb(err);
  2754. var resolvedLink = pathModule.resolve(previous, target);
  2755. if (cache) cache[base] = resolvedLink;
  2756. gotResolvedLink(resolvedLink);
  2757. }
  2758. function gotResolvedLink(resolvedLink) {
  2759. // resolve the link, then start over
  2760. p = pathModule.resolve(resolvedLink, p.slice(pos));
  2761. start();
  2762. }
  2763. };
  2764. var fs_realpath = realpath$2;
  2765. realpath$2.realpath = realpath$2;
  2766. realpath$2.sync = realpathSync;
  2767. realpath$2.realpathSync = realpathSync;
  2768. realpath$2.monkeypatch = monkeypatch;
  2769. realpath$2.unmonkeypatch = unmonkeypatch;
  2770. var fs$j = require$$0__default;
  2771. var origRealpath = fs$j.realpath;
  2772. var origRealpathSync = fs$j.realpathSync;
  2773. var version$4 = process.version;
  2774. var ok = /^v[0-5]\./.test(version$4);
  2775. var old = old$1;
  2776. function newError (er) {
  2777. return er && er.syscall === 'realpath' && (
  2778. er.code === 'ELOOP' ||
  2779. er.code === 'ENOMEM' ||
  2780. er.code === 'ENAMETOOLONG'
  2781. )
  2782. }
  2783. function realpath$2 (p, cache, cb) {
  2784. if (ok) {
  2785. return origRealpath(p, cache, cb)
  2786. }
  2787. if (typeof cache === 'function') {
  2788. cb = cache;
  2789. cache = null;
  2790. }
  2791. origRealpath(p, cache, function (er, result) {
  2792. if (newError(er)) {
  2793. old.realpath(p, cache, cb);
  2794. } else {
  2795. cb(er, result);
  2796. }
  2797. });
  2798. }
  2799. function realpathSync (p, cache) {
  2800. if (ok) {
  2801. return origRealpathSync(p, cache)
  2802. }
  2803. try {
  2804. return origRealpathSync(p, cache)
  2805. } catch (er) {
  2806. if (newError(er)) {
  2807. return old.realpathSync(p, cache)
  2808. } else {
  2809. throw er
  2810. }
  2811. }
  2812. }
  2813. function monkeypatch () {
  2814. fs$j.realpath = realpath$2;
  2815. fs$j.realpathSync = realpathSync;
  2816. }
  2817. function unmonkeypatch () {
  2818. fs$j.realpath = origRealpath;
  2819. fs$j.realpathSync = origRealpathSync;
  2820. }
  2821. const isWindows$3 = typeof process === 'object' &&
  2822. process &&
  2823. process.platform === 'win32';
  2824. var path$k = isWindows$3 ? { sep: '\\' } : { sep: '/' };
  2825. var balancedMatch = balanced$1;
  2826. function balanced$1(a, b, str) {
  2827. if (a instanceof RegExp) a = maybeMatch(a, str);
  2828. if (b instanceof RegExp) b = maybeMatch(b, str);
  2829. var r = range$1(a, b, str);
  2830. return r && {
  2831. start: r[0],
  2832. end: r[1],
  2833. pre: str.slice(0, r[0]),
  2834. body: str.slice(r[0] + a.length, r[1]),
  2835. post: str.slice(r[1] + b.length)
  2836. };
  2837. }
  2838. function maybeMatch(reg, str) {
  2839. var m = str.match(reg);
  2840. return m ? m[0] : null;
  2841. }
  2842. balanced$1.range = range$1;
  2843. function range$1(a, b, str) {
  2844. var begs, beg, left, right, result;
  2845. var ai = str.indexOf(a);
  2846. var bi = str.indexOf(b, ai + 1);
  2847. var i = ai;
  2848. if (ai >= 0 && bi > 0) {
  2849. if(a===b) {
  2850. return [ai, bi];
  2851. }
  2852. begs = [];
  2853. left = str.length;
  2854. while (i >= 0 && !result) {
  2855. if (i == ai) {
  2856. begs.push(i);
  2857. ai = str.indexOf(a, i + 1);
  2858. } else if (begs.length == 1) {
  2859. result = [ begs.pop(), bi ];
  2860. } else {
  2861. beg = begs.pop();
  2862. if (beg < left) {
  2863. left = beg;
  2864. right = bi;
  2865. }
  2866. bi = str.indexOf(b, i + 1);
  2867. }
  2868. i = ai < bi && ai >= 0 ? ai : bi;
  2869. }
  2870. if (begs.length) {
  2871. result = [ left, right ];
  2872. }
  2873. }
  2874. return result;
  2875. }
  2876. var balanced = balancedMatch;
  2877. var braceExpansion = expandTop;
  2878. var escSlash = '\0SLASH'+Math.random()+'\0';
  2879. var escOpen = '\0OPEN'+Math.random()+'\0';
  2880. var escClose = '\0CLOSE'+Math.random()+'\0';
  2881. var escComma = '\0COMMA'+Math.random()+'\0';
  2882. var escPeriod = '\0PERIOD'+Math.random()+'\0';
  2883. function numeric(str) {
  2884. return parseInt(str, 10) == str
  2885. ? parseInt(str, 10)
  2886. : str.charCodeAt(0);
  2887. }
  2888. function escapeBraces(str) {
  2889. return str.split('\\\\').join(escSlash)
  2890. .split('\\{').join(escOpen)
  2891. .split('\\}').join(escClose)
  2892. .split('\\,').join(escComma)
  2893. .split('\\.').join(escPeriod);
  2894. }
  2895. function unescapeBraces(str) {
  2896. return str.split(escSlash).join('\\')
  2897. .split(escOpen).join('{')
  2898. .split(escClose).join('}')
  2899. .split(escComma).join(',')
  2900. .split(escPeriod).join('.');
  2901. }
  2902. // Basically just str.split(","), but handling cases
  2903. // where we have nested braced sections, which should be
  2904. // treated as individual members, like {a,{b,c},d}
  2905. function parseCommaParts(str) {
  2906. if (!str)
  2907. return [''];
  2908. var parts = [];
  2909. var m = balanced('{', '}', str);
  2910. if (!m)
  2911. return str.split(',');
  2912. var pre = m.pre;
  2913. var body = m.body;
  2914. var post = m.post;
  2915. var p = pre.split(',');
  2916. p[p.length-1] += '{' + body + '}';
  2917. var postParts = parseCommaParts(post);
  2918. if (post.length) {
  2919. p[p.length-1] += postParts.shift();
  2920. p.push.apply(p, postParts);
  2921. }
  2922. parts.push.apply(parts, p);
  2923. return parts;
  2924. }
  2925. function expandTop(str) {
  2926. if (!str)
  2927. return [];
  2928. // I don't know why Bash 4.3 does this, but it does.
  2929. // Anything starting with {} will have the first two bytes preserved
  2930. // but *only* at the top level, so {},a}b will not expand to anything,
  2931. // but a{},b}c will be expanded to [a}c,abc].
  2932. // One could argue that this is a bug in Bash, but since the goal of
  2933. // this module is to match Bash's rules, we escape a leading {}
  2934. if (str.substr(0, 2) === '{}') {
  2935. str = '\\{\\}' + str.substr(2);
  2936. }
  2937. return expand$4(escapeBraces(str), true).map(unescapeBraces);
  2938. }
  2939. function embrace(str) {
  2940. return '{' + str + '}';
  2941. }
  2942. function isPadded(el) {
  2943. return /^-?0\d/.test(el);
  2944. }
  2945. function lte(i, y) {
  2946. return i <= y;
  2947. }
  2948. function gte(i, y) {
  2949. return i >= y;
  2950. }
  2951. function expand$4(str, isTop) {
  2952. var expansions = [];
  2953. var m = balanced('{', '}', str);
  2954. if (!m) return [str];
  2955. // no need to expand pre, since it is guaranteed to be free of brace-sets
  2956. var pre = m.pre;
  2957. var post = m.post.length
  2958. ? expand$4(m.post, false)
  2959. : [''];
  2960. if (/\$$/.test(m.pre)) {
  2961. for (var k = 0; k < post.length; k++) {
  2962. var expansion = pre+ '{' + m.body + '}' + post[k];
  2963. expansions.push(expansion);
  2964. }
  2965. } else {
  2966. var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
  2967. var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
  2968. var isSequence = isNumericSequence || isAlphaSequence;
  2969. var isOptions = m.body.indexOf(',') >= 0;
  2970. if (!isSequence && !isOptions) {
  2971. // {a},b}
  2972. if (m.post.match(/,.*\}/)) {
  2973. str = m.pre + '{' + m.body + escClose + m.post;
  2974. return expand$4(str);
  2975. }
  2976. return [str];
  2977. }
  2978. var n;
  2979. if (isSequence) {
  2980. n = m.body.split(/\.\./);
  2981. } else {
  2982. n = parseCommaParts(m.body);
  2983. if (n.length === 1) {
  2984. // x{{a,b}}y ==> x{a}y x{b}y
  2985. n = expand$4(n[0], false).map(embrace);
  2986. if (n.length === 1) {
  2987. return post.map(function(p) {
  2988. return m.pre + n[0] + p;
  2989. });
  2990. }
  2991. }
  2992. }
  2993. // at this point, n is the parts, and we know it's not a comma set
  2994. // with a single entry.
  2995. var N;
  2996. if (isSequence) {
  2997. var x = numeric(n[0]);
  2998. var y = numeric(n[1]);
  2999. var width = Math.max(n[0].length, n[1].length);
  3000. var incr = n.length == 3
  3001. ? Math.abs(numeric(n[2]))
  3002. : 1;
  3003. var test = lte;
  3004. var reverse = y < x;
  3005. if (reverse) {
  3006. incr *= -1;
  3007. test = gte;
  3008. }
  3009. var pad = n.some(isPadded);
  3010. N = [];
  3011. for (var i = x; test(i, y); i += incr) {
  3012. var c;
  3013. if (isAlphaSequence) {
  3014. c = String.fromCharCode(i);
  3015. if (c === '\\')
  3016. c = '';
  3017. } else {
  3018. c = String(i);
  3019. if (pad) {
  3020. var need = width - c.length;
  3021. if (need > 0) {
  3022. var z = new Array(need + 1).join('0');
  3023. if (i < 0)
  3024. c = '-' + z + c.slice(1);
  3025. else
  3026. c = z + c;
  3027. }
  3028. }
  3029. }
  3030. N.push(c);
  3031. }
  3032. } else {
  3033. N = [];
  3034. for (var j = 0; j < n.length; j++) {
  3035. N.push.apply(N, expand$4(n[j], false));
  3036. }
  3037. }
  3038. for (var j = 0; j < N.length; j++) {
  3039. for (var k = 0; k < post.length; k++) {
  3040. var expansion = pre + N[j] + post[k];
  3041. if (!isTop || isSequence || expansion)
  3042. expansions.push(expansion);
  3043. }
  3044. }
  3045. }
  3046. return expansions;
  3047. }
  3048. const minimatch$1 = minimatch_1 = (p, pattern, options = {}) => {
  3049. assertValidPattern(pattern);
  3050. // shortcut: comments match nothing.
  3051. if (!options.nocomment && pattern.charAt(0) === '#') {
  3052. return false
  3053. }
  3054. return new Minimatch$1(pattern, options).match(p)
  3055. };
  3056. var minimatch_1 = minimatch$1;
  3057. const path$j = path$k;
  3058. minimatch$1.sep = path$j.sep;
  3059. const GLOBSTAR$2 = Symbol('globstar **');
  3060. minimatch$1.GLOBSTAR = GLOBSTAR$2;
  3061. const expand$3 = braceExpansion;
  3062. const plTypes = {
  3063. '!': { open: '(?:(?!(?:', close: '))[^/]*?)'},
  3064. '?': { open: '(?:', close: ')?' },
  3065. '+': { open: '(?:', close: ')+' },
  3066. '*': { open: '(?:', close: ')*' },
  3067. '@': { open: '(?:', close: ')' }
  3068. };
  3069. // any single thing other than /
  3070. // don't need to escape / when using new RegExp()
  3071. const qmark = '[^/]';
  3072. // * => any number of characters
  3073. const star = qmark + '*?';
  3074. // ** when dots are allowed. Anything goes, except .. and .
  3075. // not (^ or / followed by one or two dots followed by $ or /),
  3076. // followed by anything, any number of times.
  3077. const twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?';
  3078. // not a ^ or / followed by a dot,
  3079. // followed by anything, any number of times.
  3080. const twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?';
  3081. // "abc" -> { a:true, b:true, c:true }
  3082. const charSet = s => s.split('').reduce((set, c) => {
  3083. set[c] = true;
  3084. return set
  3085. }, {});
  3086. // characters that need to be escaped in RegExp.
  3087. const reSpecials = charSet('().*{}+?[]^$\\!');
  3088. // characters that indicate we have to add the pattern start
  3089. const addPatternStartSet = charSet('[.(');
  3090. // normalizes slashes.
  3091. const slashSplit = /\/+/;
  3092. minimatch$1.filter = (pattern, options = {}) =>
  3093. (p, i, list) => minimatch$1(p, pattern, options);
  3094. const ext = (a, b = {}) => {
  3095. const t = {};
  3096. Object.keys(a).forEach(k => t[k] = a[k]);
  3097. Object.keys(b).forEach(k => t[k] = b[k]);
  3098. return t
  3099. };
  3100. minimatch$1.defaults = def => {
  3101. if (!def || typeof def !== 'object' || !Object.keys(def).length) {
  3102. return minimatch$1
  3103. }
  3104. const orig = minimatch$1;
  3105. const m = (p, pattern, options) => orig(p, pattern, ext(def, options));
  3106. m.Minimatch = class Minimatch extends orig.Minimatch {
  3107. constructor (pattern, options) {
  3108. super(pattern, ext(def, options));
  3109. }
  3110. };
  3111. m.Minimatch.defaults = options => orig.defaults(ext(def, options)).Minimatch;
  3112. m.filter = (pattern, options) => orig.filter(pattern, ext(def, options));
  3113. m.defaults = options => orig.defaults(ext(def, options));
  3114. m.makeRe = (pattern, options) => orig.makeRe(pattern, ext(def, options));
  3115. m.braceExpand = (pattern, options) => orig.braceExpand(pattern, ext(def, options));
  3116. m.match = (list, pattern, options) => orig.match(list, pattern, ext(def, options));
  3117. return m
  3118. };
  3119. // Brace expansion:
  3120. // a{b,c}d -> abd acd
  3121. // a{b,}c -> abc ac
  3122. // a{0..3}d -> a0d a1d a2d a3d
  3123. // a{b,c{d,e}f}g -> abg acdfg acefg
  3124. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  3125. //
  3126. // Invalid sets are not expanded.
  3127. // a{2..}b -> a{2..}b
  3128. // a{b}c -> a{b}c
  3129. minimatch$1.braceExpand = (pattern, options) => braceExpand(pattern, options);
  3130. const braceExpand = (pattern, options = {}) => {
  3131. assertValidPattern(pattern);
  3132. // Thanks to Yeting Li <https://github.com/yetingli> for
  3133. // improving this regexp to avoid a ReDOS vulnerability.
  3134. if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
  3135. // shortcut. no need to expand.
  3136. return [pattern]
  3137. }
  3138. return expand$3(pattern)
  3139. };
  3140. const MAX_PATTERN_LENGTH = 1024 * 64;
  3141. const assertValidPattern = pattern => {
  3142. if (typeof pattern !== 'string') {
  3143. throw new TypeError('invalid pattern')
  3144. }
  3145. if (pattern.length > MAX_PATTERN_LENGTH) {
  3146. throw new TypeError('pattern is too long')
  3147. }
  3148. };
  3149. // parse a component of the expanded set.
  3150. // At this point, no pattern may contain "/" in it
  3151. // so we're going to return a 2d array, where each entry is the full
  3152. // pattern, split on '/', and then turned into a regular expression.
  3153. // A regexp is made at the end which joins each array with an
  3154. // escaped /, and another full one which joins each regexp with |.
  3155. //
  3156. // Following the lead of Bash 4.1, note that "**" only has special meaning
  3157. // when it is the *only* thing in a path portion. Otherwise, any series
  3158. // of * is equivalent to a single *. Globstar behavior is enabled by
  3159. // default, and can be disabled by setting options.noglobstar.
  3160. const SUBPARSE = Symbol('subparse');
  3161. minimatch$1.makeRe = (pattern, options) =>
  3162. new Minimatch$1(pattern, options || {}).makeRe();
  3163. minimatch$1.match = (list, pattern, options = {}) => {
  3164. const mm = new Minimatch$1(pattern, options);
  3165. list = list.filter(f => mm.match(f));
  3166. if (mm.options.nonull && !list.length) {
  3167. list.push(pattern);
  3168. }
  3169. return list
  3170. };
  3171. // replace stuff like \* with *
  3172. const globUnescape = s => s.replace(/\\(.)/g, '$1');
  3173. const charUnescape = s => s.replace(/\\([^-\]])/g, '$1');
  3174. const regExpEscape = s => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  3175. const braExpEscape = s => s.replace(/[[\]\\]/g, '\\$&');
  3176. let Minimatch$1 = class Minimatch {
  3177. constructor (pattern, options) {
  3178. assertValidPattern(pattern);
  3179. if (!options) options = {};
  3180. this.options = options;
  3181. this.set = [];
  3182. this.pattern = pattern;
  3183. this.windowsPathsNoEscape = !!options.windowsPathsNoEscape ||
  3184. options.allowWindowsEscape === false;
  3185. if (this.windowsPathsNoEscape) {
  3186. this.pattern = this.pattern.replace(/\\/g, '/');
  3187. }
  3188. this.regexp = null;
  3189. this.negate = false;
  3190. this.comment = false;
  3191. this.empty = false;
  3192. this.partial = !!options.partial;
  3193. // make the set of regexps etc.
  3194. this.make();
  3195. }
  3196. debug () {}
  3197. make () {
  3198. const pattern = this.pattern;
  3199. const options = this.options;
  3200. // empty patterns and comments match nothing.
  3201. if (!options.nocomment && pattern.charAt(0) === '#') {
  3202. this.comment = true;
  3203. return
  3204. }
  3205. if (!pattern) {
  3206. this.empty = true;
  3207. return
  3208. }
  3209. // step 1: figure out negation, etc.
  3210. this.parseNegate();
  3211. // step 2: expand braces
  3212. let set = this.globSet = this.braceExpand();
  3213. if (options.debug) this.debug = (...args) => console.error(...args);
  3214. this.debug(this.pattern, set);
  3215. // step 3: now we have a set, so turn each one into a series of path-portion
  3216. // matching patterns.
  3217. // These will be regexps, except in the case of "**", which is
  3218. // set to the GLOBSTAR object for globstar behavior,
  3219. // and will not contain any / characters
  3220. set = this.globParts = set.map(s => s.split(slashSplit));
  3221. this.debug(this.pattern, set);
  3222. // glob --> regexps
  3223. set = set.map((s, si, set) => s.map(this.parse, this));
  3224. this.debug(this.pattern, set);
  3225. // filter out everything that didn't compile properly.
  3226. set = set.filter(s => s.indexOf(false) === -1);
  3227. this.debug(this.pattern, set);
  3228. this.set = set;
  3229. }
  3230. parseNegate () {
  3231. if (this.options.nonegate) return
  3232. const pattern = this.pattern;
  3233. let negate = false;
  3234. let negateOffset = 0;
  3235. for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
  3236. negate = !negate;
  3237. negateOffset++;
  3238. }
  3239. if (negateOffset) this.pattern = pattern.slice(negateOffset);
  3240. this.negate = negate;
  3241. }
  3242. // set partial to true to test if, for example,
  3243. // "/a/b" matches the start of "/*/b/*/d"
  3244. // Partial means, if you run out of file before you run
  3245. // out of pattern, then that's fine, as long as all
  3246. // the parts match.
  3247. matchOne (file, pattern, partial) {
  3248. var options = this.options;
  3249. this.debug('matchOne',
  3250. { 'this': this, file: file, pattern: pattern });
  3251. this.debug('matchOne', file.length, pattern.length);
  3252. for (var fi = 0,
  3253. pi = 0,
  3254. fl = file.length,
  3255. pl = pattern.length
  3256. ; (fi < fl) && (pi < pl)
  3257. ; fi++, pi++) {
  3258. this.debug('matchOne loop');
  3259. var p = pattern[pi];
  3260. var f = file[fi];
  3261. this.debug(pattern, p, f);
  3262. // should be impossible.
  3263. // some invalid regexp stuff in the set.
  3264. /* istanbul ignore if */
  3265. if (p === false) return false
  3266. if (p === GLOBSTAR$2) {
  3267. this.debug('GLOBSTAR', [pattern, p, f]);
  3268. // "**"
  3269. // a/**/b/**/c would match the following:
  3270. // a/b/x/y/z/c
  3271. // a/x/y/z/b/c
  3272. // a/b/x/b/x/c
  3273. // a/b/c
  3274. // To do this, take the rest of the pattern after
  3275. // the **, and see if it would match the file remainder.
  3276. // If so, return success.
  3277. // If not, the ** "swallows" a segment, and try again.
  3278. // This is recursively awful.
  3279. //
  3280. // a/**/b/**/c matching a/b/x/y/z/c
  3281. // - a matches a
  3282. // - doublestar
  3283. // - matchOne(b/x/y/z/c, b/**/c)
  3284. // - b matches b
  3285. // - doublestar
  3286. // - matchOne(x/y/z/c, c) -> no
  3287. // - matchOne(y/z/c, c) -> no
  3288. // - matchOne(z/c, c) -> no
  3289. // - matchOne(c, c) yes, hit
  3290. var fr = fi;
  3291. var pr = pi + 1;
  3292. if (pr === pl) {
  3293. this.debug('** at the end');
  3294. // a ** at the end will just swallow the rest.
  3295. // We have found a match.
  3296. // however, it will not swallow /.x, unless
  3297. // options.dot is set.
  3298. // . and .. are *never* matched by **, for explosively
  3299. // exponential reasons.
  3300. for (; fi < fl; fi++) {
  3301. if (file[fi] === '.' || file[fi] === '..' ||
  3302. (!options.dot && file[fi].charAt(0) === '.')) return false
  3303. }
  3304. return true
  3305. }
  3306. // ok, let's see if we can swallow whatever we can.
  3307. while (fr < fl) {
  3308. var swallowee = file[fr];
  3309. this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
  3310. // XXX remove this slice. Just pass the start index.
  3311. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  3312. this.debug('globstar found match!', fr, fl, swallowee);
  3313. // found a match.
  3314. return true
  3315. } else {
  3316. // can't swallow "." or ".." ever.
  3317. // can only swallow ".foo" when explicitly asked.
  3318. if (swallowee === '.' || swallowee === '..' ||
  3319. (!options.dot && swallowee.charAt(0) === '.')) {
  3320. this.debug('dot detected!', file, fr, pattern, pr);
  3321. break
  3322. }
  3323. // ** swallows a segment, and continue.
  3324. this.debug('globstar swallow a segment, and continue');
  3325. fr++;
  3326. }
  3327. }
  3328. // no match was found.
  3329. // However, in partial mode, we can't say this is necessarily over.
  3330. // If there's more *pattern* left, then
  3331. /* istanbul ignore if */
  3332. if (partial) {
  3333. // ran out of file
  3334. this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
  3335. if (fr === fl) return true
  3336. }
  3337. return false
  3338. }
  3339. // something other than **
  3340. // non-magic patterns just have to match exactly
  3341. // patterns with magic have been turned into regexps.
  3342. var hit;
  3343. if (typeof p === 'string') {
  3344. hit = f === p;
  3345. this.debug('string match', p, f, hit);
  3346. } else {
  3347. hit = f.match(p);
  3348. this.debug('pattern match', p, f, hit);
  3349. }
  3350. if (!hit) return false
  3351. }
  3352. // Note: ending in / means that we'll get a final ""
  3353. // at the end of the pattern. This can only match a
  3354. // corresponding "" at the end of the file.
  3355. // If the file ends in /, then it can only match a
  3356. // a pattern that ends in /, unless the pattern just
  3357. // doesn't have any more for it. But, a/b/ should *not*
  3358. // match "a/b/*", even though "" matches against the
  3359. // [^/]*? pattern, except in partial mode, where it might
  3360. // simply not be reached yet.
  3361. // However, a/b/ should still satisfy a/*
  3362. // now either we fell off the end of the pattern, or we're done.
  3363. if (fi === fl && pi === pl) {
  3364. // ran out of pattern and filename at the same time.
  3365. // an exact hit!
  3366. return true
  3367. } else if (fi === fl) {
  3368. // ran out of file, but still had pattern left.
  3369. // this is ok if we're doing the match as part of
  3370. // a glob fs traversal.
  3371. return partial
  3372. } else /* istanbul ignore else */ if (pi === pl) {
  3373. // ran out of pattern, still have file left.
  3374. // this is only acceptable if we're on the very last
  3375. // empty segment of a file with a trailing slash.
  3376. // a/* should match a/b/
  3377. return (fi === fl - 1) && (file[fi] === '')
  3378. }
  3379. // should be unreachable.
  3380. /* istanbul ignore next */
  3381. throw new Error('wtf?')
  3382. }
  3383. braceExpand () {
  3384. return braceExpand(this.pattern, this.options)
  3385. }
  3386. parse (pattern, isSub) {
  3387. assertValidPattern(pattern);
  3388. const options = this.options;
  3389. // shortcuts
  3390. if (pattern === '**') {
  3391. if (!options.noglobstar)
  3392. return GLOBSTAR$2
  3393. else
  3394. pattern = '*';
  3395. }
  3396. if (pattern === '') return ''
  3397. let re = '';
  3398. let hasMagic = false;
  3399. let escaping = false;
  3400. // ? => one single character
  3401. const patternListStack = [];
  3402. const negativeLists = [];
  3403. let stateChar;
  3404. let inClass = false;
  3405. let reClassStart = -1;
  3406. let classStart = -1;
  3407. let cs;
  3408. let pl;
  3409. let sp;
  3410. // . and .. never match anything that doesn't start with .,
  3411. // even when options.dot is set. However, if the pattern
  3412. // starts with ., then traversal patterns can match.
  3413. let dotTravAllowed = pattern.charAt(0) === '.';
  3414. let dotFileAllowed = options.dot || dotTravAllowed;
  3415. const patternStart = () =>
  3416. dotTravAllowed
  3417. ? ''
  3418. : dotFileAllowed
  3419. ? '(?!(?:^|\\/)\\.{1,2}(?:$|\\/))'
  3420. : '(?!\\.)';
  3421. const subPatternStart = (p) =>
  3422. p.charAt(0) === '.'
  3423. ? ''
  3424. : options.dot
  3425. ? '(?!(?:^|\\/)\\.{1,2}(?:$|\\/))'
  3426. : '(?!\\.)';
  3427. const clearStateChar = () => {
  3428. if (stateChar) {
  3429. // we had some state-tracking character
  3430. // that wasn't consumed by this pass.
  3431. switch (stateChar) {
  3432. case '*':
  3433. re += star;
  3434. hasMagic = true;
  3435. break
  3436. case '?':
  3437. re += qmark;
  3438. hasMagic = true;
  3439. break
  3440. default:
  3441. re += '\\' + stateChar;
  3442. break
  3443. }
  3444. this.debug('clearStateChar %j %j', stateChar, re);
  3445. stateChar = false;
  3446. }
  3447. };
  3448. for (let i = 0, c; (i < pattern.length) && (c = pattern.charAt(i)); i++) {
  3449. this.debug('%s\t%s %s %j', pattern, i, re, c);
  3450. // skip over any that are escaped.
  3451. if (escaping) {
  3452. /* istanbul ignore next - completely not allowed, even escaped. */
  3453. if (c === '/') {
  3454. return false
  3455. }
  3456. if (reSpecials[c]) {
  3457. re += '\\';
  3458. }
  3459. re += c;
  3460. escaping = false;
  3461. continue
  3462. }
  3463. switch (c) {
  3464. /* istanbul ignore next */
  3465. case '/': {
  3466. // Should already be path-split by now.
  3467. return false
  3468. }
  3469. case '\\':
  3470. if (inClass && pattern.charAt(i + 1) === '-') {
  3471. re += c;
  3472. continue
  3473. }
  3474. clearStateChar();
  3475. escaping = true;
  3476. continue
  3477. // the various stateChar values
  3478. // for the "extglob" stuff.
  3479. case '?':
  3480. case '*':
  3481. case '+':
  3482. case '@':
  3483. case '!':
  3484. this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c);
  3485. // all of those are literals inside a class, except that
  3486. // the glob [!a] means [^a] in regexp
  3487. if (inClass) {
  3488. this.debug(' in class');
  3489. if (c === '!' && i === classStart + 1) c = '^';
  3490. re += c;
  3491. continue
  3492. }
  3493. // if we already have a stateChar, then it means
  3494. // that there was something like ** or +? in there.
  3495. // Handle the stateChar, then proceed with this one.
  3496. this.debug('call clearStateChar %j', stateChar);
  3497. clearStateChar();
  3498. stateChar = c;
  3499. // if extglob is disabled, then +(asdf|foo) isn't a thing.
  3500. // just clear the statechar *now*, rather than even diving into
  3501. // the patternList stuff.
  3502. if (options.noext) clearStateChar();
  3503. continue
  3504. case '(': {
  3505. if (inClass) {
  3506. re += '(';
  3507. continue
  3508. }
  3509. if (!stateChar) {
  3510. re += '\\(';
  3511. continue
  3512. }
  3513. const plEntry = {
  3514. type: stateChar,
  3515. start: i - 1,
  3516. reStart: re.length,
  3517. open: plTypes[stateChar].open,
  3518. close: plTypes[stateChar].close,
  3519. };
  3520. this.debug(this.pattern, '\t', plEntry);
  3521. patternListStack.push(plEntry);
  3522. // negation is (?:(?!(?:js)(?:<rest>))[^/]*)
  3523. re += plEntry.open;
  3524. // next entry starts with a dot maybe?
  3525. if (plEntry.start === 0 && plEntry.type !== '!') {
  3526. dotTravAllowed = true;
  3527. re += subPatternStart(pattern.slice(i + 1));
  3528. }
  3529. this.debug('plType %j %j', stateChar, re);
  3530. stateChar = false;
  3531. continue
  3532. }
  3533. case ')': {
  3534. const plEntry = patternListStack[patternListStack.length - 1];
  3535. if (inClass || !plEntry) {
  3536. re += '\\)';
  3537. continue
  3538. }
  3539. patternListStack.pop();
  3540. // closing an extglob
  3541. clearStateChar();
  3542. hasMagic = true;
  3543. pl = plEntry;
  3544. // negation is (?:(?!js)[^/]*)
  3545. // The others are (?:<pattern>)<type>
  3546. re += pl.close;
  3547. if (pl.type === '!') {
  3548. negativeLists.push(Object.assign(pl, { reEnd: re.length }));
  3549. }
  3550. continue
  3551. }
  3552. case '|': {
  3553. const plEntry = patternListStack[patternListStack.length - 1];
  3554. if (inClass || !plEntry) {
  3555. re += '\\|';
  3556. continue
  3557. }
  3558. clearStateChar();
  3559. re += '|';
  3560. // next subpattern can start with a dot?
  3561. if (plEntry.start === 0 && plEntry.type !== '!') {
  3562. dotTravAllowed = true;
  3563. re += subPatternStart(pattern.slice(i + 1));
  3564. }
  3565. continue
  3566. }
  3567. // these are mostly the same in regexp and glob
  3568. case '[':
  3569. // swallow any state-tracking char before the [
  3570. clearStateChar();
  3571. if (inClass) {
  3572. re += '\\' + c;
  3573. continue
  3574. }
  3575. inClass = true;
  3576. classStart = i;
  3577. reClassStart = re.length;
  3578. re += c;
  3579. continue
  3580. case ']':
  3581. // a right bracket shall lose its special
  3582. // meaning and represent itself in
  3583. // a bracket expression if it occurs
  3584. // first in the list. -- POSIX.2 2.8.3.2
  3585. if (i === classStart + 1 || !inClass) {
  3586. re += '\\' + c;
  3587. continue
  3588. }
  3589. // split where the last [ was, make sure we don't have
  3590. // an invalid re. if so, re-walk the contents of the
  3591. // would-be class to re-translate any characters that
  3592. // were passed through as-is
  3593. // TODO: It would probably be faster to determine this
  3594. // without a try/catch and a new RegExp, but it's tricky
  3595. // to do safely. For now, this is safe and works.
  3596. cs = pattern.substring(classStart + 1, i);
  3597. try {
  3598. RegExp('[' + braExpEscape(charUnescape(cs)) + ']');
  3599. // looks good, finish up the class.
  3600. re += c;
  3601. } catch (er) {
  3602. // out of order ranges in JS are errors, but in glob syntax,
  3603. // they're just a range that matches nothing.
  3604. re = re.substring(0, reClassStart) + '(?:$.)'; // match nothing ever
  3605. }
  3606. hasMagic = true;
  3607. inClass = false;
  3608. continue
  3609. default:
  3610. // swallow any state char that wasn't consumed
  3611. clearStateChar();
  3612. if (reSpecials[c] && !(c === '^' && inClass)) {
  3613. re += '\\';
  3614. }
  3615. re += c;
  3616. break
  3617. } // switch
  3618. } // for
  3619. // handle the case where we left a class open.
  3620. // "[abc" is valid, equivalent to "\[abc"
  3621. if (inClass) {
  3622. // split where the last [ was, and escape it
  3623. // this is a huge pita. We now have to re-walk
  3624. // the contents of the would-be class to re-translate
  3625. // any characters that were passed through as-is
  3626. cs = pattern.slice(classStart + 1);
  3627. sp = this.parse(cs, SUBPARSE);
  3628. re = re.substring(0, reClassStart) + '\\[' + sp[0];
  3629. hasMagic = hasMagic || sp[1];
  3630. }
  3631. // handle the case where we had a +( thing at the *end*
  3632. // of the pattern.
  3633. // each pattern list stack adds 3 chars, and we need to go through
  3634. // and escape any | chars that were passed through as-is for the regexp.
  3635. // Go through and escape them, taking care not to double-escape any
  3636. // | chars that were already escaped.
  3637. for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
  3638. let tail;
  3639. tail = re.slice(pl.reStart + pl.open.length);
  3640. this.debug('setting tail', re, pl);
  3641. // maybe some even number of \, then maybe 1 \, followed by a |
  3642. tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, (_, $1, $2) => {
  3643. /* istanbul ignore else - should already be done */
  3644. if (!$2) {
  3645. // the | isn't already escaped, so escape it.
  3646. $2 = '\\';
  3647. }
  3648. // need to escape all those slashes *again*, without escaping the
  3649. // one that we need for escaping the | character. As it works out,
  3650. // escaping an even number of slashes can be done by simply repeating
  3651. // it exactly after itself. That's why this trick works.
  3652. //
  3653. // I am sorry that you have to see this.
  3654. return $1 + $1 + $2 + '|'
  3655. });
  3656. this.debug('tail=%j\n %s', tail, tail, pl, re);
  3657. const t = pl.type === '*' ? star
  3658. : pl.type === '?' ? qmark
  3659. : '\\' + pl.type;
  3660. hasMagic = true;
  3661. re = re.slice(0, pl.reStart) + t + '\\(' + tail;
  3662. }
  3663. // handle trailing things that only matter at the very end.
  3664. clearStateChar();
  3665. if (escaping) {
  3666. // trailing \\
  3667. re += '\\\\';
  3668. }
  3669. // only need to apply the nodot start if the re starts with
  3670. // something that could conceivably capture a dot
  3671. const addPatternStart = addPatternStartSet[re.charAt(0)];
  3672. // Hack to work around lack of negative lookbehind in JS
  3673. // A pattern like: *.!(x).!(y|z) needs to ensure that a name
  3674. // like 'a.xyz.yz' doesn't match. So, the first negative
  3675. // lookahead, has to look ALL the way ahead, to the end of
  3676. // the pattern.
  3677. for (let n = negativeLists.length - 1; n > -1; n--) {
  3678. const nl = negativeLists[n];
  3679. const nlBefore = re.slice(0, nl.reStart);
  3680. const nlFirst = re.slice(nl.reStart, nl.reEnd - 8);
  3681. let nlAfter = re.slice(nl.reEnd);
  3682. const nlLast = re.slice(nl.reEnd - 8, nl.reEnd) + nlAfter;
  3683. // Handle nested stuff like *(*.js|!(*.json)), where open parens
  3684. // mean that we should *not* include the ) in the bit that is considered
  3685. // "after" the negated section.
  3686. const closeParensBefore = nlBefore.split(')').length;
  3687. const openParensBefore = nlBefore.split('(').length - closeParensBefore;
  3688. let cleanAfter = nlAfter;
  3689. for (let i = 0; i < openParensBefore; i++) {
  3690. cleanAfter = cleanAfter.replace(/\)[+*?]?/, '');
  3691. }
  3692. nlAfter = cleanAfter;
  3693. const dollar = nlAfter === '' && isSub !== SUBPARSE ? '(?:$|\\/)' : '';
  3694. re = nlBefore + nlFirst + nlAfter + dollar + nlLast;
  3695. }
  3696. // if the re is not "" at this point, then we need to make sure
  3697. // it doesn't match against an empty path part.
  3698. // Otherwise a/* will match a/, which it should not.
  3699. if (re !== '' && hasMagic) {
  3700. re = '(?=.)' + re;
  3701. }
  3702. if (addPatternStart) {
  3703. re = patternStart() + re;
  3704. }
  3705. // parsing just a piece of a larger pattern.
  3706. if (isSub === SUBPARSE) {
  3707. return [re, hasMagic]
  3708. }
  3709. // if it's nocase, and the lcase/uppercase don't match, it's magic
  3710. if (options.nocase && !hasMagic) {
  3711. hasMagic = pattern.toUpperCase() !== pattern.toLowerCase();
  3712. }
  3713. // skip the regexp for non-magical patterns
  3714. // unescape anything in it, though, so that it'll be
  3715. // an exact match against a file etc.
  3716. if (!hasMagic) {
  3717. return globUnescape(pattern)
  3718. }
  3719. const flags = options.nocase ? 'i' : '';
  3720. try {
  3721. return Object.assign(new RegExp('^' + re + '$', flags), {
  3722. _glob: pattern,
  3723. _src: re,
  3724. })
  3725. } catch (er) /* istanbul ignore next - should be impossible */ {
  3726. // If it was an invalid regular expression, then it can't match
  3727. // anything. This trick looks for a character after the end of
  3728. // the string, which is of course impossible, except in multi-line
  3729. // mode, but it's not a /m regex.
  3730. return new RegExp('$.')
  3731. }
  3732. }
  3733. makeRe () {
  3734. if (this.regexp || this.regexp === false) return this.regexp
  3735. // at this point, this.set is a 2d array of partial
  3736. // pattern strings, or "**".
  3737. //
  3738. // It's better to use .match(). This function shouldn't
  3739. // be used, really, but it's pretty convenient sometimes,
  3740. // when you just want to work with a regex.
  3741. const set = this.set;
  3742. if (!set.length) {
  3743. this.regexp = false;
  3744. return this.regexp
  3745. }
  3746. const options = this.options;
  3747. const twoStar = options.noglobstar ? star
  3748. : options.dot ? twoStarDot
  3749. : twoStarNoDot;
  3750. const flags = options.nocase ? 'i' : '';
  3751. // coalesce globstars and regexpify non-globstar patterns
  3752. // if it's the only item, then we just do one twoStar
  3753. // if it's the first, and there are more, prepend (\/|twoStar\/)? to next
  3754. // if it's the last, append (\/twoStar|) to previous
  3755. // if it's in the middle, append (\/|\/twoStar\/) to previous
  3756. // then filter out GLOBSTAR symbols
  3757. let re = set.map(pattern => {
  3758. pattern = pattern.map(p =>
  3759. typeof p === 'string' ? regExpEscape(p)
  3760. : p === GLOBSTAR$2 ? GLOBSTAR$2
  3761. : p._src
  3762. ).reduce((set, p) => {
  3763. if (!(set[set.length - 1] === GLOBSTAR$2 && p === GLOBSTAR$2)) {
  3764. set.push(p);
  3765. }
  3766. return set
  3767. }, []);
  3768. pattern.forEach((p, i) => {
  3769. if (p !== GLOBSTAR$2 || pattern[i-1] === GLOBSTAR$2) {
  3770. return
  3771. }
  3772. if (i === 0) {
  3773. if (pattern.length > 1) {
  3774. pattern[i+1] = '(?:\\\/|' + twoStar + '\\\/)?' + pattern[i+1];
  3775. } else {
  3776. pattern[i] = twoStar;
  3777. }
  3778. } else if (i === pattern.length - 1) {
  3779. pattern[i-1] += '(?:\\\/|' + twoStar + ')?';
  3780. } else {
  3781. pattern[i-1] += '(?:\\\/|\\\/' + twoStar + '\\\/)' + pattern[i+1];
  3782. pattern[i+1] = GLOBSTAR$2;
  3783. }
  3784. });
  3785. return pattern.filter(p => p !== GLOBSTAR$2).join('/')
  3786. }).join('|');
  3787. // must match entire pattern
  3788. // ending in a * or ** will make it less strict.
  3789. re = '^(?:' + re + ')$';
  3790. // can match anything, as long as it's not this.
  3791. if (this.negate) re = '^(?!' + re + ').*$';
  3792. try {
  3793. this.regexp = new RegExp(re, flags);
  3794. } catch (ex) /* istanbul ignore next - should be impossible */ {
  3795. this.regexp = false;
  3796. }
  3797. return this.regexp
  3798. }
  3799. match (f, partial = this.partial) {
  3800. this.debug('match', f, this.pattern);
  3801. // short-circuit in the case of busted things.
  3802. // comments, etc.
  3803. if (this.comment) return false
  3804. if (this.empty) return f === ''
  3805. if (f === '/' && partial) return true
  3806. const options = this.options;
  3807. // windows: need to use /, not \
  3808. if (path$j.sep !== '/') {
  3809. f = f.split(path$j.sep).join('/');
  3810. }
  3811. // treat the test path as a set of pathparts.
  3812. f = f.split(slashSplit);
  3813. this.debug(this.pattern, 'split', f);
  3814. // just ONE of the pattern sets in this.set needs to match
  3815. // in order for it to be valid. If negating, then just one
  3816. // match means that we have failed.
  3817. // Either way, return on the first hit.
  3818. const set = this.set;
  3819. this.debug(this.pattern, 'set', set);
  3820. // Find the basename of the path by looking for the last non-empty segment
  3821. let filename;
  3822. for (let i = f.length - 1; i >= 0; i--) {
  3823. filename = f[i];
  3824. if (filename) break
  3825. }
  3826. for (let i = 0; i < set.length; i++) {
  3827. const pattern = set[i];
  3828. let file = f;
  3829. if (options.matchBase && pattern.length === 1) {
  3830. file = [filename];
  3831. }
  3832. const hit = this.matchOne(file, pattern, partial);
  3833. if (hit) {
  3834. if (options.flipNegate) return true
  3835. return !this.negate
  3836. }
  3837. }
  3838. // didn't get any hits. this is success if it's a negative
  3839. // pattern, failure otherwise.
  3840. if (options.flipNegate) return false
  3841. return this.negate
  3842. }
  3843. static defaults (def) {
  3844. return minimatch$1.defaults(def).Minimatch
  3845. }
  3846. };
  3847. minimatch$1.Minimatch = Minimatch$1;
  3848. var inherits = {exports: {}};
  3849. var inherits_browser = {exports: {}};
  3850. var hasRequiredInherits_browser;
  3851. function requireInherits_browser () {
  3852. if (hasRequiredInherits_browser) return inherits_browser.exports;
  3853. hasRequiredInherits_browser = 1;
  3854. if (typeof Object.create === 'function') {
  3855. // implementation from standard node.js 'util' module
  3856. inherits_browser.exports = function inherits(ctor, superCtor) {
  3857. if (superCtor) {
  3858. ctor.super_ = superCtor;
  3859. ctor.prototype = Object.create(superCtor.prototype, {
  3860. constructor: {
  3861. value: ctor,
  3862. enumerable: false,
  3863. writable: true,
  3864. configurable: true
  3865. }
  3866. });
  3867. }
  3868. };
  3869. } else {
  3870. // old school shim for old browsers
  3871. inherits_browser.exports = function inherits(ctor, superCtor) {
  3872. if (superCtor) {
  3873. ctor.super_ = superCtor;
  3874. var TempCtor = function () {};
  3875. TempCtor.prototype = superCtor.prototype;
  3876. ctor.prototype = new TempCtor();
  3877. ctor.prototype.constructor = ctor;
  3878. }
  3879. };
  3880. }
  3881. return inherits_browser.exports;
  3882. }
  3883. try {
  3884. var util$2 = require('util');
  3885. /* istanbul ignore next */
  3886. if (typeof util$2.inherits !== 'function') throw '';
  3887. inherits.exports = util$2.inherits;
  3888. } catch (e) {
  3889. /* istanbul ignore next */
  3890. inherits.exports = requireInherits_browser();
  3891. }
  3892. var inheritsExports = inherits.exports;
  3893. var common$c = {};
  3894. common$c.setopts = setopts;
  3895. common$c.ownProp = ownProp;
  3896. common$c.makeAbs = makeAbs;
  3897. common$c.finish = finish;
  3898. common$c.mark = mark;
  3899. common$c.isIgnored = isIgnored;
  3900. common$c.childrenIgnored = childrenIgnored;
  3901. function ownProp (obj, field) {
  3902. return Object.prototype.hasOwnProperty.call(obj, field)
  3903. }
  3904. var fs$i = require$$0__default;
  3905. var path$i = require$$0$4;
  3906. var minimatch = minimatch_1;
  3907. var isAbsolute = require$$0$4.isAbsolute;
  3908. var Minimatch = minimatch.Minimatch;
  3909. function alphasort (a, b) {
  3910. return a.localeCompare(b, 'en')
  3911. }
  3912. function setupIgnores (self, options) {
  3913. self.ignore = options.ignore || [];
  3914. if (!Array.isArray(self.ignore))
  3915. self.ignore = [self.ignore];
  3916. if (self.ignore.length) {
  3917. self.ignore = self.ignore.map(ignoreMap);
  3918. }
  3919. }
  3920. // ignore patterns are always in dot:true mode.
  3921. function ignoreMap (pattern) {
  3922. var gmatcher = null;
  3923. if (pattern.slice(-3) === '/**') {
  3924. var gpattern = pattern.replace(/(\/\*\*)+$/, '');
  3925. gmatcher = new Minimatch(gpattern, { dot: true });
  3926. }
  3927. return {
  3928. matcher: new Minimatch(pattern, { dot: true }),
  3929. gmatcher: gmatcher
  3930. }
  3931. }
  3932. function setopts (self, pattern, options) {
  3933. if (!options)
  3934. options = {};
  3935. // base-matching: just use globstar for that.
  3936. if (options.matchBase && -1 === pattern.indexOf("/")) {
  3937. if (options.noglobstar) {
  3938. throw new Error("base matching requires globstar")
  3939. }
  3940. pattern = "**/" + pattern;
  3941. }
  3942. self.windowsPathsNoEscape = !!options.windowsPathsNoEscape ||
  3943. options.allowWindowsEscape === false;
  3944. if (self.windowsPathsNoEscape) {
  3945. pattern = pattern.replace(/\\/g, '/');
  3946. }
  3947. self.silent = !!options.silent;
  3948. self.pattern = pattern;
  3949. self.strict = options.strict !== false;
  3950. self.realpath = !!options.realpath;
  3951. self.realpathCache = options.realpathCache || Object.create(null);
  3952. self.follow = !!options.follow;
  3953. self.dot = !!options.dot;
  3954. self.mark = !!options.mark;
  3955. self.nodir = !!options.nodir;
  3956. if (self.nodir)
  3957. self.mark = true;
  3958. self.sync = !!options.sync;
  3959. self.nounique = !!options.nounique;
  3960. self.nonull = !!options.nonull;
  3961. self.nosort = !!options.nosort;
  3962. self.nocase = !!options.nocase;
  3963. self.stat = !!options.stat;
  3964. self.noprocess = !!options.noprocess;
  3965. self.absolute = !!options.absolute;
  3966. self.fs = options.fs || fs$i;
  3967. self.maxLength = options.maxLength || Infinity;
  3968. self.cache = options.cache || Object.create(null);
  3969. self.statCache = options.statCache || Object.create(null);
  3970. self.symlinks = options.symlinks || Object.create(null);
  3971. setupIgnores(self, options);
  3972. self.changedCwd = false;
  3973. var cwd = process.cwd();
  3974. if (!ownProp(options, "cwd"))
  3975. self.cwd = path$i.resolve(cwd);
  3976. else {
  3977. self.cwd = path$i.resolve(options.cwd);
  3978. self.changedCwd = self.cwd !== cwd;
  3979. }
  3980. self.root = options.root || path$i.resolve(self.cwd, "/");
  3981. self.root = path$i.resolve(self.root);
  3982. // TODO: is an absolute `cwd` supposed to be resolved against `root`?
  3983. // e.g. { cwd: '/test', root: __dirname } === path.join(__dirname, '/test')
  3984. self.cwdAbs = isAbsolute(self.cwd) ? self.cwd : makeAbs(self, self.cwd);
  3985. self.nomount = !!options.nomount;
  3986. if (process.platform === "win32") {
  3987. self.root = self.root.replace(/\\/g, "/");
  3988. self.cwd = self.cwd.replace(/\\/g, "/");
  3989. self.cwdAbs = self.cwdAbs.replace(/\\/g, "/");
  3990. }
  3991. // disable comments and negation in Minimatch.
  3992. // Note that they are not supported in Glob itself anyway.
  3993. options.nonegate = true;
  3994. options.nocomment = true;
  3995. self.minimatch = new Minimatch(pattern, options);
  3996. self.options = self.minimatch.options;
  3997. }
  3998. function finish (self) {
  3999. var nou = self.nounique;
  4000. var all = nou ? [] : Object.create(null);
  4001. for (var i = 0, l = self.matches.length; i < l; i ++) {
  4002. var matches = self.matches[i];
  4003. if (!matches || Object.keys(matches).length === 0) {
  4004. if (self.nonull) {
  4005. // do like the shell, and spit out the literal glob
  4006. var literal = self.minimatch.globSet[i];
  4007. if (nou)
  4008. all.push(literal);
  4009. else
  4010. all[literal] = true;
  4011. }
  4012. } else {
  4013. // had matches
  4014. var m = Object.keys(matches);
  4015. if (nou)
  4016. all.push.apply(all, m);
  4017. else
  4018. m.forEach(function (m) {
  4019. all[m] = true;
  4020. });
  4021. }
  4022. }
  4023. if (!nou)
  4024. all = Object.keys(all);
  4025. if (!self.nosort)
  4026. all = all.sort(alphasort);
  4027. // at *some* point we statted all of these
  4028. if (self.mark) {
  4029. for (var i = 0; i < all.length; i++) {
  4030. all[i] = self._mark(all[i]);
  4031. }
  4032. if (self.nodir) {
  4033. all = all.filter(function (e) {
  4034. var notDir = !(/\/$/.test(e));
  4035. var c = self.cache[e] || self.cache[makeAbs(self, e)];
  4036. if (notDir && c)
  4037. notDir = c !== 'DIR' && !Array.isArray(c);
  4038. return notDir
  4039. });
  4040. }
  4041. }
  4042. if (self.ignore.length)
  4043. all = all.filter(function(m) {
  4044. return !isIgnored(self, m)
  4045. });
  4046. self.found = all;
  4047. }
  4048. function mark (self, p) {
  4049. var abs = makeAbs(self, p);
  4050. var c = self.cache[abs];
  4051. var m = p;
  4052. if (c) {
  4053. var isDir = c === 'DIR' || Array.isArray(c);
  4054. var slash = p.slice(-1) === '/';
  4055. if (isDir && !slash)
  4056. m += '/';
  4057. else if (!isDir && slash)
  4058. m = m.slice(0, -1);
  4059. if (m !== p) {
  4060. var mabs = makeAbs(self, m);
  4061. self.statCache[mabs] = self.statCache[abs];
  4062. self.cache[mabs] = self.cache[abs];
  4063. }
  4064. }
  4065. return m
  4066. }
  4067. // lotta situps...
  4068. function makeAbs (self, f) {
  4069. var abs = f;
  4070. if (f.charAt(0) === '/') {
  4071. abs = path$i.join(self.root, f);
  4072. } else if (isAbsolute(f) || f === '') {
  4073. abs = f;
  4074. } else if (self.changedCwd) {
  4075. abs = path$i.resolve(self.cwd, f);
  4076. } else {
  4077. abs = path$i.resolve(f);
  4078. }
  4079. if (process.platform === 'win32')
  4080. abs = abs.replace(/\\/g, '/');
  4081. return abs
  4082. }
  4083. // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
  4084. // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
  4085. function isIgnored (self, path) {
  4086. if (!self.ignore.length)
  4087. return false
  4088. return self.ignore.some(function(item) {
  4089. return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
  4090. })
  4091. }
  4092. function childrenIgnored (self, path) {
  4093. if (!self.ignore.length)
  4094. return false
  4095. return self.ignore.some(function(item) {
  4096. return !!(item.gmatcher && item.gmatcher.match(path))
  4097. })
  4098. }
  4099. var sync$9;
  4100. var hasRequiredSync;
  4101. function requireSync () {
  4102. if (hasRequiredSync) return sync$9;
  4103. hasRequiredSync = 1;
  4104. sync$9 = globSync;
  4105. globSync.GlobSync = GlobSync;
  4106. var rp = fs_realpath;
  4107. var minimatch = minimatch_1;
  4108. requireGlob().Glob;
  4109. var path = require$$0$4;
  4110. var assert = require$$5;
  4111. var isAbsolute = require$$0$4.isAbsolute;
  4112. var common = common$c;
  4113. var setopts = common.setopts;
  4114. var ownProp = common.ownProp;
  4115. var childrenIgnored = common.childrenIgnored;
  4116. var isIgnored = common.isIgnored;
  4117. function globSync (pattern, options) {
  4118. if (typeof options === 'function' || arguments.length === 3)
  4119. throw new TypeError('callback provided to sync glob\n'+
  4120. 'See: https://github.com/isaacs/node-glob/issues/167')
  4121. return new GlobSync(pattern, options).found
  4122. }
  4123. function GlobSync (pattern, options) {
  4124. if (!pattern)
  4125. throw new Error('must provide pattern')
  4126. if (typeof options === 'function' || arguments.length === 3)
  4127. throw new TypeError('callback provided to sync glob\n'+
  4128. 'See: https://github.com/isaacs/node-glob/issues/167')
  4129. if (!(this instanceof GlobSync))
  4130. return new GlobSync(pattern, options)
  4131. setopts(this, pattern, options);
  4132. if (this.noprocess)
  4133. return this
  4134. var n = this.minimatch.set.length;
  4135. this.matches = new Array(n);
  4136. for (var i = 0; i < n; i ++) {
  4137. this._process(this.minimatch.set[i], i, false);
  4138. }
  4139. this._finish();
  4140. }
  4141. GlobSync.prototype._finish = function () {
  4142. assert.ok(this instanceof GlobSync);
  4143. if (this.realpath) {
  4144. var self = this;
  4145. this.matches.forEach(function (matchset, index) {
  4146. var set = self.matches[index] = Object.create(null);
  4147. for (var p in matchset) {
  4148. try {
  4149. p = self._makeAbs(p);
  4150. var real = rp.realpathSync(p, self.realpathCache);
  4151. set[real] = true;
  4152. } catch (er) {
  4153. if (er.syscall === 'stat')
  4154. set[self._makeAbs(p)] = true;
  4155. else
  4156. throw er
  4157. }
  4158. }
  4159. });
  4160. }
  4161. common.finish(this);
  4162. };
  4163. GlobSync.prototype._process = function (pattern, index, inGlobStar) {
  4164. assert.ok(this instanceof GlobSync);
  4165. // Get the first [n] parts of pattern that are all strings.
  4166. var n = 0;
  4167. while (typeof pattern[n] === 'string') {
  4168. n ++;
  4169. }
  4170. // now n is the index of the first one that is *not* a string.
  4171. // See if there's anything else
  4172. var prefix;
  4173. switch (n) {
  4174. // if not, then this is rather simple
  4175. case pattern.length:
  4176. this._processSimple(pattern.join('/'), index);
  4177. return
  4178. case 0:
  4179. // pattern *starts* with some non-trivial item.
  4180. // going to readdir(cwd), but not include the prefix in matches.
  4181. prefix = null;
  4182. break
  4183. default:
  4184. // pattern has some string bits in the front.
  4185. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  4186. // or 'relative' like '../baz'
  4187. prefix = pattern.slice(0, n).join('/');
  4188. break
  4189. }
  4190. var remain = pattern.slice(n);
  4191. // get the list of entries.
  4192. var read;
  4193. if (prefix === null)
  4194. read = '.';
  4195. else if (isAbsolute(prefix) ||
  4196. isAbsolute(pattern.map(function (p) {
  4197. return typeof p === 'string' ? p : '[*]'
  4198. }).join('/'))) {
  4199. if (!prefix || !isAbsolute(prefix))
  4200. prefix = '/' + prefix;
  4201. read = prefix;
  4202. } else
  4203. read = prefix;
  4204. var abs = this._makeAbs(read);
  4205. //if ignored, skip processing
  4206. if (childrenIgnored(this, read))
  4207. return
  4208. var isGlobStar = remain[0] === minimatch.GLOBSTAR;
  4209. if (isGlobStar)
  4210. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar);
  4211. else
  4212. this._processReaddir(prefix, read, abs, remain, index, inGlobStar);
  4213. };
  4214. GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
  4215. var entries = this._readdir(abs, inGlobStar);
  4216. // if the abs isn't a dir, then nothing can match!
  4217. if (!entries)
  4218. return
  4219. // It will only match dot entries if it starts with a dot, or if
  4220. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  4221. var pn = remain[0];
  4222. var negate = !!this.minimatch.negate;
  4223. var rawGlob = pn._glob;
  4224. var dotOk = this.dot || rawGlob.charAt(0) === '.';
  4225. var matchedEntries = [];
  4226. for (var i = 0; i < entries.length; i++) {
  4227. var e = entries[i];
  4228. if (e.charAt(0) !== '.' || dotOk) {
  4229. var m;
  4230. if (negate && !prefix) {
  4231. m = !e.match(pn);
  4232. } else {
  4233. m = e.match(pn);
  4234. }
  4235. if (m)
  4236. matchedEntries.push(e);
  4237. }
  4238. }
  4239. var len = matchedEntries.length;
  4240. // If there are no matched entries, then nothing matches.
  4241. if (len === 0)
  4242. return
  4243. // if this is the last remaining pattern bit, then no need for
  4244. // an additional stat *unless* the user has specified mark or
  4245. // stat explicitly. We know they exist, since readdir returned
  4246. // them.
  4247. if (remain.length === 1 && !this.mark && !this.stat) {
  4248. if (!this.matches[index])
  4249. this.matches[index] = Object.create(null);
  4250. for (var i = 0; i < len; i ++) {
  4251. var e = matchedEntries[i];
  4252. if (prefix) {
  4253. if (prefix.slice(-1) !== '/')
  4254. e = prefix + '/' + e;
  4255. else
  4256. e = prefix + e;
  4257. }
  4258. if (e.charAt(0) === '/' && !this.nomount) {
  4259. e = path.join(this.root, e);
  4260. }
  4261. this._emitMatch(index, e);
  4262. }
  4263. // This was the last one, and no stats were needed
  4264. return
  4265. }
  4266. // now test all matched entries as stand-ins for that part
  4267. // of the pattern.
  4268. remain.shift();
  4269. for (var i = 0; i < len; i ++) {
  4270. var e = matchedEntries[i];
  4271. var newPattern;
  4272. if (prefix)
  4273. newPattern = [prefix, e];
  4274. else
  4275. newPattern = [e];
  4276. this._process(newPattern.concat(remain), index, inGlobStar);
  4277. }
  4278. };
  4279. GlobSync.prototype._emitMatch = function (index, e) {
  4280. if (isIgnored(this, e))
  4281. return
  4282. var abs = this._makeAbs(e);
  4283. if (this.mark)
  4284. e = this._mark(e);
  4285. if (this.absolute) {
  4286. e = abs;
  4287. }
  4288. if (this.matches[index][e])
  4289. return
  4290. if (this.nodir) {
  4291. var c = this.cache[abs];
  4292. if (c === 'DIR' || Array.isArray(c))
  4293. return
  4294. }
  4295. this.matches[index][e] = true;
  4296. if (this.stat)
  4297. this._stat(e);
  4298. };
  4299. GlobSync.prototype._readdirInGlobStar = function (abs) {
  4300. // follow all symlinked directories forever
  4301. // just proceed as if this is a non-globstar situation
  4302. if (this.follow)
  4303. return this._readdir(abs, false)
  4304. var entries;
  4305. var lstat;
  4306. try {
  4307. lstat = this.fs.lstatSync(abs);
  4308. } catch (er) {
  4309. if (er.code === 'ENOENT') {
  4310. // lstat failed, doesn't exist
  4311. return null
  4312. }
  4313. }
  4314. var isSym = lstat && lstat.isSymbolicLink();
  4315. this.symlinks[abs] = isSym;
  4316. // If it's not a symlink or a dir, then it's definitely a regular file.
  4317. // don't bother doing a readdir in that case.
  4318. if (!isSym && lstat && !lstat.isDirectory())
  4319. this.cache[abs] = 'FILE';
  4320. else
  4321. entries = this._readdir(abs, false);
  4322. return entries
  4323. };
  4324. GlobSync.prototype._readdir = function (abs, inGlobStar) {
  4325. if (inGlobStar && !ownProp(this.symlinks, abs))
  4326. return this._readdirInGlobStar(abs)
  4327. if (ownProp(this.cache, abs)) {
  4328. var c = this.cache[abs];
  4329. if (!c || c === 'FILE')
  4330. return null
  4331. if (Array.isArray(c))
  4332. return c
  4333. }
  4334. try {
  4335. return this._readdirEntries(abs, this.fs.readdirSync(abs))
  4336. } catch (er) {
  4337. this._readdirError(abs, er);
  4338. return null
  4339. }
  4340. };
  4341. GlobSync.prototype._readdirEntries = function (abs, entries) {
  4342. // if we haven't asked to stat everything, then just
  4343. // assume that everything in there exists, so we can avoid
  4344. // having to stat it a second time.
  4345. if (!this.mark && !this.stat) {
  4346. for (var i = 0; i < entries.length; i ++) {
  4347. var e = entries[i];
  4348. if (abs === '/')
  4349. e = abs + e;
  4350. else
  4351. e = abs + '/' + e;
  4352. this.cache[e] = true;
  4353. }
  4354. }
  4355. this.cache[abs] = entries;
  4356. // mark and cache dir-ness
  4357. return entries
  4358. };
  4359. GlobSync.prototype._readdirError = function (f, er) {
  4360. // handle errors, and cache the information
  4361. switch (er.code) {
  4362. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  4363. case 'ENOTDIR': // totally normal. means it *does* exist.
  4364. var abs = this._makeAbs(f);
  4365. this.cache[abs] = 'FILE';
  4366. if (abs === this.cwdAbs) {
  4367. var error = new Error(er.code + ' invalid cwd ' + this.cwd);
  4368. error.path = this.cwd;
  4369. error.code = er.code;
  4370. throw error
  4371. }
  4372. break
  4373. case 'ENOENT': // not terribly unusual
  4374. case 'ELOOP':
  4375. case 'ENAMETOOLONG':
  4376. case 'UNKNOWN':
  4377. this.cache[this._makeAbs(f)] = false;
  4378. break
  4379. default: // some unusual error. Treat as failure.
  4380. this.cache[this._makeAbs(f)] = false;
  4381. if (this.strict)
  4382. throw er
  4383. if (!this.silent)
  4384. console.error('glob error', er);
  4385. break
  4386. }
  4387. };
  4388. GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
  4389. var entries = this._readdir(abs, inGlobStar);
  4390. // no entries means not a dir, so it can never have matches
  4391. // foo.txt/** doesn't match foo.txt
  4392. if (!entries)
  4393. return
  4394. // test without the globstar, and with every child both below
  4395. // and replacing the globstar.
  4396. var remainWithoutGlobStar = remain.slice(1);
  4397. var gspref = prefix ? [ prefix ] : [];
  4398. var noGlobStar = gspref.concat(remainWithoutGlobStar);
  4399. // the noGlobStar pattern exits the inGlobStar state
  4400. this._process(noGlobStar, index, false);
  4401. var len = entries.length;
  4402. var isSym = this.symlinks[abs];
  4403. // If it's a symlink, and we're in a globstar, then stop
  4404. if (isSym && inGlobStar)
  4405. return
  4406. for (var i = 0; i < len; i++) {
  4407. var e = entries[i];
  4408. if (e.charAt(0) === '.' && !this.dot)
  4409. continue
  4410. // these two cases enter the inGlobStar state
  4411. var instead = gspref.concat(entries[i], remainWithoutGlobStar);
  4412. this._process(instead, index, true);
  4413. var below = gspref.concat(entries[i], remain);
  4414. this._process(below, index, true);
  4415. }
  4416. };
  4417. GlobSync.prototype._processSimple = function (prefix, index) {
  4418. // XXX review this. Shouldn't it be doing the mounting etc
  4419. // before doing stat? kinda weird?
  4420. var exists = this._stat(prefix);
  4421. if (!this.matches[index])
  4422. this.matches[index] = Object.create(null);
  4423. // If it doesn't exist, then just mark the lack of results
  4424. if (!exists)
  4425. return
  4426. if (prefix && isAbsolute(prefix) && !this.nomount) {
  4427. var trail = /[\/\\]$/.test(prefix);
  4428. if (prefix.charAt(0) === '/') {
  4429. prefix = path.join(this.root, prefix);
  4430. } else {
  4431. prefix = path.resolve(this.root, prefix);
  4432. if (trail)
  4433. prefix += '/';
  4434. }
  4435. }
  4436. if (process.platform === 'win32')
  4437. prefix = prefix.replace(/\\/g, '/');
  4438. // Mark this as a match
  4439. this._emitMatch(index, prefix);
  4440. };
  4441. // Returns either 'DIR', 'FILE', or false
  4442. GlobSync.prototype._stat = function (f) {
  4443. var abs = this._makeAbs(f);
  4444. var needDir = f.slice(-1) === '/';
  4445. if (f.length > this.maxLength)
  4446. return false
  4447. if (!this.stat && ownProp(this.cache, abs)) {
  4448. var c = this.cache[abs];
  4449. if (Array.isArray(c))
  4450. c = 'DIR';
  4451. // It exists, but maybe not how we need it
  4452. if (!needDir || c === 'DIR')
  4453. return c
  4454. if (needDir && c === 'FILE')
  4455. return false
  4456. // otherwise we have to stat, because maybe c=true
  4457. // if we know it exists, but not what it is.
  4458. }
  4459. var stat = this.statCache[abs];
  4460. if (!stat) {
  4461. var lstat;
  4462. try {
  4463. lstat = this.fs.lstatSync(abs);
  4464. } catch (er) {
  4465. if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
  4466. this.statCache[abs] = false;
  4467. return false
  4468. }
  4469. }
  4470. if (lstat && lstat.isSymbolicLink()) {
  4471. try {
  4472. stat = this.fs.statSync(abs);
  4473. } catch (er) {
  4474. stat = lstat;
  4475. }
  4476. } else {
  4477. stat = lstat;
  4478. }
  4479. }
  4480. this.statCache[abs] = stat;
  4481. var c = true;
  4482. if (stat)
  4483. c = stat.isDirectory() ? 'DIR' : 'FILE';
  4484. this.cache[abs] = this.cache[abs] || c;
  4485. if (needDir && c === 'FILE')
  4486. return false
  4487. return c
  4488. };
  4489. GlobSync.prototype._mark = function (p) {
  4490. return common.mark(this, p)
  4491. };
  4492. GlobSync.prototype._makeAbs = function (f) {
  4493. return common.makeAbs(this, f)
  4494. };
  4495. return sync$9;
  4496. }
  4497. // Returns a wrapper function that returns a wrapped callback
  4498. // The wrapper function should do some stuff, and return a
  4499. // presumably different callback function.
  4500. // This makes sure that own properties are retained, so that
  4501. // decorations and such are not lost along the way.
  4502. var wrappy_1 = wrappy$2;
  4503. function wrappy$2 (fn, cb) {
  4504. if (fn && cb) return wrappy$2(fn)(cb)
  4505. if (typeof fn !== 'function')
  4506. throw new TypeError('need wrapper function')
  4507. Object.keys(fn).forEach(function (k) {
  4508. wrapper[k] = fn[k];
  4509. });
  4510. return wrapper
  4511. function wrapper() {
  4512. var args = new Array(arguments.length);
  4513. for (var i = 0; i < args.length; i++) {
  4514. args[i] = arguments[i];
  4515. }
  4516. var ret = fn.apply(this, args);
  4517. var cb = args[args.length-1];
  4518. if (typeof ret === 'function' && ret !== cb) {
  4519. Object.keys(cb).forEach(function (k) {
  4520. ret[k] = cb[k];
  4521. });
  4522. }
  4523. return ret
  4524. }
  4525. }
  4526. var once$2 = {exports: {}};
  4527. var wrappy$1 = wrappy_1;
  4528. once$2.exports = wrappy$1(once$1);
  4529. once$2.exports.strict = wrappy$1(onceStrict);
  4530. once$1.proto = once$1(function () {
  4531. Object.defineProperty(Function.prototype, 'once', {
  4532. value: function () {
  4533. return once$1(this)
  4534. },
  4535. configurable: true
  4536. });
  4537. Object.defineProperty(Function.prototype, 'onceStrict', {
  4538. value: function () {
  4539. return onceStrict(this)
  4540. },
  4541. configurable: true
  4542. });
  4543. });
  4544. function once$1 (fn) {
  4545. var f = function () {
  4546. if (f.called) return f.value
  4547. f.called = true;
  4548. return f.value = fn.apply(this, arguments)
  4549. };
  4550. f.called = false;
  4551. return f
  4552. }
  4553. function onceStrict (fn) {
  4554. var f = function () {
  4555. if (f.called)
  4556. throw new Error(f.onceError)
  4557. f.called = true;
  4558. return f.value = fn.apply(this, arguments)
  4559. };
  4560. var name = fn.name || 'Function wrapped with `once`';
  4561. f.onceError = name + " shouldn't be called more than once";
  4562. f.called = false;
  4563. return f
  4564. }
  4565. var onceExports = once$2.exports;
  4566. var wrappy = wrappy_1;
  4567. var reqs = Object.create(null);
  4568. var once = onceExports;
  4569. var inflight_1 = wrappy(inflight);
  4570. function inflight (key, cb) {
  4571. if (reqs[key]) {
  4572. reqs[key].push(cb);
  4573. return null
  4574. } else {
  4575. reqs[key] = [cb];
  4576. return makeres(key)
  4577. }
  4578. }
  4579. function makeres (key) {
  4580. return once(function RES () {
  4581. var cbs = reqs[key];
  4582. var len = cbs.length;
  4583. var args = slice$1(arguments);
  4584. // XXX It's somewhat ambiguous whether a new callback added in this
  4585. // pass should be queued for later execution if something in the
  4586. // list of callbacks throws, or if it should just be discarded.
  4587. // However, it's such an edge case that it hardly matters, and either
  4588. // choice is likely as surprising as the other.
  4589. // As it happens, we do go ahead and schedule it for later execution.
  4590. try {
  4591. for (var i = 0; i < len; i++) {
  4592. cbs[i].apply(null, args);
  4593. }
  4594. } finally {
  4595. if (cbs.length > len) {
  4596. // added more in the interim.
  4597. // de-zalgo, just in case, but don't call again.
  4598. cbs.splice(0, len);
  4599. process.nextTick(function () {
  4600. RES.apply(null, args);
  4601. });
  4602. } else {
  4603. delete reqs[key];
  4604. }
  4605. }
  4606. })
  4607. }
  4608. function slice$1 (args) {
  4609. var length = args.length;
  4610. var array = [];
  4611. for (var i = 0; i < length; i++) array[i] = args[i];
  4612. return array
  4613. }
  4614. var glob_1;
  4615. var hasRequiredGlob;
  4616. function requireGlob () {
  4617. if (hasRequiredGlob) return glob_1;
  4618. hasRequiredGlob = 1;
  4619. // Approach:
  4620. //
  4621. // 1. Get the minimatch set
  4622. // 2. For each pattern in the set, PROCESS(pattern, false)
  4623. // 3. Store matches per-set, then uniq them
  4624. //
  4625. // PROCESS(pattern, inGlobStar)
  4626. // Get the first [n] items from pattern that are all strings
  4627. // Join these together. This is PREFIX.
  4628. // If there is no more remaining, then stat(PREFIX) and
  4629. // add to matches if it succeeds. END.
  4630. //
  4631. // If inGlobStar and PREFIX is symlink and points to dir
  4632. // set ENTRIES = []
  4633. // else readdir(PREFIX) as ENTRIES
  4634. // If fail, END
  4635. //
  4636. // with ENTRIES
  4637. // If pattern[n] is GLOBSTAR
  4638. // // handle the case where the globstar match is empty
  4639. // // by pruning it out, and testing the resulting pattern
  4640. // PROCESS(pattern[0..n] + pattern[n+1 .. $], false)
  4641. // // handle other cases.
  4642. // for ENTRY in ENTRIES (not dotfiles)
  4643. // // attach globstar + tail onto the entry
  4644. // // Mark that this entry is a globstar match
  4645. // PROCESS(pattern[0..n] + ENTRY + pattern[n .. $], true)
  4646. //
  4647. // else // not globstar
  4648. // for ENTRY in ENTRIES (not dotfiles, unless pattern[n] is dot)
  4649. // Test ENTRY against pattern[n]
  4650. // If fails, continue
  4651. // If passes, PROCESS(pattern[0..n] + item + pattern[n+1 .. $])
  4652. //
  4653. // Caveat:
  4654. // Cache all stats and readdirs results to minimize syscall. Since all
  4655. // we ever care about is existence and directory-ness, we can just keep
  4656. // `true` for files, and [children,...] for directories, or `false` for
  4657. // things that don't exist.
  4658. glob_1 = glob;
  4659. var rp = fs_realpath;
  4660. var minimatch = minimatch_1;
  4661. var inherits = inheritsExports;
  4662. var EE = require$$0$5.EventEmitter;
  4663. var path = require$$0$4;
  4664. var assert = require$$5;
  4665. var isAbsolute = require$$0$4.isAbsolute;
  4666. var globSync = requireSync();
  4667. var common = common$c;
  4668. var setopts = common.setopts;
  4669. var ownProp = common.ownProp;
  4670. var inflight = inflight_1;
  4671. var childrenIgnored = common.childrenIgnored;
  4672. var isIgnored = common.isIgnored;
  4673. var once = onceExports;
  4674. function glob (pattern, options, cb) {
  4675. if (typeof options === 'function') cb = options, options = {};
  4676. if (!options) options = {};
  4677. if (options.sync) {
  4678. if (cb)
  4679. throw new TypeError('callback provided to sync glob')
  4680. return globSync(pattern, options)
  4681. }
  4682. return new Glob(pattern, options, cb)
  4683. }
  4684. glob.sync = globSync;
  4685. var GlobSync = glob.GlobSync = globSync.GlobSync;
  4686. // old api surface
  4687. glob.glob = glob;
  4688. function extend (origin, add) {
  4689. if (add === null || typeof add !== 'object') {
  4690. return origin
  4691. }
  4692. var keys = Object.keys(add);
  4693. var i = keys.length;
  4694. while (i--) {
  4695. origin[keys[i]] = add[keys[i]];
  4696. }
  4697. return origin
  4698. }
  4699. glob.hasMagic = function (pattern, options_) {
  4700. var options = extend({}, options_);
  4701. options.noprocess = true;
  4702. var g = new Glob(pattern, options);
  4703. var set = g.minimatch.set;
  4704. if (!pattern)
  4705. return false
  4706. if (set.length > 1)
  4707. return true
  4708. for (var j = 0; j < set[0].length; j++) {
  4709. if (typeof set[0][j] !== 'string')
  4710. return true
  4711. }
  4712. return false
  4713. };
  4714. glob.Glob = Glob;
  4715. inherits(Glob, EE);
  4716. function Glob (pattern, options, cb) {
  4717. if (typeof options === 'function') {
  4718. cb = options;
  4719. options = null;
  4720. }
  4721. if (options && options.sync) {
  4722. if (cb)
  4723. throw new TypeError('callback provided to sync glob')
  4724. return new GlobSync(pattern, options)
  4725. }
  4726. if (!(this instanceof Glob))
  4727. return new Glob(pattern, options, cb)
  4728. setopts(this, pattern, options);
  4729. this._didRealPath = false;
  4730. // process each pattern in the minimatch set
  4731. var n = this.minimatch.set.length;
  4732. // The matches are stored as {<filename>: true,...} so that
  4733. // duplicates are automagically pruned.
  4734. // Later, we do an Object.keys() on these.
  4735. // Keep them as a list so we can fill in when nonull is set.
  4736. this.matches = new Array(n);
  4737. if (typeof cb === 'function') {
  4738. cb = once(cb);
  4739. this.on('error', cb);
  4740. this.on('end', function (matches) {
  4741. cb(null, matches);
  4742. });
  4743. }
  4744. var self = this;
  4745. this._processing = 0;
  4746. this._emitQueue = [];
  4747. this._processQueue = [];
  4748. this.paused = false;
  4749. if (this.noprocess)
  4750. return this
  4751. if (n === 0)
  4752. return done()
  4753. var sync = true;
  4754. for (var i = 0; i < n; i ++) {
  4755. this._process(this.minimatch.set[i], i, false, done);
  4756. }
  4757. sync = false;
  4758. function done () {
  4759. --self._processing;
  4760. if (self._processing <= 0) {
  4761. if (sync) {
  4762. process.nextTick(function () {
  4763. self._finish();
  4764. });
  4765. } else {
  4766. self._finish();
  4767. }
  4768. }
  4769. }
  4770. }
  4771. Glob.prototype._finish = function () {
  4772. assert(this instanceof Glob);
  4773. if (this.aborted)
  4774. return
  4775. if (this.realpath && !this._didRealpath)
  4776. return this._realpath()
  4777. common.finish(this);
  4778. this.emit('end', this.found);
  4779. };
  4780. Glob.prototype._realpath = function () {
  4781. if (this._didRealpath)
  4782. return
  4783. this._didRealpath = true;
  4784. var n = this.matches.length;
  4785. if (n === 0)
  4786. return this._finish()
  4787. var self = this;
  4788. for (var i = 0; i < this.matches.length; i++)
  4789. this._realpathSet(i, next);
  4790. function next () {
  4791. if (--n === 0)
  4792. self._finish();
  4793. }
  4794. };
  4795. Glob.prototype._realpathSet = function (index, cb) {
  4796. var matchset = this.matches[index];
  4797. if (!matchset)
  4798. return cb()
  4799. var found = Object.keys(matchset);
  4800. var self = this;
  4801. var n = found.length;
  4802. if (n === 0)
  4803. return cb()
  4804. var set = this.matches[index] = Object.create(null);
  4805. found.forEach(function (p, i) {
  4806. // If there's a problem with the stat, then it means that
  4807. // one or more of the links in the realpath couldn't be
  4808. // resolved. just return the abs value in that case.
  4809. p = self._makeAbs(p);
  4810. rp.realpath(p, self.realpathCache, function (er, real) {
  4811. if (!er)
  4812. set[real] = true;
  4813. else if (er.syscall === 'stat')
  4814. set[p] = true;
  4815. else
  4816. self.emit('error', er); // srsly wtf right here
  4817. if (--n === 0) {
  4818. self.matches[index] = set;
  4819. cb();
  4820. }
  4821. });
  4822. });
  4823. };
  4824. Glob.prototype._mark = function (p) {
  4825. return common.mark(this, p)
  4826. };
  4827. Glob.prototype._makeAbs = function (f) {
  4828. return common.makeAbs(this, f)
  4829. };
  4830. Glob.prototype.abort = function () {
  4831. this.aborted = true;
  4832. this.emit('abort');
  4833. };
  4834. Glob.prototype.pause = function () {
  4835. if (!this.paused) {
  4836. this.paused = true;
  4837. this.emit('pause');
  4838. }
  4839. };
  4840. Glob.prototype.resume = function () {
  4841. if (this.paused) {
  4842. this.emit('resume');
  4843. this.paused = false;
  4844. if (this._emitQueue.length) {
  4845. var eq = this._emitQueue.slice(0);
  4846. this._emitQueue.length = 0;
  4847. for (var i = 0; i < eq.length; i ++) {
  4848. var e = eq[i];
  4849. this._emitMatch(e[0], e[1]);
  4850. }
  4851. }
  4852. if (this._processQueue.length) {
  4853. var pq = this._processQueue.slice(0);
  4854. this._processQueue.length = 0;
  4855. for (var i = 0; i < pq.length; i ++) {
  4856. var p = pq[i];
  4857. this._processing--;
  4858. this._process(p[0], p[1], p[2], p[3]);
  4859. }
  4860. }
  4861. }
  4862. };
  4863. Glob.prototype._process = function (pattern, index, inGlobStar, cb) {
  4864. assert(this instanceof Glob);
  4865. assert(typeof cb === 'function');
  4866. if (this.aborted)
  4867. return
  4868. this._processing++;
  4869. if (this.paused) {
  4870. this._processQueue.push([pattern, index, inGlobStar, cb]);
  4871. return
  4872. }
  4873. //console.error('PROCESS %d', this._processing, pattern)
  4874. // Get the first [n] parts of pattern that are all strings.
  4875. var n = 0;
  4876. while (typeof pattern[n] === 'string') {
  4877. n ++;
  4878. }
  4879. // now n is the index of the first one that is *not* a string.
  4880. // see if there's anything else
  4881. var prefix;
  4882. switch (n) {
  4883. // if not, then this is rather simple
  4884. case pattern.length:
  4885. this._processSimple(pattern.join('/'), index, cb);
  4886. return
  4887. case 0:
  4888. // pattern *starts* with some non-trivial item.
  4889. // going to readdir(cwd), but not include the prefix in matches.
  4890. prefix = null;
  4891. break
  4892. default:
  4893. // pattern has some string bits in the front.
  4894. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  4895. // or 'relative' like '../baz'
  4896. prefix = pattern.slice(0, n).join('/');
  4897. break
  4898. }
  4899. var remain = pattern.slice(n);
  4900. // get the list of entries.
  4901. var read;
  4902. if (prefix === null)
  4903. read = '.';
  4904. else if (isAbsolute(prefix) ||
  4905. isAbsolute(pattern.map(function (p) {
  4906. return typeof p === 'string' ? p : '[*]'
  4907. }).join('/'))) {
  4908. if (!prefix || !isAbsolute(prefix))
  4909. prefix = '/' + prefix;
  4910. read = prefix;
  4911. } else
  4912. read = prefix;
  4913. var abs = this._makeAbs(read);
  4914. //if ignored, skip _processing
  4915. if (childrenIgnored(this, read))
  4916. return cb()
  4917. var isGlobStar = remain[0] === minimatch.GLOBSTAR;
  4918. if (isGlobStar)
  4919. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar, cb);
  4920. else
  4921. this._processReaddir(prefix, read, abs, remain, index, inGlobStar, cb);
  4922. };
  4923. Glob.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar, cb) {
  4924. var self = this;
  4925. this._readdir(abs, inGlobStar, function (er, entries) {
  4926. return self._processReaddir2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
  4927. });
  4928. };
  4929. Glob.prototype._processReaddir2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
  4930. // if the abs isn't a dir, then nothing can match!
  4931. if (!entries)
  4932. return cb()
  4933. // It will only match dot entries if it starts with a dot, or if
  4934. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  4935. var pn = remain[0];
  4936. var negate = !!this.minimatch.negate;
  4937. var rawGlob = pn._glob;
  4938. var dotOk = this.dot || rawGlob.charAt(0) === '.';
  4939. var matchedEntries = [];
  4940. for (var i = 0; i < entries.length; i++) {
  4941. var e = entries[i];
  4942. if (e.charAt(0) !== '.' || dotOk) {
  4943. var m;
  4944. if (negate && !prefix) {
  4945. m = !e.match(pn);
  4946. } else {
  4947. m = e.match(pn);
  4948. }
  4949. if (m)
  4950. matchedEntries.push(e);
  4951. }
  4952. }
  4953. //console.error('prd2', prefix, entries, remain[0]._glob, matchedEntries)
  4954. var len = matchedEntries.length;
  4955. // If there are no matched entries, then nothing matches.
  4956. if (len === 0)
  4957. return cb()
  4958. // if this is the last remaining pattern bit, then no need for
  4959. // an additional stat *unless* the user has specified mark or
  4960. // stat explicitly. We know they exist, since readdir returned
  4961. // them.
  4962. if (remain.length === 1 && !this.mark && !this.stat) {
  4963. if (!this.matches[index])
  4964. this.matches[index] = Object.create(null);
  4965. for (var i = 0; i < len; i ++) {
  4966. var e = matchedEntries[i];
  4967. if (prefix) {
  4968. if (prefix !== '/')
  4969. e = prefix + '/' + e;
  4970. else
  4971. e = prefix + e;
  4972. }
  4973. if (e.charAt(0) === '/' && !this.nomount) {
  4974. e = path.join(this.root, e);
  4975. }
  4976. this._emitMatch(index, e);
  4977. }
  4978. // This was the last one, and no stats were needed
  4979. return cb()
  4980. }
  4981. // now test all matched entries as stand-ins for that part
  4982. // of the pattern.
  4983. remain.shift();
  4984. for (var i = 0; i < len; i ++) {
  4985. var e = matchedEntries[i];
  4986. if (prefix) {
  4987. if (prefix !== '/')
  4988. e = prefix + '/' + e;
  4989. else
  4990. e = prefix + e;
  4991. }
  4992. this._process([e].concat(remain), index, inGlobStar, cb);
  4993. }
  4994. cb();
  4995. };
  4996. Glob.prototype._emitMatch = function (index, e) {
  4997. if (this.aborted)
  4998. return
  4999. if (isIgnored(this, e))
  5000. return
  5001. if (this.paused) {
  5002. this._emitQueue.push([index, e]);
  5003. return
  5004. }
  5005. var abs = isAbsolute(e) ? e : this._makeAbs(e);
  5006. if (this.mark)
  5007. e = this._mark(e);
  5008. if (this.absolute)
  5009. e = abs;
  5010. if (this.matches[index][e])
  5011. return
  5012. if (this.nodir) {
  5013. var c = this.cache[abs];
  5014. if (c === 'DIR' || Array.isArray(c))
  5015. return
  5016. }
  5017. this.matches[index][e] = true;
  5018. var st = this.statCache[abs];
  5019. if (st)
  5020. this.emit('stat', e, st);
  5021. this.emit('match', e);
  5022. };
  5023. Glob.prototype._readdirInGlobStar = function (abs, cb) {
  5024. if (this.aborted)
  5025. return
  5026. // follow all symlinked directories forever
  5027. // just proceed as if this is a non-globstar situation
  5028. if (this.follow)
  5029. return this._readdir(abs, false, cb)
  5030. var lstatkey = 'lstat\0' + abs;
  5031. var self = this;
  5032. var lstatcb = inflight(lstatkey, lstatcb_);
  5033. if (lstatcb)
  5034. self.fs.lstat(abs, lstatcb);
  5035. function lstatcb_ (er, lstat) {
  5036. if (er && er.code === 'ENOENT')
  5037. return cb()
  5038. var isSym = lstat && lstat.isSymbolicLink();
  5039. self.symlinks[abs] = isSym;
  5040. // If it's not a symlink or a dir, then it's definitely a regular file.
  5041. // don't bother doing a readdir in that case.
  5042. if (!isSym && lstat && !lstat.isDirectory()) {
  5043. self.cache[abs] = 'FILE';
  5044. cb();
  5045. } else
  5046. self._readdir(abs, false, cb);
  5047. }
  5048. };
  5049. Glob.prototype._readdir = function (abs, inGlobStar, cb) {
  5050. if (this.aborted)
  5051. return
  5052. cb = inflight('readdir\0'+abs+'\0'+inGlobStar, cb);
  5053. if (!cb)
  5054. return
  5055. //console.error('RD %j %j', +inGlobStar, abs)
  5056. if (inGlobStar && !ownProp(this.symlinks, abs))
  5057. return this._readdirInGlobStar(abs, cb)
  5058. if (ownProp(this.cache, abs)) {
  5059. var c = this.cache[abs];
  5060. if (!c || c === 'FILE')
  5061. return cb()
  5062. if (Array.isArray(c))
  5063. return cb(null, c)
  5064. }
  5065. var self = this;
  5066. self.fs.readdir(abs, readdirCb(this, abs, cb));
  5067. };
  5068. function readdirCb (self, abs, cb) {
  5069. return function (er, entries) {
  5070. if (er)
  5071. self._readdirError(abs, er, cb);
  5072. else
  5073. self._readdirEntries(abs, entries, cb);
  5074. }
  5075. }
  5076. Glob.prototype._readdirEntries = function (abs, entries, cb) {
  5077. if (this.aborted)
  5078. return
  5079. // if we haven't asked to stat everything, then just
  5080. // assume that everything in there exists, so we can avoid
  5081. // having to stat it a second time.
  5082. if (!this.mark && !this.stat) {
  5083. for (var i = 0; i < entries.length; i ++) {
  5084. var e = entries[i];
  5085. if (abs === '/')
  5086. e = abs + e;
  5087. else
  5088. e = abs + '/' + e;
  5089. this.cache[e] = true;
  5090. }
  5091. }
  5092. this.cache[abs] = entries;
  5093. return cb(null, entries)
  5094. };
  5095. Glob.prototype._readdirError = function (f, er, cb) {
  5096. if (this.aborted)
  5097. return
  5098. // handle errors, and cache the information
  5099. switch (er.code) {
  5100. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  5101. case 'ENOTDIR': // totally normal. means it *does* exist.
  5102. var abs = this._makeAbs(f);
  5103. this.cache[abs] = 'FILE';
  5104. if (abs === this.cwdAbs) {
  5105. var error = new Error(er.code + ' invalid cwd ' + this.cwd);
  5106. error.path = this.cwd;
  5107. error.code = er.code;
  5108. this.emit('error', error);
  5109. this.abort();
  5110. }
  5111. break
  5112. case 'ENOENT': // not terribly unusual
  5113. case 'ELOOP':
  5114. case 'ENAMETOOLONG':
  5115. case 'UNKNOWN':
  5116. this.cache[this._makeAbs(f)] = false;
  5117. break
  5118. default: // some unusual error. Treat as failure.
  5119. this.cache[this._makeAbs(f)] = false;
  5120. if (this.strict) {
  5121. this.emit('error', er);
  5122. // If the error is handled, then we abort
  5123. // if not, we threw out of here
  5124. this.abort();
  5125. }
  5126. if (!this.silent)
  5127. console.error('glob error', er);
  5128. break
  5129. }
  5130. return cb()
  5131. };
  5132. Glob.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar, cb) {
  5133. var self = this;
  5134. this._readdir(abs, inGlobStar, function (er, entries) {
  5135. self._processGlobStar2(prefix, read, abs, remain, index, inGlobStar, entries, cb);
  5136. });
  5137. };
  5138. Glob.prototype._processGlobStar2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
  5139. //console.error('pgs2', prefix, remain[0], entries)
  5140. // no entries means not a dir, so it can never have matches
  5141. // foo.txt/** doesn't match foo.txt
  5142. if (!entries)
  5143. return cb()
  5144. // test without the globstar, and with every child both below
  5145. // and replacing the globstar.
  5146. var remainWithoutGlobStar = remain.slice(1);
  5147. var gspref = prefix ? [ prefix ] : [];
  5148. var noGlobStar = gspref.concat(remainWithoutGlobStar);
  5149. // the noGlobStar pattern exits the inGlobStar state
  5150. this._process(noGlobStar, index, false, cb);
  5151. var isSym = this.symlinks[abs];
  5152. var len = entries.length;
  5153. // If it's a symlink, and we're in a globstar, then stop
  5154. if (isSym && inGlobStar)
  5155. return cb()
  5156. for (var i = 0; i < len; i++) {
  5157. var e = entries[i];
  5158. if (e.charAt(0) === '.' && !this.dot)
  5159. continue
  5160. // these two cases enter the inGlobStar state
  5161. var instead = gspref.concat(entries[i], remainWithoutGlobStar);
  5162. this._process(instead, index, true, cb);
  5163. var below = gspref.concat(entries[i], remain);
  5164. this._process(below, index, true, cb);
  5165. }
  5166. cb();
  5167. };
  5168. Glob.prototype._processSimple = function (prefix, index, cb) {
  5169. // XXX review this. Shouldn't it be doing the mounting etc
  5170. // before doing stat? kinda weird?
  5171. var self = this;
  5172. this._stat(prefix, function (er, exists) {
  5173. self._processSimple2(prefix, index, er, exists, cb);
  5174. });
  5175. };
  5176. Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
  5177. //console.error('ps2', prefix, exists)
  5178. if (!this.matches[index])
  5179. this.matches[index] = Object.create(null);
  5180. // If it doesn't exist, then just mark the lack of results
  5181. if (!exists)
  5182. return cb()
  5183. if (prefix && isAbsolute(prefix) && !this.nomount) {
  5184. var trail = /[\/\\]$/.test(prefix);
  5185. if (prefix.charAt(0) === '/') {
  5186. prefix = path.join(this.root, prefix);
  5187. } else {
  5188. prefix = path.resolve(this.root, prefix);
  5189. if (trail)
  5190. prefix += '/';
  5191. }
  5192. }
  5193. if (process.platform === 'win32')
  5194. prefix = prefix.replace(/\\/g, '/');
  5195. // Mark this as a match
  5196. this._emitMatch(index, prefix);
  5197. cb();
  5198. };
  5199. // Returns either 'DIR', 'FILE', or false
  5200. Glob.prototype._stat = function (f, cb) {
  5201. var abs = this._makeAbs(f);
  5202. var needDir = f.slice(-1) === '/';
  5203. if (f.length > this.maxLength)
  5204. return cb()
  5205. if (!this.stat && ownProp(this.cache, abs)) {
  5206. var c = this.cache[abs];
  5207. if (Array.isArray(c))
  5208. c = 'DIR';
  5209. // It exists, but maybe not how we need it
  5210. if (!needDir || c === 'DIR')
  5211. return cb(null, c)
  5212. if (needDir && c === 'FILE')
  5213. return cb()
  5214. // otherwise we have to stat, because maybe c=true
  5215. // if we know it exists, but not what it is.
  5216. }
  5217. var stat = this.statCache[abs];
  5218. if (stat !== undefined) {
  5219. if (stat === false)
  5220. return cb(null, stat)
  5221. else {
  5222. var type = stat.isDirectory() ? 'DIR' : 'FILE';
  5223. if (needDir && type === 'FILE')
  5224. return cb()
  5225. else
  5226. return cb(null, type, stat)
  5227. }
  5228. }
  5229. var self = this;
  5230. var statcb = inflight('stat\0' + abs, lstatcb_);
  5231. if (statcb)
  5232. self.fs.lstat(abs, statcb);
  5233. function lstatcb_ (er, lstat) {
  5234. if (lstat && lstat.isSymbolicLink()) {
  5235. // If it's a symlink, then treat it as the target, unless
  5236. // the target does not exist, then treat it as a file.
  5237. return self.fs.stat(abs, function (er, stat) {
  5238. if (er)
  5239. self._stat2(f, abs, null, lstat, cb);
  5240. else
  5241. self._stat2(f, abs, er, stat, cb);
  5242. })
  5243. } else {
  5244. self._stat2(f, abs, er, lstat, cb);
  5245. }
  5246. }
  5247. };
  5248. Glob.prototype._stat2 = function (f, abs, er, stat, cb) {
  5249. if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
  5250. this.statCache[abs] = false;
  5251. return cb()
  5252. }
  5253. var needDir = f.slice(-1) === '/';
  5254. this.statCache[abs] = stat;
  5255. if (abs.slice(-1) === '/' && stat && !stat.isDirectory())
  5256. return cb(null, false, stat)
  5257. var c = true;
  5258. if (stat)
  5259. c = stat.isDirectory() ? 'DIR' : 'FILE';
  5260. this.cache[abs] = this.cache[abs] || c;
  5261. if (needDir && c === 'FILE')
  5262. return cb()
  5263. return cb(null, c, stat)
  5264. };
  5265. return glob_1;
  5266. }
  5267. var globExports = requireGlob();
  5268. var glob$1 = /*@__PURE__*/getDefaultExportFromCjs(globExports);
  5269. const comma = ','.charCodeAt(0);
  5270. const semicolon = ';'.charCodeAt(0);
  5271. const chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  5272. const intToChar = new Uint8Array(64); // 64 possible chars.
  5273. const charToInt = new Uint8Array(128); // z is 122 in ASCII
  5274. for (let i = 0; i < chars$1.length; i++) {
  5275. const c = chars$1.charCodeAt(i);
  5276. intToChar[i] = c;
  5277. charToInt[c] = i;
  5278. }
  5279. // Provide a fallback for older environments.
  5280. const td = typeof TextDecoder !== 'undefined'
  5281. ? /* #__PURE__ */ new TextDecoder()
  5282. : typeof Buffer !== 'undefined'
  5283. ? {
  5284. decode(buf) {
  5285. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  5286. return out.toString();
  5287. },
  5288. }
  5289. : {
  5290. decode(buf) {
  5291. let out = '';
  5292. for (let i = 0; i < buf.length; i++) {
  5293. out += String.fromCharCode(buf[i]);
  5294. }
  5295. return out;
  5296. },
  5297. };
  5298. function decode(mappings) {
  5299. const state = new Int32Array(5);
  5300. const decoded = [];
  5301. let index = 0;
  5302. do {
  5303. const semi = indexOf(mappings, index);
  5304. const line = [];
  5305. let sorted = true;
  5306. let lastCol = 0;
  5307. state[0] = 0;
  5308. for (let i = index; i < semi; i++) {
  5309. let seg;
  5310. i = decodeInteger(mappings, i, state, 0); // genColumn
  5311. const col = state[0];
  5312. if (col < lastCol)
  5313. sorted = false;
  5314. lastCol = col;
  5315. if (hasMoreVlq(mappings, i, semi)) {
  5316. i = decodeInteger(mappings, i, state, 1); // sourcesIndex
  5317. i = decodeInteger(mappings, i, state, 2); // sourceLine
  5318. i = decodeInteger(mappings, i, state, 3); // sourceColumn
  5319. if (hasMoreVlq(mappings, i, semi)) {
  5320. i = decodeInteger(mappings, i, state, 4); // namesIndex
  5321. seg = [col, state[1], state[2], state[3], state[4]];
  5322. }
  5323. else {
  5324. seg = [col, state[1], state[2], state[3]];
  5325. }
  5326. }
  5327. else {
  5328. seg = [col];
  5329. }
  5330. line.push(seg);
  5331. }
  5332. if (!sorted)
  5333. sort(line);
  5334. decoded.push(line);
  5335. index = semi + 1;
  5336. } while (index <= mappings.length);
  5337. return decoded;
  5338. }
  5339. function indexOf(mappings, index) {
  5340. const idx = mappings.indexOf(';', index);
  5341. return idx === -1 ? mappings.length : idx;
  5342. }
  5343. function decodeInteger(mappings, pos, state, j) {
  5344. let value = 0;
  5345. let shift = 0;
  5346. let integer = 0;
  5347. do {
  5348. const c = mappings.charCodeAt(pos++);
  5349. integer = charToInt[c];
  5350. value |= (integer & 31) << shift;
  5351. shift += 5;
  5352. } while (integer & 32);
  5353. const shouldNegate = value & 1;
  5354. value >>>= 1;
  5355. if (shouldNegate) {
  5356. value = -0x80000000 | -value;
  5357. }
  5358. state[j] += value;
  5359. return pos;
  5360. }
  5361. function hasMoreVlq(mappings, i, length) {
  5362. if (i >= length)
  5363. return false;
  5364. return mappings.charCodeAt(i) !== comma;
  5365. }
  5366. function sort(line) {
  5367. line.sort(sortComparator$1);
  5368. }
  5369. function sortComparator$1(a, b) {
  5370. return a[0] - b[0];
  5371. }
  5372. function encode$1(decoded) {
  5373. const state = new Int32Array(5);
  5374. const bufLength = 1024 * 16;
  5375. const subLength = bufLength - 36;
  5376. const buf = new Uint8Array(bufLength);
  5377. const sub = buf.subarray(0, subLength);
  5378. let pos = 0;
  5379. let out = '';
  5380. for (let i = 0; i < decoded.length; i++) {
  5381. const line = decoded[i];
  5382. if (i > 0) {
  5383. if (pos === bufLength) {
  5384. out += td.decode(buf);
  5385. pos = 0;
  5386. }
  5387. buf[pos++] = semicolon;
  5388. }
  5389. if (line.length === 0)
  5390. continue;
  5391. state[0] = 0;
  5392. for (let j = 0; j < line.length; j++) {
  5393. const segment = line[j];
  5394. // We can push up to 5 ints, each int can take at most 7 chars, and we
  5395. // may push a comma.
  5396. if (pos > subLength) {
  5397. out += td.decode(sub);
  5398. buf.copyWithin(0, subLength, pos);
  5399. pos -= subLength;
  5400. }
  5401. if (j > 0)
  5402. buf[pos++] = comma;
  5403. pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
  5404. if (segment.length === 1)
  5405. continue;
  5406. pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
  5407. pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
  5408. pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
  5409. if (segment.length === 4)
  5410. continue;
  5411. pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
  5412. }
  5413. }
  5414. return out + td.decode(buf.subarray(0, pos));
  5415. }
  5416. function encodeInteger(buf, pos, state, segment, j) {
  5417. const next = segment[j];
  5418. let num = next - state[j];
  5419. state[j] = next;
  5420. num = num < 0 ? (-num << 1) | 1 : num << 1;
  5421. do {
  5422. let clamped = num & 0b011111;
  5423. num >>>= 5;
  5424. if (num > 0)
  5425. clamped |= 0b100000;
  5426. buf[pos++] = intToChar[clamped];
  5427. } while (num > 0);
  5428. return pos;
  5429. }
  5430. class BitSet {
  5431. constructor(arg) {
  5432. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  5433. }
  5434. add(n) {
  5435. this.bits[n >> 5] |= 1 << (n & 31);
  5436. }
  5437. has(n) {
  5438. return !!(this.bits[n >> 5] & (1 << (n & 31)));
  5439. }
  5440. }
  5441. class Chunk {
  5442. constructor(start, end, content) {
  5443. this.start = start;
  5444. this.end = end;
  5445. this.original = content;
  5446. this.intro = '';
  5447. this.outro = '';
  5448. this.content = content;
  5449. this.storeName = false;
  5450. this.edited = false;
  5451. {
  5452. this.previous = null;
  5453. this.next = null;
  5454. }
  5455. }
  5456. appendLeft(content) {
  5457. this.outro += content;
  5458. }
  5459. appendRight(content) {
  5460. this.intro = this.intro + content;
  5461. }
  5462. clone() {
  5463. const chunk = new Chunk(this.start, this.end, this.original);
  5464. chunk.intro = this.intro;
  5465. chunk.outro = this.outro;
  5466. chunk.content = this.content;
  5467. chunk.storeName = this.storeName;
  5468. chunk.edited = this.edited;
  5469. return chunk;
  5470. }
  5471. contains(index) {
  5472. return this.start < index && index < this.end;
  5473. }
  5474. eachNext(fn) {
  5475. let chunk = this;
  5476. while (chunk) {
  5477. fn(chunk);
  5478. chunk = chunk.next;
  5479. }
  5480. }
  5481. eachPrevious(fn) {
  5482. let chunk = this;
  5483. while (chunk) {
  5484. fn(chunk);
  5485. chunk = chunk.previous;
  5486. }
  5487. }
  5488. edit(content, storeName, contentOnly) {
  5489. this.content = content;
  5490. if (!contentOnly) {
  5491. this.intro = '';
  5492. this.outro = '';
  5493. }
  5494. this.storeName = storeName;
  5495. this.edited = true;
  5496. return this;
  5497. }
  5498. prependLeft(content) {
  5499. this.outro = content + this.outro;
  5500. }
  5501. prependRight(content) {
  5502. this.intro = content + this.intro;
  5503. }
  5504. reset() {
  5505. this.intro = '';
  5506. this.outro = '';
  5507. if (this.edited) {
  5508. this.content = this.original;
  5509. this.storeName = false;
  5510. this.edited = false;
  5511. }
  5512. }
  5513. split(index) {
  5514. const sliceIndex = index - this.start;
  5515. const originalBefore = this.original.slice(0, sliceIndex);
  5516. const originalAfter = this.original.slice(sliceIndex);
  5517. this.original = originalBefore;
  5518. const newChunk = new Chunk(index, this.end, originalAfter);
  5519. newChunk.outro = this.outro;
  5520. this.outro = '';
  5521. this.end = index;
  5522. if (this.edited) {
  5523. // after split we should save the edit content record into the correct chunk
  5524. // to make sure sourcemap correct
  5525. // For example:
  5526. // ' test'.trim()
  5527. // split -> ' ' + 'test'
  5528. // ✔️ edit -> '' + 'test'
  5529. // ✖️ edit -> 'test' + ''
  5530. // TODO is this block necessary?...
  5531. newChunk.edit('', false);
  5532. this.content = '';
  5533. } else {
  5534. this.content = originalBefore;
  5535. }
  5536. newChunk.next = this.next;
  5537. if (newChunk.next) newChunk.next.previous = newChunk;
  5538. newChunk.previous = this;
  5539. this.next = newChunk;
  5540. return newChunk;
  5541. }
  5542. toString() {
  5543. return this.intro + this.content + this.outro;
  5544. }
  5545. trimEnd(rx) {
  5546. this.outro = this.outro.replace(rx, '');
  5547. if (this.outro.length) return true;
  5548. const trimmed = this.content.replace(rx, '');
  5549. if (trimmed.length) {
  5550. if (trimmed !== this.content) {
  5551. this.split(this.start + trimmed.length).edit('', undefined, true);
  5552. if (this.edited) {
  5553. // save the change, if it has been edited
  5554. this.edit(trimmed, this.storeName, true);
  5555. }
  5556. }
  5557. return true;
  5558. } else {
  5559. this.edit('', undefined, true);
  5560. this.intro = this.intro.replace(rx, '');
  5561. if (this.intro.length) return true;
  5562. }
  5563. }
  5564. trimStart(rx) {
  5565. this.intro = this.intro.replace(rx, '');
  5566. if (this.intro.length) return true;
  5567. const trimmed = this.content.replace(rx, '');
  5568. if (trimmed.length) {
  5569. if (trimmed !== this.content) {
  5570. const newChunk = this.split(this.end - trimmed.length);
  5571. if (this.edited) {
  5572. // save the change, if it has been edited
  5573. newChunk.edit(trimmed, this.storeName, true);
  5574. }
  5575. this.edit('', undefined, true);
  5576. }
  5577. return true;
  5578. } else {
  5579. this.edit('', undefined, true);
  5580. this.outro = this.outro.replace(rx, '');
  5581. if (this.outro.length) return true;
  5582. }
  5583. }
  5584. }
  5585. function getBtoa() {
  5586. if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
  5587. return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
  5588. } else if (typeof Buffer === 'function') {
  5589. return (str) => Buffer.from(str, 'utf-8').toString('base64');
  5590. } else {
  5591. return () => {
  5592. throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
  5593. };
  5594. }
  5595. }
  5596. const btoa$1 = /*#__PURE__*/ getBtoa();
  5597. let SourceMap$1 = class SourceMap {
  5598. constructor(properties) {
  5599. this.version = 3;
  5600. this.file = properties.file;
  5601. this.sources = properties.sources;
  5602. this.sourcesContent = properties.sourcesContent;
  5603. this.names = properties.names;
  5604. this.mappings = encode$1(properties.mappings);
  5605. if (typeof properties.x_google_ignoreList !== 'undefined') {
  5606. this.x_google_ignoreList = properties.x_google_ignoreList;
  5607. }
  5608. }
  5609. toString() {
  5610. return JSON.stringify(this);
  5611. }
  5612. toUrl() {
  5613. return 'data:application/json;charset=utf-8;base64,' + btoa$1(this.toString());
  5614. }
  5615. };
  5616. function guessIndent(code) {
  5617. const lines = code.split('\n');
  5618. const tabbed = lines.filter((line) => /^\t+/.test(line));
  5619. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  5620. if (tabbed.length === 0 && spaced.length === 0) {
  5621. return null;
  5622. }
  5623. // More lines tabbed than spaced? Assume tabs, and
  5624. // default to tabs in the case of a tie (or nothing
  5625. // to go on)
  5626. if (tabbed.length >= spaced.length) {
  5627. return '\t';
  5628. }
  5629. // Otherwise, we need to guess the multiple
  5630. const min = spaced.reduce((previous, current) => {
  5631. const numSpaces = /^ +/.exec(current)[0].length;
  5632. return Math.min(numSpaces, previous);
  5633. }, Infinity);
  5634. return new Array(min + 1).join(' ');
  5635. }
  5636. function getRelativePath(from, to) {
  5637. const fromParts = from.split(/[/\\]/);
  5638. const toParts = to.split(/[/\\]/);
  5639. fromParts.pop(); // get dirname
  5640. while (fromParts[0] === toParts[0]) {
  5641. fromParts.shift();
  5642. toParts.shift();
  5643. }
  5644. if (fromParts.length) {
  5645. let i = fromParts.length;
  5646. while (i--) fromParts[i] = '..';
  5647. }
  5648. return fromParts.concat(toParts).join('/');
  5649. }
  5650. const toString$2 = Object.prototype.toString;
  5651. function isObject$2(thing) {
  5652. return toString$2.call(thing) === '[object Object]';
  5653. }
  5654. function getLocator(source) {
  5655. const originalLines = source.split('\n');
  5656. const lineOffsets = [];
  5657. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  5658. lineOffsets.push(pos);
  5659. pos += originalLines[i].length + 1;
  5660. }
  5661. return function locate(index) {
  5662. let i = 0;
  5663. let j = lineOffsets.length;
  5664. while (i < j) {
  5665. const m = (i + j) >> 1;
  5666. if (index < lineOffsets[m]) {
  5667. j = m;
  5668. } else {
  5669. i = m + 1;
  5670. }
  5671. }
  5672. const line = i - 1;
  5673. const column = index - lineOffsets[line];
  5674. return { line, column };
  5675. };
  5676. }
  5677. const wordRegex = /\w/;
  5678. class Mappings {
  5679. constructor(hires) {
  5680. this.hires = hires;
  5681. this.generatedCodeLine = 0;
  5682. this.generatedCodeColumn = 0;
  5683. this.raw = [];
  5684. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  5685. this.pending = null;
  5686. }
  5687. addEdit(sourceIndex, content, loc, nameIndex) {
  5688. if (content.length) {
  5689. const contentLengthMinusOne = content.length - 1;
  5690. let contentLineEnd = content.indexOf('\n', 0);
  5691. let previousContentLineEnd = -1;
  5692. // Loop through each line in the content and add a segment, but stop if the last line is empty,
  5693. // else code afterwards would fill one line too many
  5694. while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
  5695. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  5696. if (nameIndex >= 0) {
  5697. segment.push(nameIndex);
  5698. }
  5699. this.rawSegments.push(segment);
  5700. this.generatedCodeLine += 1;
  5701. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  5702. this.generatedCodeColumn = 0;
  5703. previousContentLineEnd = contentLineEnd;
  5704. contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
  5705. }
  5706. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  5707. if (nameIndex >= 0) {
  5708. segment.push(nameIndex);
  5709. }
  5710. this.rawSegments.push(segment);
  5711. this.advance(content.slice(previousContentLineEnd + 1));
  5712. } else if (this.pending) {
  5713. this.rawSegments.push(this.pending);
  5714. this.advance(content);
  5715. }
  5716. this.pending = null;
  5717. }
  5718. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  5719. let originalCharIndex = chunk.start;
  5720. let first = true;
  5721. // when iterating each char, check if it's in a word boundary
  5722. let charInHiresBoundary = false;
  5723. while (originalCharIndex < chunk.end) {
  5724. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  5725. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  5726. if (this.hires === 'boundary') {
  5727. // in hires "boundary", group segments per word boundary than per char
  5728. if (wordRegex.test(original[originalCharIndex])) {
  5729. // for first char in the boundary found, start the boundary by pushing a segment
  5730. if (!charInHiresBoundary) {
  5731. this.rawSegments.push(segment);
  5732. charInHiresBoundary = true;
  5733. }
  5734. } else {
  5735. // for non-word char, end the boundary by pushing a segment
  5736. this.rawSegments.push(segment);
  5737. charInHiresBoundary = false;
  5738. }
  5739. } else {
  5740. this.rawSegments.push(segment);
  5741. }
  5742. }
  5743. if (original[originalCharIndex] === '\n') {
  5744. loc.line += 1;
  5745. loc.column = 0;
  5746. this.generatedCodeLine += 1;
  5747. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  5748. this.generatedCodeColumn = 0;
  5749. first = true;
  5750. } else {
  5751. loc.column += 1;
  5752. this.generatedCodeColumn += 1;
  5753. first = false;
  5754. }
  5755. originalCharIndex += 1;
  5756. }
  5757. this.pending = null;
  5758. }
  5759. advance(str) {
  5760. if (!str) return;
  5761. const lines = str.split('\n');
  5762. if (lines.length > 1) {
  5763. for (let i = 0; i < lines.length - 1; i++) {
  5764. this.generatedCodeLine++;
  5765. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  5766. }
  5767. this.generatedCodeColumn = 0;
  5768. }
  5769. this.generatedCodeColumn += lines[lines.length - 1].length;
  5770. }
  5771. }
  5772. const n$1 = '\n';
  5773. const warned = {
  5774. insertLeft: false,
  5775. insertRight: false,
  5776. storeName: false,
  5777. };
  5778. class MagicString {
  5779. constructor(string, options = {}) {
  5780. const chunk = new Chunk(0, string.length, string);
  5781. Object.defineProperties(this, {
  5782. original: { writable: true, value: string },
  5783. outro: { writable: true, value: '' },
  5784. intro: { writable: true, value: '' },
  5785. firstChunk: { writable: true, value: chunk },
  5786. lastChunk: { writable: true, value: chunk },
  5787. lastSearchedChunk: { writable: true, value: chunk },
  5788. byStart: { writable: true, value: {} },
  5789. byEnd: { writable: true, value: {} },
  5790. filename: { writable: true, value: options.filename },
  5791. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  5792. sourcemapLocations: { writable: true, value: new BitSet() },
  5793. storedNames: { writable: true, value: {} },
  5794. indentStr: { writable: true, value: undefined },
  5795. ignoreList: { writable: true, value: options.ignoreList },
  5796. });
  5797. this.byStart[0] = chunk;
  5798. this.byEnd[string.length] = chunk;
  5799. }
  5800. addSourcemapLocation(char) {
  5801. this.sourcemapLocations.add(char);
  5802. }
  5803. append(content) {
  5804. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  5805. this.outro += content;
  5806. return this;
  5807. }
  5808. appendLeft(index, content) {
  5809. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  5810. this._split(index);
  5811. const chunk = this.byEnd[index];
  5812. if (chunk) {
  5813. chunk.appendLeft(content);
  5814. } else {
  5815. this.intro += content;
  5816. }
  5817. return this;
  5818. }
  5819. appendRight(index, content) {
  5820. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  5821. this._split(index);
  5822. const chunk = this.byStart[index];
  5823. if (chunk) {
  5824. chunk.appendRight(content);
  5825. } else {
  5826. this.outro += content;
  5827. }
  5828. return this;
  5829. }
  5830. clone() {
  5831. const cloned = new MagicString(this.original, { filename: this.filename });
  5832. let originalChunk = this.firstChunk;
  5833. let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
  5834. while (originalChunk) {
  5835. cloned.byStart[clonedChunk.start] = clonedChunk;
  5836. cloned.byEnd[clonedChunk.end] = clonedChunk;
  5837. const nextOriginalChunk = originalChunk.next;
  5838. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  5839. if (nextClonedChunk) {
  5840. clonedChunk.next = nextClonedChunk;
  5841. nextClonedChunk.previous = clonedChunk;
  5842. clonedChunk = nextClonedChunk;
  5843. }
  5844. originalChunk = nextOriginalChunk;
  5845. }
  5846. cloned.lastChunk = clonedChunk;
  5847. if (this.indentExclusionRanges) {
  5848. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  5849. }
  5850. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  5851. cloned.intro = this.intro;
  5852. cloned.outro = this.outro;
  5853. return cloned;
  5854. }
  5855. generateDecodedMap(options) {
  5856. options = options || {};
  5857. const sourceIndex = 0;
  5858. const names = Object.keys(this.storedNames);
  5859. const mappings = new Mappings(options.hires);
  5860. const locate = getLocator(this.original);
  5861. if (this.intro) {
  5862. mappings.advance(this.intro);
  5863. }
  5864. this.firstChunk.eachNext((chunk) => {
  5865. const loc = locate(chunk.start);
  5866. if (chunk.intro.length) mappings.advance(chunk.intro);
  5867. if (chunk.edited) {
  5868. mappings.addEdit(
  5869. sourceIndex,
  5870. chunk.content,
  5871. loc,
  5872. chunk.storeName ? names.indexOf(chunk.original) : -1,
  5873. );
  5874. } else {
  5875. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  5876. }
  5877. if (chunk.outro.length) mappings.advance(chunk.outro);
  5878. });
  5879. return {
  5880. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  5881. sources: [
  5882. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  5883. ],
  5884. sourcesContent: options.includeContent ? [this.original] : undefined,
  5885. names,
  5886. mappings: mappings.raw,
  5887. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  5888. };
  5889. }
  5890. generateMap(options) {
  5891. return new SourceMap$1(this.generateDecodedMap(options));
  5892. }
  5893. _ensureindentStr() {
  5894. if (this.indentStr === undefined) {
  5895. this.indentStr = guessIndent(this.original);
  5896. }
  5897. }
  5898. _getRawIndentString() {
  5899. this._ensureindentStr();
  5900. return this.indentStr;
  5901. }
  5902. getIndentString() {
  5903. this._ensureindentStr();
  5904. return this.indentStr === null ? '\t' : this.indentStr;
  5905. }
  5906. indent(indentStr, options) {
  5907. const pattern = /^[^\r\n]/gm;
  5908. if (isObject$2(indentStr)) {
  5909. options = indentStr;
  5910. indentStr = undefined;
  5911. }
  5912. if (indentStr === undefined) {
  5913. this._ensureindentStr();
  5914. indentStr = this.indentStr || '\t';
  5915. }
  5916. if (indentStr === '') return this; // noop
  5917. options = options || {};
  5918. // Process exclusion ranges
  5919. const isExcluded = {};
  5920. if (options.exclude) {
  5921. const exclusions =
  5922. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  5923. exclusions.forEach((exclusion) => {
  5924. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  5925. isExcluded[i] = true;
  5926. }
  5927. });
  5928. }
  5929. let shouldIndentNextCharacter = options.indentStart !== false;
  5930. const replacer = (match) => {
  5931. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  5932. shouldIndentNextCharacter = true;
  5933. return match;
  5934. };
  5935. this.intro = this.intro.replace(pattern, replacer);
  5936. let charIndex = 0;
  5937. let chunk = this.firstChunk;
  5938. while (chunk) {
  5939. const end = chunk.end;
  5940. if (chunk.edited) {
  5941. if (!isExcluded[charIndex]) {
  5942. chunk.content = chunk.content.replace(pattern, replacer);
  5943. if (chunk.content.length) {
  5944. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  5945. }
  5946. }
  5947. } else {
  5948. charIndex = chunk.start;
  5949. while (charIndex < end) {
  5950. if (!isExcluded[charIndex]) {
  5951. const char = this.original[charIndex];
  5952. if (char === '\n') {
  5953. shouldIndentNextCharacter = true;
  5954. } else if (char !== '\r' && shouldIndentNextCharacter) {
  5955. shouldIndentNextCharacter = false;
  5956. if (charIndex === chunk.start) {
  5957. chunk.prependRight(indentStr);
  5958. } else {
  5959. this._splitChunk(chunk, charIndex);
  5960. chunk = chunk.next;
  5961. chunk.prependRight(indentStr);
  5962. }
  5963. }
  5964. }
  5965. charIndex += 1;
  5966. }
  5967. }
  5968. charIndex = chunk.end;
  5969. chunk = chunk.next;
  5970. }
  5971. this.outro = this.outro.replace(pattern, replacer);
  5972. return this;
  5973. }
  5974. insert() {
  5975. throw new Error(
  5976. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  5977. );
  5978. }
  5979. insertLeft(index, content) {
  5980. if (!warned.insertLeft) {
  5981. console.warn(
  5982. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  5983. ); // eslint-disable-line no-console
  5984. warned.insertLeft = true;
  5985. }
  5986. return this.appendLeft(index, content);
  5987. }
  5988. insertRight(index, content) {
  5989. if (!warned.insertRight) {
  5990. console.warn(
  5991. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  5992. ); // eslint-disable-line no-console
  5993. warned.insertRight = true;
  5994. }
  5995. return this.prependRight(index, content);
  5996. }
  5997. move(start, end, index) {
  5998. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  5999. this._split(start);
  6000. this._split(end);
  6001. this._split(index);
  6002. const first = this.byStart[start];
  6003. const last = this.byEnd[end];
  6004. const oldLeft = first.previous;
  6005. const oldRight = last.next;
  6006. const newRight = this.byStart[index];
  6007. if (!newRight && last === this.lastChunk) return this;
  6008. const newLeft = newRight ? newRight.previous : this.lastChunk;
  6009. if (oldLeft) oldLeft.next = oldRight;
  6010. if (oldRight) oldRight.previous = oldLeft;
  6011. if (newLeft) newLeft.next = first;
  6012. if (newRight) newRight.previous = last;
  6013. if (!first.previous) this.firstChunk = last.next;
  6014. if (!last.next) {
  6015. this.lastChunk = first.previous;
  6016. this.lastChunk.next = null;
  6017. }
  6018. first.previous = newLeft;
  6019. last.next = newRight || null;
  6020. if (!newLeft) this.firstChunk = first;
  6021. if (!newRight) this.lastChunk = last;
  6022. return this;
  6023. }
  6024. overwrite(start, end, content, options) {
  6025. options = options || {};
  6026. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  6027. }
  6028. update(start, end, content, options) {
  6029. if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
  6030. while (start < 0) start += this.original.length;
  6031. while (end < 0) end += this.original.length;
  6032. if (end > this.original.length) throw new Error('end is out of bounds');
  6033. if (start === end)
  6034. throw new Error(
  6035. 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
  6036. );
  6037. this._split(start);
  6038. this._split(end);
  6039. if (options === true) {
  6040. if (!warned.storeName) {
  6041. console.warn(
  6042. 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
  6043. ); // eslint-disable-line no-console
  6044. warned.storeName = true;
  6045. }
  6046. options = { storeName: true };
  6047. }
  6048. const storeName = options !== undefined ? options.storeName : false;
  6049. const overwrite = options !== undefined ? options.overwrite : false;
  6050. if (storeName) {
  6051. const original = this.original.slice(start, end);
  6052. Object.defineProperty(this.storedNames, original, {
  6053. writable: true,
  6054. value: true,
  6055. enumerable: true,
  6056. });
  6057. }
  6058. const first = this.byStart[start];
  6059. const last = this.byEnd[end];
  6060. if (first) {
  6061. let chunk = first;
  6062. while (chunk !== last) {
  6063. if (chunk.next !== this.byStart[chunk.end]) {
  6064. throw new Error('Cannot overwrite across a split point');
  6065. }
  6066. chunk = chunk.next;
  6067. chunk.edit('', false);
  6068. }
  6069. first.edit(content, storeName, !overwrite);
  6070. } else {
  6071. // must be inserting at the end
  6072. const newChunk = new Chunk(start, end, '').edit(content, storeName);
  6073. // TODO last chunk in the array may not be the last chunk, if it's moved...
  6074. last.next = newChunk;
  6075. newChunk.previous = last;
  6076. }
  6077. return this;
  6078. }
  6079. prepend(content) {
  6080. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  6081. this.intro = content + this.intro;
  6082. return this;
  6083. }
  6084. prependLeft(index, content) {
  6085. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  6086. this._split(index);
  6087. const chunk = this.byEnd[index];
  6088. if (chunk) {
  6089. chunk.prependLeft(content);
  6090. } else {
  6091. this.intro = content + this.intro;
  6092. }
  6093. return this;
  6094. }
  6095. prependRight(index, content) {
  6096. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  6097. this._split(index);
  6098. const chunk = this.byStart[index];
  6099. if (chunk) {
  6100. chunk.prependRight(content);
  6101. } else {
  6102. this.outro = content + this.outro;
  6103. }
  6104. return this;
  6105. }
  6106. remove(start, end) {
  6107. while (start < 0) start += this.original.length;
  6108. while (end < 0) end += this.original.length;
  6109. if (start === end) return this;
  6110. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  6111. if (start > end) throw new Error('end must be greater than start');
  6112. this._split(start);
  6113. this._split(end);
  6114. let chunk = this.byStart[start];
  6115. while (chunk) {
  6116. chunk.intro = '';
  6117. chunk.outro = '';
  6118. chunk.edit('');
  6119. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  6120. }
  6121. return this;
  6122. }
  6123. reset(start, end) {
  6124. while (start < 0) start += this.original.length;
  6125. while (end < 0) end += this.original.length;
  6126. if (start === end) return this;
  6127. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  6128. if (start > end) throw new Error('end must be greater than start');
  6129. this._split(start);
  6130. this._split(end);
  6131. let chunk = this.byStart[start];
  6132. while (chunk) {
  6133. chunk.reset();
  6134. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  6135. }
  6136. return this;
  6137. }
  6138. lastChar() {
  6139. if (this.outro.length) return this.outro[this.outro.length - 1];
  6140. let chunk = this.lastChunk;
  6141. do {
  6142. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  6143. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  6144. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  6145. } while ((chunk = chunk.previous));
  6146. if (this.intro.length) return this.intro[this.intro.length - 1];
  6147. return '';
  6148. }
  6149. lastLine() {
  6150. let lineIndex = this.outro.lastIndexOf(n$1);
  6151. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  6152. let lineStr = this.outro;
  6153. let chunk = this.lastChunk;
  6154. do {
  6155. if (chunk.outro.length > 0) {
  6156. lineIndex = chunk.outro.lastIndexOf(n$1);
  6157. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  6158. lineStr = chunk.outro + lineStr;
  6159. }
  6160. if (chunk.content.length > 0) {
  6161. lineIndex = chunk.content.lastIndexOf(n$1);
  6162. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  6163. lineStr = chunk.content + lineStr;
  6164. }
  6165. if (chunk.intro.length > 0) {
  6166. lineIndex = chunk.intro.lastIndexOf(n$1);
  6167. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  6168. lineStr = chunk.intro + lineStr;
  6169. }
  6170. } while ((chunk = chunk.previous));
  6171. lineIndex = this.intro.lastIndexOf(n$1);
  6172. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  6173. return this.intro + lineStr;
  6174. }
  6175. slice(start = 0, end = this.original.length) {
  6176. while (start < 0) start += this.original.length;
  6177. while (end < 0) end += this.original.length;
  6178. let result = '';
  6179. // find start chunk
  6180. let chunk = this.firstChunk;
  6181. while (chunk && (chunk.start > start || chunk.end <= start)) {
  6182. // found end chunk before start
  6183. if (chunk.start < end && chunk.end >= end) {
  6184. return result;
  6185. }
  6186. chunk = chunk.next;
  6187. }
  6188. if (chunk && chunk.edited && chunk.start !== start)
  6189. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  6190. const startChunk = chunk;
  6191. while (chunk) {
  6192. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  6193. result += chunk.intro;
  6194. }
  6195. const containsEnd = chunk.start < end && chunk.end >= end;
  6196. if (containsEnd && chunk.edited && chunk.end !== end)
  6197. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  6198. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  6199. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  6200. result += chunk.content.slice(sliceStart, sliceEnd);
  6201. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  6202. result += chunk.outro;
  6203. }
  6204. if (containsEnd) {
  6205. break;
  6206. }
  6207. chunk = chunk.next;
  6208. }
  6209. return result;
  6210. }
  6211. // TODO deprecate this? not really very useful
  6212. snip(start, end) {
  6213. const clone = this.clone();
  6214. clone.remove(0, start);
  6215. clone.remove(end, clone.original.length);
  6216. return clone;
  6217. }
  6218. _split(index) {
  6219. if (this.byStart[index] || this.byEnd[index]) return;
  6220. let chunk = this.lastSearchedChunk;
  6221. const searchForward = index > chunk.end;
  6222. while (chunk) {
  6223. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  6224. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  6225. }
  6226. }
  6227. _splitChunk(chunk, index) {
  6228. if (chunk.edited && chunk.content.length) {
  6229. // zero-length edited chunks are a special case (overlapping replacements)
  6230. const loc = getLocator(this.original)(index);
  6231. throw new Error(
  6232. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  6233. );
  6234. }
  6235. const newChunk = chunk.split(index);
  6236. this.byEnd[index] = chunk;
  6237. this.byStart[index] = newChunk;
  6238. this.byEnd[newChunk.end] = newChunk;
  6239. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  6240. this.lastSearchedChunk = chunk;
  6241. return true;
  6242. }
  6243. toString() {
  6244. let str = this.intro;
  6245. let chunk = this.firstChunk;
  6246. while (chunk) {
  6247. str += chunk.toString();
  6248. chunk = chunk.next;
  6249. }
  6250. return str + this.outro;
  6251. }
  6252. isEmpty() {
  6253. let chunk = this.firstChunk;
  6254. do {
  6255. if (
  6256. (chunk.intro.length && chunk.intro.trim()) ||
  6257. (chunk.content.length && chunk.content.trim()) ||
  6258. (chunk.outro.length && chunk.outro.trim())
  6259. )
  6260. return false;
  6261. } while ((chunk = chunk.next));
  6262. return true;
  6263. }
  6264. length() {
  6265. let chunk = this.firstChunk;
  6266. let length = 0;
  6267. do {
  6268. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  6269. } while ((chunk = chunk.next));
  6270. return length;
  6271. }
  6272. trimLines() {
  6273. return this.trim('[\\r\\n]');
  6274. }
  6275. trim(charType) {
  6276. return this.trimStart(charType).trimEnd(charType);
  6277. }
  6278. trimEndAborted(charType) {
  6279. const rx = new RegExp((charType || '\\s') + '+$');
  6280. this.outro = this.outro.replace(rx, '');
  6281. if (this.outro.length) return true;
  6282. let chunk = this.lastChunk;
  6283. do {
  6284. const end = chunk.end;
  6285. const aborted = chunk.trimEnd(rx);
  6286. // if chunk was trimmed, we have a new lastChunk
  6287. if (chunk.end !== end) {
  6288. if (this.lastChunk === chunk) {
  6289. this.lastChunk = chunk.next;
  6290. }
  6291. this.byEnd[chunk.end] = chunk;
  6292. this.byStart[chunk.next.start] = chunk.next;
  6293. this.byEnd[chunk.next.end] = chunk.next;
  6294. }
  6295. if (aborted) return true;
  6296. chunk = chunk.previous;
  6297. } while (chunk);
  6298. return false;
  6299. }
  6300. trimEnd(charType) {
  6301. this.trimEndAborted(charType);
  6302. return this;
  6303. }
  6304. trimStartAborted(charType) {
  6305. const rx = new RegExp('^' + (charType || '\\s') + '+');
  6306. this.intro = this.intro.replace(rx, '');
  6307. if (this.intro.length) return true;
  6308. let chunk = this.firstChunk;
  6309. do {
  6310. const end = chunk.end;
  6311. const aborted = chunk.trimStart(rx);
  6312. if (chunk.end !== end) {
  6313. // special case...
  6314. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  6315. this.byEnd[chunk.end] = chunk;
  6316. this.byStart[chunk.next.start] = chunk.next;
  6317. this.byEnd[chunk.next.end] = chunk.next;
  6318. }
  6319. if (aborted) return true;
  6320. chunk = chunk.next;
  6321. } while (chunk);
  6322. return false;
  6323. }
  6324. trimStart(charType) {
  6325. this.trimStartAborted(charType);
  6326. return this;
  6327. }
  6328. hasChanged() {
  6329. return this.original !== this.toString();
  6330. }
  6331. _replaceRegexp(searchValue, replacement) {
  6332. function getReplacement(match, str) {
  6333. if (typeof replacement === 'string') {
  6334. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  6335. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  6336. if (i === '$') return '$';
  6337. if (i === '&') return match[0];
  6338. const num = +i;
  6339. if (num < match.length) return match[+i];
  6340. return `$${i}`;
  6341. });
  6342. } else {
  6343. return replacement(...match, match.index, str, match.groups);
  6344. }
  6345. }
  6346. function matchAll(re, str) {
  6347. let match;
  6348. const matches = [];
  6349. while ((match = re.exec(str))) {
  6350. matches.push(match);
  6351. }
  6352. return matches;
  6353. }
  6354. if (searchValue.global) {
  6355. const matches = matchAll(searchValue, this.original);
  6356. matches.forEach((match) => {
  6357. if (match.index != null) {
  6358. const replacement = getReplacement(match, this.original);
  6359. if (replacement !== match[0]) {
  6360. this.overwrite(
  6361. match.index,
  6362. match.index + match[0].length,
  6363. replacement
  6364. );
  6365. }
  6366. }
  6367. });
  6368. } else {
  6369. const match = this.original.match(searchValue);
  6370. if (match && match.index != null) {
  6371. const replacement = getReplacement(match, this.original);
  6372. if (replacement !== match[0]) {
  6373. this.overwrite(
  6374. match.index,
  6375. match.index + match[0].length,
  6376. replacement
  6377. );
  6378. }
  6379. }
  6380. }
  6381. return this;
  6382. }
  6383. _replaceString(string, replacement) {
  6384. const { original } = this;
  6385. const index = original.indexOf(string);
  6386. if (index !== -1) {
  6387. this.overwrite(index, index + string.length, replacement);
  6388. }
  6389. return this;
  6390. }
  6391. replace(searchValue, replacement) {
  6392. if (typeof searchValue === 'string') {
  6393. return this._replaceString(searchValue, replacement);
  6394. }
  6395. return this._replaceRegexp(searchValue, replacement);
  6396. }
  6397. _replaceAllString(string, replacement) {
  6398. const { original } = this;
  6399. const stringLength = string.length;
  6400. for (
  6401. let index = original.indexOf(string);
  6402. index !== -1;
  6403. index = original.indexOf(string, index + stringLength)
  6404. ) {
  6405. const previous = original.slice(index, index + stringLength);
  6406. if (previous !== replacement)
  6407. this.overwrite(index, index + stringLength, replacement);
  6408. }
  6409. return this;
  6410. }
  6411. replaceAll(searchValue, replacement) {
  6412. if (typeof searchValue === 'string') {
  6413. return this._replaceAllString(searchValue, replacement);
  6414. }
  6415. if (!searchValue.global) {
  6416. throw new TypeError(
  6417. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  6418. );
  6419. }
  6420. return this._replaceRegexp(searchValue, replacement);
  6421. }
  6422. }
  6423. function isReference(node, parent) {
  6424. if (node.type === 'MemberExpression') {
  6425. return !node.computed && isReference(node.object, node);
  6426. }
  6427. if (node.type === 'Identifier') {
  6428. if (!parent)
  6429. return true;
  6430. switch (parent.type) {
  6431. // disregard `bar` in `foo.bar`
  6432. case 'MemberExpression': return parent.computed || node === parent.object;
  6433. // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
  6434. case 'MethodDefinition': return parent.computed;
  6435. // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
  6436. case 'FieldDefinition': return parent.computed || node === parent.value;
  6437. // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
  6438. case 'Property': return parent.computed || node === parent.value;
  6439. // disregard the `bar` in `export { foo as bar }` or
  6440. // the foo in `import { foo as bar }`
  6441. case 'ExportSpecifier':
  6442. case 'ImportSpecifier': return node === parent.local;
  6443. // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
  6444. case 'LabeledStatement':
  6445. case 'BreakStatement':
  6446. case 'ContinueStatement': return false;
  6447. default: return true;
  6448. }
  6449. }
  6450. return false;
  6451. }
  6452. var version$3 = "25.0.7";
  6453. var peerDependencies = {
  6454. rollup: "^2.68.0||^3.0.0||^4.0.0"
  6455. };
  6456. function tryParse(parse, code, id) {
  6457. try {
  6458. return parse(code, { allowReturnOutsideFunction: true });
  6459. } catch (err) {
  6460. err.message += ` in ${id}`;
  6461. throw err;
  6462. }
  6463. }
  6464. const firstpassGlobal = /\b(?:require|module|exports|global)\b/;
  6465. const firstpassNoGlobal = /\b(?:require|module|exports)\b/;
  6466. function hasCjsKeywords(code, ignoreGlobal) {
  6467. const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
  6468. return firstpass.test(code);
  6469. }
  6470. /* eslint-disable no-underscore-dangle */
  6471. function analyzeTopLevelStatements(parse, code, id) {
  6472. const ast = tryParse(parse, code, id);
  6473. let isEsModule = false;
  6474. let hasDefaultExport = false;
  6475. let hasNamedExports = false;
  6476. for (const node of ast.body) {
  6477. switch (node.type) {
  6478. case 'ExportDefaultDeclaration':
  6479. isEsModule = true;
  6480. hasDefaultExport = true;
  6481. break;
  6482. case 'ExportNamedDeclaration':
  6483. isEsModule = true;
  6484. if (node.declaration) {
  6485. hasNamedExports = true;
  6486. } else {
  6487. for (const specifier of node.specifiers) {
  6488. if (specifier.exported.name === 'default') {
  6489. hasDefaultExport = true;
  6490. } else {
  6491. hasNamedExports = true;
  6492. }
  6493. }
  6494. }
  6495. break;
  6496. case 'ExportAllDeclaration':
  6497. isEsModule = true;
  6498. if (node.exported && node.exported.name === 'default') {
  6499. hasDefaultExport = true;
  6500. } else {
  6501. hasNamedExports = true;
  6502. }
  6503. break;
  6504. case 'ImportDeclaration':
  6505. isEsModule = true;
  6506. break;
  6507. }
  6508. }
  6509. return { isEsModule, hasDefaultExport, hasNamedExports, ast };
  6510. }
  6511. /* eslint-disable import/prefer-default-export */
  6512. function deconflict(scopes, globals, identifier) {
  6513. let i = 1;
  6514. let deconflicted = makeLegalIdentifier(identifier);
  6515. const hasConflicts = () =>
  6516. scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
  6517. while (hasConflicts()) {
  6518. deconflicted = makeLegalIdentifier(`${identifier}_${i}`);
  6519. i += 1;
  6520. }
  6521. for (const scope of scopes) {
  6522. scope.declarations[deconflicted] = true;
  6523. }
  6524. return deconflicted;
  6525. }
  6526. function getName(id) {
  6527. const name = makeLegalIdentifier(basename$1(id, extname(id)));
  6528. if (name !== 'index') {
  6529. return name;
  6530. }
  6531. return makeLegalIdentifier(basename$1(dirname$1(id)));
  6532. }
  6533. function normalizePathSlashes(path) {
  6534. return path.replace(/\\/g, '/');
  6535. }
  6536. const getVirtualPathForDynamicRequirePath = (path, commonDir) =>
  6537. `/${normalizePathSlashes(relative$1(commonDir, path))}`;
  6538. function capitalize(name) {
  6539. return name[0].toUpperCase() + name.slice(1);
  6540. }
  6541. function getStrictRequiresFilter({ strictRequires }) {
  6542. switch (strictRequires) {
  6543. case true:
  6544. return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
  6545. // eslint-disable-next-line no-undefined
  6546. case undefined:
  6547. case 'auto':
  6548. case 'debug':
  6549. case null:
  6550. return { strictRequiresFilter: () => false, detectCyclesAndConditional: true };
  6551. case false:
  6552. return { strictRequiresFilter: () => false, detectCyclesAndConditional: false };
  6553. default:
  6554. if (typeof strictRequires === 'string' || Array.isArray(strictRequires)) {
  6555. return {
  6556. strictRequiresFilter: createFilter$1(strictRequires),
  6557. detectCyclesAndConditional: false
  6558. };
  6559. }
  6560. throw new Error('Unexpected value for "strictRequires" option.');
  6561. }
  6562. }
  6563. function getPackageEntryPoint(dirPath) {
  6564. let entryPoint = 'index.js';
  6565. try {
  6566. if (existsSync(join$1(dirPath, 'package.json'))) {
  6567. entryPoint =
  6568. JSON.parse(readFileSync(join$1(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
  6569. entryPoint;
  6570. }
  6571. } catch (ignored) {
  6572. // ignored
  6573. }
  6574. return entryPoint;
  6575. }
  6576. function isDirectory$1(path) {
  6577. try {
  6578. if (statSync$1(path).isDirectory()) return true;
  6579. } catch (ignored) {
  6580. // Nothing to do here
  6581. }
  6582. return false;
  6583. }
  6584. function getDynamicRequireModules(patterns, dynamicRequireRoot) {
  6585. const dynamicRequireModules = new Map();
  6586. const dirNames = new Set();
  6587. for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
  6588. const isNegated = pattern.startsWith('!');
  6589. const modifyMap = (targetPath, resolvedPath) =>
  6590. isNegated
  6591. ? dynamicRequireModules.delete(targetPath)
  6592. : dynamicRequireModules.set(targetPath, resolvedPath);
  6593. for (const path of glob$1.sync(isNegated ? pattern.substr(1) : pattern)) {
  6594. const resolvedPath = resolve$3(path);
  6595. const requirePath = normalizePathSlashes(resolvedPath);
  6596. if (isDirectory$1(resolvedPath)) {
  6597. dirNames.add(resolvedPath);
  6598. const modulePath = resolve$3(join$1(resolvedPath, getPackageEntryPoint(path)));
  6599. modifyMap(requirePath, modulePath);
  6600. modifyMap(normalizePathSlashes(modulePath), modulePath);
  6601. } else {
  6602. dirNames.add(dirname$1(resolvedPath));
  6603. modifyMap(requirePath, resolvedPath);
  6604. }
  6605. }
  6606. }
  6607. return {
  6608. commonDir: dirNames.size ? getCommonDir([...dirNames, dynamicRequireRoot]) : null,
  6609. dynamicRequireModules
  6610. };
  6611. }
  6612. const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
  6613. const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
  6614. const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
  6615. function getDynamicModuleRegistry(
  6616. isDynamicRequireModulesEnabled,
  6617. dynamicRequireModules,
  6618. commonDir,
  6619. ignoreDynamicRequires
  6620. ) {
  6621. if (!isDynamicRequireModulesEnabled) {
  6622. return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
  6623. ${FAILED_REQUIRE_ERROR}
  6624. }`;
  6625. }
  6626. const dynamicModuleImports = [...dynamicRequireModules.values()]
  6627. .map(
  6628. (id, index) =>
  6629. `import ${
  6630. id.endsWith('.json') ? `json${index}` : `{ __require as require${index} }`
  6631. } from ${JSON.stringify(id)};`
  6632. )
  6633. .join('\n');
  6634. const dynamicModuleProps = [...dynamicRequireModules.keys()]
  6635. .map(
  6636. (id, index) =>
  6637. `\t\t${JSON.stringify(getVirtualPathForDynamicRequirePath(id, commonDir))}: ${
  6638. id.endsWith('.json') ? `function () { return json${index}; }` : `require${index}`
  6639. }`
  6640. )
  6641. .join(',\n');
  6642. return `${dynamicModuleImports}
  6643. var dynamicModules;
  6644. function getDynamicModules() {
  6645. return dynamicModules || (dynamicModules = {
  6646. ${dynamicModuleProps}
  6647. });
  6648. }
  6649. export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
  6650. function handleRequire(path) {
  6651. var resolvedPath = commonjsResolve(path, originalModuleDir);
  6652. if (resolvedPath !== null) {
  6653. return getDynamicModules()[resolvedPath]();
  6654. }
  6655. ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
  6656. }
  6657. handleRequire.resolve = function (path) {
  6658. var resolvedPath = commonjsResolve(path, originalModuleDir);
  6659. if (resolvedPath !== null) {
  6660. return resolvedPath;
  6661. }
  6662. return require.resolve(path);
  6663. }
  6664. return handleRequire;
  6665. }
  6666. function commonjsResolve (path, originalModuleDir) {
  6667. var shouldTryNodeModules = isPossibleNodeModulesPath(path);
  6668. path = normalize(path);
  6669. var relPath;
  6670. if (path[0] === '/') {
  6671. originalModuleDir = '';
  6672. }
  6673. var modules = getDynamicModules();
  6674. var checkedExtensions = ['', '.js', '.json'];
  6675. while (true) {
  6676. if (!shouldTryNodeModules) {
  6677. relPath = normalize(originalModuleDir + '/' + path);
  6678. } else {
  6679. relPath = normalize(originalModuleDir + '/node_modules/' + path);
  6680. }
  6681. if (relPath.endsWith('/..')) {
  6682. break; // Travelled too far up, avoid infinite loop
  6683. }
  6684. for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {
  6685. var resolvedPath = relPath + checkedExtensions[extensionIndex];
  6686. if (modules[resolvedPath]) {
  6687. return resolvedPath;
  6688. }
  6689. }
  6690. if (!shouldTryNodeModules) break;
  6691. var nextDir = normalize(originalModuleDir + '/..');
  6692. if (nextDir === originalModuleDir) break;
  6693. originalModuleDir = nextDir;
  6694. }
  6695. return null;
  6696. }
  6697. function isPossibleNodeModulesPath (modulePath) {
  6698. var c0 = modulePath[0];
  6699. if (c0 === '/' || c0 === '\\\\') return false;
  6700. var c1 = modulePath[1], c2 = modulePath[2];
  6701. if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
  6702. (c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
  6703. if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;
  6704. return true;
  6705. }
  6706. function normalize (path) {
  6707. path = path.replace(/\\\\/g, '/');
  6708. var parts = path.split('/');
  6709. var slashed = parts[0] === '';
  6710. for (var i = 1; i < parts.length; i++) {
  6711. if (parts[i] === '.' || parts[i] === '') {
  6712. parts.splice(i--, 1);
  6713. }
  6714. }
  6715. for (var i = 1; i < parts.length; i++) {
  6716. if (parts[i] !== '..') continue;
  6717. if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
  6718. parts.splice(--i, 2);
  6719. i--;
  6720. }
  6721. }
  6722. path = parts.join('/');
  6723. if (slashed && path[0] !== '/') path = '/' + path;
  6724. else if (path.length === 0) path = '.';
  6725. return path;
  6726. }`;
  6727. }
  6728. const isWrappedId = (id, suffix) => id.endsWith(suffix);
  6729. const wrapId = (id, suffix) => `\0${id}${suffix}`;
  6730. const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
  6731. const PROXY_SUFFIX = '?commonjs-proxy';
  6732. const WRAPPED_SUFFIX = '?commonjs-wrapped';
  6733. const EXTERNAL_SUFFIX = '?commonjs-external';
  6734. const EXPORTS_SUFFIX = '?commonjs-exports';
  6735. const MODULE_SUFFIX = '?commonjs-module';
  6736. const ENTRY_SUFFIX = '?commonjs-entry';
  6737. const ES_IMPORT_SUFFIX = '?commonjs-es-import';
  6738. const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
  6739. const HELPERS_ID = '\0commonjsHelpers.js';
  6740. const IS_WRAPPED_COMMONJS = 'withRequireFunction';
  6741. // `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
  6742. // Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
  6743. // This could be improved by inspecting Rollup's "generatedCode" option
  6744. const HELPERS = `
  6745. export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  6746. export function getDefaultExportFromCjs (x) {
  6747. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  6748. }
  6749. export function getDefaultExportFromNamespaceIfPresent (n) {
  6750. return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
  6751. }
  6752. export function getDefaultExportFromNamespaceIfNotNamed (n) {
  6753. return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
  6754. }
  6755. export function getAugmentedNamespace(n) {
  6756. if (n.__esModule) return n;
  6757. var f = n.default;
  6758. if (typeof f == "function") {
  6759. var a = function a () {
  6760. if (this instanceof a) {
  6761. return Reflect.construct(f, arguments, this.constructor);
  6762. }
  6763. return f.apply(this, arguments);
  6764. };
  6765. a.prototype = f.prototype;
  6766. } else a = {};
  6767. Object.defineProperty(a, '__esModule', {value: true});
  6768. Object.keys(n).forEach(function (k) {
  6769. var d = Object.getOwnPropertyDescriptor(n, k);
  6770. Object.defineProperty(a, k, d.get ? d : {
  6771. enumerable: true,
  6772. get: function () {
  6773. return n[k];
  6774. }
  6775. });
  6776. });
  6777. return a;
  6778. }
  6779. `;
  6780. function getHelpersModule() {
  6781. return HELPERS;
  6782. }
  6783. function getUnknownRequireProxy(id, requireReturnsDefault) {
  6784. if (requireReturnsDefault === true || id.endsWith('.json')) {
  6785. return `export { default } from ${JSON.stringify(id)};`;
  6786. }
  6787. const name = getName(id);
  6788. const exported =
  6789. requireReturnsDefault === 'auto'
  6790. ? `import { getDefaultExportFromNamespaceIfNotNamed } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
  6791. : requireReturnsDefault === 'preferred'
  6792. ? `import { getDefaultExportFromNamespaceIfPresent } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
  6793. : !requireReturnsDefault
  6794. ? `import { getAugmentedNamespace } from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
  6795. : `export default ${name};`;
  6796. return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
  6797. }
  6798. async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
  6799. const name = getName(id);
  6800. const {
  6801. meta: { commonjs: commonjsMeta }
  6802. } = await loadModule({ id });
  6803. if (!commonjsMeta) {
  6804. return getUnknownRequireProxy(id, requireReturnsDefault);
  6805. }
  6806. if (commonjsMeta.isCommonJS) {
  6807. return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
  6808. }
  6809. if (!requireReturnsDefault) {
  6810. return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
  6811. id
  6812. )}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
  6813. }
  6814. if (
  6815. requireReturnsDefault !== true &&
  6816. (requireReturnsDefault === 'namespace' ||
  6817. !commonjsMeta.hasDefaultExport ||
  6818. (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
  6819. ) {
  6820. return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
  6821. }
  6822. return `export { default } from ${JSON.stringify(id)};`;
  6823. }
  6824. function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
  6825. const {
  6826. meta: { commonjs: commonjsMeta },
  6827. hasDefaultExport
  6828. } = getModuleInfo(id);
  6829. if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
  6830. const stringifiedId = JSON.stringify(id);
  6831. let code = `export * from ${stringifiedId};`;
  6832. if (hasDefaultExport) {
  6833. code += `export { default } from ${stringifiedId};`;
  6834. }
  6835. return shebang + code;
  6836. }
  6837. const result = getEsImportProxy(id, defaultIsModuleExports);
  6838. return {
  6839. ...result,
  6840. code: shebang + result.code
  6841. };
  6842. }
  6843. function getEsImportProxy(id, defaultIsModuleExports) {
  6844. const name = getName(id);
  6845. const exportsName = `${name}Exports`;
  6846. const requireModule = `require${capitalize(name)}`;
  6847. let code =
  6848. `import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
  6849. `import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
  6850. `var ${exportsName} = ${requireModule}();\n` +
  6851. `export { ${exportsName} as __moduleExports };`;
  6852. if (defaultIsModuleExports === true) {
  6853. code += `\nexport { ${exportsName} as default };`;
  6854. } else {
  6855. code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
  6856. }
  6857. return {
  6858. code,
  6859. syntheticNamedExports: '__moduleExports'
  6860. };
  6861. }
  6862. /* eslint-disable no-param-reassign, no-undefined */
  6863. function getCandidatesForExtension(resolved, extension) {
  6864. return [resolved + extension, `${resolved}${sep}index${extension}`];
  6865. }
  6866. function getCandidates(resolved, extensions) {
  6867. return extensions.reduce(
  6868. (paths, extension) => paths.concat(getCandidatesForExtension(resolved, extension)),
  6869. [resolved]
  6870. );
  6871. }
  6872. function resolveExtensions(importee, importer, extensions) {
  6873. // not our problem
  6874. if (importee[0] !== '.' || !importer) return undefined;
  6875. const resolved = resolve$3(dirname$1(importer), importee);
  6876. const candidates = getCandidates(resolved, extensions);
  6877. for (let i = 0; i < candidates.length; i += 1) {
  6878. try {
  6879. const stats = statSync$1(candidates[i]);
  6880. if (stats.isFile()) return { id: candidates[i] };
  6881. } catch (err) {
  6882. /* noop */
  6883. }
  6884. }
  6885. return undefined;
  6886. }
  6887. function getResolveId(extensions, isPossibleCjsId) {
  6888. const currentlyResolving = new Map();
  6889. return {
  6890. /**
  6891. * This is a Maps of importers to Sets of require sources being resolved at
  6892. * the moment by resolveRequireSourcesAndUpdateMeta
  6893. */
  6894. currentlyResolving,
  6895. async resolveId(importee, importer, resolveOptions) {
  6896. const customOptions = resolveOptions.custom;
  6897. // All logic below is specific to ES imports.
  6898. // Also, if we do not skip this logic for requires that are resolved while
  6899. // transforming a commonjs file, it can easily lead to deadlocks.
  6900. if (
  6901. customOptions &&
  6902. customOptions['node-resolve'] &&
  6903. customOptions['node-resolve'].isRequire
  6904. ) {
  6905. return null;
  6906. }
  6907. const currentlyResolvingForParent = currentlyResolving.get(importer);
  6908. if (currentlyResolvingForParent && currentlyResolvingForParent.has(importee)) {
  6909. this.warn({
  6910. code: 'THIS_RESOLVE_WITHOUT_OPTIONS',
  6911. message:
  6912. 'It appears a plugin has implemented a "resolveId" hook that uses "this.resolve" without forwarding the third "options" parameter of "resolveId". This is problematic as it can lead to wrong module resolutions especially for the node-resolve plugin and in certain cases cause early exit errors for the commonjs plugin.\nIn rare cases, this warning can appear if the same file is both imported and required from the same mixed ES/CommonJS module, in which case it can be ignored.',
  6913. url: 'https://rollupjs.org/guide/en/#resolveid'
  6914. });
  6915. return null;
  6916. }
  6917. if (isWrappedId(importee, WRAPPED_SUFFIX)) {
  6918. return unwrapId(importee, WRAPPED_SUFFIX);
  6919. }
  6920. if (
  6921. importee.endsWith(ENTRY_SUFFIX) ||
  6922. isWrappedId(importee, MODULE_SUFFIX) ||
  6923. isWrappedId(importee, EXPORTS_SUFFIX) ||
  6924. isWrappedId(importee, PROXY_SUFFIX) ||
  6925. isWrappedId(importee, ES_IMPORT_SUFFIX) ||
  6926. isWrappedId(importee, EXTERNAL_SUFFIX) ||
  6927. importee.startsWith(HELPERS_ID) ||
  6928. importee === DYNAMIC_MODULES_ID
  6929. ) {
  6930. return importee;
  6931. }
  6932. if (importer) {
  6933. if (
  6934. importer === DYNAMIC_MODULES_ID ||
  6935. // Proxies are only importing resolved ids, no need to resolve again
  6936. isWrappedId(importer, PROXY_SUFFIX) ||
  6937. isWrappedId(importer, ES_IMPORT_SUFFIX) ||
  6938. importer.endsWith(ENTRY_SUFFIX)
  6939. ) {
  6940. return importee;
  6941. }
  6942. if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
  6943. // We need to return null for unresolved imports so that the proper warning is shown
  6944. if (
  6945. !(await this.resolve(
  6946. importee,
  6947. importer,
  6948. Object.assign({ skipSelf: true }, resolveOptions)
  6949. ))
  6950. ) {
  6951. return null;
  6952. }
  6953. // For other external imports, we need to make sure they are handled as external
  6954. return { id: importee, external: true };
  6955. }
  6956. }
  6957. if (importee.startsWith('\0')) {
  6958. return null;
  6959. }
  6960. // If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
  6961. // if that is the case, we need to add a proxy.
  6962. const resolved =
  6963. (await this.resolve(
  6964. importee,
  6965. importer,
  6966. Object.assign({ skipSelf: true }, resolveOptions)
  6967. )) || resolveExtensions(importee, importer, extensions);
  6968. // Make sure that even if other plugins resolve again, we ignore our own proxies
  6969. if (
  6970. !resolved ||
  6971. resolved.external ||
  6972. resolved.id.endsWith(ENTRY_SUFFIX) ||
  6973. isWrappedId(resolved.id, ES_IMPORT_SUFFIX) ||
  6974. !isPossibleCjsId(resolved.id)
  6975. ) {
  6976. return resolved;
  6977. }
  6978. const moduleInfo = await this.load(resolved);
  6979. const {
  6980. meta: { commonjs: commonjsMeta }
  6981. } = moduleInfo;
  6982. if (commonjsMeta) {
  6983. const { isCommonJS } = commonjsMeta;
  6984. if (isCommonJS) {
  6985. if (resolveOptions.isEntry) {
  6986. moduleInfo.moduleSideEffects = true;
  6987. // We must not precede entry proxies with a `\0` as that will mess up relative external resolution
  6988. return resolved.id + ENTRY_SUFFIX;
  6989. }
  6990. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  6991. return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
  6992. }
  6993. }
  6994. }
  6995. return resolved;
  6996. }
  6997. };
  6998. }
  6999. function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
  7000. const knownCjsModuleTypes = Object.create(null);
  7001. const requiredIds = Object.create(null);
  7002. const unconditionallyRequiredIds = Object.create(null);
  7003. const dependencies = Object.create(null);
  7004. const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
  7005. const isCyclic = (id) => {
  7006. const dependenciesToCheck = new Set(getDependencies(id));
  7007. for (const dependency of dependenciesToCheck) {
  7008. if (dependency === id) {
  7009. return true;
  7010. }
  7011. for (const childDependency of getDependencies(dependency)) {
  7012. dependenciesToCheck.add(childDependency);
  7013. }
  7014. }
  7015. return false;
  7016. };
  7017. // Once a module is listed here, its type (wrapped or not) is fixed and may
  7018. // not change for the rest of the current build, to not break already
  7019. // transformed modules.
  7020. const fullyAnalyzedModules = Object.create(null);
  7021. const getTypeForFullyAnalyzedModule = (id) => {
  7022. const knownType = knownCjsModuleTypes[id];
  7023. if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
  7024. return knownType;
  7025. }
  7026. if (isCyclic(id)) {
  7027. return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
  7028. }
  7029. return knownType;
  7030. };
  7031. const setInitialParentType = (id, initialCommonJSType) => {
  7032. // Fully analyzed modules may never change type
  7033. if (fullyAnalyzedModules[id]) {
  7034. return;
  7035. }
  7036. knownCjsModuleTypes[id] = initialCommonJSType;
  7037. if (
  7038. detectCyclesAndConditional &&
  7039. knownCjsModuleTypes[id] === true &&
  7040. requiredIds[id] &&
  7041. !unconditionallyRequiredIds[id]
  7042. ) {
  7043. knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
  7044. }
  7045. };
  7046. const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
  7047. const childId = resolved.id;
  7048. requiredIds[childId] = true;
  7049. if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
  7050. unconditionallyRequiredIds[childId] = true;
  7051. }
  7052. getDependencies(parentId).add(childId);
  7053. if (!isCyclic(childId)) {
  7054. // This makes sure the current transform handler waits for all direct
  7055. // dependencies to be loaded and transformed and therefore for all
  7056. // transitive CommonJS dependencies to be loaded as well so that all
  7057. // cycles have been found and knownCjsModuleTypes is reliable.
  7058. await loadModule(resolved);
  7059. }
  7060. };
  7061. const getTypeForImportedModule = async (resolved, loadModule) => {
  7062. if (resolved.id in knownCjsModuleTypes) {
  7063. // This handles cyclic ES dependencies
  7064. return knownCjsModuleTypes[resolved.id];
  7065. }
  7066. const {
  7067. meta: { commonjs }
  7068. } = await loadModule(resolved);
  7069. return (commonjs && commonjs.isCommonJS) || false;
  7070. };
  7071. return {
  7072. getWrappedIds: () =>
  7073. Object.keys(knownCjsModuleTypes).filter(
  7074. (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
  7075. ),
  7076. isRequiredId: (id) => requiredIds[id],
  7077. async shouldTransformCachedModule({
  7078. id: parentId,
  7079. resolvedSources,
  7080. meta: { commonjs: parentMeta }
  7081. }) {
  7082. // We explicitly track ES modules to handle circular imports
  7083. if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
  7084. if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
  7085. const parentRequires = parentMeta && parentMeta.requires;
  7086. if (parentRequires) {
  7087. setInitialParentType(parentId, parentMeta.initialCommonJSType);
  7088. await Promise.all(
  7089. parentRequires.map(({ resolved, isConditional }) =>
  7090. analyzeRequiredModule(parentId, resolved, isConditional, this.load)
  7091. )
  7092. );
  7093. if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
  7094. return true;
  7095. }
  7096. for (const {
  7097. resolved: { id }
  7098. } of parentRequires) {
  7099. if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
  7100. return true;
  7101. }
  7102. }
  7103. // Now that we decided to go with the cached copy, neither the parent
  7104. // module nor any of its children may change types anymore
  7105. fullyAnalyzedModules[parentId] = true;
  7106. for (const {
  7107. resolved: { id }
  7108. } of parentRequires) {
  7109. fullyAnalyzedModules[id] = true;
  7110. }
  7111. }
  7112. const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
  7113. return (
  7114. await Promise.all(
  7115. Object.keys(resolvedSources)
  7116. .map((source) => resolvedSources[source])
  7117. .filter(({ id, external }) => !(external || parentRequireSet.has(id)))
  7118. .map(async (resolved) => {
  7119. if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
  7120. return (
  7121. (await getTypeForImportedModule(
  7122. (
  7123. await this.load({ id: resolved.id })
  7124. ).meta.commonjs.resolved,
  7125. this.load
  7126. )) !== IS_WRAPPED_COMMONJS
  7127. );
  7128. }
  7129. return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
  7130. })
  7131. )
  7132. ).some((shouldTransform) => shouldTransform);
  7133. },
  7134. /* eslint-disable no-param-reassign */
  7135. resolveRequireSourcesAndUpdateMeta:
  7136. (rollupContext) => async (parentId, isParentCommonJS, parentMeta, sources) => {
  7137. parentMeta.initialCommonJSType = isParentCommonJS;
  7138. parentMeta.requires = [];
  7139. parentMeta.isRequiredCommonJS = Object.create(null);
  7140. setInitialParentType(parentId, isParentCommonJS);
  7141. const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
  7142. currentlyResolving.set(parentId, currentlyResolvingForParent);
  7143. const requireTargets = await Promise.all(
  7144. sources.map(async ({ source, isConditional }) => {
  7145. // Never analyze or proxy internal modules
  7146. if (source.startsWith('\0')) {
  7147. return { id: source, allowProxy: false };
  7148. }
  7149. currentlyResolvingForParent.add(source);
  7150. const resolved =
  7151. (await rollupContext.resolve(source, parentId, {
  7152. skipSelf: false,
  7153. custom: { 'node-resolve': { isRequire: true } }
  7154. })) || resolveExtensions(source, parentId, extensions);
  7155. currentlyResolvingForParent.delete(source);
  7156. if (!resolved) {
  7157. return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
  7158. }
  7159. const childId = resolved.id;
  7160. if (resolved.external) {
  7161. return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
  7162. }
  7163. parentMeta.requires.push({ resolved, isConditional });
  7164. await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
  7165. return { id: childId, allowProxy: true };
  7166. })
  7167. );
  7168. parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
  7169. fullyAnalyzedModules[parentId] = true;
  7170. return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
  7171. // eslint-disable-next-line no-multi-assign
  7172. const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
  7173. getTypeForFullyAnalyzedModule(dependencyId));
  7174. fullyAnalyzedModules[dependencyId] = true;
  7175. return {
  7176. source: sources[index].source,
  7177. id: allowProxy
  7178. ? isCommonJS === IS_WRAPPED_COMMONJS
  7179. ? wrapId(dependencyId, WRAPPED_SUFFIX)
  7180. : wrapId(dependencyId, PROXY_SUFFIX)
  7181. : dependencyId,
  7182. isCommonJS
  7183. };
  7184. });
  7185. },
  7186. isCurrentlyResolving(source, parentId) {
  7187. const currentlyResolvingForParent = currentlyResolving.get(parentId);
  7188. return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
  7189. }
  7190. };
  7191. }
  7192. function validateVersion(actualVersion, peerDependencyVersion, name) {
  7193. const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
  7194. let minMajor = Infinity;
  7195. let minMinor = Infinity;
  7196. let minPatch = Infinity;
  7197. let foundVersion;
  7198. // eslint-disable-next-line no-cond-assign
  7199. while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
  7200. const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
  7201. if (foundMajor < minMajor) {
  7202. minMajor = foundMajor;
  7203. minMinor = foundMinor;
  7204. minPatch = foundPatch;
  7205. }
  7206. }
  7207. if (!actualVersion) {
  7208. throw new Error(
  7209. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
  7210. );
  7211. }
  7212. const [major, minor, patch] = actualVersion.split('.').map(Number);
  7213. if (
  7214. major < minMajor ||
  7215. (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
  7216. ) {
  7217. throw new Error(
  7218. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
  7219. );
  7220. }
  7221. }
  7222. const operators = {
  7223. '==': (x) => equals(x.left, x.right, false),
  7224. '!=': (x) => not(operators['=='](x)),
  7225. '===': (x) => equals(x.left, x.right, true),
  7226. '!==': (x) => not(operators['==='](x)),
  7227. '!': (x) => isFalsy(x.argument),
  7228. '&&': (x) => isTruthy(x.left) && isTruthy(x.right),
  7229. '||': (x) => isTruthy(x.left) || isTruthy(x.right)
  7230. };
  7231. function not(value) {
  7232. return value === null ? value : !value;
  7233. }
  7234. function equals(a, b, strict) {
  7235. if (a.type !== b.type) return null;
  7236. // eslint-disable-next-line eqeqeq
  7237. if (a.type === 'Literal') return strict ? a.value === b.value : a.value == b.value;
  7238. return null;
  7239. }
  7240. function isTruthy(node) {
  7241. if (!node) return false;
  7242. if (node.type === 'Literal') return !!node.value;
  7243. if (node.type === 'ParenthesizedExpression') return isTruthy(node.expression);
  7244. if (node.operator in operators) return operators[node.operator](node);
  7245. return null;
  7246. }
  7247. function isFalsy(node) {
  7248. return not(isTruthy(node));
  7249. }
  7250. function getKeypath(node) {
  7251. const parts = [];
  7252. while (node.type === 'MemberExpression') {
  7253. if (node.computed) return null;
  7254. parts.unshift(node.property.name);
  7255. // eslint-disable-next-line no-param-reassign
  7256. node = node.object;
  7257. }
  7258. if (node.type !== 'Identifier') return null;
  7259. const { name } = node;
  7260. parts.unshift(name);
  7261. return { name, keypath: parts.join('.') };
  7262. }
  7263. const KEY_COMPILED_ESM = '__esModule';
  7264. function getDefineCompiledEsmType(node) {
  7265. const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
  7266. const definedProperty =
  7267. definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
  7268. if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
  7269. return isTruthy(definedProperty.value)
  7270. ? definedPropertyWithExports
  7271. ? 'exports'
  7272. : 'module'
  7273. : false;
  7274. }
  7275. return false;
  7276. }
  7277. function getDefinePropertyCallName(node, targetName) {
  7278. const {
  7279. callee: { object, property }
  7280. } = node;
  7281. if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
  7282. if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
  7283. if (node.arguments.length !== 3) return;
  7284. const targetNames = targetName.split('.');
  7285. const [target, key, value] = node.arguments;
  7286. if (targetNames.length === 1) {
  7287. if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
  7288. return;
  7289. }
  7290. }
  7291. if (targetNames.length === 2) {
  7292. if (
  7293. target.type !== 'MemberExpression' ||
  7294. target.object.name !== targetNames[0] ||
  7295. target.property.name !== targetNames[1]
  7296. ) {
  7297. return;
  7298. }
  7299. }
  7300. if (value.type !== 'ObjectExpression' || !value.properties) return;
  7301. const valueProperty = value.properties.find((p) => p.key && p.key.name === 'value');
  7302. if (!valueProperty || !valueProperty.value) return;
  7303. // eslint-disable-next-line consistent-return
  7304. return { key: key.value, value: valueProperty.value };
  7305. }
  7306. function isShorthandProperty(parent) {
  7307. return parent && parent.type === 'Property' && parent.shorthand;
  7308. }
  7309. function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
  7310. const args = [];
  7311. const passedArgs = [];
  7312. if (uses.module) {
  7313. args.push('module');
  7314. passedArgs.push(moduleName);
  7315. }
  7316. if (uses.exports) {
  7317. args.push('exports');
  7318. passedArgs.push(uses.module ? `${moduleName}.exports` : exportsName);
  7319. }
  7320. magicString
  7321. .trim()
  7322. .indent('\t', { exclude: indentExclusionRanges })
  7323. .prepend(`(function (${args.join(', ')}) {\n`)
  7324. // For some reason, this line is only indented correctly when using a
  7325. // require-wrapper if we have this leading space
  7326. .append(` \n} (${passedArgs.join(', ')}));`);
  7327. }
  7328. function rewriteExportsAndGetExportsBlock(
  7329. magicString,
  7330. moduleName,
  7331. exportsName,
  7332. exportedExportsName,
  7333. wrapped,
  7334. moduleExportsAssignments,
  7335. firstTopLevelModuleExportsAssignment,
  7336. exportsAssignmentsByName,
  7337. topLevelAssignments,
  7338. defineCompiledEsmExpressions,
  7339. deconflictedExportNames,
  7340. code,
  7341. HELPERS_NAME,
  7342. exportMode,
  7343. defaultIsModuleExports,
  7344. usesRequireWrapper,
  7345. requireName
  7346. ) {
  7347. const exports = [];
  7348. const exportDeclarations = [];
  7349. if (usesRequireWrapper) {
  7350. getExportsWhenUsingRequireWrapper(
  7351. magicString,
  7352. wrapped,
  7353. exportMode,
  7354. exports,
  7355. moduleExportsAssignments,
  7356. exportsAssignmentsByName,
  7357. moduleName,
  7358. exportsName,
  7359. requireName,
  7360. defineCompiledEsmExpressions
  7361. );
  7362. } else if (exportMode === 'replace') {
  7363. getExportsForReplacedModuleExports(
  7364. magicString,
  7365. exports,
  7366. exportDeclarations,
  7367. moduleExportsAssignments,
  7368. firstTopLevelModuleExportsAssignment,
  7369. exportsName,
  7370. defaultIsModuleExports,
  7371. HELPERS_NAME
  7372. );
  7373. } else {
  7374. if (exportMode === 'module') {
  7375. exportDeclarations.push(`var ${exportedExportsName} = ${moduleName}.exports`);
  7376. exports.push(`${exportedExportsName} as __moduleExports`);
  7377. } else {
  7378. exports.push(`${exportsName} as __moduleExports`);
  7379. }
  7380. if (wrapped) {
  7381. exportDeclarations.push(
  7382. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  7383. );
  7384. } else {
  7385. getExports(
  7386. magicString,
  7387. exports,
  7388. exportDeclarations,
  7389. moduleExportsAssignments,
  7390. exportsAssignmentsByName,
  7391. deconflictedExportNames,
  7392. topLevelAssignments,
  7393. moduleName,
  7394. exportsName,
  7395. exportedExportsName,
  7396. defineCompiledEsmExpressions,
  7397. HELPERS_NAME,
  7398. defaultIsModuleExports,
  7399. exportMode
  7400. );
  7401. }
  7402. }
  7403. if (exports.length) {
  7404. exportDeclarations.push(`export { ${exports.join(', ')} }`);
  7405. }
  7406. return `\n\n${exportDeclarations.join(';\n')};`;
  7407. }
  7408. function getExportsWhenUsingRequireWrapper(
  7409. magicString,
  7410. wrapped,
  7411. exportMode,
  7412. exports,
  7413. moduleExportsAssignments,
  7414. exportsAssignmentsByName,
  7415. moduleName,
  7416. exportsName,
  7417. requireName,
  7418. defineCompiledEsmExpressions
  7419. ) {
  7420. exports.push(`${requireName} as __require`);
  7421. if (wrapped) return;
  7422. if (exportMode === 'replace') {
  7423. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName);
  7424. } else {
  7425. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, `${moduleName}.exports`);
  7426. // Collect and rewrite named exports
  7427. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  7428. for (const { node, type } of nodes) {
  7429. magicString.overwrite(
  7430. node.start,
  7431. node.left.end,
  7432. `${
  7433. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  7434. }.${exportName}`
  7435. );
  7436. }
  7437. }
  7438. replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  7439. defineCompiledEsmExpressions,
  7440. magicString,
  7441. exportMode,
  7442. moduleName,
  7443. exportsName
  7444. );
  7445. }
  7446. }
  7447. function getExportsForReplacedModuleExports(
  7448. magicString,
  7449. exports,
  7450. exportDeclarations,
  7451. moduleExportsAssignments,
  7452. firstTopLevelModuleExportsAssignment,
  7453. exportsName,
  7454. defaultIsModuleExports,
  7455. HELPERS_NAME
  7456. ) {
  7457. for (const { left } of moduleExportsAssignments) {
  7458. magicString.overwrite(left.start, left.end, exportsName);
  7459. }
  7460. magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
  7461. exports.push(`${exportsName} as __moduleExports`);
  7462. exportDeclarations.push(
  7463. getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
  7464. );
  7465. }
  7466. function getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME) {
  7467. return `export default ${
  7468. defaultIsModuleExports === true
  7469. ? exportedExportsName
  7470. : defaultIsModuleExports === false
  7471. ? `${exportedExportsName}.default`
  7472. : `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportedExportsName})`
  7473. }`;
  7474. }
  7475. function getExports(
  7476. magicString,
  7477. exports,
  7478. exportDeclarations,
  7479. moduleExportsAssignments,
  7480. exportsAssignmentsByName,
  7481. deconflictedExportNames,
  7482. topLevelAssignments,
  7483. moduleName,
  7484. exportsName,
  7485. exportedExportsName,
  7486. defineCompiledEsmExpressions,
  7487. HELPERS_NAME,
  7488. defaultIsModuleExports,
  7489. exportMode
  7490. ) {
  7491. let deconflictedDefaultExportName;
  7492. // Collect and rewrite module.exports assignments
  7493. for (const { left } of moduleExportsAssignments) {
  7494. magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
  7495. }
  7496. // Collect and rewrite named exports
  7497. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  7498. const deconflicted = deconflictedExportNames[exportName];
  7499. let needsDeclaration = true;
  7500. for (const { node, type } of nodes) {
  7501. let replacement = `${deconflicted} = ${
  7502. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  7503. }.${exportName}`;
  7504. if (needsDeclaration && topLevelAssignments.has(node)) {
  7505. replacement = `var ${replacement}`;
  7506. needsDeclaration = false;
  7507. }
  7508. magicString.overwrite(node.start, node.left.end, replacement);
  7509. }
  7510. if (needsDeclaration) {
  7511. magicString.prepend(`var ${deconflicted};\n`);
  7512. }
  7513. if (exportName === 'default') {
  7514. deconflictedDefaultExportName = deconflicted;
  7515. } else {
  7516. exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
  7517. }
  7518. }
  7519. const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  7520. defineCompiledEsmExpressions,
  7521. magicString,
  7522. exportMode,
  7523. moduleName,
  7524. exportsName
  7525. );
  7526. if (
  7527. defaultIsModuleExports === false ||
  7528. (defaultIsModuleExports === 'auto' &&
  7529. isRestorableCompiledEsm &&
  7530. moduleExportsAssignments.length === 0)
  7531. ) {
  7532. // If there is no deconflictedDefaultExportName, then we use the namespace as
  7533. // fallback because there can be no "default" property on the namespace
  7534. exports.push(`${deconflictedDefaultExportName || exportedExportsName} as default`);
  7535. } else if (
  7536. defaultIsModuleExports === true ||
  7537. (!isRestorableCompiledEsm && moduleExportsAssignments.length === 0)
  7538. ) {
  7539. exports.push(`${exportedExportsName} as default`);
  7540. } else {
  7541. exportDeclarations.push(
  7542. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  7543. );
  7544. }
  7545. }
  7546. function rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName) {
  7547. for (const { left } of moduleExportsAssignments) {
  7548. magicString.overwrite(left.start, left.end, exportsName);
  7549. }
  7550. }
  7551. function replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  7552. defineCompiledEsmExpressions,
  7553. magicString,
  7554. exportMode,
  7555. moduleName,
  7556. exportsName
  7557. ) {
  7558. let isRestorableCompiledEsm = false;
  7559. for (const { node, type } of defineCompiledEsmExpressions) {
  7560. isRestorableCompiledEsm = true;
  7561. const moduleExportsExpression =
  7562. node.type === 'CallExpression' ? node.arguments[0] : node.left.object;
  7563. magicString.overwrite(
  7564. moduleExportsExpression.start,
  7565. moduleExportsExpression.end,
  7566. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  7567. );
  7568. }
  7569. return isRestorableCompiledEsm;
  7570. }
  7571. function isRequireExpression(node, scope) {
  7572. if (!node) return false;
  7573. if (node.type !== 'CallExpression') return false;
  7574. // Weird case of `require()` or `module.require()` without arguments
  7575. if (node.arguments.length === 0) return false;
  7576. return isRequire(node.callee, scope);
  7577. }
  7578. function isRequire(node, scope) {
  7579. return (
  7580. (node.type === 'Identifier' && node.name === 'require' && !scope.contains('require')) ||
  7581. (node.type === 'MemberExpression' && isModuleRequire(node, scope))
  7582. );
  7583. }
  7584. function isModuleRequire({ object, property }, scope) {
  7585. return (
  7586. object.type === 'Identifier' &&
  7587. object.name === 'module' &&
  7588. property.type === 'Identifier' &&
  7589. property.name === 'require' &&
  7590. !scope.contains('module')
  7591. );
  7592. }
  7593. function hasDynamicArguments(node) {
  7594. return (
  7595. node.arguments.length > 1 ||
  7596. (node.arguments[0].type !== 'Literal' &&
  7597. (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0))
  7598. );
  7599. }
  7600. const reservedMethod = { resolve: true, cache: true, main: true };
  7601. function isNodeRequirePropertyAccess(parent) {
  7602. return parent && parent.property && reservedMethod[parent.property.name];
  7603. }
  7604. function getRequireStringArg(node) {
  7605. return node.arguments[0].type === 'Literal'
  7606. ? node.arguments[0].value
  7607. : node.arguments[0].quasis[0].value.cooked;
  7608. }
  7609. function getRequireHandlers() {
  7610. const requireExpressions = [];
  7611. function addRequireExpression(
  7612. sourceId,
  7613. node,
  7614. scope,
  7615. usesReturnValue,
  7616. isInsideTryBlock,
  7617. isInsideConditional,
  7618. toBeRemoved
  7619. ) {
  7620. requireExpressions.push({
  7621. sourceId,
  7622. node,
  7623. scope,
  7624. usesReturnValue,
  7625. isInsideTryBlock,
  7626. isInsideConditional,
  7627. toBeRemoved
  7628. });
  7629. }
  7630. async function rewriteRequireExpressionsAndGetImportBlock(
  7631. magicString,
  7632. topLevelDeclarations,
  7633. reassignedNames,
  7634. helpersName,
  7635. dynamicRequireName,
  7636. moduleName,
  7637. exportsName,
  7638. id,
  7639. exportMode,
  7640. resolveRequireSourcesAndUpdateMeta,
  7641. needsRequireWrapper,
  7642. isEsModule,
  7643. isDynamicRequireModulesEnabled,
  7644. getIgnoreTryCatchRequireStatementMode,
  7645. commonjsMeta
  7646. ) {
  7647. const imports = [];
  7648. imports.push(`import * as ${helpersName} from "${HELPERS_ID}"`);
  7649. if (dynamicRequireName) {
  7650. imports.push(
  7651. `import { ${
  7652. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  7653. } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"`
  7654. );
  7655. }
  7656. if (exportMode === 'module') {
  7657. imports.push(
  7658. `import { __module as ${moduleName} } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`,
  7659. `var ${exportsName} = ${moduleName}.exports`
  7660. );
  7661. } else if (exportMode === 'exports') {
  7662. imports.push(
  7663. `import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
  7664. );
  7665. }
  7666. const requiresBySource = collectSources(requireExpressions);
  7667. const requireTargets = await resolveRequireSourcesAndUpdateMeta(
  7668. id,
  7669. needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
  7670. commonjsMeta,
  7671. Object.keys(requiresBySource).map((source) => {
  7672. return {
  7673. source,
  7674. isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
  7675. };
  7676. })
  7677. );
  7678. processRequireExpressions(
  7679. imports,
  7680. requireTargets,
  7681. requiresBySource,
  7682. getIgnoreTryCatchRequireStatementMode,
  7683. magicString
  7684. );
  7685. return imports.length ? `${imports.join(';\n')};\n\n` : '';
  7686. }
  7687. return {
  7688. addRequireExpression,
  7689. rewriteRequireExpressionsAndGetImportBlock
  7690. };
  7691. }
  7692. function collectSources(requireExpressions) {
  7693. const requiresBySource = Object.create(null);
  7694. for (const requireExpression of requireExpressions) {
  7695. const { sourceId } = requireExpression;
  7696. if (!requiresBySource[sourceId]) {
  7697. requiresBySource[sourceId] = [];
  7698. }
  7699. const requires = requiresBySource[sourceId];
  7700. requires.push(requireExpression);
  7701. }
  7702. return requiresBySource;
  7703. }
  7704. function processRequireExpressions(
  7705. imports,
  7706. requireTargets,
  7707. requiresBySource,
  7708. getIgnoreTryCatchRequireStatementMode,
  7709. magicString
  7710. ) {
  7711. const generateRequireName = getGenerateRequireName();
  7712. for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
  7713. const requires = requiresBySource[source];
  7714. const name = generateRequireName(requires);
  7715. let usesRequired = false;
  7716. let needsImport = false;
  7717. for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
  7718. const { canConvertRequire, shouldRemoveRequire } =
  7719. isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
  7720. ? getIgnoreTryCatchRequireStatementMode(source)
  7721. : { canConvertRequire: true, shouldRemoveRequire: false };
  7722. if (shouldRemoveRequire) {
  7723. if (usesReturnValue) {
  7724. magicString.overwrite(node.start, node.end, 'undefined');
  7725. } else {
  7726. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  7727. }
  7728. } else if (canConvertRequire) {
  7729. needsImport = true;
  7730. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  7731. magicString.overwrite(node.start, node.end, `${name}()`);
  7732. } else if (usesReturnValue) {
  7733. usesRequired = true;
  7734. magicString.overwrite(node.start, node.end, name);
  7735. } else {
  7736. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  7737. }
  7738. }
  7739. }
  7740. if (needsImport) {
  7741. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  7742. imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}`);
  7743. } else {
  7744. imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}`);
  7745. }
  7746. }
  7747. }
  7748. }
  7749. function getGenerateRequireName() {
  7750. let uid = 0;
  7751. return (requires) => {
  7752. let name;
  7753. const hasNameConflict = ({ scope }) => scope.contains(name);
  7754. do {
  7755. name = `require$$${uid}`;
  7756. uid += 1;
  7757. } while (requires.some(hasNameConflict));
  7758. return name;
  7759. };
  7760. }
  7761. /* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
  7762. const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
  7763. const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
  7764. // There are three different types of CommonJS modules, described by their
  7765. // "exportMode":
  7766. // - exports: Only assignments to (module.)exports properties
  7767. // - replace: A single assignment to module.exports itself
  7768. // - module: Anything else
  7769. // Special cases:
  7770. // - usesRequireWrapper
  7771. // - isWrapped
  7772. async function transformCommonjs(
  7773. parse,
  7774. code,
  7775. id,
  7776. isEsModule,
  7777. ignoreGlobal,
  7778. ignoreRequire,
  7779. ignoreDynamicRequires,
  7780. getIgnoreTryCatchRequireStatementMode,
  7781. sourceMap,
  7782. isDynamicRequireModulesEnabled,
  7783. dynamicRequireModules,
  7784. commonDir,
  7785. astCache,
  7786. defaultIsModuleExports,
  7787. needsRequireWrapper,
  7788. resolveRequireSourcesAndUpdateMeta,
  7789. isRequired,
  7790. checkDynamicRequire,
  7791. commonjsMeta
  7792. ) {
  7793. const ast = astCache || tryParse(parse, code, id);
  7794. const magicString = new MagicString(code);
  7795. const uses = {
  7796. module: false,
  7797. exports: false,
  7798. global: false,
  7799. require: false
  7800. };
  7801. const virtualDynamicRequirePath =
  7802. isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname$1(id), commonDir);
  7803. let scope = attachScopes(ast, 'scope');
  7804. let lexicalDepth = 0;
  7805. let programDepth = 0;
  7806. let classBodyDepth = 0;
  7807. let currentTryBlockEnd = null;
  7808. let shouldWrap = false;
  7809. const globals = new Set();
  7810. // A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
  7811. // or contains nested requires, those should be handled as function calls unless there is an
  7812. // unconditional require elsewhere.
  7813. let currentConditionalNodeEnd = null;
  7814. const conditionalNodes = new Set();
  7815. const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
  7816. // See which names are assigned to. This is necessary to prevent
  7817. // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
  7818. // where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
  7819. const reassignedNames = new Set();
  7820. const topLevelDeclarations = [];
  7821. const skippedNodes = new Set();
  7822. const moduleAccessScopes = new Set([scope]);
  7823. const exportsAccessScopes = new Set([scope]);
  7824. const moduleExportsAssignments = [];
  7825. let firstTopLevelModuleExportsAssignment = null;
  7826. const exportsAssignmentsByName = new Map();
  7827. const topLevelAssignments = new Set();
  7828. const topLevelDefineCompiledEsmExpressions = [];
  7829. const replacedGlobal = [];
  7830. const replacedDynamicRequires = [];
  7831. const importedVariables = new Set();
  7832. const indentExclusionRanges = [];
  7833. walk$3(ast, {
  7834. enter(node, parent) {
  7835. if (skippedNodes.has(node)) {
  7836. this.skip();
  7837. return;
  7838. }
  7839. if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
  7840. currentTryBlockEnd = null;
  7841. }
  7842. if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
  7843. currentConditionalNodeEnd = null;
  7844. }
  7845. if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
  7846. currentConditionalNodeEnd = node.end;
  7847. }
  7848. programDepth += 1;
  7849. if (node.scope) ({ scope } = node);
  7850. if (functionType.test(node.type)) lexicalDepth += 1;
  7851. if (sourceMap) {
  7852. magicString.addSourcemapLocation(node.start);
  7853. magicString.addSourcemapLocation(node.end);
  7854. }
  7855. // eslint-disable-next-line default-case
  7856. switch (node.type) {
  7857. case 'AssignmentExpression':
  7858. if (node.left.type === 'MemberExpression') {
  7859. const flattened = getKeypath(node.left);
  7860. if (!flattened || scope.contains(flattened.name)) return;
  7861. const exportsPatternMatch = exportsPattern.exec(flattened.keypath);
  7862. if (!exportsPatternMatch || flattened.keypath === 'exports') return;
  7863. const [, exportName] = exportsPatternMatch;
  7864. uses[flattened.name] = true;
  7865. // we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
  7866. if (flattened.keypath === 'module.exports') {
  7867. moduleExportsAssignments.push(node);
  7868. if (programDepth > 3) {
  7869. moduleAccessScopes.add(scope);
  7870. } else if (!firstTopLevelModuleExportsAssignment) {
  7871. firstTopLevelModuleExportsAssignment = node;
  7872. }
  7873. } else if (exportName === KEY_COMPILED_ESM) {
  7874. if (programDepth > 3) {
  7875. shouldWrap = true;
  7876. } else {
  7877. // The "type" is either "module" or "exports" to discern
  7878. // assignments to module.exports vs exports if needed
  7879. topLevelDefineCompiledEsmExpressions.push({ node, type: flattened.name });
  7880. }
  7881. } else {
  7882. const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
  7883. nodes: [],
  7884. scopes: new Set()
  7885. };
  7886. exportsAssignments.nodes.push({ node, type: flattened.name });
  7887. exportsAssignments.scopes.add(scope);
  7888. exportsAccessScopes.add(scope);
  7889. exportsAssignmentsByName.set(exportName, exportsAssignments);
  7890. if (programDepth <= 3) {
  7891. topLevelAssignments.add(node);
  7892. }
  7893. }
  7894. skippedNodes.add(node.left);
  7895. } else {
  7896. for (const name of extractAssignedNames(node.left)) {
  7897. reassignedNames.add(name);
  7898. }
  7899. }
  7900. return;
  7901. case 'CallExpression': {
  7902. const defineCompiledEsmType = getDefineCompiledEsmType(node);
  7903. if (defineCompiledEsmType) {
  7904. if (programDepth === 3 && parent.type === 'ExpressionStatement') {
  7905. // skip special handling for [module.]exports until we know we render this
  7906. skippedNodes.add(node.arguments[0]);
  7907. topLevelDefineCompiledEsmExpressions.push({ node, type: defineCompiledEsmType });
  7908. } else {
  7909. shouldWrap = true;
  7910. }
  7911. return;
  7912. }
  7913. // Transform require.resolve
  7914. if (
  7915. isDynamicRequireModulesEnabled &&
  7916. node.callee.object &&
  7917. isRequire(node.callee.object, scope) &&
  7918. node.callee.property.name === 'resolve'
  7919. ) {
  7920. checkDynamicRequire(node.start);
  7921. uses.require = true;
  7922. const requireNode = node.callee.object;
  7923. replacedDynamicRequires.push(requireNode);
  7924. skippedNodes.add(node.callee);
  7925. return;
  7926. }
  7927. if (!isRequireExpression(node, scope)) {
  7928. const keypath = getKeypath(node.callee);
  7929. if (keypath && importedVariables.has(keypath.name)) {
  7930. // Heuristic to deoptimize requires after a required function has been called
  7931. currentConditionalNodeEnd = Infinity;
  7932. }
  7933. return;
  7934. }
  7935. skippedNodes.add(node.callee);
  7936. uses.require = true;
  7937. if (hasDynamicArguments(node)) {
  7938. if (isDynamicRequireModulesEnabled) {
  7939. checkDynamicRequire(node.start);
  7940. }
  7941. if (!ignoreDynamicRequires) {
  7942. replacedDynamicRequires.push(node.callee);
  7943. }
  7944. return;
  7945. }
  7946. const requireStringArg = getRequireStringArg(node);
  7947. if (!ignoreRequire(requireStringArg)) {
  7948. const usesReturnValue = parent.type !== 'ExpressionStatement';
  7949. const toBeRemoved =
  7950. parent.type === 'ExpressionStatement' &&
  7951. (!currentConditionalNodeEnd ||
  7952. // We should completely remove requires directly in a try-catch
  7953. // so that Rollup can remove up the try-catch
  7954. (currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
  7955. ? parent
  7956. : node;
  7957. addRequireExpression(
  7958. requireStringArg,
  7959. node,
  7960. scope,
  7961. usesReturnValue,
  7962. currentTryBlockEnd !== null,
  7963. currentConditionalNodeEnd !== null,
  7964. toBeRemoved
  7965. );
  7966. if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
  7967. for (const name of extractAssignedNames(parent.id)) {
  7968. importedVariables.add(name);
  7969. }
  7970. }
  7971. }
  7972. return;
  7973. }
  7974. case 'ClassBody':
  7975. classBodyDepth += 1;
  7976. return;
  7977. case 'ConditionalExpression':
  7978. case 'IfStatement':
  7979. // skip dead branches
  7980. if (isFalsy(node.test)) {
  7981. skippedNodes.add(node.consequent);
  7982. } else if (isTruthy(node.test)) {
  7983. if (node.alternate) {
  7984. skippedNodes.add(node.alternate);
  7985. }
  7986. } else {
  7987. conditionalNodes.add(node.consequent);
  7988. if (node.alternate) {
  7989. conditionalNodes.add(node.alternate);
  7990. }
  7991. }
  7992. return;
  7993. case 'ArrowFunctionExpression':
  7994. case 'FunctionDeclaration':
  7995. case 'FunctionExpression':
  7996. // requires in functions should be conditional unless it is an IIFE
  7997. if (
  7998. currentConditionalNodeEnd === null &&
  7999. !(parent.type === 'CallExpression' && parent.callee === node)
  8000. ) {
  8001. currentConditionalNodeEnd = node.end;
  8002. }
  8003. return;
  8004. case 'Identifier': {
  8005. const { name } = node;
  8006. if (!isReference(node, parent) || scope.contains(name)) return;
  8007. switch (name) {
  8008. case 'require':
  8009. uses.require = true;
  8010. if (isNodeRequirePropertyAccess(parent)) {
  8011. return;
  8012. }
  8013. if (!ignoreDynamicRequires) {
  8014. if (isShorthandProperty(parent)) {
  8015. // as key and value are the same object, isReference regards
  8016. // both as references, so we need to skip now
  8017. skippedNodes.add(parent.value);
  8018. magicString.prependRight(node.start, 'require: ');
  8019. }
  8020. replacedDynamicRequires.push(node);
  8021. }
  8022. return;
  8023. case 'module':
  8024. case 'exports':
  8025. shouldWrap = true;
  8026. uses[name] = true;
  8027. return;
  8028. case 'global':
  8029. uses.global = true;
  8030. if (!ignoreGlobal) {
  8031. replacedGlobal.push(node);
  8032. }
  8033. return;
  8034. case 'define':
  8035. magicString.overwrite(node.start, node.end, 'undefined', {
  8036. storeName: true
  8037. });
  8038. return;
  8039. default:
  8040. globals.add(name);
  8041. return;
  8042. }
  8043. }
  8044. case 'LogicalExpression':
  8045. // skip dead branches
  8046. if (node.operator === '&&') {
  8047. if (isFalsy(node.left)) {
  8048. skippedNodes.add(node.right);
  8049. } else if (!isTruthy(node.left)) {
  8050. conditionalNodes.add(node.right);
  8051. }
  8052. } else if (node.operator === '||') {
  8053. if (isTruthy(node.left)) {
  8054. skippedNodes.add(node.right);
  8055. } else if (!isFalsy(node.left)) {
  8056. conditionalNodes.add(node.right);
  8057. }
  8058. }
  8059. return;
  8060. case 'MemberExpression':
  8061. if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
  8062. uses.require = true;
  8063. replacedDynamicRequires.push(node);
  8064. skippedNodes.add(node.object);
  8065. skippedNodes.add(node.property);
  8066. }
  8067. return;
  8068. case 'ReturnStatement':
  8069. // if top-level return, we need to wrap it
  8070. if (lexicalDepth === 0) {
  8071. shouldWrap = true;
  8072. }
  8073. return;
  8074. case 'ThisExpression':
  8075. // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal`
  8076. if (lexicalDepth === 0 && !classBodyDepth) {
  8077. uses.global = true;
  8078. if (!ignoreGlobal) {
  8079. replacedGlobal.push(node);
  8080. }
  8081. }
  8082. return;
  8083. case 'TryStatement':
  8084. if (currentTryBlockEnd === null) {
  8085. currentTryBlockEnd = node.block.end;
  8086. }
  8087. if (currentConditionalNodeEnd === null) {
  8088. currentConditionalNodeEnd = node.end;
  8089. }
  8090. return;
  8091. case 'UnaryExpression':
  8092. // rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
  8093. if (node.operator === 'typeof') {
  8094. const flattened = getKeypath(node.argument);
  8095. if (!flattened) return;
  8096. if (scope.contains(flattened.name)) return;
  8097. if (
  8098. !isEsModule &&
  8099. (flattened.keypath === 'module.exports' ||
  8100. flattened.keypath === 'module' ||
  8101. flattened.keypath === 'exports')
  8102. ) {
  8103. magicString.overwrite(node.start, node.end, `'object'`, {
  8104. storeName: false
  8105. });
  8106. }
  8107. }
  8108. return;
  8109. case 'VariableDeclaration':
  8110. if (!scope.parent) {
  8111. topLevelDeclarations.push(node);
  8112. }
  8113. return;
  8114. case 'TemplateElement':
  8115. if (node.value.raw.includes('\n')) {
  8116. indentExclusionRanges.push([node.start, node.end]);
  8117. }
  8118. }
  8119. },
  8120. leave(node) {
  8121. programDepth -= 1;
  8122. if (node.scope) scope = scope.parent;
  8123. if (functionType.test(node.type)) lexicalDepth -= 1;
  8124. if (node.type === 'ClassBody') classBodyDepth -= 1;
  8125. }
  8126. });
  8127. const nameBase = getName(id);
  8128. const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
  8129. const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
  8130. const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
  8131. const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
  8132. const helpersName = deconflict([scope], globals, 'commonjsHelpers');
  8133. const dynamicRequireName =
  8134. replacedDynamicRequires.length > 0 &&
  8135. deconflict(
  8136. [scope],
  8137. globals,
  8138. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  8139. );
  8140. const deconflictedExportNames = Object.create(null);
  8141. for (const [exportName, { scopes }] of exportsAssignmentsByName) {
  8142. deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
  8143. }
  8144. for (const node of replacedGlobal) {
  8145. magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
  8146. storeName: true
  8147. });
  8148. }
  8149. for (const node of replacedDynamicRequires) {
  8150. magicString.overwrite(
  8151. node.start,
  8152. node.end,
  8153. isDynamicRequireModulesEnabled
  8154. ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
  8155. : dynamicRequireName,
  8156. {
  8157. contentOnly: true,
  8158. storeName: true
  8159. }
  8160. );
  8161. }
  8162. // We cannot wrap ES/mixed modules
  8163. shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
  8164. if (
  8165. !(
  8166. shouldWrap ||
  8167. isRequired ||
  8168. needsRequireWrapper ||
  8169. uses.module ||
  8170. uses.exports ||
  8171. uses.require ||
  8172. topLevelDefineCompiledEsmExpressions.length > 0
  8173. ) &&
  8174. (ignoreGlobal || !uses.global)
  8175. ) {
  8176. return { meta: { commonjs: { isCommonJS: false } } };
  8177. }
  8178. let leadingComment = '';
  8179. if (code.startsWith('/*')) {
  8180. const commentEnd = code.indexOf('*/', 2) + 2;
  8181. leadingComment = `${code.slice(0, commentEnd)}\n`;
  8182. magicString.remove(0, commentEnd).trim();
  8183. }
  8184. let shebang = '';
  8185. if (code.startsWith('#!')) {
  8186. const shebangEndPosition = code.indexOf('\n') + 1;
  8187. shebang = code.slice(0, shebangEndPosition);
  8188. magicString.remove(0, shebangEndPosition).trim();
  8189. }
  8190. const exportMode = isEsModule
  8191. ? 'none'
  8192. : shouldWrap
  8193. ? uses.module
  8194. ? 'module'
  8195. : 'exports'
  8196. : firstTopLevelModuleExportsAssignment
  8197. ? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
  8198. ? 'replace'
  8199. : 'module'
  8200. : moduleExportsAssignments.length === 0
  8201. ? 'exports'
  8202. : 'module';
  8203. const exportedExportsName =
  8204. exportMode === 'module' ? deconflict([], globals, `${nameBase}Exports`) : exportsName;
  8205. const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
  8206. magicString,
  8207. topLevelDeclarations,
  8208. reassignedNames,
  8209. helpersName,
  8210. dynamicRequireName,
  8211. moduleName,
  8212. exportsName,
  8213. id,
  8214. exportMode,
  8215. resolveRequireSourcesAndUpdateMeta,
  8216. needsRequireWrapper,
  8217. isEsModule,
  8218. isDynamicRequireModulesEnabled,
  8219. getIgnoreTryCatchRequireStatementMode,
  8220. commonjsMeta
  8221. );
  8222. const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
  8223. const exportBlock = isEsModule
  8224. ? ''
  8225. : rewriteExportsAndGetExportsBlock(
  8226. magicString,
  8227. moduleName,
  8228. exportsName,
  8229. exportedExportsName,
  8230. shouldWrap,
  8231. moduleExportsAssignments,
  8232. firstTopLevelModuleExportsAssignment,
  8233. exportsAssignmentsByName,
  8234. topLevelAssignments,
  8235. topLevelDefineCompiledEsmExpressions,
  8236. deconflictedExportNames,
  8237. code,
  8238. helpersName,
  8239. exportMode,
  8240. defaultIsModuleExports,
  8241. usesRequireWrapper,
  8242. requireName
  8243. );
  8244. if (shouldWrap) {
  8245. wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges);
  8246. }
  8247. if (usesRequireWrapper) {
  8248. magicString.trim().indent('\t', {
  8249. exclude: indentExclusionRanges
  8250. });
  8251. const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
  8252. magicString.prepend(
  8253. `var ${isRequiredName};
  8254. function ${requireName} () {
  8255. \tif (${isRequiredName}) return ${exported};
  8256. \t${isRequiredName} = 1;
  8257. `
  8258. ).append(`
  8259. \treturn ${exported};
  8260. }`);
  8261. if (exportMode === 'replace') {
  8262. magicString.prepend(`var ${exportsName};\n`);
  8263. }
  8264. }
  8265. magicString
  8266. .trim()
  8267. .prepend(shebang + leadingComment + importBlock)
  8268. .append(exportBlock);
  8269. return {
  8270. code: magicString.toString(),
  8271. map: sourceMap ? magicString.generateMap() : null,
  8272. syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
  8273. meta: { commonjs: { ...commonjsMeta, shebang } }
  8274. };
  8275. }
  8276. const PLUGIN_NAME = 'commonjs';
  8277. function commonjs(options = {}) {
  8278. const {
  8279. ignoreGlobal,
  8280. ignoreDynamicRequires,
  8281. requireReturnsDefault: requireReturnsDefaultOption,
  8282. defaultIsModuleExports: defaultIsModuleExportsOption,
  8283. esmExternals
  8284. } = options;
  8285. const extensions = options.extensions || ['.js'];
  8286. const filter = createFilter$1(options.include, options.exclude);
  8287. const isPossibleCjsId = (id) => {
  8288. const extName = extname(id);
  8289. return extName === '.cjs' || (extensions.includes(extName) && filter(id));
  8290. };
  8291. const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
  8292. const getRequireReturnsDefault =
  8293. typeof requireReturnsDefaultOption === 'function'
  8294. ? requireReturnsDefaultOption
  8295. : () => requireReturnsDefaultOption;
  8296. let esmExternalIds;
  8297. const isEsmExternal =
  8298. typeof esmExternals === 'function'
  8299. ? esmExternals
  8300. : Array.isArray(esmExternals)
  8301. ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
  8302. : () => esmExternals;
  8303. const getDefaultIsModuleExports =
  8304. typeof defaultIsModuleExportsOption === 'function'
  8305. ? defaultIsModuleExportsOption
  8306. : () =>
  8307. typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
  8308. const dynamicRequireRoot =
  8309. typeof options.dynamicRequireRoot === 'string'
  8310. ? resolve$3(options.dynamicRequireRoot)
  8311. : process.cwd();
  8312. const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
  8313. options.dynamicRequireTargets,
  8314. dynamicRequireRoot
  8315. );
  8316. const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
  8317. const ignoreRequire =
  8318. typeof options.ignore === 'function'
  8319. ? options.ignore
  8320. : Array.isArray(options.ignore)
  8321. ? (id) => options.ignore.includes(id)
  8322. : () => false;
  8323. const getIgnoreTryCatchRequireStatementMode = (id) => {
  8324. const mode =
  8325. typeof options.ignoreTryCatch === 'function'
  8326. ? options.ignoreTryCatch(id)
  8327. : Array.isArray(options.ignoreTryCatch)
  8328. ? options.ignoreTryCatch.includes(id)
  8329. : typeof options.ignoreTryCatch !== 'undefined'
  8330. ? options.ignoreTryCatch
  8331. : true;
  8332. return {
  8333. canConvertRequire: mode !== 'remove' && mode !== true,
  8334. shouldRemoveRequire: mode === 'remove'
  8335. };
  8336. };
  8337. const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
  8338. const sourceMap = options.sourceMap !== false;
  8339. // Initialized in buildStart
  8340. let requireResolver;
  8341. function transformAndCheckExports(code, id) {
  8342. const normalizedId = normalizePathSlashes(id);
  8343. const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
  8344. this.parse,
  8345. code,
  8346. id
  8347. );
  8348. const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
  8349. if (hasDefaultExport) {
  8350. commonjsMeta.hasDefaultExport = true;
  8351. }
  8352. if (hasNamedExports) {
  8353. commonjsMeta.hasNamedExports = true;
  8354. }
  8355. if (
  8356. !dynamicRequireModules.has(normalizedId) &&
  8357. (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
  8358. (isEsModule && !options.transformMixedEsModules))
  8359. ) {
  8360. commonjsMeta.isCommonJS = false;
  8361. return { meta: { commonjs: commonjsMeta } };
  8362. }
  8363. const needsRequireWrapper =
  8364. !isEsModule && (dynamicRequireModules.has(normalizedId) || strictRequiresFilter(id));
  8365. const checkDynamicRequire = (position) => {
  8366. const normalizedDynamicRequireRoot = normalizePathSlashes(dynamicRequireRoot);
  8367. if (normalizedId.indexOf(normalizedDynamicRequireRoot) !== 0) {
  8368. this.error(
  8369. {
  8370. code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
  8371. normalizedId,
  8372. normalizedDynamicRequireRoot,
  8373. message: `"${normalizedId}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${normalizedDynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname$1(
  8374. normalizedId
  8375. )}" or one of its parent directories.`
  8376. },
  8377. position
  8378. );
  8379. }
  8380. };
  8381. return transformCommonjs(
  8382. this.parse,
  8383. code,
  8384. id,
  8385. isEsModule,
  8386. ignoreGlobal || isEsModule,
  8387. ignoreRequire,
  8388. ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
  8389. getIgnoreTryCatchRequireStatementMode,
  8390. sourceMap,
  8391. isDynamicRequireModulesEnabled,
  8392. dynamicRequireModules,
  8393. commonDir,
  8394. ast,
  8395. getDefaultIsModuleExports(id),
  8396. needsRequireWrapper,
  8397. requireResolver.resolveRequireSourcesAndUpdateMeta(this),
  8398. requireResolver.isRequiredId(id),
  8399. checkDynamicRequire,
  8400. commonjsMeta
  8401. );
  8402. }
  8403. return {
  8404. name: PLUGIN_NAME,
  8405. version: version$3,
  8406. options(rawOptions) {
  8407. // We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
  8408. // do not prevent our plugin from resolving entry points ot proxies.
  8409. const plugins = Array.isArray(rawOptions.plugins)
  8410. ? [...rawOptions.plugins]
  8411. : rawOptions.plugins
  8412. ? [rawOptions.plugins]
  8413. : [];
  8414. plugins.unshift({
  8415. name: 'commonjs--resolver',
  8416. resolveId
  8417. });
  8418. return { ...rawOptions, plugins };
  8419. },
  8420. buildStart({ plugins }) {
  8421. validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
  8422. const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
  8423. if (nodeResolve) {
  8424. validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
  8425. }
  8426. if (options.namedExports != null) {
  8427. this.warn(
  8428. 'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
  8429. );
  8430. }
  8431. requireResolver = getRequireResolver(
  8432. extensions,
  8433. detectCyclesAndConditional,
  8434. currentlyResolving
  8435. );
  8436. },
  8437. buildEnd() {
  8438. if (options.strictRequires === 'debug') {
  8439. const wrappedIds = requireResolver.getWrappedIds();
  8440. if (wrappedIds.length) {
  8441. this.warn({
  8442. code: 'WRAPPED_IDS',
  8443. ids: wrappedIds,
  8444. message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
  8445. .map((id) => `\t${JSON.stringify(relative$1(process.cwd(), id))}`)
  8446. .join(',\n')}\n]`
  8447. });
  8448. } else {
  8449. this.warn({
  8450. code: 'WRAPPED_IDS',
  8451. ids: wrappedIds,
  8452. message: 'The commonjs plugin did not wrap any files.'
  8453. });
  8454. }
  8455. }
  8456. },
  8457. load(id) {
  8458. if (id === HELPERS_ID) {
  8459. return getHelpersModule();
  8460. }
  8461. if (isWrappedId(id, MODULE_SUFFIX)) {
  8462. const name = getName(unwrapId(id, MODULE_SUFFIX));
  8463. return {
  8464. code: `var ${name} = {exports: {}}; export {${name} as __module}`,
  8465. meta: { commonjs: { isCommonJS: false } }
  8466. };
  8467. }
  8468. if (isWrappedId(id, EXPORTS_SUFFIX)) {
  8469. const name = getName(unwrapId(id, EXPORTS_SUFFIX));
  8470. return {
  8471. code: `var ${name} = {}; export {${name} as __exports}`,
  8472. meta: { commonjs: { isCommonJS: false } }
  8473. };
  8474. }
  8475. if (isWrappedId(id, EXTERNAL_SUFFIX)) {
  8476. const actualId = unwrapId(id, EXTERNAL_SUFFIX);
  8477. return getUnknownRequireProxy(
  8478. actualId,
  8479. isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
  8480. );
  8481. }
  8482. // entry suffix is just appended to not mess up relative external resolution
  8483. if (id.endsWith(ENTRY_SUFFIX)) {
  8484. const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
  8485. const {
  8486. meta: { commonjs: commonjsMeta }
  8487. } = this.getModuleInfo(acutalId);
  8488. const shebang = commonjsMeta?.shebang ?? '';
  8489. return getEntryProxy(
  8490. acutalId,
  8491. getDefaultIsModuleExports(acutalId),
  8492. this.getModuleInfo,
  8493. shebang
  8494. );
  8495. }
  8496. if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
  8497. const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
  8498. return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
  8499. }
  8500. if (id === DYNAMIC_MODULES_ID) {
  8501. return getDynamicModuleRegistry(
  8502. isDynamicRequireModulesEnabled,
  8503. dynamicRequireModules,
  8504. commonDir,
  8505. ignoreDynamicRequires
  8506. );
  8507. }
  8508. if (isWrappedId(id, PROXY_SUFFIX)) {
  8509. const actualId = unwrapId(id, PROXY_SUFFIX);
  8510. return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
  8511. }
  8512. return null;
  8513. },
  8514. shouldTransformCachedModule(...args) {
  8515. return requireResolver.shouldTransformCachedModule.call(this, ...args);
  8516. },
  8517. transform(code, id) {
  8518. if (!isPossibleCjsId(id)) return null;
  8519. try {
  8520. return transformAndCheckExports.call(this, code, id);
  8521. } catch (err) {
  8522. return this.error(err, err.pos);
  8523. }
  8524. }
  8525. };
  8526. }
  8527. // Matches the scheme of a URL, eg "http://"
  8528. const schemeRegex = /^[\w+.-]+:\/\//;
  8529. /**
  8530. * Matches the parts of a URL:
  8531. * 1. Scheme, including ":", guaranteed.
  8532. * 2. User/password, including "@", optional.
  8533. * 3. Host, guaranteed.
  8534. * 4. Port, including ":", optional.
  8535. * 5. Path, including "/", optional.
  8536. * 6. Query, including "?", optional.
  8537. * 7. Hash, including "#", optional.
  8538. */
  8539. const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
  8540. /**
  8541. * File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start
  8542. * with a leading `/`, they can have a domain (but only if they don't start with a Windows drive).
  8543. *
  8544. * 1. Host, optional.
  8545. * 2. Path, which may include "/", guaranteed.
  8546. * 3. Query, including "?", optional.
  8547. * 4. Hash, including "#", optional.
  8548. */
  8549. const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
  8550. function isAbsoluteUrl(input) {
  8551. return schemeRegex.test(input);
  8552. }
  8553. function isSchemeRelativeUrl(input) {
  8554. return input.startsWith('//');
  8555. }
  8556. function isAbsolutePath(input) {
  8557. return input.startsWith('/');
  8558. }
  8559. function isFileUrl(input) {
  8560. return input.startsWith('file:');
  8561. }
  8562. function isRelative(input) {
  8563. return /^[.?#]/.test(input);
  8564. }
  8565. function parseAbsoluteUrl(input) {
  8566. const match = urlRegex.exec(input);
  8567. return makeUrl(match[1], match[2] || '', match[3], match[4] || '', match[5] || '/', match[6] || '', match[7] || '');
  8568. }
  8569. function parseFileUrl(input) {
  8570. const match = fileRegex.exec(input);
  8571. const path = match[2];
  8572. return makeUrl('file:', '', match[1] || '', '', isAbsolutePath(path) ? path : '/' + path, match[3] || '', match[4] || '');
  8573. }
  8574. function makeUrl(scheme, user, host, port, path, query, hash) {
  8575. return {
  8576. scheme,
  8577. user,
  8578. host,
  8579. port,
  8580. path,
  8581. query,
  8582. hash,
  8583. type: 7 /* Absolute */,
  8584. };
  8585. }
  8586. function parseUrl$3(input) {
  8587. if (isSchemeRelativeUrl(input)) {
  8588. const url = parseAbsoluteUrl('http:' + input);
  8589. url.scheme = '';
  8590. url.type = 6 /* SchemeRelative */;
  8591. return url;
  8592. }
  8593. if (isAbsolutePath(input)) {
  8594. const url = parseAbsoluteUrl('http://foo.com' + input);
  8595. url.scheme = '';
  8596. url.host = '';
  8597. url.type = 5 /* AbsolutePath */;
  8598. return url;
  8599. }
  8600. if (isFileUrl(input))
  8601. return parseFileUrl(input);
  8602. if (isAbsoluteUrl(input))
  8603. return parseAbsoluteUrl(input);
  8604. const url = parseAbsoluteUrl('http://foo.com/' + input);
  8605. url.scheme = '';
  8606. url.host = '';
  8607. url.type = input
  8608. ? input.startsWith('?')
  8609. ? 3 /* Query */
  8610. : input.startsWith('#')
  8611. ? 2 /* Hash */
  8612. : 4 /* RelativePath */
  8613. : 1 /* Empty */;
  8614. return url;
  8615. }
  8616. function stripPathFilename(path) {
  8617. // If a path ends with a parent directory "..", then it's a relative path with excess parent
  8618. // paths. It's not a file, so we can't strip it.
  8619. if (path.endsWith('/..'))
  8620. return path;
  8621. const index = path.lastIndexOf('/');
  8622. return path.slice(0, index + 1);
  8623. }
  8624. function mergePaths(url, base) {
  8625. normalizePath$4(base, base.type);
  8626. // If the path is just a "/", then it was an empty path to begin with (remember, we're a relative
  8627. // path).
  8628. if (url.path === '/') {
  8629. url.path = base.path;
  8630. }
  8631. else {
  8632. // Resolution happens relative to the base path's directory, not the file.
  8633. url.path = stripPathFilename(base.path) + url.path;
  8634. }
  8635. }
  8636. /**
  8637. * The path can have empty directories "//", unneeded parents "foo/..", or current directory
  8638. * "foo/.". We need to normalize to a standard representation.
  8639. */
  8640. function normalizePath$4(url, type) {
  8641. const rel = type <= 4 /* RelativePath */;
  8642. const pieces = url.path.split('/');
  8643. // We need to preserve the first piece always, so that we output a leading slash. The item at
  8644. // pieces[0] is an empty string.
  8645. let pointer = 1;
  8646. // Positive is the number of real directories we've output, used for popping a parent directory.
  8647. // Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo".
  8648. let positive = 0;
  8649. // We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will
  8650. // generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a
  8651. // real directory, we won't need to append, unless the other conditions happen again.
  8652. let addTrailingSlash = false;
  8653. for (let i = 1; i < pieces.length; i++) {
  8654. const piece = pieces[i];
  8655. // An empty directory, could be a trailing slash, or just a double "//" in the path.
  8656. if (!piece) {
  8657. addTrailingSlash = true;
  8658. continue;
  8659. }
  8660. // If we encounter a real directory, then we don't need to append anymore.
  8661. addTrailingSlash = false;
  8662. // A current directory, which we can always drop.
  8663. if (piece === '.')
  8664. continue;
  8665. // A parent directory, we need to see if there are any real directories we can pop. Else, we
  8666. // have an excess of parents, and we'll need to keep the "..".
  8667. if (piece === '..') {
  8668. if (positive) {
  8669. addTrailingSlash = true;
  8670. positive--;
  8671. pointer--;
  8672. }
  8673. else if (rel) {
  8674. // If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute
  8675. // URL, protocol relative URL, or an absolute path, we don't need to keep excess.
  8676. pieces[pointer++] = piece;
  8677. }
  8678. continue;
  8679. }
  8680. // We've encountered a real directory. Move it to the next insertion pointer, which accounts for
  8681. // any popped or dropped directories.
  8682. pieces[pointer++] = piece;
  8683. positive++;
  8684. }
  8685. let path = '';
  8686. for (let i = 1; i < pointer; i++) {
  8687. path += '/' + pieces[i];
  8688. }
  8689. if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
  8690. path += '/';
  8691. }
  8692. url.path = path;
  8693. }
  8694. /**
  8695. * Attempts to resolve `input` URL/path relative to `base`.
  8696. */
  8697. function resolve$2(input, base) {
  8698. if (!input && !base)
  8699. return '';
  8700. const url = parseUrl$3(input);
  8701. let inputType = url.type;
  8702. if (base && inputType !== 7 /* Absolute */) {
  8703. const baseUrl = parseUrl$3(base);
  8704. const baseType = baseUrl.type;
  8705. switch (inputType) {
  8706. case 1 /* Empty */:
  8707. url.hash = baseUrl.hash;
  8708. // fall through
  8709. case 2 /* Hash */:
  8710. url.query = baseUrl.query;
  8711. // fall through
  8712. case 3 /* Query */:
  8713. case 4 /* RelativePath */:
  8714. mergePaths(url, baseUrl);
  8715. // fall through
  8716. case 5 /* AbsolutePath */:
  8717. // The host, user, and port are joined, you can't copy one without the others.
  8718. url.user = baseUrl.user;
  8719. url.host = baseUrl.host;
  8720. url.port = baseUrl.port;
  8721. // fall through
  8722. case 6 /* SchemeRelative */:
  8723. // The input doesn't have a schema at least, so we need to copy at least that over.
  8724. url.scheme = baseUrl.scheme;
  8725. }
  8726. if (baseType > inputType)
  8727. inputType = baseType;
  8728. }
  8729. normalizePath$4(url, inputType);
  8730. const queryHash = url.query + url.hash;
  8731. switch (inputType) {
  8732. // This is impossible, because of the empty checks at the start of the function.
  8733. // case UrlType.Empty:
  8734. case 2 /* Hash */:
  8735. case 3 /* Query */:
  8736. return queryHash;
  8737. case 4 /* RelativePath */: {
  8738. // The first char is always a "/", and we need it to be relative.
  8739. const path = url.path.slice(1);
  8740. if (!path)
  8741. return queryHash || '.';
  8742. if (isRelative(base || input) && !isRelative(path)) {
  8743. // If base started with a leading ".", or there is no base and input started with a ".",
  8744. // then we need to ensure that the relative path starts with a ".". We don't know if
  8745. // relative starts with a "..", though, so check before prepending.
  8746. return './' + path + queryHash;
  8747. }
  8748. return path + queryHash;
  8749. }
  8750. case 5 /* AbsolutePath */:
  8751. return url.path + queryHash;
  8752. default:
  8753. return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
  8754. }
  8755. }
  8756. function resolve$1(input, base) {
  8757. // The base is always treated as a directory, if it's not empty.
  8758. // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
  8759. // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
  8760. if (base && !base.endsWith('/'))
  8761. base += '/';
  8762. return resolve$2(input, base);
  8763. }
  8764. /**
  8765. * Removes everything after the last "/", but leaves the slash.
  8766. */
  8767. function stripFilename(path) {
  8768. if (!path)
  8769. return '';
  8770. const index = path.lastIndexOf('/');
  8771. return path.slice(0, index + 1);
  8772. }
  8773. const COLUMN$1 = 0;
  8774. const SOURCES_INDEX$1 = 1;
  8775. const SOURCE_LINE$1 = 2;
  8776. const SOURCE_COLUMN$1 = 3;
  8777. const NAMES_INDEX$1 = 4;
  8778. function maybeSort(mappings, owned) {
  8779. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  8780. if (unsortedIndex === mappings.length)
  8781. return mappings;
  8782. // If we own the array (meaning we parsed it from JSON), then we're free to directly mutate it. If
  8783. // not, we do not want to modify the consumer's input array.
  8784. if (!owned)
  8785. mappings = mappings.slice();
  8786. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  8787. mappings[i] = sortSegments(mappings[i], owned);
  8788. }
  8789. return mappings;
  8790. }
  8791. function nextUnsortedSegmentLine(mappings, start) {
  8792. for (let i = start; i < mappings.length; i++) {
  8793. if (!isSorted(mappings[i]))
  8794. return i;
  8795. }
  8796. return mappings.length;
  8797. }
  8798. function isSorted(line) {
  8799. for (let j = 1; j < line.length; j++) {
  8800. if (line[j][COLUMN$1] < line[j - 1][COLUMN$1]) {
  8801. return false;
  8802. }
  8803. }
  8804. return true;
  8805. }
  8806. function sortSegments(line, owned) {
  8807. if (!owned)
  8808. line = line.slice();
  8809. return line.sort(sortComparator);
  8810. }
  8811. function sortComparator(a, b) {
  8812. return a[COLUMN$1] - b[COLUMN$1];
  8813. }
  8814. let found = false;
  8815. /**
  8816. * A binary search implementation that returns the index if a match is found.
  8817. * If no match is found, then the left-index (the index associated with the item that comes just
  8818. * before the desired index) is returned. To maintain proper sort order, a splice would happen at
  8819. * the next index:
  8820. *
  8821. * ```js
  8822. * const array = [1, 3];
  8823. * const needle = 2;
  8824. * const index = binarySearch(array, needle, (item, needle) => item - needle);
  8825. *
  8826. * assert.equal(index, 0);
  8827. * array.splice(index + 1, 0, needle);
  8828. * assert.deepEqual(array, [1, 2, 3]);
  8829. * ```
  8830. */
  8831. function binarySearch(haystack, needle, low, high) {
  8832. while (low <= high) {
  8833. const mid = low + ((high - low) >> 1);
  8834. const cmp = haystack[mid][COLUMN$1] - needle;
  8835. if (cmp === 0) {
  8836. found = true;
  8837. return mid;
  8838. }
  8839. if (cmp < 0) {
  8840. low = mid + 1;
  8841. }
  8842. else {
  8843. high = mid - 1;
  8844. }
  8845. }
  8846. found = false;
  8847. return low - 1;
  8848. }
  8849. function upperBound(haystack, needle, index) {
  8850. for (let i = index + 1; i < haystack.length; index = i++) {
  8851. if (haystack[i][COLUMN$1] !== needle)
  8852. break;
  8853. }
  8854. return index;
  8855. }
  8856. function lowerBound(haystack, needle, index) {
  8857. for (let i = index - 1; i >= 0; index = i--) {
  8858. if (haystack[i][COLUMN$1] !== needle)
  8859. break;
  8860. }
  8861. return index;
  8862. }
  8863. function memoizedState() {
  8864. return {
  8865. lastKey: -1,
  8866. lastNeedle: -1,
  8867. lastIndex: -1,
  8868. };
  8869. }
  8870. /**
  8871. * This overly complicated beast is just to record the last tested line/column and the resulting
  8872. * index, allowing us to skip a few tests if mappings are monotonically increasing.
  8873. */
  8874. function memoizedBinarySearch(haystack, needle, state, key) {
  8875. const { lastKey, lastNeedle, lastIndex } = state;
  8876. let low = 0;
  8877. let high = haystack.length - 1;
  8878. if (key === lastKey) {
  8879. if (needle === lastNeedle) {
  8880. found = lastIndex !== -1 && haystack[lastIndex][COLUMN$1] === needle;
  8881. return lastIndex;
  8882. }
  8883. if (needle >= lastNeedle) {
  8884. // lastIndex may be -1 if the previous needle was not found.
  8885. low = lastIndex === -1 ? 0 : lastIndex;
  8886. }
  8887. else {
  8888. high = lastIndex;
  8889. }
  8890. }
  8891. state.lastKey = key;
  8892. state.lastNeedle = needle;
  8893. return (state.lastIndex = binarySearch(haystack, needle, low, high));
  8894. }
  8895. const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
  8896. const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
  8897. const LEAST_UPPER_BOUND = -1;
  8898. const GREATEST_LOWER_BOUND = 1;
  8899. class TraceMap {
  8900. constructor(map, mapUrl) {
  8901. const isString = typeof map === 'string';
  8902. if (!isString && map._decodedMemo)
  8903. return map;
  8904. const parsed = (isString ? JSON.parse(map) : map);
  8905. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  8906. this.version = version;
  8907. this.file = file;
  8908. this.names = names || [];
  8909. this.sourceRoot = sourceRoot;
  8910. this.sources = sources;
  8911. this.sourcesContent = sourcesContent;
  8912. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || undefined;
  8913. const from = resolve$1(sourceRoot || '', stripFilename(mapUrl));
  8914. this.resolvedSources = sources.map((s) => resolve$1(s || '', from));
  8915. const { mappings } = parsed;
  8916. if (typeof mappings === 'string') {
  8917. this._encoded = mappings;
  8918. this._decoded = undefined;
  8919. }
  8920. else {
  8921. this._encoded = undefined;
  8922. this._decoded = maybeSort(mappings, isString);
  8923. }
  8924. this._decodedMemo = memoizedState();
  8925. this._bySources = undefined;
  8926. this._bySourceMemos = undefined;
  8927. }
  8928. }
  8929. /**
  8930. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  8931. * with public access modifiers.
  8932. */
  8933. function cast$2(map) {
  8934. return map;
  8935. }
  8936. /**
  8937. * Returns the encoded (VLQ string) form of the SourceMap's mappings field.
  8938. */
  8939. function encodedMappings(map) {
  8940. var _a;
  8941. var _b;
  8942. return ((_a = (_b = cast$2(map))._encoded) !== null && _a !== void 0 ? _a : (_b._encoded = encode$1(cast$2(map)._decoded)));
  8943. }
  8944. /**
  8945. * Returns the decoded (array of lines of segments) form of the SourceMap's mappings field.
  8946. */
  8947. function decodedMappings(map) {
  8948. var _a;
  8949. return ((_a = cast$2(map))._decoded || (_a._decoded = decode(cast$2(map)._encoded)));
  8950. }
  8951. /**
  8952. * A low-level API to find the segment associated with a generated line/column (think, from a
  8953. * stack trace). Line and column here are 0-based, unlike `originalPositionFor`.
  8954. */
  8955. function traceSegment(map, line, column) {
  8956. const decoded = decodedMappings(map);
  8957. // It's common for parent source maps to have pointers to lines that have no
  8958. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  8959. if (line >= decoded.length)
  8960. return null;
  8961. const segments = decoded[line];
  8962. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, GREATEST_LOWER_BOUND);
  8963. return index === -1 ? null : segments[index];
  8964. }
  8965. /**
  8966. * A higher-level API to find the source/line/column associated with a generated line/column
  8967. * (think, from a stack trace). Line is 1-based, but column is 0-based, due to legacy behavior in
  8968. * `source-map` library.
  8969. */
  8970. function originalPositionFor$1(map, needle) {
  8971. let { line, column, bias } = needle;
  8972. line--;
  8973. if (line < 0)
  8974. throw new Error(LINE_GTR_ZERO);
  8975. if (column < 0)
  8976. throw new Error(COL_GTR_EQ_ZERO);
  8977. const decoded = decodedMappings(map);
  8978. // It's common for parent source maps to have pointers to lines that have no
  8979. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  8980. if (line >= decoded.length)
  8981. return OMapping(null, null, null, null);
  8982. const segments = decoded[line];
  8983. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
  8984. if (index === -1)
  8985. return OMapping(null, null, null, null);
  8986. const segment = segments[index];
  8987. if (segment.length === 1)
  8988. return OMapping(null, null, null, null);
  8989. const { names, resolvedSources } = map;
  8990. return OMapping(resolvedSources[segment[SOURCES_INDEX$1]], segment[SOURCE_LINE$1] + 1, segment[SOURCE_COLUMN$1], segment.length === 5 ? names[segment[NAMES_INDEX$1]] : null);
  8991. }
  8992. /**
  8993. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  8994. * a sourcemap, or to JSON.stringify.
  8995. */
  8996. function decodedMap(map) {
  8997. return clone(map, decodedMappings(map));
  8998. }
  8999. /**
  9000. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  9001. * a sourcemap, or to JSON.stringify.
  9002. */
  9003. function encodedMap(map) {
  9004. return clone(map, encodedMappings(map));
  9005. }
  9006. function clone(map, mappings) {
  9007. return {
  9008. version: map.version,
  9009. file: map.file,
  9010. names: map.names,
  9011. sourceRoot: map.sourceRoot,
  9012. sources: map.sources,
  9013. sourcesContent: map.sourcesContent,
  9014. mappings,
  9015. ignoreList: map.ignoreList || map.x_google_ignoreList,
  9016. };
  9017. }
  9018. function OMapping(source, line, column, name) {
  9019. return { source, line, column, name };
  9020. }
  9021. function traceSegmentInternal(segments, memo, line, column, bias) {
  9022. let index = memoizedBinarySearch(segments, column, memo, line);
  9023. if (found) {
  9024. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  9025. }
  9026. else if (bias === LEAST_UPPER_BOUND)
  9027. index++;
  9028. if (index === -1 || index === segments.length)
  9029. return -1;
  9030. return index;
  9031. }
  9032. /**
  9033. * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
  9034. * index of the `key` in the backing array.
  9035. *
  9036. * This is designed to allow synchronizing a second array with the contents of the backing array,
  9037. * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
  9038. * and there are never duplicates.
  9039. */
  9040. class SetArray {
  9041. constructor() {
  9042. this._indexes = { __proto__: null };
  9043. this.array = [];
  9044. }
  9045. }
  9046. /**
  9047. * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
  9048. * with public access modifiers.
  9049. */
  9050. function cast$1(set) {
  9051. return set;
  9052. }
  9053. /**
  9054. * Gets the index associated with `key` in the backing array, if it is already present.
  9055. */
  9056. function get(setarr, key) {
  9057. return cast$1(setarr)._indexes[key];
  9058. }
  9059. /**
  9060. * Puts `key` into the backing array, if it is not already present. Returns
  9061. * the index of the `key` in the backing array.
  9062. */
  9063. function put(setarr, key) {
  9064. // The key may or may not be present. If it is present, it's a number.
  9065. const index = get(setarr, key);
  9066. if (index !== undefined)
  9067. return index;
  9068. const { array, _indexes: indexes } = cast$1(setarr);
  9069. const length = array.push(key);
  9070. return (indexes[key] = length - 1);
  9071. }
  9072. /**
  9073. * Removes the key, if it exists in the set.
  9074. */
  9075. function remove(setarr, key) {
  9076. const index = get(setarr, key);
  9077. if (index === undefined)
  9078. return;
  9079. const { array, _indexes: indexes } = cast$1(setarr);
  9080. for (let i = index + 1; i < array.length; i++) {
  9081. const k = array[i];
  9082. array[i - 1] = k;
  9083. indexes[k]--;
  9084. }
  9085. indexes[key] = undefined;
  9086. array.pop();
  9087. }
  9088. const COLUMN = 0;
  9089. const SOURCES_INDEX = 1;
  9090. const SOURCE_LINE = 2;
  9091. const SOURCE_COLUMN = 3;
  9092. const NAMES_INDEX = 4;
  9093. const NO_NAME = -1;
  9094. /**
  9095. * Provides the state to generate a sourcemap.
  9096. */
  9097. class GenMapping {
  9098. constructor({ file, sourceRoot } = {}) {
  9099. this._names = new SetArray();
  9100. this._sources = new SetArray();
  9101. this._sourcesContent = [];
  9102. this._mappings = [];
  9103. this.file = file;
  9104. this.sourceRoot = sourceRoot;
  9105. this._ignoreList = new SetArray();
  9106. }
  9107. }
  9108. /**
  9109. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  9110. * with public access modifiers.
  9111. */
  9112. function cast(map) {
  9113. return map;
  9114. }
  9115. /**
  9116. * Same as `addSegment`, but will only add the segment if it generates useful information in the
  9117. * resulting map. This only works correctly if segments are added **in order**, meaning you should
  9118. * not add a segment with a lower generated line/column than one that came before.
  9119. */
  9120. const maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  9121. return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
  9122. };
  9123. /**
  9124. * Adds/removes the content of the source file to the source map.
  9125. */
  9126. function setSourceContent(map, source, content) {
  9127. const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
  9128. const index = put(sources, source);
  9129. sourcesContent[index] = content;
  9130. }
  9131. function setIgnore(map, source, ignore = true) {
  9132. const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
  9133. const index = put(sources, source);
  9134. if (index === sourcesContent.length)
  9135. sourcesContent[index] = null;
  9136. if (ignore)
  9137. put(ignoreList, index);
  9138. else
  9139. remove(ignoreList, index);
  9140. }
  9141. /**
  9142. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  9143. * a sourcemap, or to JSON.stringify.
  9144. */
  9145. function toDecodedMap(map) {
  9146. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
  9147. removeEmptyFinalLines(mappings);
  9148. return {
  9149. version: 3,
  9150. file: map.file || undefined,
  9151. names: names.array,
  9152. sourceRoot: map.sourceRoot || undefined,
  9153. sources: sources.array,
  9154. sourcesContent,
  9155. mappings,
  9156. ignoreList: ignoreList.array,
  9157. };
  9158. }
  9159. /**
  9160. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  9161. * a sourcemap, or to JSON.stringify.
  9162. */
  9163. function toEncodedMap(map) {
  9164. const decoded = toDecodedMap(map);
  9165. return Object.assign(Object.assign({}, decoded), { mappings: encode$1(decoded.mappings) });
  9166. }
  9167. // This split declaration is only so that terser can elminiate the static initialization block.
  9168. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  9169. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
  9170. const line = getLine(mappings, genLine);
  9171. const index = getColumnIndex(line, genColumn);
  9172. if (!source) {
  9173. if (skipable && skipSourceless(line, index))
  9174. return;
  9175. return insert(line, index, [genColumn]);
  9176. }
  9177. const sourcesIndex = put(sources, source);
  9178. const namesIndex = name ? put(names, name) : NO_NAME;
  9179. if (sourcesIndex === sourcesContent.length)
  9180. sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
  9181. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  9182. return;
  9183. }
  9184. return insert(line, index, name
  9185. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
  9186. : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  9187. }
  9188. function getLine(mappings, index) {
  9189. for (let i = mappings.length; i <= index; i++) {
  9190. mappings[i] = [];
  9191. }
  9192. return mappings[index];
  9193. }
  9194. function getColumnIndex(line, genColumn) {
  9195. let index = line.length;
  9196. for (let i = index - 1; i >= 0; index = i--) {
  9197. const current = line[i];
  9198. if (genColumn >= current[COLUMN])
  9199. break;
  9200. }
  9201. return index;
  9202. }
  9203. function insert(array, index, value) {
  9204. for (let i = array.length; i > index; i--) {
  9205. array[i] = array[i - 1];
  9206. }
  9207. array[index] = value;
  9208. }
  9209. function removeEmptyFinalLines(mappings) {
  9210. const { length } = mappings;
  9211. let len = length;
  9212. for (let i = len - 1; i >= 0; len = i, i--) {
  9213. if (mappings[i].length > 0)
  9214. break;
  9215. }
  9216. if (len < length)
  9217. mappings.length = len;
  9218. }
  9219. function skipSourceless(line, index) {
  9220. // The start of a line is already sourceless, so adding a sourceless segment to the beginning
  9221. // doesn't generate any useful information.
  9222. if (index === 0)
  9223. return true;
  9224. const prev = line[index - 1];
  9225. // If the previous segment is also sourceless, then adding another sourceless segment doesn't
  9226. // genrate any new information. Else, this segment will end the source/named segment and point to
  9227. // a sourceless position, which is useful.
  9228. return prev.length === 1;
  9229. }
  9230. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  9231. // A source/named segment at the start of a line gives position at that genColumn
  9232. if (index === 0)
  9233. return false;
  9234. const prev = line[index - 1];
  9235. // If the previous segment is sourceless, then we're transitioning to a source.
  9236. if (prev.length === 1)
  9237. return false;
  9238. // If the previous segment maps to the exact same source position, then this segment doesn't
  9239. // provide any new position information.
  9240. return (sourcesIndex === prev[SOURCES_INDEX] &&
  9241. sourceLine === prev[SOURCE_LINE] &&
  9242. sourceColumn === prev[SOURCE_COLUMN] &&
  9243. namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
  9244. }
  9245. const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
  9246. const EMPTY_SOURCES = [];
  9247. function SegmentObject(source, line, column, name, content, ignore) {
  9248. return { source, line, column, name, content, ignore };
  9249. }
  9250. function Source(map, sources, source, content, ignore) {
  9251. return {
  9252. map,
  9253. sources,
  9254. source,
  9255. content,
  9256. ignore,
  9257. };
  9258. }
  9259. /**
  9260. * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
  9261. * (which may themselves be SourceMapTrees).
  9262. */
  9263. function MapSource(map, sources) {
  9264. return Source(map, sources, '', null, false);
  9265. }
  9266. /**
  9267. * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
  9268. * segment tracing ends at the `OriginalSource`.
  9269. */
  9270. function OriginalSource(source, content, ignore) {
  9271. return Source(null, EMPTY_SOURCES, source, content, ignore);
  9272. }
  9273. /**
  9274. * traceMappings is only called on the root level SourceMapTree, and begins the process of
  9275. * resolving each mapping in terms of the original source files.
  9276. */
  9277. function traceMappings(tree) {
  9278. // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
  9279. // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
  9280. const gen = new GenMapping({ file: tree.map.file });
  9281. const { sources: rootSources, map } = tree;
  9282. const rootNames = map.names;
  9283. const rootMappings = decodedMappings(map);
  9284. for (let i = 0; i < rootMappings.length; i++) {
  9285. const segments = rootMappings[i];
  9286. for (let j = 0; j < segments.length; j++) {
  9287. const segment = segments[j];
  9288. const genCol = segment[0];
  9289. let traced = SOURCELESS_MAPPING;
  9290. // 1-length segments only move the current generated column, there's no source information
  9291. // to gather from it.
  9292. if (segment.length !== 1) {
  9293. const source = rootSources[segment[1]];
  9294. traced = originalPositionFor(source, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
  9295. // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
  9296. // respective segment into an original source.
  9297. if (traced == null)
  9298. continue;
  9299. }
  9300. const { column, line, name, content, source, ignore } = traced;
  9301. maybeAddSegment(gen, i, genCol, source, line, column, name);
  9302. if (source && content != null)
  9303. setSourceContent(gen, source, content);
  9304. if (ignore)
  9305. setIgnore(gen, source, true);
  9306. }
  9307. }
  9308. return gen;
  9309. }
  9310. /**
  9311. * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
  9312. * child SourceMapTrees, until we find the original source map.
  9313. */
  9314. function originalPositionFor(source, line, column, name) {
  9315. if (!source.map) {
  9316. return SegmentObject(source.source, line, column, name, source.content, source.ignore);
  9317. }
  9318. const segment = traceSegment(source.map, line, column);
  9319. // If we couldn't find a segment, then this doesn't exist in the sourcemap.
  9320. if (segment == null)
  9321. return null;
  9322. // 1-length segments only move the current generated column, there's no source information
  9323. // to gather from it.
  9324. if (segment.length === 1)
  9325. return SOURCELESS_MAPPING;
  9326. return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  9327. }
  9328. function asArray(value) {
  9329. if (Array.isArray(value))
  9330. return value;
  9331. return [value];
  9332. }
  9333. /**
  9334. * Recursively builds a tree structure out of sourcemap files, with each node
  9335. * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
  9336. * `OriginalSource`s and `SourceMapTree`s.
  9337. *
  9338. * Every sourcemap is composed of a collection of source files and mappings
  9339. * into locations of those source files. When we generate a `SourceMapTree` for
  9340. * the sourcemap, we attempt to load each source file's own sourcemap. If it
  9341. * does not have an associated sourcemap, it is considered an original,
  9342. * unmodified source file.
  9343. */
  9344. function buildSourceMapTree(input, loader) {
  9345. const maps = asArray(input).map((m) => new TraceMap(m, ''));
  9346. const map = maps.pop();
  9347. for (let i = 0; i < maps.length; i++) {
  9348. if (maps[i].sources.length > 1) {
  9349. throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
  9350. 'Did you specify these with the most recent transformation maps first?');
  9351. }
  9352. }
  9353. let tree = build$2(map, loader, '', 0);
  9354. for (let i = maps.length - 1; i >= 0; i--) {
  9355. tree = MapSource(maps[i], [tree]);
  9356. }
  9357. return tree;
  9358. }
  9359. function build$2(map, loader, importer, importerDepth) {
  9360. const { resolvedSources, sourcesContent, ignoreList } = map;
  9361. const depth = importerDepth + 1;
  9362. const children = resolvedSources.map((sourceFile, i) => {
  9363. // The loading context gives the loader more information about why this file is being loaded
  9364. // (eg, from which importer). It also allows the loader to override the location of the loaded
  9365. // sourcemap/original source, or to override the content in the sourcesContent field if it's
  9366. // an unmodified source file.
  9367. const ctx = {
  9368. importer,
  9369. depth,
  9370. source: sourceFile || '',
  9371. content: undefined,
  9372. ignore: undefined,
  9373. };
  9374. // Use the provided loader callback to retrieve the file's sourcemap.
  9375. // TODO: We should eventually support async loading of sourcemap files.
  9376. const sourceMap = loader(ctx.source, ctx);
  9377. const { source, content, ignore } = ctx;
  9378. // If there is a sourcemap, then we need to recurse into it to load its source files.
  9379. if (sourceMap)
  9380. return build$2(new TraceMap(sourceMap, source), loader, source, depth);
  9381. // Else, it's an unmodified source file.
  9382. // The contents of this unmodified source file can be overridden via the loader context,
  9383. // allowing it to be explicitly null or a string. If it remains undefined, we fall back to
  9384. // the importing sourcemap's `sourcesContent` field.
  9385. const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
  9386. const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false;
  9387. return OriginalSource(source, sourceContent, ignored);
  9388. });
  9389. return MapSource(map, children);
  9390. }
  9391. /**
  9392. * A SourceMap v3 compatible sourcemap, which only includes fields that were
  9393. * provided to it.
  9394. */
  9395. class SourceMap {
  9396. constructor(map, options) {
  9397. const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
  9398. this.version = out.version; // SourceMap spec says this should be first.
  9399. this.file = out.file;
  9400. this.mappings = out.mappings;
  9401. this.names = out.names;
  9402. this.ignoreList = out.ignoreList;
  9403. this.sourceRoot = out.sourceRoot;
  9404. this.sources = out.sources;
  9405. if (!options.excludeContent) {
  9406. this.sourcesContent = out.sourcesContent;
  9407. }
  9408. }
  9409. toString() {
  9410. return JSON.stringify(this);
  9411. }
  9412. }
  9413. /**
  9414. * Traces through all the mappings in the root sourcemap, through the sources
  9415. * (and their sourcemaps), all the way back to the original source location.
  9416. *
  9417. * `loader` will be called every time we encounter a source file. If it returns
  9418. * a sourcemap, we will recurse into that sourcemap to continue the trace. If
  9419. * it returns a falsey value, that source file is treated as an original,
  9420. * unmodified source file.
  9421. *
  9422. * Pass `excludeContent` to exclude any self-containing source file content
  9423. * from the output sourcemap.
  9424. *
  9425. * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
  9426. * VLQ encoded) mappings.
  9427. */
  9428. function remapping(input, loader, options) {
  9429. const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
  9430. const tree = buildSourceMapTree(input, loader);
  9431. return new SourceMap(traceMappings(tree), opts);
  9432. }
  9433. var src$2 = {exports: {}};
  9434. var browser$3 = {exports: {}};
  9435. /**
  9436. * Helpers.
  9437. */
  9438. var ms$1;
  9439. var hasRequiredMs$1;
  9440. function requireMs$1 () {
  9441. if (hasRequiredMs$1) return ms$1;
  9442. hasRequiredMs$1 = 1;
  9443. var s = 1000;
  9444. var m = s * 60;
  9445. var h = m * 60;
  9446. var d = h * 24;
  9447. var w = d * 7;
  9448. var y = d * 365.25;
  9449. /**
  9450. * Parse or format the given `val`.
  9451. *
  9452. * Options:
  9453. *
  9454. * - `long` verbose formatting [false]
  9455. *
  9456. * @param {String|Number} val
  9457. * @param {Object} [options]
  9458. * @throws {Error} throw an error if val is not a non-empty string or a number
  9459. * @return {String|Number}
  9460. * @api public
  9461. */
  9462. ms$1 = function(val, options) {
  9463. options = options || {};
  9464. var type = typeof val;
  9465. if (type === 'string' && val.length > 0) {
  9466. return parse(val);
  9467. } else if (type === 'number' && isFinite(val)) {
  9468. return options.long ? fmtLong(val) : fmtShort(val);
  9469. }
  9470. throw new Error(
  9471. 'val is not a non-empty string or a valid number. val=' +
  9472. JSON.stringify(val)
  9473. );
  9474. };
  9475. /**
  9476. * Parse the given `str` and return milliseconds.
  9477. *
  9478. * @param {String} str
  9479. * @return {Number}
  9480. * @api private
  9481. */
  9482. function parse(str) {
  9483. str = String(str);
  9484. if (str.length > 100) {
  9485. return;
  9486. }
  9487. var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
  9488. str
  9489. );
  9490. if (!match) {
  9491. return;
  9492. }
  9493. var n = parseFloat(match[1]);
  9494. var type = (match[2] || 'ms').toLowerCase();
  9495. switch (type) {
  9496. case 'years':
  9497. case 'year':
  9498. case 'yrs':
  9499. case 'yr':
  9500. case 'y':
  9501. return n * y;
  9502. case 'weeks':
  9503. case 'week':
  9504. case 'w':
  9505. return n * w;
  9506. case 'days':
  9507. case 'day':
  9508. case 'd':
  9509. return n * d;
  9510. case 'hours':
  9511. case 'hour':
  9512. case 'hrs':
  9513. case 'hr':
  9514. case 'h':
  9515. return n * h;
  9516. case 'minutes':
  9517. case 'minute':
  9518. case 'mins':
  9519. case 'min':
  9520. case 'm':
  9521. return n * m;
  9522. case 'seconds':
  9523. case 'second':
  9524. case 'secs':
  9525. case 'sec':
  9526. case 's':
  9527. return n * s;
  9528. case 'milliseconds':
  9529. case 'millisecond':
  9530. case 'msecs':
  9531. case 'msec':
  9532. case 'ms':
  9533. return n;
  9534. default:
  9535. return undefined;
  9536. }
  9537. }
  9538. /**
  9539. * Short format for `ms`.
  9540. *
  9541. * @param {Number} ms
  9542. * @return {String}
  9543. * @api private
  9544. */
  9545. function fmtShort(ms) {
  9546. var msAbs = Math.abs(ms);
  9547. if (msAbs >= d) {
  9548. return Math.round(ms / d) + 'd';
  9549. }
  9550. if (msAbs >= h) {
  9551. return Math.round(ms / h) + 'h';
  9552. }
  9553. if (msAbs >= m) {
  9554. return Math.round(ms / m) + 'm';
  9555. }
  9556. if (msAbs >= s) {
  9557. return Math.round(ms / s) + 's';
  9558. }
  9559. return ms + 'ms';
  9560. }
  9561. /**
  9562. * Long format for `ms`.
  9563. *
  9564. * @param {Number} ms
  9565. * @return {String}
  9566. * @api private
  9567. */
  9568. function fmtLong(ms) {
  9569. var msAbs = Math.abs(ms);
  9570. if (msAbs >= d) {
  9571. return plural(ms, msAbs, d, 'day');
  9572. }
  9573. if (msAbs >= h) {
  9574. return plural(ms, msAbs, h, 'hour');
  9575. }
  9576. if (msAbs >= m) {
  9577. return plural(ms, msAbs, m, 'minute');
  9578. }
  9579. if (msAbs >= s) {
  9580. return plural(ms, msAbs, s, 'second');
  9581. }
  9582. return ms + ' ms';
  9583. }
  9584. /**
  9585. * Pluralization helper.
  9586. */
  9587. function plural(ms, msAbs, n, name) {
  9588. var isPlural = msAbs >= n * 1.5;
  9589. return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
  9590. }
  9591. return ms$1;
  9592. }
  9593. var common$b;
  9594. var hasRequiredCommon;
  9595. function requireCommon () {
  9596. if (hasRequiredCommon) return common$b;
  9597. hasRequiredCommon = 1;
  9598. /**
  9599. * This is the common logic for both the Node.js and web browser
  9600. * implementations of `debug()`.
  9601. */
  9602. function setup(env) {
  9603. createDebug.debug = createDebug;
  9604. createDebug.default = createDebug;
  9605. createDebug.coerce = coerce;
  9606. createDebug.disable = disable;
  9607. createDebug.enable = enable;
  9608. createDebug.enabled = enabled;
  9609. createDebug.humanize = requireMs$1();
  9610. createDebug.destroy = destroy;
  9611. Object.keys(env).forEach(key => {
  9612. createDebug[key] = env[key];
  9613. });
  9614. /**
  9615. * The currently active debug mode names, and names to skip.
  9616. */
  9617. createDebug.names = [];
  9618. createDebug.skips = [];
  9619. /**
  9620. * Map of special "%n" handling functions, for the debug "format" argument.
  9621. *
  9622. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  9623. */
  9624. createDebug.formatters = {};
  9625. /**
  9626. * Selects a color for a debug namespace
  9627. * @param {String} namespace The namespace string for the debug instance to be colored
  9628. * @return {Number|String} An ANSI color code for the given namespace
  9629. * @api private
  9630. */
  9631. function selectColor(namespace) {
  9632. let hash = 0;
  9633. for (let i = 0; i < namespace.length; i++) {
  9634. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  9635. hash |= 0; // Convert to 32bit integer
  9636. }
  9637. return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
  9638. }
  9639. createDebug.selectColor = selectColor;
  9640. /**
  9641. * Create a debugger with the given `namespace`.
  9642. *
  9643. * @param {String} namespace
  9644. * @return {Function}
  9645. * @api public
  9646. */
  9647. function createDebug(namespace) {
  9648. let prevTime;
  9649. let enableOverride = null;
  9650. let namespacesCache;
  9651. let enabledCache;
  9652. function debug(...args) {
  9653. // Disabled?
  9654. if (!debug.enabled) {
  9655. return;
  9656. }
  9657. const self = debug;
  9658. // Set `diff` timestamp
  9659. const curr = Number(new Date());
  9660. const ms = curr - (prevTime || curr);
  9661. self.diff = ms;
  9662. self.prev = prevTime;
  9663. self.curr = curr;
  9664. prevTime = curr;
  9665. args[0] = createDebug.coerce(args[0]);
  9666. if (typeof args[0] !== 'string') {
  9667. // Anything else let's inspect with %O
  9668. args.unshift('%O');
  9669. }
  9670. // Apply any `formatters` transformations
  9671. let index = 0;
  9672. args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
  9673. // If we encounter an escaped % then don't increase the array index
  9674. if (match === '%%') {
  9675. return '%';
  9676. }
  9677. index++;
  9678. const formatter = createDebug.formatters[format];
  9679. if (typeof formatter === 'function') {
  9680. const val = args[index];
  9681. match = formatter.call(self, val);
  9682. // Now we need to remove `args[index]` since it's inlined in the `format`
  9683. args.splice(index, 1);
  9684. index--;
  9685. }
  9686. return match;
  9687. });
  9688. // Apply env-specific formatting (colors, etc.)
  9689. createDebug.formatArgs.call(self, args);
  9690. const logFn = self.log || createDebug.log;
  9691. logFn.apply(self, args);
  9692. }
  9693. debug.namespace = namespace;
  9694. debug.useColors = createDebug.useColors();
  9695. debug.color = createDebug.selectColor(namespace);
  9696. debug.extend = extend;
  9697. debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
  9698. Object.defineProperty(debug, 'enabled', {
  9699. enumerable: true,
  9700. configurable: false,
  9701. get: () => {
  9702. if (enableOverride !== null) {
  9703. return enableOverride;
  9704. }
  9705. if (namespacesCache !== createDebug.namespaces) {
  9706. namespacesCache = createDebug.namespaces;
  9707. enabledCache = createDebug.enabled(namespace);
  9708. }
  9709. return enabledCache;
  9710. },
  9711. set: v => {
  9712. enableOverride = v;
  9713. }
  9714. });
  9715. // Env-specific initialization logic for debug instances
  9716. if (typeof createDebug.init === 'function') {
  9717. createDebug.init(debug);
  9718. }
  9719. return debug;
  9720. }
  9721. function extend(namespace, delimiter) {
  9722. const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
  9723. newDebug.log = this.log;
  9724. return newDebug;
  9725. }
  9726. /**
  9727. * Enables a debug mode by namespaces. This can include modes
  9728. * separated by a colon and wildcards.
  9729. *
  9730. * @param {String} namespaces
  9731. * @api public
  9732. */
  9733. function enable(namespaces) {
  9734. createDebug.save(namespaces);
  9735. createDebug.namespaces = namespaces;
  9736. createDebug.names = [];
  9737. createDebug.skips = [];
  9738. let i;
  9739. const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  9740. const len = split.length;
  9741. for (i = 0; i < len; i++) {
  9742. if (!split[i]) {
  9743. // ignore empty strings
  9744. continue;
  9745. }
  9746. namespaces = split[i].replace(/\*/g, '.*?');
  9747. if (namespaces[0] === '-') {
  9748. createDebug.skips.push(new RegExp('^' + namespaces.slice(1) + '$'));
  9749. } else {
  9750. createDebug.names.push(new RegExp('^' + namespaces + '$'));
  9751. }
  9752. }
  9753. }
  9754. /**
  9755. * Disable debug output.
  9756. *
  9757. * @return {String} namespaces
  9758. * @api public
  9759. */
  9760. function disable() {
  9761. const namespaces = [
  9762. ...createDebug.names.map(toNamespace),
  9763. ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
  9764. ].join(',');
  9765. createDebug.enable('');
  9766. return namespaces;
  9767. }
  9768. /**
  9769. * Returns true if the given mode name is enabled, false otherwise.
  9770. *
  9771. * @param {String} name
  9772. * @return {Boolean}
  9773. * @api public
  9774. */
  9775. function enabled(name) {
  9776. if (name[name.length - 1] === '*') {
  9777. return true;
  9778. }
  9779. let i;
  9780. let len;
  9781. for (i = 0, len = createDebug.skips.length; i < len; i++) {
  9782. if (createDebug.skips[i].test(name)) {
  9783. return false;
  9784. }
  9785. }
  9786. for (i = 0, len = createDebug.names.length; i < len; i++) {
  9787. if (createDebug.names[i].test(name)) {
  9788. return true;
  9789. }
  9790. }
  9791. return false;
  9792. }
  9793. /**
  9794. * Convert regexp to namespace
  9795. *
  9796. * @param {RegExp} regxep
  9797. * @return {String} namespace
  9798. * @api private
  9799. */
  9800. function toNamespace(regexp) {
  9801. return regexp.toString()
  9802. .substring(2, regexp.toString().length - 2)
  9803. .replace(/\.\*\?$/, '*');
  9804. }
  9805. /**
  9806. * Coerce `val`.
  9807. *
  9808. * @param {Mixed} val
  9809. * @return {Mixed}
  9810. * @api private
  9811. */
  9812. function coerce(val) {
  9813. if (val instanceof Error) {
  9814. return val.stack || val.message;
  9815. }
  9816. return val;
  9817. }
  9818. /**
  9819. * XXX DO NOT USE. This is a temporary stub function.
  9820. * XXX It WILL be removed in the next major release.
  9821. */
  9822. function destroy() {
  9823. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  9824. }
  9825. createDebug.enable(createDebug.load());
  9826. return createDebug;
  9827. }
  9828. common$b = setup;
  9829. return common$b;
  9830. }
  9831. /* eslint-env browser */
  9832. var hasRequiredBrowser$1;
  9833. function requireBrowser$1 () {
  9834. if (hasRequiredBrowser$1) return browser$3.exports;
  9835. hasRequiredBrowser$1 = 1;
  9836. (function (module, exports) {
  9837. /**
  9838. * This is the web browser implementation of `debug()`.
  9839. */
  9840. exports.formatArgs = formatArgs;
  9841. exports.save = save;
  9842. exports.load = load;
  9843. exports.useColors = useColors;
  9844. exports.storage = localstorage();
  9845. exports.destroy = (() => {
  9846. let warned = false;
  9847. return () => {
  9848. if (!warned) {
  9849. warned = true;
  9850. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  9851. }
  9852. };
  9853. })();
  9854. /**
  9855. * Colors.
  9856. */
  9857. exports.colors = [
  9858. '#0000CC',
  9859. '#0000FF',
  9860. '#0033CC',
  9861. '#0033FF',
  9862. '#0066CC',
  9863. '#0066FF',
  9864. '#0099CC',
  9865. '#0099FF',
  9866. '#00CC00',
  9867. '#00CC33',
  9868. '#00CC66',
  9869. '#00CC99',
  9870. '#00CCCC',
  9871. '#00CCFF',
  9872. '#3300CC',
  9873. '#3300FF',
  9874. '#3333CC',
  9875. '#3333FF',
  9876. '#3366CC',
  9877. '#3366FF',
  9878. '#3399CC',
  9879. '#3399FF',
  9880. '#33CC00',
  9881. '#33CC33',
  9882. '#33CC66',
  9883. '#33CC99',
  9884. '#33CCCC',
  9885. '#33CCFF',
  9886. '#6600CC',
  9887. '#6600FF',
  9888. '#6633CC',
  9889. '#6633FF',
  9890. '#66CC00',
  9891. '#66CC33',
  9892. '#9900CC',
  9893. '#9900FF',
  9894. '#9933CC',
  9895. '#9933FF',
  9896. '#99CC00',
  9897. '#99CC33',
  9898. '#CC0000',
  9899. '#CC0033',
  9900. '#CC0066',
  9901. '#CC0099',
  9902. '#CC00CC',
  9903. '#CC00FF',
  9904. '#CC3300',
  9905. '#CC3333',
  9906. '#CC3366',
  9907. '#CC3399',
  9908. '#CC33CC',
  9909. '#CC33FF',
  9910. '#CC6600',
  9911. '#CC6633',
  9912. '#CC9900',
  9913. '#CC9933',
  9914. '#CCCC00',
  9915. '#CCCC33',
  9916. '#FF0000',
  9917. '#FF0033',
  9918. '#FF0066',
  9919. '#FF0099',
  9920. '#FF00CC',
  9921. '#FF00FF',
  9922. '#FF3300',
  9923. '#FF3333',
  9924. '#FF3366',
  9925. '#FF3399',
  9926. '#FF33CC',
  9927. '#FF33FF',
  9928. '#FF6600',
  9929. '#FF6633',
  9930. '#FF9900',
  9931. '#FF9933',
  9932. '#FFCC00',
  9933. '#FFCC33'
  9934. ];
  9935. /**
  9936. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  9937. * and the Firebug extension (any Firefox version) are known
  9938. * to support "%c" CSS customizations.
  9939. *
  9940. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  9941. */
  9942. // eslint-disable-next-line complexity
  9943. function useColors() {
  9944. // NB: In an Electron preload script, document will be defined but not fully
  9945. // initialized. Since we know we're in Chrome, we'll just detect this case
  9946. // explicitly
  9947. if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
  9948. return true;
  9949. }
  9950. // Internet Explorer and Edge do not support colors.
  9951. if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
  9952. return false;
  9953. }
  9954. // Is webkit? http://stackoverflow.com/a/16459606/376773
  9955. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  9956. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  9957. // Is firebug? http://stackoverflow.com/a/398120/376773
  9958. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  9959. // Is firefox >= v31?
  9960. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  9961. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
  9962. // Double check webkit in userAgent just in case we are in a worker
  9963. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  9964. }
  9965. /**
  9966. * Colorize log arguments if enabled.
  9967. *
  9968. * @api public
  9969. */
  9970. function formatArgs(args) {
  9971. args[0] = (this.useColors ? '%c' : '') +
  9972. this.namespace +
  9973. (this.useColors ? ' %c' : ' ') +
  9974. args[0] +
  9975. (this.useColors ? '%c ' : ' ') +
  9976. '+' + module.exports.humanize(this.diff);
  9977. if (!this.useColors) {
  9978. return;
  9979. }
  9980. const c = 'color: ' + this.color;
  9981. args.splice(1, 0, c, 'color: inherit');
  9982. // The final "%c" is somewhat tricky, because there could be other
  9983. // arguments passed either before or after the %c, so we need to
  9984. // figure out the correct index to insert the CSS into
  9985. let index = 0;
  9986. let lastC = 0;
  9987. args[0].replace(/%[a-zA-Z%]/g, match => {
  9988. if (match === '%%') {
  9989. return;
  9990. }
  9991. index++;
  9992. if (match === '%c') {
  9993. // We only are interested in the *last* %c
  9994. // (the user may have provided their own)
  9995. lastC = index;
  9996. }
  9997. });
  9998. args.splice(lastC, 0, c);
  9999. }
  10000. /**
  10001. * Invokes `console.debug()` when available.
  10002. * No-op when `console.debug` is not a "function".
  10003. * If `console.debug` is not available, falls back
  10004. * to `console.log`.
  10005. *
  10006. * @api public
  10007. */
  10008. exports.log = console.debug || console.log || (() => {});
  10009. /**
  10010. * Save `namespaces`.
  10011. *
  10012. * @param {String} namespaces
  10013. * @api private
  10014. */
  10015. function save(namespaces) {
  10016. try {
  10017. if (namespaces) {
  10018. exports.storage.setItem('debug', namespaces);
  10019. } else {
  10020. exports.storage.removeItem('debug');
  10021. }
  10022. } catch (error) {
  10023. // Swallow
  10024. // XXX (@Qix-) should we be logging these?
  10025. }
  10026. }
  10027. /**
  10028. * Load `namespaces`.
  10029. *
  10030. * @return {String} returns the previously persisted debug modes
  10031. * @api private
  10032. */
  10033. function load() {
  10034. let r;
  10035. try {
  10036. r = exports.storage.getItem('debug');
  10037. } catch (error) {
  10038. // Swallow
  10039. // XXX (@Qix-) should we be logging these?
  10040. }
  10041. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  10042. if (!r && typeof process !== 'undefined' && 'env' in process) {
  10043. r = process.env.DEBUG;
  10044. }
  10045. return r;
  10046. }
  10047. /**
  10048. * Localstorage attempts to return the localstorage.
  10049. *
  10050. * This is necessary because safari throws
  10051. * when a user disables cookies/localstorage
  10052. * and you attempt to access it.
  10053. *
  10054. * @return {LocalStorage}
  10055. * @api private
  10056. */
  10057. function localstorage() {
  10058. try {
  10059. // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
  10060. // The Browser also has localStorage in the global context.
  10061. return localStorage;
  10062. } catch (error) {
  10063. // Swallow
  10064. // XXX (@Qix-) should we be logging these?
  10065. }
  10066. }
  10067. module.exports = requireCommon()(exports);
  10068. const {formatters} = module.exports;
  10069. /**
  10070. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  10071. */
  10072. formatters.j = function (v) {
  10073. try {
  10074. return JSON.stringify(v);
  10075. } catch (error) {
  10076. return '[UnexpectedJSONParseError]: ' + error.message;
  10077. }
  10078. };
  10079. } (browser$3, browser$3.exports));
  10080. return browser$3.exports;
  10081. }
  10082. var node$1 = {exports: {}};
  10083. /**
  10084. * Module dependencies.
  10085. */
  10086. var hasRequiredNode$1;
  10087. function requireNode$1 () {
  10088. if (hasRequiredNode$1) return node$1.exports;
  10089. hasRequiredNode$1 = 1;
  10090. (function (module, exports) {
  10091. const tty = require$$0$3;
  10092. const util = require$$0$6;
  10093. /**
  10094. * This is the Node.js implementation of `debug()`.
  10095. */
  10096. exports.init = init;
  10097. exports.log = log;
  10098. exports.formatArgs = formatArgs;
  10099. exports.save = save;
  10100. exports.load = load;
  10101. exports.useColors = useColors;
  10102. exports.destroy = util.deprecate(
  10103. () => {},
  10104. 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
  10105. );
  10106. /**
  10107. * Colors.
  10108. */
  10109. exports.colors = [6, 2, 3, 4, 5, 1];
  10110. try {
  10111. // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
  10112. // eslint-disable-next-line import/no-extraneous-dependencies
  10113. const supportsColor = require('supports-color');
  10114. if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
  10115. exports.colors = [
  10116. 20,
  10117. 21,
  10118. 26,
  10119. 27,
  10120. 32,
  10121. 33,
  10122. 38,
  10123. 39,
  10124. 40,
  10125. 41,
  10126. 42,
  10127. 43,
  10128. 44,
  10129. 45,
  10130. 56,
  10131. 57,
  10132. 62,
  10133. 63,
  10134. 68,
  10135. 69,
  10136. 74,
  10137. 75,
  10138. 76,
  10139. 77,
  10140. 78,
  10141. 79,
  10142. 80,
  10143. 81,
  10144. 92,
  10145. 93,
  10146. 98,
  10147. 99,
  10148. 112,
  10149. 113,
  10150. 128,
  10151. 129,
  10152. 134,
  10153. 135,
  10154. 148,
  10155. 149,
  10156. 160,
  10157. 161,
  10158. 162,
  10159. 163,
  10160. 164,
  10161. 165,
  10162. 166,
  10163. 167,
  10164. 168,
  10165. 169,
  10166. 170,
  10167. 171,
  10168. 172,
  10169. 173,
  10170. 178,
  10171. 179,
  10172. 184,
  10173. 185,
  10174. 196,
  10175. 197,
  10176. 198,
  10177. 199,
  10178. 200,
  10179. 201,
  10180. 202,
  10181. 203,
  10182. 204,
  10183. 205,
  10184. 206,
  10185. 207,
  10186. 208,
  10187. 209,
  10188. 214,
  10189. 215,
  10190. 220,
  10191. 221
  10192. ];
  10193. }
  10194. } catch (error) {
  10195. // Swallow - we only care if `supports-color` is available; it doesn't have to be.
  10196. }
  10197. /**
  10198. * Build up the default `inspectOpts` object from the environment variables.
  10199. *
  10200. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  10201. */
  10202. exports.inspectOpts = Object.keys(process.env).filter(key => {
  10203. return /^debug_/i.test(key);
  10204. }).reduce((obj, key) => {
  10205. // Camel-case
  10206. const prop = key
  10207. .substring(6)
  10208. .toLowerCase()
  10209. .replace(/_([a-z])/g, (_, k) => {
  10210. return k.toUpperCase();
  10211. });
  10212. // Coerce string value into JS value
  10213. let val = process.env[key];
  10214. if (/^(yes|on|true|enabled)$/i.test(val)) {
  10215. val = true;
  10216. } else if (/^(no|off|false|disabled)$/i.test(val)) {
  10217. val = false;
  10218. } else if (val === 'null') {
  10219. val = null;
  10220. } else {
  10221. val = Number(val);
  10222. }
  10223. obj[prop] = val;
  10224. return obj;
  10225. }, {});
  10226. /**
  10227. * Is stdout a TTY? Colored output is enabled when `true`.
  10228. */
  10229. function useColors() {
  10230. return 'colors' in exports.inspectOpts ?
  10231. Boolean(exports.inspectOpts.colors) :
  10232. tty.isatty(process.stderr.fd);
  10233. }
  10234. /**
  10235. * Adds ANSI color escape codes if enabled.
  10236. *
  10237. * @api public
  10238. */
  10239. function formatArgs(args) {
  10240. const {namespace: name, useColors} = this;
  10241. if (useColors) {
  10242. const c = this.color;
  10243. const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
  10244. const prefix = ` ${colorCode};1m${name} \u001B[0m`;
  10245. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  10246. args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
  10247. } else {
  10248. args[0] = getDate() + name + ' ' + args[0];
  10249. }
  10250. }
  10251. function getDate() {
  10252. if (exports.inspectOpts.hideDate) {
  10253. return '';
  10254. }
  10255. return new Date().toISOString() + ' ';
  10256. }
  10257. /**
  10258. * Invokes `util.format()` with the specified arguments and writes to stderr.
  10259. */
  10260. function log(...args) {
  10261. return process.stderr.write(util.format(...args) + '\n');
  10262. }
  10263. /**
  10264. * Save `namespaces`.
  10265. *
  10266. * @param {String} namespaces
  10267. * @api private
  10268. */
  10269. function save(namespaces) {
  10270. if (namespaces) {
  10271. process.env.DEBUG = namespaces;
  10272. } else {
  10273. // If you set a process.env field to null or undefined, it gets cast to the
  10274. // string 'null' or 'undefined'. Just delete instead.
  10275. delete process.env.DEBUG;
  10276. }
  10277. }
  10278. /**
  10279. * Load `namespaces`.
  10280. *
  10281. * @return {String} returns the previously persisted debug modes
  10282. * @api private
  10283. */
  10284. function load() {
  10285. return process.env.DEBUG;
  10286. }
  10287. /**
  10288. * Init logic for `debug` instances.
  10289. *
  10290. * Create a new `inspectOpts` object in case `useColors` is set
  10291. * differently for a particular `debug` instance.
  10292. */
  10293. function init(debug) {
  10294. debug.inspectOpts = {};
  10295. const keys = Object.keys(exports.inspectOpts);
  10296. for (let i = 0; i < keys.length; i++) {
  10297. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  10298. }
  10299. }
  10300. module.exports = requireCommon()(exports);
  10301. const {formatters} = module.exports;
  10302. /**
  10303. * Map %o to `util.inspect()`, all on a single line.
  10304. */
  10305. formatters.o = function (v) {
  10306. this.inspectOpts.colors = this.useColors;
  10307. return util.inspect(v, this.inspectOpts)
  10308. .split('\n')
  10309. .map(str => str.trim())
  10310. .join(' ');
  10311. };
  10312. /**
  10313. * Map %O to `util.inspect()`, allowing multiple lines if needed.
  10314. */
  10315. formatters.O = function (v) {
  10316. this.inspectOpts.colors = this.useColors;
  10317. return util.inspect(v, this.inspectOpts);
  10318. };
  10319. } (node$1, node$1.exports));
  10320. return node$1.exports;
  10321. }
  10322. /**
  10323. * Detect Electron renderer / nwjs process, which is node, but we should
  10324. * treat as a browser.
  10325. */
  10326. if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
  10327. src$2.exports = requireBrowser$1();
  10328. } else {
  10329. src$2.exports = requireNode$1();
  10330. }
  10331. var srcExports$1 = src$2.exports;
  10332. var debug$i = /*@__PURE__*/getDefaultExportFromCjs(srcExports$1);
  10333. let pnp;
  10334. if (process.versions.pnp) {
  10335. try {
  10336. pnp = createRequire$1(import.meta.url)('pnpapi');
  10337. }
  10338. catch { }
  10339. }
  10340. function invalidatePackageData(packageCache, pkgPath) {
  10341. const pkgDir = normalizePath$3(path$o.dirname(pkgPath));
  10342. packageCache.forEach((pkg, cacheKey) => {
  10343. if (pkg.dir === pkgDir) {
  10344. packageCache.delete(cacheKey);
  10345. }
  10346. });
  10347. }
  10348. function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageCache) {
  10349. if (pnp) {
  10350. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  10351. if (packageCache?.has(cacheKey))
  10352. return packageCache.get(cacheKey);
  10353. try {
  10354. const pkg = pnp.resolveToUnqualified(pkgName, basedir, {
  10355. considerBuiltins: false,
  10356. });
  10357. if (!pkg)
  10358. return null;
  10359. const pkgData = loadPackageData(path$o.join(pkg, 'package.json'));
  10360. packageCache?.set(cacheKey, pkgData);
  10361. return pkgData;
  10362. }
  10363. catch {
  10364. return null;
  10365. }
  10366. }
  10367. const originalBasedir = basedir;
  10368. while (basedir) {
  10369. if (packageCache) {
  10370. const cached = getRpdCache(packageCache, pkgName, basedir, originalBasedir, preserveSymlinks);
  10371. if (cached)
  10372. return cached;
  10373. }
  10374. const pkg = path$o.join(basedir, 'node_modules', pkgName, 'package.json');
  10375. try {
  10376. if (fs$l.existsSync(pkg)) {
  10377. const pkgPath = preserveSymlinks ? pkg : safeRealpathSync(pkg);
  10378. const pkgData = loadPackageData(pkgPath);
  10379. if (packageCache) {
  10380. setRpdCache(packageCache, pkgData, pkgName, basedir, originalBasedir, preserveSymlinks);
  10381. }
  10382. return pkgData;
  10383. }
  10384. }
  10385. catch { }
  10386. const nextBasedir = path$o.dirname(basedir);
  10387. if (nextBasedir === basedir)
  10388. break;
  10389. basedir = nextBasedir;
  10390. }
  10391. return null;
  10392. }
  10393. function findNearestPackageData(basedir, packageCache) {
  10394. const originalBasedir = basedir;
  10395. while (basedir) {
  10396. if (packageCache) {
  10397. const cached = getFnpdCache(packageCache, basedir, originalBasedir);
  10398. if (cached)
  10399. return cached;
  10400. }
  10401. const pkgPath = path$o.join(basedir, 'package.json');
  10402. if (tryStatSync(pkgPath)?.isFile()) {
  10403. try {
  10404. const pkgData = loadPackageData(pkgPath);
  10405. if (packageCache) {
  10406. setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
  10407. }
  10408. return pkgData;
  10409. }
  10410. catch { }
  10411. }
  10412. const nextBasedir = path$o.dirname(basedir);
  10413. if (nextBasedir === basedir)
  10414. break;
  10415. basedir = nextBasedir;
  10416. }
  10417. return null;
  10418. }
  10419. // Finds the nearest package.json with a `name` field
  10420. function findNearestMainPackageData(basedir, packageCache) {
  10421. const nearestPackage = findNearestPackageData(basedir, packageCache);
  10422. return (nearestPackage &&
  10423. (nearestPackage.data.name
  10424. ? nearestPackage
  10425. : findNearestMainPackageData(path$o.dirname(nearestPackage.dir), packageCache)));
  10426. }
  10427. function loadPackageData(pkgPath) {
  10428. const data = JSON.parse(fs$l.readFileSync(pkgPath, 'utf-8'));
  10429. const pkgDir = normalizePath$3(path$o.dirname(pkgPath));
  10430. const { sideEffects } = data;
  10431. let hasSideEffects;
  10432. if (typeof sideEffects === 'boolean') {
  10433. hasSideEffects = () => sideEffects;
  10434. }
  10435. else if (Array.isArray(sideEffects)) {
  10436. if (sideEffects.length <= 0) {
  10437. // createFilter always returns true if `includes` is an empty array
  10438. // but here we want it to always return false
  10439. hasSideEffects = () => false;
  10440. }
  10441. else {
  10442. const finalPackageSideEffects = sideEffects.map((sideEffect) => {
  10443. /*
  10444. * The array accepts simple glob patterns to the relevant files... Patterns like *.css, which do not include a /, will be treated like **\/*.css.
  10445. * https://webpack.js.org/guides/tree-shaking/
  10446. * https://github.com/vitejs/vite/pull/11807
  10447. */
  10448. if (sideEffect.includes('/')) {
  10449. return sideEffect;
  10450. }
  10451. return `**/${sideEffect}`;
  10452. });
  10453. hasSideEffects = createFilter(finalPackageSideEffects, null, {
  10454. resolve: pkgDir,
  10455. });
  10456. }
  10457. }
  10458. else {
  10459. hasSideEffects = () => null;
  10460. }
  10461. const pkg = {
  10462. dir: pkgDir,
  10463. data,
  10464. hasSideEffects,
  10465. webResolvedImports: {},
  10466. nodeResolvedImports: {},
  10467. setResolvedCache(key, entry, targetWeb) {
  10468. if (targetWeb) {
  10469. pkg.webResolvedImports[key] = entry;
  10470. }
  10471. else {
  10472. pkg.nodeResolvedImports[key] = entry;
  10473. }
  10474. },
  10475. getResolvedCache(key, targetWeb) {
  10476. if (targetWeb) {
  10477. return pkg.webResolvedImports[key];
  10478. }
  10479. else {
  10480. return pkg.nodeResolvedImports[key];
  10481. }
  10482. },
  10483. };
  10484. return pkg;
  10485. }
  10486. function watchPackageDataPlugin(packageCache) {
  10487. // a list of files to watch before the plugin is ready
  10488. const watchQueue = new Set();
  10489. const watchedDirs = new Set();
  10490. const watchFileStub = (id) => {
  10491. watchQueue.add(id);
  10492. };
  10493. let watchFile = watchFileStub;
  10494. const setPackageData = packageCache.set.bind(packageCache);
  10495. packageCache.set = (id, pkg) => {
  10496. if (!isInNodeModules$1(pkg.dir) && !watchedDirs.has(pkg.dir)) {
  10497. watchedDirs.add(pkg.dir);
  10498. watchFile(path$o.join(pkg.dir, 'package.json'));
  10499. }
  10500. return setPackageData(id, pkg);
  10501. };
  10502. return {
  10503. name: 'vite:watch-package-data',
  10504. buildStart() {
  10505. watchFile = this.addWatchFile.bind(this);
  10506. watchQueue.forEach(watchFile);
  10507. watchQueue.clear();
  10508. },
  10509. buildEnd() {
  10510. watchFile = watchFileStub;
  10511. },
  10512. watchChange(id) {
  10513. if (id.endsWith('/package.json')) {
  10514. invalidatePackageData(packageCache, path$o.normalize(id));
  10515. }
  10516. },
  10517. handleHotUpdate({ file }) {
  10518. if (file.endsWith('/package.json')) {
  10519. invalidatePackageData(packageCache, path$o.normalize(file));
  10520. }
  10521. },
  10522. };
  10523. }
  10524. /**
  10525. * Get cached `resolvePackageData` value based on `basedir`. When one is found,
  10526. * and we've already traversed some directories between `basedir` and `originalBasedir`,
  10527. * we cache the value for those in-between directories as well.
  10528. *
  10529. * This makes it so the fs is only read once for a shared `basedir`.
  10530. */
  10531. function getRpdCache(packageCache, pkgName, basedir, originalBasedir, preserveSymlinks) {
  10532. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  10533. const pkgData = packageCache.get(cacheKey);
  10534. if (pkgData) {
  10535. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  10536. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  10537. });
  10538. return pkgData;
  10539. }
  10540. }
  10541. function setRpdCache(packageCache, pkgData, pkgName, basedir, originalBasedir, preserveSymlinks) {
  10542. packageCache.set(getRpdCacheKey(pkgName, basedir, preserveSymlinks), pkgData);
  10543. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  10544. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  10545. });
  10546. }
  10547. // package cache key for `resolvePackageData`
  10548. function getRpdCacheKey(pkgName, basedir, preserveSymlinks) {
  10549. return `rpd_${pkgName}_${basedir}_${preserveSymlinks}`;
  10550. }
  10551. /**
  10552. * Get cached `findNearestPackageData` value based on `basedir`. When one is found,
  10553. * and we've already traversed some directories between `basedir` and `originalBasedir`,
  10554. * we cache the value for those in-between directories as well.
  10555. *
  10556. * This makes it so the fs is only read once for a shared `basedir`.
  10557. */
  10558. function getFnpdCache(packageCache, basedir, originalBasedir) {
  10559. const cacheKey = getFnpdCacheKey(basedir);
  10560. const pkgData = packageCache.get(cacheKey);
  10561. if (pkgData) {
  10562. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  10563. packageCache.set(getFnpdCacheKey(dir), pkgData);
  10564. });
  10565. return pkgData;
  10566. }
  10567. }
  10568. function setFnpdCache(packageCache, pkgData, basedir, originalBasedir) {
  10569. packageCache.set(getFnpdCacheKey(basedir), pkgData);
  10570. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  10571. packageCache.set(getFnpdCacheKey(dir), pkgData);
  10572. });
  10573. }
  10574. // package cache key for `findNearestPackageData`
  10575. function getFnpdCacheKey(basedir) {
  10576. return `fnpd_${basedir}`;
  10577. }
  10578. /**
  10579. * Traverse between `longerDir` (inclusive) and `shorterDir` (exclusive) and call `cb` for each dir.
  10580. * @param longerDir Longer dir path, e.g. `/User/foo/bar/baz`
  10581. * @param shorterDir Shorter dir path, e.g. `/User/foo`
  10582. */
  10583. function traverseBetweenDirs(longerDir, shorterDir, cb) {
  10584. while (longerDir !== shorterDir) {
  10585. cb(longerDir);
  10586. longerDir = path$o.dirname(longerDir);
  10587. }
  10588. }
  10589. const createFilter = createFilter$1;
  10590. const replaceSlashOrColonRE = /[/:]/g;
  10591. const replaceDotRE = /\./g;
  10592. const replaceNestedIdRE = /(\s*>\s*)/g;
  10593. const replaceHashRE = /#/g;
  10594. const flattenId = (id) => {
  10595. const flatId = limitFlattenIdLength(id
  10596. .replace(replaceSlashOrColonRE, '_')
  10597. .replace(replaceDotRE, '__')
  10598. .replace(replaceNestedIdRE, '___')
  10599. .replace(replaceHashRE, '____'));
  10600. return flatId;
  10601. };
  10602. const FLATTEN_ID_HASH_LENGTH = 8;
  10603. const FLATTEN_ID_MAX_FILE_LENGTH = 170;
  10604. const limitFlattenIdLength = (id, limit = FLATTEN_ID_MAX_FILE_LENGTH) => {
  10605. if (id.length <= limit) {
  10606. return id;
  10607. }
  10608. return id.slice(0, limit - (FLATTEN_ID_HASH_LENGTH + 1)) + '_' + getHash(id);
  10609. };
  10610. const normalizeId = (id) => id.replace(replaceNestedIdRE, ' > ');
  10611. // Supported by Node, Deno, Bun
  10612. const NODE_BUILTIN_NAMESPACE = 'node:';
  10613. // Supported by Deno
  10614. const NPM_BUILTIN_NAMESPACE = 'npm:';
  10615. // Supported by Bun
  10616. const BUN_BUILTIN_NAMESPACE = 'bun:';
  10617. // Some runtimes like Bun injects namespaced modules here, which is not a node builtin
  10618. const nodeBuiltins = builtinModules.filter((id) => !id.includes(':'));
  10619. // TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
  10620. function isBuiltin(id) {
  10621. if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE))
  10622. return true;
  10623. if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE))
  10624. return true;
  10625. return isNodeBuiltin(id);
  10626. }
  10627. function isNodeBuiltin(id) {
  10628. if (id.startsWith(NODE_BUILTIN_NAMESPACE))
  10629. return true;
  10630. return nodeBuiltins.includes(id);
  10631. }
  10632. function isInNodeModules$1(id) {
  10633. return id.includes('node_modules');
  10634. }
  10635. function moduleListContains(moduleList, id) {
  10636. return moduleList?.some((m) => m === id || id.startsWith(withTrailingSlash(m)));
  10637. }
  10638. function isOptimizable(id, optimizeDeps) {
  10639. const { extensions } = optimizeDeps;
  10640. return (OPTIMIZABLE_ENTRY_RE.test(id) ||
  10641. (extensions?.some((ext) => id.endsWith(ext)) ?? false));
  10642. }
  10643. const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/;
  10644. const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
  10645. // TODO: use import()
  10646. const _require$1 = createRequire$1(import.meta.url);
  10647. function resolveDependencyVersion(dep, pkgRelativePath = '../../package.json') {
  10648. const pkgPath = path$o.resolve(_require$1.resolve(dep), pkgRelativePath);
  10649. return JSON.parse(fs$l.readFileSync(pkgPath, 'utf-8')).version;
  10650. }
  10651. const rollupVersion = resolveDependencyVersion('rollup');
  10652. // set in bin/vite.js
  10653. const filter = process.env.VITE_DEBUG_FILTER;
  10654. const DEBUG = process.env.DEBUG;
  10655. function createDebugger(namespace, options = {}) {
  10656. const log = debug$i(namespace);
  10657. const { onlyWhenFocused } = options;
  10658. let enabled = log.enabled;
  10659. if (enabled && onlyWhenFocused) {
  10660. const ns = typeof onlyWhenFocused === 'string' ? onlyWhenFocused : namespace;
  10661. enabled = !!DEBUG?.includes(ns);
  10662. }
  10663. if (enabled) {
  10664. return (...args) => {
  10665. if (!filter || args.some((a) => a?.includes?.(filter))) {
  10666. log(...args);
  10667. }
  10668. };
  10669. }
  10670. }
  10671. function testCaseInsensitiveFS() {
  10672. if (!CLIENT_ENTRY.endsWith('client.mjs')) {
  10673. throw new Error(`cannot test case insensitive FS, CLIENT_ENTRY const doesn't contain client.mjs`);
  10674. }
  10675. if (!fs$l.existsSync(CLIENT_ENTRY)) {
  10676. throw new Error('cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: ' +
  10677. CLIENT_ENTRY);
  10678. }
  10679. return fs$l.existsSync(CLIENT_ENTRY.replace('client.mjs', 'cLiEnT.mjs'));
  10680. }
  10681. const urlCanParse =
  10682. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  10683. URL$3.canParse ??
  10684. // URL.canParse is supported from Node.js 18.17.0+, 20.0.0+
  10685. ((path, base) => {
  10686. try {
  10687. new URL$3(path, base);
  10688. return true;
  10689. }
  10690. catch {
  10691. return false;
  10692. }
  10693. });
  10694. const isCaseInsensitiveFS = testCaseInsensitiveFS();
  10695. const VOLUME_RE = /^[A-Z]:/i;
  10696. function normalizePath$3(id) {
  10697. return path$o.posix.normalize(isWindows$5 ? slash$1(id) : id);
  10698. }
  10699. function fsPathFromId(id) {
  10700. const fsPath = normalizePath$3(id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id);
  10701. return fsPath[0] === '/' || VOLUME_RE.test(fsPath) ? fsPath : `/${fsPath}`;
  10702. }
  10703. function fsPathFromUrl(url) {
  10704. return fsPathFromId(cleanUrl(url));
  10705. }
  10706. /**
  10707. * Check if dir is a parent of file
  10708. *
  10709. * Warning: parameters are not validated, only works with normalized absolute paths
  10710. *
  10711. * @param dir - normalized absolute path
  10712. * @param file - normalized absolute path
  10713. * @returns true if dir is a parent of file
  10714. */
  10715. function isParentDirectory(dir, file) {
  10716. dir = withTrailingSlash(dir);
  10717. return (file.startsWith(dir) ||
  10718. (isCaseInsensitiveFS && file.toLowerCase().startsWith(dir.toLowerCase())));
  10719. }
  10720. /**
  10721. * Check if 2 file name are identical
  10722. *
  10723. * Warning: parameters are not validated, only works with normalized absolute paths
  10724. *
  10725. * @param file1 - normalized absolute path
  10726. * @param file2 - normalized absolute path
  10727. * @returns true if both files url are identical
  10728. */
  10729. function isSameFileUri(file1, file2) {
  10730. return (file1 === file2 ||
  10731. (isCaseInsensitiveFS && file1.toLowerCase() === file2.toLowerCase()));
  10732. }
  10733. const externalRE = /^(https?:)?\/\//;
  10734. const isExternalUrl = (url) => externalRE.test(url);
  10735. const dataUrlRE = /^\s*data:/i;
  10736. const isDataUrl = (url) => dataUrlRE.test(url);
  10737. const virtualModuleRE = /^virtual-module:.*/;
  10738. const virtualModulePrefix = 'virtual-module:';
  10739. const knownJsSrcRE = /\.(?:[jt]sx?|m[jt]s|vue|marko|svelte|astro|imba|mdx)(?:$|\?)/;
  10740. const isJSRequest = (url) => {
  10741. url = cleanUrl(url);
  10742. if (knownJsSrcRE.test(url)) {
  10743. return true;
  10744. }
  10745. if (!path$o.extname(url) && url[url.length - 1] !== '/') {
  10746. return true;
  10747. }
  10748. return false;
  10749. };
  10750. const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/;
  10751. const isTsRequest = (url) => knownTsRE.test(url);
  10752. const importQueryRE = /(\?|&)import=?(?:&|$)/;
  10753. const directRequestRE$1 = /(\?|&)direct=?(?:&|$)/;
  10754. const internalPrefixes = [
  10755. FS_PREFIX,
  10756. VALID_ID_PREFIX,
  10757. CLIENT_PUBLIC_PATH,
  10758. ENV_PUBLIC_PATH,
  10759. ];
  10760. const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join('|')})`);
  10761. const trailingSeparatorRE = /[?&]$/;
  10762. const isImportRequest = (url) => importQueryRE.test(url);
  10763. const isInternalRequest = (url) => InternalPrefixRE.test(url);
  10764. function removeImportQuery(url) {
  10765. return url.replace(importQueryRE, '$1').replace(trailingSeparatorRE, '');
  10766. }
  10767. function removeDirectQuery(url) {
  10768. return url.replace(directRequestRE$1, '$1').replace(trailingSeparatorRE, '');
  10769. }
  10770. const urlRE = /(\?|&)url(?:&|$)/;
  10771. const rawRE = /(\?|&)raw(?:&|$)/;
  10772. function removeUrlQuery(url) {
  10773. return url.replace(urlRE, '$1').replace(trailingSeparatorRE, '');
  10774. }
  10775. const replacePercentageRE = /%/g;
  10776. function injectQuery(url, queryToInject) {
  10777. // encode percents for consistent behavior with pathToFileURL
  10778. // see #2614 for details
  10779. const resolvedUrl = new URL$3(url.replace(replacePercentageRE, '%25'), 'relative:///');
  10780. const { search, hash } = resolvedUrl;
  10781. let pathname = cleanUrl(url);
  10782. pathname = isWindows$5 ? slash$1(pathname) : pathname;
  10783. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${hash ?? ''}`;
  10784. }
  10785. const timestampRE = /\bt=\d{13}&?\b/;
  10786. function removeTimestampQuery(url) {
  10787. return url.replace(timestampRE, '').replace(trailingSeparatorRE, '');
  10788. }
  10789. async function asyncReplace(input, re, replacer) {
  10790. let match;
  10791. let remaining = input;
  10792. let rewritten = '';
  10793. while ((match = re.exec(remaining))) {
  10794. rewritten += remaining.slice(0, match.index);
  10795. rewritten += await replacer(match);
  10796. remaining = remaining.slice(match.index + match[0].length);
  10797. }
  10798. rewritten += remaining;
  10799. return rewritten;
  10800. }
  10801. function timeFrom(start, subtract = 0) {
  10802. const time = performance.now() - start - subtract;
  10803. const timeString = (time.toFixed(2) + `ms`).padEnd(5, ' ');
  10804. if (time < 10) {
  10805. return colors$1.green(timeString);
  10806. }
  10807. else if (time < 50) {
  10808. return colors$1.yellow(timeString);
  10809. }
  10810. else {
  10811. return colors$1.red(timeString);
  10812. }
  10813. }
  10814. /**
  10815. * pretty url for logging.
  10816. */
  10817. function prettifyUrl(url, root) {
  10818. url = removeTimestampQuery(url);
  10819. const isAbsoluteFile = url.startsWith(root);
  10820. if (isAbsoluteFile || url.startsWith(FS_PREFIX)) {
  10821. const file = path$o.posix.relative(root, isAbsoluteFile ? url : fsPathFromId(url));
  10822. return colors$1.dim(file);
  10823. }
  10824. else {
  10825. return colors$1.dim(url);
  10826. }
  10827. }
  10828. function isObject$1(value) {
  10829. return Object.prototype.toString.call(value) === '[object Object]';
  10830. }
  10831. function isDefined(value) {
  10832. return value != null;
  10833. }
  10834. function tryStatSync(file) {
  10835. try {
  10836. // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist
  10837. return fs$l.statSync(file, { throwIfNoEntry: false });
  10838. }
  10839. catch {
  10840. // Ignore errors
  10841. }
  10842. }
  10843. function lookupFile(dir, fileNames) {
  10844. while (dir) {
  10845. for (const fileName of fileNames) {
  10846. const fullPath = path$o.join(dir, fileName);
  10847. if (tryStatSync(fullPath)?.isFile())
  10848. return fullPath;
  10849. }
  10850. const parentDir = path$o.dirname(dir);
  10851. if (parentDir === dir)
  10852. return;
  10853. dir = parentDir;
  10854. }
  10855. }
  10856. function isFilePathESM(filePath, packageCache) {
  10857. if (/\.m[jt]s$/.test(filePath)) {
  10858. return true;
  10859. }
  10860. else if (/\.c[jt]s$/.test(filePath)) {
  10861. return false;
  10862. }
  10863. else {
  10864. // check package.json for type: "module"
  10865. try {
  10866. const pkg = findNearestPackageData(path$o.dirname(filePath), packageCache);
  10867. return pkg?.data.type === 'module';
  10868. }
  10869. catch {
  10870. return false;
  10871. }
  10872. }
  10873. }
  10874. const splitRE = /\r?\n/;
  10875. const range = 2;
  10876. function pad$1(source, n = 2) {
  10877. const lines = source.split(splitRE);
  10878. return lines.map((l) => ` `.repeat(n) + l).join(`\n`);
  10879. }
  10880. function posToNumber(source, pos) {
  10881. if (typeof pos === 'number')
  10882. return pos;
  10883. const lines = source.split(splitRE);
  10884. const { line, column } = pos;
  10885. let start = 0;
  10886. for (let i = 0; i < line - 1 && i < lines.length; i++) {
  10887. start += lines[i].length + 1;
  10888. }
  10889. return start + column;
  10890. }
  10891. function numberToPos(source, offset) {
  10892. if (typeof offset !== 'number')
  10893. return offset;
  10894. if (offset > source.length) {
  10895. throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
  10896. }
  10897. const lines = source.split(splitRE);
  10898. let counted = 0;
  10899. let line = 0;
  10900. let column = 0;
  10901. for (; line < lines.length; line++) {
  10902. const lineLength = lines[line].length + 1;
  10903. if (counted + lineLength >= offset) {
  10904. column = offset - counted + 1;
  10905. break;
  10906. }
  10907. counted += lineLength;
  10908. }
  10909. return { line: line + 1, column };
  10910. }
  10911. function generateCodeFrame(source, start = 0, end) {
  10912. start = Math.max(posToNumber(source, start), 0);
  10913. end = Math.min(end !== undefined ? posToNumber(source, end) : start, source.length);
  10914. const lines = source.split(splitRE);
  10915. let count = 0;
  10916. const res = [];
  10917. for (let i = 0; i < lines.length; i++) {
  10918. count += lines[i].length;
  10919. if (count >= start) {
  10920. for (let j = i - range; j <= i + range || end > count; j++) {
  10921. if (j < 0 || j >= lines.length)
  10922. continue;
  10923. const line = j + 1;
  10924. res.push(`${line}${' '.repeat(Math.max(3 - String(line).length, 0))}| ${lines[j]}`);
  10925. const lineLength = lines[j].length;
  10926. if (j === i) {
  10927. // push underline
  10928. const pad = Math.max(start - (count - lineLength), 0);
  10929. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  10930. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  10931. }
  10932. else if (j > i) {
  10933. if (end > count) {
  10934. const length = Math.max(Math.min(end - count, lineLength), 1);
  10935. res.push(` | ` + '^'.repeat(length));
  10936. }
  10937. count += lineLength + 1;
  10938. }
  10939. }
  10940. break;
  10941. }
  10942. count++;
  10943. }
  10944. return res.join('\n');
  10945. }
  10946. function isFileReadable(filename) {
  10947. if (!tryStatSync(filename)) {
  10948. return false;
  10949. }
  10950. try {
  10951. // Check if current process has read permission to the file
  10952. fs$l.accessSync(filename, fs$l.constants.R_OK);
  10953. return true;
  10954. }
  10955. catch {
  10956. return false;
  10957. }
  10958. }
  10959. const splitFirstDirRE = /(.+?)[\\/](.+)/;
  10960. /**
  10961. * Delete every file and subdirectory. **The given directory must exist.**
  10962. * Pass an optional `skip` array to preserve files under the root directory.
  10963. */
  10964. function emptyDir(dir, skip) {
  10965. const skipInDir = [];
  10966. let nested = null;
  10967. if (skip?.length) {
  10968. for (const file of skip) {
  10969. if (path$o.dirname(file) !== '.') {
  10970. const matched = file.match(splitFirstDirRE);
  10971. if (matched) {
  10972. nested ??= new Map();
  10973. const [, nestedDir, skipPath] = matched;
  10974. let nestedSkip = nested.get(nestedDir);
  10975. if (!nestedSkip) {
  10976. nestedSkip = [];
  10977. nested.set(nestedDir, nestedSkip);
  10978. }
  10979. if (!nestedSkip.includes(skipPath)) {
  10980. nestedSkip.push(skipPath);
  10981. }
  10982. }
  10983. }
  10984. else {
  10985. skipInDir.push(file);
  10986. }
  10987. }
  10988. }
  10989. for (const file of fs$l.readdirSync(dir)) {
  10990. if (skipInDir.includes(file)) {
  10991. continue;
  10992. }
  10993. if (nested?.has(file)) {
  10994. emptyDir(path$o.resolve(dir, file), nested.get(file));
  10995. }
  10996. else {
  10997. fs$l.rmSync(path$o.resolve(dir, file), { recursive: true, force: true });
  10998. }
  10999. }
  11000. }
  11001. function copyDir(srcDir, destDir) {
  11002. fs$l.mkdirSync(destDir, { recursive: true });
  11003. for (const file of fs$l.readdirSync(srcDir)) {
  11004. const srcFile = path$o.resolve(srcDir, file);
  11005. if (srcFile === destDir) {
  11006. continue;
  11007. }
  11008. const destFile = path$o.resolve(destDir, file);
  11009. const stat = fs$l.statSync(srcFile);
  11010. if (stat.isDirectory()) {
  11011. copyDir(srcFile, destFile);
  11012. }
  11013. else {
  11014. fs$l.copyFileSync(srcFile, destFile);
  11015. }
  11016. }
  11017. }
  11018. const ERR_SYMLINK_IN_RECURSIVE_READDIR = 'ERR_SYMLINK_IN_RECURSIVE_READDIR';
  11019. async function recursiveReaddir(dir) {
  11020. if (!fs$l.existsSync(dir)) {
  11021. return [];
  11022. }
  11023. let dirents;
  11024. try {
  11025. dirents = await fsp.readdir(dir, { withFileTypes: true });
  11026. }
  11027. catch (e) {
  11028. if (e.code === 'EACCES') {
  11029. // Ignore permission errors
  11030. return [];
  11031. }
  11032. throw e;
  11033. }
  11034. if (dirents.some((dirent) => dirent.isSymbolicLink())) {
  11035. const err = new Error('Symbolic links are not supported in recursiveReaddir');
  11036. err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR;
  11037. throw err;
  11038. }
  11039. const files = await Promise.all(dirents.map((dirent) => {
  11040. const res = path$o.resolve(dir, dirent.name);
  11041. return dirent.isDirectory() ? recursiveReaddir(res) : normalizePath$3(res);
  11042. }));
  11043. return files.flat(1);
  11044. }
  11045. // `fs.realpathSync.native` resolves differently in Windows network drive,
  11046. // causing file read errors. skip for now.
  11047. // https://github.com/nodejs/node/issues/37737
  11048. let safeRealpathSync = isWindows$5
  11049. ? windowsSafeRealPathSync
  11050. : fs$l.realpathSync.native;
  11051. // Based on https://github.com/larrybahr/windows-network-drive
  11052. // MIT License, Copyright (c) 2017 Larry Bahr
  11053. const windowsNetworkMap = new Map();
  11054. function windowsMappedRealpathSync(path) {
  11055. const realPath = fs$l.realpathSync.native(path);
  11056. if (realPath.startsWith('\\\\')) {
  11057. for (const [network, volume] of windowsNetworkMap) {
  11058. if (realPath.startsWith(network))
  11059. return realPath.replace(network, volume);
  11060. }
  11061. }
  11062. return realPath;
  11063. }
  11064. const parseNetUseRE = /^(\w+)? +(\w:) +([^ ]+)\s/;
  11065. let firstSafeRealPathSyncRun = false;
  11066. function windowsSafeRealPathSync(path) {
  11067. if (!firstSafeRealPathSyncRun) {
  11068. optimizeSafeRealPathSync();
  11069. firstSafeRealPathSyncRun = true;
  11070. }
  11071. return fs$l.realpathSync(path);
  11072. }
  11073. function optimizeSafeRealPathSync() {
  11074. // Skip if using Node <18.10 due to MAX_PATH issue: https://github.com/vitejs/vite/issues/12931
  11075. const nodeVersion = process.versions.node.split('.').map(Number);
  11076. if (nodeVersion[0] < 18 || (nodeVersion[0] === 18 && nodeVersion[1] < 10)) {
  11077. safeRealpathSync = fs$l.realpathSync;
  11078. return;
  11079. }
  11080. // Check the availability `fs.realpathSync.native`
  11081. // in Windows virtual and RAM disks that bypass the Volume Mount Manager, in programs such as imDisk
  11082. // get the error EISDIR: illegal operation on a directory
  11083. try {
  11084. fs$l.realpathSync.native(path$o.resolve('./'));
  11085. }
  11086. catch (error) {
  11087. if (error.message.includes('EISDIR: illegal operation on a directory')) {
  11088. safeRealpathSync = fs$l.realpathSync;
  11089. return;
  11090. }
  11091. }
  11092. exec('net use', (error, stdout) => {
  11093. if (error)
  11094. return;
  11095. const lines = stdout.split('\n');
  11096. // OK Y: \\NETWORKA\Foo Microsoft Windows Network
  11097. // OK Z: \\NETWORKA\Bar Microsoft Windows Network
  11098. for (const line of lines) {
  11099. const m = line.match(parseNetUseRE);
  11100. if (m)
  11101. windowsNetworkMap.set(m[3], m[2]);
  11102. }
  11103. if (windowsNetworkMap.size === 0) {
  11104. safeRealpathSync = fs$l.realpathSync.native;
  11105. }
  11106. else {
  11107. safeRealpathSync = windowsMappedRealpathSync;
  11108. }
  11109. });
  11110. }
  11111. function ensureWatchedFile(watcher, file, root) {
  11112. if (file &&
  11113. // only need to watch if out of root
  11114. !file.startsWith(withTrailingSlash(root)) &&
  11115. // some rollup plugins use null bytes for private resolved Ids
  11116. !file.includes('\0') &&
  11117. fs$l.existsSync(file)) {
  11118. // resolve file to normalized system path
  11119. watcher.add(path$o.resolve(file));
  11120. }
  11121. }
  11122. const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g;
  11123. const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/;
  11124. function joinSrcset(ret) {
  11125. return ret
  11126. .map(({ url, descriptor }) => url + (descriptor ? ` ${descriptor}` : ''))
  11127. .join(', ');
  11128. }
  11129. // NOTE: The returned `url` should perhaps be decoded so all handled URLs within Vite are consistently decoded.
  11130. // However, this may also require a refactor for `cssReplacer` to accept decoded URLs instead.
  11131. function splitSrcSetDescriptor(srcs) {
  11132. return splitSrcSet(srcs)
  11133. .map((s) => {
  11134. const src = s.replace(escapedSpaceCharacters, ' ').trim();
  11135. const url = imageSetUrlRE.exec(src)?.[0] ?? '';
  11136. return {
  11137. url,
  11138. descriptor: src.slice(url.length).trim(),
  11139. };
  11140. })
  11141. .filter(({ url }) => !!url);
  11142. }
  11143. function processSrcSet(srcs, replacer) {
  11144. return Promise.all(splitSrcSetDescriptor(srcs).map(async ({ url, descriptor }) => ({
  11145. url: await replacer({ url, descriptor }),
  11146. descriptor,
  11147. }))).then(joinSrcset);
  11148. }
  11149. function processSrcSetSync(srcs, replacer) {
  11150. return joinSrcset(splitSrcSetDescriptor(srcs).map(({ url, descriptor }) => ({
  11151. url: replacer({ url, descriptor }),
  11152. descriptor,
  11153. })));
  11154. }
  11155. const cleanSrcSetRE = /(?:url|image|gradient|cross-fade)\([^)]*\)|"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|data:\w+\/[\w.+\-]+;base64,[\w+/=]+|\?\S+,/g;
  11156. function splitSrcSet(srcs) {
  11157. const parts = [];
  11158. /**
  11159. * There could be a ',' inside of:
  11160. * - url(data:...)
  11161. * - linear-gradient(...)
  11162. * - "data:..."
  11163. * - data:...
  11164. * - query parameter ?...
  11165. */
  11166. const cleanedSrcs = srcs.replace(cleanSrcSetRE, blankReplacer);
  11167. let startIndex = 0;
  11168. let splitIndex;
  11169. do {
  11170. splitIndex = cleanedSrcs.indexOf(',', startIndex);
  11171. parts.push(srcs.slice(startIndex, splitIndex !== -1 ? splitIndex : undefined));
  11172. startIndex = splitIndex + 1;
  11173. } while (splitIndex !== -1);
  11174. return parts;
  11175. }
  11176. const windowsDriveRE = /^[A-Z]:/;
  11177. const replaceWindowsDriveRE = /^([A-Z]):\//;
  11178. const linuxAbsolutePathRE = /^\/[^/]/;
  11179. function escapeToLinuxLikePath(path) {
  11180. if (windowsDriveRE.test(path)) {
  11181. return path.replace(replaceWindowsDriveRE, '/windows/$1/');
  11182. }
  11183. if (linuxAbsolutePathRE.test(path)) {
  11184. return `/linux${path}`;
  11185. }
  11186. return path;
  11187. }
  11188. const revertWindowsDriveRE = /^\/windows\/([A-Z])\//;
  11189. function unescapeToLinuxLikePath(path) {
  11190. if (path.startsWith('/linux/')) {
  11191. return path.slice('/linux'.length);
  11192. }
  11193. if (path.startsWith('/windows/')) {
  11194. return path.replace(revertWindowsDriveRE, '$1:/');
  11195. }
  11196. return path;
  11197. }
  11198. // based on https://github.com/sveltejs/svelte/blob/abf11bb02b2afbd3e4cac509a0f70e318c306364/src/compiler/utils/mapped_code.ts#L221
  11199. const nullSourceMap = {
  11200. names: [],
  11201. sources: [],
  11202. mappings: '',
  11203. version: 3,
  11204. };
  11205. function combineSourcemaps(filename, sourcemapList) {
  11206. if (sourcemapList.length === 0 ||
  11207. sourcemapList.every((m) => m.sources.length === 0)) {
  11208. return { ...nullSourceMap };
  11209. }
  11210. // hack for parse broken with normalized absolute paths on windows (C:/path/to/something).
  11211. // escape them to linux like paths
  11212. // also avoid mutation here to prevent breaking plugin's using cache to generate sourcemaps like vue (see #7442)
  11213. sourcemapList = sourcemapList.map((sourcemap) => {
  11214. const newSourcemaps = { ...sourcemap };
  11215. newSourcemaps.sources = sourcemap.sources.map((source) => source ? escapeToLinuxLikePath(source) : null);
  11216. if (sourcemap.sourceRoot) {
  11217. newSourcemaps.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot);
  11218. }
  11219. return newSourcemaps;
  11220. });
  11221. const escapedFilename = escapeToLinuxLikePath(filename);
  11222. // We don't declare type here so we can convert/fake/map as RawSourceMap
  11223. let map; //: SourceMap
  11224. let mapIndex = 1;
  11225. const useArrayInterface = sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined;
  11226. if (useArrayInterface) {
  11227. map = remapping(sourcemapList, () => null);
  11228. }
  11229. else {
  11230. map = remapping(sourcemapList[0], function loader(sourcefile) {
  11231. if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
  11232. return sourcemapList[mapIndex++];
  11233. }
  11234. else {
  11235. return null;
  11236. }
  11237. });
  11238. }
  11239. if (!map.file) {
  11240. delete map.file;
  11241. }
  11242. // unescape the previous hack
  11243. map.sources = map.sources.map((source) => source ? unescapeToLinuxLikePath(source) : source);
  11244. map.file = filename;
  11245. return map;
  11246. }
  11247. function unique(arr) {
  11248. return Array.from(new Set(arr));
  11249. }
  11250. /**
  11251. * Returns resolved localhost address when `dns.lookup` result differs from DNS
  11252. *
  11253. * `dns.lookup` result is same when defaultResultOrder is `verbatim`.
  11254. * Even if defaultResultOrder is `ipv4first`, `dns.lookup` result maybe same.
  11255. * For example, when IPv6 is not supported on that machine/network.
  11256. */
  11257. async function getLocalhostAddressIfDiffersFromDNS() {
  11258. const [nodeResult, dnsResult] = await Promise.all([
  11259. promises.lookup('localhost'),
  11260. promises.lookup('localhost', { verbatim: true }),
  11261. ]);
  11262. const isSame = nodeResult.family === dnsResult.family &&
  11263. nodeResult.address === dnsResult.address;
  11264. return isSame ? undefined : nodeResult.address;
  11265. }
  11266. function diffDnsOrderChange(oldUrls, newUrls) {
  11267. return !(oldUrls === newUrls ||
  11268. (oldUrls &&
  11269. newUrls &&
  11270. arrayEqual(oldUrls.local, newUrls.local) &&
  11271. arrayEqual(oldUrls.network, newUrls.network)));
  11272. }
  11273. async function resolveHostname(optionsHost) {
  11274. let host;
  11275. if (optionsHost === undefined || optionsHost === false) {
  11276. // Use a secure default
  11277. host = 'localhost';
  11278. }
  11279. else if (optionsHost === true) {
  11280. // If passed --host in the CLI without arguments
  11281. host = undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs)
  11282. }
  11283. else {
  11284. host = optionsHost;
  11285. }
  11286. // Set host name to localhost when possible
  11287. let name = host === undefined || wildcardHosts.has(host) ? 'localhost' : host;
  11288. if (host === 'localhost') {
  11289. // See #8647 for more details.
  11290. const localhostAddr = await getLocalhostAddressIfDiffersFromDNS();
  11291. if (localhostAddr) {
  11292. name = localhostAddr;
  11293. }
  11294. }
  11295. return { host, name };
  11296. }
  11297. async function resolveServerUrls(server, options, config) {
  11298. const address = server.address();
  11299. const isAddressInfo = (x) => x?.address;
  11300. if (!isAddressInfo(address)) {
  11301. return { local: [], network: [] };
  11302. }
  11303. const local = [];
  11304. const network = [];
  11305. const hostname = await resolveHostname(options.host);
  11306. const protocol = options.https ? 'https' : 'http';
  11307. const port = address.port;
  11308. const base = config.rawBase === './' || config.rawBase === '' ? '/' : config.rawBase;
  11309. if (hostname.host !== undefined && !wildcardHosts.has(hostname.host)) {
  11310. let hostnameName = hostname.name;
  11311. // ipv6 host
  11312. if (hostnameName.includes(':')) {
  11313. hostnameName = `[${hostnameName}]`;
  11314. }
  11315. const address = `${protocol}://${hostnameName}:${port}${base}`;
  11316. if (loopbackHosts.has(hostname.host)) {
  11317. local.push(address);
  11318. }
  11319. else {
  11320. network.push(address);
  11321. }
  11322. }
  11323. else {
  11324. Object.values(os$4.networkInterfaces())
  11325. .flatMap((nInterface) => nInterface ?? [])
  11326. .filter((detail) => detail &&
  11327. detail.address &&
  11328. (detail.family === 'IPv4' ||
  11329. // @ts-expect-error Node 18.0 - 18.3 returns number
  11330. detail.family === 4))
  11331. .forEach((detail) => {
  11332. let host = detail.address.replace('127.0.0.1', hostname.name);
  11333. // ipv6 host
  11334. if (host.includes(':')) {
  11335. host = `[${host}]`;
  11336. }
  11337. const url = `${protocol}://${host}:${port}${base}`;
  11338. if (detail.address.includes('127.0.0.1')) {
  11339. local.push(url);
  11340. }
  11341. else {
  11342. network.push(url);
  11343. }
  11344. });
  11345. }
  11346. return { local, network };
  11347. }
  11348. function arraify(target) {
  11349. return Array.isArray(target) ? target : [target];
  11350. }
  11351. // Taken from https://stackoverflow.com/a/36328890
  11352. const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g;
  11353. const singlelineCommentsRE = /\/\/.*/g;
  11354. const requestQuerySplitRE = /\?(?!.*[/|}])/;
  11355. const requestQueryMaybeEscapedSplitRE = /\\?\?(?!.*[/|}])/;
  11356. const blankReplacer = (match) => ' '.repeat(match.length);
  11357. function getHash(text, length = 8) {
  11358. const h = createHash$2('sha256').update(text).digest('hex').substring(0, length);
  11359. if (length <= 64)
  11360. return h;
  11361. return h.padEnd(length, '_');
  11362. }
  11363. const _dirname = path$o.dirname(fileURLToPath(import.meta.url));
  11364. const requireResolveFromRootWithFallback = (root, id) => {
  11365. // check existence first, so if the package is not found,
  11366. // it won't be cached by nodejs, since there isn't a way to invalidate them:
  11367. // https://github.com/nodejs/node/issues/44663
  11368. const found = resolvePackageData(id, root) || resolvePackageData(id, _dirname);
  11369. if (!found) {
  11370. const error = new Error(`${JSON.stringify(id)} not found.`);
  11371. error.code = 'MODULE_NOT_FOUND';
  11372. throw error;
  11373. }
  11374. // actually resolve
  11375. // Search in the root directory first, and fallback to the default require paths.
  11376. return _require$1.resolve(id, { paths: [root, _dirname] });
  11377. };
  11378. function emptyCssComments(raw) {
  11379. return raw.replace(multilineCommentsRE, blankReplacer);
  11380. }
  11381. function backwardCompatibleWorkerPlugins(plugins) {
  11382. if (Array.isArray(plugins)) {
  11383. return plugins;
  11384. }
  11385. if (typeof plugins === 'function') {
  11386. return plugins();
  11387. }
  11388. return [];
  11389. }
  11390. function mergeConfigRecursively(defaults, overrides, rootPath) {
  11391. const merged = { ...defaults };
  11392. for (const key in overrides) {
  11393. const value = overrides[key];
  11394. if (value == null) {
  11395. continue;
  11396. }
  11397. const existing = merged[key];
  11398. if (existing == null) {
  11399. merged[key] = value;
  11400. continue;
  11401. }
  11402. // fields that require special handling
  11403. if (key === 'alias' && (rootPath === 'resolve' || rootPath === '')) {
  11404. merged[key] = mergeAlias(existing, value);
  11405. continue;
  11406. }
  11407. else if (key === 'assetsInclude' && rootPath === '') {
  11408. merged[key] = [].concat(existing, value);
  11409. continue;
  11410. }
  11411. else if (key === 'noExternal' &&
  11412. rootPath === 'ssr' &&
  11413. (existing === true || value === true)) {
  11414. merged[key] = true;
  11415. continue;
  11416. }
  11417. else if (key === 'plugins' && rootPath === 'worker') {
  11418. merged[key] = () => [
  11419. ...backwardCompatibleWorkerPlugins(existing),
  11420. ...backwardCompatibleWorkerPlugins(value),
  11421. ];
  11422. continue;
  11423. }
  11424. if (Array.isArray(existing) || Array.isArray(value)) {
  11425. merged[key] = [...arraify(existing), ...arraify(value)];
  11426. continue;
  11427. }
  11428. if (isObject$1(existing) && isObject$1(value)) {
  11429. merged[key] = mergeConfigRecursively(existing, value, rootPath ? `${rootPath}.${key}` : key);
  11430. continue;
  11431. }
  11432. merged[key] = value;
  11433. }
  11434. return merged;
  11435. }
  11436. function mergeConfig(defaults, overrides, isRoot = true) {
  11437. if (typeof defaults === 'function' || typeof overrides === 'function') {
  11438. throw new Error(`Cannot merge config in form of callback`);
  11439. }
  11440. return mergeConfigRecursively(defaults, overrides, isRoot ? '' : '.');
  11441. }
  11442. function mergeAlias(a, b) {
  11443. if (!a)
  11444. return b;
  11445. if (!b)
  11446. return a;
  11447. if (isObject$1(a) && isObject$1(b)) {
  11448. return { ...a, ...b };
  11449. }
  11450. // the order is flipped because the alias is resolved from top-down,
  11451. // where the later should have higher priority
  11452. return [...normalizeAlias(b), ...normalizeAlias(a)];
  11453. }
  11454. function normalizeAlias(o = []) {
  11455. return Array.isArray(o)
  11456. ? o.map(normalizeSingleAlias)
  11457. : Object.keys(o).map((find) => normalizeSingleAlias({
  11458. find,
  11459. replacement: o[find],
  11460. }));
  11461. }
  11462. // https://github.com/vitejs/vite/issues/1363
  11463. // work around https://github.com/rollup/plugins/issues/759
  11464. function normalizeSingleAlias({ find, replacement, customResolver, }) {
  11465. if (typeof find === 'string' &&
  11466. find[find.length - 1] === '/' &&
  11467. replacement[replacement.length - 1] === '/') {
  11468. find = find.slice(0, find.length - 1);
  11469. replacement = replacement.slice(0, replacement.length - 1);
  11470. }
  11471. const alias = {
  11472. find,
  11473. replacement,
  11474. };
  11475. if (customResolver) {
  11476. alias.customResolver = customResolver;
  11477. }
  11478. return alias;
  11479. }
  11480. /**
  11481. * Transforms transpiled code result where line numbers aren't altered,
  11482. * so we can skip sourcemap generation during dev
  11483. */
  11484. function transformStableResult(s, id, config) {
  11485. return {
  11486. code: s.toString(),
  11487. map: config.command === 'build' && config.build.sourcemap
  11488. ? s.generateMap({ hires: 'boundary', source: id })
  11489. : null,
  11490. };
  11491. }
  11492. async function asyncFlatten(arr) {
  11493. do {
  11494. arr = (await Promise.all(arr)).flat(Infinity);
  11495. } while (arr.some((v) => v?.then));
  11496. return arr;
  11497. }
  11498. // strip UTF-8 BOM
  11499. function stripBomTag(content) {
  11500. if (content.charCodeAt(0) === 0xfeff) {
  11501. return content.slice(1);
  11502. }
  11503. return content;
  11504. }
  11505. const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/;
  11506. /**
  11507. * path.isAbsolute also returns true for drive relative paths on windows (e.g. /something)
  11508. * this function returns false for them but true for absolute paths (e.g. C:/something)
  11509. */
  11510. const isNonDriveRelativeAbsolutePath = (p) => {
  11511. if (!isWindows$5)
  11512. return p[0] === '/';
  11513. return windowsDrivePathPrefixRE.test(p);
  11514. };
  11515. /**
  11516. * Determine if a file is being requested with the correct case, to ensure
  11517. * consistent behavior between dev and prod and across operating systems.
  11518. */
  11519. function shouldServeFile(filePath, root) {
  11520. // can skip case check on Linux
  11521. if (!isCaseInsensitiveFS)
  11522. return true;
  11523. return hasCorrectCase(filePath, root);
  11524. }
  11525. /**
  11526. * Note that we can't use realpath here, because we don't want to follow
  11527. * symlinks.
  11528. */
  11529. function hasCorrectCase(file, assets) {
  11530. if (file === assets)
  11531. return true;
  11532. const parent = path$o.dirname(file);
  11533. if (fs$l.readdirSync(parent).includes(path$o.basename(file))) {
  11534. return hasCorrectCase(parent, assets);
  11535. }
  11536. return false;
  11537. }
  11538. function joinUrlSegments(a, b) {
  11539. if (!a || !b) {
  11540. return a || b || '';
  11541. }
  11542. if (a[a.length - 1] === '/') {
  11543. a = a.substring(0, a.length - 1);
  11544. }
  11545. if (b[0] !== '/') {
  11546. b = '/' + b;
  11547. }
  11548. return a + b;
  11549. }
  11550. function removeLeadingSlash(str) {
  11551. return str[0] === '/' ? str.slice(1) : str;
  11552. }
  11553. function stripBase(path, base) {
  11554. if (path === base) {
  11555. return '/';
  11556. }
  11557. const devBase = withTrailingSlash(base);
  11558. return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path;
  11559. }
  11560. function arrayEqual(a, b) {
  11561. if (a === b)
  11562. return true;
  11563. if (a.length !== b.length)
  11564. return false;
  11565. for (let i = 0; i < a.length; i++) {
  11566. if (a[i] !== b[i])
  11567. return false;
  11568. }
  11569. return true;
  11570. }
  11571. function evalValue(rawValue) {
  11572. const fn = new Function(`
  11573. var console, exports, global, module, process, require
  11574. return (\n${rawValue}\n)
  11575. `);
  11576. return fn();
  11577. }
  11578. function getNpmPackageName(importPath) {
  11579. const parts = importPath.split('/');
  11580. if (parts[0][0] === '@') {
  11581. if (!parts[1])
  11582. return null;
  11583. return `${parts[0]}/${parts[1]}`;
  11584. }
  11585. else {
  11586. return parts[0];
  11587. }
  11588. }
  11589. const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
  11590. function escapeRegex(str) {
  11591. return str.replace(escapeRegexRE, '\\$&');
  11592. }
  11593. function getPackageManagerCommand(type = 'install') {
  11594. const packageManager = process.env.npm_config_user_agent?.split(' ')[0].split('/')[0] || 'npm';
  11595. switch (type) {
  11596. case 'install':
  11597. return packageManager === 'npm' ? 'npm install' : `${packageManager} add`;
  11598. case 'uninstall':
  11599. return packageManager === 'npm'
  11600. ? 'npm uninstall'
  11601. : `${packageManager} remove`;
  11602. case 'update':
  11603. return packageManager === 'yarn'
  11604. ? 'yarn upgrade'
  11605. : `${packageManager} update`;
  11606. default:
  11607. throw new TypeError(`Unknown command type: ${type}`);
  11608. }
  11609. }
  11610. function isDevServer(server) {
  11611. return 'pluginContainer' in server;
  11612. }
  11613. function promiseWithResolvers() {
  11614. let resolve;
  11615. let reject;
  11616. const promise = new Promise((_resolve, _reject) => {
  11617. resolve = _resolve;
  11618. reject = _reject;
  11619. });
  11620. return { promise, resolve, reject };
  11621. }
  11622. function createSerialPromiseQueue() {
  11623. let previousTask;
  11624. return {
  11625. async run(f) {
  11626. const thisTask = f();
  11627. // wait for both the previous task and this task
  11628. // so that this function resolves in the order this function is called
  11629. const depTasks = Promise.all([previousTask, thisTask]);
  11630. previousTask = depTasks;
  11631. const [, result] = await depTasks;
  11632. // this task was the last one, clear `previousTask` to free up memory
  11633. if (previousTask === depTasks) {
  11634. previousTask = undefined;
  11635. }
  11636. return result;
  11637. },
  11638. };
  11639. }
  11640. function sortObjectKeys(obj) {
  11641. const sorted = {};
  11642. for (const key of Object.keys(obj).sort()) {
  11643. sorted[key] = obj[key];
  11644. }
  11645. return sorted;
  11646. }
  11647. function displayTime(time) {
  11648. // display: {X}ms
  11649. if (time < 1000) {
  11650. return `${time}ms`;
  11651. }
  11652. time = time / 1000;
  11653. // display: {X}s
  11654. if (time < 60) {
  11655. return `${time.toFixed(2)}s`;
  11656. }
  11657. const mins = parseInt((time / 60).toString());
  11658. const seconds = time % 60;
  11659. // display: {X}m {Y}s
  11660. return `${mins}m${seconds < 1 ? '' : ` ${seconds.toFixed(0)}s`}`;
  11661. }
  11662. /**
  11663. * Encodes the URI path portion (ignores part after ? or #)
  11664. */
  11665. function encodeURIPath(uri) {
  11666. if (uri.startsWith('data:'))
  11667. return uri;
  11668. const filePath = cleanUrl(uri);
  11669. const postfix = filePath !== uri ? uri.slice(filePath.length) : '';
  11670. return encodeURI(filePath) + postfix;
  11671. }
  11672. /**
  11673. * Like `encodeURIPath`, but only replacing `%` as `%25`. This is useful for environments
  11674. * that can handle un-encoded URIs, where `%` is the only ambiguous character.
  11675. */
  11676. function partialEncodeURIPath(uri) {
  11677. if (uri.startsWith('data:'))
  11678. return uri;
  11679. const filePath = cleanUrl(uri);
  11680. const postfix = filePath !== uri ? uri.slice(filePath.length) : '';
  11681. return filePath.replaceAll('%', '%25') + postfix;
  11682. }
  11683. /* eslint no-console: 0 */
  11684. const LogLevels = {
  11685. silent: 0,
  11686. error: 1,
  11687. warn: 2,
  11688. info: 3,
  11689. };
  11690. let lastType;
  11691. let lastMsg;
  11692. let sameCount = 0;
  11693. function clearScreen() {
  11694. const repeatCount = process.stdout.rows - 2;
  11695. const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : '';
  11696. console.log(blank);
  11697. readline.cursorTo(process.stdout, 0, 0);
  11698. readline.clearScreenDown(process.stdout);
  11699. }
  11700. // Only initialize the timeFormatter when the timestamp option is used, and
  11701. // reuse it across all loggers
  11702. let timeFormatter;
  11703. function getTimeFormatter() {
  11704. timeFormatter ??= new Intl.DateTimeFormat(undefined, {
  11705. hour: 'numeric',
  11706. minute: 'numeric',
  11707. second: 'numeric',
  11708. });
  11709. return timeFormatter;
  11710. }
  11711. function createLogger(level = 'info', options = {}) {
  11712. if (options.customLogger) {
  11713. return options.customLogger;
  11714. }
  11715. const loggedErrors = new WeakSet();
  11716. const { prefix = '[vite]', allowClearScreen = true } = options;
  11717. const thresh = LogLevels[level];
  11718. const canClearScreen = allowClearScreen && process.stdout.isTTY && !process.env.CI;
  11719. const clear = canClearScreen ? clearScreen : () => { };
  11720. function format(type, msg, options = {}) {
  11721. if (options.timestamp) {
  11722. const tag = type === 'info'
  11723. ? colors$1.cyan(colors$1.bold(prefix))
  11724. : type === 'warn'
  11725. ? colors$1.yellow(colors$1.bold(prefix))
  11726. : colors$1.red(colors$1.bold(prefix));
  11727. return `${colors$1.dim(getTimeFormatter().format(new Date()))} ${tag} ${msg}`;
  11728. }
  11729. else {
  11730. return msg;
  11731. }
  11732. }
  11733. function output(type, msg, options = {}) {
  11734. if (thresh >= LogLevels[type]) {
  11735. const method = type === 'info' ? 'log' : type;
  11736. if (options.error) {
  11737. loggedErrors.add(options.error);
  11738. }
  11739. if (canClearScreen) {
  11740. if (type === lastType && msg === lastMsg) {
  11741. sameCount++;
  11742. clear();
  11743. console[method](format(type, msg, options), colors$1.yellow(`(x${sameCount + 1})`));
  11744. }
  11745. else {
  11746. sameCount = 0;
  11747. lastMsg = msg;
  11748. lastType = type;
  11749. if (options.clear) {
  11750. clear();
  11751. }
  11752. console[method](format(type, msg, options));
  11753. }
  11754. }
  11755. else {
  11756. console[method](format(type, msg, options));
  11757. }
  11758. }
  11759. }
  11760. const warnedMessages = new Set();
  11761. const logger = {
  11762. hasWarned: false,
  11763. info(msg, opts) {
  11764. output('info', msg, opts);
  11765. },
  11766. warn(msg, opts) {
  11767. logger.hasWarned = true;
  11768. output('warn', msg, opts);
  11769. },
  11770. warnOnce(msg, opts) {
  11771. if (warnedMessages.has(msg))
  11772. return;
  11773. logger.hasWarned = true;
  11774. output('warn', msg, opts);
  11775. warnedMessages.add(msg);
  11776. },
  11777. error(msg, opts) {
  11778. logger.hasWarned = true;
  11779. output('error', msg, opts);
  11780. },
  11781. clearScreen(type) {
  11782. if (thresh >= LogLevels[type]) {
  11783. clear();
  11784. }
  11785. },
  11786. hasErrorLogged(error) {
  11787. return loggedErrors.has(error);
  11788. },
  11789. };
  11790. return logger;
  11791. }
  11792. function printServerUrls(urls, optionsHost, info) {
  11793. const colorUrl = (url) => colors$1.cyan(url.replace(/:(\d+)\//, (_, port) => `:${colors$1.bold(port)}/`));
  11794. for (const url of urls.local) {
  11795. info(` ${colors$1.green('➜')} ${colors$1.bold('Local')}: ${colorUrl(url)}`);
  11796. }
  11797. for (const url of urls.network) {
  11798. info(` ${colors$1.green('➜')} ${colors$1.bold('Network')}: ${colorUrl(url)}`);
  11799. }
  11800. if (urls.network.length === 0 && optionsHost === undefined) {
  11801. info(colors$1.dim(` ${colors$1.green('➜')} ${colors$1.bold('Network')}: use `) +
  11802. colors$1.bold('--host') +
  11803. colors$1.dim(' to expose'));
  11804. }
  11805. }
  11806. const groups = [
  11807. { name: 'Assets', color: colors$1.green },
  11808. { name: 'CSS', color: colors$1.magenta },
  11809. { name: 'JS', color: colors$1.cyan },
  11810. ];
  11811. const COMPRESSIBLE_ASSETS_RE = /\.(?:html|json|svg|txt|xml|xhtml)$/;
  11812. function buildReporterPlugin(config) {
  11813. const compress = promisify$4(gzip);
  11814. const chunkLimit = config.build.chunkSizeWarningLimit;
  11815. const numberFormatter = new Intl.NumberFormat('en', {
  11816. maximumFractionDigits: 2,
  11817. minimumFractionDigits: 2,
  11818. });
  11819. const displaySize = (bytes) => {
  11820. return `${numberFormatter.format(bytes / 1000)} kB`;
  11821. };
  11822. const tty = process.stdout.isTTY && !process.env.CI;
  11823. const shouldLogInfo = LogLevels[config.logLevel || 'info'] >= LogLevels.info;
  11824. let hasTransformed = false;
  11825. let hasRenderedChunk = false;
  11826. let hasCompressChunk = false;
  11827. let transformedCount = 0;
  11828. let chunkCount = 0;
  11829. let compressedCount = 0;
  11830. async function getCompressedSize(code) {
  11831. if (config.build.ssr || !config.build.reportCompressedSize) {
  11832. return null;
  11833. }
  11834. if (shouldLogInfo && !hasCompressChunk) {
  11835. if (!tty) {
  11836. config.logger.info('computing gzip size...');
  11837. }
  11838. else {
  11839. writeLine('computing gzip size (0)...');
  11840. }
  11841. hasCompressChunk = true;
  11842. }
  11843. const compressed = await compress(typeof code === 'string' ? code : Buffer.from(code));
  11844. compressedCount++;
  11845. if (shouldLogInfo && tty) {
  11846. writeLine(`computing gzip size (${compressedCount})...`);
  11847. }
  11848. return compressed.length;
  11849. }
  11850. const logTransform = throttle((id) => {
  11851. writeLine(`transforming (${transformedCount}) ${colors$1.dim(path$o.relative(config.root, id))}`);
  11852. });
  11853. return {
  11854. name: 'vite:reporter',
  11855. transform(_, id) {
  11856. transformedCount++;
  11857. if (shouldLogInfo) {
  11858. if (!tty) {
  11859. if (!hasTransformed) {
  11860. config.logger.info(`transforming...`);
  11861. }
  11862. }
  11863. else {
  11864. if (id.includes(`?`))
  11865. return;
  11866. logTransform(id);
  11867. }
  11868. hasTransformed = true;
  11869. }
  11870. return null;
  11871. },
  11872. buildStart() {
  11873. transformedCount = 0;
  11874. },
  11875. buildEnd() {
  11876. if (shouldLogInfo) {
  11877. if (tty) {
  11878. clearLine$1();
  11879. }
  11880. config.logger.info(`${colors$1.green(`✓`)} ${transformedCount} modules transformed.`);
  11881. }
  11882. },
  11883. renderStart() {
  11884. chunkCount = 0;
  11885. compressedCount = 0;
  11886. },
  11887. renderChunk(code, chunk, options) {
  11888. if (!options.inlineDynamicImports) {
  11889. for (const id of chunk.moduleIds) {
  11890. const module = this.getModuleInfo(id);
  11891. if (!module)
  11892. continue;
  11893. // When a dynamic importer shares a chunk with the imported module,
  11894. // warn that the dynamic imported module will not be moved to another chunk (#12850).
  11895. if (module.importers.length && module.dynamicImporters.length) {
  11896. // Filter out the intersection of dynamic importers and sibling modules in
  11897. // the same chunk. The intersecting dynamic importers' dynamic import is not
  11898. // expected to work. Note we're only detecting the direct ineffective
  11899. // dynamic import here.
  11900. const detectedIneffectiveDynamicImport = module.dynamicImporters.some((id) => !isInNodeModules$1(id) && chunk.moduleIds.includes(id));
  11901. if (detectedIneffectiveDynamicImport) {
  11902. this.warn(`\n(!) ${module.id} is dynamically imported by ${module.dynamicImporters.join(', ')} but also statically imported by ${module.importers.join(', ')}, dynamic import will not move module into another chunk.\n`);
  11903. }
  11904. }
  11905. }
  11906. }
  11907. chunkCount++;
  11908. if (shouldLogInfo) {
  11909. if (!tty) {
  11910. if (!hasRenderedChunk) {
  11911. config.logger.info('rendering chunks...');
  11912. }
  11913. }
  11914. else {
  11915. writeLine(`rendering chunks (${chunkCount})...`);
  11916. }
  11917. hasRenderedChunk = true;
  11918. }
  11919. return null;
  11920. },
  11921. generateBundle() {
  11922. if (shouldLogInfo && tty)
  11923. clearLine$1();
  11924. },
  11925. async writeBundle({ dir: outDir }, output) {
  11926. let hasLargeChunks = false;
  11927. if (shouldLogInfo) {
  11928. const entries = (await Promise.all(Object.values(output).map(async (chunk) => {
  11929. if (chunk.type === 'chunk') {
  11930. return {
  11931. name: chunk.fileName,
  11932. group: 'JS',
  11933. size: chunk.code.length,
  11934. compressedSize: await getCompressedSize(chunk.code),
  11935. mapSize: chunk.map ? chunk.map.toString().length : null,
  11936. };
  11937. }
  11938. else {
  11939. if (chunk.fileName.endsWith('.map'))
  11940. return null;
  11941. const isCSS = chunk.fileName.endsWith('.css');
  11942. const isCompressible = isCSS || COMPRESSIBLE_ASSETS_RE.test(chunk.fileName);
  11943. return {
  11944. name: chunk.fileName,
  11945. group: isCSS ? 'CSS' : 'Assets',
  11946. size: chunk.source.length,
  11947. mapSize: null,
  11948. compressedSize: isCompressible
  11949. ? await getCompressedSize(chunk.source)
  11950. : null,
  11951. };
  11952. }
  11953. }))).filter(isDefined);
  11954. if (tty)
  11955. clearLine$1();
  11956. let longest = 0;
  11957. let biggestSize = 0;
  11958. let biggestMap = 0;
  11959. let biggestCompressSize = 0;
  11960. for (const entry of entries) {
  11961. if (entry.name.length > longest)
  11962. longest = entry.name.length;
  11963. if (entry.size > biggestSize)
  11964. biggestSize = entry.size;
  11965. if (entry.mapSize && entry.mapSize > biggestMap) {
  11966. biggestMap = entry.mapSize;
  11967. }
  11968. if (entry.compressedSize &&
  11969. entry.compressedSize > biggestCompressSize) {
  11970. biggestCompressSize = entry.compressedSize;
  11971. }
  11972. }
  11973. const sizePad = displaySize(biggestSize).length;
  11974. const mapPad = displaySize(biggestMap).length;
  11975. const compressPad = displaySize(biggestCompressSize).length;
  11976. const relativeOutDir = normalizePath$3(path$o.relative(config.root, path$o.resolve(config.root, outDir ?? config.build.outDir)));
  11977. const assetsDir = path$o.join(config.build.assetsDir, '/');
  11978. for (const group of groups) {
  11979. const filtered = entries.filter((e) => e.group === group.name);
  11980. if (!filtered.length)
  11981. continue;
  11982. for (const entry of filtered.sort((a, z) => a.size - z.size)) {
  11983. const isLarge = group.name === 'JS' && entry.size / 1000 > chunkLimit;
  11984. if (isLarge)
  11985. hasLargeChunks = true;
  11986. const sizeColor = isLarge ? colors$1.yellow : colors$1.dim;
  11987. let log = colors$1.dim(withTrailingSlash(relativeOutDir));
  11988. log +=
  11989. !config.build.lib &&
  11990. entry.name.startsWith(withTrailingSlash(assetsDir))
  11991. ? colors$1.dim(assetsDir) +
  11992. group.color(entry.name
  11993. .slice(assetsDir.length)
  11994. .padEnd(longest + 2 - assetsDir.length))
  11995. : group.color(entry.name.padEnd(longest + 2));
  11996. log += colors$1.bold(sizeColor(displaySize(entry.size).padStart(sizePad)));
  11997. if (entry.compressedSize) {
  11998. log += colors$1.dim(` │ gzip: ${displaySize(entry.compressedSize).padStart(compressPad)}`);
  11999. }
  12000. if (entry.mapSize) {
  12001. log += colors$1.dim(` │ map: ${displaySize(entry.mapSize).padStart(mapPad)}`);
  12002. }
  12003. config.logger.info(log);
  12004. }
  12005. }
  12006. }
  12007. else {
  12008. hasLargeChunks = Object.values(output).some((chunk) => {
  12009. return chunk.type === 'chunk' && chunk.code.length / 1000 > chunkLimit;
  12010. });
  12011. }
  12012. if (hasLargeChunks &&
  12013. config.build.minify &&
  12014. !config.build.lib &&
  12015. !config.build.ssr) {
  12016. config.logger.warn(colors$1.yellow(`\n(!) Some chunks are larger than ${chunkLimit} kB after minification. Consider:\n` +
  12017. `- Using dynamic import() to code-split the application\n` +
  12018. `- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks\n` +
  12019. `- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.`));
  12020. }
  12021. },
  12022. };
  12023. }
  12024. function writeLine(output) {
  12025. clearLine$1();
  12026. if (output.length < process.stdout.columns) {
  12027. process.stdout.write(output);
  12028. }
  12029. else {
  12030. process.stdout.write(output.substring(0, process.stdout.columns - 1));
  12031. }
  12032. }
  12033. function clearLine$1() {
  12034. process.stdout.clearLine(0);
  12035. process.stdout.cursorTo(0);
  12036. }
  12037. function throttle(fn) {
  12038. let timerHandle = null;
  12039. return (...args) => {
  12040. if (timerHandle)
  12041. return;
  12042. fn(...args);
  12043. timerHandle = setTimeout(() => {
  12044. timerHandle = null;
  12045. }, 100);
  12046. };
  12047. }
  12048. const POSIX_SEP_RE = new RegExp('\\' + path$o.posix.sep, 'g');
  12049. const NATIVE_SEP_RE = new RegExp('\\' + path$o.sep, 'g');
  12050. /** @type {Map<string,RegExp>}*/
  12051. const PATTERN_REGEX_CACHE = new Map();
  12052. const GLOB_ALL_PATTERN = `**/*`;
  12053. const TS_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts'];
  12054. const JS_EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs'];
  12055. const TSJS_EXTENSIONS = TS_EXTENSIONS.concat(JS_EXTENSIONS);
  12056. const TS_EXTENSIONS_RE_GROUP = `\\.(?:${TS_EXTENSIONS.map((ext) => ext.substring(1)).join('|')})`;
  12057. const TSJS_EXTENSIONS_RE_GROUP = `\\.(?:${TSJS_EXTENSIONS.map((ext) => ext.substring(1)).join(
  12058. '|'
  12059. )})`;
  12060. const IS_POSIX = path$o.posix.sep === path$o.sep;
  12061. /**
  12062. * @template T
  12063. * @returns {{resolve:(result:T)=>void, reject:(error:any)=>void, promise: Promise<T>}}
  12064. */
  12065. function makePromise() {
  12066. let resolve, reject;
  12067. const promise = new Promise((res, rej) => {
  12068. resolve = res;
  12069. reject = rej;
  12070. });
  12071. return { promise, resolve, reject };
  12072. }
  12073. /**
  12074. * @param {string} filename
  12075. * @param {import('./cache.js').TSConfckCache} [cache]
  12076. * @returns {Promise<string|void>}
  12077. */
  12078. async function resolveTSConfigJson(filename, cache) {
  12079. if (path$o.extname(filename) !== '.json') {
  12080. return; // ignore files that are not json
  12081. }
  12082. const tsconfig = path$o.resolve(filename);
  12083. if (cache && (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename))) {
  12084. return tsconfig;
  12085. }
  12086. return promises$1.stat(tsconfig).then((stat) => {
  12087. if (stat.isFile() || stat.isFIFO()) {
  12088. return tsconfig;
  12089. } else {
  12090. throw new Error(`${filename} exists but is not a regular file.`);
  12091. }
  12092. });
  12093. }
  12094. /**
  12095. *
  12096. * @param {string} dir an absolute directory path
  12097. * @returns {boolean} if dir path includes a node_modules segment
  12098. */
  12099. const isInNodeModules = IS_POSIX
  12100. ? (dir) => dir.includes('/node_modules/')
  12101. : (dir) => dir.match(/[/\\]node_modules[/\\]/);
  12102. /**
  12103. * convert posix separator to native separator
  12104. *
  12105. * eg.
  12106. * windows: C:/foo/bar -> c:\foo\bar
  12107. * linux: /foo/bar -> /foo/bar
  12108. *
  12109. * @param {string} filename with posix separators
  12110. * @returns {string} filename with native separators
  12111. */
  12112. const posix2native = IS_POSIX
  12113. ? (filename) => filename
  12114. : (filename) => filename.replace(POSIX_SEP_RE, path$o.sep);
  12115. /**
  12116. * convert native separator to posix separator
  12117. *
  12118. * eg.
  12119. * windows: C:\foo\bar -> c:/foo/bar
  12120. * linux: /foo/bar -> /foo/bar
  12121. *
  12122. * @param {string} filename - filename with native separators
  12123. * @returns {string} filename with posix separators
  12124. */
  12125. const native2posix = IS_POSIX
  12126. ? (filename) => filename
  12127. : (filename) => filename.replace(NATIVE_SEP_RE, path$o.posix.sep);
  12128. /**
  12129. * converts params to native separator, resolves path and converts native back to posix
  12130. *
  12131. * needed on windows to handle posix paths in tsconfig
  12132. *
  12133. * @param dir {string|null} directory to resolve from
  12134. * @param filename {string} filename or pattern to resolve
  12135. * @returns string
  12136. */
  12137. const resolve2posix = IS_POSIX
  12138. ? (dir, filename) => (dir ? path$o.resolve(dir, filename) : path$o.resolve(filename))
  12139. : (dir, filename) =>
  12140. native2posix(
  12141. dir
  12142. ? path$o.resolve(posix2native(dir), posix2native(filename))
  12143. : path$o.resolve(posix2native(filename))
  12144. );
  12145. /**
  12146. *
  12147. * @param {import('./public.d.ts').TSConfckParseResult} result
  12148. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  12149. * @returns {string[]}
  12150. */
  12151. function resolveReferencedTSConfigFiles(result, options) {
  12152. const dir = path$o.dirname(result.tsconfigFile);
  12153. return result.tsconfig.references.map((ref) => {
  12154. const refPath = ref.path.endsWith('.json')
  12155. ? ref.path
  12156. : path$o.join(ref.path, options?.configName ?? 'tsconfig.json');
  12157. return resolve2posix(dir, refPath);
  12158. });
  12159. }
  12160. /**
  12161. * @param {string} filename
  12162. * @param {import('./public.d.ts').TSConfckParseResult} result
  12163. * @returns {import('./public.d.ts').TSConfckParseResult}
  12164. */
  12165. function resolveSolutionTSConfig(filename, result) {
  12166. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  12167. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  12168. if (
  12169. result.referenced &&
  12170. extensions.some((ext) => filename.endsWith(ext)) &&
  12171. !isIncluded(filename, result)
  12172. ) {
  12173. const solutionTSConfig = result.referenced.find((referenced) =>
  12174. isIncluded(filename, referenced)
  12175. );
  12176. if (solutionTSConfig) {
  12177. return solutionTSConfig;
  12178. }
  12179. }
  12180. return result;
  12181. }
  12182. /**
  12183. *
  12184. * @param {string} filename
  12185. * @param {import('./public.d.ts').TSConfckParseResult} result
  12186. * @returns {boolean}
  12187. */
  12188. function isIncluded(filename, result) {
  12189. const dir = native2posix(path$o.dirname(result.tsconfigFile));
  12190. const files = (result.tsconfig.files || []).map((file) => resolve2posix(dir, file));
  12191. const absoluteFilename = resolve2posix(null, filename);
  12192. if (files.includes(filename)) {
  12193. return true;
  12194. }
  12195. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  12196. const isIncluded = isGlobMatch(
  12197. absoluteFilename,
  12198. dir,
  12199. result.tsconfig.include || (result.tsconfig.files ? [] : [GLOB_ALL_PATTERN]),
  12200. allowJs
  12201. );
  12202. if (isIncluded) {
  12203. const isExcluded = isGlobMatch(absoluteFilename, dir, result.tsconfig.exclude || [], allowJs);
  12204. return !isExcluded;
  12205. }
  12206. return false;
  12207. }
  12208. /**
  12209. * test filenames agains glob patterns in tsconfig
  12210. *
  12211. * @param filename {string} posix style abolute path to filename to test
  12212. * @param dir {string} posix style absolute path to directory of tsconfig containing patterns
  12213. * @param patterns {string[]} glob patterns to match against
  12214. * @param allowJs {boolean} allowJs setting in tsconfig to include js extensions in checks
  12215. * @returns {boolean} true when at least one pattern matches filename
  12216. */
  12217. function isGlobMatch(filename, dir, patterns, allowJs) {
  12218. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  12219. return patterns.some((pattern) => {
  12220. // filename must end with part of pattern that comes after last wildcard
  12221. let lastWildcardIndex = pattern.length;
  12222. let hasWildcard = false;
  12223. for (let i = pattern.length - 1; i > -1; i--) {
  12224. if (pattern[i] === '*' || pattern[i] === '?') {
  12225. lastWildcardIndex = i;
  12226. hasWildcard = true;
  12227. break;
  12228. }
  12229. }
  12230. // if pattern does not end with wildcard, filename must end with pattern after last wildcard
  12231. if (
  12232. lastWildcardIndex < pattern.length - 1 &&
  12233. !filename.endsWith(pattern.slice(lastWildcardIndex + 1))
  12234. ) {
  12235. return false;
  12236. }
  12237. // if pattern ends with *, filename must end with a default extension
  12238. if (pattern.endsWith('*') && !extensions.some((ext) => filename.endsWith(ext))) {
  12239. return false;
  12240. }
  12241. // for **/* , filename must start with the dir
  12242. if (pattern === GLOB_ALL_PATTERN) {
  12243. return filename.startsWith(`${dir}/`);
  12244. }
  12245. const resolvedPattern = resolve2posix(dir, pattern);
  12246. // filename must start with part of pattern that comes before first wildcard
  12247. let firstWildcardIndex = -1;
  12248. for (let i = 0; i < resolvedPattern.length; i++) {
  12249. if (resolvedPattern[i] === '*' || resolvedPattern[i] === '?') {
  12250. firstWildcardIndex = i;
  12251. hasWildcard = true;
  12252. break;
  12253. }
  12254. }
  12255. if (
  12256. firstWildcardIndex > 1 &&
  12257. !filename.startsWith(resolvedPattern.slice(0, firstWildcardIndex - 1))
  12258. ) {
  12259. return false;
  12260. }
  12261. // if no wildcard in pattern, filename must be equal to resolved pattern
  12262. if (!hasWildcard) {
  12263. return filename === resolvedPattern;
  12264. }
  12265. // complex pattern, use regex to check it
  12266. if (PATTERN_REGEX_CACHE.has(resolvedPattern)) {
  12267. return PATTERN_REGEX_CACHE.get(resolvedPattern).test(filename);
  12268. }
  12269. const regex = pattern2regex(resolvedPattern, allowJs);
  12270. PATTERN_REGEX_CACHE.set(resolvedPattern, regex);
  12271. return regex.test(filename);
  12272. });
  12273. }
  12274. /**
  12275. * @param {string} resolvedPattern
  12276. * @param {boolean} allowJs
  12277. * @returns {RegExp}
  12278. */
  12279. function pattern2regex(resolvedPattern, allowJs) {
  12280. let regexStr = '^';
  12281. for (let i = 0; i < resolvedPattern.length; i++) {
  12282. const char = resolvedPattern[i];
  12283. if (char === '?') {
  12284. regexStr += '[^\\/]';
  12285. continue;
  12286. }
  12287. if (char === '*') {
  12288. if (resolvedPattern[i + 1] === '*' && resolvedPattern[i + 2] === '/') {
  12289. i += 2;
  12290. regexStr += '(?:[^\\/]*\\/)*'; // zero or more path segments
  12291. continue;
  12292. }
  12293. regexStr += '[^\\/]*';
  12294. continue;
  12295. }
  12296. if ('/.+^${}()|[]\\'.includes(char)) {
  12297. regexStr += `\\`;
  12298. }
  12299. regexStr += char;
  12300. }
  12301. // add known file endings if pattern ends on *
  12302. if (resolvedPattern.endsWith('*')) {
  12303. regexStr += allowJs ? TSJS_EXTENSIONS_RE_GROUP : TS_EXTENSIONS_RE_GROUP;
  12304. }
  12305. regexStr += '$';
  12306. return new RegExp(regexStr);
  12307. }
  12308. /**
  12309. * find the closest tsconfig.json file
  12310. *
  12311. * @param {string} filename - path to file to find tsconfig for (absolute or relative to cwd)
  12312. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  12313. * @returns {Promise<string|null>} absolute path to closest tsconfig.json or null if not found
  12314. */
  12315. async function find(filename, options) {
  12316. let dir = path$o.dirname(path$o.resolve(filename));
  12317. if (options?.ignoreNodeModules && isInNodeModules(dir)) {
  12318. return null;
  12319. }
  12320. const cache = options?.cache;
  12321. const configName = options?.configName ?? 'tsconfig.json';
  12322. if (cache?.hasConfigPath(dir, configName)) {
  12323. return cache.getConfigPath(dir, configName);
  12324. }
  12325. const { /** @type {Promise<string|null>} */ promise, resolve, reject } = makePromise();
  12326. if (options?.root && !path$o.isAbsolute(options.root)) {
  12327. options.root = path$o.resolve(options.root);
  12328. }
  12329. findUp(dir, { promise, resolve, reject }, options);
  12330. return promise;
  12331. }
  12332. /**
  12333. *
  12334. * @param {string} dir
  12335. * @param {{promise:Promise<string|null>,resolve:(result:string|null)=>void,reject:(err:any)=>void}} madePromise
  12336. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  12337. */
  12338. function findUp(dir, { resolve, reject, promise }, options) {
  12339. const { cache, root, configName } = options ?? {};
  12340. if (cache) {
  12341. if (cache.hasConfigPath(dir, configName)) {
  12342. let cached;
  12343. try {
  12344. cached = cache.getConfigPath(dir, configName);
  12345. } catch (e) {
  12346. reject(e);
  12347. return;
  12348. }
  12349. if (cached?.then) {
  12350. cached.then(resolve).catch(reject);
  12351. } else {
  12352. resolve(cached);
  12353. }
  12354. } else {
  12355. cache.setConfigPath(dir, promise, configName);
  12356. }
  12357. }
  12358. const tsconfig = path$o.join(dir, options?.configName ?? 'tsconfig.json');
  12359. fs$l.stat(tsconfig, (err, stats) => {
  12360. if (stats && (stats.isFile() || stats.isFIFO())) {
  12361. resolve(tsconfig);
  12362. } else if (err?.code !== 'ENOENT') {
  12363. reject(err);
  12364. } else {
  12365. let parent;
  12366. if (root === dir || (parent = path$o.dirname(dir)) === dir) {
  12367. resolve(null);
  12368. } else {
  12369. findUp(parent, { promise, resolve, reject }, options);
  12370. }
  12371. }
  12372. });
  12373. }
  12374. /*
  12375. this file contains code from strip-bom and strip-json-comments by Sindre Sorhus
  12376. https://github.com/sindresorhus/strip-json-comments/blob/v4.0.0/index.js
  12377. https://github.com/sindresorhus/strip-bom/blob/v5.0.0/index.js
  12378. licensed under MIT, see ../LICENSE
  12379. */
  12380. /**
  12381. * convert content of tsconfig.json to regular json
  12382. *
  12383. * @param {string} tsconfigJson - content of tsconfig.json
  12384. * @returns {string} content as regular json, comments and dangling commas have been replaced with whitespace
  12385. */
  12386. function toJson(tsconfigJson) {
  12387. const stripped = stripDanglingComma(stripJsonComments(stripBom(tsconfigJson)));
  12388. if (stripped.trim() === '') {
  12389. // only whitespace left after stripping, return empty object so that JSON.parse still works
  12390. return '{}';
  12391. } else {
  12392. return stripped;
  12393. }
  12394. }
  12395. /**
  12396. * replace dangling commas from pseudo-json string with single space
  12397. * implementation heavily inspired by strip-json-comments
  12398. *
  12399. * @param {string} pseudoJson
  12400. * @returns {string}
  12401. */
  12402. function stripDanglingComma(pseudoJson) {
  12403. let insideString = false;
  12404. let offset = 0;
  12405. let result = '';
  12406. let danglingCommaPos = null;
  12407. for (let i = 0; i < pseudoJson.length; i++) {
  12408. const currentCharacter = pseudoJson[i];
  12409. if (currentCharacter === '"') {
  12410. const escaped = isEscaped(pseudoJson, i);
  12411. if (!escaped) {
  12412. insideString = !insideString;
  12413. }
  12414. }
  12415. if (insideString) {
  12416. danglingCommaPos = null;
  12417. continue;
  12418. }
  12419. if (currentCharacter === ',') {
  12420. danglingCommaPos = i;
  12421. continue;
  12422. }
  12423. if (danglingCommaPos) {
  12424. if (currentCharacter === '}' || currentCharacter === ']') {
  12425. result += pseudoJson.slice(offset, danglingCommaPos) + ' ';
  12426. offset = danglingCommaPos + 1;
  12427. danglingCommaPos = null;
  12428. } else if (!currentCharacter.match(/\s/)) {
  12429. danglingCommaPos = null;
  12430. }
  12431. }
  12432. }
  12433. return result + pseudoJson.substring(offset);
  12434. }
  12435. // start strip-json-comments
  12436. /**
  12437. *
  12438. * @param {string} jsonString
  12439. * @param {number} quotePosition
  12440. * @returns {boolean}
  12441. */
  12442. function isEscaped(jsonString, quotePosition) {
  12443. let index = quotePosition - 1;
  12444. let backslashCount = 0;
  12445. while (jsonString[index] === '\\') {
  12446. index -= 1;
  12447. backslashCount += 1;
  12448. }
  12449. return Boolean(backslashCount % 2);
  12450. }
  12451. /**
  12452. *
  12453. * @param {string} string
  12454. * @param {number?} start
  12455. * @param {number?} end
  12456. */
  12457. function strip(string, start, end) {
  12458. return string.slice(start, end).replace(/\S/g, ' ');
  12459. }
  12460. const singleComment = Symbol('singleComment');
  12461. const multiComment = Symbol('multiComment');
  12462. /**
  12463. * @param {string} jsonString
  12464. * @returns {string}
  12465. */
  12466. function stripJsonComments(jsonString) {
  12467. let isInsideString = false;
  12468. /** @type {false | symbol} */
  12469. let isInsideComment = false;
  12470. let offset = 0;
  12471. let result = '';
  12472. for (let index = 0; index < jsonString.length; index++) {
  12473. const currentCharacter = jsonString[index];
  12474. const nextCharacter = jsonString[index + 1];
  12475. if (!isInsideComment && currentCharacter === '"') {
  12476. const escaped = isEscaped(jsonString, index);
  12477. if (!escaped) {
  12478. isInsideString = !isInsideString;
  12479. }
  12480. }
  12481. if (isInsideString) {
  12482. continue;
  12483. }
  12484. if (!isInsideComment && currentCharacter + nextCharacter === '//') {
  12485. result += jsonString.slice(offset, index);
  12486. offset = index;
  12487. isInsideComment = singleComment;
  12488. index++;
  12489. } else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
  12490. index++;
  12491. isInsideComment = false;
  12492. result += strip(jsonString, offset, index);
  12493. offset = index;
  12494. } else if (isInsideComment === singleComment && currentCharacter === '\n') {
  12495. isInsideComment = false;
  12496. result += strip(jsonString, offset, index);
  12497. offset = index;
  12498. } else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
  12499. result += jsonString.slice(offset, index);
  12500. offset = index;
  12501. isInsideComment = multiComment;
  12502. index++;
  12503. } else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
  12504. index++;
  12505. isInsideComment = false;
  12506. result += strip(jsonString, offset, index + 1);
  12507. offset = index + 1;
  12508. }
  12509. }
  12510. return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
  12511. }
  12512. // end strip-json-comments
  12513. // start strip-bom
  12514. /**
  12515. * @param {string} string
  12516. * @returns {string}
  12517. */
  12518. function stripBom(string) {
  12519. // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
  12520. // conversion translates it to FEFF (UTF-16 BOM).
  12521. if (string.charCodeAt(0) === 0xfeff) {
  12522. return string.slice(1);
  12523. }
  12524. return string;
  12525. }
  12526. // end strip-bom
  12527. const not_found_result = {
  12528. tsconfigFile: null,
  12529. tsconfig: {}
  12530. };
  12531. /**
  12532. * parse the closest tsconfig.json file
  12533. *
  12534. * @param {string} filename - path to a tsconfig .json or a source file or directory (absolute or relative to cwd)
  12535. * @param {import('./public.d.ts').TSConfckParseOptions} [options] - options
  12536. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  12537. * @throws {TSConfckParseError}
  12538. */
  12539. async function parse$f(filename, options) {
  12540. /** @type {import('./cache.js').TSConfckCache} */
  12541. const cache = options?.cache;
  12542. if (cache?.hasParseResult(filename)) {
  12543. return getParsedDeep(filename, cache, options);
  12544. }
  12545. const {
  12546. resolve,
  12547. reject,
  12548. /** @type {Promise<import('./public.d.ts').TSConfckParseResult>}*/
  12549. promise
  12550. } = makePromise();
  12551. cache?.setParseResult(filename, promise, true);
  12552. try {
  12553. let tsconfigFile =
  12554. (await resolveTSConfigJson(filename, cache)) || (await find(filename, options));
  12555. if (!tsconfigFile) {
  12556. resolve(not_found_result);
  12557. return promise;
  12558. }
  12559. let result;
  12560. if (filename !== tsconfigFile && cache?.hasParseResult(tsconfigFile)) {
  12561. result = await getParsedDeep(tsconfigFile, cache, options);
  12562. } else {
  12563. result = await parseFile$1(tsconfigFile, cache, filename === tsconfigFile);
  12564. await Promise.all([parseExtends(result, cache), parseReferences(result, options)]);
  12565. }
  12566. resolve(resolveSolutionTSConfig(filename, result));
  12567. } catch (e) {
  12568. reject(e);
  12569. }
  12570. return promise;
  12571. }
  12572. /**
  12573. * ensure extends and references are parsed
  12574. *
  12575. * @param {string} filename - cached file
  12576. * @param {import('./cache.js').TSConfckCache} cache - cache
  12577. * @param {import('./public.d.ts').TSConfckParseOptions} options - options
  12578. */
  12579. async function getParsedDeep(filename, cache, options) {
  12580. const result = await cache.getParseResult(filename);
  12581. if (
  12582. (result.tsconfig.extends && !result.extended) ||
  12583. (result.tsconfig.references && !result.referenced)
  12584. ) {
  12585. const promise = Promise.all([
  12586. parseExtends(result, cache),
  12587. parseReferences(result, options)
  12588. ]).then(() => result);
  12589. cache.setParseResult(filename, promise, true);
  12590. return promise;
  12591. }
  12592. return result;
  12593. }
  12594. /**
  12595. *
  12596. * @param {string} tsconfigFile - path to tsconfig file
  12597. * @param {import('./cache.js').TSConfckCache} [cache] - cache
  12598. * @param {boolean} [skipCache] - skip cache
  12599. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  12600. */
  12601. async function parseFile$1(tsconfigFile, cache, skipCache) {
  12602. if (
  12603. !skipCache &&
  12604. cache?.hasParseResult(tsconfigFile) &&
  12605. !cache.getParseResult(tsconfigFile)._isRootFile_
  12606. ) {
  12607. return cache.getParseResult(tsconfigFile);
  12608. }
  12609. const promise = promises$1
  12610. .readFile(tsconfigFile, 'utf-8')
  12611. .then(toJson)
  12612. .then((json) => {
  12613. const parsed = JSON.parse(json);
  12614. applyDefaults(parsed, tsconfigFile);
  12615. return {
  12616. tsconfigFile,
  12617. tsconfig: normalizeTSConfig(parsed, path$o.dirname(tsconfigFile))
  12618. };
  12619. })
  12620. .catch((e) => {
  12621. throw new TSConfckParseError(
  12622. `parsing ${tsconfigFile} failed: ${e}`,
  12623. 'PARSE_FILE',
  12624. tsconfigFile,
  12625. e
  12626. );
  12627. });
  12628. if (
  12629. !skipCache &&
  12630. (!cache?.hasParseResult(tsconfigFile) || !cache.getParseResult(tsconfigFile)._isRootFile_)
  12631. ) {
  12632. cache?.setParseResult(tsconfigFile, promise);
  12633. }
  12634. return promise;
  12635. }
  12636. /**
  12637. * normalize to match the output of ts.parseJsonConfigFileContent
  12638. *
  12639. * @param {any} tsconfig - typescript tsconfig output
  12640. * @param {string} dir - directory
  12641. */
  12642. function normalizeTSConfig(tsconfig, dir) {
  12643. // set baseUrl to absolute path
  12644. if (tsconfig.compilerOptions?.baseUrl && !path$o.isAbsolute(tsconfig.compilerOptions.baseUrl)) {
  12645. tsconfig.compilerOptions.baseUrl = resolve2posix(dir, tsconfig.compilerOptions.baseUrl);
  12646. }
  12647. return tsconfig;
  12648. }
  12649. /**
  12650. *
  12651. * @param {import('./public.d.ts').TSConfckParseResult} result
  12652. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  12653. * @returns {Promise<void>}
  12654. */
  12655. async function parseReferences(result, options) {
  12656. if (!result.tsconfig.references) {
  12657. return;
  12658. }
  12659. const referencedFiles = resolveReferencedTSConfigFiles(result, options);
  12660. const referenced = await Promise.all(
  12661. referencedFiles.map((file) => parseFile$1(file, options?.cache))
  12662. );
  12663. await Promise.all(referenced.map((ref) => parseExtends(ref, options?.cache)));
  12664. referenced.forEach((ref) => {
  12665. ref.solution = result;
  12666. });
  12667. result.referenced = referenced;
  12668. }
  12669. /**
  12670. * @param {import('./public.d.ts').TSConfckParseResult} result
  12671. * @param {import('./cache.js').TSConfckCache}[cache]
  12672. * @returns {Promise<void>}
  12673. */
  12674. async function parseExtends(result, cache) {
  12675. if (!result.tsconfig.extends) {
  12676. return;
  12677. }
  12678. // use result as first element in extended
  12679. // but dereference tsconfig so that mergeExtended can modify the original without affecting extended[0]
  12680. /** @type {import('./public.d.ts').TSConfckParseResult[]} */
  12681. const extended = [
  12682. { tsconfigFile: result.tsconfigFile, tsconfig: JSON.parse(JSON.stringify(result.tsconfig)) }
  12683. ];
  12684. // flatten extends graph into extended
  12685. let pos = 0;
  12686. /** @type {string[]} */
  12687. const extendsPath = [];
  12688. let currentBranchDepth = 0;
  12689. while (pos < extended.length) {
  12690. const extending = extended[pos];
  12691. extendsPath.push(extending.tsconfigFile);
  12692. if (extending.tsconfig.extends) {
  12693. // keep following this branch
  12694. currentBranchDepth += 1;
  12695. /** @type {string[]} */
  12696. let resolvedExtends;
  12697. if (!Array.isArray(extending.tsconfig.extends)) {
  12698. resolvedExtends = [resolveExtends(extending.tsconfig.extends, extending.tsconfigFile)];
  12699. } else {
  12700. // reverse because typescript 5.0 treats ['a','b','c'] as c extends b extends a
  12701. resolvedExtends = extending.tsconfig.extends
  12702. .reverse()
  12703. .map((ex) => resolveExtends(ex, extending.tsconfigFile));
  12704. }
  12705. const circularExtends = resolvedExtends.find((tsconfigFile) =>
  12706. extendsPath.includes(tsconfigFile)
  12707. );
  12708. if (circularExtends) {
  12709. const circle = extendsPath.concat([circularExtends]).join(' -> ');
  12710. throw new TSConfckParseError(
  12711. `Circular dependency in "extends": ${circle}`,
  12712. 'EXTENDS_CIRCULAR',
  12713. result.tsconfigFile
  12714. );
  12715. }
  12716. // add new extends to the list directly after current
  12717. extended.splice(
  12718. pos + 1,
  12719. 0,
  12720. ...(await Promise.all(resolvedExtends.map((file) => parseFile$1(file, cache))))
  12721. );
  12722. } else {
  12723. // reached a leaf, backtrack to the last branching point and continue
  12724. extendsPath.splice(-currentBranchDepth);
  12725. currentBranchDepth = 0;
  12726. }
  12727. pos = pos + 1;
  12728. }
  12729. result.extended = extended;
  12730. // skip first as it is the original config
  12731. for (const ext of result.extended.slice(1)) {
  12732. extendTSConfig(result, ext);
  12733. }
  12734. }
  12735. /**
  12736. *
  12737. * @param {string} extended
  12738. * @param {string} from
  12739. * @returns {string}
  12740. */
  12741. function resolveExtends(extended, from) {
  12742. if (extended === '..') {
  12743. // see #149
  12744. extended = '../tsconfig.json';
  12745. }
  12746. const req = createRequire$2(from);
  12747. let error;
  12748. try {
  12749. return req.resolve(extended);
  12750. } catch (e) {
  12751. error = e;
  12752. }
  12753. if (extended[0] !== '.' && !path$o.isAbsolute(extended)) {
  12754. try {
  12755. return req.resolve(`${extended}/tsconfig.json`);
  12756. } catch (e) {
  12757. error = e;
  12758. }
  12759. }
  12760. throw new TSConfckParseError(
  12761. `failed to resolve "extends":"${extended}" in ${from}`,
  12762. 'EXTENDS_RESOLVE',
  12763. from,
  12764. error
  12765. );
  12766. }
  12767. // references, extends and custom keys are not carried over
  12768. const EXTENDABLE_KEYS = [
  12769. 'compilerOptions',
  12770. 'files',
  12771. 'include',
  12772. 'exclude',
  12773. 'watchOptions',
  12774. 'compileOnSave',
  12775. 'typeAcquisition',
  12776. 'buildOptions'
  12777. ];
  12778. /**
  12779. *
  12780. * @param {import('./public.d.ts').TSConfckParseResult} extending
  12781. * @param {import('./public.d.ts').TSConfckParseResult} extended
  12782. * @returns void
  12783. */
  12784. function extendTSConfig(extending, extended) {
  12785. const extendingConfig = extending.tsconfig;
  12786. const extendedConfig = extended.tsconfig;
  12787. const relativePath = native2posix(
  12788. path$o.relative(path$o.dirname(extending.tsconfigFile), path$o.dirname(extended.tsconfigFile))
  12789. );
  12790. for (const key of Object.keys(extendedConfig).filter((key) => EXTENDABLE_KEYS.includes(key))) {
  12791. if (key === 'compilerOptions') {
  12792. if (!extendingConfig.compilerOptions) {
  12793. extendingConfig.compilerOptions = {};
  12794. }
  12795. for (const option of Object.keys(extendedConfig.compilerOptions)) {
  12796. if (Object.prototype.hasOwnProperty.call(extendingConfig.compilerOptions, option)) {
  12797. continue; // already set
  12798. }
  12799. extendingConfig.compilerOptions[option] = rebaseRelative(
  12800. option,
  12801. extendedConfig.compilerOptions[option],
  12802. relativePath
  12803. );
  12804. }
  12805. } else if (extendingConfig[key] === undefined) {
  12806. if (key === 'watchOptions') {
  12807. extendingConfig.watchOptions = {};
  12808. for (const option of Object.keys(extendedConfig.watchOptions)) {
  12809. extendingConfig.watchOptions[option] = rebaseRelative(
  12810. option,
  12811. extendedConfig.watchOptions[option],
  12812. relativePath
  12813. );
  12814. }
  12815. } else {
  12816. extendingConfig[key] = rebaseRelative(key, extendedConfig[key], relativePath);
  12817. }
  12818. }
  12819. }
  12820. }
  12821. const REBASE_KEYS = [
  12822. // root
  12823. 'files',
  12824. 'include',
  12825. 'exclude',
  12826. // compilerOptions
  12827. 'baseUrl',
  12828. 'rootDir',
  12829. 'rootDirs',
  12830. 'typeRoots',
  12831. 'outDir',
  12832. 'outFile',
  12833. 'declarationDir',
  12834. // watchOptions
  12835. 'excludeDirectories',
  12836. 'excludeFiles'
  12837. ];
  12838. /** @typedef {string | string[]} PathValue */
  12839. /**
  12840. *
  12841. * @param {string} key
  12842. * @param {PathValue} value
  12843. * @param {string} prependPath
  12844. * @returns {PathValue}
  12845. */
  12846. function rebaseRelative(key, value, prependPath) {
  12847. if (!REBASE_KEYS.includes(key)) {
  12848. return value;
  12849. }
  12850. if (Array.isArray(value)) {
  12851. return value.map((x) => rebasePath(x, prependPath));
  12852. } else {
  12853. return rebasePath(value, prependPath);
  12854. }
  12855. }
  12856. /**
  12857. *
  12858. * @param {string} value
  12859. * @param {string} prependPath
  12860. * @returns {string}
  12861. */
  12862. function rebasePath(value, prependPath) {
  12863. if (path$o.isAbsolute(value)) {
  12864. return value;
  12865. } else {
  12866. // relative paths use posix syntax in tsconfig
  12867. return path$o.posix.normalize(path$o.posix.join(prependPath, value));
  12868. }
  12869. }
  12870. class TSConfckParseError extends Error {
  12871. /**
  12872. * error code
  12873. * @type {string}
  12874. */
  12875. code;
  12876. /**
  12877. * error cause
  12878. * @type { Error | undefined}
  12879. */
  12880. cause;
  12881. /**
  12882. * absolute path of tsconfig file where the error happened
  12883. * @type {string}
  12884. */
  12885. tsconfigFile;
  12886. /**
  12887. *
  12888. * @param {string} message - error message
  12889. * @param {string} code - error code
  12890. * @param {string} tsconfigFile - path to tsconfig file
  12891. * @param {Error?} cause - cause of this error
  12892. */
  12893. constructor(message, code, tsconfigFile, cause) {
  12894. super(message);
  12895. // Set the prototype explicitly.
  12896. Object.setPrototypeOf(this, TSConfckParseError.prototype);
  12897. this.name = TSConfckParseError.name;
  12898. this.code = code;
  12899. this.cause = cause;
  12900. this.tsconfigFile = tsconfigFile;
  12901. }
  12902. }
  12903. /**
  12904. *
  12905. * @param {any} tsconfig
  12906. * @param {string} tsconfigFile
  12907. */
  12908. function applyDefaults(tsconfig, tsconfigFile) {
  12909. if (isJSConfig(tsconfigFile)) {
  12910. tsconfig.compilerOptions = {
  12911. ...DEFAULT_JSCONFIG_COMPILER_OPTIONS,
  12912. ...tsconfig.compilerOptions
  12913. };
  12914. }
  12915. }
  12916. const DEFAULT_JSCONFIG_COMPILER_OPTIONS = {
  12917. allowJs: true,
  12918. maxNodeModuleJsDepth: 2,
  12919. allowSyntheticDefaultImports: true,
  12920. skipLibCheck: true,
  12921. noEmit: true
  12922. };
  12923. /**
  12924. * @param {string} configFileName
  12925. */
  12926. function isJSConfig(configFileName) {
  12927. return path$o.basename(configFileName) === 'jsconfig.json';
  12928. }
  12929. /** @template T */
  12930. class TSConfckCache {
  12931. /**
  12932. * clear cache, use this if you have a long running process and tsconfig files have been added,changed or deleted
  12933. */
  12934. clear() {
  12935. this.#configPaths.clear();
  12936. this.#parsed.clear();
  12937. }
  12938. /**
  12939. * has cached closest config for files in dir
  12940. * @param {string} dir
  12941. * @param {string} [configName=tsconfig.json]
  12942. * @returns {boolean}
  12943. */
  12944. hasConfigPath(dir, configName = 'tsconfig.json') {
  12945. return this.#configPaths.has(`${dir}/${configName}`);
  12946. }
  12947. /**
  12948. * get cached closest tsconfig for files in dir
  12949. * @param {string} dir
  12950. * @param {string} [configName=tsconfig.json]
  12951. * @returns {Promise<string|null>|string|null}
  12952. * @throws {unknown} if cached value is an error
  12953. */
  12954. getConfigPath(dir, configName = 'tsconfig.json') {
  12955. const key = `${dir}/${configName}`;
  12956. const value = this.#configPaths.get(key);
  12957. if (value == null || value.length || value.then) {
  12958. return value;
  12959. } else {
  12960. throw value;
  12961. }
  12962. }
  12963. /**
  12964. * has parsed tsconfig for file
  12965. * @param {string} file
  12966. * @returns {boolean}
  12967. */
  12968. hasParseResult(file) {
  12969. return this.#parsed.has(file);
  12970. }
  12971. /**
  12972. * get parsed tsconfig for file
  12973. * @param {string} file
  12974. * @returns {Promise<T>|T}
  12975. * @throws {unknown} if cached value is an error
  12976. */
  12977. getParseResult(file) {
  12978. const value = this.#parsed.get(file);
  12979. if (value.then || value.tsconfig) {
  12980. return value;
  12981. } else {
  12982. throw value; // cached error, rethrow
  12983. }
  12984. }
  12985. /**
  12986. * @internal
  12987. * @private
  12988. * @param file
  12989. * @param {boolean} isRootFile a flag to check if current file which involking the parse() api, used to distinguish the normal cache which only parsed by parseFile()
  12990. * @param {Promise<T>} result
  12991. */
  12992. setParseResult(file, result, isRootFile = false) {
  12993. // _isRootFile_ is a temporary property for Promise result, used to prevent deadlock with cache
  12994. Object.defineProperty(result, '_isRootFile_', {
  12995. value: isRootFile,
  12996. writable: false,
  12997. enumerable: false,
  12998. configurable: false
  12999. });
  13000. this.#parsed.set(file, result);
  13001. result
  13002. .then((parsed) => {
  13003. if (this.#parsed.get(file) === result) {
  13004. this.#parsed.set(file, parsed);
  13005. }
  13006. })
  13007. .catch((e) => {
  13008. if (this.#parsed.get(file) === result) {
  13009. this.#parsed.set(file, e);
  13010. }
  13011. });
  13012. }
  13013. /**
  13014. * @internal
  13015. * @private
  13016. * @param {string} dir
  13017. * @param {Promise<string|null>} configPath
  13018. * @param {string} [configName=tsconfig.json]
  13019. */
  13020. setConfigPath(dir, configPath, configName = 'tsconfig.json') {
  13021. const key = `${dir}/${configName}`;
  13022. this.#configPaths.set(key, configPath);
  13023. configPath
  13024. .then((path) => {
  13025. if (this.#configPaths.get(key) === configPath) {
  13026. this.#configPaths.set(key, path);
  13027. }
  13028. })
  13029. .catch((e) => {
  13030. if (this.#configPaths.get(key) === configPath) {
  13031. this.#configPaths.set(key, e);
  13032. }
  13033. });
  13034. }
  13035. /**
  13036. * map directories to their closest tsconfig.json
  13037. * @internal
  13038. * @private
  13039. * @type{Map<string,(Promise<string|null>|string|null)>}
  13040. */
  13041. #configPaths = new Map();
  13042. /**
  13043. * map files to their parsed tsconfig result
  13044. * @internal
  13045. * @private
  13046. * @type {Map<string,(Promise<T>|T)> }
  13047. */
  13048. #parsed = new Map();
  13049. }
  13050. const debug$h = createDebugger('vite:esbuild');
  13051. // IIFE content looks like `var MyLib = function() {`.
  13052. // Spaces are removed and parameters are mangled when minified
  13053. const IIFE_BEGIN_RE = /(const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/;
  13054. const validExtensionRE = /\.\w+$/;
  13055. const jsxExtensionsRE = /\.(?:j|t)sx\b/;
  13056. // the final build should always support dynamic import and import.meta.
  13057. // if they need to be polyfilled, plugin-legacy should be used.
  13058. // plugin-legacy detects these two features when checking for modern code.
  13059. const defaultEsbuildSupported = {
  13060. 'dynamic-import': true,
  13061. 'import-meta': true,
  13062. };
  13063. let server;
  13064. async function transformWithEsbuild(code, filename, options, inMap) {
  13065. let loader = options?.loader;
  13066. if (!loader) {
  13067. // if the id ends with a valid ext, use it (e.g. vue blocks)
  13068. // otherwise, cleanup the query before checking the ext
  13069. const ext = path$o
  13070. .extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename))
  13071. .slice(1);
  13072. if (ext === 'cjs' || ext === 'mjs') {
  13073. loader = 'js';
  13074. }
  13075. else if (ext === 'cts' || ext === 'mts') {
  13076. loader = 'ts';
  13077. }
  13078. else {
  13079. loader = ext;
  13080. }
  13081. }
  13082. let tsconfigRaw = options?.tsconfigRaw;
  13083. // if options provide tsconfigRaw in string, it takes highest precedence
  13084. if (typeof tsconfigRaw !== 'string') {
  13085. // these fields would affect the compilation result
  13086. // https://esbuild.github.io/content-types/#tsconfig-json
  13087. const meaningfulFields = [
  13088. 'alwaysStrict',
  13089. 'experimentalDecorators',
  13090. 'importsNotUsedAsValues',
  13091. 'jsx',
  13092. 'jsxFactory',
  13093. 'jsxFragmentFactory',
  13094. 'jsxImportSource',
  13095. 'preserveValueImports',
  13096. 'target',
  13097. 'useDefineForClassFields',
  13098. 'verbatimModuleSyntax',
  13099. ];
  13100. const compilerOptionsForFile = {};
  13101. if (loader === 'ts' || loader === 'tsx') {
  13102. const loadedTsconfig = await loadTsconfigJsonForFile(filename);
  13103. const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {};
  13104. for (const field of meaningfulFields) {
  13105. if (field in loadedCompilerOptions) {
  13106. // @ts-expect-error TypeScript can't tell they are of the same type
  13107. compilerOptionsForFile[field] = loadedCompilerOptions[field];
  13108. }
  13109. }
  13110. }
  13111. const compilerOptions = {
  13112. ...compilerOptionsForFile,
  13113. ...tsconfigRaw?.compilerOptions,
  13114. };
  13115. // esbuild uses `useDefineForClassFields: true` when `tsconfig.compilerOptions.target` isn't declared
  13116. // but we want `useDefineForClassFields: false` when `tsconfig.compilerOptions.target` isn't declared
  13117. // to align with the TypeScript's behavior
  13118. if (compilerOptions.useDefineForClassFields === undefined &&
  13119. compilerOptions.target === undefined) {
  13120. compilerOptions.useDefineForClassFields = false;
  13121. }
  13122. // esbuild uses tsconfig fields when both the normal options and tsconfig was set
  13123. // but we want to prioritize the normal options
  13124. if (options) {
  13125. options.jsx && (compilerOptions.jsx = undefined);
  13126. options.jsxFactory && (compilerOptions.jsxFactory = undefined);
  13127. options.jsxFragment && (compilerOptions.jsxFragmentFactory = undefined);
  13128. options.jsxImportSource && (compilerOptions.jsxImportSource = undefined);
  13129. }
  13130. tsconfigRaw = {
  13131. ...tsconfigRaw,
  13132. compilerOptions,
  13133. };
  13134. }
  13135. const resolvedOptions = {
  13136. sourcemap: true,
  13137. // ensure source file name contains full query
  13138. sourcefile: filename,
  13139. ...options,
  13140. loader,
  13141. tsconfigRaw,
  13142. };
  13143. // Some projects in the ecosystem are calling this function with an ESBuildOptions
  13144. // object and esbuild throws an error for extra fields
  13145. // @ts-expect-error include exists in ESBuildOptions
  13146. delete resolvedOptions.include;
  13147. // @ts-expect-error exclude exists in ESBuildOptions
  13148. delete resolvedOptions.exclude;
  13149. // @ts-expect-error jsxInject exists in ESBuildOptions
  13150. delete resolvedOptions.jsxInject;
  13151. try {
  13152. const result = await transform$1(code, resolvedOptions);
  13153. let map;
  13154. if (inMap && resolvedOptions.sourcemap) {
  13155. const nextMap = JSON.parse(result.map);
  13156. nextMap.sourcesContent = [];
  13157. map = combineSourcemaps(filename, [
  13158. nextMap,
  13159. inMap,
  13160. ]);
  13161. }
  13162. else {
  13163. map =
  13164. resolvedOptions.sourcemap && resolvedOptions.sourcemap !== 'inline'
  13165. ? JSON.parse(result.map)
  13166. : { mappings: '' };
  13167. }
  13168. return {
  13169. ...result,
  13170. map,
  13171. };
  13172. }
  13173. catch (e) {
  13174. debug$h?.(`esbuild error with options used: `, resolvedOptions);
  13175. // patch error information
  13176. if (e.errors) {
  13177. e.frame = '';
  13178. e.errors.forEach((m) => {
  13179. if (m.text === 'Experimental decorators are not currently enabled' ||
  13180. m.text ===
  13181. 'Parameter decorators only work when experimental decorators are enabled') {
  13182. m.text +=
  13183. '. Vite 5 now uses esbuild 0.18 and you need to enable them by adding "experimentalDecorators": true in your "tsconfig.json" file.';
  13184. }
  13185. e.frame += `\n` + prettifyMessage(m, code);
  13186. });
  13187. e.loc = e.errors[0].location;
  13188. }
  13189. throw e;
  13190. }
  13191. }
  13192. function esbuildPlugin(config) {
  13193. const options = config.esbuild;
  13194. const { jsxInject, include, exclude, ...esbuildTransformOptions } = options;
  13195. const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/);
  13196. // Remove optimization options for dev as we only need to transpile them,
  13197. // and for build as the final optimization is in `buildEsbuildPlugin`
  13198. const transformOptions = {
  13199. target: 'esnext',
  13200. charset: 'utf8',
  13201. ...esbuildTransformOptions,
  13202. minify: false,
  13203. minifyIdentifiers: false,
  13204. minifySyntax: false,
  13205. minifyWhitespace: false,
  13206. treeShaking: false,
  13207. // keepNames is not needed when minify is disabled.
  13208. // Also transforming multiple times with keepNames enabled breaks
  13209. // tree-shaking. (#9164)
  13210. keepNames: false,
  13211. supported: {
  13212. ...defaultEsbuildSupported,
  13213. ...esbuildTransformOptions.supported,
  13214. },
  13215. };
  13216. return {
  13217. name: 'vite:esbuild',
  13218. configureServer(_server) {
  13219. server = _server;
  13220. server.watcher
  13221. .on('add', reloadOnTsconfigChange)
  13222. .on('change', reloadOnTsconfigChange)
  13223. .on('unlink', reloadOnTsconfigChange);
  13224. },
  13225. buildEnd() {
  13226. // recycle serve to avoid preventing Node self-exit (#6815)
  13227. server = null;
  13228. },
  13229. async transform(code, id) {
  13230. if (filter(id) || filter(cleanUrl(id))) {
  13231. const result = await transformWithEsbuild(code, id, transformOptions);
  13232. if (result.warnings.length) {
  13233. result.warnings.forEach((m) => {
  13234. this.warn(prettifyMessage(m, code));
  13235. });
  13236. }
  13237. if (jsxInject && jsxExtensionsRE.test(id)) {
  13238. result.code = jsxInject + ';' + result.code;
  13239. }
  13240. return {
  13241. code: result.code,
  13242. map: result.map,
  13243. };
  13244. }
  13245. },
  13246. };
  13247. }
  13248. const rollupToEsbuildFormatMap = {
  13249. es: 'esm',
  13250. cjs: 'cjs',
  13251. // passing `var Lib = (() => {})()` to esbuild with format = "iife"
  13252. // will turn it to `(() => { var Lib = (() => {})() })()`,
  13253. // so we remove the format config to tell esbuild not doing this
  13254. //
  13255. // although esbuild doesn't change format, there is still possibility
  13256. // that `{ treeShaking: true }` removes a top-level no-side-effect variable
  13257. // like: `var Lib = 1`, which becomes `` after esbuild transforming,
  13258. // but thankfully rollup does not do this optimization now
  13259. iife: undefined,
  13260. };
  13261. const buildEsbuildPlugin = (config) => {
  13262. return {
  13263. name: 'vite:esbuild-transpile',
  13264. async renderChunk(code, chunk, opts) {
  13265. // @ts-expect-error injected by @vitejs/plugin-legacy
  13266. if (opts.__vite_skip_esbuild__) {
  13267. return null;
  13268. }
  13269. const options = resolveEsbuildTranspileOptions(config, opts.format);
  13270. if (!options) {
  13271. return null;
  13272. }
  13273. const res = await transformWithEsbuild(code, chunk.fileName, options);
  13274. if (config.build.lib) {
  13275. // #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
  13276. // names are minified potentially causing collision with other globals.
  13277. // We inject the helpers inside the wrappers.
  13278. // e.g. turn:
  13279. // <esbuild helpers> (function(){ /*actual content/* })()
  13280. // into:
  13281. // (function(){ <esbuild helpers> /*actual content/* })()
  13282. // Not using regex because it's too hard to rule out performance issues like #8738 #8099 #10900 #14065
  13283. // Instead, using plain string index manipulation (indexOf, slice) which is simple and performant
  13284. // We don't need to create a MagicString here because both the helpers and
  13285. // the headers don't modify the sourcemap
  13286. const esbuildCode = res.code;
  13287. const contentIndex = opts.format === 'iife'
  13288. ? Math.max(esbuildCode.search(IIFE_BEGIN_RE), 0)
  13289. : opts.format === 'umd'
  13290. ? esbuildCode.indexOf(`(function(`) // same for minified or not
  13291. : 0;
  13292. if (contentIndex > 0) {
  13293. const esbuildHelpers = esbuildCode.slice(0, contentIndex);
  13294. res.code = esbuildCode
  13295. .slice(contentIndex)
  13296. .replace(`"use strict";`, `"use strict";` + esbuildHelpers);
  13297. }
  13298. }
  13299. return res;
  13300. },
  13301. };
  13302. };
  13303. function resolveEsbuildTranspileOptions(config, format) {
  13304. const target = config.build.target;
  13305. const minify = config.build.minify === 'esbuild';
  13306. if ((!target || target === 'esnext') && !minify) {
  13307. return null;
  13308. }
  13309. // Do not minify whitespace for ES lib output since that would remove
  13310. // pure annotations and break tree-shaking
  13311. // https://github.com/vuejs/core/issues/2860#issuecomment-926882793
  13312. const isEsLibBuild = config.build.lib && format === 'es';
  13313. const esbuildOptions = config.esbuild || {};
  13314. const options = {
  13315. charset: 'utf8',
  13316. ...esbuildOptions,
  13317. loader: 'js',
  13318. target: target || undefined,
  13319. format: rollupToEsbuildFormatMap[format],
  13320. supported: {
  13321. ...defaultEsbuildSupported,
  13322. ...esbuildOptions.supported,
  13323. },
  13324. };
  13325. // If no minify, disable all minify options
  13326. if (!minify) {
  13327. return {
  13328. ...options,
  13329. minify: false,
  13330. minifyIdentifiers: false,
  13331. minifySyntax: false,
  13332. minifyWhitespace: false,
  13333. treeShaking: false,
  13334. };
  13335. }
  13336. // If user enable fine-grain minify options, minify with their options instead
  13337. if (options.minifyIdentifiers != null ||
  13338. options.minifySyntax != null ||
  13339. options.minifyWhitespace != null) {
  13340. if (isEsLibBuild) {
  13341. // Disable minify whitespace as it breaks tree-shaking
  13342. return {
  13343. ...options,
  13344. minify: false,
  13345. minifyIdentifiers: options.minifyIdentifiers ?? true,
  13346. minifySyntax: options.minifySyntax ?? true,
  13347. minifyWhitespace: false,
  13348. treeShaking: true,
  13349. };
  13350. }
  13351. else {
  13352. return {
  13353. ...options,
  13354. minify: false,
  13355. minifyIdentifiers: options.minifyIdentifiers ?? true,
  13356. minifySyntax: options.minifySyntax ?? true,
  13357. minifyWhitespace: options.minifyWhitespace ?? true,
  13358. treeShaking: true,
  13359. };
  13360. }
  13361. }
  13362. // Else apply default minify options
  13363. if (isEsLibBuild) {
  13364. // Minify all except whitespace as it breaks tree-shaking
  13365. return {
  13366. ...options,
  13367. minify: false,
  13368. minifyIdentifiers: true,
  13369. minifySyntax: true,
  13370. minifyWhitespace: false,
  13371. treeShaking: true,
  13372. };
  13373. }
  13374. else {
  13375. return {
  13376. ...options,
  13377. minify: true,
  13378. treeShaking: true,
  13379. };
  13380. }
  13381. }
  13382. function prettifyMessage(m, code) {
  13383. let res = colors$1.yellow(m.text);
  13384. if (m.location) {
  13385. res += `\n` + generateCodeFrame(code, m.location);
  13386. }
  13387. return res + `\n`;
  13388. }
  13389. let tsconfckCache;
  13390. async function loadTsconfigJsonForFile(filename) {
  13391. try {
  13392. if (!tsconfckCache) {
  13393. tsconfckCache = new TSConfckCache();
  13394. }
  13395. const result = await parse$f(filename, {
  13396. cache: tsconfckCache,
  13397. ignoreNodeModules: true,
  13398. });
  13399. // tsconfig could be out of root, make sure it is watched on dev
  13400. if (server && result.tsconfigFile) {
  13401. ensureWatchedFile(server.watcher, result.tsconfigFile, server.config.root);
  13402. }
  13403. return result.tsconfig;
  13404. }
  13405. catch (e) {
  13406. if (e instanceof TSConfckParseError) {
  13407. // tsconfig could be out of root, make sure it is watched on dev
  13408. if (server && e.tsconfigFile) {
  13409. ensureWatchedFile(server.watcher, e.tsconfigFile, server.config.root);
  13410. }
  13411. }
  13412. throw e;
  13413. }
  13414. }
  13415. async function reloadOnTsconfigChange(changedFile) {
  13416. // server could be closed externally after a file change is detected
  13417. if (!server)
  13418. return;
  13419. // any tsconfig.json that's added in the workspace could be closer to a code file than a previously cached one
  13420. // any json file in the tsconfig cache could have been used to compile ts
  13421. if (path$o.basename(changedFile) === 'tsconfig.json' ||
  13422. (changedFile.endsWith('.json') &&
  13423. tsconfckCache?.hasParseResult(changedFile))) {
  13424. server.config.logger.info(`changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`, { clear: server.config.clearScreen, timestamp: true });
  13425. // clear module graph to remove code compiled with outdated config
  13426. server.moduleGraph.invalidateAll();
  13427. // reset tsconfck so that recompile works with up2date configs
  13428. tsconfckCache?.clear();
  13429. // server may not be available if vite config is updated at the same time
  13430. if (server) {
  13431. // force full reload
  13432. server.hot.send({
  13433. type: 'full-reload',
  13434. path: '*',
  13435. });
  13436. }
  13437. }
  13438. }
  13439. // src/realWorker.ts
  13440. var Worker = class {
  13441. /** @internal */
  13442. _code;
  13443. /** @internal */
  13444. _parentFunctions;
  13445. /** @internal */
  13446. _max;
  13447. /** @internal */
  13448. _pool;
  13449. /** @internal */
  13450. _idlePool;
  13451. /** @internal */
  13452. _queue;
  13453. constructor(fn, options = {}) {
  13454. this._code = genWorkerCode(fn, options.parentFunctions ?? {});
  13455. this._parentFunctions = options.parentFunctions ?? {};
  13456. const defaultMax = Math.max(
  13457. 1,
  13458. // os.availableParallelism is available from Node.js 18.14.0
  13459. (os$4.availableParallelism?.() ?? os$4.cpus().length) - 1
  13460. );
  13461. this._max = options.max || defaultMax;
  13462. this._pool = [];
  13463. this._idlePool = [];
  13464. this._queue = [];
  13465. }
  13466. async run(...args) {
  13467. const worker = await this._getAvailableWorker();
  13468. return new Promise((resolve, reject) => {
  13469. worker.currentResolve = resolve;
  13470. worker.currentReject = reject;
  13471. worker.postMessage({ type: "run", args });
  13472. });
  13473. }
  13474. stop() {
  13475. this._pool.forEach((w) => w.unref());
  13476. this._queue.forEach(
  13477. ([, reject]) => reject(
  13478. new Error("Main worker pool stopped before a worker was available.")
  13479. )
  13480. );
  13481. this._pool = [];
  13482. this._idlePool = [];
  13483. this._queue = [];
  13484. }
  13485. /** @internal */
  13486. async _getAvailableWorker() {
  13487. if (this._idlePool.length) {
  13488. return this._idlePool.shift();
  13489. }
  13490. if (this._pool.length < this._max) {
  13491. const worker = new Worker$1(this._code, { eval: true });
  13492. worker.on("message", async (args) => {
  13493. if (args.type === "run") {
  13494. if ("result" in args) {
  13495. worker.currentResolve && worker.currentResolve(args.result);
  13496. worker.currentResolve = null;
  13497. } else {
  13498. if (args.error instanceof ReferenceError) {
  13499. args.error.message += ". Maybe you forgot to pass the function to parentFunction?";
  13500. }
  13501. worker.currentReject && worker.currentReject(args.error);
  13502. worker.currentReject = null;
  13503. }
  13504. this._assignDoneWorker(worker);
  13505. } else if (args.type === "parentFunction") {
  13506. try {
  13507. const result = await this._parentFunctions[args.name](...args.args);
  13508. worker.postMessage({ type: "parentFunction", id: args.id, result });
  13509. } catch (e) {
  13510. worker.postMessage({
  13511. type: "parentFunction",
  13512. id: args.id,
  13513. error: e
  13514. });
  13515. }
  13516. }
  13517. });
  13518. worker.on("error", (err) => {
  13519. worker.currentReject && worker.currentReject(err);
  13520. worker.currentReject = null;
  13521. });
  13522. worker.on("exit", (code) => {
  13523. const i = this._pool.indexOf(worker);
  13524. if (i > -1)
  13525. this._pool.splice(i, 1);
  13526. if (code !== 0 && worker.currentReject) {
  13527. worker.currentReject(
  13528. new Error(`Worker stopped with non-0 exit code ${code}`)
  13529. );
  13530. worker.currentReject = null;
  13531. }
  13532. });
  13533. this._pool.push(worker);
  13534. return worker;
  13535. }
  13536. let resolve;
  13537. let reject;
  13538. const onWorkerAvailablePromise = new Promise((r, rj) => {
  13539. resolve = r;
  13540. reject = rj;
  13541. });
  13542. this._queue.push([resolve, reject]);
  13543. return onWorkerAvailablePromise;
  13544. }
  13545. /** @internal */
  13546. _assignDoneWorker(worker) {
  13547. if (this._queue.length) {
  13548. const [resolve] = this._queue.shift();
  13549. resolve(worker);
  13550. return;
  13551. }
  13552. this._idlePool.push(worker);
  13553. }
  13554. };
  13555. function genWorkerCode(fn, parentFunctions) {
  13556. const createParentFunctionCaller = (parentPort) => {
  13557. let id = 0;
  13558. const resolvers = /* @__PURE__ */ new Map();
  13559. const call = (key) => async (...args) => {
  13560. id++;
  13561. let resolve, reject;
  13562. const promise = new Promise((res, rej) => {
  13563. resolve = res;
  13564. reject = rej;
  13565. });
  13566. resolvers.set(id, { resolve, reject });
  13567. parentPort.postMessage({ type: "parentFunction", id, name: key, args });
  13568. return await promise;
  13569. };
  13570. const receive = (id2, args) => {
  13571. if (resolvers.has(id2)) {
  13572. const { resolve, reject } = resolvers.get(id2);
  13573. resolvers.delete(id2);
  13574. if ("result" in args) {
  13575. resolve(args.result);
  13576. } else {
  13577. reject(args.error);
  13578. }
  13579. }
  13580. };
  13581. return { call, receive };
  13582. };
  13583. return `
  13584. const { parentPort } = require('worker_threads')
  13585. const parentFunctionCaller = (${createParentFunctionCaller.toString()})(parentPort)
  13586. const doWork = (() => {
  13587. ${Object.keys(parentFunctions).map(
  13588. (key) => `const ${key} = parentFunctionCaller.call(${JSON.stringify(key)});`
  13589. ).join("\n")}
  13590. return (${fn.toString()})()
  13591. })()
  13592. parentPort.on('message', async (args) => {
  13593. if (args.type === 'run') {
  13594. try {
  13595. const res = await doWork(...args.args)
  13596. parentPort.postMessage({ type: 'run', result: res })
  13597. } catch (e) {
  13598. parentPort.postMessage({ type: 'run', error: e })
  13599. }
  13600. } else if (args.type === 'parentFunction') {
  13601. parentFunctionCaller.receive(args.id, args)
  13602. }
  13603. })
  13604. `;
  13605. }
  13606. var FakeWorker = class {
  13607. /** @internal */
  13608. _fn;
  13609. constructor(fn, options = {}) {
  13610. const argsAndCode = genFakeWorkerArgsAndCode(
  13611. fn,
  13612. options.parentFunctions ?? {}
  13613. );
  13614. const require2 = createRequire$1(import.meta.url);
  13615. this._fn = new Function(...argsAndCode)(require2, options.parentFunctions);
  13616. }
  13617. async run(...args) {
  13618. try {
  13619. return await this._fn(...args);
  13620. } catch (err) {
  13621. if (err instanceof ReferenceError) {
  13622. err.message += ". Maybe you forgot to pass the function to parentFunction?";
  13623. }
  13624. throw err;
  13625. }
  13626. }
  13627. stop() {
  13628. }
  13629. };
  13630. function genFakeWorkerArgsAndCode(fn, parentFunctions) {
  13631. return [
  13632. "require",
  13633. "parentFunctions",
  13634. `
  13635. ${Object.keys(parentFunctions).map((key) => `const ${key} = parentFunctions[${JSON.stringify(key)}];`).join("\n")}
  13636. return (${fn.toString()})()
  13637. `
  13638. ];
  13639. }
  13640. // src/workerWithFallback.ts
  13641. var WorkerWithFallback = class {
  13642. /** @internal */
  13643. _disableReal;
  13644. /** @internal */
  13645. _realWorker;
  13646. /** @internal */
  13647. _fakeWorker;
  13648. /** @internal */
  13649. _shouldUseFake;
  13650. constructor(fn, options) {
  13651. this._disableReal = options.max !== void 0 && options.max <= 0;
  13652. this._realWorker = new Worker(fn, options);
  13653. this._fakeWorker = new FakeWorker(fn, options);
  13654. this._shouldUseFake = options.shouldUseFake;
  13655. }
  13656. async run(...args) {
  13657. const useFake = this._disableReal || this._shouldUseFake(...args);
  13658. return this[useFake ? "_fakeWorker" : "_realWorker"].run(...args);
  13659. }
  13660. stop() {
  13661. this._realWorker.stop();
  13662. this._fakeWorker.stop();
  13663. }
  13664. };
  13665. let terserPath;
  13666. const loadTerserPath = (root) => {
  13667. if (terserPath)
  13668. return terserPath;
  13669. try {
  13670. terserPath = requireResolveFromRootWithFallback(root, 'terser');
  13671. }
  13672. catch (e) {
  13673. if (e.code === 'MODULE_NOT_FOUND') {
  13674. throw new Error('terser not found. Since Vite v3, terser has become an optional dependency. You need to install it.');
  13675. }
  13676. else {
  13677. const message = new Error(`terser failed to load:\n${e.message}`);
  13678. message.stack = e.stack + '\n' + message.stack;
  13679. throw message;
  13680. }
  13681. }
  13682. return terserPath;
  13683. };
  13684. function terserPlugin(config) {
  13685. const { maxWorkers, ...terserOptions } = config.build.terserOptions;
  13686. const makeWorker = () => new Worker(() => async (terserPath, code, options) => {
  13687. // test fails when using `import`. maybe related: https://github.com/nodejs/node/issues/43205
  13688. // eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
  13689. const terser = require(terserPath);
  13690. return terser.minify(code, options);
  13691. }, {
  13692. max: maxWorkers,
  13693. });
  13694. let worker;
  13695. return {
  13696. name: 'vite:terser',
  13697. async renderChunk(code, _chunk, outputOptions) {
  13698. // This plugin is included for any non-false value of config.build.minify,
  13699. // so that normal chunks can use the preferred minifier, and legacy chunks
  13700. // can use terser.
  13701. if (config.build.minify !== 'terser' &&
  13702. // @ts-expect-error injected by @vitejs/plugin-legacy
  13703. !outputOptions.__vite_force_terser__) {
  13704. return null;
  13705. }
  13706. // Do not minify ES lib output since that would remove pure annotations
  13707. // and break tree-shaking.
  13708. if (config.build.lib && outputOptions.format === 'es') {
  13709. return null;
  13710. }
  13711. // Lazy load worker.
  13712. worker ||= makeWorker();
  13713. const terserPath = loadTerserPath(config.root);
  13714. const res = await worker.run(terserPath, code, {
  13715. safari10: true,
  13716. ...terserOptions,
  13717. sourceMap: !!outputOptions.sourcemap,
  13718. module: outputOptions.format.startsWith('es'),
  13719. toplevel: outputOptions.format === 'cjs',
  13720. });
  13721. return {
  13722. code: res.code,
  13723. map: res.map,
  13724. };
  13725. },
  13726. closeBundle() {
  13727. worker?.stop();
  13728. },
  13729. };
  13730. }
  13731. const mimes = {
  13732. "3g2": "video/3gpp2",
  13733. "3gp": "video/3gpp",
  13734. "3gpp": "video/3gpp",
  13735. "3mf": "model/3mf",
  13736. "aac": "audio/aac",
  13737. "ac": "application/pkix-attr-cert",
  13738. "adp": "audio/adpcm",
  13739. "adts": "audio/aac",
  13740. "ai": "application/postscript",
  13741. "aml": "application/automationml-aml+xml",
  13742. "amlx": "application/automationml-amlx+zip",
  13743. "amr": "audio/amr",
  13744. "apng": "image/apng",
  13745. "appcache": "text/cache-manifest",
  13746. "appinstaller": "application/appinstaller",
  13747. "appx": "application/appx",
  13748. "appxbundle": "application/appxbundle",
  13749. "asc": "application/pgp-keys",
  13750. "atom": "application/atom+xml",
  13751. "atomcat": "application/atomcat+xml",
  13752. "atomdeleted": "application/atomdeleted+xml",
  13753. "atomsvc": "application/atomsvc+xml",
  13754. "au": "audio/basic",
  13755. "avci": "image/avci",
  13756. "avcs": "image/avcs",
  13757. "avif": "image/avif",
  13758. "aw": "application/applixware",
  13759. "bdoc": "application/bdoc",
  13760. "bin": "application/octet-stream",
  13761. "bmp": "image/bmp",
  13762. "bpk": "application/octet-stream",
  13763. "btf": "image/prs.btif",
  13764. "btif": "image/prs.btif",
  13765. "buffer": "application/octet-stream",
  13766. "ccxml": "application/ccxml+xml",
  13767. "cdfx": "application/cdfx+xml",
  13768. "cdmia": "application/cdmi-capability",
  13769. "cdmic": "application/cdmi-container",
  13770. "cdmid": "application/cdmi-domain",
  13771. "cdmio": "application/cdmi-object",
  13772. "cdmiq": "application/cdmi-queue",
  13773. "cer": "application/pkix-cert",
  13774. "cgm": "image/cgm",
  13775. "cjs": "application/node",
  13776. "class": "application/java-vm",
  13777. "coffee": "text/coffeescript",
  13778. "conf": "text/plain",
  13779. "cpl": "application/cpl+xml",
  13780. "cpt": "application/mac-compactpro",
  13781. "crl": "application/pkix-crl",
  13782. "css": "text/css",
  13783. "csv": "text/csv",
  13784. "cu": "application/cu-seeme",
  13785. "cwl": "application/cwl",
  13786. "cww": "application/prs.cww",
  13787. "davmount": "application/davmount+xml",
  13788. "dbk": "application/docbook+xml",
  13789. "deb": "application/octet-stream",
  13790. "def": "text/plain",
  13791. "deploy": "application/octet-stream",
  13792. "dib": "image/bmp",
  13793. "disposition-notification": "message/disposition-notification",
  13794. "dist": "application/octet-stream",
  13795. "distz": "application/octet-stream",
  13796. "dll": "application/octet-stream",
  13797. "dmg": "application/octet-stream",
  13798. "dms": "application/octet-stream",
  13799. "doc": "application/msword",
  13800. "dot": "application/msword",
  13801. "dpx": "image/dpx",
  13802. "drle": "image/dicom-rle",
  13803. "dsc": "text/prs.lines.tag",
  13804. "dssc": "application/dssc+der",
  13805. "dtd": "application/xml-dtd",
  13806. "dump": "application/octet-stream",
  13807. "dwd": "application/atsc-dwd+xml",
  13808. "ear": "application/java-archive",
  13809. "ecma": "application/ecmascript",
  13810. "elc": "application/octet-stream",
  13811. "emf": "image/emf",
  13812. "eml": "message/rfc822",
  13813. "emma": "application/emma+xml",
  13814. "emotionml": "application/emotionml+xml",
  13815. "eps": "application/postscript",
  13816. "epub": "application/epub+zip",
  13817. "exe": "application/octet-stream",
  13818. "exi": "application/exi",
  13819. "exp": "application/express",
  13820. "exr": "image/aces",
  13821. "ez": "application/andrew-inset",
  13822. "fdf": "application/fdf",
  13823. "fdt": "application/fdt+xml",
  13824. "fits": "image/fits",
  13825. "g3": "image/g3fax",
  13826. "gbr": "application/rpki-ghostbusters",
  13827. "geojson": "application/geo+json",
  13828. "gif": "image/gif",
  13829. "glb": "model/gltf-binary",
  13830. "gltf": "model/gltf+json",
  13831. "gml": "application/gml+xml",
  13832. "gpx": "application/gpx+xml",
  13833. "gram": "application/srgs",
  13834. "grxml": "application/srgs+xml",
  13835. "gxf": "application/gxf",
  13836. "gz": "application/gzip",
  13837. "h261": "video/h261",
  13838. "h263": "video/h263",
  13839. "h264": "video/h264",
  13840. "heic": "image/heic",
  13841. "heics": "image/heic-sequence",
  13842. "heif": "image/heif",
  13843. "heifs": "image/heif-sequence",
  13844. "hej2": "image/hej2k",
  13845. "held": "application/atsc-held+xml",
  13846. "hjson": "application/hjson",
  13847. "hlp": "application/winhlp",
  13848. "hqx": "application/mac-binhex40",
  13849. "hsj2": "image/hsj2",
  13850. "htm": "text/html",
  13851. "html": "text/html",
  13852. "ics": "text/calendar",
  13853. "ief": "image/ief",
  13854. "ifb": "text/calendar",
  13855. "iges": "model/iges",
  13856. "igs": "model/iges",
  13857. "img": "application/octet-stream",
  13858. "in": "text/plain",
  13859. "ini": "text/plain",
  13860. "ink": "application/inkml+xml",
  13861. "inkml": "application/inkml+xml",
  13862. "ipfix": "application/ipfix",
  13863. "iso": "application/octet-stream",
  13864. "its": "application/its+xml",
  13865. "jade": "text/jade",
  13866. "jar": "application/java-archive",
  13867. "jhc": "image/jphc",
  13868. "jls": "image/jls",
  13869. "jp2": "image/jp2",
  13870. "jpe": "image/jpeg",
  13871. "jpeg": "image/jpeg",
  13872. "jpf": "image/jpx",
  13873. "jpg": "image/jpeg",
  13874. "jpg2": "image/jp2",
  13875. "jpgm": "image/jpm",
  13876. "jpgv": "video/jpeg",
  13877. "jph": "image/jph",
  13878. "jpm": "image/jpm",
  13879. "jpx": "image/jpx",
  13880. "js": "text/javascript",
  13881. "json": "application/json",
  13882. "json5": "application/json5",
  13883. "jsonld": "application/ld+json",
  13884. "jsonml": "application/jsonml+json",
  13885. "jsx": "text/jsx",
  13886. "jt": "model/jt",
  13887. "jxr": "image/jxr",
  13888. "jxra": "image/jxra",
  13889. "jxrs": "image/jxrs",
  13890. "jxs": "image/jxs",
  13891. "jxsc": "image/jxsc",
  13892. "jxsi": "image/jxsi",
  13893. "jxss": "image/jxss",
  13894. "kar": "audio/midi",
  13895. "ktx": "image/ktx",
  13896. "ktx2": "image/ktx2",
  13897. "less": "text/less",
  13898. "lgr": "application/lgr+xml",
  13899. "list": "text/plain",
  13900. "litcoffee": "text/coffeescript",
  13901. "log": "text/plain",
  13902. "lostxml": "application/lost+xml",
  13903. "lrf": "application/octet-stream",
  13904. "m1v": "video/mpeg",
  13905. "m21": "application/mp21",
  13906. "m2a": "audio/mpeg",
  13907. "m2v": "video/mpeg",
  13908. "m3a": "audio/mpeg",
  13909. "m4a": "audio/mp4",
  13910. "m4p": "application/mp4",
  13911. "m4s": "video/iso.segment",
  13912. "ma": "application/mathematica",
  13913. "mads": "application/mads+xml",
  13914. "maei": "application/mmt-aei+xml",
  13915. "man": "text/troff",
  13916. "manifest": "text/cache-manifest",
  13917. "map": "application/json",
  13918. "mar": "application/octet-stream",
  13919. "markdown": "text/markdown",
  13920. "mathml": "application/mathml+xml",
  13921. "mb": "application/mathematica",
  13922. "mbox": "application/mbox",
  13923. "md": "text/markdown",
  13924. "mdx": "text/mdx",
  13925. "me": "text/troff",
  13926. "mesh": "model/mesh",
  13927. "meta4": "application/metalink4+xml",
  13928. "metalink": "application/metalink+xml",
  13929. "mets": "application/mets+xml",
  13930. "mft": "application/rpki-manifest",
  13931. "mid": "audio/midi",
  13932. "midi": "audio/midi",
  13933. "mime": "message/rfc822",
  13934. "mj2": "video/mj2",
  13935. "mjp2": "video/mj2",
  13936. "mjs": "text/javascript",
  13937. "mml": "text/mathml",
  13938. "mods": "application/mods+xml",
  13939. "mov": "video/quicktime",
  13940. "mp2": "audio/mpeg",
  13941. "mp21": "application/mp21",
  13942. "mp2a": "audio/mpeg",
  13943. "mp3": "audio/mpeg",
  13944. "mp4": "video/mp4",
  13945. "mp4a": "audio/mp4",
  13946. "mp4s": "application/mp4",
  13947. "mp4v": "video/mp4",
  13948. "mpd": "application/dash+xml",
  13949. "mpe": "video/mpeg",
  13950. "mpeg": "video/mpeg",
  13951. "mpf": "application/media-policy-dataset+xml",
  13952. "mpg": "video/mpeg",
  13953. "mpg4": "video/mp4",
  13954. "mpga": "audio/mpeg",
  13955. "mpp": "application/dash-patch+xml",
  13956. "mrc": "application/marc",
  13957. "mrcx": "application/marcxml+xml",
  13958. "ms": "text/troff",
  13959. "mscml": "application/mediaservercontrol+xml",
  13960. "msh": "model/mesh",
  13961. "msi": "application/octet-stream",
  13962. "msix": "application/msix",
  13963. "msixbundle": "application/msixbundle",
  13964. "msm": "application/octet-stream",
  13965. "msp": "application/octet-stream",
  13966. "mtl": "model/mtl",
  13967. "musd": "application/mmt-usd+xml",
  13968. "mxf": "application/mxf",
  13969. "mxmf": "audio/mobile-xmf",
  13970. "mxml": "application/xv+xml",
  13971. "n3": "text/n3",
  13972. "nb": "application/mathematica",
  13973. "nq": "application/n-quads",
  13974. "nt": "application/n-triples",
  13975. "obj": "model/obj",
  13976. "oda": "application/oda",
  13977. "oga": "audio/ogg",
  13978. "ogg": "audio/ogg",
  13979. "ogv": "video/ogg",
  13980. "ogx": "application/ogg",
  13981. "omdoc": "application/omdoc+xml",
  13982. "onepkg": "application/onenote",
  13983. "onetmp": "application/onenote",
  13984. "onetoc": "application/onenote",
  13985. "onetoc2": "application/onenote",
  13986. "opf": "application/oebps-package+xml",
  13987. "opus": "audio/ogg",
  13988. "otf": "font/otf",
  13989. "owl": "application/rdf+xml",
  13990. "oxps": "application/oxps",
  13991. "p10": "application/pkcs10",
  13992. "p7c": "application/pkcs7-mime",
  13993. "p7m": "application/pkcs7-mime",
  13994. "p7s": "application/pkcs7-signature",
  13995. "p8": "application/pkcs8",
  13996. "pdf": "application/pdf",
  13997. "pfr": "application/font-tdpfr",
  13998. "pgp": "application/pgp-encrypted",
  13999. "pkg": "application/octet-stream",
  14000. "pki": "application/pkixcmp",
  14001. "pkipath": "application/pkix-pkipath",
  14002. "pls": "application/pls+xml",
  14003. "png": "image/png",
  14004. "prc": "model/prc",
  14005. "prf": "application/pics-rules",
  14006. "provx": "application/provenance+xml",
  14007. "ps": "application/postscript",
  14008. "pskcxml": "application/pskc+xml",
  14009. "pti": "image/prs.pti",
  14010. "qt": "video/quicktime",
  14011. "raml": "application/raml+yaml",
  14012. "rapd": "application/route-apd+xml",
  14013. "rdf": "application/rdf+xml",
  14014. "relo": "application/p2p-overlay+xml",
  14015. "rif": "application/reginfo+xml",
  14016. "rl": "application/resource-lists+xml",
  14017. "rld": "application/resource-lists-diff+xml",
  14018. "rmi": "audio/midi",
  14019. "rnc": "application/relax-ng-compact-syntax",
  14020. "rng": "application/xml",
  14021. "roa": "application/rpki-roa",
  14022. "roff": "text/troff",
  14023. "rq": "application/sparql-query",
  14024. "rs": "application/rls-services+xml",
  14025. "rsat": "application/atsc-rsat+xml",
  14026. "rsd": "application/rsd+xml",
  14027. "rsheet": "application/urc-ressheet+xml",
  14028. "rss": "application/rss+xml",
  14029. "rtf": "text/rtf",
  14030. "rtx": "text/richtext",
  14031. "rusd": "application/route-usd+xml",
  14032. "s3m": "audio/s3m",
  14033. "sbml": "application/sbml+xml",
  14034. "scq": "application/scvp-cv-request",
  14035. "scs": "application/scvp-cv-response",
  14036. "sdp": "application/sdp",
  14037. "senmlx": "application/senml+xml",
  14038. "sensmlx": "application/sensml+xml",
  14039. "ser": "application/java-serialized-object",
  14040. "setpay": "application/set-payment-initiation",
  14041. "setreg": "application/set-registration-initiation",
  14042. "sgi": "image/sgi",
  14043. "sgm": "text/sgml",
  14044. "sgml": "text/sgml",
  14045. "shex": "text/shex",
  14046. "shf": "application/shf+xml",
  14047. "shtml": "text/html",
  14048. "sieve": "application/sieve",
  14049. "sig": "application/pgp-signature",
  14050. "sil": "audio/silk",
  14051. "silo": "model/mesh",
  14052. "siv": "application/sieve",
  14053. "slim": "text/slim",
  14054. "slm": "text/slim",
  14055. "sls": "application/route-s-tsid+xml",
  14056. "smi": "application/smil+xml",
  14057. "smil": "application/smil+xml",
  14058. "snd": "audio/basic",
  14059. "so": "application/octet-stream",
  14060. "spdx": "text/spdx",
  14061. "spp": "application/scvp-vp-response",
  14062. "spq": "application/scvp-vp-request",
  14063. "spx": "audio/ogg",
  14064. "sql": "application/sql",
  14065. "sru": "application/sru+xml",
  14066. "srx": "application/sparql-results+xml",
  14067. "ssdl": "application/ssdl+xml",
  14068. "ssml": "application/ssml+xml",
  14069. "stk": "application/hyperstudio",
  14070. "stl": "model/stl",
  14071. "stpx": "model/step+xml",
  14072. "stpxz": "model/step-xml+zip",
  14073. "stpz": "model/step+zip",
  14074. "styl": "text/stylus",
  14075. "stylus": "text/stylus",
  14076. "svg": "image/svg+xml",
  14077. "svgz": "image/svg+xml",
  14078. "swidtag": "application/swid+xml",
  14079. "t": "text/troff",
  14080. "t38": "image/t38",
  14081. "td": "application/urc-targetdesc+xml",
  14082. "tei": "application/tei+xml",
  14083. "teicorpus": "application/tei+xml",
  14084. "text": "text/plain",
  14085. "tfi": "application/thraud+xml",
  14086. "tfx": "image/tiff-fx",
  14087. "tif": "image/tiff",
  14088. "tiff": "image/tiff",
  14089. "toml": "application/toml",
  14090. "tr": "text/troff",
  14091. "trig": "application/trig",
  14092. "ts": "video/mp2t",
  14093. "tsd": "application/timestamped-data",
  14094. "tsv": "text/tab-separated-values",
  14095. "ttc": "font/collection",
  14096. "ttf": "font/ttf",
  14097. "ttl": "text/turtle",
  14098. "ttml": "application/ttml+xml",
  14099. "txt": "text/plain",
  14100. "u3d": "model/u3d",
  14101. "u8dsn": "message/global-delivery-status",
  14102. "u8hdr": "message/global-headers",
  14103. "u8mdn": "message/global-disposition-notification",
  14104. "u8msg": "message/global",
  14105. "ubj": "application/ubjson",
  14106. "uri": "text/uri-list",
  14107. "uris": "text/uri-list",
  14108. "urls": "text/uri-list",
  14109. "vcard": "text/vcard",
  14110. "vrml": "model/vrml",
  14111. "vtt": "text/vtt",
  14112. "vxml": "application/voicexml+xml",
  14113. "war": "application/java-archive",
  14114. "wasm": "application/wasm",
  14115. "wav": "audio/wav",
  14116. "weba": "audio/webm",
  14117. "webm": "video/webm",
  14118. "webmanifest": "application/manifest+json",
  14119. "webp": "image/webp",
  14120. "wgsl": "text/wgsl",
  14121. "wgt": "application/widget",
  14122. "wif": "application/watcherinfo+xml",
  14123. "wmf": "image/wmf",
  14124. "woff": "font/woff",
  14125. "woff2": "font/woff2",
  14126. "wrl": "model/vrml",
  14127. "wsdl": "application/wsdl+xml",
  14128. "wspolicy": "application/wspolicy+xml",
  14129. "x3d": "model/x3d+xml",
  14130. "x3db": "model/x3d+fastinfoset",
  14131. "x3dbz": "model/x3d+binary",
  14132. "x3dv": "model/x3d-vrml",
  14133. "x3dvz": "model/x3d+vrml",
  14134. "x3dz": "model/x3d+xml",
  14135. "xaml": "application/xaml+xml",
  14136. "xav": "application/xcap-att+xml",
  14137. "xca": "application/xcap-caps+xml",
  14138. "xcs": "application/calendar+xml",
  14139. "xdf": "application/xcap-diff+xml",
  14140. "xdssc": "application/dssc+xml",
  14141. "xel": "application/xcap-el+xml",
  14142. "xenc": "application/xenc+xml",
  14143. "xer": "application/patch-ops-error+xml",
  14144. "xfdf": "application/xfdf",
  14145. "xht": "application/xhtml+xml",
  14146. "xhtml": "application/xhtml+xml",
  14147. "xhvml": "application/xv+xml",
  14148. "xlf": "application/xliff+xml",
  14149. "xm": "audio/xm",
  14150. "xml": "text/xml",
  14151. "xns": "application/xcap-ns+xml",
  14152. "xop": "application/xop+xml",
  14153. "xpl": "application/xproc+xml",
  14154. "xsd": "application/xml",
  14155. "xsf": "application/prs.xsf+xml",
  14156. "xsl": "application/xml",
  14157. "xslt": "application/xml",
  14158. "xspf": "application/xspf+xml",
  14159. "xvm": "application/xv+xml",
  14160. "xvml": "application/xv+xml",
  14161. "yaml": "text/yaml",
  14162. "yang": "application/yang",
  14163. "yin": "application/yin+xml",
  14164. "yml": "text/yaml",
  14165. "zip": "application/zip"
  14166. };
  14167. function lookup(extn) {
  14168. let tmp = ('' + extn).trim().toLowerCase();
  14169. let idx = tmp.lastIndexOf('.');
  14170. return mimes[!~idx ? tmp : tmp.substring(++idx)];
  14171. }
  14172. const publicFilesMap = new WeakMap();
  14173. async function initPublicFiles(config) {
  14174. let fileNames;
  14175. try {
  14176. fileNames = await recursiveReaddir(config.publicDir);
  14177. }
  14178. catch (e) {
  14179. if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
  14180. return;
  14181. }
  14182. throw e;
  14183. }
  14184. const publicFiles = new Set(fileNames.map((fileName) => fileName.slice(config.publicDir.length)));
  14185. publicFilesMap.set(config, publicFiles);
  14186. return publicFiles;
  14187. }
  14188. function getPublicFiles(config) {
  14189. return publicFilesMap.get(config);
  14190. }
  14191. function checkPublicFile(url, config) {
  14192. // note if the file is in /public, the resolver would have returned it
  14193. // as-is so it's not going to be a fully resolved path.
  14194. const { publicDir } = config;
  14195. if (!publicDir || url[0] !== '/') {
  14196. return;
  14197. }
  14198. const fileName = cleanUrl(url);
  14199. // short-circuit if we have an in-memory publicFiles cache
  14200. const publicFiles = getPublicFiles(config);
  14201. if (publicFiles) {
  14202. return publicFiles.has(fileName)
  14203. ? normalizePath$3(path$o.join(publicDir, fileName))
  14204. : undefined;
  14205. }
  14206. const publicFile = normalizePath$3(path$o.join(publicDir, fileName));
  14207. if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
  14208. // can happen if URL starts with '../'
  14209. return;
  14210. }
  14211. return fs$l.existsSync(publicFile) ? publicFile : undefined;
  14212. }
  14213. // referenceId is base64url but replaces - with $
  14214. const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g;
  14215. const jsSourceMapRE = /\.[cm]?js\.map$/;
  14216. const assetCache = new WeakMap();
  14217. const generatedAssets = new WeakMap();
  14218. // add own dictionary entry by directly assigning mrmime
  14219. function registerCustomMime() {
  14220. // https://github.com/lukeed/mrmime/issues/3
  14221. mimes['ico'] = 'image/x-icon';
  14222. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Containers#flac
  14223. mimes['flac'] = 'audio/flac';
  14224. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  14225. mimes['eot'] = 'application/vnd.ms-fontobject';
  14226. }
  14227. function renderAssetUrlInJS(ctx, config, chunk, opts, code) {
  14228. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(opts.format, config.isWorker);
  14229. let match;
  14230. let s;
  14231. // Urls added with JS using e.g.
  14232. // imgElement.src = "__VITE_ASSET__5aA0Ddc0__" are using quotes
  14233. // Urls added in CSS that is imported in JS end up like
  14234. // var inlined = ".inlined{color:green;background:url(__VITE_ASSET__5aA0Ddc0__)}\n";
  14235. // In both cases, the wrapping should already be fine
  14236. assetUrlRE.lastIndex = 0;
  14237. while ((match = assetUrlRE.exec(code))) {
  14238. s ||= new MagicString(code);
  14239. const [full, referenceId, postfix = ''] = match;
  14240. const file = ctx.getFileName(referenceId);
  14241. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  14242. const filename = file + postfix;
  14243. const replacement = toOutputFilePathInJS(filename, 'asset', chunk.fileName, 'js', config, toRelativeRuntime);
  14244. const replacementString = typeof replacement === 'string'
  14245. ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
  14246. : `"+${replacement.runtime}+"`;
  14247. s.update(match.index, match.index + full.length, replacementString);
  14248. }
  14249. // Replace __VITE_PUBLIC_ASSET__5aA0Ddc0__ with absolute paths
  14250. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  14251. publicAssetUrlRE.lastIndex = 0;
  14252. while ((match = publicAssetUrlRE.exec(code))) {
  14253. s ||= new MagicString(code);
  14254. const [full, hash] = match;
  14255. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  14256. const replacement = toOutputFilePathInJS(publicUrl, 'public', chunk.fileName, 'js', config, toRelativeRuntime);
  14257. const replacementString = typeof replacement === 'string'
  14258. ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
  14259. : `"+${replacement.runtime}+"`;
  14260. s.update(match.index, match.index + full.length, replacementString);
  14261. }
  14262. return s;
  14263. }
  14264. // During build, if we don't use a virtual file for public assets, rollup will
  14265. // watch for these ids resulting in watching the root of the file system in Windows,
  14266. const viteBuildPublicIdPrefix = '\0vite:asset:public';
  14267. /**
  14268. * Also supports loading plain strings with import text from './foo.txt?raw'
  14269. */
  14270. function assetPlugin(config) {
  14271. registerCustomMime();
  14272. let moduleGraph;
  14273. return {
  14274. name: 'vite:asset',
  14275. buildStart() {
  14276. assetCache.set(config, new Map());
  14277. generatedAssets.set(config, new Map());
  14278. },
  14279. configureServer(server) {
  14280. moduleGraph = server.moduleGraph;
  14281. },
  14282. resolveId(id) {
  14283. if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
  14284. return;
  14285. }
  14286. // imports to absolute urls pointing to files in /public
  14287. // will fail to resolve in the main resolver. handle them here.
  14288. const publicFile = checkPublicFile(id, config);
  14289. if (publicFile) {
  14290. return config.command === 'build'
  14291. ? `${viteBuildPublicIdPrefix}${id}`
  14292. : id;
  14293. }
  14294. },
  14295. async load(id) {
  14296. if (id.startsWith(viteBuildPublicIdPrefix)) {
  14297. id = id.slice(viteBuildPublicIdPrefix.length);
  14298. }
  14299. if (id[0] === '\0') {
  14300. // Rollup convention, this id should be handled by the
  14301. // plugin that marked it with \0
  14302. return;
  14303. }
  14304. // raw requests, read from disk
  14305. if (rawRE.test(id)) {
  14306. const file = checkPublicFile(id, config) || cleanUrl(id);
  14307. this.addWatchFile(file);
  14308. // raw query, read file and return as string
  14309. return `export default ${JSON.stringify(await fsp.readFile(file, 'utf-8'))}`;
  14310. }
  14311. if (!urlRE.test(id) && !config.assetsInclude(cleanUrl(id))) {
  14312. return;
  14313. }
  14314. id = removeUrlQuery(id);
  14315. let url = await fileToUrl$1(id, config, this);
  14316. // Inherit HMR timestamp if this asset was invalidated
  14317. if (moduleGraph) {
  14318. const mod = moduleGraph.getModuleById(id);
  14319. if (mod && mod.lastHMRTimestamp > 0) {
  14320. url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
  14321. }
  14322. }
  14323. return {
  14324. code: `export default ${JSON.stringify(encodeURIPath(url))}`,
  14325. // Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
  14326. // If the resulting chunk is empty, it will be removed in generateBundle.
  14327. moduleSideEffects: config.command === 'build' && this.getModuleInfo(id)?.isEntry
  14328. ? 'no-treeshake'
  14329. : false,
  14330. };
  14331. },
  14332. renderChunk(code, chunk, opts) {
  14333. const s = renderAssetUrlInJS(this, config, chunk, opts, code);
  14334. if (s) {
  14335. return {
  14336. code: s.toString(),
  14337. map: config.build.sourcemap
  14338. ? s.generateMap({ hires: 'boundary' })
  14339. : null,
  14340. };
  14341. }
  14342. else {
  14343. return null;
  14344. }
  14345. },
  14346. generateBundle(_, bundle) {
  14347. // Remove empty entry point file
  14348. for (const file in bundle) {
  14349. const chunk = bundle[file];
  14350. if (chunk.type === 'chunk' &&
  14351. chunk.isEntry &&
  14352. chunk.moduleIds.length === 1 &&
  14353. config.assetsInclude(chunk.moduleIds[0])) {
  14354. delete bundle[file];
  14355. }
  14356. }
  14357. // do not emit assets for SSR build
  14358. if (config.command === 'build' &&
  14359. config.build.ssr &&
  14360. !config.build.ssrEmitAssets) {
  14361. for (const file in bundle) {
  14362. if (bundle[file].type === 'asset' &&
  14363. !file.endsWith('ssr-manifest.json') &&
  14364. !jsSourceMapRE.test(file)) {
  14365. delete bundle[file];
  14366. }
  14367. }
  14368. }
  14369. },
  14370. };
  14371. }
  14372. async function fileToUrl$1(id, config, ctx) {
  14373. if (config.command === 'serve') {
  14374. return fileToDevUrl(id, config);
  14375. }
  14376. else {
  14377. return fileToBuiltUrl(id, config, ctx);
  14378. }
  14379. }
  14380. function fileToDevUrl(id, config) {
  14381. let rtn;
  14382. if (checkPublicFile(id, config)) {
  14383. // in public dir during dev, keep the url as-is
  14384. rtn = id;
  14385. }
  14386. else if (id.startsWith(withTrailingSlash(config.root))) {
  14387. // in project root, infer short public path
  14388. rtn = '/' + path$o.posix.relative(config.root, id);
  14389. }
  14390. else {
  14391. // outside of project root, use absolute fs path
  14392. // (this is special handled by the serve static middleware
  14393. rtn = path$o.posix.join(FS_PREFIX, id);
  14394. }
  14395. const base = joinUrlSegments(config.server?.origin ?? '', config.base);
  14396. return joinUrlSegments(base, removeLeadingSlash(rtn));
  14397. }
  14398. function getPublicAssetFilename(hash, config) {
  14399. return publicAssetUrlCache.get(config)?.get(hash);
  14400. }
  14401. const publicAssetUrlCache = new WeakMap();
  14402. const publicAssetUrlRE = /__VITE_PUBLIC_ASSET__([a-z\d]{8})__/g;
  14403. function publicFileToBuiltUrl(url, config) {
  14404. if (config.command !== 'build') {
  14405. // We don't need relative base or renderBuiltUrl support during dev
  14406. return joinUrlSegments(config.base, url);
  14407. }
  14408. const hash = getHash(url);
  14409. let cache = publicAssetUrlCache.get(config);
  14410. if (!cache) {
  14411. cache = new Map();
  14412. publicAssetUrlCache.set(config, cache);
  14413. }
  14414. if (!cache.get(hash)) {
  14415. cache.set(hash, url);
  14416. }
  14417. return `__VITE_PUBLIC_ASSET__${hash}__`;
  14418. }
  14419. const GIT_LFS_PREFIX = Buffer$1.from('version https://git-lfs.github.com');
  14420. function isGitLfsPlaceholder(content) {
  14421. if (content.length < GIT_LFS_PREFIX.length)
  14422. return false;
  14423. // Check whether the content begins with the characteristic string of Git LFS placeholders
  14424. return GIT_LFS_PREFIX.compare(content, 0, GIT_LFS_PREFIX.length) === 0;
  14425. }
  14426. /**
  14427. * Register an asset to be emitted as part of the bundle (if necessary)
  14428. * and returns the resolved public URL
  14429. */
  14430. async function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false, forceInline) {
  14431. if (!skipPublicCheck && checkPublicFile(id, config)) {
  14432. return publicFileToBuiltUrl(id, config);
  14433. }
  14434. const cache = assetCache.get(config);
  14435. const cached = cache.get(id);
  14436. if (cached) {
  14437. return cached;
  14438. }
  14439. const file = cleanUrl(id);
  14440. const content = await fsp.readFile(file);
  14441. let url;
  14442. if (shouldInline(config, file, id, content, pluginContext, forceInline)) {
  14443. if (config.build.lib && isGitLfsPlaceholder(content)) {
  14444. config.logger.warn(colors$1.yellow(`Inlined file ${id} was not downloaded via Git LFS`));
  14445. }
  14446. if (file.endsWith('.svg')) {
  14447. url = svgToDataURL(content);
  14448. }
  14449. else {
  14450. const mimeType = lookup(file) ?? 'application/octet-stream';
  14451. // base64 inlined as a string
  14452. url = `data:${mimeType};base64,${content.toString('base64')}`;
  14453. }
  14454. }
  14455. else {
  14456. // emit as asset
  14457. const { search, hash } = parse$i(id);
  14458. const postfix = (search || '') + (hash || '');
  14459. const referenceId = pluginContext.emitFile({
  14460. // Ignore directory structure for asset file names
  14461. name: path$o.basename(file),
  14462. type: 'asset',
  14463. source: content,
  14464. });
  14465. const originalName = normalizePath$3(path$o.relative(config.root, file));
  14466. generatedAssets.get(config).set(referenceId, { originalName });
  14467. url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`; // TODO_BASE
  14468. }
  14469. cache.set(id, url);
  14470. return url;
  14471. }
  14472. async function urlToBuiltUrl(url, importer, config, pluginContext, forceInline) {
  14473. if (checkPublicFile(url, config)) {
  14474. return publicFileToBuiltUrl(url, config);
  14475. }
  14476. const file = url[0] === '/'
  14477. ? path$o.join(config.root, url)
  14478. : path$o.join(path$o.dirname(importer), url);
  14479. return fileToBuiltUrl(file, config, pluginContext,
  14480. // skip public check since we just did it above
  14481. true, forceInline);
  14482. }
  14483. const shouldInline = (config, file, id, content, pluginContext, forceInline) => {
  14484. if (config.build.lib)
  14485. return true;
  14486. if (pluginContext.getModuleInfo(id)?.isEntry)
  14487. return false;
  14488. if (forceInline !== undefined)
  14489. return forceInline;
  14490. let limit;
  14491. if (typeof config.build.assetsInlineLimit === 'function') {
  14492. const userShouldInline = config.build.assetsInlineLimit(file, content);
  14493. if (userShouldInline != null)
  14494. return userShouldInline;
  14495. limit = DEFAULT_ASSETS_INLINE_LIMIT;
  14496. }
  14497. else {
  14498. limit = Number(config.build.assetsInlineLimit);
  14499. }
  14500. if (file.endsWith('.html'))
  14501. return false;
  14502. // Don't inline SVG with fragments, as they are meant to be reused
  14503. if (file.endsWith('.svg') && id.includes('#'))
  14504. return false;
  14505. return content.length < limit && !isGitLfsPlaceholder(content);
  14506. };
  14507. const nestedQuotesRE = /"[^"']*'[^"]*"|'[^'"]*"[^']*'/;
  14508. // Inspired by https://github.com/iconify/iconify/blob/main/packages/utils/src/svg/url.ts
  14509. function svgToDataURL(content) {
  14510. const stringContent = content.toString();
  14511. // If the SVG contains some text or HTML, any transformation is unsafe, and given that double quotes would then
  14512. // need to be escaped, the gain to use a data URI would be ridiculous if not negative
  14513. if (stringContent.includes('<text') ||
  14514. stringContent.includes('<foreignObject') ||
  14515. nestedQuotesRE.test(stringContent)) {
  14516. return `data:image/svg+xml;base64,${content.toString('base64')}`;
  14517. }
  14518. else {
  14519. return ('data:image/svg+xml,' +
  14520. stringContent
  14521. .trim()
  14522. .replaceAll(/>\s+</g, '><')
  14523. .replaceAll('"', "'")
  14524. .replaceAll('%', '%25')
  14525. .replaceAll('#', '%23')
  14526. .replaceAll('<', '%3c')
  14527. .replaceAll('>', '%3e')
  14528. // Spaces are not valid in srcset it has some use cases
  14529. // it can make the uncompressed URI slightly higher than base64, but will compress way better
  14530. // https://github.com/vitejs/vite/pull/14643#issuecomment-1766288673
  14531. .replaceAll(/\s+/g, '%20'));
  14532. }
  14533. }
  14534. const endsWithJSRE = /\.[cm]?js$/;
  14535. function manifestPlugin(config) {
  14536. const manifest = {};
  14537. let outputCount;
  14538. return {
  14539. name: 'vite:manifest',
  14540. buildStart() {
  14541. outputCount = 0;
  14542. },
  14543. generateBundle({ format }, bundle) {
  14544. function getChunkName(chunk) {
  14545. return getChunkOriginalFileName(chunk, config.root, format);
  14546. }
  14547. function getInternalImports(imports) {
  14548. const filteredImports = [];
  14549. for (const file of imports) {
  14550. if (bundle[file] === undefined) {
  14551. continue;
  14552. }
  14553. filteredImports.push(getChunkName(bundle[file]));
  14554. }
  14555. return filteredImports;
  14556. }
  14557. function createChunk(chunk) {
  14558. const manifestChunk = {
  14559. file: chunk.fileName,
  14560. name: chunk.name,
  14561. };
  14562. if (chunk.facadeModuleId) {
  14563. manifestChunk.src = getChunkName(chunk);
  14564. }
  14565. if (chunk.isEntry) {
  14566. manifestChunk.isEntry = true;
  14567. }
  14568. if (chunk.isDynamicEntry) {
  14569. manifestChunk.isDynamicEntry = true;
  14570. }
  14571. if (chunk.imports.length) {
  14572. const internalImports = getInternalImports(chunk.imports);
  14573. if (internalImports.length > 0) {
  14574. manifestChunk.imports = internalImports;
  14575. }
  14576. }
  14577. if (chunk.dynamicImports.length) {
  14578. const internalImports = getInternalImports(chunk.dynamicImports);
  14579. if (internalImports.length > 0) {
  14580. manifestChunk.dynamicImports = internalImports;
  14581. }
  14582. }
  14583. if (chunk.viteMetadata?.importedCss.size) {
  14584. manifestChunk.css = [...chunk.viteMetadata.importedCss];
  14585. }
  14586. if (chunk.viteMetadata?.importedAssets.size) {
  14587. manifestChunk.assets = [...chunk.viteMetadata.importedAssets];
  14588. }
  14589. return manifestChunk;
  14590. }
  14591. function createAsset(asset, src, isEntry) {
  14592. const manifestChunk = {
  14593. file: asset.fileName,
  14594. src,
  14595. };
  14596. if (isEntry)
  14597. manifestChunk.isEntry = true;
  14598. return manifestChunk;
  14599. }
  14600. const fileNameToAssetMeta = new Map();
  14601. const assets = generatedAssets.get(config);
  14602. assets.forEach((asset, referenceId) => {
  14603. try {
  14604. const fileName = this.getFileName(referenceId);
  14605. fileNameToAssetMeta.set(fileName, asset);
  14606. }
  14607. catch (error) {
  14608. // The asset was generated as part of a different output option.
  14609. // It was already handled during the previous run of this plugin.
  14610. assets.delete(referenceId);
  14611. }
  14612. });
  14613. const fileNameToAsset = new Map();
  14614. for (const file in bundle) {
  14615. const chunk = bundle[file];
  14616. if (chunk.type === 'chunk') {
  14617. manifest[getChunkName(chunk)] = createChunk(chunk);
  14618. }
  14619. else if (chunk.type === 'asset' && typeof chunk.name === 'string') {
  14620. // Add every unique asset to the manifest, keyed by its original name
  14621. const assetMeta = fileNameToAssetMeta.get(chunk.fileName);
  14622. const src = assetMeta?.originalName ?? chunk.name;
  14623. const asset = createAsset(chunk, src, assetMeta?.isEntry);
  14624. // If JS chunk and asset chunk are both generated from the same source file,
  14625. // prioritize JS chunk as it contains more information
  14626. const file = manifest[src]?.file;
  14627. if (file && endsWithJSRE.test(file))
  14628. continue;
  14629. manifest[src] = asset;
  14630. fileNameToAsset.set(chunk.fileName, asset);
  14631. }
  14632. }
  14633. // Add deduplicated assets to the manifest
  14634. assets.forEach(({ originalName }, referenceId) => {
  14635. if (!manifest[originalName]) {
  14636. const fileName = this.getFileName(referenceId);
  14637. const asset = fileNameToAsset.get(fileName);
  14638. if (asset) {
  14639. manifest[originalName] = asset;
  14640. }
  14641. }
  14642. });
  14643. outputCount++;
  14644. const output = config.build.rollupOptions?.output;
  14645. const outputLength = Array.isArray(output) ? output.length : 1;
  14646. if (outputCount >= outputLength) {
  14647. this.emitFile({
  14648. fileName: typeof config.build.manifest === 'string'
  14649. ? config.build.manifest
  14650. : '.vite/manifest.json',
  14651. type: 'asset',
  14652. source: JSON.stringify(sortObjectKeys(manifest), undefined, 2),
  14653. });
  14654. }
  14655. },
  14656. };
  14657. }
  14658. function getChunkOriginalFileName(chunk, root, format) {
  14659. if (chunk.facadeModuleId) {
  14660. let name = normalizePath$3(path$o.relative(root, chunk.facadeModuleId));
  14661. if (format === 'system' && !chunk.name.includes('-legacy')) {
  14662. const ext = path$o.extname(name);
  14663. const endPos = ext.length !== 0 ? -ext.length : undefined;
  14664. name = name.slice(0, endPos) + `-legacy` + ext;
  14665. }
  14666. return name.replace(/\0/g, '');
  14667. }
  14668. else {
  14669. return `_` + path$o.basename(chunk.fileName);
  14670. }
  14671. }
  14672. // This is based on @rollup/plugin-data-uri
  14673. // MIT Licensed https://github.com/rollup/plugins/blob/master/LICENSE
  14674. // ref https://github.com/vitejs/vite/issues/1428#issuecomment-757033808
  14675. const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/;
  14676. const base64RE = /base64/i;
  14677. const dataUriPrefix = `\0/@data-uri/`;
  14678. /**
  14679. * Build only, since importing from a data URI works natively.
  14680. */
  14681. function dataURIPlugin() {
  14682. let resolved;
  14683. return {
  14684. name: 'vite:data-uri',
  14685. buildStart() {
  14686. resolved = new Map();
  14687. },
  14688. resolveId(id) {
  14689. if (!dataUriRE.test(id)) {
  14690. return;
  14691. }
  14692. const uri = new URL$3(id);
  14693. if (uri.protocol !== 'data:') {
  14694. return;
  14695. }
  14696. const match = uri.pathname.match(dataUriRE);
  14697. if (!match) {
  14698. return;
  14699. }
  14700. const [, mime, format, data] = match;
  14701. if (mime !== 'text/javascript') {
  14702. throw new Error(`data URI with non-JavaScript mime type is not supported. If you're using legacy JavaScript MIME types (such as 'application/javascript'), please use 'text/javascript' instead.`);
  14703. }
  14704. // decode data
  14705. const base64 = format && base64RE.test(format.substring(1));
  14706. const content = base64
  14707. ? Buffer.from(data, 'base64').toString('utf-8')
  14708. : data;
  14709. resolved.set(id, content);
  14710. return dataUriPrefix + id;
  14711. },
  14712. load(id) {
  14713. if (id.startsWith(dataUriPrefix)) {
  14714. return resolved.get(id.slice(dataUriPrefix.length));
  14715. }
  14716. },
  14717. };
  14718. }
  14719. /* es-module-lexer 1.5.0 */
  14720. var ImportType;!function(A){A[A.Static=1]="Static",A[A.Dynamic=2]="Dynamic",A[A.ImportMeta=3]="ImportMeta",A[A.StaticSourcePhase=4]="StaticSourcePhase",A[A.DynamicSourcePhase=5]="DynamicSourcePhase";}(ImportType||(ImportType={}));const A=1===new Uint8Array(new Uint16Array([1]).buffer)[0];function parse$e(E,g="@"){if(!C)return init.then((()=>parse$e(E)));const I=E.length+1,w=(C.__heap_base.value||C.__heap_base)+4*I-C.memory.buffer.byteLength;w>0&&C.memory.grow(Math.ceil(w/65536));const D=C.sa(I-1);if((A?B:Q)(E,new Uint16Array(C.memory.buffer,D,I)),!C.parse())throw Object.assign(new Error(`Parse error ${g}:${E.slice(0,C.e()).split("\n").length}:${C.e()-E.lastIndexOf("\n",C.e()-1)}`),{idx:C.e()});const o=[],K=[];for(;C.ri();){const A=C.is(),Q=C.ie(),B=C.it(),g=C.ai(),I=C.id(),w=C.ss(),D=C.se();let K;C.ip()&&(K=k(E.slice(-1===I?A-1:A,-1===I?Q+1:Q))),o.push({n:K,t:B,s:A,e:Q,ss:w,se:D,d:I,a:g});}for(;C.re();){const A=C.es(),Q=C.ee(),B=C.els(),g=C.ele(),I=E.slice(A,Q),w=I[0],D=B<0?void 0:E.slice(B,g),o=D?D[0]:"";K.push({s:A,e:Q,ls:B,le:g,n:'"'===w||"'"===w?k(I):I,ln:'"'===o||"'"===o?k(D):D});}function k(A){try{return (0, eval)(A)}catch(A){}}return [o,K,!!C.f(),!!C.ms()]}function Q(A,Q){const B=A.length;let C=0;for(;C<B;){const B=A.charCodeAt(C);Q[C++]=(255&B)<<8|B>>>8;}}function B(A,Q){const B=A.length;let C=0;for(;C<B;)Q[C]=A.charCodeAt(C++);}let C;const init=WebAssembly.compile((E="AGFzbQEAAAABKwhgAX8Bf2AEf39/fwBgAAF/YAAAYAF/AGADf39/AX9gAn9/AX9gA39/fwADMTAAAQECAgICAgICAgICAgICAgICAgIAAwMDBAQAAAAAAAAAAwMDAAUGAAAABwAGAgUEBQFwAQEBBQMBAAEGDwJ/AUHA8gALfwBBwPIACwd6FQZtZW1vcnkCAAJzYQAAAWUAAwJpcwAEAmllAAUCc3MABgJzZQAHAml0AAgCYWkACQJpZAAKAmlwAAsCZXMADAJlZQANA2VscwAOA2VsZQAPAnJpABACcmUAEQFmABICbXMAEwVwYXJzZQAUC19faGVhcF9iYXNlAwEKxUAwaAEBf0EAIAA2AoAKQQAoAtwJIgEgAEEBdGoiAEEAOwEAQQAgAEECaiIANgKECkEAIAA2AogKQQBBADYC4AlBAEEANgLwCUEAQQA2AugJQQBBADYC5AlBAEEANgL4CUEAQQA2AuwJIAEL0wEBA39BACgC8AkhBEEAQQAoAogKIgU2AvAJQQAgBDYC9AlBACAFQSRqNgKICiAEQSBqQeAJIAQbIAU2AgBBACgC1AkhBEEAKALQCSEGIAUgATYCACAFIAA2AgggBSACIAJBAmpBACAGIANGIgAbIAQgA0YiBBs2AgwgBSADNgIUIAVBADYCECAFIAI2AgQgBUEANgIgIAVBA0ECQQEgABsgBBs2AhwgBUEAKALQCSADRiICOgAYAkACQCACDQBBACgC1AkgA0cNAQtBAEEBOgCMCgsLXgEBf0EAKAL4CSIEQRBqQeQJIAQbQQAoAogKIgQ2AgBBACAENgL4CUEAIARBFGo2AogKQQBBAToAjAogBEEANgIQIAQgAzYCDCAEIAI2AgggBCABNgIEIAQgADYCAAsIAEEAKAKQCgsVAEEAKALoCSgCAEEAKALcCWtBAXULHgEBf0EAKALoCSgCBCIAQQAoAtwJa0EBdUF/IAAbCxUAQQAoAugJKAIIQQAoAtwJa0EBdQseAQF/QQAoAugJKAIMIgBBACgC3AlrQQF1QX8gABsLCwBBACgC6AkoAhwLHgEBf0EAKALoCSgCECIAQQAoAtwJa0EBdUF/IAAbCzsBAX8CQEEAKALoCSgCFCIAQQAoAtAJRw0AQX8PCwJAIABBACgC1AlHDQBBfg8LIABBACgC3AlrQQF1CwsAQQAoAugJLQAYCxUAQQAoAuwJKAIAQQAoAtwJa0EBdQsVAEEAKALsCSgCBEEAKALcCWtBAXULHgEBf0EAKALsCSgCCCIAQQAoAtwJa0EBdUF/IAAbCx4BAX9BACgC7AkoAgwiAEEAKALcCWtBAXVBfyAAGwslAQF/QQBBACgC6AkiAEEgakHgCSAAGygCACIANgLoCSAAQQBHCyUBAX9BAEEAKALsCSIAQRBqQeQJIAAbKAIAIgA2AuwJIABBAEcLCABBAC0AlAoLCABBAC0AjAoLhw0BBX8jAEGA0ABrIgAkAEEAQQE6AJQKQQBBACgC2Ak2ApwKQQBBACgC3AlBfmoiATYCsApBACABQQAoAoAKQQF0aiICNgK0CkEAQQA6AIwKQQBBADsBlgpBAEEAOwGYCkEAQQA6AKAKQQBBADYCkApBAEEAOgD8CUEAIABBgBBqNgKkCkEAIAA2AqgKQQBBADoArAoCQAJAAkACQANAQQAgAUECaiIDNgKwCiABIAJPDQECQCADLwEAIgJBd2pBBUkNAAJAAkACQAJAAkAgAkGbf2oOBQEICAgCAAsgAkEgRg0EIAJBL0YNAyACQTtGDQIMBwtBAC8BmAoNASADEBVFDQEgAUEEakGCCEEKEC8NARAWQQAtAJQKDQFBAEEAKAKwCiIBNgKcCgwHCyADEBVFDQAgAUEEakGMCEEKEC8NABAXC0EAQQAoArAKNgKcCgwBCwJAIAEvAQQiA0EqRg0AIANBL0cNBBAYDAELQQEQGQtBACgCtAohAkEAKAKwCiEBDAALC0EAIQIgAyEBQQAtAPwJDQIMAQtBACABNgKwCkEAQQA6AJQKCwNAQQAgAUECaiIDNgKwCgJAAkACQAJAAkACQAJAIAFBACgCtApPDQAgAy8BACICQXdqQQVJDQYCQAJAAkACQAJAAkACQAJAAkACQCACQWBqDgoQDwYPDw8PBQECAAsCQAJAAkACQCACQaB/ag4KCxISAxIBEhISAgALIAJBhX9qDgMFEQYJC0EALwGYCg0QIAMQFUUNECABQQRqQYIIQQoQLw0QEBYMEAsgAxAVRQ0PIAFBBGpBjAhBChAvDQ8QFwwPCyADEBVFDQ4gASkABELsgISDsI7AOVINDiABLwEMIgNBd2oiAUEXSw0MQQEgAXRBn4CABHFFDQwMDQtBAEEALwGYCiIBQQFqOwGYCkEAKAKkCiABQQN0aiIBQQE2AgAgAUEAKAKcCjYCBAwNC0EALwGYCiIDRQ0JQQAgA0F/aiIDOwGYCkEALwGWCiICRQ0MQQAoAqQKIANB//8DcUEDdGooAgBBBUcNDAJAIAJBAnRBACgCqApqQXxqKAIAIgMoAgQNACADQQAoApwKQQJqNgIEC0EAIAJBf2o7AZYKIAMgAUEEajYCDAwMCwJAQQAoApwKIgEvAQBBKUcNAEEAKALwCSIDRQ0AIAMoAgQgAUcNAEEAQQAoAvQJIgM2AvAJAkAgA0UNACADQQA2AiAMAQtBAEEANgLgCQtBAEEALwGYCiIDQQFqOwGYCkEAKAKkCiADQQN0aiIDQQZBAkEALQCsChs2AgAgAyABNgIEQQBBADoArAoMCwtBAC8BmAoiAUUNB0EAIAFBf2oiATsBmApBACgCpAogAUH//wNxQQN0aigCAEEERg0EDAoLQScQGgwJC0EiEBoMCAsgAkEvRw0HAkACQCABLwEEIgFBKkYNACABQS9HDQEQGAwKC0EBEBkMCQsCQAJAQQAoApwKIgEvAQAiAxAbRQ0AAkACQAJAIANBVWoOBAEIAgAICyABQX5qLwEAQVBqQf//A3FBCkkNAwwHCyABQX5qLwEAQStGDQIMBgsgAUF+ai8BAEEtRg0BDAULAkAgA0H9AEYNACADQSlHDQFBACgCpApBAC8BmApBA3RqKAIEEBxFDQEMBQtBACgCpApBAC8BmApBA3RqIgIoAgQQHQ0EIAIoAgBBBkYNBAsgARAeDQMgA0UNAyADQS9GQQAtAKAKQQBHcQ0DAkBBACgC+AkiAkUNACABIAIoAgBJDQAgASACKAIETQ0ECyABQX5qIQFBACgC3AkhAgJAA0AgAUECaiIEIAJNDQFBACABNgKcCiABLwEAIQMgAUF+aiIEIQEgAxAfRQ0ACyAEQQJqIQQLAkAgA0H//wNxECBFDQAgBEF+aiEBAkADQCABQQJqIgMgAk0NAUEAIAE2ApwKIAEvAQAhAyABQX5qIgQhASADECANAAsgBEECaiEDCyADECENBAtBAEEBOgCgCgwHC0EAKAKkCkEALwGYCiIBQQN0IgNqQQAoApwKNgIEQQAgAUEBajsBmApBACgCpAogA2pBAzYCAAsQIgwFC0EALQD8CUEALwGWCkEALwGYCnJyRSECDAcLECNBAEEAOgCgCgwDCxAkQQAhAgwFCyADQaABRw0BC0EAQQE6AKwKC0EAQQAoArAKNgKcCgtBACgCsAohAQwACwsgAEGA0ABqJAAgAgsaAAJAQQAoAtwJIABHDQBBAQ8LIABBfmoQJQv+CgEGf0EAQQAoArAKIgBBDGoiATYCsApBACgC+AkhAkEBECkhAwJAAkACQAJAAkACQAJAAkACQEEAKAKwCiIEIAFHDQAgAxAoRQ0BCwJAAkACQAJAAkACQAJAIANBKkYNACADQfsARw0BQQAgBEECajYCsApBARApIQNBACgCsAohBANAAkACQCADQf//A3EiA0EiRg0AIANBJ0YNACADECwaQQAoArAKIQMMAQsgAxAaQQBBACgCsApBAmoiAzYCsAoLQQEQKRoCQCAEIAMQLSIDQSxHDQBBAEEAKAKwCkECajYCsApBARApIQMLIANB/QBGDQNBACgCsAoiBSAERg0PIAUhBCAFQQAoArQKTQ0ADA8LC0EAIARBAmo2ArAKQQEQKRpBACgCsAoiAyADEC0aDAILQQBBADoAlAoCQAJAAkACQAJAAkAgA0Gff2oODAILBAELAwsLCwsLBQALIANB9gBGDQQMCgtBACAEQQ5qIgM2ArAKAkACQAJAQQEQKUGff2oOBgASAhISARILQQAoArAKIgUpAAJC84Dkg+CNwDFSDREgBS8BChAgRQ0RQQAgBUEKajYCsApBABApGgtBACgCsAoiBUECakGsCEEOEC8NECAFLwEQIgJBd2oiAUEXSw0NQQEgAXRBn4CABHFFDQ0MDgtBACgCsAoiBSkAAkLsgISDsI7AOVINDyAFLwEKIgJBd2oiAUEXTQ0GDAoLQQAgBEEKajYCsApBABApGkEAKAKwCiEEC0EAIARBEGo2ArAKAkBBARApIgRBKkcNAEEAQQAoArAKQQJqNgKwCkEBECkhBAtBACgCsAohAyAEECwaIANBACgCsAoiBCADIAQQAkEAQQAoArAKQX5qNgKwCg8LAkAgBCkAAkLsgISDsI7AOVINACAELwEKEB9FDQBBACAEQQpqNgKwCkEBECkhBEEAKAKwCiEDIAQQLBogA0EAKAKwCiIEIAMgBBACQQBBACgCsApBfmo2ArAKDwtBACAEQQRqIgQ2ArAKC0EAIARBBmo2ArAKQQBBADoAlApBARApIQRBACgCsAohAyAEECwhBEEAKAKwCiECIARB3/8DcSIBQdsARw0DQQAgAkECajYCsApBARApIQVBACgCsAohA0EAIQQMBAtBAEEBOgCMCkEAQQAoArAKQQJqNgKwCgtBARApIQRBACgCsAohAwJAIARB5gBHDQAgA0ECakGmCEEGEC8NAEEAIANBCGo2ArAKIABBARApQQAQKyACQRBqQeQJIAIbIQMDQCADKAIAIgNFDQUgA0IANwIIIANBEGohAwwACwtBACADQX5qNgKwCgwDC0EBIAF0QZ+AgARxRQ0DDAQLQQEhBAsDQAJAAkAgBA4CAAEBCyAFQf//A3EQLBpBASEEDAELAkACQEEAKAKwCiIEIANGDQAgAyAEIAMgBBACQQEQKSEEAkAgAUHbAEcNACAEQSByQf0ARg0EC0EAKAKwCiEDAkAgBEEsRw0AQQAgA0ECajYCsApBARApIQVBACgCsAohAyAFQSByQfsARw0CC0EAIANBfmo2ArAKCyABQdsARw0CQQAgAkF+ajYCsAoPC0EAIQQMAAsLDwsgAkGgAUYNACACQfsARw0EC0EAIAVBCmo2ArAKQQEQKSIFQfsARg0DDAILAkAgAkFYag4DAQMBAAsgAkGgAUcNAgtBACAFQRBqNgKwCgJAQQEQKSIFQSpHDQBBAEEAKAKwCkECajYCsApBARApIQULIAVBKEYNAQtBACgCsAohASAFECwaQQAoArAKIgUgAU0NACAEIAMgASAFEAJBAEEAKAKwCkF+ajYCsAoPCyAEIANBAEEAEAJBACAEQQxqNgKwCg8LECQL3AgBBn9BACEAQQBBACgCsAoiAUEMaiICNgKwCkEBECkhA0EAKAKwCiEEAkACQAJAAkACQAJAAkACQCADQS5HDQBBACAEQQJqNgKwCgJAQQEQKSIDQfMARg0AIANB7QBHDQdBACgCsAoiA0ECakGWCEEGEC8NBwJAQQAoApwKIgQQKg0AIAQvAQBBLkYNCAsgASABIANBCGpBACgC1AkQAQ8LQQAoArAKIgNBAmpBnAhBChAvDQYCQEEAKAKcCiIEECoNACAELwEAQS5GDQcLIANBDGohAwwBCyADQfMARw0BIAQgAk0NAUEGIQBBACECIARBAmpBnAhBChAvDQIgBEEMaiEDAkAgBC8BDCIFQXdqIgRBF0sNAEEBIAR0QZ+AgARxDQELIAVBoAFHDQILQQAgAzYCsApBASEAQQEQKSEDCwJAAkACQAJAIANB+wBGDQAgA0EoRw0BQQAoAqQKQQAvAZgKIgNBA3RqIgRBACgCsAo2AgRBACADQQFqOwGYCiAEQQU2AgBBACgCnAovAQBBLkYNB0EAQQAoArAKIgRBAmo2ArAKQQEQKSEDIAFBACgCsApBACAEEAECQAJAIAANAEEAKALwCSEEDAELQQAoAvAJIgRBBTYCHAtBAEEALwGWCiIAQQFqOwGWCkEAKAKoCiAAQQJ0aiAENgIAAkAgA0EiRg0AIANBJ0YNAEEAQQAoArAKQX5qNgKwCg8LIAMQGkEAQQAoArAKQQJqIgM2ArAKAkACQAJAQQEQKUFXag4EAQICAAILQQBBACgCsApBAmo2ArAKQQEQKRpBACgC8AkiBCADNgIEIARBAToAGCAEQQAoArAKIgM2AhBBACADQX5qNgKwCg8LQQAoAvAJIgQgAzYCBCAEQQE6ABhBAEEALwGYCkF/ajsBmAogBEEAKAKwCkECajYCDEEAQQAvAZYKQX9qOwGWCg8LQQBBACgCsApBfmo2ArAKDwsgAA0CQQAoArAKIQNBAC8BmAoNAQNAAkACQAJAIANBACgCtApPDQBBARApIgNBIkYNASADQSdGDQEgA0H9AEcNAkEAQQAoArAKQQJqNgKwCgtBARApIQRBACgCsAohAwJAIARB5gBHDQAgA0ECakGmCEEGEC8NCQtBACADQQhqNgKwCgJAQQEQKSIDQSJGDQAgA0EnRw0JCyABIANBABArDwsgAxAaC0EAQQAoArAKQQJqIgM2ArAKDAALCyAADQFBBiEAQQAhAgJAIANBWWoOBAQDAwQACyADQSJGDQMMAgtBACADQX5qNgKwCg8LQQwhAEEBIQILQQAoArAKIgMgASAAQQF0akcNAEEAIANBfmo2ArAKDwtBAC8BmAoNAkEAKAKwCiEDQQAoArQKIQADQCADIABPDQECQAJAIAMvAQAiBEEnRg0AIARBIkcNAQsgASAEIAIQKw8LQQAgA0ECaiIDNgKwCgwACwsQJAsPC0EAQQAoArAKQX5qNgKwCgtHAQN/QQAoArAKQQJqIQBBACgCtAohAQJAA0AgACICQX5qIAFPDQEgAkECaiEAIAIvAQBBdmoOBAEAAAEACwtBACACNgKwCguYAQEDf0EAQQAoArAKIgFBAmo2ArAKIAFBBmohAUEAKAK0CiECA0ACQAJAAkAgAUF8aiACTw0AIAFBfmovAQAhAwJAAkAgAA0AIANBKkYNASADQXZqDgQCBAQCBAsgA0EqRw0DCyABLwEAQS9HDQJBACABQX5qNgKwCgwBCyABQX5qIQELQQAgATYCsAoPCyABQQJqIQEMAAsLiAEBBH9BACgCsAohAUEAKAK0CiECAkACQANAIAEiA0ECaiEBIAMgAk8NASABLwEAIgQgAEYNAgJAIARB3ABGDQAgBEF2ag4EAgEBAgELIANBBGohASADLwEEQQ1HDQAgA0EGaiABIAMvAQZBCkYbIQEMAAsLQQAgATYCsAoQJA8LQQAgATYCsAoLbAEBfwJAAkAgAEFfaiIBQQVLDQBBASABdEExcQ0BCyAAQUZqQf//A3FBBkkNACAAQSlHIABBWGpB//8DcUEHSXENAAJAIABBpX9qDgQBAAABAAsgAEH9AEcgAEGFf2pB//8DcUEESXEPC0EBCy4BAX9BASEBAkAgAEGgCUEFECYNACAAQaoJQQMQJg0AIABBsAlBAhAmIQELIAELgwEBAn9BASEBAkACQAJAAkACQAJAIAAvAQAiAkFFag4EBQQEAQALAkAgAkGbf2oOBAMEBAIACyACQSlGDQQgAkH5AEcNAyAAQX5qQbwJQQYQJg8LIABBfmovAQBBPUYPCyAAQX5qQbQJQQQQJg8LIABBfmpByAlBAxAmDwtBACEBCyABC7QDAQJ/QQAhAQJAAkACQAJAAkACQAJAAkACQAJAIAAvAQBBnH9qDhQAAQIJCQkJAwkJBAUJCQYJBwkJCAkLAkACQCAAQX5qLwEAQZd/ag4EAAoKAQoLIABBfGpBxAhBAhAmDwsgAEF8akHICEEDECYPCwJAAkACQCAAQX5qLwEAQY1/ag4DAAECCgsCQCAAQXxqLwEAIgJB4QBGDQAgAkHsAEcNCiAAQXpqQeUAECcPCyAAQXpqQeMAECcPCyAAQXxqQc4IQQQQJg8LIABBfGpB1ghBBhAmDwsgAEF+ai8BAEHvAEcNBiAAQXxqLwEAQeUARw0GAkAgAEF6ai8BACICQfAARg0AIAJB4wBHDQcgAEF4akHiCEEGECYPCyAAQXhqQe4IQQIQJg8LIABBfmpB8ghBBBAmDwtBASEBIABBfmoiAEHpABAnDQQgAEH6CEEFECYPCyAAQX5qQeQAECcPCyAAQX5qQYQJQQcQJg8LIABBfmpBkglBBBAmDwsCQCAAQX5qLwEAIgJB7wBGDQAgAkHlAEcNASAAQXxqQe4AECcPCyAAQXxqQZoJQQMQJiEBCyABCzQBAX9BASEBAkAgAEF3akH//wNxQQVJDQAgAEGAAXJBoAFGDQAgAEEuRyAAEChxIQELIAELMAEBfwJAAkAgAEF3aiIBQRdLDQBBASABdEGNgIAEcQ0BCyAAQaABRg0AQQAPC0EBC04BAn9BACEBAkACQCAALwEAIgJB5QBGDQAgAkHrAEcNASAAQX5qQfIIQQQQJg8LIABBfmovAQBB9QBHDQAgAEF8akHWCEEGECYhAQsgAQveAQEEf0EAKAKwCiEAQQAoArQKIQECQAJAAkADQCAAIgJBAmohACACIAFPDQECQAJAAkAgAC8BACIDQaR/ag4FAgMDAwEACyADQSRHDQIgAi8BBEH7AEcNAkEAIAJBBGoiADYCsApBAEEALwGYCiICQQFqOwGYCkEAKAKkCiACQQN0aiICQQQ2AgAgAiAANgIEDwtBACAANgKwCkEAQQAvAZgKQX9qIgA7AZgKQQAoAqQKIABB//8DcUEDdGooAgBBA0cNAwwECyACQQRqIQAMAAsLQQAgADYCsAoLECQLC3ABAn8CQAJAA0BBAEEAKAKwCiIAQQJqIgE2ArAKIABBACgCtApPDQECQAJAAkAgAS8BACIBQaV/ag4CAQIACwJAIAFBdmoOBAQDAwQACyABQS9HDQIMBAsQLhoMAQtBACAAQQRqNgKwCgwACwsQJAsLNQEBf0EAQQE6APwJQQAoArAKIQBBAEEAKAK0CkECajYCsApBACAAQQAoAtwJa0EBdTYCkAoLQwECf0EBIQECQCAALwEAIgJBd2pB//8DcUEFSQ0AIAJBgAFyQaABRg0AQQAhASACEChFDQAgAkEuRyAAECpyDwsgAQtGAQN/QQAhAwJAIAAgAkEBdCICayIEQQJqIgBBACgC3AkiBUkNACAAIAEgAhAvDQACQCAAIAVHDQBBAQ8LIAQQJSEDCyADCz0BAn9BACECAkBBACgC3AkiAyAASw0AIAAvAQAgAUcNAAJAIAMgAEcNAEEBDwsgAEF+ai8BABAfIQILIAILaAECf0EBIQECQAJAIABBX2oiAkEFSw0AQQEgAnRBMXENAQsgAEH4/wNxQShGDQAgAEFGakH//wNxQQZJDQACQCAAQaV/aiICQQNLDQAgAkEBRw0BCyAAQYV/akH//wNxQQRJIQELIAELnAEBA39BACgCsAohAQJAA0ACQAJAIAEvAQAiAkEvRw0AAkAgAS8BAiIBQSpGDQAgAUEvRw0EEBgMAgsgABAZDAELAkACQCAARQ0AIAJBd2oiAUEXSw0BQQEgAXRBn4CABHFFDQEMAgsgAhAgRQ0DDAELIAJBoAFHDQILQQBBACgCsAoiA0ECaiIBNgKwCiADQQAoArQKSQ0ACwsgAgsxAQF/QQAhAQJAIAAvAQBBLkcNACAAQX5qLwEAQS5HDQAgAEF8ai8BAEEuRiEBCyABC5wEAQF/AkAgAUEiRg0AIAFBJ0YNABAkDwtBACgCsAohAyABEBogACADQQJqQQAoArAKQQAoAtAJEAECQCACRQ0AQQAoAvAJQQQ2AhwLQQBBACgCsApBAmo2ArAKAkACQAJAAkBBABApIgFB4QBGDQAgAUH3AEYNAUEAKAKwCiEBDAILQQAoArAKIgFBAmpBughBChAvDQFBBiEADAILQQAoArAKIgEvAQJB6QBHDQAgAS8BBEH0AEcNAEEEIQAgAS8BBkHoAEYNAQtBACABQX5qNgKwCg8LQQAgASAAQQF0ajYCsAoCQEEBEClB+wBGDQBBACABNgKwCg8LQQAoArAKIgIhAANAQQAgAEECajYCsAoCQAJAAkBBARApIgBBIkYNACAAQSdHDQFBJxAaQQBBACgCsApBAmo2ArAKQQEQKSEADAILQSIQGkEAQQAoArAKQQJqNgKwCkEBECkhAAwBCyAAECwhAAsCQCAAQTpGDQBBACABNgKwCg8LQQBBACgCsApBAmo2ArAKAkBBARApIgBBIkYNACAAQSdGDQBBACABNgKwCg8LIAAQGkEAQQAoArAKQQJqNgKwCgJAAkBBARApIgBBLEYNACAAQf0ARg0BQQAgATYCsAoPC0EAQQAoArAKQQJqNgKwCkEBEClB/QBGDQBBACgCsAohAAwBCwtBACgC8AkiASACNgIQIAFBACgCsApBAmo2AgwLbQECfwJAAkADQAJAIABB//8DcSIBQXdqIgJBF0sNAEEBIAJ0QZ+AgARxDQILIAFBoAFGDQEgACECIAEQKA0CQQAhAkEAQQAoArAKIgBBAmo2ArAKIAAvAQIiAA0ADAILCyAAIQILIAJB//8DcQurAQEEfwJAAkBBACgCsAoiAi8BACIDQeEARg0AIAEhBCAAIQUMAQtBACACQQRqNgKwCkEBECkhAkEAKAKwCiEFAkACQCACQSJGDQAgAkEnRg0AIAIQLBpBACgCsAohBAwBCyACEBpBAEEAKAKwCkECaiIENgKwCgtBARApIQNBACgCsAohAgsCQCACIAVGDQAgBSAEQQAgACAAIAFGIgIbQQAgASACGxACCyADC3IBBH9BACgCsAohAEEAKAK0CiEBAkACQANAIABBAmohAiAAIAFPDQECQAJAIAIvAQAiA0Gkf2oOAgEEAAsgAiEAIANBdmoOBAIBAQIBCyAAQQRqIQAMAAsLQQAgAjYCsAoQJEEADwtBACACNgKwCkHdAAtJAQN/QQAhAwJAIAJFDQACQANAIAAtAAAiBCABLQAAIgVHDQEgAUEBaiEBIABBAWohACACQX9qIgINAAwCCwsgBCAFayEDCyADCwvsAQIAQYAIC84BAAB4AHAAbwByAHQAbQBwAG8AcgB0AGUAdABhAG8AdQByAGMAZQByAG8AbQB1AG4AYwB0AGkAbwBuAHMAcwBlAHIAdAB2AG8AeQBpAGUAZABlAGwAZQBjAG8AbgB0AGkAbgBpAG4AcwB0AGEAbgB0AHkAYgByAGUAYQByAGUAdAB1AHIAZABlAGIAdQBnAGcAZQBhAHcAYQBpAHQAaAByAHcAaABpAGwAZQBmAG8AcgBpAGYAYwBhAHQAYwBmAGkAbgBhAGwAbABlAGwAcwAAQdAJCxABAAAAAgAAAAAEAABAOQAA","undefined"!=typeof Buffer?Buffer.from(E,"base64"):Uint8Array.from(atob(E),(A=>A.charCodeAt(0))))).then(WebAssembly.instantiate).then((({exports:A})=>{C=A;}));var E;
  14721. var convertSourceMap$1 = {};
  14722. (function (exports) {
  14723. Object.defineProperty(exports, 'commentRegex', {
  14724. get: function getCommentRegex () {
  14725. // Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
  14726. return /^\s*?\/[\/\*][@#]\s+?sourceMappingURL=data:(((?:application|text)\/json)(?:;charset=([^;,]+?)?)?)?(?:;(base64))?,(.*?)$/mg;
  14727. }
  14728. });
  14729. Object.defineProperty(exports, 'mapFileCommentRegex', {
  14730. get: function getMapFileCommentRegex () {
  14731. // Matches sourceMappingURL in either // or /* comment styles.
  14732. return /(?:\/\/[@#][ \t]+?sourceMappingURL=([^\s'"`]+?)[ \t]*?$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*?(?:\*\/){1}[ \t]*?$)/mg;
  14733. }
  14734. });
  14735. var decodeBase64;
  14736. if (typeof Buffer !== 'undefined') {
  14737. if (typeof Buffer.from === 'function') {
  14738. decodeBase64 = decodeBase64WithBufferFrom;
  14739. } else {
  14740. decodeBase64 = decodeBase64WithNewBuffer;
  14741. }
  14742. } else {
  14743. decodeBase64 = decodeBase64WithAtob;
  14744. }
  14745. function decodeBase64WithBufferFrom(base64) {
  14746. return Buffer.from(base64, 'base64').toString();
  14747. }
  14748. function decodeBase64WithNewBuffer(base64) {
  14749. if (typeof value === 'number') {
  14750. throw new TypeError('The value to decode must not be of type number.');
  14751. }
  14752. return new Buffer(base64, 'base64').toString();
  14753. }
  14754. function decodeBase64WithAtob(base64) {
  14755. return decodeURIComponent(escape(atob(base64)));
  14756. }
  14757. function stripComment(sm) {
  14758. return sm.split(',').pop();
  14759. }
  14760. function readFromFileMap(sm, read) {
  14761. var r = exports.mapFileCommentRegex.exec(sm);
  14762. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  14763. var filename = r[1] || r[2];
  14764. try {
  14765. var sm = read(filename);
  14766. if (sm != null && typeof sm.catch === 'function') {
  14767. return sm.catch(throwError);
  14768. } else {
  14769. return sm;
  14770. }
  14771. } catch (e) {
  14772. throwError(e);
  14773. }
  14774. function throwError(e) {
  14775. throw new Error('An error occurred while trying to read the map file at ' + filename + '\n' + e.stack);
  14776. }
  14777. }
  14778. function Converter (sm, opts) {
  14779. opts = opts || {};
  14780. if (opts.hasComment) {
  14781. sm = stripComment(sm);
  14782. }
  14783. if (opts.encoding === 'base64') {
  14784. sm = decodeBase64(sm);
  14785. } else if (opts.encoding === 'uri') {
  14786. sm = decodeURIComponent(sm);
  14787. }
  14788. if (opts.isJSON || opts.encoding) {
  14789. sm = JSON.parse(sm);
  14790. }
  14791. this.sourcemap = sm;
  14792. }
  14793. Converter.prototype.toJSON = function (space) {
  14794. return JSON.stringify(this.sourcemap, null, space);
  14795. };
  14796. if (typeof Buffer !== 'undefined') {
  14797. if (typeof Buffer.from === 'function') {
  14798. Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
  14799. } else {
  14800. Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
  14801. }
  14802. } else {
  14803. Converter.prototype.toBase64 = encodeBase64WithBtoa;
  14804. }
  14805. function encodeBase64WithBufferFrom() {
  14806. var json = this.toJSON();
  14807. return Buffer.from(json, 'utf8').toString('base64');
  14808. }
  14809. function encodeBase64WithNewBuffer() {
  14810. var json = this.toJSON();
  14811. if (typeof json === 'number') {
  14812. throw new TypeError('The json to encode must not be of type number.');
  14813. }
  14814. return new Buffer(json, 'utf8').toString('base64');
  14815. }
  14816. function encodeBase64WithBtoa() {
  14817. var json = this.toJSON();
  14818. return btoa(unescape(encodeURIComponent(json)));
  14819. }
  14820. Converter.prototype.toURI = function () {
  14821. var json = this.toJSON();
  14822. return encodeURIComponent(json);
  14823. };
  14824. Converter.prototype.toComment = function (options) {
  14825. var encoding, content, data;
  14826. if (options != null && options.encoding === 'uri') {
  14827. encoding = '';
  14828. content = this.toURI();
  14829. } else {
  14830. encoding = ';base64';
  14831. content = this.toBase64();
  14832. }
  14833. data = 'sourceMappingURL=data:application/json;charset=utf-8' + encoding + ',' + content;
  14834. return options != null && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  14835. };
  14836. // returns copy instead of original
  14837. Converter.prototype.toObject = function () {
  14838. return JSON.parse(this.toJSON());
  14839. };
  14840. Converter.prototype.addProperty = function (key, value) {
  14841. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
  14842. return this.setProperty(key, value);
  14843. };
  14844. Converter.prototype.setProperty = function (key, value) {
  14845. this.sourcemap[key] = value;
  14846. return this;
  14847. };
  14848. Converter.prototype.getProperty = function (key) {
  14849. return this.sourcemap[key];
  14850. };
  14851. exports.fromObject = function (obj) {
  14852. return new Converter(obj);
  14853. };
  14854. exports.fromJSON = function (json) {
  14855. return new Converter(json, { isJSON: true });
  14856. };
  14857. exports.fromURI = function (uri) {
  14858. return new Converter(uri, { encoding: 'uri' });
  14859. };
  14860. exports.fromBase64 = function (base64) {
  14861. return new Converter(base64, { encoding: 'base64' });
  14862. };
  14863. exports.fromComment = function (comment) {
  14864. var m, encoding;
  14865. comment = comment
  14866. .replace(/^\/\*/g, '//')
  14867. .replace(/\*\/$/g, '');
  14868. m = exports.commentRegex.exec(comment);
  14869. encoding = m && m[4] || 'uri';
  14870. return new Converter(comment, { encoding: encoding, hasComment: true });
  14871. };
  14872. function makeConverter(sm) {
  14873. return new Converter(sm, { isJSON: true });
  14874. }
  14875. exports.fromMapFileComment = function (comment, read) {
  14876. if (typeof read === 'string') {
  14877. throw new Error(
  14878. 'String directory paths are no longer supported with `fromMapFileComment`\n' +
  14879. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  14880. )
  14881. }
  14882. var sm = readFromFileMap(comment, read);
  14883. if (sm != null && typeof sm.then === 'function') {
  14884. return sm.then(makeConverter);
  14885. } else {
  14886. return makeConverter(sm);
  14887. }
  14888. };
  14889. // Finds last sourcemap comment in file or returns null if none was found
  14890. exports.fromSource = function (content) {
  14891. var m = content.match(exports.commentRegex);
  14892. return m ? exports.fromComment(m.pop()) : null;
  14893. };
  14894. // Finds last sourcemap comment in file or returns null if none was found
  14895. exports.fromMapFileSource = function (content, read) {
  14896. if (typeof read === 'string') {
  14897. throw new Error(
  14898. 'String directory paths are no longer supported with `fromMapFileSource`\n' +
  14899. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  14900. )
  14901. }
  14902. var m = content.match(exports.mapFileCommentRegex);
  14903. return m ? exports.fromMapFileComment(m.pop(), read) : null;
  14904. };
  14905. exports.removeComments = function (src) {
  14906. return src.replace(exports.commentRegex, '');
  14907. };
  14908. exports.removeMapFileComments = function (src) {
  14909. return src.replace(exports.mapFileCommentRegex, '');
  14910. };
  14911. exports.generateMapFileComment = function (file, options) {
  14912. var data = 'sourceMappingURL=' + file;
  14913. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  14914. };
  14915. } (convertSourceMap$1));
  14916. var convertSourceMap = /*@__PURE__*/getDefaultExportFromCjs(convertSourceMap$1);
  14917. const debug$g = createDebugger('vite:sourcemap', {
  14918. onlyWhenFocused: true,
  14919. });
  14920. // Virtual modules should be prefixed with a null byte to avoid a
  14921. // false positive "missing source" warning. We also check for certain
  14922. // prefixes used for special handling in esbuildDepPlugin.
  14923. const virtualSourceRE = /^(?:dep:|browser-external:|virtual:)|\0/;
  14924. async function computeSourceRoute(map, file) {
  14925. let sourceRoot;
  14926. try {
  14927. // The source root is undefined for virtual modules and permission errors.
  14928. sourceRoot = await fsp.realpath(path$o.resolve(path$o.dirname(file), map.sourceRoot || ''));
  14929. }
  14930. catch { }
  14931. return sourceRoot;
  14932. }
  14933. async function injectSourcesContent(map, file, logger) {
  14934. let sourceRootPromise;
  14935. const missingSources = [];
  14936. const sourcesContent = map.sourcesContent || [];
  14937. const sourcesContentPromises = [];
  14938. for (let index = 0; index < map.sources.length; index++) {
  14939. const sourcePath = map.sources[index];
  14940. if (sourcesContent[index] == null &&
  14941. sourcePath &&
  14942. !virtualSourceRE.test(sourcePath)) {
  14943. sourcesContentPromises.push((async () => {
  14944. // inject content from source file when sourcesContent is null
  14945. sourceRootPromise ??= computeSourceRoute(map, file);
  14946. const sourceRoot = await sourceRootPromise;
  14947. let resolvedSourcePath = decodeURI(sourcePath);
  14948. if (sourceRoot) {
  14949. resolvedSourcePath = path$o.resolve(sourceRoot, resolvedSourcePath);
  14950. }
  14951. sourcesContent[index] = await fsp
  14952. .readFile(resolvedSourcePath, 'utf-8')
  14953. .catch(() => {
  14954. missingSources.push(resolvedSourcePath);
  14955. return null;
  14956. });
  14957. })());
  14958. }
  14959. }
  14960. await Promise.all(sourcesContentPromises);
  14961. map.sourcesContent = sourcesContent;
  14962. // Use this command…
  14963. // DEBUG="vite:sourcemap" vite build
  14964. // …to log the missing sources.
  14965. if (missingSources.length) {
  14966. logger.warnOnce(`Sourcemap for "${file}" points to missing source files`);
  14967. debug$g?.(`Missing sources:\n ` + missingSources.join(`\n `));
  14968. }
  14969. }
  14970. function genSourceMapUrl(map) {
  14971. if (typeof map !== 'string') {
  14972. map = JSON.stringify(map);
  14973. }
  14974. return `data:application/json;base64,${Buffer.from(map).toString('base64')}`;
  14975. }
  14976. function getCodeWithSourcemap(type, code, map) {
  14977. if (debug$g) {
  14978. code += `\n/*${JSON.stringify(map, null, 2).replace(/\*\//g, '*\\/')}*/\n`;
  14979. }
  14980. if (type === 'js') {
  14981. code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`;
  14982. }
  14983. else if (type === 'css') {
  14984. code += `\n/*# sourceMappingURL=${genSourceMapUrl(map)} */`;
  14985. }
  14986. return code;
  14987. }
  14988. function applySourcemapIgnoreList(map, sourcemapPath, sourcemapIgnoreList, logger) {
  14989. let { x_google_ignoreList } = map;
  14990. if (x_google_ignoreList === undefined) {
  14991. x_google_ignoreList = [];
  14992. }
  14993. for (let sourcesIndex = 0; sourcesIndex < map.sources.length; ++sourcesIndex) {
  14994. const sourcePath = map.sources[sourcesIndex];
  14995. if (!sourcePath)
  14996. continue;
  14997. const ignoreList = sourcemapIgnoreList(path$o.isAbsolute(sourcePath)
  14998. ? sourcePath
  14999. : path$o.resolve(path$o.dirname(sourcemapPath), sourcePath), sourcemapPath);
  15000. if (logger && typeof ignoreList !== 'boolean') {
  15001. logger.warn('sourcemapIgnoreList function must return a boolean.');
  15002. }
  15003. if (ignoreList && !x_google_ignoreList.includes(sourcesIndex)) {
  15004. x_google_ignoreList.push(sourcesIndex);
  15005. }
  15006. }
  15007. if (x_google_ignoreList.length > 0) {
  15008. if (!map.x_google_ignoreList)
  15009. map.x_google_ignoreList = x_google_ignoreList;
  15010. }
  15011. }
  15012. async function extractSourcemapFromFile(code, filePath) {
  15013. const map = (convertSourceMap.fromSource(code) ||
  15014. (await convertSourceMap.fromMapFileSource(code, createConvertSourceMapReadMap(filePath))))?.toObject();
  15015. if (map) {
  15016. return {
  15017. code: code.replace(convertSourceMap.mapFileCommentRegex, blankReplacer),
  15018. map,
  15019. };
  15020. }
  15021. }
  15022. function createConvertSourceMapReadMap(originalFileName) {
  15023. return (filename) => {
  15024. return fsp.readFile(path$o.resolve(path$o.dirname(originalFileName), filename), 'utf-8');
  15025. };
  15026. }
  15027. var tasks = {};
  15028. var utils$g = {};
  15029. var array$1 = {};
  15030. Object.defineProperty(array$1, "__esModule", { value: true });
  15031. array$1.splitWhen = array$1.flatten = void 0;
  15032. function flatten$1(items) {
  15033. return items.reduce((collection, item) => [].concat(collection, item), []);
  15034. }
  15035. array$1.flatten = flatten$1;
  15036. function splitWhen(items, predicate) {
  15037. const result = [[]];
  15038. let groupIndex = 0;
  15039. for (const item of items) {
  15040. if (predicate(item)) {
  15041. groupIndex++;
  15042. result[groupIndex] = [];
  15043. }
  15044. else {
  15045. result[groupIndex].push(item);
  15046. }
  15047. }
  15048. return result;
  15049. }
  15050. array$1.splitWhen = splitWhen;
  15051. var errno$1 = {};
  15052. Object.defineProperty(errno$1, "__esModule", { value: true });
  15053. errno$1.isEnoentCodeError = void 0;
  15054. function isEnoentCodeError(error) {
  15055. return error.code === 'ENOENT';
  15056. }
  15057. errno$1.isEnoentCodeError = isEnoentCodeError;
  15058. var fs$h = {};
  15059. Object.defineProperty(fs$h, "__esModule", { value: true });
  15060. fs$h.createDirentFromStats = void 0;
  15061. let DirentFromStats$1 = class DirentFromStats {
  15062. constructor(name, stats) {
  15063. this.name = name;
  15064. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  15065. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  15066. this.isDirectory = stats.isDirectory.bind(stats);
  15067. this.isFIFO = stats.isFIFO.bind(stats);
  15068. this.isFile = stats.isFile.bind(stats);
  15069. this.isSocket = stats.isSocket.bind(stats);
  15070. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  15071. }
  15072. };
  15073. function createDirentFromStats$1(name, stats) {
  15074. return new DirentFromStats$1(name, stats);
  15075. }
  15076. fs$h.createDirentFromStats = createDirentFromStats$1;
  15077. var path$h = {};
  15078. Object.defineProperty(path$h, "__esModule", { value: true });
  15079. path$h.convertPosixPathToPattern = path$h.convertWindowsPathToPattern = path$h.convertPathToPattern = path$h.escapePosixPath = path$h.escapeWindowsPath = path$h.escape = path$h.removeLeadingDotSegment = path$h.makeAbsolute = path$h.unixify = void 0;
  15080. const os$3 = require$$2;
  15081. const path$g = require$$0$4;
  15082. const IS_WINDOWS_PLATFORM = os$3.platform() === 'win32';
  15083. const LEADING_DOT_SEGMENT_CHARACTERS_COUNT = 2; // ./ or .\\
  15084. /**
  15085. * All non-escaped special characters.
  15086. * Posix: ()*?[]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
  15087. * Windows: (){}[], !+@ before (, ! at the beginning.
  15088. */
  15089. const POSIX_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\()|\\(?![!()*+?@[\]{|}]))/g;
  15090. const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()[\]{}]|^!|[!+@](?=\())/g;
  15091. /**
  15092. * The device path (\\.\ or \\?\).
  15093. * https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
  15094. */
  15095. const DOS_DEVICE_PATH_RE = /^\\\\([.?])/;
  15096. /**
  15097. * All backslashes except those escaping special characters.
  15098. * Windows: !()+@{}
  15099. * https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
  15100. */
  15101. const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@[\]{}])/g;
  15102. /**
  15103. * Designed to work only with simple paths: `dir\\file`.
  15104. */
  15105. function unixify(filepath) {
  15106. return filepath.replace(/\\/g, '/');
  15107. }
  15108. path$h.unixify = unixify;
  15109. function makeAbsolute(cwd, filepath) {
  15110. return path$g.resolve(cwd, filepath);
  15111. }
  15112. path$h.makeAbsolute = makeAbsolute;
  15113. function removeLeadingDotSegment(entry) {
  15114. // We do not use `startsWith` because this is 10x slower than current implementation for some cases.
  15115. // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
  15116. if (entry.charAt(0) === '.') {
  15117. const secondCharactery = entry.charAt(1);
  15118. if (secondCharactery === '/' || secondCharactery === '\\') {
  15119. return entry.slice(LEADING_DOT_SEGMENT_CHARACTERS_COUNT);
  15120. }
  15121. }
  15122. return entry;
  15123. }
  15124. path$h.removeLeadingDotSegment = removeLeadingDotSegment;
  15125. path$h.escape = IS_WINDOWS_PLATFORM ? escapeWindowsPath : escapePosixPath;
  15126. function escapeWindowsPath(pattern) {
  15127. return pattern.replace(WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  15128. }
  15129. path$h.escapeWindowsPath = escapeWindowsPath;
  15130. function escapePosixPath(pattern) {
  15131. return pattern.replace(POSIX_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  15132. }
  15133. path$h.escapePosixPath = escapePosixPath;
  15134. path$h.convertPathToPattern = IS_WINDOWS_PLATFORM ? convertWindowsPathToPattern : convertPosixPathToPattern;
  15135. function convertWindowsPathToPattern(filepath) {
  15136. return escapeWindowsPath(filepath)
  15137. .replace(DOS_DEVICE_PATH_RE, '//$1')
  15138. .replace(WINDOWS_BACKSLASHES_RE, '/');
  15139. }
  15140. path$h.convertWindowsPathToPattern = convertWindowsPathToPattern;
  15141. function convertPosixPathToPattern(filepath) {
  15142. return escapePosixPath(filepath);
  15143. }
  15144. path$h.convertPosixPathToPattern = convertPosixPathToPattern;
  15145. var pattern$1 = {};
  15146. /*!
  15147. * is-extglob <https://github.com/jonschlinkert/is-extglob>
  15148. *
  15149. * Copyright (c) 2014-2016, Jon Schlinkert.
  15150. * Licensed under the MIT License.
  15151. */
  15152. var isExtglob$1 = function isExtglob(str) {
  15153. if (typeof str !== 'string' || str === '') {
  15154. return false;
  15155. }
  15156. var match;
  15157. while ((match = /(\\).|([@?!+*]\(.*\))/g.exec(str))) {
  15158. if (match[2]) return true;
  15159. str = str.slice(match.index + match[0].length);
  15160. }
  15161. return false;
  15162. };
  15163. /*!
  15164. * is-glob <https://github.com/jonschlinkert/is-glob>
  15165. *
  15166. * Copyright (c) 2014-2017, Jon Schlinkert.
  15167. * Released under the MIT License.
  15168. */
  15169. var isExtglob = isExtglob$1;
  15170. var chars = { '{': '}', '(': ')', '[': ']'};
  15171. var strictCheck = function(str) {
  15172. if (str[0] === '!') {
  15173. return true;
  15174. }
  15175. var index = 0;
  15176. var pipeIndex = -2;
  15177. var closeSquareIndex = -2;
  15178. var closeCurlyIndex = -2;
  15179. var closeParenIndex = -2;
  15180. var backSlashIndex = -2;
  15181. while (index < str.length) {
  15182. if (str[index] === '*') {
  15183. return true;
  15184. }
  15185. if (str[index + 1] === '?' && /[\].+)]/.test(str[index])) {
  15186. return true;
  15187. }
  15188. if (closeSquareIndex !== -1 && str[index] === '[' && str[index + 1] !== ']') {
  15189. if (closeSquareIndex < index) {
  15190. closeSquareIndex = str.indexOf(']', index);
  15191. }
  15192. if (closeSquareIndex > index) {
  15193. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  15194. return true;
  15195. }
  15196. backSlashIndex = str.indexOf('\\', index);
  15197. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  15198. return true;
  15199. }
  15200. }
  15201. }
  15202. if (closeCurlyIndex !== -1 && str[index] === '{' && str[index + 1] !== '}') {
  15203. closeCurlyIndex = str.indexOf('}', index);
  15204. if (closeCurlyIndex > index) {
  15205. backSlashIndex = str.indexOf('\\', index);
  15206. if (backSlashIndex === -1 || backSlashIndex > closeCurlyIndex) {
  15207. return true;
  15208. }
  15209. }
  15210. }
  15211. if (closeParenIndex !== -1 && str[index] === '(' && str[index + 1] === '?' && /[:!=]/.test(str[index + 2]) && str[index + 3] !== ')') {
  15212. closeParenIndex = str.indexOf(')', index);
  15213. if (closeParenIndex > index) {
  15214. backSlashIndex = str.indexOf('\\', index);
  15215. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  15216. return true;
  15217. }
  15218. }
  15219. }
  15220. if (pipeIndex !== -1 && str[index] === '(' && str[index + 1] !== '|') {
  15221. if (pipeIndex < index) {
  15222. pipeIndex = str.indexOf('|', index);
  15223. }
  15224. if (pipeIndex !== -1 && str[pipeIndex + 1] !== ')') {
  15225. closeParenIndex = str.indexOf(')', pipeIndex);
  15226. if (closeParenIndex > pipeIndex) {
  15227. backSlashIndex = str.indexOf('\\', pipeIndex);
  15228. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  15229. return true;
  15230. }
  15231. }
  15232. }
  15233. }
  15234. if (str[index] === '\\') {
  15235. var open = str[index + 1];
  15236. index += 2;
  15237. var close = chars[open];
  15238. if (close) {
  15239. var n = str.indexOf(close, index);
  15240. if (n !== -1) {
  15241. index = n + 1;
  15242. }
  15243. }
  15244. if (str[index] === '!') {
  15245. return true;
  15246. }
  15247. } else {
  15248. index++;
  15249. }
  15250. }
  15251. return false;
  15252. };
  15253. var relaxedCheck = function(str) {
  15254. if (str[0] === '!') {
  15255. return true;
  15256. }
  15257. var index = 0;
  15258. while (index < str.length) {
  15259. if (/[*?{}()[\]]/.test(str[index])) {
  15260. return true;
  15261. }
  15262. if (str[index] === '\\') {
  15263. var open = str[index + 1];
  15264. index += 2;
  15265. var close = chars[open];
  15266. if (close) {
  15267. var n = str.indexOf(close, index);
  15268. if (n !== -1) {
  15269. index = n + 1;
  15270. }
  15271. }
  15272. if (str[index] === '!') {
  15273. return true;
  15274. }
  15275. } else {
  15276. index++;
  15277. }
  15278. }
  15279. return false;
  15280. };
  15281. var isGlob$2 = function isGlob(str, options) {
  15282. if (typeof str !== 'string' || str === '') {
  15283. return false;
  15284. }
  15285. if (isExtglob(str)) {
  15286. return true;
  15287. }
  15288. var check = strictCheck;
  15289. // optionally relax check
  15290. if (options && options.strict === false) {
  15291. check = relaxedCheck;
  15292. }
  15293. return check(str);
  15294. };
  15295. var isGlob$1 = isGlob$2;
  15296. var pathPosixDirname = require$$0$4.posix.dirname;
  15297. var isWin32 = require$$2.platform() === 'win32';
  15298. var slash = '/';
  15299. var backslash = /\\/g;
  15300. var enclosure = /[\{\[].*[\}\]]$/;
  15301. var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
  15302. var escaped = /\\([\!\*\?\|\[\]\(\)\{\}])/g;
  15303. /**
  15304. * @param {string} str
  15305. * @param {Object} opts
  15306. * @param {boolean} [opts.flipBackslashes=true]
  15307. * @returns {string}
  15308. */
  15309. var globParent$2 = function globParent(str, opts) {
  15310. var options = Object.assign({ flipBackslashes: true }, opts);
  15311. // flip windows path separators
  15312. if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
  15313. str = str.replace(backslash, slash);
  15314. }
  15315. // special case for strings ending in enclosure containing path separator
  15316. if (enclosure.test(str)) {
  15317. str += slash;
  15318. }
  15319. // preserves full path in case of trailing path separator
  15320. str += 'a';
  15321. // remove path parts that are globby
  15322. do {
  15323. str = pathPosixDirname(str);
  15324. } while (isGlob$1(str) || globby.test(str));
  15325. // remove escape chars and return result
  15326. return str.replace(escaped, '$1');
  15327. };
  15328. var utils$f = {};
  15329. (function (exports) {
  15330. exports.isInteger = num => {
  15331. if (typeof num === 'number') {
  15332. return Number.isInteger(num);
  15333. }
  15334. if (typeof num === 'string' && num.trim() !== '') {
  15335. return Number.isInteger(Number(num));
  15336. }
  15337. return false;
  15338. };
  15339. /**
  15340. * Find a node of the given type
  15341. */
  15342. exports.find = (node, type) => node.nodes.find(node => node.type === type);
  15343. /**
  15344. * Find a node of the given type
  15345. */
  15346. exports.exceedsLimit = (min, max, step = 1, limit) => {
  15347. if (limit === false) return false;
  15348. if (!exports.isInteger(min) || !exports.isInteger(max)) return false;
  15349. return ((Number(max) - Number(min)) / Number(step)) >= limit;
  15350. };
  15351. /**
  15352. * Escape the given node with '\\' before node.value
  15353. */
  15354. exports.escapeNode = (block, n = 0, type) => {
  15355. let node = block.nodes[n];
  15356. if (!node) return;
  15357. if ((type && node.type === type) || node.type === 'open' || node.type === 'close') {
  15358. if (node.escaped !== true) {
  15359. node.value = '\\' + node.value;
  15360. node.escaped = true;
  15361. }
  15362. }
  15363. };
  15364. /**
  15365. * Returns true if the given brace node should be enclosed in literal braces
  15366. */
  15367. exports.encloseBrace = node => {
  15368. if (node.type !== 'brace') return false;
  15369. if ((node.commas >> 0 + node.ranges >> 0) === 0) {
  15370. node.invalid = true;
  15371. return true;
  15372. }
  15373. return false;
  15374. };
  15375. /**
  15376. * Returns true if a brace node is invalid.
  15377. */
  15378. exports.isInvalidBrace = block => {
  15379. if (block.type !== 'brace') return false;
  15380. if (block.invalid === true || block.dollar) return true;
  15381. if ((block.commas >> 0 + block.ranges >> 0) === 0) {
  15382. block.invalid = true;
  15383. return true;
  15384. }
  15385. if (block.open !== true || block.close !== true) {
  15386. block.invalid = true;
  15387. return true;
  15388. }
  15389. return false;
  15390. };
  15391. /**
  15392. * Returns true if a node is an open or close node
  15393. */
  15394. exports.isOpenOrClose = node => {
  15395. if (node.type === 'open' || node.type === 'close') {
  15396. return true;
  15397. }
  15398. return node.open === true || node.close === true;
  15399. };
  15400. /**
  15401. * Reduce an array of text nodes.
  15402. */
  15403. exports.reduce = nodes => nodes.reduce((acc, node) => {
  15404. if (node.type === 'text') acc.push(node.value);
  15405. if (node.type === 'range') node.type = 'text';
  15406. return acc;
  15407. }, []);
  15408. /**
  15409. * Flatten an array
  15410. */
  15411. exports.flatten = (...args) => {
  15412. const result = [];
  15413. const flat = arr => {
  15414. for (let i = 0; i < arr.length; i++) {
  15415. let ele = arr[i];
  15416. Array.isArray(ele) ? flat(ele) : ele !== void 0 && result.push(ele);
  15417. }
  15418. return result;
  15419. };
  15420. flat(args);
  15421. return result;
  15422. };
  15423. } (utils$f));
  15424. const utils$e = utils$f;
  15425. var stringify$7 = (ast, options = {}) => {
  15426. let stringify = (node, parent = {}) => {
  15427. let invalidBlock = options.escapeInvalid && utils$e.isInvalidBrace(parent);
  15428. let invalidNode = node.invalid === true && options.escapeInvalid === true;
  15429. let output = '';
  15430. if (node.value) {
  15431. if ((invalidBlock || invalidNode) && utils$e.isOpenOrClose(node)) {
  15432. return '\\' + node.value;
  15433. }
  15434. return node.value;
  15435. }
  15436. if (node.value) {
  15437. return node.value;
  15438. }
  15439. if (node.nodes) {
  15440. for (let child of node.nodes) {
  15441. output += stringify(child);
  15442. }
  15443. }
  15444. return output;
  15445. };
  15446. return stringify(ast);
  15447. };
  15448. /*!
  15449. * is-number <https://github.com/jonschlinkert/is-number>
  15450. *
  15451. * Copyright (c) 2014-present, Jon Schlinkert.
  15452. * Released under the MIT License.
  15453. */
  15454. var isNumber$2 = function(num) {
  15455. if (typeof num === 'number') {
  15456. return num - num === 0;
  15457. }
  15458. if (typeof num === 'string' && num.trim() !== '') {
  15459. return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
  15460. }
  15461. return false;
  15462. };
  15463. /*!
  15464. * to-regex-range <https://github.com/micromatch/to-regex-range>
  15465. *
  15466. * Copyright (c) 2015-present, Jon Schlinkert.
  15467. * Released under the MIT License.
  15468. */
  15469. const isNumber$1 = isNumber$2;
  15470. const toRegexRange$1 = (min, max, options) => {
  15471. if (isNumber$1(min) === false) {
  15472. throw new TypeError('toRegexRange: expected the first argument to be a number');
  15473. }
  15474. if (max === void 0 || min === max) {
  15475. return String(min);
  15476. }
  15477. if (isNumber$1(max) === false) {
  15478. throw new TypeError('toRegexRange: expected the second argument to be a number.');
  15479. }
  15480. let opts = { relaxZeros: true, ...options };
  15481. if (typeof opts.strictZeros === 'boolean') {
  15482. opts.relaxZeros = opts.strictZeros === false;
  15483. }
  15484. let relax = String(opts.relaxZeros);
  15485. let shorthand = String(opts.shorthand);
  15486. let capture = String(opts.capture);
  15487. let wrap = String(opts.wrap);
  15488. let cacheKey = min + ':' + max + '=' + relax + shorthand + capture + wrap;
  15489. if (toRegexRange$1.cache.hasOwnProperty(cacheKey)) {
  15490. return toRegexRange$1.cache[cacheKey].result;
  15491. }
  15492. let a = Math.min(min, max);
  15493. let b = Math.max(min, max);
  15494. if (Math.abs(a - b) === 1) {
  15495. let result = min + '|' + max;
  15496. if (opts.capture) {
  15497. return `(${result})`;
  15498. }
  15499. if (opts.wrap === false) {
  15500. return result;
  15501. }
  15502. return `(?:${result})`;
  15503. }
  15504. let isPadded = hasPadding(min) || hasPadding(max);
  15505. let state = { min, max, a, b };
  15506. let positives = [];
  15507. let negatives = [];
  15508. if (isPadded) {
  15509. state.isPadded = isPadded;
  15510. state.maxLen = String(state.max).length;
  15511. }
  15512. if (a < 0) {
  15513. let newMin = b < 0 ? Math.abs(b) : 1;
  15514. negatives = splitToPatterns(newMin, Math.abs(a), state, opts);
  15515. a = state.a = 0;
  15516. }
  15517. if (b >= 0) {
  15518. positives = splitToPatterns(a, b, state, opts);
  15519. }
  15520. state.negatives = negatives;
  15521. state.positives = positives;
  15522. state.result = collatePatterns(negatives, positives);
  15523. if (opts.capture === true) {
  15524. state.result = `(${state.result})`;
  15525. } else if (opts.wrap !== false && (positives.length + negatives.length) > 1) {
  15526. state.result = `(?:${state.result})`;
  15527. }
  15528. toRegexRange$1.cache[cacheKey] = state;
  15529. return state.result;
  15530. };
  15531. function collatePatterns(neg, pos, options) {
  15532. let onlyNegative = filterPatterns(neg, pos, '-', false) || [];
  15533. let onlyPositive = filterPatterns(pos, neg, '', false) || [];
  15534. let intersected = filterPatterns(neg, pos, '-?', true) || [];
  15535. let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
  15536. return subpatterns.join('|');
  15537. }
  15538. function splitToRanges(min, max) {
  15539. let nines = 1;
  15540. let zeros = 1;
  15541. let stop = countNines(min, nines);
  15542. let stops = new Set([max]);
  15543. while (min <= stop && stop <= max) {
  15544. stops.add(stop);
  15545. nines += 1;
  15546. stop = countNines(min, nines);
  15547. }
  15548. stop = countZeros(max + 1, zeros) - 1;
  15549. while (min < stop && stop <= max) {
  15550. stops.add(stop);
  15551. zeros += 1;
  15552. stop = countZeros(max + 1, zeros) - 1;
  15553. }
  15554. stops = [...stops];
  15555. stops.sort(compare);
  15556. return stops;
  15557. }
  15558. /**
  15559. * Convert a range to a regex pattern
  15560. * @param {Number} `start`
  15561. * @param {Number} `stop`
  15562. * @return {String}
  15563. */
  15564. function rangeToPattern(start, stop, options) {
  15565. if (start === stop) {
  15566. return { pattern: start, count: [], digits: 0 };
  15567. }
  15568. let zipped = zip(start, stop);
  15569. let digits = zipped.length;
  15570. let pattern = '';
  15571. let count = 0;
  15572. for (let i = 0; i < digits; i++) {
  15573. let [startDigit, stopDigit] = zipped[i];
  15574. if (startDigit === stopDigit) {
  15575. pattern += startDigit;
  15576. } else if (startDigit !== '0' || stopDigit !== '9') {
  15577. pattern += toCharacterClass(startDigit, stopDigit);
  15578. } else {
  15579. count++;
  15580. }
  15581. }
  15582. if (count) {
  15583. pattern += options.shorthand === true ? '\\d' : '[0-9]';
  15584. }
  15585. return { pattern, count: [count], digits };
  15586. }
  15587. function splitToPatterns(min, max, tok, options) {
  15588. let ranges = splitToRanges(min, max);
  15589. let tokens = [];
  15590. let start = min;
  15591. let prev;
  15592. for (let i = 0; i < ranges.length; i++) {
  15593. let max = ranges[i];
  15594. let obj = rangeToPattern(String(start), String(max), options);
  15595. let zeros = '';
  15596. if (!tok.isPadded && prev && prev.pattern === obj.pattern) {
  15597. if (prev.count.length > 1) {
  15598. prev.count.pop();
  15599. }
  15600. prev.count.push(obj.count[0]);
  15601. prev.string = prev.pattern + toQuantifier(prev.count);
  15602. start = max + 1;
  15603. continue;
  15604. }
  15605. if (tok.isPadded) {
  15606. zeros = padZeros(max, tok, options);
  15607. }
  15608. obj.string = zeros + obj.pattern + toQuantifier(obj.count);
  15609. tokens.push(obj);
  15610. start = max + 1;
  15611. prev = obj;
  15612. }
  15613. return tokens;
  15614. }
  15615. function filterPatterns(arr, comparison, prefix, intersection, options) {
  15616. let result = [];
  15617. for (let ele of arr) {
  15618. let { string } = ele;
  15619. // only push if _both_ are negative...
  15620. if (!intersection && !contains(comparison, 'string', string)) {
  15621. result.push(prefix + string);
  15622. }
  15623. // or _both_ are positive
  15624. if (intersection && contains(comparison, 'string', string)) {
  15625. result.push(prefix + string);
  15626. }
  15627. }
  15628. return result;
  15629. }
  15630. /**
  15631. * Zip strings
  15632. */
  15633. function zip(a, b) {
  15634. let arr = [];
  15635. for (let i = 0; i < a.length; i++) arr.push([a[i], b[i]]);
  15636. return arr;
  15637. }
  15638. function compare(a, b) {
  15639. return a > b ? 1 : b > a ? -1 : 0;
  15640. }
  15641. function contains(arr, key, val) {
  15642. return arr.some(ele => ele[key] === val);
  15643. }
  15644. function countNines(min, len) {
  15645. return Number(String(min).slice(0, -len) + '9'.repeat(len));
  15646. }
  15647. function countZeros(integer, zeros) {
  15648. return integer - (integer % Math.pow(10, zeros));
  15649. }
  15650. function toQuantifier(digits) {
  15651. let [start = 0, stop = ''] = digits;
  15652. if (stop || start > 1) {
  15653. return `{${start + (stop ? ',' + stop : '')}}`;
  15654. }
  15655. return '';
  15656. }
  15657. function toCharacterClass(a, b, options) {
  15658. return `[${a}${(b - a === 1) ? '' : '-'}${b}]`;
  15659. }
  15660. function hasPadding(str) {
  15661. return /^-?(0+)\d/.test(str);
  15662. }
  15663. function padZeros(value, tok, options) {
  15664. if (!tok.isPadded) {
  15665. return value;
  15666. }
  15667. let diff = Math.abs(tok.maxLen - String(value).length);
  15668. let relax = options.relaxZeros !== false;
  15669. switch (diff) {
  15670. case 0:
  15671. return '';
  15672. case 1:
  15673. return relax ? '0?' : '0';
  15674. case 2:
  15675. return relax ? '0{0,2}' : '00';
  15676. default: {
  15677. return relax ? `0{0,${diff}}` : `0{${diff}}`;
  15678. }
  15679. }
  15680. }
  15681. /**
  15682. * Cache
  15683. */
  15684. toRegexRange$1.cache = {};
  15685. toRegexRange$1.clearCache = () => (toRegexRange$1.cache = {});
  15686. /**
  15687. * Expose `toRegexRange`
  15688. */
  15689. var toRegexRange_1 = toRegexRange$1;
  15690. /*!
  15691. * fill-range <https://github.com/jonschlinkert/fill-range>
  15692. *
  15693. * Copyright (c) 2014-present, Jon Schlinkert.
  15694. * Licensed under the MIT License.
  15695. */
  15696. const util$1 = require$$0$6;
  15697. const toRegexRange = toRegexRange_1;
  15698. const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  15699. const transform = toNumber => {
  15700. return value => toNumber === true ? Number(value) : String(value);
  15701. };
  15702. const isValidValue = value => {
  15703. return typeof value === 'number' || (typeof value === 'string' && value !== '');
  15704. };
  15705. const isNumber = num => Number.isInteger(+num);
  15706. const zeros = input => {
  15707. let value = `${input}`;
  15708. let index = -1;
  15709. if (value[0] === '-') value = value.slice(1);
  15710. if (value === '0') return false;
  15711. while (value[++index] === '0');
  15712. return index > 0;
  15713. };
  15714. const stringify$6 = (start, end, options) => {
  15715. if (typeof start === 'string' || typeof end === 'string') {
  15716. return true;
  15717. }
  15718. return options.stringify === true;
  15719. };
  15720. const pad = (input, maxLength, toNumber) => {
  15721. if (maxLength > 0) {
  15722. let dash = input[0] === '-' ? '-' : '';
  15723. if (dash) input = input.slice(1);
  15724. input = (dash + input.padStart(dash ? maxLength - 1 : maxLength, '0'));
  15725. }
  15726. if (toNumber === false) {
  15727. return String(input);
  15728. }
  15729. return input;
  15730. };
  15731. const toMaxLen = (input, maxLength) => {
  15732. let negative = input[0] === '-' ? '-' : '';
  15733. if (negative) {
  15734. input = input.slice(1);
  15735. maxLength--;
  15736. }
  15737. while (input.length < maxLength) input = '0' + input;
  15738. return negative ? ('-' + input) : input;
  15739. };
  15740. const toSequence = (parts, options) => {
  15741. parts.negatives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  15742. parts.positives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  15743. let prefix = options.capture ? '' : '?:';
  15744. let positives = '';
  15745. let negatives = '';
  15746. let result;
  15747. if (parts.positives.length) {
  15748. positives = parts.positives.join('|');
  15749. }
  15750. if (parts.negatives.length) {
  15751. negatives = `-(${prefix}${parts.negatives.join('|')})`;
  15752. }
  15753. if (positives && negatives) {
  15754. result = `${positives}|${negatives}`;
  15755. } else {
  15756. result = positives || negatives;
  15757. }
  15758. if (options.wrap) {
  15759. return `(${prefix}${result})`;
  15760. }
  15761. return result;
  15762. };
  15763. const toRange = (a, b, isNumbers, options) => {
  15764. if (isNumbers) {
  15765. return toRegexRange(a, b, { wrap: false, ...options });
  15766. }
  15767. let start = String.fromCharCode(a);
  15768. if (a === b) return start;
  15769. let stop = String.fromCharCode(b);
  15770. return `[${start}-${stop}]`;
  15771. };
  15772. const toRegex = (start, end, options) => {
  15773. if (Array.isArray(start)) {
  15774. let wrap = options.wrap === true;
  15775. let prefix = options.capture ? '' : '?:';
  15776. return wrap ? `(${prefix}${start.join('|')})` : start.join('|');
  15777. }
  15778. return toRegexRange(start, end, options);
  15779. };
  15780. const rangeError = (...args) => {
  15781. return new RangeError('Invalid range arguments: ' + util$1.inspect(...args));
  15782. };
  15783. const invalidRange = (start, end, options) => {
  15784. if (options.strictRanges === true) throw rangeError([start, end]);
  15785. return [];
  15786. };
  15787. const invalidStep = (step, options) => {
  15788. if (options.strictRanges === true) {
  15789. throw new TypeError(`Expected step "${step}" to be a number`);
  15790. }
  15791. return [];
  15792. };
  15793. const fillNumbers = (start, end, step = 1, options = {}) => {
  15794. let a = Number(start);
  15795. let b = Number(end);
  15796. if (!Number.isInteger(a) || !Number.isInteger(b)) {
  15797. if (options.strictRanges === true) throw rangeError([start, end]);
  15798. return [];
  15799. }
  15800. // fix negative zero
  15801. if (a === 0) a = 0;
  15802. if (b === 0) b = 0;
  15803. let descending = a > b;
  15804. let startString = String(start);
  15805. let endString = String(end);
  15806. let stepString = String(step);
  15807. step = Math.max(Math.abs(step), 1);
  15808. let padded = zeros(startString) || zeros(endString) || zeros(stepString);
  15809. let maxLen = padded ? Math.max(startString.length, endString.length, stepString.length) : 0;
  15810. let toNumber = padded === false && stringify$6(start, end, options) === false;
  15811. let format = options.transform || transform(toNumber);
  15812. if (options.toRegex && step === 1) {
  15813. return toRange(toMaxLen(start, maxLen), toMaxLen(end, maxLen), true, options);
  15814. }
  15815. let parts = { negatives: [], positives: [] };
  15816. let push = num => parts[num < 0 ? 'negatives' : 'positives'].push(Math.abs(num));
  15817. let range = [];
  15818. let index = 0;
  15819. while (descending ? a >= b : a <= b) {
  15820. if (options.toRegex === true && step > 1) {
  15821. push(a);
  15822. } else {
  15823. range.push(pad(format(a, index), maxLen, toNumber));
  15824. }
  15825. a = descending ? a - step : a + step;
  15826. index++;
  15827. }
  15828. if (options.toRegex === true) {
  15829. return step > 1
  15830. ? toSequence(parts, options)
  15831. : toRegex(range, null, { wrap: false, ...options });
  15832. }
  15833. return range;
  15834. };
  15835. const fillLetters = (start, end, step = 1, options = {}) => {
  15836. if ((!isNumber(start) && start.length > 1) || (!isNumber(end) && end.length > 1)) {
  15837. return invalidRange(start, end, options);
  15838. }
  15839. let format = options.transform || (val => String.fromCharCode(val));
  15840. let a = `${start}`.charCodeAt(0);
  15841. let b = `${end}`.charCodeAt(0);
  15842. let descending = a > b;
  15843. let min = Math.min(a, b);
  15844. let max = Math.max(a, b);
  15845. if (options.toRegex && step === 1) {
  15846. return toRange(min, max, false, options);
  15847. }
  15848. let range = [];
  15849. let index = 0;
  15850. while (descending ? a >= b : a <= b) {
  15851. range.push(format(a, index));
  15852. a = descending ? a - step : a + step;
  15853. index++;
  15854. }
  15855. if (options.toRegex === true) {
  15856. return toRegex(range, null, { wrap: false, options });
  15857. }
  15858. return range;
  15859. };
  15860. const fill$2 = (start, end, step, options = {}) => {
  15861. if (end == null && isValidValue(start)) {
  15862. return [start];
  15863. }
  15864. if (!isValidValue(start) || !isValidValue(end)) {
  15865. return invalidRange(start, end, options);
  15866. }
  15867. if (typeof step === 'function') {
  15868. return fill$2(start, end, 1, { transform: step });
  15869. }
  15870. if (isObject(step)) {
  15871. return fill$2(start, end, 0, step);
  15872. }
  15873. let opts = { ...options };
  15874. if (opts.capture === true) opts.wrap = true;
  15875. step = step || opts.step || 1;
  15876. if (!isNumber(step)) {
  15877. if (step != null && !isObject(step)) return invalidStep(step, opts);
  15878. return fill$2(start, end, 1, step);
  15879. }
  15880. if (isNumber(start) && isNumber(end)) {
  15881. return fillNumbers(start, end, step, opts);
  15882. }
  15883. return fillLetters(start, end, Math.max(Math.abs(step), 1), opts);
  15884. };
  15885. var fillRange = fill$2;
  15886. const fill$1 = fillRange;
  15887. const utils$d = utils$f;
  15888. const compile$1 = (ast, options = {}) => {
  15889. let walk = (node, parent = {}) => {
  15890. let invalidBlock = utils$d.isInvalidBrace(parent);
  15891. let invalidNode = node.invalid === true && options.escapeInvalid === true;
  15892. let invalid = invalidBlock === true || invalidNode === true;
  15893. let prefix = options.escapeInvalid === true ? '\\' : '';
  15894. let output = '';
  15895. if (node.isOpen === true) {
  15896. return prefix + node.value;
  15897. }
  15898. if (node.isClose === true) {
  15899. return prefix + node.value;
  15900. }
  15901. if (node.type === 'open') {
  15902. return invalid ? (prefix + node.value) : '(';
  15903. }
  15904. if (node.type === 'close') {
  15905. return invalid ? (prefix + node.value) : ')';
  15906. }
  15907. if (node.type === 'comma') {
  15908. return node.prev.type === 'comma' ? '' : (invalid ? node.value : '|');
  15909. }
  15910. if (node.value) {
  15911. return node.value;
  15912. }
  15913. if (node.nodes && node.ranges > 0) {
  15914. let args = utils$d.reduce(node.nodes);
  15915. let range = fill$1(...args, { ...options, wrap: false, toRegex: true });
  15916. if (range.length !== 0) {
  15917. return args.length > 1 && range.length > 1 ? `(${range})` : range;
  15918. }
  15919. }
  15920. if (node.nodes) {
  15921. for (let child of node.nodes) {
  15922. output += walk(child, node);
  15923. }
  15924. }
  15925. return output;
  15926. };
  15927. return walk(ast);
  15928. };
  15929. var compile_1 = compile$1;
  15930. const fill = fillRange;
  15931. const stringify$5 = stringify$7;
  15932. const utils$c = utils$f;
  15933. const append$1 = (queue = '', stash = '', enclose = false) => {
  15934. let result = [];
  15935. queue = [].concat(queue);
  15936. stash = [].concat(stash);
  15937. if (!stash.length) return queue;
  15938. if (!queue.length) {
  15939. return enclose ? utils$c.flatten(stash).map(ele => `{${ele}}`) : stash;
  15940. }
  15941. for (let item of queue) {
  15942. if (Array.isArray(item)) {
  15943. for (let value of item) {
  15944. result.push(append$1(value, stash, enclose));
  15945. }
  15946. } else {
  15947. for (let ele of stash) {
  15948. if (enclose === true && typeof ele === 'string') ele = `{${ele}}`;
  15949. result.push(Array.isArray(ele) ? append$1(item, ele, enclose) : (item + ele));
  15950. }
  15951. }
  15952. }
  15953. return utils$c.flatten(result);
  15954. };
  15955. const expand$2 = (ast, options = {}) => {
  15956. let rangeLimit = options.rangeLimit === void 0 ? 1000 : options.rangeLimit;
  15957. let walk = (node, parent = {}) => {
  15958. node.queue = [];
  15959. let p = parent;
  15960. let q = parent.queue;
  15961. while (p.type !== 'brace' && p.type !== 'root' && p.parent) {
  15962. p = p.parent;
  15963. q = p.queue;
  15964. }
  15965. if (node.invalid || node.dollar) {
  15966. q.push(append$1(q.pop(), stringify$5(node, options)));
  15967. return;
  15968. }
  15969. if (node.type === 'brace' && node.invalid !== true && node.nodes.length === 2) {
  15970. q.push(append$1(q.pop(), ['{}']));
  15971. return;
  15972. }
  15973. if (node.nodes && node.ranges > 0) {
  15974. let args = utils$c.reduce(node.nodes);
  15975. if (utils$c.exceedsLimit(...args, options.step, rangeLimit)) {
  15976. throw new RangeError('expanded array length exceeds range limit. Use options.rangeLimit to increase or disable the limit.');
  15977. }
  15978. let range = fill(...args, options);
  15979. if (range.length === 0) {
  15980. range = stringify$5(node, options);
  15981. }
  15982. q.push(append$1(q.pop(), range));
  15983. node.nodes = [];
  15984. return;
  15985. }
  15986. let enclose = utils$c.encloseBrace(node);
  15987. let queue = node.queue;
  15988. let block = node;
  15989. while (block.type !== 'brace' && block.type !== 'root' && block.parent) {
  15990. block = block.parent;
  15991. queue = block.queue;
  15992. }
  15993. for (let i = 0; i < node.nodes.length; i++) {
  15994. let child = node.nodes[i];
  15995. if (child.type === 'comma' && node.type === 'brace') {
  15996. if (i === 1) queue.push('');
  15997. queue.push('');
  15998. continue;
  15999. }
  16000. if (child.type === 'close') {
  16001. q.push(append$1(q.pop(), queue, enclose));
  16002. continue;
  16003. }
  16004. if (child.value && child.type !== 'open') {
  16005. queue.push(append$1(queue.pop(), child.value));
  16006. continue;
  16007. }
  16008. if (child.nodes) {
  16009. walk(child, node);
  16010. }
  16011. }
  16012. return queue;
  16013. };
  16014. return utils$c.flatten(walk(ast));
  16015. };
  16016. var expand_1$1 = expand$2;
  16017. var constants$3 = {
  16018. MAX_LENGTH: 1024 * 64,
  16019. // Digits
  16020. CHAR_0: '0', /* 0 */
  16021. CHAR_9: '9', /* 9 */
  16022. // Alphabet chars.
  16023. CHAR_UPPERCASE_A: 'A', /* A */
  16024. CHAR_LOWERCASE_A: 'a', /* a */
  16025. CHAR_UPPERCASE_Z: 'Z', /* Z */
  16026. CHAR_LOWERCASE_Z: 'z', /* z */
  16027. CHAR_LEFT_PARENTHESES: '(', /* ( */
  16028. CHAR_RIGHT_PARENTHESES: ')', /* ) */
  16029. CHAR_ASTERISK: '*', /* * */
  16030. // Non-alphabetic chars.
  16031. CHAR_AMPERSAND: '&', /* & */
  16032. CHAR_AT: '@', /* @ */
  16033. CHAR_BACKSLASH: '\\', /* \ */
  16034. CHAR_BACKTICK: '`', /* ` */
  16035. CHAR_CARRIAGE_RETURN: '\r', /* \r */
  16036. CHAR_CIRCUMFLEX_ACCENT: '^', /* ^ */
  16037. CHAR_COLON: ':', /* : */
  16038. CHAR_COMMA: ',', /* , */
  16039. CHAR_DOLLAR: '$', /* . */
  16040. CHAR_DOT: '.', /* . */
  16041. CHAR_DOUBLE_QUOTE: '"', /* " */
  16042. CHAR_EQUAL: '=', /* = */
  16043. CHAR_EXCLAMATION_MARK: '!', /* ! */
  16044. CHAR_FORM_FEED: '\f', /* \f */
  16045. CHAR_FORWARD_SLASH: '/', /* / */
  16046. CHAR_HASH: '#', /* # */
  16047. CHAR_HYPHEN_MINUS: '-', /* - */
  16048. CHAR_LEFT_ANGLE_BRACKET: '<', /* < */
  16049. CHAR_LEFT_CURLY_BRACE: '{', /* { */
  16050. CHAR_LEFT_SQUARE_BRACKET: '[', /* [ */
  16051. CHAR_LINE_FEED: '\n', /* \n */
  16052. CHAR_NO_BREAK_SPACE: '\u00A0', /* \u00A0 */
  16053. CHAR_PERCENT: '%', /* % */
  16054. CHAR_PLUS: '+', /* + */
  16055. CHAR_QUESTION_MARK: '?', /* ? */
  16056. CHAR_RIGHT_ANGLE_BRACKET: '>', /* > */
  16057. CHAR_RIGHT_CURLY_BRACE: '}', /* } */
  16058. CHAR_RIGHT_SQUARE_BRACKET: ']', /* ] */
  16059. CHAR_SEMICOLON: ';', /* ; */
  16060. CHAR_SINGLE_QUOTE: '\'', /* ' */
  16061. CHAR_SPACE: ' ', /* */
  16062. CHAR_TAB: '\t', /* \t */
  16063. CHAR_UNDERSCORE: '_', /* _ */
  16064. CHAR_VERTICAL_LINE: '|', /* | */
  16065. CHAR_ZERO_WIDTH_NOBREAK_SPACE: '\uFEFF' /* \uFEFF */
  16066. };
  16067. const stringify$4 = stringify$7;
  16068. /**
  16069. * Constants
  16070. */
  16071. const {
  16072. MAX_LENGTH,
  16073. CHAR_BACKSLASH, /* \ */
  16074. CHAR_BACKTICK, /* ` */
  16075. CHAR_COMMA, /* , */
  16076. CHAR_DOT, /* . */
  16077. CHAR_LEFT_PARENTHESES, /* ( */
  16078. CHAR_RIGHT_PARENTHESES, /* ) */
  16079. CHAR_LEFT_CURLY_BRACE, /* { */
  16080. CHAR_RIGHT_CURLY_BRACE, /* } */
  16081. CHAR_LEFT_SQUARE_BRACKET, /* [ */
  16082. CHAR_RIGHT_SQUARE_BRACKET, /* ] */
  16083. CHAR_DOUBLE_QUOTE, /* " */
  16084. CHAR_SINGLE_QUOTE, /* ' */
  16085. CHAR_NO_BREAK_SPACE,
  16086. CHAR_ZERO_WIDTH_NOBREAK_SPACE
  16087. } = constants$3;
  16088. /**
  16089. * parse
  16090. */
  16091. const parse$d = (input, options = {}) => {
  16092. if (typeof input !== 'string') {
  16093. throw new TypeError('Expected a string');
  16094. }
  16095. let opts = options || {};
  16096. let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  16097. if (input.length > max) {
  16098. throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
  16099. }
  16100. let ast = { type: 'root', input, nodes: [] };
  16101. let stack = [ast];
  16102. let block = ast;
  16103. let prev = ast;
  16104. let brackets = 0;
  16105. let length = input.length;
  16106. let index = 0;
  16107. let depth = 0;
  16108. let value;
  16109. /**
  16110. * Helpers
  16111. */
  16112. const advance = () => input[index++];
  16113. const push = node => {
  16114. if (node.type === 'text' && prev.type === 'dot') {
  16115. prev.type = 'text';
  16116. }
  16117. if (prev && prev.type === 'text' && node.type === 'text') {
  16118. prev.value += node.value;
  16119. return;
  16120. }
  16121. block.nodes.push(node);
  16122. node.parent = block;
  16123. node.prev = prev;
  16124. prev = node;
  16125. return node;
  16126. };
  16127. push({ type: 'bos' });
  16128. while (index < length) {
  16129. block = stack[stack.length - 1];
  16130. value = advance();
  16131. /**
  16132. * Invalid chars
  16133. */
  16134. if (value === CHAR_ZERO_WIDTH_NOBREAK_SPACE || value === CHAR_NO_BREAK_SPACE) {
  16135. continue;
  16136. }
  16137. /**
  16138. * Escaped chars
  16139. */
  16140. if (value === CHAR_BACKSLASH) {
  16141. push({ type: 'text', value: (options.keepEscaping ? value : '') + advance() });
  16142. continue;
  16143. }
  16144. /**
  16145. * Right square bracket (literal): ']'
  16146. */
  16147. if (value === CHAR_RIGHT_SQUARE_BRACKET) {
  16148. push({ type: 'text', value: '\\' + value });
  16149. continue;
  16150. }
  16151. /**
  16152. * Left square bracket: '['
  16153. */
  16154. if (value === CHAR_LEFT_SQUARE_BRACKET) {
  16155. brackets++;
  16156. let next;
  16157. while (index < length && (next = advance())) {
  16158. value += next;
  16159. if (next === CHAR_LEFT_SQUARE_BRACKET) {
  16160. brackets++;
  16161. continue;
  16162. }
  16163. if (next === CHAR_BACKSLASH) {
  16164. value += advance();
  16165. continue;
  16166. }
  16167. if (next === CHAR_RIGHT_SQUARE_BRACKET) {
  16168. brackets--;
  16169. if (brackets === 0) {
  16170. break;
  16171. }
  16172. }
  16173. }
  16174. push({ type: 'text', value });
  16175. continue;
  16176. }
  16177. /**
  16178. * Parentheses
  16179. */
  16180. if (value === CHAR_LEFT_PARENTHESES) {
  16181. block = push({ type: 'paren', nodes: [] });
  16182. stack.push(block);
  16183. push({ type: 'text', value });
  16184. continue;
  16185. }
  16186. if (value === CHAR_RIGHT_PARENTHESES) {
  16187. if (block.type !== 'paren') {
  16188. push({ type: 'text', value });
  16189. continue;
  16190. }
  16191. block = stack.pop();
  16192. push({ type: 'text', value });
  16193. block = stack[stack.length - 1];
  16194. continue;
  16195. }
  16196. /**
  16197. * Quotes: '|"|`
  16198. */
  16199. if (value === CHAR_DOUBLE_QUOTE || value === CHAR_SINGLE_QUOTE || value === CHAR_BACKTICK) {
  16200. let open = value;
  16201. let next;
  16202. if (options.keepQuotes !== true) {
  16203. value = '';
  16204. }
  16205. while (index < length && (next = advance())) {
  16206. if (next === CHAR_BACKSLASH) {
  16207. value += next + advance();
  16208. continue;
  16209. }
  16210. if (next === open) {
  16211. if (options.keepQuotes === true) value += next;
  16212. break;
  16213. }
  16214. value += next;
  16215. }
  16216. push({ type: 'text', value });
  16217. continue;
  16218. }
  16219. /**
  16220. * Left curly brace: '{'
  16221. */
  16222. if (value === CHAR_LEFT_CURLY_BRACE) {
  16223. depth++;
  16224. let dollar = prev.value && prev.value.slice(-1) === '$' || block.dollar === true;
  16225. let brace = {
  16226. type: 'brace',
  16227. open: true,
  16228. close: false,
  16229. dollar,
  16230. depth,
  16231. commas: 0,
  16232. ranges: 0,
  16233. nodes: []
  16234. };
  16235. block = push(brace);
  16236. stack.push(block);
  16237. push({ type: 'open', value });
  16238. continue;
  16239. }
  16240. /**
  16241. * Right curly brace: '}'
  16242. */
  16243. if (value === CHAR_RIGHT_CURLY_BRACE) {
  16244. if (block.type !== 'brace') {
  16245. push({ type: 'text', value });
  16246. continue;
  16247. }
  16248. let type = 'close';
  16249. block = stack.pop();
  16250. block.close = true;
  16251. push({ type, value });
  16252. depth--;
  16253. block = stack[stack.length - 1];
  16254. continue;
  16255. }
  16256. /**
  16257. * Comma: ','
  16258. */
  16259. if (value === CHAR_COMMA && depth > 0) {
  16260. if (block.ranges > 0) {
  16261. block.ranges = 0;
  16262. let open = block.nodes.shift();
  16263. block.nodes = [open, { type: 'text', value: stringify$4(block) }];
  16264. }
  16265. push({ type: 'comma', value });
  16266. block.commas++;
  16267. continue;
  16268. }
  16269. /**
  16270. * Dot: '.'
  16271. */
  16272. if (value === CHAR_DOT && depth > 0 && block.commas === 0) {
  16273. let siblings = block.nodes;
  16274. if (depth === 0 || siblings.length === 0) {
  16275. push({ type: 'text', value });
  16276. continue;
  16277. }
  16278. if (prev.type === 'dot') {
  16279. block.range = [];
  16280. prev.value += value;
  16281. prev.type = 'range';
  16282. if (block.nodes.length !== 3 && block.nodes.length !== 5) {
  16283. block.invalid = true;
  16284. block.ranges = 0;
  16285. prev.type = 'text';
  16286. continue;
  16287. }
  16288. block.ranges++;
  16289. block.args = [];
  16290. continue;
  16291. }
  16292. if (prev.type === 'range') {
  16293. siblings.pop();
  16294. let before = siblings[siblings.length - 1];
  16295. before.value += prev.value + value;
  16296. prev = before;
  16297. block.ranges--;
  16298. continue;
  16299. }
  16300. push({ type: 'dot', value });
  16301. continue;
  16302. }
  16303. /**
  16304. * Text
  16305. */
  16306. push({ type: 'text', value });
  16307. }
  16308. // Mark imbalanced braces and brackets as invalid
  16309. do {
  16310. block = stack.pop();
  16311. if (block.type !== 'root') {
  16312. block.nodes.forEach(node => {
  16313. if (!node.nodes) {
  16314. if (node.type === 'open') node.isOpen = true;
  16315. if (node.type === 'close') node.isClose = true;
  16316. if (!node.nodes) node.type = 'text';
  16317. node.invalid = true;
  16318. }
  16319. });
  16320. // get the location of the block on parent.nodes (block's siblings)
  16321. let parent = stack[stack.length - 1];
  16322. let index = parent.nodes.indexOf(block);
  16323. // replace the (invalid) block with it's nodes
  16324. parent.nodes.splice(index, 1, ...block.nodes);
  16325. }
  16326. } while (stack.length > 0);
  16327. push({ type: 'eos' });
  16328. return ast;
  16329. };
  16330. var parse_1$2 = parse$d;
  16331. const stringify$3 = stringify$7;
  16332. const compile = compile_1;
  16333. const expand$1 = expand_1$1;
  16334. const parse$c = parse_1$2;
  16335. /**
  16336. * Expand the given pattern or create a regex-compatible string.
  16337. *
  16338. * ```js
  16339. * const braces = require('braces');
  16340. * console.log(braces('{a,b,c}', { compile: true })); //=> ['(a|b|c)']
  16341. * console.log(braces('{a,b,c}')); //=> ['a', 'b', 'c']
  16342. * ```
  16343. * @param {String} `str`
  16344. * @param {Object} `options`
  16345. * @return {String}
  16346. * @api public
  16347. */
  16348. const braces$2 = (input, options = {}) => {
  16349. let output = [];
  16350. if (Array.isArray(input)) {
  16351. for (let pattern of input) {
  16352. let result = braces$2.create(pattern, options);
  16353. if (Array.isArray(result)) {
  16354. output.push(...result);
  16355. } else {
  16356. output.push(result);
  16357. }
  16358. }
  16359. } else {
  16360. output = [].concat(braces$2.create(input, options));
  16361. }
  16362. if (options && options.expand === true && options.nodupes === true) {
  16363. output = [...new Set(output)];
  16364. }
  16365. return output;
  16366. };
  16367. /**
  16368. * Parse the given `str` with the given `options`.
  16369. *
  16370. * ```js
  16371. * // braces.parse(pattern, [, options]);
  16372. * const ast = braces.parse('a/{b,c}/d');
  16373. * console.log(ast);
  16374. * ```
  16375. * @param {String} pattern Brace pattern to parse
  16376. * @param {Object} options
  16377. * @return {Object} Returns an AST
  16378. * @api public
  16379. */
  16380. braces$2.parse = (input, options = {}) => parse$c(input, options);
  16381. /**
  16382. * Creates a braces string from an AST, or an AST node.
  16383. *
  16384. * ```js
  16385. * const braces = require('braces');
  16386. * let ast = braces.parse('foo/{a,b}/bar');
  16387. * console.log(stringify(ast.nodes[2])); //=> '{a,b}'
  16388. * ```
  16389. * @param {String} `input` Brace pattern or AST.
  16390. * @param {Object} `options`
  16391. * @return {Array} Returns an array of expanded values.
  16392. * @api public
  16393. */
  16394. braces$2.stringify = (input, options = {}) => {
  16395. if (typeof input === 'string') {
  16396. return stringify$3(braces$2.parse(input, options), options);
  16397. }
  16398. return stringify$3(input, options);
  16399. };
  16400. /**
  16401. * Compiles a brace pattern into a regex-compatible, optimized string.
  16402. * This method is called by the main [braces](#braces) function by default.
  16403. *
  16404. * ```js
  16405. * const braces = require('braces');
  16406. * console.log(braces.compile('a/{b,c}/d'));
  16407. * //=> ['a/(b|c)/d']
  16408. * ```
  16409. * @param {String} `input` Brace pattern or AST.
  16410. * @param {Object} `options`
  16411. * @return {Array} Returns an array of expanded values.
  16412. * @api public
  16413. */
  16414. braces$2.compile = (input, options = {}) => {
  16415. if (typeof input === 'string') {
  16416. input = braces$2.parse(input, options);
  16417. }
  16418. return compile(input, options);
  16419. };
  16420. /**
  16421. * Expands a brace pattern into an array. This method is called by the
  16422. * main [braces](#braces) function when `options.expand` is true. Before
  16423. * using this method it's recommended that you read the [performance notes](#performance))
  16424. * and advantages of using [.compile](#compile) instead.
  16425. *
  16426. * ```js
  16427. * const braces = require('braces');
  16428. * console.log(braces.expand('a/{b,c}/d'));
  16429. * //=> ['a/b/d', 'a/c/d'];
  16430. * ```
  16431. * @param {String} `pattern` Brace pattern
  16432. * @param {Object} `options`
  16433. * @return {Array} Returns an array of expanded values.
  16434. * @api public
  16435. */
  16436. braces$2.expand = (input, options = {}) => {
  16437. if (typeof input === 'string') {
  16438. input = braces$2.parse(input, options);
  16439. }
  16440. let result = expand$1(input, options);
  16441. // filter out empty strings if specified
  16442. if (options.noempty === true) {
  16443. result = result.filter(Boolean);
  16444. }
  16445. // filter out duplicates if specified
  16446. if (options.nodupes === true) {
  16447. result = [...new Set(result)];
  16448. }
  16449. return result;
  16450. };
  16451. /**
  16452. * Processes a brace pattern and returns either an expanded array
  16453. * (if `options.expand` is true), a highly optimized regex-compatible string.
  16454. * This method is called by the main [braces](#braces) function.
  16455. *
  16456. * ```js
  16457. * const braces = require('braces');
  16458. * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
  16459. * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
  16460. * ```
  16461. * @param {String} `pattern` Brace pattern
  16462. * @param {Object} `options`
  16463. * @return {Array} Returns an array of expanded values.
  16464. * @api public
  16465. */
  16466. braces$2.create = (input, options = {}) => {
  16467. if (input === '' || input.length < 3) {
  16468. return [input];
  16469. }
  16470. return options.expand !== true
  16471. ? braces$2.compile(input, options)
  16472. : braces$2.expand(input, options);
  16473. };
  16474. /**
  16475. * Expose "braces"
  16476. */
  16477. var braces_1 = braces$2;
  16478. const util = require$$0$6;
  16479. const braces$1 = braces_1;
  16480. const picomatch$2 = picomatch$3;
  16481. const utils$b = utils$k;
  16482. const isEmptyString = val => val === '' || val === './';
  16483. /**
  16484. * Returns an array of strings that match one or more glob patterns.
  16485. *
  16486. * ```js
  16487. * const mm = require('micromatch');
  16488. * // mm(list, patterns[, options]);
  16489. *
  16490. * console.log(mm(['a.js', 'a.txt'], ['*.js']));
  16491. * //=> [ 'a.js' ]
  16492. * ```
  16493. * @param {String|Array<string>} `list` List of strings to match.
  16494. * @param {String|Array<string>} `patterns` One or more glob patterns to use for matching.
  16495. * @param {Object} `options` See available [options](#options)
  16496. * @return {Array} Returns an array of matches
  16497. * @summary false
  16498. * @api public
  16499. */
  16500. const micromatch$1 = (list, patterns, options) => {
  16501. patterns = [].concat(patterns);
  16502. list = [].concat(list);
  16503. let omit = new Set();
  16504. let keep = new Set();
  16505. let items = new Set();
  16506. let negatives = 0;
  16507. let onResult = state => {
  16508. items.add(state.output);
  16509. if (options && options.onResult) {
  16510. options.onResult(state);
  16511. }
  16512. };
  16513. for (let i = 0; i < patterns.length; i++) {
  16514. let isMatch = picomatch$2(String(patterns[i]), { ...options, onResult }, true);
  16515. let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
  16516. if (negated) negatives++;
  16517. for (let item of list) {
  16518. let matched = isMatch(item, true);
  16519. let match = negated ? !matched.isMatch : matched.isMatch;
  16520. if (!match) continue;
  16521. if (negated) {
  16522. omit.add(matched.output);
  16523. } else {
  16524. omit.delete(matched.output);
  16525. keep.add(matched.output);
  16526. }
  16527. }
  16528. }
  16529. let result = negatives === patterns.length ? [...items] : [...keep];
  16530. let matches = result.filter(item => !omit.has(item));
  16531. if (options && matches.length === 0) {
  16532. if (options.failglob === true) {
  16533. throw new Error(`No matches found for "${patterns.join(', ')}"`);
  16534. }
  16535. if (options.nonull === true || options.nullglob === true) {
  16536. return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
  16537. }
  16538. }
  16539. return matches;
  16540. };
  16541. /**
  16542. * Backwards compatibility
  16543. */
  16544. micromatch$1.match = micromatch$1;
  16545. /**
  16546. * Returns a matcher function from the given glob `pattern` and `options`.
  16547. * The returned function takes a string to match as its only argument and returns
  16548. * true if the string is a match.
  16549. *
  16550. * ```js
  16551. * const mm = require('micromatch');
  16552. * // mm.matcher(pattern[, options]);
  16553. *
  16554. * const isMatch = mm.matcher('*.!(*a)');
  16555. * console.log(isMatch('a.a')); //=> false
  16556. * console.log(isMatch('a.b')); //=> true
  16557. * ```
  16558. * @param {String} `pattern` Glob pattern
  16559. * @param {Object} `options`
  16560. * @return {Function} Returns a matcher function.
  16561. * @api public
  16562. */
  16563. micromatch$1.matcher = (pattern, options) => picomatch$2(pattern, options);
  16564. /**
  16565. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  16566. *
  16567. * ```js
  16568. * const mm = require('micromatch');
  16569. * // mm.isMatch(string, patterns[, options]);
  16570. *
  16571. * console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
  16572. * console.log(mm.isMatch('a.a', 'b.*')); //=> false
  16573. * ```
  16574. * @param {String} `str` The string to test.
  16575. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  16576. * @param {Object} `[options]` See available [options](#options).
  16577. * @return {Boolean} Returns true if any patterns match `str`
  16578. * @api public
  16579. */
  16580. micromatch$1.isMatch = (str, patterns, options) => picomatch$2(patterns, options)(str);
  16581. /**
  16582. * Backwards compatibility
  16583. */
  16584. micromatch$1.any = micromatch$1.isMatch;
  16585. /**
  16586. * Returns a list of strings that _**do not match any**_ of the given `patterns`.
  16587. *
  16588. * ```js
  16589. * const mm = require('micromatch');
  16590. * // mm.not(list, patterns[, options]);
  16591. *
  16592. * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  16593. * //=> ['b.b', 'c.c']
  16594. * ```
  16595. * @param {Array} `list` Array of strings to match.
  16596. * @param {String|Array} `patterns` One or more glob pattern to use for matching.
  16597. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16598. * @return {Array} Returns an array of strings that **do not match** the given patterns.
  16599. * @api public
  16600. */
  16601. micromatch$1.not = (list, patterns, options = {}) => {
  16602. patterns = [].concat(patterns).map(String);
  16603. let result = new Set();
  16604. let items = [];
  16605. let onResult = state => {
  16606. if (options.onResult) options.onResult(state);
  16607. items.push(state.output);
  16608. };
  16609. let matches = new Set(micromatch$1(list, patterns, { ...options, onResult }));
  16610. for (let item of items) {
  16611. if (!matches.has(item)) {
  16612. result.add(item);
  16613. }
  16614. }
  16615. return [...result];
  16616. };
  16617. /**
  16618. * Returns true if the given `string` contains the given pattern. Similar
  16619. * to [.isMatch](#isMatch) but the pattern can match any part of the string.
  16620. *
  16621. * ```js
  16622. * var mm = require('micromatch');
  16623. * // mm.contains(string, pattern[, options]);
  16624. *
  16625. * console.log(mm.contains('aa/bb/cc', '*b'));
  16626. * //=> true
  16627. * console.log(mm.contains('aa/bb/cc', '*d'));
  16628. * //=> false
  16629. * ```
  16630. * @param {String} `str` The string to match.
  16631. * @param {String|Array} `patterns` Glob pattern to use for matching.
  16632. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16633. * @return {Boolean} Returns true if any of the patterns matches any part of `str`.
  16634. * @api public
  16635. */
  16636. micromatch$1.contains = (str, pattern, options) => {
  16637. if (typeof str !== 'string') {
  16638. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  16639. }
  16640. if (Array.isArray(pattern)) {
  16641. return pattern.some(p => micromatch$1.contains(str, p, options));
  16642. }
  16643. if (typeof pattern === 'string') {
  16644. if (isEmptyString(str) || isEmptyString(pattern)) {
  16645. return false;
  16646. }
  16647. if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
  16648. return true;
  16649. }
  16650. }
  16651. return micromatch$1.isMatch(str, pattern, { ...options, contains: true });
  16652. };
  16653. /**
  16654. * Filter the keys of the given object with the given `glob` pattern
  16655. * and `options`. Does not attempt to match nested keys. If you need this feature,
  16656. * use [glob-object][] instead.
  16657. *
  16658. * ```js
  16659. * const mm = require('micromatch');
  16660. * // mm.matchKeys(object, patterns[, options]);
  16661. *
  16662. * const obj = { aa: 'a', ab: 'b', ac: 'c' };
  16663. * console.log(mm.matchKeys(obj, '*b'));
  16664. * //=> { ab: 'b' }
  16665. * ```
  16666. * @param {Object} `object` The object with keys to filter.
  16667. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  16668. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16669. * @return {Object} Returns an object with only keys that match the given patterns.
  16670. * @api public
  16671. */
  16672. micromatch$1.matchKeys = (obj, patterns, options) => {
  16673. if (!utils$b.isObject(obj)) {
  16674. throw new TypeError('Expected the first argument to be an object');
  16675. }
  16676. let keys = micromatch$1(Object.keys(obj), patterns, options);
  16677. let res = {};
  16678. for (let key of keys) res[key] = obj[key];
  16679. return res;
  16680. };
  16681. /**
  16682. * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
  16683. *
  16684. * ```js
  16685. * const mm = require('micromatch');
  16686. * // mm.some(list, patterns[, options]);
  16687. *
  16688. * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  16689. * // true
  16690. * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  16691. * // false
  16692. * ```
  16693. * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
  16694. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  16695. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16696. * @return {Boolean} Returns true if any `patterns` matches any of the strings in `list`
  16697. * @api public
  16698. */
  16699. micromatch$1.some = (list, patterns, options) => {
  16700. let items = [].concat(list);
  16701. for (let pattern of [].concat(patterns)) {
  16702. let isMatch = picomatch$2(String(pattern), options);
  16703. if (items.some(item => isMatch(item))) {
  16704. return true;
  16705. }
  16706. }
  16707. return false;
  16708. };
  16709. /**
  16710. * Returns true if every string in the given `list` matches
  16711. * any of the given glob `patterns`.
  16712. *
  16713. * ```js
  16714. * const mm = require('micromatch');
  16715. * // mm.every(list, patterns[, options]);
  16716. *
  16717. * console.log(mm.every('foo.js', ['foo.js']));
  16718. * // true
  16719. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  16720. * // true
  16721. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  16722. * // false
  16723. * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  16724. * // false
  16725. * ```
  16726. * @param {String|Array} `list` The string or array of strings to test.
  16727. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  16728. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16729. * @return {Boolean} Returns true if all `patterns` matches all of the strings in `list`
  16730. * @api public
  16731. */
  16732. micromatch$1.every = (list, patterns, options) => {
  16733. let items = [].concat(list);
  16734. for (let pattern of [].concat(patterns)) {
  16735. let isMatch = picomatch$2(String(pattern), options);
  16736. if (!items.every(item => isMatch(item))) {
  16737. return false;
  16738. }
  16739. }
  16740. return true;
  16741. };
  16742. /**
  16743. * Returns true if **all** of the given `patterns` match
  16744. * the specified string.
  16745. *
  16746. * ```js
  16747. * const mm = require('micromatch');
  16748. * // mm.all(string, patterns[, options]);
  16749. *
  16750. * console.log(mm.all('foo.js', ['foo.js']));
  16751. * // true
  16752. *
  16753. * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  16754. * // false
  16755. *
  16756. * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  16757. * // true
  16758. *
  16759. * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  16760. * // true
  16761. * ```
  16762. * @param {String|Array} `str` The string to test.
  16763. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  16764. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16765. * @return {Boolean} Returns true if any patterns match `str`
  16766. * @api public
  16767. */
  16768. micromatch$1.all = (str, patterns, options) => {
  16769. if (typeof str !== 'string') {
  16770. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  16771. }
  16772. return [].concat(patterns).every(p => picomatch$2(p, options)(str));
  16773. };
  16774. /**
  16775. * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
  16776. *
  16777. * ```js
  16778. * const mm = require('micromatch');
  16779. * // mm.capture(pattern, string[, options]);
  16780. *
  16781. * console.log(mm.capture('test/*.js', 'test/foo.js'));
  16782. * //=> ['foo']
  16783. * console.log(mm.capture('test/*.js', 'foo/bar.css'));
  16784. * //=> null
  16785. * ```
  16786. * @param {String} `glob` Glob pattern to use for matching.
  16787. * @param {String} `input` String to match
  16788. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  16789. * @return {Array|null} Returns an array of captures if the input matches the glob pattern, otherwise `null`.
  16790. * @api public
  16791. */
  16792. micromatch$1.capture = (glob, input, options) => {
  16793. let posix = utils$b.isWindows(options);
  16794. let regex = picomatch$2.makeRe(String(glob), { ...options, capture: true });
  16795. let match = regex.exec(posix ? utils$b.toPosixSlashes(input) : input);
  16796. if (match) {
  16797. return match.slice(1).map(v => v === void 0 ? '' : v);
  16798. }
  16799. };
  16800. /**
  16801. * Create a regular expression from the given glob `pattern`.
  16802. *
  16803. * ```js
  16804. * const mm = require('micromatch');
  16805. * // mm.makeRe(pattern[, options]);
  16806. *
  16807. * console.log(mm.makeRe('*.js'));
  16808. * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  16809. * ```
  16810. * @param {String} `pattern` A glob pattern to convert to regex.
  16811. * @param {Object} `options`
  16812. * @return {RegExp} Returns a regex created from the given pattern.
  16813. * @api public
  16814. */
  16815. micromatch$1.makeRe = (...args) => picomatch$2.makeRe(...args);
  16816. /**
  16817. * Scan a glob pattern to separate the pattern into segments. Used
  16818. * by the [split](#split) method.
  16819. *
  16820. * ```js
  16821. * const mm = require('micromatch');
  16822. * const state = mm.scan(pattern[, options]);
  16823. * ```
  16824. * @param {String} `pattern`
  16825. * @param {Object} `options`
  16826. * @return {Object} Returns an object with
  16827. * @api public
  16828. */
  16829. micromatch$1.scan = (...args) => picomatch$2.scan(...args);
  16830. /**
  16831. * Parse a glob pattern to create the source string for a regular
  16832. * expression.
  16833. *
  16834. * ```js
  16835. * const mm = require('micromatch');
  16836. * const state = mm.parse(pattern[, options]);
  16837. * ```
  16838. * @param {String} `glob`
  16839. * @param {Object} `options`
  16840. * @return {Object} Returns an object with useful properties and output to be used as regex source string.
  16841. * @api public
  16842. */
  16843. micromatch$1.parse = (patterns, options) => {
  16844. let res = [];
  16845. for (let pattern of [].concat(patterns || [])) {
  16846. for (let str of braces$1(String(pattern), options)) {
  16847. res.push(picomatch$2.parse(str, options));
  16848. }
  16849. }
  16850. return res;
  16851. };
  16852. /**
  16853. * Process the given brace `pattern`.
  16854. *
  16855. * ```js
  16856. * const { braces } = require('micromatch');
  16857. * console.log(braces('foo/{a,b,c}/bar'));
  16858. * //=> [ 'foo/(a|b|c)/bar' ]
  16859. *
  16860. * console.log(braces('foo/{a,b,c}/bar', { expand: true }));
  16861. * //=> [ 'foo/a/bar', 'foo/b/bar', 'foo/c/bar' ]
  16862. * ```
  16863. * @param {String} `pattern` String with brace pattern to process.
  16864. * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
  16865. * @return {Array}
  16866. * @api public
  16867. */
  16868. micromatch$1.braces = (pattern, options) => {
  16869. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  16870. if ((options && options.nobrace === true) || !/\{.*\}/.test(pattern)) {
  16871. return [pattern];
  16872. }
  16873. return braces$1(pattern, options);
  16874. };
  16875. /**
  16876. * Expand braces
  16877. */
  16878. micromatch$1.braceExpand = (pattern, options) => {
  16879. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  16880. return micromatch$1.braces(pattern, { ...options, expand: true });
  16881. };
  16882. /**
  16883. * Expose micromatch
  16884. */
  16885. var micromatch_1 = micromatch$1;
  16886. var micromatch$2 = /*@__PURE__*/getDefaultExportFromCjs(micromatch_1);
  16887. Object.defineProperty(pattern$1, "__esModule", { value: true });
  16888. pattern$1.removeDuplicateSlashes = pattern$1.matchAny = pattern$1.convertPatternsToRe = pattern$1.makeRe = pattern$1.getPatternParts = pattern$1.expandBraceExpansion = pattern$1.expandPatternsWithBraceExpansion = pattern$1.isAffectDepthOfReadingPattern = pattern$1.endsWithSlashGlobStar = pattern$1.hasGlobStar = pattern$1.getBaseDirectory = pattern$1.isPatternRelatedToParentDirectory = pattern$1.getPatternsOutsideCurrentDirectory = pattern$1.getPatternsInsideCurrentDirectory = pattern$1.getPositivePatterns = pattern$1.getNegativePatterns = pattern$1.isPositivePattern = pattern$1.isNegativePattern = pattern$1.convertToNegativePattern = pattern$1.convertToPositivePattern = pattern$1.isDynamicPattern = pattern$1.isStaticPattern = void 0;
  16889. const path$f = require$$0$4;
  16890. const globParent$1 = globParent$2;
  16891. const micromatch = micromatch_1;
  16892. const GLOBSTAR$1 = '**';
  16893. const ESCAPE_SYMBOL = '\\';
  16894. const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
  16895. const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*]/;
  16896. const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/;
  16897. const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/;
  16898. const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./;
  16899. /**
  16900. * Matches a sequence of two or more consecutive slashes, excluding the first two slashes at the beginning of the string.
  16901. * The latter is due to the presence of the device path at the beginning of the UNC path.
  16902. */
  16903. const DOUBLE_SLASH_RE$1 = /(?!^)\/{2,}/g;
  16904. function isStaticPattern(pattern, options = {}) {
  16905. return !isDynamicPattern(pattern, options);
  16906. }
  16907. pattern$1.isStaticPattern = isStaticPattern;
  16908. function isDynamicPattern(pattern, options = {}) {
  16909. /**
  16910. * A special case with an empty string is necessary for matching patterns that start with a forward slash.
  16911. * An empty string cannot be a dynamic pattern.
  16912. * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
  16913. */
  16914. if (pattern === '') {
  16915. return false;
  16916. }
  16917. /**
  16918. * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
  16919. * filepath directly (without read directory).
  16920. */
  16921. if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
  16922. return true;
  16923. }
  16924. if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
  16925. return true;
  16926. }
  16927. if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
  16928. return true;
  16929. }
  16930. if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
  16931. return true;
  16932. }
  16933. return false;
  16934. }
  16935. pattern$1.isDynamicPattern = isDynamicPattern;
  16936. function hasBraceExpansion(pattern) {
  16937. const openingBraceIndex = pattern.indexOf('{');
  16938. if (openingBraceIndex === -1) {
  16939. return false;
  16940. }
  16941. const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1);
  16942. if (closingBraceIndex === -1) {
  16943. return false;
  16944. }
  16945. const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex);
  16946. return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent);
  16947. }
  16948. function convertToPositivePattern(pattern) {
  16949. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  16950. }
  16951. pattern$1.convertToPositivePattern = convertToPositivePattern;
  16952. function convertToNegativePattern(pattern) {
  16953. return '!' + pattern;
  16954. }
  16955. pattern$1.convertToNegativePattern = convertToNegativePattern;
  16956. function isNegativePattern(pattern) {
  16957. return pattern.startsWith('!') && pattern[1] !== '(';
  16958. }
  16959. pattern$1.isNegativePattern = isNegativePattern;
  16960. function isPositivePattern(pattern) {
  16961. return !isNegativePattern(pattern);
  16962. }
  16963. pattern$1.isPositivePattern = isPositivePattern;
  16964. function getNegativePatterns(patterns) {
  16965. return patterns.filter(isNegativePattern);
  16966. }
  16967. pattern$1.getNegativePatterns = getNegativePatterns;
  16968. function getPositivePatterns$1(patterns) {
  16969. return patterns.filter(isPositivePattern);
  16970. }
  16971. pattern$1.getPositivePatterns = getPositivePatterns$1;
  16972. /**
  16973. * Returns patterns that can be applied inside the current directory.
  16974. *
  16975. * @example
  16976. * // ['./*', '*', 'a/*']
  16977. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  16978. */
  16979. function getPatternsInsideCurrentDirectory(patterns) {
  16980. return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
  16981. }
  16982. pattern$1.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
  16983. /**
  16984. * Returns patterns to be expanded relative to (outside) the current directory.
  16985. *
  16986. * @example
  16987. * // ['../*', './../*']
  16988. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  16989. */
  16990. function getPatternsOutsideCurrentDirectory(patterns) {
  16991. return patterns.filter(isPatternRelatedToParentDirectory);
  16992. }
  16993. pattern$1.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
  16994. function isPatternRelatedToParentDirectory(pattern) {
  16995. return pattern.startsWith('..') || pattern.startsWith('./..');
  16996. }
  16997. pattern$1.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
  16998. function getBaseDirectory(pattern) {
  16999. return globParent$1(pattern, { flipBackslashes: false });
  17000. }
  17001. pattern$1.getBaseDirectory = getBaseDirectory;
  17002. function hasGlobStar(pattern) {
  17003. return pattern.includes(GLOBSTAR$1);
  17004. }
  17005. pattern$1.hasGlobStar = hasGlobStar;
  17006. function endsWithSlashGlobStar(pattern) {
  17007. return pattern.endsWith('/' + GLOBSTAR$1);
  17008. }
  17009. pattern$1.endsWithSlashGlobStar = endsWithSlashGlobStar;
  17010. function isAffectDepthOfReadingPattern(pattern) {
  17011. const basename = path$f.basename(pattern);
  17012. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  17013. }
  17014. pattern$1.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  17015. function expandPatternsWithBraceExpansion(patterns) {
  17016. return patterns.reduce((collection, pattern) => {
  17017. return collection.concat(expandBraceExpansion(pattern));
  17018. }, []);
  17019. }
  17020. pattern$1.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  17021. function expandBraceExpansion(pattern) {
  17022. const patterns = micromatch.braces(pattern, { expand: true, nodupes: true, keepEscaping: true });
  17023. /**
  17024. * Sort the patterns by length so that the same depth patterns are processed side by side.
  17025. * `a/{b,}/{c,}/*` – `['a///*', 'a/b//*', 'a//c/*', 'a/b/c/*']`
  17026. */
  17027. patterns.sort((a, b) => a.length - b.length);
  17028. /**
  17029. * Micromatch can return an empty string in the case of patterns like `{a,}`.
  17030. */
  17031. return patterns.filter((pattern) => pattern !== '');
  17032. }
  17033. pattern$1.expandBraceExpansion = expandBraceExpansion;
  17034. function getPatternParts(pattern, options) {
  17035. let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  17036. /**
  17037. * The scan method returns an empty array in some cases.
  17038. * See micromatch/picomatch#58 for more details.
  17039. */
  17040. if (parts.length === 0) {
  17041. parts = [pattern];
  17042. }
  17043. /**
  17044. * The scan method does not return an empty part for the pattern with a forward slash.
  17045. * This is another part of micromatch/picomatch#58.
  17046. */
  17047. if (parts[0].startsWith('/')) {
  17048. parts[0] = parts[0].slice(1);
  17049. parts.unshift('');
  17050. }
  17051. return parts;
  17052. }
  17053. pattern$1.getPatternParts = getPatternParts;
  17054. function makeRe(pattern, options) {
  17055. return micromatch.makeRe(pattern, options);
  17056. }
  17057. pattern$1.makeRe = makeRe;
  17058. function convertPatternsToRe(patterns, options) {
  17059. return patterns.map((pattern) => makeRe(pattern, options));
  17060. }
  17061. pattern$1.convertPatternsToRe = convertPatternsToRe;
  17062. function matchAny(entry, patternsRe) {
  17063. return patternsRe.some((patternRe) => patternRe.test(entry));
  17064. }
  17065. pattern$1.matchAny = matchAny;
  17066. /**
  17067. * This package only works with forward slashes as a path separator.
  17068. * Because of this, we cannot use the standard `path.normalize` method, because on Windows platform it will use of backslashes.
  17069. */
  17070. function removeDuplicateSlashes(pattern) {
  17071. return pattern.replace(DOUBLE_SLASH_RE$1, '/');
  17072. }
  17073. pattern$1.removeDuplicateSlashes = removeDuplicateSlashes;
  17074. var stream$4 = {};
  17075. /*
  17076. * merge2
  17077. * https://github.com/teambition/merge2
  17078. *
  17079. * Copyright (c) 2014-2020 Teambition
  17080. * Licensed under the MIT license.
  17081. */
  17082. const Stream = require$$0$7;
  17083. const PassThrough = Stream.PassThrough;
  17084. const slice = Array.prototype.slice;
  17085. var merge2_1 = merge2$1;
  17086. function merge2$1 () {
  17087. const streamsQueue = [];
  17088. const args = slice.call(arguments);
  17089. let merging = false;
  17090. let options = args[args.length - 1];
  17091. if (options && !Array.isArray(options) && options.pipe == null) {
  17092. args.pop();
  17093. } else {
  17094. options = {};
  17095. }
  17096. const doEnd = options.end !== false;
  17097. const doPipeError = options.pipeError === true;
  17098. if (options.objectMode == null) {
  17099. options.objectMode = true;
  17100. }
  17101. if (options.highWaterMark == null) {
  17102. options.highWaterMark = 64 * 1024;
  17103. }
  17104. const mergedStream = PassThrough(options);
  17105. function addStream () {
  17106. for (let i = 0, len = arguments.length; i < len; i++) {
  17107. streamsQueue.push(pauseStreams(arguments[i], options));
  17108. }
  17109. mergeStream();
  17110. return this
  17111. }
  17112. function mergeStream () {
  17113. if (merging) {
  17114. return
  17115. }
  17116. merging = true;
  17117. let streams = streamsQueue.shift();
  17118. if (!streams) {
  17119. process.nextTick(endStream);
  17120. return
  17121. }
  17122. if (!Array.isArray(streams)) {
  17123. streams = [streams];
  17124. }
  17125. let pipesCount = streams.length + 1;
  17126. function next () {
  17127. if (--pipesCount > 0) {
  17128. return
  17129. }
  17130. merging = false;
  17131. mergeStream();
  17132. }
  17133. function pipe (stream) {
  17134. function onend () {
  17135. stream.removeListener('merge2UnpipeEnd', onend);
  17136. stream.removeListener('end', onend);
  17137. if (doPipeError) {
  17138. stream.removeListener('error', onerror);
  17139. }
  17140. next();
  17141. }
  17142. function onerror (err) {
  17143. mergedStream.emit('error', err);
  17144. }
  17145. // skip ended stream
  17146. if (stream._readableState.endEmitted) {
  17147. return next()
  17148. }
  17149. stream.on('merge2UnpipeEnd', onend);
  17150. stream.on('end', onend);
  17151. if (doPipeError) {
  17152. stream.on('error', onerror);
  17153. }
  17154. stream.pipe(mergedStream, { end: false });
  17155. // compatible for old stream
  17156. stream.resume();
  17157. }
  17158. for (let i = 0; i < streams.length; i++) {
  17159. pipe(streams[i]);
  17160. }
  17161. next();
  17162. }
  17163. function endStream () {
  17164. merging = false;
  17165. // emit 'queueDrain' when all streams merged.
  17166. mergedStream.emit('queueDrain');
  17167. if (doEnd) {
  17168. mergedStream.end();
  17169. }
  17170. }
  17171. mergedStream.setMaxListeners(0);
  17172. mergedStream.add = addStream;
  17173. mergedStream.on('unpipe', function (stream) {
  17174. stream.emit('merge2UnpipeEnd');
  17175. });
  17176. if (args.length) {
  17177. addStream.apply(null, args);
  17178. }
  17179. return mergedStream
  17180. }
  17181. // check and pause streams for pipe.
  17182. function pauseStreams (streams, options) {
  17183. if (!Array.isArray(streams)) {
  17184. // Backwards-compat with old-style streams
  17185. if (!streams._readableState && streams.pipe) {
  17186. streams = streams.pipe(PassThrough(options));
  17187. }
  17188. if (!streams._readableState || !streams.pause || !streams.pipe) {
  17189. throw new Error('Only readable stream can be merged.')
  17190. }
  17191. streams.pause();
  17192. } else {
  17193. for (let i = 0, len = streams.length; i < len; i++) {
  17194. streams[i] = pauseStreams(streams[i], options);
  17195. }
  17196. }
  17197. return streams
  17198. }
  17199. Object.defineProperty(stream$4, "__esModule", { value: true });
  17200. stream$4.merge = void 0;
  17201. const merge2 = merge2_1;
  17202. function merge$1(streams) {
  17203. const mergedStream = merge2(streams);
  17204. streams.forEach((stream) => {
  17205. stream.once('error', (error) => mergedStream.emit('error', error));
  17206. });
  17207. mergedStream.once('close', () => propagateCloseEventToSources(streams));
  17208. mergedStream.once('end', () => propagateCloseEventToSources(streams));
  17209. return mergedStream;
  17210. }
  17211. stream$4.merge = merge$1;
  17212. function propagateCloseEventToSources(streams) {
  17213. streams.forEach((stream) => stream.emit('close'));
  17214. }
  17215. var string$2 = {};
  17216. Object.defineProperty(string$2, "__esModule", { value: true });
  17217. string$2.isEmpty = string$2.isString = void 0;
  17218. function isString$1(input) {
  17219. return typeof input === 'string';
  17220. }
  17221. string$2.isString = isString$1;
  17222. function isEmpty$1(input) {
  17223. return input === '';
  17224. }
  17225. string$2.isEmpty = isEmpty$1;
  17226. Object.defineProperty(utils$g, "__esModule", { value: true });
  17227. utils$g.string = utils$g.stream = utils$g.pattern = utils$g.path = utils$g.fs = utils$g.errno = utils$g.array = void 0;
  17228. const array = array$1;
  17229. utils$g.array = array;
  17230. const errno = errno$1;
  17231. utils$g.errno = errno;
  17232. const fs$g = fs$h;
  17233. utils$g.fs = fs$g;
  17234. const path$e = path$h;
  17235. utils$g.path = path$e;
  17236. const pattern = pattern$1;
  17237. utils$g.pattern = pattern;
  17238. const stream$3 = stream$4;
  17239. utils$g.stream = stream$3;
  17240. const string$1 = string$2;
  17241. utils$g.string = string$1;
  17242. Object.defineProperty(tasks, "__esModule", { value: true });
  17243. tasks.convertPatternGroupToTask = tasks.convertPatternGroupsToTasks = tasks.groupPatternsByBaseDirectory = tasks.getNegativePatternsAsPositive = tasks.getPositivePatterns = tasks.convertPatternsToTasks = tasks.generate = void 0;
  17244. const utils$a = utils$g;
  17245. function generate(input, settings) {
  17246. const patterns = processPatterns(input, settings);
  17247. const ignore = processPatterns(settings.ignore, settings);
  17248. const positivePatterns = getPositivePatterns(patterns);
  17249. const negativePatterns = getNegativePatternsAsPositive(patterns, ignore);
  17250. const staticPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isStaticPattern(pattern, settings));
  17251. const dynamicPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isDynamicPattern(pattern, settings));
  17252. const staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  17253. const dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  17254. return staticTasks.concat(dynamicTasks);
  17255. }
  17256. tasks.generate = generate;
  17257. function processPatterns(input, settings) {
  17258. let patterns = input;
  17259. /**
  17260. * The original pattern like `{,*,**,a/*}` can lead to problems checking the depth when matching entry
  17261. * and some problems with the micromatch package (see fast-glob issues: #365, #394).
  17262. *
  17263. * To solve this problem, we expand all patterns containing brace expansion. This can lead to a slight slowdown
  17264. * in matching in the case of a large set of patterns after expansion.
  17265. */
  17266. if (settings.braceExpansion) {
  17267. patterns = utils$a.pattern.expandPatternsWithBraceExpansion(patterns);
  17268. }
  17269. /**
  17270. * If the `baseNameMatch` option is enabled, we must add globstar to patterns, so that they can be used
  17271. * at any nesting level.
  17272. *
  17273. * We do this here, because otherwise we have to complicate the filtering logic. For example, we need to change
  17274. * the pattern in the filter before creating a regular expression. There is no need to change the patterns
  17275. * in the application. Only on the input.
  17276. */
  17277. if (settings.baseNameMatch) {
  17278. patterns = patterns.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`);
  17279. }
  17280. /**
  17281. * This method also removes duplicate slashes that may have been in the pattern or formed as a result of expansion.
  17282. */
  17283. return patterns.map((pattern) => utils$a.pattern.removeDuplicateSlashes(pattern));
  17284. }
  17285. /**
  17286. * Returns tasks grouped by basic pattern directories.
  17287. *
  17288. * Patterns that can be found inside (`./`) and outside (`../`) the current directory are handled separately.
  17289. * This is necessary because directory traversal starts at the base directory and goes deeper.
  17290. */
  17291. function convertPatternsToTasks(positive, negative, dynamic) {
  17292. const tasks = [];
  17293. const patternsOutsideCurrentDirectory = utils$a.pattern.getPatternsOutsideCurrentDirectory(positive);
  17294. const patternsInsideCurrentDirectory = utils$a.pattern.getPatternsInsideCurrentDirectory(positive);
  17295. const outsideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsOutsideCurrentDirectory);
  17296. const insideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsInsideCurrentDirectory);
  17297. tasks.push(...convertPatternGroupsToTasks(outsideCurrentDirectoryGroup, negative, dynamic));
  17298. /*
  17299. * For the sake of reducing future accesses to the file system, we merge all tasks within the current directory
  17300. * into a global task, if at least one pattern refers to the root (`.`). In this case, the global task covers the rest.
  17301. */
  17302. if ('.' in insideCurrentDirectoryGroup) {
  17303. tasks.push(convertPatternGroupToTask('.', patternsInsideCurrentDirectory, negative, dynamic));
  17304. }
  17305. else {
  17306. tasks.push(...convertPatternGroupsToTasks(insideCurrentDirectoryGroup, negative, dynamic));
  17307. }
  17308. return tasks;
  17309. }
  17310. tasks.convertPatternsToTasks = convertPatternsToTasks;
  17311. function getPositivePatterns(patterns) {
  17312. return utils$a.pattern.getPositivePatterns(patterns);
  17313. }
  17314. tasks.getPositivePatterns = getPositivePatterns;
  17315. function getNegativePatternsAsPositive(patterns, ignore) {
  17316. const negative = utils$a.pattern.getNegativePatterns(patterns).concat(ignore);
  17317. const positive = negative.map(utils$a.pattern.convertToPositivePattern);
  17318. return positive;
  17319. }
  17320. tasks.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  17321. function groupPatternsByBaseDirectory(patterns) {
  17322. const group = {};
  17323. return patterns.reduce((collection, pattern) => {
  17324. const base = utils$a.pattern.getBaseDirectory(pattern);
  17325. if (base in collection) {
  17326. collection[base].push(pattern);
  17327. }
  17328. else {
  17329. collection[base] = [pattern];
  17330. }
  17331. return collection;
  17332. }, group);
  17333. }
  17334. tasks.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  17335. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  17336. return Object.keys(positive).map((base) => {
  17337. return convertPatternGroupToTask(base, positive[base], negative, dynamic);
  17338. });
  17339. }
  17340. tasks.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  17341. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  17342. return {
  17343. dynamic,
  17344. positive,
  17345. negative,
  17346. base,
  17347. patterns: [].concat(positive, negative.map(utils$a.pattern.convertToNegativePattern))
  17348. };
  17349. }
  17350. tasks.convertPatternGroupToTask = convertPatternGroupToTask;
  17351. var async$7 = {};
  17352. var async$6 = {};
  17353. var out$3 = {};
  17354. var async$5 = {};
  17355. var async$4 = {};
  17356. var out$2 = {};
  17357. var async$3 = {};
  17358. var out$1 = {};
  17359. var async$2 = {};
  17360. Object.defineProperty(async$2, "__esModule", { value: true });
  17361. async$2.read = void 0;
  17362. function read$3(path, settings, callback) {
  17363. settings.fs.lstat(path, (lstatError, lstat) => {
  17364. if (lstatError !== null) {
  17365. callFailureCallback$2(callback, lstatError);
  17366. return;
  17367. }
  17368. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  17369. callSuccessCallback$2(callback, lstat);
  17370. return;
  17371. }
  17372. settings.fs.stat(path, (statError, stat) => {
  17373. if (statError !== null) {
  17374. if (settings.throwErrorOnBrokenSymbolicLink) {
  17375. callFailureCallback$2(callback, statError);
  17376. return;
  17377. }
  17378. callSuccessCallback$2(callback, lstat);
  17379. return;
  17380. }
  17381. if (settings.markSymbolicLink) {
  17382. stat.isSymbolicLink = () => true;
  17383. }
  17384. callSuccessCallback$2(callback, stat);
  17385. });
  17386. });
  17387. }
  17388. async$2.read = read$3;
  17389. function callFailureCallback$2(callback, error) {
  17390. callback(error);
  17391. }
  17392. function callSuccessCallback$2(callback, result) {
  17393. callback(null, result);
  17394. }
  17395. var sync$8 = {};
  17396. Object.defineProperty(sync$8, "__esModule", { value: true });
  17397. sync$8.read = void 0;
  17398. function read$2(path, settings) {
  17399. const lstat = settings.fs.lstatSync(path);
  17400. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  17401. return lstat;
  17402. }
  17403. try {
  17404. const stat = settings.fs.statSync(path);
  17405. if (settings.markSymbolicLink) {
  17406. stat.isSymbolicLink = () => true;
  17407. }
  17408. return stat;
  17409. }
  17410. catch (error) {
  17411. if (!settings.throwErrorOnBrokenSymbolicLink) {
  17412. return lstat;
  17413. }
  17414. throw error;
  17415. }
  17416. }
  17417. sync$8.read = read$2;
  17418. var settings$3 = {};
  17419. var fs$f = {};
  17420. (function (exports) {
  17421. Object.defineProperty(exports, "__esModule", { value: true });
  17422. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  17423. const fs = require$$0__default;
  17424. exports.FILE_SYSTEM_ADAPTER = {
  17425. lstat: fs.lstat,
  17426. stat: fs.stat,
  17427. lstatSync: fs.lstatSync,
  17428. statSync: fs.statSync
  17429. };
  17430. function createFileSystemAdapter(fsMethods) {
  17431. if (fsMethods === undefined) {
  17432. return exports.FILE_SYSTEM_ADAPTER;
  17433. }
  17434. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  17435. }
  17436. exports.createFileSystemAdapter = createFileSystemAdapter;
  17437. } (fs$f));
  17438. Object.defineProperty(settings$3, "__esModule", { value: true });
  17439. const fs$e = fs$f;
  17440. let Settings$2 = class Settings {
  17441. constructor(_options = {}) {
  17442. this._options = _options;
  17443. this.followSymbolicLink = this._getValue(this._options.followSymbolicLink, true);
  17444. this.fs = fs$e.createFileSystemAdapter(this._options.fs);
  17445. this.markSymbolicLink = this._getValue(this._options.markSymbolicLink, false);
  17446. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  17447. }
  17448. _getValue(option, value) {
  17449. return option !== null && option !== void 0 ? option : value;
  17450. }
  17451. };
  17452. settings$3.default = Settings$2;
  17453. Object.defineProperty(out$1, "__esModule", { value: true });
  17454. out$1.statSync = out$1.stat = out$1.Settings = void 0;
  17455. const async$1 = async$2;
  17456. const sync$7 = sync$8;
  17457. const settings_1$3 = settings$3;
  17458. out$1.Settings = settings_1$3.default;
  17459. function stat$4(path, optionsOrSettingsOrCallback, callback) {
  17460. if (typeof optionsOrSettingsOrCallback === 'function') {
  17461. async$1.read(path, getSettings$2(), optionsOrSettingsOrCallback);
  17462. return;
  17463. }
  17464. async$1.read(path, getSettings$2(optionsOrSettingsOrCallback), callback);
  17465. }
  17466. out$1.stat = stat$4;
  17467. function statSync(path, optionsOrSettings) {
  17468. const settings = getSettings$2(optionsOrSettings);
  17469. return sync$7.read(path, settings);
  17470. }
  17471. out$1.statSync = statSync;
  17472. function getSettings$2(settingsOrOptions = {}) {
  17473. if (settingsOrOptions instanceof settings_1$3.default) {
  17474. return settingsOrOptions;
  17475. }
  17476. return new settings_1$3.default(settingsOrOptions);
  17477. }
  17478. /*! queue-microtask. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  17479. let promise$1;
  17480. var queueMicrotask_1 = typeof queueMicrotask === 'function'
  17481. ? queueMicrotask.bind(typeof window !== 'undefined' ? window : commonjsGlobal)
  17482. // reuse resolved promise, and allocate it lazily
  17483. : cb => (promise$1 || (promise$1 = Promise.resolve()))
  17484. .then(cb)
  17485. .catch(err => setTimeout(() => { throw err }, 0));
  17486. /*! run-parallel. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  17487. var runParallel_1 = runParallel;
  17488. const queueMicrotask$1 = queueMicrotask_1;
  17489. function runParallel (tasks, cb) {
  17490. let results, pending, keys;
  17491. let isSync = true;
  17492. if (Array.isArray(tasks)) {
  17493. results = [];
  17494. pending = tasks.length;
  17495. } else {
  17496. keys = Object.keys(tasks);
  17497. results = {};
  17498. pending = keys.length;
  17499. }
  17500. function done (err) {
  17501. function end () {
  17502. if (cb) cb(err, results);
  17503. cb = null;
  17504. }
  17505. if (isSync) queueMicrotask$1(end);
  17506. else end();
  17507. }
  17508. function each (i, err, result) {
  17509. results[i] = result;
  17510. if (--pending === 0 || err) {
  17511. done(err);
  17512. }
  17513. }
  17514. if (!pending) {
  17515. // empty
  17516. done(null);
  17517. } else if (keys) {
  17518. // object
  17519. keys.forEach(function (key) {
  17520. tasks[key](function (err, result) { each(key, err, result); });
  17521. });
  17522. } else {
  17523. // array
  17524. tasks.forEach(function (task, i) {
  17525. task(function (err, result) { each(i, err, result); });
  17526. });
  17527. }
  17528. isSync = false;
  17529. }
  17530. var constants$2 = {};
  17531. Object.defineProperty(constants$2, "__esModule", { value: true });
  17532. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = void 0;
  17533. const NODE_PROCESS_VERSION_PARTS = process.versions.node.split('.');
  17534. if (NODE_PROCESS_VERSION_PARTS[0] === undefined || NODE_PROCESS_VERSION_PARTS[1] === undefined) {
  17535. throw new Error(`Unexpected behavior. The 'process.versions.node' variable has invalid value: ${process.versions.node}`);
  17536. }
  17537. const MAJOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[0], 10);
  17538. const MINOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[1], 10);
  17539. const SUPPORTED_MAJOR_VERSION = 10;
  17540. const SUPPORTED_MINOR_VERSION = 10;
  17541. const IS_MATCHED_BY_MAJOR = MAJOR_VERSION > SUPPORTED_MAJOR_VERSION;
  17542. const IS_MATCHED_BY_MAJOR_AND_MINOR = MAJOR_VERSION === SUPPORTED_MAJOR_VERSION && MINOR_VERSION >= SUPPORTED_MINOR_VERSION;
  17543. /**
  17544. * IS `true` for Node.js 10.10 and greater.
  17545. */
  17546. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = IS_MATCHED_BY_MAJOR || IS_MATCHED_BY_MAJOR_AND_MINOR;
  17547. var utils$9 = {};
  17548. var fs$d = {};
  17549. Object.defineProperty(fs$d, "__esModule", { value: true });
  17550. fs$d.createDirentFromStats = void 0;
  17551. class DirentFromStats {
  17552. constructor(name, stats) {
  17553. this.name = name;
  17554. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  17555. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  17556. this.isDirectory = stats.isDirectory.bind(stats);
  17557. this.isFIFO = stats.isFIFO.bind(stats);
  17558. this.isFile = stats.isFile.bind(stats);
  17559. this.isSocket = stats.isSocket.bind(stats);
  17560. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  17561. }
  17562. }
  17563. function createDirentFromStats(name, stats) {
  17564. return new DirentFromStats(name, stats);
  17565. }
  17566. fs$d.createDirentFromStats = createDirentFromStats;
  17567. Object.defineProperty(utils$9, "__esModule", { value: true });
  17568. utils$9.fs = void 0;
  17569. const fs$c = fs$d;
  17570. utils$9.fs = fs$c;
  17571. var common$a = {};
  17572. Object.defineProperty(common$a, "__esModule", { value: true });
  17573. common$a.joinPathSegments = void 0;
  17574. function joinPathSegments$1(a, b, separator) {
  17575. /**
  17576. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  17577. */
  17578. if (a.endsWith(separator)) {
  17579. return a + b;
  17580. }
  17581. return a + separator + b;
  17582. }
  17583. common$a.joinPathSegments = joinPathSegments$1;
  17584. Object.defineProperty(async$3, "__esModule", { value: true });
  17585. async$3.readdir = async$3.readdirWithFileTypes = async$3.read = void 0;
  17586. const fsStat$5 = out$1;
  17587. const rpl = runParallel_1;
  17588. const constants_1$1 = constants$2;
  17589. const utils$8 = utils$9;
  17590. const common$9 = common$a;
  17591. function read$1(directory, settings, callback) {
  17592. if (!settings.stats && constants_1$1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  17593. readdirWithFileTypes$1(directory, settings, callback);
  17594. return;
  17595. }
  17596. readdir$3(directory, settings, callback);
  17597. }
  17598. async$3.read = read$1;
  17599. function readdirWithFileTypes$1(directory, settings, callback) {
  17600. settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
  17601. if (readdirError !== null) {
  17602. callFailureCallback$1(callback, readdirError);
  17603. return;
  17604. }
  17605. const entries = dirents.map((dirent) => ({
  17606. dirent,
  17607. name: dirent.name,
  17608. path: common$9.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  17609. }));
  17610. if (!settings.followSymbolicLinks) {
  17611. callSuccessCallback$1(callback, entries);
  17612. return;
  17613. }
  17614. const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
  17615. rpl(tasks, (rplError, rplEntries) => {
  17616. if (rplError !== null) {
  17617. callFailureCallback$1(callback, rplError);
  17618. return;
  17619. }
  17620. callSuccessCallback$1(callback, rplEntries);
  17621. });
  17622. });
  17623. }
  17624. async$3.readdirWithFileTypes = readdirWithFileTypes$1;
  17625. function makeRplTaskEntry(entry, settings) {
  17626. return (done) => {
  17627. if (!entry.dirent.isSymbolicLink()) {
  17628. done(null, entry);
  17629. return;
  17630. }
  17631. settings.fs.stat(entry.path, (statError, stats) => {
  17632. if (statError !== null) {
  17633. if (settings.throwErrorOnBrokenSymbolicLink) {
  17634. done(statError);
  17635. return;
  17636. }
  17637. done(null, entry);
  17638. return;
  17639. }
  17640. entry.dirent = utils$8.fs.createDirentFromStats(entry.name, stats);
  17641. done(null, entry);
  17642. });
  17643. };
  17644. }
  17645. function readdir$3(directory, settings, callback) {
  17646. settings.fs.readdir(directory, (readdirError, names) => {
  17647. if (readdirError !== null) {
  17648. callFailureCallback$1(callback, readdirError);
  17649. return;
  17650. }
  17651. const tasks = names.map((name) => {
  17652. const path = common$9.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  17653. return (done) => {
  17654. fsStat$5.stat(path, settings.fsStatSettings, (error, stats) => {
  17655. if (error !== null) {
  17656. done(error);
  17657. return;
  17658. }
  17659. const entry = {
  17660. name,
  17661. path,
  17662. dirent: utils$8.fs.createDirentFromStats(name, stats)
  17663. };
  17664. if (settings.stats) {
  17665. entry.stats = stats;
  17666. }
  17667. done(null, entry);
  17668. });
  17669. };
  17670. });
  17671. rpl(tasks, (rplError, entries) => {
  17672. if (rplError !== null) {
  17673. callFailureCallback$1(callback, rplError);
  17674. return;
  17675. }
  17676. callSuccessCallback$1(callback, entries);
  17677. });
  17678. });
  17679. }
  17680. async$3.readdir = readdir$3;
  17681. function callFailureCallback$1(callback, error) {
  17682. callback(error);
  17683. }
  17684. function callSuccessCallback$1(callback, result) {
  17685. callback(null, result);
  17686. }
  17687. var sync$6 = {};
  17688. Object.defineProperty(sync$6, "__esModule", { value: true });
  17689. sync$6.readdir = sync$6.readdirWithFileTypes = sync$6.read = void 0;
  17690. const fsStat$4 = out$1;
  17691. const constants_1 = constants$2;
  17692. const utils$7 = utils$9;
  17693. const common$8 = common$a;
  17694. function read(directory, settings) {
  17695. if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  17696. return readdirWithFileTypes(directory, settings);
  17697. }
  17698. return readdir$2(directory, settings);
  17699. }
  17700. sync$6.read = read;
  17701. function readdirWithFileTypes(directory, settings) {
  17702. const dirents = settings.fs.readdirSync(directory, { withFileTypes: true });
  17703. return dirents.map((dirent) => {
  17704. const entry = {
  17705. dirent,
  17706. name: dirent.name,
  17707. path: common$8.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  17708. };
  17709. if (entry.dirent.isSymbolicLink() && settings.followSymbolicLinks) {
  17710. try {
  17711. const stats = settings.fs.statSync(entry.path);
  17712. entry.dirent = utils$7.fs.createDirentFromStats(entry.name, stats);
  17713. }
  17714. catch (error) {
  17715. if (settings.throwErrorOnBrokenSymbolicLink) {
  17716. throw error;
  17717. }
  17718. }
  17719. }
  17720. return entry;
  17721. });
  17722. }
  17723. sync$6.readdirWithFileTypes = readdirWithFileTypes;
  17724. function readdir$2(directory, settings) {
  17725. const names = settings.fs.readdirSync(directory);
  17726. return names.map((name) => {
  17727. const entryPath = common$8.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  17728. const stats = fsStat$4.statSync(entryPath, settings.fsStatSettings);
  17729. const entry = {
  17730. name,
  17731. path: entryPath,
  17732. dirent: utils$7.fs.createDirentFromStats(name, stats)
  17733. };
  17734. if (settings.stats) {
  17735. entry.stats = stats;
  17736. }
  17737. return entry;
  17738. });
  17739. }
  17740. sync$6.readdir = readdir$2;
  17741. var settings$2 = {};
  17742. var fs$b = {};
  17743. (function (exports) {
  17744. Object.defineProperty(exports, "__esModule", { value: true });
  17745. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  17746. const fs = require$$0__default;
  17747. exports.FILE_SYSTEM_ADAPTER = {
  17748. lstat: fs.lstat,
  17749. stat: fs.stat,
  17750. lstatSync: fs.lstatSync,
  17751. statSync: fs.statSync,
  17752. readdir: fs.readdir,
  17753. readdirSync: fs.readdirSync
  17754. };
  17755. function createFileSystemAdapter(fsMethods) {
  17756. if (fsMethods === undefined) {
  17757. return exports.FILE_SYSTEM_ADAPTER;
  17758. }
  17759. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  17760. }
  17761. exports.createFileSystemAdapter = createFileSystemAdapter;
  17762. } (fs$b));
  17763. Object.defineProperty(settings$2, "__esModule", { value: true });
  17764. const path$d = require$$0$4;
  17765. const fsStat$3 = out$1;
  17766. const fs$a = fs$b;
  17767. let Settings$1 = class Settings {
  17768. constructor(_options = {}) {
  17769. this._options = _options;
  17770. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, false);
  17771. this.fs = fs$a.createFileSystemAdapter(this._options.fs);
  17772. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$d.sep);
  17773. this.stats = this._getValue(this._options.stats, false);
  17774. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  17775. this.fsStatSettings = new fsStat$3.Settings({
  17776. followSymbolicLink: this.followSymbolicLinks,
  17777. fs: this.fs,
  17778. throwErrorOnBrokenSymbolicLink: this.throwErrorOnBrokenSymbolicLink
  17779. });
  17780. }
  17781. _getValue(option, value) {
  17782. return option !== null && option !== void 0 ? option : value;
  17783. }
  17784. };
  17785. settings$2.default = Settings$1;
  17786. Object.defineProperty(out$2, "__esModule", { value: true });
  17787. out$2.Settings = out$2.scandirSync = out$2.scandir = void 0;
  17788. const async = async$3;
  17789. const sync$5 = sync$6;
  17790. const settings_1$2 = settings$2;
  17791. out$2.Settings = settings_1$2.default;
  17792. function scandir(path, optionsOrSettingsOrCallback, callback) {
  17793. if (typeof optionsOrSettingsOrCallback === 'function') {
  17794. async.read(path, getSettings$1(), optionsOrSettingsOrCallback);
  17795. return;
  17796. }
  17797. async.read(path, getSettings$1(optionsOrSettingsOrCallback), callback);
  17798. }
  17799. out$2.scandir = scandir;
  17800. function scandirSync(path, optionsOrSettings) {
  17801. const settings = getSettings$1(optionsOrSettings);
  17802. return sync$5.read(path, settings);
  17803. }
  17804. out$2.scandirSync = scandirSync;
  17805. function getSettings$1(settingsOrOptions = {}) {
  17806. if (settingsOrOptions instanceof settings_1$2.default) {
  17807. return settingsOrOptions;
  17808. }
  17809. return new settings_1$2.default(settingsOrOptions);
  17810. }
  17811. var queue = {exports: {}};
  17812. function reusify$1 (Constructor) {
  17813. var head = new Constructor();
  17814. var tail = head;
  17815. function get () {
  17816. var current = head;
  17817. if (current.next) {
  17818. head = current.next;
  17819. } else {
  17820. head = new Constructor();
  17821. tail = head;
  17822. }
  17823. current.next = null;
  17824. return current
  17825. }
  17826. function release (obj) {
  17827. tail.next = obj;
  17828. tail = obj;
  17829. }
  17830. return {
  17831. get: get,
  17832. release: release
  17833. }
  17834. }
  17835. var reusify_1 = reusify$1;
  17836. /* eslint-disable no-var */
  17837. var reusify = reusify_1;
  17838. function fastqueue (context, worker, concurrency) {
  17839. if (typeof context === 'function') {
  17840. concurrency = worker;
  17841. worker = context;
  17842. context = null;
  17843. }
  17844. if (concurrency < 1) {
  17845. throw new Error('fastqueue concurrency must be greater than 1')
  17846. }
  17847. var cache = reusify(Task);
  17848. var queueHead = null;
  17849. var queueTail = null;
  17850. var _running = 0;
  17851. var errorHandler = null;
  17852. var self = {
  17853. push: push,
  17854. drain: noop$3,
  17855. saturated: noop$3,
  17856. pause: pause,
  17857. paused: false,
  17858. concurrency: concurrency,
  17859. running: running,
  17860. resume: resume,
  17861. idle: idle,
  17862. length: length,
  17863. getQueue: getQueue,
  17864. unshift: unshift,
  17865. empty: noop$3,
  17866. kill: kill,
  17867. killAndDrain: killAndDrain,
  17868. error: error
  17869. };
  17870. return self
  17871. function running () {
  17872. return _running
  17873. }
  17874. function pause () {
  17875. self.paused = true;
  17876. }
  17877. function length () {
  17878. var current = queueHead;
  17879. var counter = 0;
  17880. while (current) {
  17881. current = current.next;
  17882. counter++;
  17883. }
  17884. return counter
  17885. }
  17886. function getQueue () {
  17887. var current = queueHead;
  17888. var tasks = [];
  17889. while (current) {
  17890. tasks.push(current.value);
  17891. current = current.next;
  17892. }
  17893. return tasks
  17894. }
  17895. function resume () {
  17896. if (!self.paused) return
  17897. self.paused = false;
  17898. for (var i = 0; i < self.concurrency; i++) {
  17899. _running++;
  17900. release();
  17901. }
  17902. }
  17903. function idle () {
  17904. return _running === 0 && self.length() === 0
  17905. }
  17906. function push (value, done) {
  17907. var current = cache.get();
  17908. current.context = context;
  17909. current.release = release;
  17910. current.value = value;
  17911. current.callback = done || noop$3;
  17912. current.errorHandler = errorHandler;
  17913. if (_running === self.concurrency || self.paused) {
  17914. if (queueTail) {
  17915. queueTail.next = current;
  17916. queueTail = current;
  17917. } else {
  17918. queueHead = current;
  17919. queueTail = current;
  17920. self.saturated();
  17921. }
  17922. } else {
  17923. _running++;
  17924. worker.call(context, current.value, current.worked);
  17925. }
  17926. }
  17927. function unshift (value, done) {
  17928. var current = cache.get();
  17929. current.context = context;
  17930. current.release = release;
  17931. current.value = value;
  17932. current.callback = done || noop$3;
  17933. if (_running === self.concurrency || self.paused) {
  17934. if (queueHead) {
  17935. current.next = queueHead;
  17936. queueHead = current;
  17937. } else {
  17938. queueHead = current;
  17939. queueTail = current;
  17940. self.saturated();
  17941. }
  17942. } else {
  17943. _running++;
  17944. worker.call(context, current.value, current.worked);
  17945. }
  17946. }
  17947. function release (holder) {
  17948. if (holder) {
  17949. cache.release(holder);
  17950. }
  17951. var next = queueHead;
  17952. if (next) {
  17953. if (!self.paused) {
  17954. if (queueTail === queueHead) {
  17955. queueTail = null;
  17956. }
  17957. queueHead = next.next;
  17958. next.next = null;
  17959. worker.call(context, next.value, next.worked);
  17960. if (queueTail === null) {
  17961. self.empty();
  17962. }
  17963. } else {
  17964. _running--;
  17965. }
  17966. } else if (--_running === 0) {
  17967. self.drain();
  17968. }
  17969. }
  17970. function kill () {
  17971. queueHead = null;
  17972. queueTail = null;
  17973. self.drain = noop$3;
  17974. }
  17975. function killAndDrain () {
  17976. queueHead = null;
  17977. queueTail = null;
  17978. self.drain();
  17979. self.drain = noop$3;
  17980. }
  17981. function error (handler) {
  17982. errorHandler = handler;
  17983. }
  17984. }
  17985. function noop$3 () {}
  17986. function Task () {
  17987. this.value = null;
  17988. this.callback = noop$3;
  17989. this.next = null;
  17990. this.release = noop$3;
  17991. this.context = null;
  17992. this.errorHandler = null;
  17993. var self = this;
  17994. this.worked = function worked (err, result) {
  17995. var callback = self.callback;
  17996. var errorHandler = self.errorHandler;
  17997. var val = self.value;
  17998. self.value = null;
  17999. self.callback = noop$3;
  18000. if (self.errorHandler) {
  18001. errorHandler(err, val);
  18002. }
  18003. callback.call(self.context, err, result);
  18004. self.release(self);
  18005. };
  18006. }
  18007. function queueAsPromised (context, worker, concurrency) {
  18008. if (typeof context === 'function') {
  18009. concurrency = worker;
  18010. worker = context;
  18011. context = null;
  18012. }
  18013. function asyncWrapper (arg, cb) {
  18014. worker.call(this, arg)
  18015. .then(function (res) {
  18016. cb(null, res);
  18017. }, cb);
  18018. }
  18019. var queue = fastqueue(context, asyncWrapper, concurrency);
  18020. var pushCb = queue.push;
  18021. var unshiftCb = queue.unshift;
  18022. queue.push = push;
  18023. queue.unshift = unshift;
  18024. queue.drained = drained;
  18025. return queue
  18026. function push (value) {
  18027. var p = new Promise(function (resolve, reject) {
  18028. pushCb(value, function (err, result) {
  18029. if (err) {
  18030. reject(err);
  18031. return
  18032. }
  18033. resolve(result);
  18034. });
  18035. });
  18036. // Let's fork the promise chain to
  18037. // make the error bubble up to the user but
  18038. // not lead to a unhandledRejection
  18039. p.catch(noop$3);
  18040. return p
  18041. }
  18042. function unshift (value) {
  18043. var p = new Promise(function (resolve, reject) {
  18044. unshiftCb(value, function (err, result) {
  18045. if (err) {
  18046. reject(err);
  18047. return
  18048. }
  18049. resolve(result);
  18050. });
  18051. });
  18052. // Let's fork the promise chain to
  18053. // make the error bubble up to the user but
  18054. // not lead to a unhandledRejection
  18055. p.catch(noop$3);
  18056. return p
  18057. }
  18058. function drained () {
  18059. var previousDrain = queue.drain;
  18060. var p = new Promise(function (resolve) {
  18061. queue.drain = function () {
  18062. previousDrain();
  18063. resolve();
  18064. };
  18065. });
  18066. return p
  18067. }
  18068. }
  18069. queue.exports = fastqueue;
  18070. queue.exports.promise = queueAsPromised;
  18071. var queueExports = queue.exports;
  18072. var common$7 = {};
  18073. Object.defineProperty(common$7, "__esModule", { value: true });
  18074. common$7.joinPathSegments = common$7.replacePathSegmentSeparator = common$7.isAppliedFilter = common$7.isFatalError = void 0;
  18075. function isFatalError(settings, error) {
  18076. if (settings.errorFilter === null) {
  18077. return true;
  18078. }
  18079. return !settings.errorFilter(error);
  18080. }
  18081. common$7.isFatalError = isFatalError;
  18082. function isAppliedFilter(filter, value) {
  18083. return filter === null || filter(value);
  18084. }
  18085. common$7.isAppliedFilter = isAppliedFilter;
  18086. function replacePathSegmentSeparator(filepath, separator) {
  18087. return filepath.split(/[/\\]/).join(separator);
  18088. }
  18089. common$7.replacePathSegmentSeparator = replacePathSegmentSeparator;
  18090. function joinPathSegments(a, b, separator) {
  18091. if (a === '') {
  18092. return b;
  18093. }
  18094. /**
  18095. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  18096. */
  18097. if (a.endsWith(separator)) {
  18098. return a + b;
  18099. }
  18100. return a + separator + b;
  18101. }
  18102. common$7.joinPathSegments = joinPathSegments;
  18103. var reader$1 = {};
  18104. Object.defineProperty(reader$1, "__esModule", { value: true });
  18105. const common$6 = common$7;
  18106. let Reader$1 = class Reader {
  18107. constructor(_root, _settings) {
  18108. this._root = _root;
  18109. this._settings = _settings;
  18110. this._root = common$6.replacePathSegmentSeparator(_root, _settings.pathSegmentSeparator);
  18111. }
  18112. };
  18113. reader$1.default = Reader$1;
  18114. Object.defineProperty(async$4, "__esModule", { value: true });
  18115. const events_1 = require$$0$5;
  18116. const fsScandir$2 = out$2;
  18117. const fastq = queueExports;
  18118. const common$5 = common$7;
  18119. const reader_1$4 = reader$1;
  18120. class AsyncReader extends reader_1$4.default {
  18121. constructor(_root, _settings) {
  18122. super(_root, _settings);
  18123. this._settings = _settings;
  18124. this._scandir = fsScandir$2.scandir;
  18125. this._emitter = new events_1.EventEmitter();
  18126. this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
  18127. this._isFatalError = false;
  18128. this._isDestroyed = false;
  18129. this._queue.drain = () => {
  18130. if (!this._isFatalError) {
  18131. this._emitter.emit('end');
  18132. }
  18133. };
  18134. }
  18135. read() {
  18136. this._isFatalError = false;
  18137. this._isDestroyed = false;
  18138. setImmediate(() => {
  18139. this._pushToQueue(this._root, this._settings.basePath);
  18140. });
  18141. return this._emitter;
  18142. }
  18143. get isDestroyed() {
  18144. return this._isDestroyed;
  18145. }
  18146. destroy() {
  18147. if (this._isDestroyed) {
  18148. throw new Error('The reader is already destroyed');
  18149. }
  18150. this._isDestroyed = true;
  18151. this._queue.killAndDrain();
  18152. }
  18153. onEntry(callback) {
  18154. this._emitter.on('entry', callback);
  18155. }
  18156. onError(callback) {
  18157. this._emitter.once('error', callback);
  18158. }
  18159. onEnd(callback) {
  18160. this._emitter.once('end', callback);
  18161. }
  18162. _pushToQueue(directory, base) {
  18163. const queueItem = { directory, base };
  18164. this._queue.push(queueItem, (error) => {
  18165. if (error !== null) {
  18166. this._handleError(error);
  18167. }
  18168. });
  18169. }
  18170. _worker(item, done) {
  18171. this._scandir(item.directory, this._settings.fsScandirSettings, (error, entries) => {
  18172. if (error !== null) {
  18173. done(error, undefined);
  18174. return;
  18175. }
  18176. for (const entry of entries) {
  18177. this._handleEntry(entry, item.base);
  18178. }
  18179. done(null, undefined);
  18180. });
  18181. }
  18182. _handleError(error) {
  18183. if (this._isDestroyed || !common$5.isFatalError(this._settings, error)) {
  18184. return;
  18185. }
  18186. this._isFatalError = true;
  18187. this._isDestroyed = true;
  18188. this._emitter.emit('error', error);
  18189. }
  18190. _handleEntry(entry, base) {
  18191. if (this._isDestroyed || this._isFatalError) {
  18192. return;
  18193. }
  18194. const fullpath = entry.path;
  18195. if (base !== undefined) {
  18196. entry.path = common$5.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  18197. }
  18198. if (common$5.isAppliedFilter(this._settings.entryFilter, entry)) {
  18199. this._emitEntry(entry);
  18200. }
  18201. if (entry.dirent.isDirectory() && common$5.isAppliedFilter(this._settings.deepFilter, entry)) {
  18202. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  18203. }
  18204. }
  18205. _emitEntry(entry) {
  18206. this._emitter.emit('entry', entry);
  18207. }
  18208. }
  18209. async$4.default = AsyncReader;
  18210. Object.defineProperty(async$5, "__esModule", { value: true });
  18211. const async_1$4 = async$4;
  18212. class AsyncProvider {
  18213. constructor(_root, _settings) {
  18214. this._root = _root;
  18215. this._settings = _settings;
  18216. this._reader = new async_1$4.default(this._root, this._settings);
  18217. this._storage = [];
  18218. }
  18219. read(callback) {
  18220. this._reader.onError((error) => {
  18221. callFailureCallback(callback, error);
  18222. });
  18223. this._reader.onEntry((entry) => {
  18224. this._storage.push(entry);
  18225. });
  18226. this._reader.onEnd(() => {
  18227. callSuccessCallback(callback, this._storage);
  18228. });
  18229. this._reader.read();
  18230. }
  18231. }
  18232. async$5.default = AsyncProvider;
  18233. function callFailureCallback(callback, error) {
  18234. callback(error);
  18235. }
  18236. function callSuccessCallback(callback, entries) {
  18237. callback(null, entries);
  18238. }
  18239. var stream$2 = {};
  18240. Object.defineProperty(stream$2, "__esModule", { value: true });
  18241. const stream_1$5 = require$$0$7;
  18242. const async_1$3 = async$4;
  18243. class StreamProvider {
  18244. constructor(_root, _settings) {
  18245. this._root = _root;
  18246. this._settings = _settings;
  18247. this._reader = new async_1$3.default(this._root, this._settings);
  18248. this._stream = new stream_1$5.Readable({
  18249. objectMode: true,
  18250. read: () => { },
  18251. destroy: () => {
  18252. if (!this._reader.isDestroyed) {
  18253. this._reader.destroy();
  18254. }
  18255. }
  18256. });
  18257. }
  18258. read() {
  18259. this._reader.onError((error) => {
  18260. this._stream.emit('error', error);
  18261. });
  18262. this._reader.onEntry((entry) => {
  18263. this._stream.push(entry);
  18264. });
  18265. this._reader.onEnd(() => {
  18266. this._stream.push(null);
  18267. });
  18268. this._reader.read();
  18269. return this._stream;
  18270. }
  18271. }
  18272. stream$2.default = StreamProvider;
  18273. var sync$4 = {};
  18274. var sync$3 = {};
  18275. Object.defineProperty(sync$3, "__esModule", { value: true });
  18276. const fsScandir$1 = out$2;
  18277. const common$4 = common$7;
  18278. const reader_1$3 = reader$1;
  18279. class SyncReader extends reader_1$3.default {
  18280. constructor() {
  18281. super(...arguments);
  18282. this._scandir = fsScandir$1.scandirSync;
  18283. this._storage = [];
  18284. this._queue = new Set();
  18285. }
  18286. read() {
  18287. this._pushToQueue(this._root, this._settings.basePath);
  18288. this._handleQueue();
  18289. return this._storage;
  18290. }
  18291. _pushToQueue(directory, base) {
  18292. this._queue.add({ directory, base });
  18293. }
  18294. _handleQueue() {
  18295. for (const item of this._queue.values()) {
  18296. this._handleDirectory(item.directory, item.base);
  18297. }
  18298. }
  18299. _handleDirectory(directory, base) {
  18300. try {
  18301. const entries = this._scandir(directory, this._settings.fsScandirSettings);
  18302. for (const entry of entries) {
  18303. this._handleEntry(entry, base);
  18304. }
  18305. }
  18306. catch (error) {
  18307. this._handleError(error);
  18308. }
  18309. }
  18310. _handleError(error) {
  18311. if (!common$4.isFatalError(this._settings, error)) {
  18312. return;
  18313. }
  18314. throw error;
  18315. }
  18316. _handleEntry(entry, base) {
  18317. const fullpath = entry.path;
  18318. if (base !== undefined) {
  18319. entry.path = common$4.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  18320. }
  18321. if (common$4.isAppliedFilter(this._settings.entryFilter, entry)) {
  18322. this._pushToStorage(entry);
  18323. }
  18324. if (entry.dirent.isDirectory() && common$4.isAppliedFilter(this._settings.deepFilter, entry)) {
  18325. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  18326. }
  18327. }
  18328. _pushToStorage(entry) {
  18329. this._storage.push(entry);
  18330. }
  18331. }
  18332. sync$3.default = SyncReader;
  18333. Object.defineProperty(sync$4, "__esModule", { value: true });
  18334. const sync_1$3 = sync$3;
  18335. class SyncProvider {
  18336. constructor(_root, _settings) {
  18337. this._root = _root;
  18338. this._settings = _settings;
  18339. this._reader = new sync_1$3.default(this._root, this._settings);
  18340. }
  18341. read() {
  18342. return this._reader.read();
  18343. }
  18344. }
  18345. sync$4.default = SyncProvider;
  18346. var settings$1 = {};
  18347. Object.defineProperty(settings$1, "__esModule", { value: true });
  18348. const path$c = require$$0$4;
  18349. const fsScandir = out$2;
  18350. class Settings {
  18351. constructor(_options = {}) {
  18352. this._options = _options;
  18353. this.basePath = this._getValue(this._options.basePath, undefined);
  18354. this.concurrency = this._getValue(this._options.concurrency, Number.POSITIVE_INFINITY);
  18355. this.deepFilter = this._getValue(this._options.deepFilter, null);
  18356. this.entryFilter = this._getValue(this._options.entryFilter, null);
  18357. this.errorFilter = this._getValue(this._options.errorFilter, null);
  18358. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$c.sep);
  18359. this.fsScandirSettings = new fsScandir.Settings({
  18360. followSymbolicLinks: this._options.followSymbolicLinks,
  18361. fs: this._options.fs,
  18362. pathSegmentSeparator: this._options.pathSegmentSeparator,
  18363. stats: this._options.stats,
  18364. throwErrorOnBrokenSymbolicLink: this._options.throwErrorOnBrokenSymbolicLink
  18365. });
  18366. }
  18367. _getValue(option, value) {
  18368. return option !== null && option !== void 0 ? option : value;
  18369. }
  18370. }
  18371. settings$1.default = Settings;
  18372. Object.defineProperty(out$3, "__esModule", { value: true });
  18373. out$3.Settings = out$3.walkStream = out$3.walkSync = out$3.walk = void 0;
  18374. const async_1$2 = async$5;
  18375. const stream_1$4 = stream$2;
  18376. const sync_1$2 = sync$4;
  18377. const settings_1$1 = settings$1;
  18378. out$3.Settings = settings_1$1.default;
  18379. function walk$2(directory, optionsOrSettingsOrCallback, callback) {
  18380. if (typeof optionsOrSettingsOrCallback === 'function') {
  18381. new async_1$2.default(directory, getSettings()).read(optionsOrSettingsOrCallback);
  18382. return;
  18383. }
  18384. new async_1$2.default(directory, getSettings(optionsOrSettingsOrCallback)).read(callback);
  18385. }
  18386. out$3.walk = walk$2;
  18387. function walkSync(directory, optionsOrSettings) {
  18388. const settings = getSettings(optionsOrSettings);
  18389. const provider = new sync_1$2.default(directory, settings);
  18390. return provider.read();
  18391. }
  18392. out$3.walkSync = walkSync;
  18393. function walkStream(directory, optionsOrSettings) {
  18394. const settings = getSettings(optionsOrSettings);
  18395. const provider = new stream_1$4.default(directory, settings);
  18396. return provider.read();
  18397. }
  18398. out$3.walkStream = walkStream;
  18399. function getSettings(settingsOrOptions = {}) {
  18400. if (settingsOrOptions instanceof settings_1$1.default) {
  18401. return settingsOrOptions;
  18402. }
  18403. return new settings_1$1.default(settingsOrOptions);
  18404. }
  18405. var reader = {};
  18406. Object.defineProperty(reader, "__esModule", { value: true });
  18407. const path$b = require$$0$4;
  18408. const fsStat$2 = out$1;
  18409. const utils$6 = utils$g;
  18410. class Reader {
  18411. constructor(_settings) {
  18412. this._settings = _settings;
  18413. this._fsStatSettings = new fsStat$2.Settings({
  18414. followSymbolicLink: this._settings.followSymbolicLinks,
  18415. fs: this._settings.fs,
  18416. throwErrorOnBrokenSymbolicLink: this._settings.followSymbolicLinks
  18417. });
  18418. }
  18419. _getFullEntryPath(filepath) {
  18420. return path$b.resolve(this._settings.cwd, filepath);
  18421. }
  18422. _makeEntry(stats, pattern) {
  18423. const entry = {
  18424. name: pattern,
  18425. path: pattern,
  18426. dirent: utils$6.fs.createDirentFromStats(pattern, stats)
  18427. };
  18428. if (this._settings.stats) {
  18429. entry.stats = stats;
  18430. }
  18431. return entry;
  18432. }
  18433. _isFatalError(error) {
  18434. return !utils$6.errno.isEnoentCodeError(error) && !this._settings.suppressErrors;
  18435. }
  18436. }
  18437. reader.default = Reader;
  18438. var stream$1 = {};
  18439. Object.defineProperty(stream$1, "__esModule", { value: true });
  18440. const stream_1$3 = require$$0$7;
  18441. const fsStat$1 = out$1;
  18442. const fsWalk$2 = out$3;
  18443. const reader_1$2 = reader;
  18444. class ReaderStream extends reader_1$2.default {
  18445. constructor() {
  18446. super(...arguments);
  18447. this._walkStream = fsWalk$2.walkStream;
  18448. this._stat = fsStat$1.stat;
  18449. }
  18450. dynamic(root, options) {
  18451. return this._walkStream(root, options);
  18452. }
  18453. static(patterns, options) {
  18454. const filepaths = patterns.map(this._getFullEntryPath, this);
  18455. const stream = new stream_1$3.PassThrough({ objectMode: true });
  18456. stream._write = (index, _enc, done) => {
  18457. return this._getEntry(filepaths[index], patterns[index], options)
  18458. .then((entry) => {
  18459. if (entry !== null && options.entryFilter(entry)) {
  18460. stream.push(entry);
  18461. }
  18462. if (index === filepaths.length - 1) {
  18463. stream.end();
  18464. }
  18465. done();
  18466. })
  18467. .catch(done);
  18468. };
  18469. for (let i = 0; i < filepaths.length; i++) {
  18470. stream.write(i);
  18471. }
  18472. return stream;
  18473. }
  18474. _getEntry(filepath, pattern, options) {
  18475. return this._getStat(filepath)
  18476. .then((stats) => this._makeEntry(stats, pattern))
  18477. .catch((error) => {
  18478. if (options.errorFilter(error)) {
  18479. return null;
  18480. }
  18481. throw error;
  18482. });
  18483. }
  18484. _getStat(filepath) {
  18485. return new Promise((resolve, reject) => {
  18486. this._stat(filepath, this._fsStatSettings, (error, stats) => {
  18487. return error === null ? resolve(stats) : reject(error);
  18488. });
  18489. });
  18490. }
  18491. }
  18492. stream$1.default = ReaderStream;
  18493. Object.defineProperty(async$6, "__esModule", { value: true });
  18494. const fsWalk$1 = out$3;
  18495. const reader_1$1 = reader;
  18496. const stream_1$2 = stream$1;
  18497. class ReaderAsync extends reader_1$1.default {
  18498. constructor() {
  18499. super(...arguments);
  18500. this._walkAsync = fsWalk$1.walk;
  18501. this._readerStream = new stream_1$2.default(this._settings);
  18502. }
  18503. dynamic(root, options) {
  18504. return new Promise((resolve, reject) => {
  18505. this._walkAsync(root, options, (error, entries) => {
  18506. if (error === null) {
  18507. resolve(entries);
  18508. }
  18509. else {
  18510. reject(error);
  18511. }
  18512. });
  18513. });
  18514. }
  18515. async static(patterns, options) {
  18516. const entries = [];
  18517. const stream = this._readerStream.static(patterns, options);
  18518. // After #235, replace it with an asynchronous iterator.
  18519. return new Promise((resolve, reject) => {
  18520. stream.once('error', reject);
  18521. stream.on('data', (entry) => entries.push(entry));
  18522. stream.once('end', () => resolve(entries));
  18523. });
  18524. }
  18525. }
  18526. async$6.default = ReaderAsync;
  18527. var provider = {};
  18528. var deep = {};
  18529. var partial = {};
  18530. var matcher = {};
  18531. Object.defineProperty(matcher, "__esModule", { value: true });
  18532. const utils$5 = utils$g;
  18533. class Matcher {
  18534. constructor(_patterns, _settings, _micromatchOptions) {
  18535. this._patterns = _patterns;
  18536. this._settings = _settings;
  18537. this._micromatchOptions = _micromatchOptions;
  18538. this._storage = [];
  18539. this._fillStorage();
  18540. }
  18541. _fillStorage() {
  18542. for (const pattern of this._patterns) {
  18543. const segments = this._getPatternSegments(pattern);
  18544. const sections = this._splitSegmentsIntoSections(segments);
  18545. this._storage.push({
  18546. complete: sections.length <= 1,
  18547. pattern,
  18548. segments,
  18549. sections
  18550. });
  18551. }
  18552. }
  18553. _getPatternSegments(pattern) {
  18554. const parts = utils$5.pattern.getPatternParts(pattern, this._micromatchOptions);
  18555. return parts.map((part) => {
  18556. const dynamic = utils$5.pattern.isDynamicPattern(part, this._settings);
  18557. if (!dynamic) {
  18558. return {
  18559. dynamic: false,
  18560. pattern: part
  18561. };
  18562. }
  18563. return {
  18564. dynamic: true,
  18565. pattern: part,
  18566. patternRe: utils$5.pattern.makeRe(part, this._micromatchOptions)
  18567. };
  18568. });
  18569. }
  18570. _splitSegmentsIntoSections(segments) {
  18571. return utils$5.array.splitWhen(segments, (segment) => segment.dynamic && utils$5.pattern.hasGlobStar(segment.pattern));
  18572. }
  18573. }
  18574. matcher.default = Matcher;
  18575. Object.defineProperty(partial, "__esModule", { value: true });
  18576. const matcher_1 = matcher;
  18577. class PartialMatcher extends matcher_1.default {
  18578. match(filepath) {
  18579. const parts = filepath.split('/');
  18580. const levels = parts.length;
  18581. const patterns = this._storage.filter((info) => !info.complete || info.segments.length > levels);
  18582. for (const pattern of patterns) {
  18583. const section = pattern.sections[0];
  18584. /**
  18585. * In this case, the pattern has a globstar and we must read all directories unconditionally,
  18586. * but only if the level has reached the end of the first group.
  18587. *
  18588. * fixtures/{a,b}/**
  18589. * ^ true/false ^ always true
  18590. */
  18591. if (!pattern.complete && levels > section.length) {
  18592. return true;
  18593. }
  18594. const match = parts.every((part, index) => {
  18595. const segment = pattern.segments[index];
  18596. if (segment.dynamic && segment.patternRe.test(part)) {
  18597. return true;
  18598. }
  18599. if (!segment.dynamic && segment.pattern === part) {
  18600. return true;
  18601. }
  18602. return false;
  18603. });
  18604. if (match) {
  18605. return true;
  18606. }
  18607. }
  18608. return false;
  18609. }
  18610. }
  18611. partial.default = PartialMatcher;
  18612. Object.defineProperty(deep, "__esModule", { value: true });
  18613. const utils$4 = utils$g;
  18614. const partial_1 = partial;
  18615. class DeepFilter {
  18616. constructor(_settings, _micromatchOptions) {
  18617. this._settings = _settings;
  18618. this._micromatchOptions = _micromatchOptions;
  18619. }
  18620. getFilter(basePath, positive, negative) {
  18621. const matcher = this._getMatcher(positive);
  18622. const negativeRe = this._getNegativePatternsRe(negative);
  18623. return (entry) => this._filter(basePath, entry, matcher, negativeRe);
  18624. }
  18625. _getMatcher(patterns) {
  18626. return new partial_1.default(patterns, this._settings, this._micromatchOptions);
  18627. }
  18628. _getNegativePatternsRe(patterns) {
  18629. const affectDepthOfReadingPatterns = patterns.filter(utils$4.pattern.isAffectDepthOfReadingPattern);
  18630. return utils$4.pattern.convertPatternsToRe(affectDepthOfReadingPatterns, this._micromatchOptions);
  18631. }
  18632. _filter(basePath, entry, matcher, negativeRe) {
  18633. if (this._isSkippedByDeep(basePath, entry.path)) {
  18634. return false;
  18635. }
  18636. if (this._isSkippedSymbolicLink(entry)) {
  18637. return false;
  18638. }
  18639. const filepath = utils$4.path.removeLeadingDotSegment(entry.path);
  18640. if (this._isSkippedByPositivePatterns(filepath, matcher)) {
  18641. return false;
  18642. }
  18643. return this._isSkippedByNegativePatterns(filepath, negativeRe);
  18644. }
  18645. _isSkippedByDeep(basePath, entryPath) {
  18646. /**
  18647. * Avoid unnecessary depth calculations when it doesn't matter.
  18648. */
  18649. if (this._settings.deep === Infinity) {
  18650. return false;
  18651. }
  18652. return this._getEntryLevel(basePath, entryPath) >= this._settings.deep;
  18653. }
  18654. _getEntryLevel(basePath, entryPath) {
  18655. const entryPathDepth = entryPath.split('/').length;
  18656. if (basePath === '') {
  18657. return entryPathDepth;
  18658. }
  18659. const basePathDepth = basePath.split('/').length;
  18660. return entryPathDepth - basePathDepth;
  18661. }
  18662. _isSkippedSymbolicLink(entry) {
  18663. return !this._settings.followSymbolicLinks && entry.dirent.isSymbolicLink();
  18664. }
  18665. _isSkippedByPositivePatterns(entryPath, matcher) {
  18666. return !this._settings.baseNameMatch && !matcher.match(entryPath);
  18667. }
  18668. _isSkippedByNegativePatterns(entryPath, patternsRe) {
  18669. return !utils$4.pattern.matchAny(entryPath, patternsRe);
  18670. }
  18671. }
  18672. deep.default = DeepFilter;
  18673. var entry$1 = {};
  18674. Object.defineProperty(entry$1, "__esModule", { value: true });
  18675. const utils$3 = utils$g;
  18676. class EntryFilter {
  18677. constructor(_settings, _micromatchOptions) {
  18678. this._settings = _settings;
  18679. this._micromatchOptions = _micromatchOptions;
  18680. this.index = new Map();
  18681. }
  18682. getFilter(positive, negative) {
  18683. const positiveRe = utils$3.pattern.convertPatternsToRe(positive, this._micromatchOptions);
  18684. const negativeRe = utils$3.pattern.convertPatternsToRe(negative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true }));
  18685. return (entry) => this._filter(entry, positiveRe, negativeRe);
  18686. }
  18687. _filter(entry, positiveRe, negativeRe) {
  18688. const filepath = utils$3.path.removeLeadingDotSegment(entry.path);
  18689. if (this._settings.unique && this._isDuplicateEntry(filepath)) {
  18690. return false;
  18691. }
  18692. if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
  18693. return false;
  18694. }
  18695. if (this._isSkippedByAbsoluteNegativePatterns(filepath, negativeRe)) {
  18696. return false;
  18697. }
  18698. const isDirectory = entry.dirent.isDirectory();
  18699. const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(filepath, negativeRe, isDirectory);
  18700. if (this._settings.unique && isMatched) {
  18701. this._createIndexRecord(filepath);
  18702. }
  18703. return isMatched;
  18704. }
  18705. _isDuplicateEntry(filepath) {
  18706. return this.index.has(filepath);
  18707. }
  18708. _createIndexRecord(filepath) {
  18709. this.index.set(filepath, undefined);
  18710. }
  18711. _onlyFileFilter(entry) {
  18712. return this._settings.onlyFiles && !entry.dirent.isFile();
  18713. }
  18714. _onlyDirectoryFilter(entry) {
  18715. return this._settings.onlyDirectories && !entry.dirent.isDirectory();
  18716. }
  18717. _isSkippedByAbsoluteNegativePatterns(entryPath, patternsRe) {
  18718. if (!this._settings.absolute) {
  18719. return false;
  18720. }
  18721. const fullpath = utils$3.path.makeAbsolute(this._settings.cwd, entryPath);
  18722. return utils$3.pattern.matchAny(fullpath, patternsRe);
  18723. }
  18724. _isMatchToPatterns(filepath, patternsRe, isDirectory) {
  18725. // Trying to match files and directories by patterns.
  18726. const isMatched = utils$3.pattern.matchAny(filepath, patternsRe);
  18727. // A pattern with a trailling slash can be used for directory matching.
  18728. // To apply such pattern, we need to add a tralling slash to the path.
  18729. if (!isMatched && isDirectory) {
  18730. return utils$3.pattern.matchAny(filepath + '/', patternsRe);
  18731. }
  18732. return isMatched;
  18733. }
  18734. }
  18735. entry$1.default = EntryFilter;
  18736. var error$1 = {};
  18737. Object.defineProperty(error$1, "__esModule", { value: true });
  18738. const utils$2 = utils$g;
  18739. class ErrorFilter {
  18740. constructor(_settings) {
  18741. this._settings = _settings;
  18742. }
  18743. getFilter() {
  18744. return (error) => this._isNonFatalError(error);
  18745. }
  18746. _isNonFatalError(error) {
  18747. return utils$2.errno.isEnoentCodeError(error) || this._settings.suppressErrors;
  18748. }
  18749. }
  18750. error$1.default = ErrorFilter;
  18751. var entry = {};
  18752. Object.defineProperty(entry, "__esModule", { value: true });
  18753. const utils$1 = utils$g;
  18754. class EntryTransformer {
  18755. constructor(_settings) {
  18756. this._settings = _settings;
  18757. }
  18758. getTransformer() {
  18759. return (entry) => this._transform(entry);
  18760. }
  18761. _transform(entry) {
  18762. let filepath = entry.path;
  18763. if (this._settings.absolute) {
  18764. filepath = utils$1.path.makeAbsolute(this._settings.cwd, filepath);
  18765. filepath = utils$1.path.unixify(filepath);
  18766. }
  18767. if (this._settings.markDirectories && entry.dirent.isDirectory()) {
  18768. filepath += '/';
  18769. }
  18770. if (!this._settings.objectMode) {
  18771. return filepath;
  18772. }
  18773. return Object.assign(Object.assign({}, entry), { path: filepath });
  18774. }
  18775. }
  18776. entry.default = EntryTransformer;
  18777. Object.defineProperty(provider, "__esModule", { value: true });
  18778. const path$a = require$$0$4;
  18779. const deep_1 = deep;
  18780. const entry_1 = entry$1;
  18781. const error_1 = error$1;
  18782. const entry_2 = entry;
  18783. class Provider {
  18784. constructor(_settings) {
  18785. this._settings = _settings;
  18786. this.errorFilter = new error_1.default(this._settings);
  18787. this.entryFilter = new entry_1.default(this._settings, this._getMicromatchOptions());
  18788. this.deepFilter = new deep_1.default(this._settings, this._getMicromatchOptions());
  18789. this.entryTransformer = new entry_2.default(this._settings);
  18790. }
  18791. _getRootDirectory(task) {
  18792. return path$a.resolve(this._settings.cwd, task.base);
  18793. }
  18794. _getReaderOptions(task) {
  18795. const basePath = task.base === '.' ? '' : task.base;
  18796. return {
  18797. basePath,
  18798. pathSegmentSeparator: '/',
  18799. concurrency: this._settings.concurrency,
  18800. deepFilter: this.deepFilter.getFilter(basePath, task.positive, task.negative),
  18801. entryFilter: this.entryFilter.getFilter(task.positive, task.negative),
  18802. errorFilter: this.errorFilter.getFilter(),
  18803. followSymbolicLinks: this._settings.followSymbolicLinks,
  18804. fs: this._settings.fs,
  18805. stats: this._settings.stats,
  18806. throwErrorOnBrokenSymbolicLink: this._settings.throwErrorOnBrokenSymbolicLink,
  18807. transform: this.entryTransformer.getTransformer()
  18808. };
  18809. }
  18810. _getMicromatchOptions() {
  18811. return {
  18812. dot: this._settings.dot,
  18813. matchBase: this._settings.baseNameMatch,
  18814. nobrace: !this._settings.braceExpansion,
  18815. nocase: !this._settings.caseSensitiveMatch,
  18816. noext: !this._settings.extglob,
  18817. noglobstar: !this._settings.globstar,
  18818. posix: true,
  18819. strictSlashes: false
  18820. };
  18821. }
  18822. }
  18823. provider.default = Provider;
  18824. Object.defineProperty(async$7, "__esModule", { value: true });
  18825. const async_1$1 = async$6;
  18826. const provider_1$2 = provider;
  18827. class ProviderAsync extends provider_1$2.default {
  18828. constructor() {
  18829. super(...arguments);
  18830. this._reader = new async_1$1.default(this._settings);
  18831. }
  18832. async read(task) {
  18833. const root = this._getRootDirectory(task);
  18834. const options = this._getReaderOptions(task);
  18835. const entries = await this.api(root, task, options);
  18836. return entries.map((entry) => options.transform(entry));
  18837. }
  18838. api(root, task, options) {
  18839. if (task.dynamic) {
  18840. return this._reader.dynamic(root, options);
  18841. }
  18842. return this._reader.static(task.patterns, options);
  18843. }
  18844. }
  18845. async$7.default = ProviderAsync;
  18846. var stream = {};
  18847. Object.defineProperty(stream, "__esModule", { value: true });
  18848. const stream_1$1 = require$$0$7;
  18849. const stream_2 = stream$1;
  18850. const provider_1$1 = provider;
  18851. class ProviderStream extends provider_1$1.default {
  18852. constructor() {
  18853. super(...arguments);
  18854. this._reader = new stream_2.default(this._settings);
  18855. }
  18856. read(task) {
  18857. const root = this._getRootDirectory(task);
  18858. const options = this._getReaderOptions(task);
  18859. const source = this.api(root, task, options);
  18860. const destination = new stream_1$1.Readable({ objectMode: true, read: () => { } });
  18861. source
  18862. .once('error', (error) => destination.emit('error', error))
  18863. .on('data', (entry) => destination.emit('data', options.transform(entry)))
  18864. .once('end', () => destination.emit('end'));
  18865. destination
  18866. .once('close', () => source.destroy());
  18867. return destination;
  18868. }
  18869. api(root, task, options) {
  18870. if (task.dynamic) {
  18871. return this._reader.dynamic(root, options);
  18872. }
  18873. return this._reader.static(task.patterns, options);
  18874. }
  18875. }
  18876. stream.default = ProviderStream;
  18877. var sync$2 = {};
  18878. var sync$1 = {};
  18879. Object.defineProperty(sync$1, "__esModule", { value: true });
  18880. const fsStat = out$1;
  18881. const fsWalk = out$3;
  18882. const reader_1 = reader;
  18883. class ReaderSync extends reader_1.default {
  18884. constructor() {
  18885. super(...arguments);
  18886. this._walkSync = fsWalk.walkSync;
  18887. this._statSync = fsStat.statSync;
  18888. }
  18889. dynamic(root, options) {
  18890. return this._walkSync(root, options);
  18891. }
  18892. static(patterns, options) {
  18893. const entries = [];
  18894. for (const pattern of patterns) {
  18895. const filepath = this._getFullEntryPath(pattern);
  18896. const entry = this._getEntry(filepath, pattern, options);
  18897. if (entry === null || !options.entryFilter(entry)) {
  18898. continue;
  18899. }
  18900. entries.push(entry);
  18901. }
  18902. return entries;
  18903. }
  18904. _getEntry(filepath, pattern, options) {
  18905. try {
  18906. const stats = this._getStat(filepath);
  18907. return this._makeEntry(stats, pattern);
  18908. }
  18909. catch (error) {
  18910. if (options.errorFilter(error)) {
  18911. return null;
  18912. }
  18913. throw error;
  18914. }
  18915. }
  18916. _getStat(filepath) {
  18917. return this._statSync(filepath, this._fsStatSettings);
  18918. }
  18919. }
  18920. sync$1.default = ReaderSync;
  18921. Object.defineProperty(sync$2, "__esModule", { value: true });
  18922. const sync_1$1 = sync$1;
  18923. const provider_1 = provider;
  18924. class ProviderSync extends provider_1.default {
  18925. constructor() {
  18926. super(...arguments);
  18927. this._reader = new sync_1$1.default(this._settings);
  18928. }
  18929. read(task) {
  18930. const root = this._getRootDirectory(task);
  18931. const options = this._getReaderOptions(task);
  18932. const entries = this.api(root, task, options);
  18933. return entries.map(options.transform);
  18934. }
  18935. api(root, task, options) {
  18936. if (task.dynamic) {
  18937. return this._reader.dynamic(root, options);
  18938. }
  18939. return this._reader.static(task.patterns, options);
  18940. }
  18941. }
  18942. sync$2.default = ProviderSync;
  18943. var settings = {};
  18944. (function (exports) {
  18945. Object.defineProperty(exports, "__esModule", { value: true });
  18946. exports.DEFAULT_FILE_SYSTEM_ADAPTER = void 0;
  18947. const fs = require$$0__default;
  18948. const os = require$$2;
  18949. /**
  18950. * The `os.cpus` method can return zero. We expect the number of cores to be greater than zero.
  18951. * https://github.com/nodejs/node/blob/7faeddf23a98c53896f8b574a6e66589e8fb1eb8/lib/os.js#L106-L107
  18952. */
  18953. const CPU_COUNT = Math.max(os.cpus().length, 1);
  18954. exports.DEFAULT_FILE_SYSTEM_ADAPTER = {
  18955. lstat: fs.lstat,
  18956. lstatSync: fs.lstatSync,
  18957. stat: fs.stat,
  18958. statSync: fs.statSync,
  18959. readdir: fs.readdir,
  18960. readdirSync: fs.readdirSync
  18961. };
  18962. class Settings {
  18963. constructor(_options = {}) {
  18964. this._options = _options;
  18965. this.absolute = this._getValue(this._options.absolute, false);
  18966. this.baseNameMatch = this._getValue(this._options.baseNameMatch, false);
  18967. this.braceExpansion = this._getValue(this._options.braceExpansion, true);
  18968. this.caseSensitiveMatch = this._getValue(this._options.caseSensitiveMatch, true);
  18969. this.concurrency = this._getValue(this._options.concurrency, CPU_COUNT);
  18970. this.cwd = this._getValue(this._options.cwd, process.cwd());
  18971. this.deep = this._getValue(this._options.deep, Infinity);
  18972. this.dot = this._getValue(this._options.dot, false);
  18973. this.extglob = this._getValue(this._options.extglob, true);
  18974. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, true);
  18975. this.fs = this._getFileSystemMethods(this._options.fs);
  18976. this.globstar = this._getValue(this._options.globstar, true);
  18977. this.ignore = this._getValue(this._options.ignore, []);
  18978. this.markDirectories = this._getValue(this._options.markDirectories, false);
  18979. this.objectMode = this._getValue(this._options.objectMode, false);
  18980. this.onlyDirectories = this._getValue(this._options.onlyDirectories, false);
  18981. this.onlyFiles = this._getValue(this._options.onlyFiles, true);
  18982. this.stats = this._getValue(this._options.stats, false);
  18983. this.suppressErrors = this._getValue(this._options.suppressErrors, false);
  18984. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, false);
  18985. this.unique = this._getValue(this._options.unique, true);
  18986. if (this.onlyDirectories) {
  18987. this.onlyFiles = false;
  18988. }
  18989. if (this.stats) {
  18990. this.objectMode = true;
  18991. }
  18992. // Remove the cast to the array in the next major (#404).
  18993. this.ignore = [].concat(this.ignore);
  18994. }
  18995. _getValue(option, value) {
  18996. return option === undefined ? value : option;
  18997. }
  18998. _getFileSystemMethods(methods = {}) {
  18999. return Object.assign(Object.assign({}, exports.DEFAULT_FILE_SYSTEM_ADAPTER), methods);
  19000. }
  19001. }
  19002. exports.default = Settings;
  19003. } (settings));
  19004. const taskManager = tasks;
  19005. const async_1 = async$7;
  19006. const stream_1 = stream;
  19007. const sync_1 = sync$2;
  19008. const settings_1 = settings;
  19009. const utils = utils$g;
  19010. async function FastGlob(source, options) {
  19011. assertPatternsInput(source);
  19012. const works = getWorks(source, async_1.default, options);
  19013. const result = await Promise.all(works);
  19014. return utils.array.flatten(result);
  19015. }
  19016. // https://github.com/typescript-eslint/typescript-eslint/issues/60
  19017. // eslint-disable-next-line no-redeclare
  19018. (function (FastGlob) {
  19019. FastGlob.glob = FastGlob;
  19020. FastGlob.globSync = sync;
  19021. FastGlob.globStream = stream;
  19022. FastGlob.async = FastGlob;
  19023. function sync(source, options) {
  19024. assertPatternsInput(source);
  19025. const works = getWorks(source, sync_1.default, options);
  19026. return utils.array.flatten(works);
  19027. }
  19028. FastGlob.sync = sync;
  19029. function stream(source, options) {
  19030. assertPatternsInput(source);
  19031. const works = getWorks(source, stream_1.default, options);
  19032. /**
  19033. * The stream returned by the provider cannot work with an asynchronous iterator.
  19034. * To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
  19035. * This affects performance (+25%). I don't see best solution right now.
  19036. */
  19037. return utils.stream.merge(works);
  19038. }
  19039. FastGlob.stream = stream;
  19040. function generateTasks(source, options) {
  19041. assertPatternsInput(source);
  19042. const patterns = [].concat(source);
  19043. const settings = new settings_1.default(options);
  19044. return taskManager.generate(patterns, settings);
  19045. }
  19046. FastGlob.generateTasks = generateTasks;
  19047. function isDynamicPattern(source, options) {
  19048. assertPatternsInput(source);
  19049. const settings = new settings_1.default(options);
  19050. return utils.pattern.isDynamicPattern(source, settings);
  19051. }
  19052. FastGlob.isDynamicPattern = isDynamicPattern;
  19053. function escapePath(source) {
  19054. assertPatternsInput(source);
  19055. return utils.path.escape(source);
  19056. }
  19057. FastGlob.escapePath = escapePath;
  19058. function convertPathToPattern(source) {
  19059. assertPatternsInput(source);
  19060. return utils.path.convertPathToPattern(source);
  19061. }
  19062. FastGlob.convertPathToPattern = convertPathToPattern;
  19063. (function (posix) {
  19064. function escapePath(source) {
  19065. assertPatternsInput(source);
  19066. return utils.path.escapePosixPath(source);
  19067. }
  19068. posix.escapePath = escapePath;
  19069. function convertPathToPattern(source) {
  19070. assertPatternsInput(source);
  19071. return utils.path.convertPosixPathToPattern(source);
  19072. }
  19073. posix.convertPathToPattern = convertPathToPattern;
  19074. })(FastGlob.posix || (FastGlob.posix = {}));
  19075. (function (win32) {
  19076. function escapePath(source) {
  19077. assertPatternsInput(source);
  19078. return utils.path.escapeWindowsPath(source);
  19079. }
  19080. win32.escapePath = escapePath;
  19081. function convertPathToPattern(source) {
  19082. assertPatternsInput(source);
  19083. return utils.path.convertWindowsPathToPattern(source);
  19084. }
  19085. win32.convertPathToPattern = convertPathToPattern;
  19086. })(FastGlob.win32 || (FastGlob.win32 = {}));
  19087. })(FastGlob || (FastGlob = {}));
  19088. function getWorks(source, _Provider, options) {
  19089. const patterns = [].concat(source);
  19090. const settings = new settings_1.default(options);
  19091. const tasks = taskManager.generate(patterns, settings);
  19092. const provider = new _Provider(settings);
  19093. return tasks.map(provider.read, provider);
  19094. }
  19095. function assertPatternsInput(input) {
  19096. const source = [].concat(input);
  19097. const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
  19098. if (!isValidSource) {
  19099. throw new TypeError('Patterns must be a string (non empty) or an array of strings');
  19100. }
  19101. }
  19102. var out = FastGlob;
  19103. var glob = /*@__PURE__*/getDefaultExportFromCjs(out);
  19104. var dist = {};
  19105. (function (exports) {
  19106. Object.defineProperty(exports, "__esModule", { value: true });
  19107. exports.lilconfigSync = exports.lilconfig = exports.defaultLoaders = void 0;
  19108. const path = require$$0$4;
  19109. const fs = require$$0__default;
  19110. const os = require$$2;
  19111. const fsReadFileAsync = fs.promises.readFile;
  19112. function getDefaultSearchPlaces(name) {
  19113. return [
  19114. 'package.json',
  19115. `.${name}rc.json`,
  19116. `.${name}rc.js`,
  19117. `.${name}rc.cjs`,
  19118. `.config/${name}rc`,
  19119. `.config/${name}rc.json`,
  19120. `.config/${name}rc.js`,
  19121. `.config/${name}rc.cjs`,
  19122. `${name}.config.js`,
  19123. `${name}.config.cjs`,
  19124. ];
  19125. }
  19126. function parentDir(p) {
  19127. return path.dirname(p) || path.sep;
  19128. }
  19129. exports.defaultLoaders = Object.freeze({
  19130. '.js': __require,
  19131. '.json': __require,
  19132. '.cjs': __require,
  19133. noExt(_, content) {
  19134. return JSON.parse(content);
  19135. },
  19136. });
  19137. function getExtDesc(ext) {
  19138. return ext === 'noExt' ? 'files without extensions' : `extension "${ext}"`;
  19139. }
  19140. function getOptions(name, options = {}) {
  19141. const conf = {
  19142. stopDir: os.homedir(),
  19143. searchPlaces: getDefaultSearchPlaces(name),
  19144. ignoreEmptySearchPlaces: true,
  19145. cache: true,
  19146. transform: (x) => x,
  19147. packageProp: [name],
  19148. ...options,
  19149. loaders: { ...exports.defaultLoaders, ...options.loaders },
  19150. };
  19151. conf.searchPlaces.forEach(place => {
  19152. const key = path.extname(place) || 'noExt';
  19153. const loader = conf.loaders[key];
  19154. if (!loader) {
  19155. throw new Error(`No loader specified for ${getExtDesc(key)}, so searchPlaces item "${place}" is invalid`);
  19156. }
  19157. if (typeof loader !== 'function') {
  19158. throw new Error(`loader for ${getExtDesc(key)} is not a function (type provided: "${typeof loader}"), so searchPlaces item "${place}" is invalid`);
  19159. }
  19160. });
  19161. return conf;
  19162. }
  19163. function getPackageProp(props, obj) {
  19164. if (typeof props === 'string' && props in obj)
  19165. return obj[props];
  19166. return ((Array.isArray(props) ? props : props.split('.')).reduce((acc, prop) => (acc === undefined ? acc : acc[prop]), obj) || null);
  19167. }
  19168. function validateFilePath(filepath) {
  19169. if (!filepath)
  19170. throw new Error('load must pass a non-empty string');
  19171. }
  19172. function validateLoader(loader, ext) {
  19173. if (!loader)
  19174. throw new Error(`No loader specified for extension "${ext}"`);
  19175. if (typeof loader !== 'function')
  19176. throw new Error('loader is not a function');
  19177. }
  19178. const makeEmplace = (enableCache) => (c, filepath, res) => {
  19179. if (enableCache)
  19180. c.set(filepath, res);
  19181. return res;
  19182. };
  19183. function lilconfig(name, options) {
  19184. const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options);
  19185. const searchCache = new Map();
  19186. const loadCache = new Map();
  19187. const emplace = makeEmplace(cache);
  19188. return {
  19189. async search(searchFrom = process.cwd()) {
  19190. const result = {
  19191. config: null,
  19192. filepath: '',
  19193. };
  19194. const visited = new Set();
  19195. let dir = searchFrom;
  19196. dirLoop: while (true) {
  19197. if (cache) {
  19198. const r = searchCache.get(dir);
  19199. if (r !== undefined) {
  19200. for (const p of visited)
  19201. searchCache.set(p, r);
  19202. return r;
  19203. }
  19204. visited.add(dir);
  19205. }
  19206. for (const searchPlace of searchPlaces) {
  19207. const filepath = path.join(dir, searchPlace);
  19208. try {
  19209. await fs.promises.access(filepath);
  19210. }
  19211. catch (_a) {
  19212. continue;
  19213. }
  19214. const content = String(await fsReadFileAsync(filepath));
  19215. const loaderKey = path.extname(searchPlace) || 'noExt';
  19216. const loader = loaders[loaderKey];
  19217. if (searchPlace === 'package.json') {
  19218. const pkg = await loader(filepath, content);
  19219. const maybeConfig = getPackageProp(packageProp, pkg);
  19220. if (maybeConfig != null) {
  19221. result.config = maybeConfig;
  19222. result.filepath = filepath;
  19223. break dirLoop;
  19224. }
  19225. continue;
  19226. }
  19227. const isEmpty = content.trim() === '';
  19228. if (isEmpty && ignoreEmptySearchPlaces)
  19229. continue;
  19230. if (isEmpty) {
  19231. result.isEmpty = true;
  19232. result.config = undefined;
  19233. }
  19234. else {
  19235. validateLoader(loader, loaderKey);
  19236. result.config = await loader(filepath, content);
  19237. }
  19238. result.filepath = filepath;
  19239. break dirLoop;
  19240. }
  19241. if (dir === stopDir || dir === parentDir(dir))
  19242. break dirLoop;
  19243. dir = parentDir(dir);
  19244. }
  19245. const transformed = result.filepath === '' && result.config === null
  19246. ? transform(null)
  19247. : transform(result);
  19248. if (cache) {
  19249. for (const p of visited)
  19250. searchCache.set(p, transformed);
  19251. }
  19252. return transformed;
  19253. },
  19254. async load(filepath) {
  19255. validateFilePath(filepath);
  19256. const absPath = path.resolve(process.cwd(), filepath);
  19257. if (cache && loadCache.has(absPath)) {
  19258. return loadCache.get(absPath);
  19259. }
  19260. const { base, ext } = path.parse(absPath);
  19261. const loaderKey = ext || 'noExt';
  19262. const loader = loaders[loaderKey];
  19263. validateLoader(loader, loaderKey);
  19264. const content = String(await fsReadFileAsync(absPath));
  19265. if (base === 'package.json') {
  19266. const pkg = await loader(absPath, content);
  19267. return emplace(loadCache, absPath, transform({
  19268. config: getPackageProp(packageProp, pkg),
  19269. filepath: absPath,
  19270. }));
  19271. }
  19272. const result = {
  19273. config: null,
  19274. filepath: absPath,
  19275. };
  19276. const isEmpty = content.trim() === '';
  19277. if (isEmpty && ignoreEmptySearchPlaces)
  19278. return emplace(loadCache, absPath, transform({
  19279. config: undefined,
  19280. filepath: absPath,
  19281. isEmpty: true,
  19282. }));
  19283. result.config = isEmpty
  19284. ? undefined
  19285. : await loader(absPath, content);
  19286. return emplace(loadCache, absPath, transform(isEmpty ? { ...result, isEmpty, config: undefined } : result));
  19287. },
  19288. clearLoadCache() {
  19289. if (cache)
  19290. loadCache.clear();
  19291. },
  19292. clearSearchCache() {
  19293. if (cache)
  19294. searchCache.clear();
  19295. },
  19296. clearCaches() {
  19297. if (cache) {
  19298. loadCache.clear();
  19299. searchCache.clear();
  19300. }
  19301. },
  19302. };
  19303. }
  19304. exports.lilconfig = lilconfig;
  19305. function lilconfigSync(name, options) {
  19306. const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options);
  19307. const searchCache = new Map();
  19308. const loadCache = new Map();
  19309. const emplace = makeEmplace(cache);
  19310. return {
  19311. search(searchFrom = process.cwd()) {
  19312. const result = {
  19313. config: null,
  19314. filepath: '',
  19315. };
  19316. const visited = new Set();
  19317. let dir = searchFrom;
  19318. dirLoop: while (true) {
  19319. if (cache) {
  19320. const r = searchCache.get(dir);
  19321. if (r !== undefined) {
  19322. for (const p of visited)
  19323. searchCache.set(p, r);
  19324. return r;
  19325. }
  19326. visited.add(dir);
  19327. }
  19328. for (const searchPlace of searchPlaces) {
  19329. const filepath = path.join(dir, searchPlace);
  19330. try {
  19331. fs.accessSync(filepath);
  19332. }
  19333. catch (_a) {
  19334. continue;
  19335. }
  19336. const loaderKey = path.extname(searchPlace) || 'noExt';
  19337. const loader = loaders[loaderKey];
  19338. const content = String(fs.readFileSync(filepath));
  19339. if (searchPlace === 'package.json') {
  19340. const pkg = loader(filepath, content);
  19341. const maybeConfig = getPackageProp(packageProp, pkg);
  19342. if (maybeConfig != null) {
  19343. result.config = maybeConfig;
  19344. result.filepath = filepath;
  19345. break dirLoop;
  19346. }
  19347. continue;
  19348. }
  19349. const isEmpty = content.trim() === '';
  19350. if (isEmpty && ignoreEmptySearchPlaces)
  19351. continue;
  19352. if (isEmpty) {
  19353. result.isEmpty = true;
  19354. result.config = undefined;
  19355. }
  19356. else {
  19357. validateLoader(loader, loaderKey);
  19358. result.config = loader(filepath, content);
  19359. }
  19360. result.filepath = filepath;
  19361. break dirLoop;
  19362. }
  19363. if (dir === stopDir || dir === parentDir(dir))
  19364. break dirLoop;
  19365. dir = parentDir(dir);
  19366. }
  19367. const transformed = result.filepath === '' && result.config === null
  19368. ? transform(null)
  19369. : transform(result);
  19370. if (cache) {
  19371. for (const p of visited)
  19372. searchCache.set(p, transformed);
  19373. }
  19374. return transformed;
  19375. },
  19376. load(filepath) {
  19377. validateFilePath(filepath);
  19378. const absPath = path.resolve(process.cwd(), filepath);
  19379. if (cache && loadCache.has(absPath)) {
  19380. return loadCache.get(absPath);
  19381. }
  19382. const { base, ext } = path.parse(absPath);
  19383. const loaderKey = ext || 'noExt';
  19384. const loader = loaders[loaderKey];
  19385. validateLoader(loader, loaderKey);
  19386. const content = String(fs.readFileSync(absPath));
  19387. if (base === 'package.json') {
  19388. const pkg = loader(absPath, content);
  19389. return transform({
  19390. config: getPackageProp(packageProp, pkg),
  19391. filepath: absPath,
  19392. });
  19393. }
  19394. const result = {
  19395. config: null,
  19396. filepath: absPath,
  19397. };
  19398. const isEmpty = content.trim() === '';
  19399. if (isEmpty && ignoreEmptySearchPlaces)
  19400. return emplace(loadCache, absPath, transform({
  19401. filepath: absPath,
  19402. config: undefined,
  19403. isEmpty: true,
  19404. }));
  19405. result.config = isEmpty ? undefined : loader(absPath, content);
  19406. return emplace(loadCache, absPath, transform(isEmpty ? { ...result, isEmpty, config: undefined } : result));
  19407. },
  19408. clearLoadCache() {
  19409. if (cache)
  19410. loadCache.clear();
  19411. },
  19412. clearSearchCache() {
  19413. if (cache)
  19414. searchCache.clear();
  19415. },
  19416. clearCaches() {
  19417. if (cache) {
  19418. loadCache.clear();
  19419. searchCache.clear();
  19420. }
  19421. },
  19422. };
  19423. }
  19424. exports.lilconfigSync = lilconfigSync;
  19425. } (dist));
  19426. const ALIAS = Symbol.for('yaml.alias');
  19427. const DOC = Symbol.for('yaml.document');
  19428. const MAP = Symbol.for('yaml.map');
  19429. const PAIR = Symbol.for('yaml.pair');
  19430. const SCALAR$1 = Symbol.for('yaml.scalar');
  19431. const SEQ = Symbol.for('yaml.seq');
  19432. const NODE_TYPE = Symbol.for('yaml.node.type');
  19433. const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
  19434. const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
  19435. const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
  19436. const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
  19437. const isScalar$1 = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR$1;
  19438. const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
  19439. function isCollection$1(node) {
  19440. if (node && typeof node === 'object')
  19441. switch (node[NODE_TYPE]) {
  19442. case MAP:
  19443. case SEQ:
  19444. return true;
  19445. }
  19446. return false;
  19447. }
  19448. function isNode$1(node) {
  19449. if (node && typeof node === 'object')
  19450. switch (node[NODE_TYPE]) {
  19451. case ALIAS:
  19452. case MAP:
  19453. case SCALAR$1:
  19454. case SEQ:
  19455. return true;
  19456. }
  19457. return false;
  19458. }
  19459. const hasAnchor = (node) => (isScalar$1(node) || isCollection$1(node)) && !!node.anchor;
  19460. const BREAK$1 = Symbol('break visit');
  19461. const SKIP$1 = Symbol('skip children');
  19462. const REMOVE$1 = Symbol('remove node');
  19463. /**
  19464. * Apply a visitor to an AST node or document.
  19465. *
  19466. * Walks through the tree (depth-first) starting from `node`, calling a
  19467. * `visitor` function with three arguments:
  19468. * - `key`: For sequence values and map `Pair`, the node's index in the
  19469. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  19470. * `null` for the root node.
  19471. * - `node`: The current node.
  19472. * - `path`: The ancestry of the current node.
  19473. *
  19474. * The return value of the visitor may be used to control the traversal:
  19475. * - `undefined` (default): Do nothing and continue
  19476. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  19477. * sibling
  19478. * - `visit.BREAK`: Terminate traversal completely
  19479. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  19480. * - `Node`: Replace the current node, then continue by visiting it
  19481. * - `number`: While iterating the items of a sequence or map, set the index
  19482. * of the next step. This is useful especially if the index of the current
  19483. * node has changed.
  19484. *
  19485. * If `visitor` is a single function, it will be called with all values
  19486. * encountered in the tree, including e.g. `null` values. Alternatively,
  19487. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  19488. * `Alias` and `Scalar` node. To define the same visitor function for more than
  19489. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  19490. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  19491. * specific defined one will be used for each node.
  19492. */
  19493. function visit$1(node, visitor) {
  19494. const visitor_ = initVisitor(visitor);
  19495. if (isDocument(node)) {
  19496. const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
  19497. if (cd === REMOVE$1)
  19498. node.contents = null;
  19499. }
  19500. else
  19501. visit_(null, node, visitor_, Object.freeze([]));
  19502. }
  19503. // Without the `as symbol` casts, TS declares these in the `visit`
  19504. // namespace using `var`, but then complains about that because
  19505. // `unique symbol` must be `const`.
  19506. /** Terminate visit traversal completely */
  19507. visit$1.BREAK = BREAK$1;
  19508. /** Do not visit the children of the current node */
  19509. visit$1.SKIP = SKIP$1;
  19510. /** Remove the current node */
  19511. visit$1.REMOVE = REMOVE$1;
  19512. function visit_(key, node, visitor, path) {
  19513. const ctrl = callVisitor(key, node, visitor, path);
  19514. if (isNode$1(ctrl) || isPair(ctrl)) {
  19515. replaceNode(key, path, ctrl);
  19516. return visit_(key, ctrl, visitor, path);
  19517. }
  19518. if (typeof ctrl !== 'symbol') {
  19519. if (isCollection$1(node)) {
  19520. path = Object.freeze(path.concat(node));
  19521. for (let i = 0; i < node.items.length; ++i) {
  19522. const ci = visit_(i, node.items[i], visitor, path);
  19523. if (typeof ci === 'number')
  19524. i = ci - 1;
  19525. else if (ci === BREAK$1)
  19526. return BREAK$1;
  19527. else if (ci === REMOVE$1) {
  19528. node.items.splice(i, 1);
  19529. i -= 1;
  19530. }
  19531. }
  19532. }
  19533. else if (isPair(node)) {
  19534. path = Object.freeze(path.concat(node));
  19535. const ck = visit_('key', node.key, visitor, path);
  19536. if (ck === BREAK$1)
  19537. return BREAK$1;
  19538. else if (ck === REMOVE$1)
  19539. node.key = null;
  19540. const cv = visit_('value', node.value, visitor, path);
  19541. if (cv === BREAK$1)
  19542. return BREAK$1;
  19543. else if (cv === REMOVE$1)
  19544. node.value = null;
  19545. }
  19546. }
  19547. return ctrl;
  19548. }
  19549. /**
  19550. * Apply an async visitor to an AST node or document.
  19551. *
  19552. * Walks through the tree (depth-first) starting from `node`, calling a
  19553. * `visitor` function with three arguments:
  19554. * - `key`: For sequence values and map `Pair`, the node's index in the
  19555. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  19556. * `null` for the root node.
  19557. * - `node`: The current node.
  19558. * - `path`: The ancestry of the current node.
  19559. *
  19560. * The return value of the visitor may be used to control the traversal:
  19561. * - `Promise`: Must resolve to one of the following values
  19562. * - `undefined` (default): Do nothing and continue
  19563. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  19564. * sibling
  19565. * - `visit.BREAK`: Terminate traversal completely
  19566. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  19567. * - `Node`: Replace the current node, then continue by visiting it
  19568. * - `number`: While iterating the items of a sequence or map, set the index
  19569. * of the next step. This is useful especially if the index of the current
  19570. * node has changed.
  19571. *
  19572. * If `visitor` is a single function, it will be called with all values
  19573. * encountered in the tree, including e.g. `null` values. Alternatively,
  19574. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  19575. * `Alias` and `Scalar` node. To define the same visitor function for more than
  19576. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  19577. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  19578. * specific defined one will be used for each node.
  19579. */
  19580. async function visitAsync(node, visitor) {
  19581. const visitor_ = initVisitor(visitor);
  19582. if (isDocument(node)) {
  19583. const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
  19584. if (cd === REMOVE$1)
  19585. node.contents = null;
  19586. }
  19587. else
  19588. await visitAsync_(null, node, visitor_, Object.freeze([]));
  19589. }
  19590. // Without the `as symbol` casts, TS declares these in the `visit`
  19591. // namespace using `var`, but then complains about that because
  19592. // `unique symbol` must be `const`.
  19593. /** Terminate visit traversal completely */
  19594. visitAsync.BREAK = BREAK$1;
  19595. /** Do not visit the children of the current node */
  19596. visitAsync.SKIP = SKIP$1;
  19597. /** Remove the current node */
  19598. visitAsync.REMOVE = REMOVE$1;
  19599. async function visitAsync_(key, node, visitor, path) {
  19600. const ctrl = await callVisitor(key, node, visitor, path);
  19601. if (isNode$1(ctrl) || isPair(ctrl)) {
  19602. replaceNode(key, path, ctrl);
  19603. return visitAsync_(key, ctrl, visitor, path);
  19604. }
  19605. if (typeof ctrl !== 'symbol') {
  19606. if (isCollection$1(node)) {
  19607. path = Object.freeze(path.concat(node));
  19608. for (let i = 0; i < node.items.length; ++i) {
  19609. const ci = await visitAsync_(i, node.items[i], visitor, path);
  19610. if (typeof ci === 'number')
  19611. i = ci - 1;
  19612. else if (ci === BREAK$1)
  19613. return BREAK$1;
  19614. else if (ci === REMOVE$1) {
  19615. node.items.splice(i, 1);
  19616. i -= 1;
  19617. }
  19618. }
  19619. }
  19620. else if (isPair(node)) {
  19621. path = Object.freeze(path.concat(node));
  19622. const ck = await visitAsync_('key', node.key, visitor, path);
  19623. if (ck === BREAK$1)
  19624. return BREAK$1;
  19625. else if (ck === REMOVE$1)
  19626. node.key = null;
  19627. const cv = await visitAsync_('value', node.value, visitor, path);
  19628. if (cv === BREAK$1)
  19629. return BREAK$1;
  19630. else if (cv === REMOVE$1)
  19631. node.value = null;
  19632. }
  19633. }
  19634. return ctrl;
  19635. }
  19636. function initVisitor(visitor) {
  19637. if (typeof visitor === 'object' &&
  19638. (visitor.Collection || visitor.Node || visitor.Value)) {
  19639. return Object.assign({
  19640. Alias: visitor.Node,
  19641. Map: visitor.Node,
  19642. Scalar: visitor.Node,
  19643. Seq: visitor.Node
  19644. }, visitor.Value && {
  19645. Map: visitor.Value,
  19646. Scalar: visitor.Value,
  19647. Seq: visitor.Value
  19648. }, visitor.Collection && {
  19649. Map: visitor.Collection,
  19650. Seq: visitor.Collection
  19651. }, visitor);
  19652. }
  19653. return visitor;
  19654. }
  19655. function callVisitor(key, node, visitor, path) {
  19656. if (typeof visitor === 'function')
  19657. return visitor(key, node, path);
  19658. if (isMap(node))
  19659. return visitor.Map?.(key, node, path);
  19660. if (isSeq(node))
  19661. return visitor.Seq?.(key, node, path);
  19662. if (isPair(node))
  19663. return visitor.Pair?.(key, node, path);
  19664. if (isScalar$1(node))
  19665. return visitor.Scalar?.(key, node, path);
  19666. if (isAlias(node))
  19667. return visitor.Alias?.(key, node, path);
  19668. return undefined;
  19669. }
  19670. function replaceNode(key, path, node) {
  19671. const parent = path[path.length - 1];
  19672. if (isCollection$1(parent)) {
  19673. parent.items[key] = node;
  19674. }
  19675. else if (isPair(parent)) {
  19676. if (key === 'key')
  19677. parent.key = node;
  19678. else
  19679. parent.value = node;
  19680. }
  19681. else if (isDocument(parent)) {
  19682. parent.contents = node;
  19683. }
  19684. else {
  19685. const pt = isAlias(parent) ? 'alias' : 'scalar';
  19686. throw new Error(`Cannot replace node with ${pt} parent`);
  19687. }
  19688. }
  19689. const escapeChars = {
  19690. '!': '%21',
  19691. ',': '%2C',
  19692. '[': '%5B',
  19693. ']': '%5D',
  19694. '{': '%7B',
  19695. '}': '%7D'
  19696. };
  19697. const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
  19698. class Directives {
  19699. constructor(yaml, tags) {
  19700. /**
  19701. * The directives-end/doc-start marker `---`. If `null`, a marker may still be
  19702. * included in the document's stringified representation.
  19703. */
  19704. this.docStart = null;
  19705. /** The doc-end marker `...`. */
  19706. this.docEnd = false;
  19707. this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
  19708. this.tags = Object.assign({}, Directives.defaultTags, tags);
  19709. }
  19710. clone() {
  19711. const copy = new Directives(this.yaml, this.tags);
  19712. copy.docStart = this.docStart;
  19713. return copy;
  19714. }
  19715. /**
  19716. * During parsing, get a Directives instance for the current document and
  19717. * update the stream state according to the current version's spec.
  19718. */
  19719. atDocument() {
  19720. const res = new Directives(this.yaml, this.tags);
  19721. switch (this.yaml.version) {
  19722. case '1.1':
  19723. this.atNextDocument = true;
  19724. break;
  19725. case '1.2':
  19726. this.atNextDocument = false;
  19727. this.yaml = {
  19728. explicit: Directives.defaultYaml.explicit,
  19729. version: '1.2'
  19730. };
  19731. this.tags = Object.assign({}, Directives.defaultTags);
  19732. break;
  19733. }
  19734. return res;
  19735. }
  19736. /**
  19737. * @param onError - May be called even if the action was successful
  19738. * @returns `true` on success
  19739. */
  19740. add(line, onError) {
  19741. if (this.atNextDocument) {
  19742. this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
  19743. this.tags = Object.assign({}, Directives.defaultTags);
  19744. this.atNextDocument = false;
  19745. }
  19746. const parts = line.trim().split(/[ \t]+/);
  19747. const name = parts.shift();
  19748. switch (name) {
  19749. case '%TAG': {
  19750. if (parts.length !== 2) {
  19751. onError(0, '%TAG directive should contain exactly two parts');
  19752. if (parts.length < 2)
  19753. return false;
  19754. }
  19755. const [handle, prefix] = parts;
  19756. this.tags[handle] = prefix;
  19757. return true;
  19758. }
  19759. case '%YAML': {
  19760. this.yaml.explicit = true;
  19761. if (parts.length !== 1) {
  19762. onError(0, '%YAML directive should contain exactly one part');
  19763. return false;
  19764. }
  19765. const [version] = parts;
  19766. if (version === '1.1' || version === '1.2') {
  19767. this.yaml.version = version;
  19768. return true;
  19769. }
  19770. else {
  19771. const isValid = /^\d+\.\d+$/.test(version);
  19772. onError(6, `Unsupported YAML version ${version}`, isValid);
  19773. return false;
  19774. }
  19775. }
  19776. default:
  19777. onError(0, `Unknown directive ${name}`, true);
  19778. return false;
  19779. }
  19780. }
  19781. /**
  19782. * Resolves a tag, matching handles to those defined in %TAG directives.
  19783. *
  19784. * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
  19785. * `'!local'` tag, or `null` if unresolvable.
  19786. */
  19787. tagName(source, onError) {
  19788. if (source === '!')
  19789. return '!'; // non-specific tag
  19790. if (source[0] !== '!') {
  19791. onError(`Not a valid tag: ${source}`);
  19792. return null;
  19793. }
  19794. if (source[1] === '<') {
  19795. const verbatim = source.slice(2, -1);
  19796. if (verbatim === '!' || verbatim === '!!') {
  19797. onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
  19798. return null;
  19799. }
  19800. if (source[source.length - 1] !== '>')
  19801. onError('Verbatim tags must end with a >');
  19802. return verbatim;
  19803. }
  19804. const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/s);
  19805. if (!suffix)
  19806. onError(`The ${source} tag has no suffix`);
  19807. const prefix = this.tags[handle];
  19808. if (prefix) {
  19809. try {
  19810. return prefix + decodeURIComponent(suffix);
  19811. }
  19812. catch (error) {
  19813. onError(String(error));
  19814. return null;
  19815. }
  19816. }
  19817. if (handle === '!')
  19818. return source; // local tag
  19819. onError(`Could not resolve tag: ${source}`);
  19820. return null;
  19821. }
  19822. /**
  19823. * Given a fully resolved tag, returns its printable string form,
  19824. * taking into account current tag prefixes and defaults.
  19825. */
  19826. tagString(tag) {
  19827. for (const [handle, prefix] of Object.entries(this.tags)) {
  19828. if (tag.startsWith(prefix))
  19829. return handle + escapeTagName(tag.substring(prefix.length));
  19830. }
  19831. return tag[0] === '!' ? tag : `!<${tag}>`;
  19832. }
  19833. toString(doc) {
  19834. const lines = this.yaml.explicit
  19835. ? [`%YAML ${this.yaml.version || '1.2'}`]
  19836. : [];
  19837. const tagEntries = Object.entries(this.tags);
  19838. let tagNames;
  19839. if (doc && tagEntries.length > 0 && isNode$1(doc.contents)) {
  19840. const tags = {};
  19841. visit$1(doc.contents, (_key, node) => {
  19842. if (isNode$1(node) && node.tag)
  19843. tags[node.tag] = true;
  19844. });
  19845. tagNames = Object.keys(tags);
  19846. }
  19847. else
  19848. tagNames = [];
  19849. for (const [handle, prefix] of tagEntries) {
  19850. if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
  19851. continue;
  19852. if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
  19853. lines.push(`%TAG ${handle} ${prefix}`);
  19854. }
  19855. return lines.join('\n');
  19856. }
  19857. }
  19858. Directives.defaultYaml = { explicit: false, version: '1.2' };
  19859. Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
  19860. /**
  19861. * Verify that the input string is a valid anchor.
  19862. *
  19863. * Will throw on errors.
  19864. */
  19865. function anchorIsValid(anchor) {
  19866. if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
  19867. const sa = JSON.stringify(anchor);
  19868. const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
  19869. throw new Error(msg);
  19870. }
  19871. return true;
  19872. }
  19873. function anchorNames(root) {
  19874. const anchors = new Set();
  19875. visit$1(root, {
  19876. Value(_key, node) {
  19877. if (node.anchor)
  19878. anchors.add(node.anchor);
  19879. }
  19880. });
  19881. return anchors;
  19882. }
  19883. /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
  19884. function findNewAnchor(prefix, exclude) {
  19885. for (let i = 1; true; ++i) {
  19886. const name = `${prefix}${i}`;
  19887. if (!exclude.has(name))
  19888. return name;
  19889. }
  19890. }
  19891. function createNodeAnchors(doc, prefix) {
  19892. const aliasObjects = [];
  19893. const sourceObjects = new Map();
  19894. let prevAnchors = null;
  19895. return {
  19896. onAnchor: (source) => {
  19897. aliasObjects.push(source);
  19898. if (!prevAnchors)
  19899. prevAnchors = anchorNames(doc);
  19900. const anchor = findNewAnchor(prefix, prevAnchors);
  19901. prevAnchors.add(anchor);
  19902. return anchor;
  19903. },
  19904. /**
  19905. * With circular references, the source node is only resolved after all
  19906. * of its child nodes are. This is why anchors are set only after all of
  19907. * the nodes have been created.
  19908. */
  19909. setAnchors: () => {
  19910. for (const source of aliasObjects) {
  19911. const ref = sourceObjects.get(source);
  19912. if (typeof ref === 'object' &&
  19913. ref.anchor &&
  19914. (isScalar$1(ref.node) || isCollection$1(ref.node))) {
  19915. ref.node.anchor = ref.anchor;
  19916. }
  19917. else {
  19918. const error = new Error('Failed to resolve repeated object (this should not happen)');
  19919. error.source = source;
  19920. throw error;
  19921. }
  19922. }
  19923. },
  19924. sourceObjects
  19925. };
  19926. }
  19927. /**
  19928. * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
  19929. * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
  19930. * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
  19931. *
  19932. * Includes extensions for handling Map and Set objects.
  19933. */
  19934. function applyReviver(reviver, obj, key, val) {
  19935. if (val && typeof val === 'object') {
  19936. if (Array.isArray(val)) {
  19937. for (let i = 0, len = val.length; i < len; ++i) {
  19938. const v0 = val[i];
  19939. const v1 = applyReviver(reviver, val, String(i), v0);
  19940. if (v1 === undefined)
  19941. delete val[i];
  19942. else if (v1 !== v0)
  19943. val[i] = v1;
  19944. }
  19945. }
  19946. else if (val instanceof Map) {
  19947. for (const k of Array.from(val.keys())) {
  19948. const v0 = val.get(k);
  19949. const v1 = applyReviver(reviver, val, k, v0);
  19950. if (v1 === undefined)
  19951. val.delete(k);
  19952. else if (v1 !== v0)
  19953. val.set(k, v1);
  19954. }
  19955. }
  19956. else if (val instanceof Set) {
  19957. for (const v0 of Array.from(val)) {
  19958. const v1 = applyReviver(reviver, val, v0, v0);
  19959. if (v1 === undefined)
  19960. val.delete(v0);
  19961. else if (v1 !== v0) {
  19962. val.delete(v0);
  19963. val.add(v1);
  19964. }
  19965. }
  19966. }
  19967. else {
  19968. for (const [k, v0] of Object.entries(val)) {
  19969. const v1 = applyReviver(reviver, val, k, v0);
  19970. if (v1 === undefined)
  19971. delete val[k];
  19972. else if (v1 !== v0)
  19973. val[k] = v1;
  19974. }
  19975. }
  19976. }
  19977. return reviver.call(obj, key, val);
  19978. }
  19979. /**
  19980. * Recursively convert any node or its contents to native JavaScript
  19981. *
  19982. * @param value - The input value
  19983. * @param arg - If `value` defines a `toJSON()` method, use this
  19984. * as its first argument
  19985. * @param ctx - Conversion context, originally set in Document#toJS(). If
  19986. * `{ keep: true }` is not set, output should be suitable for JSON
  19987. * stringification.
  19988. */
  19989. function toJS(value, arg, ctx) {
  19990. // eslint-disable-next-line @typescript-eslint/no-unsafe-return
  19991. if (Array.isArray(value))
  19992. return value.map((v, i) => toJS(v, String(i), ctx));
  19993. if (value && typeof value.toJSON === 'function') {
  19994. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  19995. if (!ctx || !hasAnchor(value))
  19996. return value.toJSON(arg, ctx);
  19997. const data = { aliasCount: 0, count: 1, res: undefined };
  19998. ctx.anchors.set(value, data);
  19999. ctx.onCreate = res => {
  20000. data.res = res;
  20001. delete ctx.onCreate;
  20002. };
  20003. const res = value.toJSON(arg, ctx);
  20004. if (ctx.onCreate)
  20005. ctx.onCreate(res);
  20006. return res;
  20007. }
  20008. if (typeof value === 'bigint' && !ctx?.keep)
  20009. return Number(value);
  20010. return value;
  20011. }
  20012. class NodeBase {
  20013. constructor(type) {
  20014. Object.defineProperty(this, NODE_TYPE, { value: type });
  20015. }
  20016. /** Create a copy of this node. */
  20017. clone() {
  20018. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  20019. if (this.range)
  20020. copy.range = this.range.slice();
  20021. return copy;
  20022. }
  20023. /** A plain JavaScript representation of this node. */
  20024. toJS(doc, { mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  20025. if (!isDocument(doc))
  20026. throw new TypeError('A document argument is required');
  20027. const ctx = {
  20028. anchors: new Map(),
  20029. doc,
  20030. keep: true,
  20031. mapAsMap: mapAsMap === true,
  20032. mapKeyWarned: false,
  20033. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  20034. };
  20035. const res = toJS(this, '', ctx);
  20036. if (typeof onAnchor === 'function')
  20037. for (const { count, res } of ctx.anchors.values())
  20038. onAnchor(res, count);
  20039. return typeof reviver === 'function'
  20040. ? applyReviver(reviver, { '': res }, '', res)
  20041. : res;
  20042. }
  20043. }
  20044. class Alias extends NodeBase {
  20045. constructor(source) {
  20046. super(ALIAS);
  20047. this.source = source;
  20048. Object.defineProperty(this, 'tag', {
  20049. set() {
  20050. throw new Error('Alias nodes cannot have tags');
  20051. }
  20052. });
  20053. }
  20054. /**
  20055. * Resolve the value of this alias within `doc`, finding the last
  20056. * instance of the `source` anchor before this node.
  20057. */
  20058. resolve(doc) {
  20059. let found = undefined;
  20060. visit$1(doc, {
  20061. Node: (_key, node) => {
  20062. if (node === this)
  20063. return visit$1.BREAK;
  20064. if (node.anchor === this.source)
  20065. found = node;
  20066. }
  20067. });
  20068. return found;
  20069. }
  20070. toJSON(_arg, ctx) {
  20071. if (!ctx)
  20072. return { source: this.source };
  20073. const { anchors, doc, maxAliasCount } = ctx;
  20074. const source = this.resolve(doc);
  20075. if (!source) {
  20076. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  20077. throw new ReferenceError(msg);
  20078. }
  20079. let data = anchors.get(source);
  20080. if (!data) {
  20081. // Resolve anchors for Node.prototype.toJS()
  20082. toJS(source, null, ctx);
  20083. data = anchors.get(source);
  20084. }
  20085. /* istanbul ignore if */
  20086. if (!data || data.res === undefined) {
  20087. const msg = 'This should not happen: Alias anchor was not resolved?';
  20088. throw new ReferenceError(msg);
  20089. }
  20090. if (maxAliasCount >= 0) {
  20091. data.count += 1;
  20092. if (data.aliasCount === 0)
  20093. data.aliasCount = getAliasCount(doc, source, anchors);
  20094. if (data.count * data.aliasCount > maxAliasCount) {
  20095. const msg = 'Excessive alias count indicates a resource exhaustion attack';
  20096. throw new ReferenceError(msg);
  20097. }
  20098. }
  20099. return data.res;
  20100. }
  20101. toString(ctx, _onComment, _onChompKeep) {
  20102. const src = `*${this.source}`;
  20103. if (ctx) {
  20104. anchorIsValid(this.source);
  20105. if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
  20106. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  20107. throw new Error(msg);
  20108. }
  20109. if (ctx.implicitKey)
  20110. return `${src} `;
  20111. }
  20112. return src;
  20113. }
  20114. }
  20115. function getAliasCount(doc, node, anchors) {
  20116. if (isAlias(node)) {
  20117. const source = node.resolve(doc);
  20118. const anchor = anchors && source && anchors.get(source);
  20119. return anchor ? anchor.count * anchor.aliasCount : 0;
  20120. }
  20121. else if (isCollection$1(node)) {
  20122. let count = 0;
  20123. for (const item of node.items) {
  20124. const c = getAliasCount(doc, item, anchors);
  20125. if (c > count)
  20126. count = c;
  20127. }
  20128. return count;
  20129. }
  20130. else if (isPair(node)) {
  20131. const kc = getAliasCount(doc, node.key, anchors);
  20132. const vc = getAliasCount(doc, node.value, anchors);
  20133. return Math.max(kc, vc);
  20134. }
  20135. return 1;
  20136. }
  20137. const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
  20138. class Scalar extends NodeBase {
  20139. constructor(value) {
  20140. super(SCALAR$1);
  20141. this.value = value;
  20142. }
  20143. toJSON(arg, ctx) {
  20144. return ctx?.keep ? this.value : toJS(this.value, arg, ctx);
  20145. }
  20146. toString() {
  20147. return String(this.value);
  20148. }
  20149. }
  20150. Scalar.BLOCK_FOLDED = 'BLOCK_FOLDED';
  20151. Scalar.BLOCK_LITERAL = 'BLOCK_LITERAL';
  20152. Scalar.PLAIN = 'PLAIN';
  20153. Scalar.QUOTE_DOUBLE = 'QUOTE_DOUBLE';
  20154. Scalar.QUOTE_SINGLE = 'QUOTE_SINGLE';
  20155. const defaultTagPrefix = 'tag:yaml.org,2002:';
  20156. function findTagObject(value, tagName, tags) {
  20157. if (tagName) {
  20158. const match = tags.filter(t => t.tag === tagName);
  20159. const tagObj = match.find(t => !t.format) ?? match[0];
  20160. if (!tagObj)
  20161. throw new Error(`Tag ${tagName} not found`);
  20162. return tagObj;
  20163. }
  20164. return tags.find(t => t.identify?.(value) && !t.format);
  20165. }
  20166. function createNode(value, tagName, ctx) {
  20167. if (isDocument(value))
  20168. value = value.contents;
  20169. if (isNode$1(value))
  20170. return value;
  20171. if (isPair(value)) {
  20172. const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx);
  20173. map.items.push(value);
  20174. return map;
  20175. }
  20176. if (value instanceof String ||
  20177. value instanceof Number ||
  20178. value instanceof Boolean ||
  20179. (typeof BigInt !== 'undefined' && value instanceof BigInt) // not supported everywhere
  20180. ) {
  20181. // https://tc39.es/ecma262/#sec-serializejsonproperty
  20182. value = value.valueOf();
  20183. }
  20184. const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
  20185. // Detect duplicate references to the same object & use Alias nodes for all
  20186. // after first. The `ref` wrapper allows for circular references to resolve.
  20187. let ref = undefined;
  20188. if (aliasDuplicateObjects && value && typeof value === 'object') {
  20189. ref = sourceObjects.get(value);
  20190. if (ref) {
  20191. if (!ref.anchor)
  20192. ref.anchor = onAnchor(value);
  20193. return new Alias(ref.anchor);
  20194. }
  20195. else {
  20196. ref = { anchor: null, node: null };
  20197. sourceObjects.set(value, ref);
  20198. }
  20199. }
  20200. if (tagName?.startsWith('!!'))
  20201. tagName = defaultTagPrefix + tagName.slice(2);
  20202. let tagObj = findTagObject(value, tagName, schema.tags);
  20203. if (!tagObj) {
  20204. if (value && typeof value.toJSON === 'function') {
  20205. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  20206. value = value.toJSON();
  20207. }
  20208. if (!value || typeof value !== 'object') {
  20209. const node = new Scalar(value);
  20210. if (ref)
  20211. ref.node = node;
  20212. return node;
  20213. }
  20214. tagObj =
  20215. value instanceof Map
  20216. ? schema[MAP]
  20217. : Symbol.iterator in Object(value)
  20218. ? schema[SEQ]
  20219. : schema[MAP];
  20220. }
  20221. if (onTagObj) {
  20222. onTagObj(tagObj);
  20223. delete ctx.onTagObj;
  20224. }
  20225. const node = tagObj?.createNode
  20226. ? tagObj.createNode(ctx.schema, value, ctx)
  20227. : typeof tagObj?.nodeClass?.from === 'function'
  20228. ? tagObj.nodeClass.from(ctx.schema, value, ctx)
  20229. : new Scalar(value);
  20230. if (tagName)
  20231. node.tag = tagName;
  20232. else if (!tagObj.default)
  20233. node.tag = tagObj.tag;
  20234. if (ref)
  20235. ref.node = node;
  20236. return node;
  20237. }
  20238. function collectionFromPath(schema, path, value) {
  20239. let v = value;
  20240. for (let i = path.length - 1; i >= 0; --i) {
  20241. const k = path[i];
  20242. if (typeof k === 'number' && Number.isInteger(k) && k >= 0) {
  20243. const a = [];
  20244. a[k] = v;
  20245. v = a;
  20246. }
  20247. else {
  20248. v = new Map([[k, v]]);
  20249. }
  20250. }
  20251. return createNode(v, undefined, {
  20252. aliasDuplicateObjects: false,
  20253. keepUndefined: false,
  20254. onAnchor: () => {
  20255. throw new Error('This should not happen, please report a bug.');
  20256. },
  20257. schema,
  20258. sourceObjects: new Map()
  20259. });
  20260. }
  20261. // Type guard is intentionally a little wrong so as to be more useful,
  20262. // as it does not cover untypable empty non-string iterables (e.g. []).
  20263. const isEmptyPath = (path) => path == null ||
  20264. (typeof path === 'object' && !!path[Symbol.iterator]().next().done);
  20265. class Collection extends NodeBase {
  20266. constructor(type, schema) {
  20267. super(type);
  20268. Object.defineProperty(this, 'schema', {
  20269. value: schema,
  20270. configurable: true,
  20271. enumerable: false,
  20272. writable: true
  20273. });
  20274. }
  20275. /**
  20276. * Create a copy of this collection.
  20277. *
  20278. * @param schema - If defined, overwrites the original's schema
  20279. */
  20280. clone(schema) {
  20281. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  20282. if (schema)
  20283. copy.schema = schema;
  20284. copy.items = copy.items.map(it => isNode$1(it) || isPair(it) ? it.clone(schema) : it);
  20285. if (this.range)
  20286. copy.range = this.range.slice();
  20287. return copy;
  20288. }
  20289. /**
  20290. * Adds a value to the collection. For `!!map` and `!!omap` the value must
  20291. * be a Pair instance or a `{ key, value }` object, which may not have a key
  20292. * that already exists in the map.
  20293. */
  20294. addIn(path, value) {
  20295. if (isEmptyPath(path))
  20296. this.add(value);
  20297. else {
  20298. const [key, ...rest] = path;
  20299. const node = this.get(key, true);
  20300. if (isCollection$1(node))
  20301. node.addIn(rest, value);
  20302. else if (node === undefined && this.schema)
  20303. this.set(key, collectionFromPath(this.schema, rest, value));
  20304. else
  20305. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  20306. }
  20307. }
  20308. /**
  20309. * Removes a value from the collection.
  20310. * @returns `true` if the item was found and removed.
  20311. */
  20312. deleteIn(path) {
  20313. const [key, ...rest] = path;
  20314. if (rest.length === 0)
  20315. return this.delete(key);
  20316. const node = this.get(key, true);
  20317. if (isCollection$1(node))
  20318. return node.deleteIn(rest);
  20319. else
  20320. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  20321. }
  20322. /**
  20323. * Returns item at `key`, or `undefined` if not found. By default unwraps
  20324. * scalar values from their surrounding node; to disable set `keepScalar` to
  20325. * `true` (collections are always returned intact).
  20326. */
  20327. getIn(path, keepScalar) {
  20328. const [key, ...rest] = path;
  20329. const node = this.get(key, true);
  20330. if (rest.length === 0)
  20331. return !keepScalar && isScalar$1(node) ? node.value : node;
  20332. else
  20333. return isCollection$1(node) ? node.getIn(rest, keepScalar) : undefined;
  20334. }
  20335. hasAllNullValues(allowScalar) {
  20336. return this.items.every(node => {
  20337. if (!isPair(node))
  20338. return false;
  20339. const n = node.value;
  20340. return (n == null ||
  20341. (allowScalar &&
  20342. isScalar$1(n) &&
  20343. n.value == null &&
  20344. !n.commentBefore &&
  20345. !n.comment &&
  20346. !n.tag));
  20347. });
  20348. }
  20349. /**
  20350. * Checks if the collection includes a value with the key `key`.
  20351. */
  20352. hasIn(path) {
  20353. const [key, ...rest] = path;
  20354. if (rest.length === 0)
  20355. return this.has(key);
  20356. const node = this.get(key, true);
  20357. return isCollection$1(node) ? node.hasIn(rest) : false;
  20358. }
  20359. /**
  20360. * Sets a value in this collection. For `!!set`, `value` needs to be a
  20361. * boolean to add/remove the item from the set.
  20362. */
  20363. setIn(path, value) {
  20364. const [key, ...rest] = path;
  20365. if (rest.length === 0) {
  20366. this.set(key, value);
  20367. }
  20368. else {
  20369. const node = this.get(key, true);
  20370. if (isCollection$1(node))
  20371. node.setIn(rest, value);
  20372. else if (node === undefined && this.schema)
  20373. this.set(key, collectionFromPath(this.schema, rest, value));
  20374. else
  20375. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  20376. }
  20377. }
  20378. }
  20379. Collection.maxFlowStringSingleLineLength = 60;
  20380. /**
  20381. * Stringifies a comment.
  20382. *
  20383. * Empty comment lines are left empty,
  20384. * lines consisting of a single space are replaced by `#`,
  20385. * and all other lines are prefixed with a `#`.
  20386. */
  20387. const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
  20388. function indentComment(comment, indent) {
  20389. if (/^\n+$/.test(comment))
  20390. return comment.substring(1);
  20391. return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
  20392. }
  20393. const lineComment = (str, indent, comment) => str.endsWith('\n')
  20394. ? indentComment(comment, indent)
  20395. : comment.includes('\n')
  20396. ? '\n' + indentComment(comment, indent)
  20397. : (str.endsWith(' ') ? '' : ' ') + comment;
  20398. const FOLD_FLOW = 'flow';
  20399. const FOLD_BLOCK = 'block';
  20400. const FOLD_QUOTED = 'quoted';
  20401. /**
  20402. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  20403. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  20404. * terminated with `\n` and started with `indent`.
  20405. */
  20406. function foldFlowLines(text, indent, mode = 'flow', { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow } = {}) {
  20407. if (!lineWidth || lineWidth < 0)
  20408. return text;
  20409. const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  20410. if (text.length <= endStep)
  20411. return text;
  20412. const folds = [];
  20413. const escapedFolds = {};
  20414. let end = lineWidth - indent.length;
  20415. if (typeof indentAtStart === 'number') {
  20416. if (indentAtStart > lineWidth - Math.max(2, minContentWidth))
  20417. folds.push(0);
  20418. else
  20419. end = lineWidth - indentAtStart;
  20420. }
  20421. let split = undefined;
  20422. let prev = undefined;
  20423. let overflow = false;
  20424. let i = -1;
  20425. let escStart = -1;
  20426. let escEnd = -1;
  20427. if (mode === FOLD_BLOCK) {
  20428. i = consumeMoreIndentedLines(text, i);
  20429. if (i !== -1)
  20430. end = i + endStep;
  20431. }
  20432. for (let ch; (ch = text[(i += 1)]);) {
  20433. if (mode === FOLD_QUOTED && ch === '\\') {
  20434. escStart = i;
  20435. switch (text[i + 1]) {
  20436. case 'x':
  20437. i += 3;
  20438. break;
  20439. case 'u':
  20440. i += 5;
  20441. break;
  20442. case 'U':
  20443. i += 9;
  20444. break;
  20445. default:
  20446. i += 1;
  20447. }
  20448. escEnd = i;
  20449. }
  20450. if (ch === '\n') {
  20451. if (mode === FOLD_BLOCK)
  20452. i = consumeMoreIndentedLines(text, i);
  20453. end = i + endStep;
  20454. split = undefined;
  20455. }
  20456. else {
  20457. if (ch === ' ' &&
  20458. prev &&
  20459. prev !== ' ' &&
  20460. prev !== '\n' &&
  20461. prev !== '\t') {
  20462. // space surrounded by non-space can be replaced with newline + indent
  20463. const next = text[i + 1];
  20464. if (next && next !== ' ' && next !== '\n' && next !== '\t')
  20465. split = i;
  20466. }
  20467. if (i >= end) {
  20468. if (split) {
  20469. folds.push(split);
  20470. end = split + endStep;
  20471. split = undefined;
  20472. }
  20473. else if (mode === FOLD_QUOTED) {
  20474. // white-space collected at end may stretch past lineWidth
  20475. while (prev === ' ' || prev === '\t') {
  20476. prev = ch;
  20477. ch = text[(i += 1)];
  20478. overflow = true;
  20479. }
  20480. // Account for newline escape, but don't break preceding escape
  20481. const j = i > escEnd + 1 ? i - 2 : escStart - 1;
  20482. // Bail out if lineWidth & minContentWidth are shorter than an escape string
  20483. if (escapedFolds[j])
  20484. return text;
  20485. folds.push(j);
  20486. escapedFolds[j] = true;
  20487. end = j + endStep;
  20488. split = undefined;
  20489. }
  20490. else {
  20491. overflow = true;
  20492. }
  20493. }
  20494. }
  20495. prev = ch;
  20496. }
  20497. if (overflow && onOverflow)
  20498. onOverflow();
  20499. if (folds.length === 0)
  20500. return text;
  20501. if (onFold)
  20502. onFold();
  20503. let res = text.slice(0, folds[0]);
  20504. for (let i = 0; i < folds.length; ++i) {
  20505. const fold = folds[i];
  20506. const end = folds[i + 1] || text.length;
  20507. if (fold === 0)
  20508. res = `\n${indent}${text.slice(0, end)}`;
  20509. else {
  20510. if (mode === FOLD_QUOTED && escapedFolds[fold])
  20511. res += `${text[fold]}\\`;
  20512. res += `\n${indent}${text.slice(fold + 1, end)}`;
  20513. }
  20514. }
  20515. return res;
  20516. }
  20517. /**
  20518. * Presumes `i + 1` is at the start of a line
  20519. * @returns index of last newline in more-indented block
  20520. */
  20521. function consumeMoreIndentedLines(text, i) {
  20522. let ch = text[i + 1];
  20523. while (ch === ' ' || ch === '\t') {
  20524. do {
  20525. ch = text[(i += 1)];
  20526. } while (ch && ch !== '\n');
  20527. ch = text[i + 1];
  20528. }
  20529. return i;
  20530. }
  20531. const getFoldOptions = (ctx, isBlock) => ({
  20532. indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
  20533. lineWidth: ctx.options.lineWidth,
  20534. minContentWidth: ctx.options.minContentWidth
  20535. });
  20536. // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
  20537. // presume that's starting a new document.
  20538. const containsDocumentMarker = (str) => /^(%|---|\.\.\.)/m.test(str);
  20539. function lineLengthOverLimit(str, lineWidth, indentLength) {
  20540. if (!lineWidth || lineWidth < 0)
  20541. return false;
  20542. const limit = lineWidth - indentLength;
  20543. const strLen = str.length;
  20544. if (strLen <= limit)
  20545. return false;
  20546. for (let i = 0, start = 0; i < strLen; ++i) {
  20547. if (str[i] === '\n') {
  20548. if (i - start > limit)
  20549. return true;
  20550. start = i + 1;
  20551. if (strLen - start <= limit)
  20552. return false;
  20553. }
  20554. }
  20555. return true;
  20556. }
  20557. function doubleQuotedString(value, ctx) {
  20558. const json = JSON.stringify(value);
  20559. if (ctx.options.doubleQuotedAsJSON)
  20560. return json;
  20561. const { implicitKey } = ctx;
  20562. const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength;
  20563. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  20564. let str = '';
  20565. let start = 0;
  20566. for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
  20567. if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
  20568. // space before newline needs to be escaped to not be folded
  20569. str += json.slice(start, i) + '\\ ';
  20570. i += 1;
  20571. start = i;
  20572. ch = '\\';
  20573. }
  20574. if (ch === '\\')
  20575. switch (json[i + 1]) {
  20576. case 'u':
  20577. {
  20578. str += json.slice(start, i);
  20579. const code = json.substr(i + 2, 4);
  20580. switch (code) {
  20581. case '0000':
  20582. str += '\\0';
  20583. break;
  20584. case '0007':
  20585. str += '\\a';
  20586. break;
  20587. case '000b':
  20588. str += '\\v';
  20589. break;
  20590. case '001b':
  20591. str += '\\e';
  20592. break;
  20593. case '0085':
  20594. str += '\\N';
  20595. break;
  20596. case '00a0':
  20597. str += '\\_';
  20598. break;
  20599. case '2028':
  20600. str += '\\L';
  20601. break;
  20602. case '2029':
  20603. str += '\\P';
  20604. break;
  20605. default:
  20606. if (code.substr(0, 2) === '00')
  20607. str += '\\x' + code.substr(2);
  20608. else
  20609. str += json.substr(i, 6);
  20610. }
  20611. i += 5;
  20612. start = i + 1;
  20613. }
  20614. break;
  20615. case 'n':
  20616. if (implicitKey ||
  20617. json[i + 2] === '"' ||
  20618. json.length < minMultiLineLength) {
  20619. i += 1;
  20620. }
  20621. else {
  20622. // folding will eat first newline
  20623. str += json.slice(start, i) + '\n\n';
  20624. while (json[i + 2] === '\\' &&
  20625. json[i + 3] === 'n' &&
  20626. json[i + 4] !== '"') {
  20627. str += '\n';
  20628. i += 2;
  20629. }
  20630. str += indent;
  20631. // space after newline needs to be escaped to not be folded
  20632. if (json[i + 2] === ' ')
  20633. str += '\\';
  20634. i += 1;
  20635. start = i + 1;
  20636. }
  20637. break;
  20638. default:
  20639. i += 1;
  20640. }
  20641. }
  20642. str = start ? str + json.slice(start) : json;
  20643. return implicitKey
  20644. ? str
  20645. : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx, false));
  20646. }
  20647. function singleQuotedString(value, ctx) {
  20648. if (ctx.options.singleQuote === false ||
  20649. (ctx.implicitKey && value.includes('\n')) ||
  20650. /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
  20651. )
  20652. return doubleQuotedString(value, ctx);
  20653. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  20654. const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
  20655. return ctx.implicitKey
  20656. ? res
  20657. : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  20658. }
  20659. function quotedString(value, ctx) {
  20660. const { singleQuote } = ctx.options;
  20661. let qs;
  20662. if (singleQuote === false)
  20663. qs = doubleQuotedString;
  20664. else {
  20665. const hasDouble = value.includes('"');
  20666. const hasSingle = value.includes("'");
  20667. if (hasDouble && !hasSingle)
  20668. qs = singleQuotedString;
  20669. else if (hasSingle && !hasDouble)
  20670. qs = doubleQuotedString;
  20671. else
  20672. qs = singleQuote ? singleQuotedString : doubleQuotedString;
  20673. }
  20674. return qs(value, ctx);
  20675. }
  20676. // The negative lookbehind avoids a polynomial search,
  20677. // but isn't supported yet on Safari: https://caniuse.com/js-regexp-lookbehind
  20678. let blockEndNewlines;
  20679. try {
  20680. blockEndNewlines = new RegExp('(^|(?<!\n))\n+(?!\n|$)', 'g');
  20681. }
  20682. catch {
  20683. blockEndNewlines = /\n+(?!\n|$)/g;
  20684. }
  20685. function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
  20686. const { blockQuote, commentString, lineWidth } = ctx.options;
  20687. // 1. Block can't end in whitespace unless the last line is non-empty.
  20688. // 2. Strings consisting of only whitespace are best rendered explicitly.
  20689. if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
  20690. return quotedString(value, ctx);
  20691. }
  20692. const indent = ctx.indent ||
  20693. (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
  20694. const literal = blockQuote === 'literal'
  20695. ? true
  20696. : blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
  20697. ? false
  20698. : type === Scalar.BLOCK_LITERAL
  20699. ? true
  20700. : !lineLengthOverLimit(value, lineWidth, indent.length);
  20701. if (!value)
  20702. return literal ? '|\n' : '>\n';
  20703. // determine chomping from whitespace at value end
  20704. let chomp;
  20705. let endStart;
  20706. for (endStart = value.length; endStart > 0; --endStart) {
  20707. const ch = value[endStart - 1];
  20708. if (ch !== '\n' && ch !== '\t' && ch !== ' ')
  20709. break;
  20710. }
  20711. let end = value.substring(endStart);
  20712. const endNlPos = end.indexOf('\n');
  20713. if (endNlPos === -1) {
  20714. chomp = '-'; // strip
  20715. }
  20716. else if (value === end || endNlPos !== end.length - 1) {
  20717. chomp = '+'; // keep
  20718. if (onChompKeep)
  20719. onChompKeep();
  20720. }
  20721. else {
  20722. chomp = ''; // clip
  20723. }
  20724. if (end) {
  20725. value = value.slice(0, -end.length);
  20726. if (end[end.length - 1] === '\n')
  20727. end = end.slice(0, -1);
  20728. end = end.replace(blockEndNewlines, `$&${indent}`);
  20729. }
  20730. // determine indent indicator from whitespace at value start
  20731. let startWithSpace = false;
  20732. let startEnd;
  20733. let startNlPos = -1;
  20734. for (startEnd = 0; startEnd < value.length; ++startEnd) {
  20735. const ch = value[startEnd];
  20736. if (ch === ' ')
  20737. startWithSpace = true;
  20738. else if (ch === '\n')
  20739. startNlPos = startEnd;
  20740. else
  20741. break;
  20742. }
  20743. let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
  20744. if (start) {
  20745. value = value.substring(start.length);
  20746. start = start.replace(/\n+/g, `$&${indent}`);
  20747. }
  20748. const indentSize = indent ? '2' : '1'; // root is at -1
  20749. let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
  20750. if (comment) {
  20751. header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
  20752. if (onComment)
  20753. onComment();
  20754. }
  20755. if (literal) {
  20756. value = value.replace(/\n+/g, `$&${indent}`);
  20757. return `${header}\n${indent}${start}${value}${end}`;
  20758. }
  20759. value = value
  20760. .replace(/\n+/g, '\n$&')
  20761. .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
  20762. // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
  20763. .replace(/\n+/g, `$&${indent}`);
  20764. const body = foldFlowLines(`${start}${value}${end}`, indent, FOLD_BLOCK, getFoldOptions(ctx, true));
  20765. return `${header}\n${indent}${body}`;
  20766. }
  20767. function plainString(item, ctx, onComment, onChompKeep) {
  20768. const { type, value } = item;
  20769. const { actualString, implicitKey, indent, indentStep, inFlow } = ctx;
  20770. if ((implicitKey && value.includes('\n')) ||
  20771. (inFlow && /[[\]{},]/.test(value))) {
  20772. return quotedString(value, ctx);
  20773. }
  20774. if (!value ||
  20775. /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
  20776. // not allowed:
  20777. // - empty string, '-' or '?'
  20778. // - start with an indicator character (except [?:-]) or /[?-] /
  20779. // - '\n ', ': ' or ' \n' anywhere
  20780. // - '#' not preceded by a non-space char
  20781. // - end with ' ' or ':'
  20782. return implicitKey || inFlow || !value.includes('\n')
  20783. ? quotedString(value, ctx)
  20784. : blockString(item, ctx, onComment, onChompKeep);
  20785. }
  20786. if (!implicitKey &&
  20787. !inFlow &&
  20788. type !== Scalar.PLAIN &&
  20789. value.includes('\n')) {
  20790. // Where allowed & type not set explicitly, prefer block style for multiline strings
  20791. return blockString(item, ctx, onComment, onChompKeep);
  20792. }
  20793. if (containsDocumentMarker(value)) {
  20794. if (indent === '') {
  20795. ctx.forceBlockIndent = true;
  20796. return blockString(item, ctx, onComment, onChompKeep);
  20797. }
  20798. else if (implicitKey && indent === indentStep) {
  20799. return quotedString(value, ctx);
  20800. }
  20801. }
  20802. const str = value.replace(/\n+/g, `$&\n${indent}`);
  20803. // Verify that output will be parsed as a string, as e.g. plain numbers and
  20804. // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
  20805. // and others in v1.1.
  20806. if (actualString) {
  20807. const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
  20808. const { compat, tags } = ctx.doc.schema;
  20809. if (tags.some(test) || compat?.some(test))
  20810. return quotedString(value, ctx);
  20811. }
  20812. return implicitKey
  20813. ? str
  20814. : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  20815. }
  20816. function stringifyString(item, ctx, onComment, onChompKeep) {
  20817. const { implicitKey, inFlow } = ctx;
  20818. const ss = typeof item.value === 'string'
  20819. ? item
  20820. : Object.assign({}, item, { value: String(item.value) });
  20821. let { type } = item;
  20822. if (type !== Scalar.QUOTE_DOUBLE) {
  20823. // force double quotes on control characters & unpaired surrogates
  20824. if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
  20825. type = Scalar.QUOTE_DOUBLE;
  20826. }
  20827. const _stringify = (_type) => {
  20828. switch (_type) {
  20829. case Scalar.BLOCK_FOLDED:
  20830. case Scalar.BLOCK_LITERAL:
  20831. return implicitKey || inFlow
  20832. ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
  20833. : blockString(ss, ctx, onComment, onChompKeep);
  20834. case Scalar.QUOTE_DOUBLE:
  20835. return doubleQuotedString(ss.value, ctx);
  20836. case Scalar.QUOTE_SINGLE:
  20837. return singleQuotedString(ss.value, ctx);
  20838. case Scalar.PLAIN:
  20839. return plainString(ss, ctx, onComment, onChompKeep);
  20840. default:
  20841. return null;
  20842. }
  20843. };
  20844. let res = _stringify(type);
  20845. if (res === null) {
  20846. const { defaultKeyType, defaultStringType } = ctx.options;
  20847. const t = (implicitKey && defaultKeyType) || defaultStringType;
  20848. res = _stringify(t);
  20849. if (res === null)
  20850. throw new Error(`Unsupported default string type ${t}`);
  20851. }
  20852. return res;
  20853. }
  20854. function createStringifyContext(doc, options) {
  20855. const opt = Object.assign({
  20856. blockQuote: true,
  20857. commentString: stringifyComment,
  20858. defaultKeyType: null,
  20859. defaultStringType: 'PLAIN',
  20860. directives: null,
  20861. doubleQuotedAsJSON: false,
  20862. doubleQuotedMinMultiLineLength: 40,
  20863. falseStr: 'false',
  20864. flowCollectionPadding: true,
  20865. indentSeq: true,
  20866. lineWidth: 80,
  20867. minContentWidth: 20,
  20868. nullStr: 'null',
  20869. simpleKeys: false,
  20870. singleQuote: null,
  20871. trueStr: 'true',
  20872. verifyAliasOrder: true
  20873. }, doc.schema.toStringOptions, options);
  20874. let inFlow;
  20875. switch (opt.collectionStyle) {
  20876. case 'block':
  20877. inFlow = false;
  20878. break;
  20879. case 'flow':
  20880. inFlow = true;
  20881. break;
  20882. default:
  20883. inFlow = null;
  20884. }
  20885. return {
  20886. anchors: new Set(),
  20887. doc,
  20888. flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '',
  20889. indent: '',
  20890. indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
  20891. inFlow,
  20892. options: opt
  20893. };
  20894. }
  20895. function getTagObject(tags, item) {
  20896. if (item.tag) {
  20897. const match = tags.filter(t => t.tag === item.tag);
  20898. if (match.length > 0)
  20899. return match.find(t => t.format === item.format) ?? match[0];
  20900. }
  20901. let tagObj = undefined;
  20902. let obj;
  20903. if (isScalar$1(item)) {
  20904. obj = item.value;
  20905. const match = tags.filter(t => t.identify?.(obj));
  20906. tagObj =
  20907. match.find(t => t.format === item.format) ?? match.find(t => !t.format);
  20908. }
  20909. else {
  20910. obj = item;
  20911. tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
  20912. }
  20913. if (!tagObj) {
  20914. const name = obj?.constructor?.name ?? typeof obj;
  20915. throw new Error(`Tag not resolved for ${name} value`);
  20916. }
  20917. return tagObj;
  20918. }
  20919. // needs to be called before value stringifier to allow for circular anchor refs
  20920. function stringifyProps(node, tagObj, { anchors, doc }) {
  20921. if (!doc.directives)
  20922. return '';
  20923. const props = [];
  20924. const anchor = (isScalar$1(node) || isCollection$1(node)) && node.anchor;
  20925. if (anchor && anchorIsValid(anchor)) {
  20926. anchors.add(anchor);
  20927. props.push(`&${anchor}`);
  20928. }
  20929. const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
  20930. if (tag)
  20931. props.push(doc.directives.tagString(tag));
  20932. return props.join(' ');
  20933. }
  20934. function stringify$2(item, ctx, onComment, onChompKeep) {
  20935. if (isPair(item))
  20936. return item.toString(ctx, onComment, onChompKeep);
  20937. if (isAlias(item)) {
  20938. if (ctx.doc.directives)
  20939. return item.toString(ctx);
  20940. if (ctx.resolvedAliases?.has(item)) {
  20941. throw new TypeError(`Cannot stringify circular structure without alias nodes`);
  20942. }
  20943. else {
  20944. if (ctx.resolvedAliases)
  20945. ctx.resolvedAliases.add(item);
  20946. else
  20947. ctx.resolvedAliases = new Set([item]);
  20948. item = item.resolve(ctx.doc);
  20949. }
  20950. }
  20951. let tagObj = undefined;
  20952. const node = isNode$1(item)
  20953. ? item
  20954. : ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) });
  20955. if (!tagObj)
  20956. tagObj = getTagObject(ctx.doc.schema.tags, node);
  20957. const props = stringifyProps(node, tagObj, ctx);
  20958. if (props.length > 0)
  20959. ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
  20960. const str = typeof tagObj.stringify === 'function'
  20961. ? tagObj.stringify(node, ctx, onComment, onChompKeep)
  20962. : isScalar$1(node)
  20963. ? stringifyString(node, ctx, onComment, onChompKeep)
  20964. : node.toString(ctx, onComment, onChompKeep);
  20965. if (!props)
  20966. return str;
  20967. return isScalar$1(node) || str[0] === '{' || str[0] === '['
  20968. ? `${props} ${str}`
  20969. : `${props}\n${ctx.indent}${str}`;
  20970. }
  20971. function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
  20972. const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
  20973. let keyComment = (isNode$1(key) && key.comment) || null;
  20974. if (simpleKeys) {
  20975. if (keyComment) {
  20976. throw new Error('With simple keys, key nodes cannot have comments');
  20977. }
  20978. if (isCollection$1(key)) {
  20979. const msg = 'With simple keys, collection cannot be used as a key value';
  20980. throw new Error(msg);
  20981. }
  20982. }
  20983. let explicitKey = !simpleKeys &&
  20984. (!key ||
  20985. (keyComment && value == null && !ctx.inFlow) ||
  20986. isCollection$1(key) ||
  20987. (isScalar$1(key)
  20988. ? key.type === Scalar.BLOCK_FOLDED || key.type === Scalar.BLOCK_LITERAL
  20989. : typeof key === 'object'));
  20990. ctx = Object.assign({}, ctx, {
  20991. allNullValues: false,
  20992. implicitKey: !explicitKey && (simpleKeys || !allNullValues),
  20993. indent: indent + indentStep
  20994. });
  20995. let keyCommentDone = false;
  20996. let chompKeep = false;
  20997. let str = stringify$2(key, ctx, () => (keyCommentDone = true), () => (chompKeep = true));
  20998. if (!explicitKey && !ctx.inFlow && str.length > 1024) {
  20999. if (simpleKeys)
  21000. throw new Error('With simple keys, single line scalar must not span more than 1024 characters');
  21001. explicitKey = true;
  21002. }
  21003. if (ctx.inFlow) {
  21004. if (allNullValues || value == null) {
  21005. if (keyCommentDone && onComment)
  21006. onComment();
  21007. return str === '' ? '?' : explicitKey ? `? ${str}` : str;
  21008. }
  21009. }
  21010. else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
  21011. str = `? ${str}`;
  21012. if (keyComment && !keyCommentDone) {
  21013. str += lineComment(str, ctx.indent, commentString(keyComment));
  21014. }
  21015. else if (chompKeep && onChompKeep)
  21016. onChompKeep();
  21017. return str;
  21018. }
  21019. if (keyCommentDone)
  21020. keyComment = null;
  21021. if (explicitKey) {
  21022. if (keyComment)
  21023. str += lineComment(str, ctx.indent, commentString(keyComment));
  21024. str = `? ${str}\n${indent}:`;
  21025. }
  21026. else {
  21027. str = `${str}:`;
  21028. if (keyComment)
  21029. str += lineComment(str, ctx.indent, commentString(keyComment));
  21030. }
  21031. let vsb, vcb, valueComment;
  21032. if (isNode$1(value)) {
  21033. vsb = !!value.spaceBefore;
  21034. vcb = value.commentBefore;
  21035. valueComment = value.comment;
  21036. }
  21037. else {
  21038. vsb = false;
  21039. vcb = null;
  21040. valueComment = null;
  21041. if (value && typeof value === 'object')
  21042. value = doc.createNode(value);
  21043. }
  21044. ctx.implicitKey = false;
  21045. if (!explicitKey && !keyComment && isScalar$1(value))
  21046. ctx.indentAtStart = str.length + 1;
  21047. chompKeep = false;
  21048. if (!indentSeq &&
  21049. indentStep.length >= 2 &&
  21050. !ctx.inFlow &&
  21051. !explicitKey &&
  21052. isSeq(value) &&
  21053. !value.flow &&
  21054. !value.tag &&
  21055. !value.anchor) {
  21056. // If indentSeq === false, consider '- ' as part of indentation where possible
  21057. ctx.indent = ctx.indent.substring(2);
  21058. }
  21059. let valueCommentDone = false;
  21060. const valueStr = stringify$2(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
  21061. let ws = ' ';
  21062. if (keyComment || vsb || vcb) {
  21063. ws = vsb ? '\n' : '';
  21064. if (vcb) {
  21065. const cs = commentString(vcb);
  21066. ws += `\n${indentComment(cs, ctx.indent)}`;
  21067. }
  21068. if (valueStr === '' && !ctx.inFlow) {
  21069. if (ws === '\n')
  21070. ws = '\n\n';
  21071. }
  21072. else {
  21073. ws += `\n${ctx.indent}`;
  21074. }
  21075. }
  21076. else if (!explicitKey && isCollection$1(value)) {
  21077. const vs0 = valueStr[0];
  21078. const nl0 = valueStr.indexOf('\n');
  21079. const hasNewline = nl0 !== -1;
  21080. const flow = ctx.inFlow ?? value.flow ?? value.items.length === 0;
  21081. if (hasNewline || !flow) {
  21082. let hasPropsLine = false;
  21083. if (hasNewline && (vs0 === '&' || vs0 === '!')) {
  21084. let sp0 = valueStr.indexOf(' ');
  21085. if (vs0 === '&' &&
  21086. sp0 !== -1 &&
  21087. sp0 < nl0 &&
  21088. valueStr[sp0 + 1] === '!') {
  21089. sp0 = valueStr.indexOf(' ', sp0 + 1);
  21090. }
  21091. if (sp0 === -1 || nl0 < sp0)
  21092. hasPropsLine = true;
  21093. }
  21094. if (!hasPropsLine)
  21095. ws = `\n${ctx.indent}`;
  21096. }
  21097. }
  21098. else if (valueStr === '' || valueStr[0] === '\n') {
  21099. ws = '';
  21100. }
  21101. str += ws + valueStr;
  21102. if (ctx.inFlow) {
  21103. if (valueCommentDone && onComment)
  21104. onComment();
  21105. }
  21106. else if (valueComment && !valueCommentDone) {
  21107. str += lineComment(str, ctx.indent, commentString(valueComment));
  21108. }
  21109. else if (chompKeep && onChompKeep) {
  21110. onChompKeep();
  21111. }
  21112. return str;
  21113. }
  21114. function warn(logLevel, warning) {
  21115. if (logLevel === 'debug' || logLevel === 'warn') {
  21116. // https://github.com/typescript-eslint/typescript-eslint/issues/7478
  21117. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  21118. if (typeof process !== 'undefined' && process.emitWarning)
  21119. process.emitWarning(warning);
  21120. else
  21121. console.warn(warning);
  21122. }
  21123. }
  21124. const MERGE_KEY = '<<';
  21125. function addPairToJSMap(ctx, map, { key, value }) {
  21126. if (ctx?.doc.schema.merge && isMergeKey(key)) {
  21127. value = isAlias(value) ? value.resolve(ctx.doc) : value;
  21128. if (isSeq(value))
  21129. for (const it of value.items)
  21130. mergeToJSMap(ctx, map, it);
  21131. else if (Array.isArray(value))
  21132. for (const it of value)
  21133. mergeToJSMap(ctx, map, it);
  21134. else
  21135. mergeToJSMap(ctx, map, value);
  21136. }
  21137. else {
  21138. const jsKey = toJS(key, '', ctx);
  21139. if (map instanceof Map) {
  21140. map.set(jsKey, toJS(value, jsKey, ctx));
  21141. }
  21142. else if (map instanceof Set) {
  21143. map.add(jsKey);
  21144. }
  21145. else {
  21146. const stringKey = stringifyKey(key, jsKey, ctx);
  21147. const jsValue = toJS(value, stringKey, ctx);
  21148. if (stringKey in map)
  21149. Object.defineProperty(map, stringKey, {
  21150. value: jsValue,
  21151. writable: true,
  21152. enumerable: true,
  21153. configurable: true
  21154. });
  21155. else
  21156. map[stringKey] = jsValue;
  21157. }
  21158. }
  21159. return map;
  21160. }
  21161. const isMergeKey = (key) => key === MERGE_KEY ||
  21162. (isScalar$1(key) &&
  21163. key.value === MERGE_KEY &&
  21164. (!key.type || key.type === Scalar.PLAIN));
  21165. // If the value associated with a merge key is a single mapping node, each of
  21166. // its key/value pairs is inserted into the current mapping, unless the key
  21167. // already exists in it. If the value associated with the merge key is a
  21168. // sequence, then this sequence is expected to contain mapping nodes and each
  21169. // of these nodes is merged in turn according to its order in the sequence.
  21170. // Keys in mapping nodes earlier in the sequence override keys specified in
  21171. // later mapping nodes. -- http://yaml.org/type/merge.html
  21172. function mergeToJSMap(ctx, map, value) {
  21173. const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
  21174. if (!isMap(source))
  21175. throw new Error('Merge sources must be maps or map aliases');
  21176. const srcMap = source.toJSON(null, ctx, Map);
  21177. for (const [key, value] of srcMap) {
  21178. if (map instanceof Map) {
  21179. if (!map.has(key))
  21180. map.set(key, value);
  21181. }
  21182. else if (map instanceof Set) {
  21183. map.add(key);
  21184. }
  21185. else if (!Object.prototype.hasOwnProperty.call(map, key)) {
  21186. Object.defineProperty(map, key, {
  21187. value,
  21188. writable: true,
  21189. enumerable: true,
  21190. configurable: true
  21191. });
  21192. }
  21193. }
  21194. return map;
  21195. }
  21196. function stringifyKey(key, jsKey, ctx) {
  21197. if (jsKey === null)
  21198. return '';
  21199. if (typeof jsKey !== 'object')
  21200. return String(jsKey);
  21201. if (isNode$1(key) && ctx?.doc) {
  21202. const strCtx = createStringifyContext(ctx.doc, {});
  21203. strCtx.anchors = new Set();
  21204. for (const node of ctx.anchors.keys())
  21205. strCtx.anchors.add(node.anchor);
  21206. strCtx.inFlow = true;
  21207. strCtx.inStringifyKey = true;
  21208. const strKey = key.toString(strCtx);
  21209. if (!ctx.mapKeyWarned) {
  21210. let jsonStr = JSON.stringify(strKey);
  21211. if (jsonStr.length > 40)
  21212. jsonStr = jsonStr.substring(0, 36) + '..."';
  21213. warn(ctx.doc.options.logLevel, `Keys with collection values will be stringified due to JS Object restrictions: ${jsonStr}. Set mapAsMap: true to use object keys.`);
  21214. ctx.mapKeyWarned = true;
  21215. }
  21216. return strKey;
  21217. }
  21218. return JSON.stringify(jsKey);
  21219. }
  21220. function createPair(key, value, ctx) {
  21221. const k = createNode(key, undefined, ctx);
  21222. const v = createNode(value, undefined, ctx);
  21223. return new Pair(k, v);
  21224. }
  21225. class Pair {
  21226. constructor(key, value = null) {
  21227. Object.defineProperty(this, NODE_TYPE, { value: PAIR });
  21228. this.key = key;
  21229. this.value = value;
  21230. }
  21231. clone(schema) {
  21232. let { key, value } = this;
  21233. if (isNode$1(key))
  21234. key = key.clone(schema);
  21235. if (isNode$1(value))
  21236. value = value.clone(schema);
  21237. return new Pair(key, value);
  21238. }
  21239. toJSON(_, ctx) {
  21240. const pair = ctx?.mapAsMap ? new Map() : {};
  21241. return addPairToJSMap(ctx, pair, this);
  21242. }
  21243. toString(ctx, onComment, onChompKeep) {
  21244. return ctx?.doc
  21245. ? stringifyPair(this, ctx, onComment, onChompKeep)
  21246. : JSON.stringify(this);
  21247. }
  21248. }
  21249. function stringifyCollection(collection, ctx, options) {
  21250. const flow = ctx.inFlow ?? collection.flow;
  21251. const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
  21252. return stringify(collection, ctx, options);
  21253. }
  21254. function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
  21255. const { indent, options: { commentString } } = ctx;
  21256. const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
  21257. let chompKeep = false; // flag for the preceding node's status
  21258. const lines = [];
  21259. for (let i = 0; i < items.length; ++i) {
  21260. const item = items[i];
  21261. let comment = null;
  21262. if (isNode$1(item)) {
  21263. if (!chompKeep && item.spaceBefore)
  21264. lines.push('');
  21265. addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
  21266. if (item.comment)
  21267. comment = item.comment;
  21268. }
  21269. else if (isPair(item)) {
  21270. const ik = isNode$1(item.key) ? item.key : null;
  21271. if (ik) {
  21272. if (!chompKeep && ik.spaceBefore)
  21273. lines.push('');
  21274. addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
  21275. }
  21276. }
  21277. chompKeep = false;
  21278. let str = stringify$2(item, itemCtx, () => (comment = null), () => (chompKeep = true));
  21279. if (comment)
  21280. str += lineComment(str, itemIndent, commentString(comment));
  21281. if (chompKeep && comment)
  21282. chompKeep = false;
  21283. lines.push(blockItemPrefix + str);
  21284. }
  21285. let str;
  21286. if (lines.length === 0) {
  21287. str = flowChars.start + flowChars.end;
  21288. }
  21289. else {
  21290. str = lines[0];
  21291. for (let i = 1; i < lines.length; ++i) {
  21292. const line = lines[i];
  21293. str += line ? `\n${indent}${line}` : '\n';
  21294. }
  21295. }
  21296. if (comment) {
  21297. str += '\n' + indentComment(commentString(comment), indent);
  21298. if (onComment)
  21299. onComment();
  21300. }
  21301. else if (chompKeep && onChompKeep)
  21302. onChompKeep();
  21303. return str;
  21304. }
  21305. function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
  21306. const { indent, indentStep, flowCollectionPadding: fcPadding, options: { commentString } } = ctx;
  21307. itemIndent += indentStep;
  21308. const itemCtx = Object.assign({}, ctx, {
  21309. indent: itemIndent,
  21310. inFlow: true,
  21311. type: null
  21312. });
  21313. let reqNewline = false;
  21314. let linesAtValue = 0;
  21315. const lines = [];
  21316. for (let i = 0; i < items.length; ++i) {
  21317. const item = items[i];
  21318. let comment = null;
  21319. if (isNode$1(item)) {
  21320. if (item.spaceBefore)
  21321. lines.push('');
  21322. addCommentBefore(ctx, lines, item.commentBefore, false);
  21323. if (item.comment)
  21324. comment = item.comment;
  21325. }
  21326. else if (isPair(item)) {
  21327. const ik = isNode$1(item.key) ? item.key : null;
  21328. if (ik) {
  21329. if (ik.spaceBefore)
  21330. lines.push('');
  21331. addCommentBefore(ctx, lines, ik.commentBefore, false);
  21332. if (ik.comment)
  21333. reqNewline = true;
  21334. }
  21335. const iv = isNode$1(item.value) ? item.value : null;
  21336. if (iv) {
  21337. if (iv.comment)
  21338. comment = iv.comment;
  21339. if (iv.commentBefore)
  21340. reqNewline = true;
  21341. }
  21342. else if (item.value == null && ik?.comment) {
  21343. comment = ik.comment;
  21344. }
  21345. }
  21346. if (comment)
  21347. reqNewline = true;
  21348. let str = stringify$2(item, itemCtx, () => (comment = null));
  21349. if (i < items.length - 1)
  21350. str += ',';
  21351. if (comment)
  21352. str += lineComment(str, itemIndent, commentString(comment));
  21353. if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
  21354. reqNewline = true;
  21355. lines.push(str);
  21356. linesAtValue = lines.length;
  21357. }
  21358. let str;
  21359. const { start, end } = flowChars;
  21360. if (lines.length === 0) {
  21361. str = start + end;
  21362. }
  21363. else {
  21364. if (!reqNewline) {
  21365. const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
  21366. reqNewline = len > Collection.maxFlowStringSingleLineLength;
  21367. }
  21368. if (reqNewline) {
  21369. str = start;
  21370. for (const line of lines)
  21371. str += line ? `\n${indentStep}${indent}${line}` : '\n';
  21372. str += `\n${indent}${end}`;
  21373. }
  21374. else {
  21375. str = `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`;
  21376. }
  21377. }
  21378. if (comment) {
  21379. str += lineComment(str, indent, commentString(comment));
  21380. if (onComment)
  21381. onComment();
  21382. }
  21383. return str;
  21384. }
  21385. function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
  21386. if (comment && chompKeep)
  21387. comment = comment.replace(/^\n+/, '');
  21388. if (comment) {
  21389. const ic = indentComment(commentString(comment), indent);
  21390. lines.push(ic.trimStart()); // Avoid double indent on first line
  21391. }
  21392. }
  21393. function findPair(items, key) {
  21394. const k = isScalar$1(key) ? key.value : key;
  21395. for (const it of items) {
  21396. if (isPair(it)) {
  21397. if (it.key === key || it.key === k)
  21398. return it;
  21399. if (isScalar$1(it.key) && it.key.value === k)
  21400. return it;
  21401. }
  21402. }
  21403. return undefined;
  21404. }
  21405. class YAMLMap extends Collection {
  21406. static get tagName() {
  21407. return 'tag:yaml.org,2002:map';
  21408. }
  21409. constructor(schema) {
  21410. super(MAP, schema);
  21411. this.items = [];
  21412. }
  21413. /**
  21414. * A generic collection parsing method that can be extended
  21415. * to other node classes that inherit from YAMLMap
  21416. */
  21417. static from(schema, obj, ctx) {
  21418. const { keepUndefined, replacer } = ctx;
  21419. const map = new this(schema);
  21420. const add = (key, value) => {
  21421. if (typeof replacer === 'function')
  21422. value = replacer.call(obj, key, value);
  21423. else if (Array.isArray(replacer) && !replacer.includes(key))
  21424. return;
  21425. if (value !== undefined || keepUndefined)
  21426. map.items.push(createPair(key, value, ctx));
  21427. };
  21428. if (obj instanceof Map) {
  21429. for (const [key, value] of obj)
  21430. add(key, value);
  21431. }
  21432. else if (obj && typeof obj === 'object') {
  21433. for (const key of Object.keys(obj))
  21434. add(key, obj[key]);
  21435. }
  21436. if (typeof schema.sortMapEntries === 'function') {
  21437. map.items.sort(schema.sortMapEntries);
  21438. }
  21439. return map;
  21440. }
  21441. /**
  21442. * Adds a value to the collection.
  21443. *
  21444. * @param overwrite - If not set `true`, using a key that is already in the
  21445. * collection will throw. Otherwise, overwrites the previous value.
  21446. */
  21447. add(pair, overwrite) {
  21448. let _pair;
  21449. if (isPair(pair))
  21450. _pair = pair;
  21451. else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
  21452. // In TypeScript, this never happens.
  21453. _pair = new Pair(pair, pair?.value);
  21454. }
  21455. else
  21456. _pair = new Pair(pair.key, pair.value);
  21457. const prev = findPair(this.items, _pair.key);
  21458. const sortEntries = this.schema?.sortMapEntries;
  21459. if (prev) {
  21460. if (!overwrite)
  21461. throw new Error(`Key ${_pair.key} already set`);
  21462. // For scalars, keep the old node & its comments and anchors
  21463. if (isScalar$1(prev.value) && isScalarValue(_pair.value))
  21464. prev.value.value = _pair.value;
  21465. else
  21466. prev.value = _pair.value;
  21467. }
  21468. else if (sortEntries) {
  21469. const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
  21470. if (i === -1)
  21471. this.items.push(_pair);
  21472. else
  21473. this.items.splice(i, 0, _pair);
  21474. }
  21475. else {
  21476. this.items.push(_pair);
  21477. }
  21478. }
  21479. delete(key) {
  21480. const it = findPair(this.items, key);
  21481. if (!it)
  21482. return false;
  21483. const del = this.items.splice(this.items.indexOf(it), 1);
  21484. return del.length > 0;
  21485. }
  21486. get(key, keepScalar) {
  21487. const it = findPair(this.items, key);
  21488. const node = it?.value;
  21489. return (!keepScalar && isScalar$1(node) ? node.value : node) ?? undefined;
  21490. }
  21491. has(key) {
  21492. return !!findPair(this.items, key);
  21493. }
  21494. set(key, value) {
  21495. this.add(new Pair(key, value), true);
  21496. }
  21497. /**
  21498. * @param ctx - Conversion context, originally set in Document#toJS()
  21499. * @param {Class} Type - If set, forces the returned collection type
  21500. * @returns Instance of Type, Map, or Object
  21501. */
  21502. toJSON(_, ctx, Type) {
  21503. const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
  21504. if (ctx?.onCreate)
  21505. ctx.onCreate(map);
  21506. for (const item of this.items)
  21507. addPairToJSMap(ctx, map, item);
  21508. return map;
  21509. }
  21510. toString(ctx, onComment, onChompKeep) {
  21511. if (!ctx)
  21512. return JSON.stringify(this);
  21513. for (const item of this.items) {
  21514. if (!isPair(item))
  21515. throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
  21516. }
  21517. if (!ctx.allNullValues && this.hasAllNullValues(false))
  21518. ctx = Object.assign({}, ctx, { allNullValues: true });
  21519. return stringifyCollection(this, ctx, {
  21520. blockItemPrefix: '',
  21521. flowChars: { start: '{', end: '}' },
  21522. itemIndent: ctx.indent || '',
  21523. onChompKeep,
  21524. onComment
  21525. });
  21526. }
  21527. }
  21528. const map$1 = {
  21529. collection: 'map',
  21530. default: true,
  21531. nodeClass: YAMLMap,
  21532. tag: 'tag:yaml.org,2002:map',
  21533. resolve(map, onError) {
  21534. if (!isMap(map))
  21535. onError('Expected a mapping for this tag');
  21536. return map;
  21537. },
  21538. createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx)
  21539. };
  21540. class YAMLSeq extends Collection {
  21541. static get tagName() {
  21542. return 'tag:yaml.org,2002:seq';
  21543. }
  21544. constructor(schema) {
  21545. super(SEQ, schema);
  21546. this.items = [];
  21547. }
  21548. add(value) {
  21549. this.items.push(value);
  21550. }
  21551. /**
  21552. * Removes a value from the collection.
  21553. *
  21554. * `key` must contain a representation of an integer for this to succeed.
  21555. * It may be wrapped in a `Scalar`.
  21556. *
  21557. * @returns `true` if the item was found and removed.
  21558. */
  21559. delete(key) {
  21560. const idx = asItemIndex(key);
  21561. if (typeof idx !== 'number')
  21562. return false;
  21563. const del = this.items.splice(idx, 1);
  21564. return del.length > 0;
  21565. }
  21566. get(key, keepScalar) {
  21567. const idx = asItemIndex(key);
  21568. if (typeof idx !== 'number')
  21569. return undefined;
  21570. const it = this.items[idx];
  21571. return !keepScalar && isScalar$1(it) ? it.value : it;
  21572. }
  21573. /**
  21574. * Checks if the collection includes a value with the key `key`.
  21575. *
  21576. * `key` must contain a representation of an integer for this to succeed.
  21577. * It may be wrapped in a `Scalar`.
  21578. */
  21579. has(key) {
  21580. const idx = asItemIndex(key);
  21581. return typeof idx === 'number' && idx < this.items.length;
  21582. }
  21583. /**
  21584. * Sets a value in this collection. For `!!set`, `value` needs to be a
  21585. * boolean to add/remove the item from the set.
  21586. *
  21587. * If `key` does not contain a representation of an integer, this will throw.
  21588. * It may be wrapped in a `Scalar`.
  21589. */
  21590. set(key, value) {
  21591. const idx = asItemIndex(key);
  21592. if (typeof idx !== 'number')
  21593. throw new Error(`Expected a valid index, not ${key}.`);
  21594. const prev = this.items[idx];
  21595. if (isScalar$1(prev) && isScalarValue(value))
  21596. prev.value = value;
  21597. else
  21598. this.items[idx] = value;
  21599. }
  21600. toJSON(_, ctx) {
  21601. const seq = [];
  21602. if (ctx?.onCreate)
  21603. ctx.onCreate(seq);
  21604. let i = 0;
  21605. for (const item of this.items)
  21606. seq.push(toJS(item, String(i++), ctx));
  21607. return seq;
  21608. }
  21609. toString(ctx, onComment, onChompKeep) {
  21610. if (!ctx)
  21611. return JSON.stringify(this);
  21612. return stringifyCollection(this, ctx, {
  21613. blockItemPrefix: '- ',
  21614. flowChars: { start: '[', end: ']' },
  21615. itemIndent: (ctx.indent || '') + ' ',
  21616. onChompKeep,
  21617. onComment
  21618. });
  21619. }
  21620. static from(schema, obj, ctx) {
  21621. const { replacer } = ctx;
  21622. const seq = new this(schema);
  21623. if (obj && Symbol.iterator in Object(obj)) {
  21624. let i = 0;
  21625. for (let it of obj) {
  21626. if (typeof replacer === 'function') {
  21627. const key = obj instanceof Set ? it : String(i++);
  21628. it = replacer.call(obj, key, it);
  21629. }
  21630. seq.items.push(createNode(it, undefined, ctx));
  21631. }
  21632. }
  21633. return seq;
  21634. }
  21635. }
  21636. function asItemIndex(key) {
  21637. let idx = isScalar$1(key) ? key.value : key;
  21638. if (idx && typeof idx === 'string')
  21639. idx = Number(idx);
  21640. return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
  21641. ? idx
  21642. : null;
  21643. }
  21644. const seq = {
  21645. collection: 'seq',
  21646. default: true,
  21647. nodeClass: YAMLSeq,
  21648. tag: 'tag:yaml.org,2002:seq',
  21649. resolve(seq, onError) {
  21650. if (!isSeq(seq))
  21651. onError('Expected a sequence for this tag');
  21652. return seq;
  21653. },
  21654. createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx)
  21655. };
  21656. const string = {
  21657. identify: value => typeof value === 'string',
  21658. default: true,
  21659. tag: 'tag:yaml.org,2002:str',
  21660. resolve: str => str,
  21661. stringify(item, ctx, onComment, onChompKeep) {
  21662. ctx = Object.assign({ actualString: true }, ctx);
  21663. return stringifyString(item, ctx, onComment, onChompKeep);
  21664. }
  21665. };
  21666. const nullTag = {
  21667. identify: value => value == null,
  21668. createNode: () => new Scalar(null),
  21669. default: true,
  21670. tag: 'tag:yaml.org,2002:null',
  21671. test: /^(?:~|[Nn]ull|NULL)?$/,
  21672. resolve: () => new Scalar(null),
  21673. stringify: ({ source }, ctx) => typeof source === 'string' && nullTag.test.test(source)
  21674. ? source
  21675. : ctx.options.nullStr
  21676. };
  21677. const boolTag = {
  21678. identify: value => typeof value === 'boolean',
  21679. default: true,
  21680. tag: 'tag:yaml.org,2002:bool',
  21681. test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
  21682. resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
  21683. stringify({ source, value }, ctx) {
  21684. if (source && boolTag.test.test(source)) {
  21685. const sv = source[0] === 't' || source[0] === 'T';
  21686. if (value === sv)
  21687. return source;
  21688. }
  21689. return value ? ctx.options.trueStr : ctx.options.falseStr;
  21690. }
  21691. };
  21692. function stringifyNumber({ format, minFractionDigits, tag, value }) {
  21693. if (typeof value === 'bigint')
  21694. return String(value);
  21695. const num = typeof value === 'number' ? value : Number(value);
  21696. if (!isFinite(num))
  21697. return isNaN(num) ? '.nan' : num < 0 ? '-.inf' : '.inf';
  21698. let n = JSON.stringify(value);
  21699. if (!format &&
  21700. minFractionDigits &&
  21701. (!tag || tag === 'tag:yaml.org,2002:float') &&
  21702. /^\d/.test(n)) {
  21703. let i = n.indexOf('.');
  21704. if (i < 0) {
  21705. i = n.length;
  21706. n += '.';
  21707. }
  21708. let d = minFractionDigits - (n.length - i - 1);
  21709. while (d-- > 0)
  21710. n += '0';
  21711. }
  21712. return n;
  21713. }
  21714. const floatNaN$1 = {
  21715. identify: value => typeof value === 'number',
  21716. default: true,
  21717. tag: 'tag:yaml.org,2002:float',
  21718. test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
  21719. resolve: str => str.slice(-3).toLowerCase() === 'nan'
  21720. ? NaN
  21721. : str[0] === '-'
  21722. ? Number.NEGATIVE_INFINITY
  21723. : Number.POSITIVE_INFINITY,
  21724. stringify: stringifyNumber
  21725. };
  21726. const floatExp$1 = {
  21727. identify: value => typeof value === 'number',
  21728. default: true,
  21729. tag: 'tag:yaml.org,2002:float',
  21730. format: 'EXP',
  21731. test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
  21732. resolve: str => parseFloat(str),
  21733. stringify(node) {
  21734. const num = Number(node.value);
  21735. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  21736. }
  21737. };
  21738. const float$1 = {
  21739. identify: value => typeof value === 'number',
  21740. default: true,
  21741. tag: 'tag:yaml.org,2002:float',
  21742. test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
  21743. resolve(str) {
  21744. const node = new Scalar(parseFloat(str));
  21745. const dot = str.indexOf('.');
  21746. if (dot !== -1 && str[str.length - 1] === '0')
  21747. node.minFractionDigits = str.length - dot - 1;
  21748. return node;
  21749. },
  21750. stringify: stringifyNumber
  21751. };
  21752. const intIdentify$2 = (value) => typeof value === 'bigint' || Number.isInteger(value);
  21753. const intResolve$1 = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
  21754. function intStringify$1(node, radix, prefix) {
  21755. const { value } = node;
  21756. if (intIdentify$2(value) && value >= 0)
  21757. return prefix + value.toString(radix);
  21758. return stringifyNumber(node);
  21759. }
  21760. const intOct$1 = {
  21761. identify: value => intIdentify$2(value) && value >= 0,
  21762. default: true,
  21763. tag: 'tag:yaml.org,2002:int',
  21764. format: 'OCT',
  21765. test: /^0o[0-7]+$/,
  21766. resolve: (str, _onError, opt) => intResolve$1(str, 2, 8, opt),
  21767. stringify: node => intStringify$1(node, 8, '0o')
  21768. };
  21769. const int$1 = {
  21770. identify: intIdentify$2,
  21771. default: true,
  21772. tag: 'tag:yaml.org,2002:int',
  21773. test: /^[-+]?[0-9]+$/,
  21774. resolve: (str, _onError, opt) => intResolve$1(str, 0, 10, opt),
  21775. stringify: stringifyNumber
  21776. };
  21777. const intHex$1 = {
  21778. identify: value => intIdentify$2(value) && value >= 0,
  21779. default: true,
  21780. tag: 'tag:yaml.org,2002:int',
  21781. format: 'HEX',
  21782. test: /^0x[0-9a-fA-F]+$/,
  21783. resolve: (str, _onError, opt) => intResolve$1(str, 2, 16, opt),
  21784. stringify: node => intStringify$1(node, 16, '0x')
  21785. };
  21786. const schema$2 = [
  21787. map$1,
  21788. seq,
  21789. string,
  21790. nullTag,
  21791. boolTag,
  21792. intOct$1,
  21793. int$1,
  21794. intHex$1,
  21795. floatNaN$1,
  21796. floatExp$1,
  21797. float$1
  21798. ];
  21799. function intIdentify$1(value) {
  21800. return typeof value === 'bigint' || Number.isInteger(value);
  21801. }
  21802. const stringifyJSON = ({ value }) => JSON.stringify(value);
  21803. const jsonScalars = [
  21804. {
  21805. identify: value => typeof value === 'string',
  21806. default: true,
  21807. tag: 'tag:yaml.org,2002:str',
  21808. resolve: str => str,
  21809. stringify: stringifyJSON
  21810. },
  21811. {
  21812. identify: value => value == null,
  21813. createNode: () => new Scalar(null),
  21814. default: true,
  21815. tag: 'tag:yaml.org,2002:null',
  21816. test: /^null$/,
  21817. resolve: () => null,
  21818. stringify: stringifyJSON
  21819. },
  21820. {
  21821. identify: value => typeof value === 'boolean',
  21822. default: true,
  21823. tag: 'tag:yaml.org,2002:bool',
  21824. test: /^true|false$/,
  21825. resolve: str => str === 'true',
  21826. stringify: stringifyJSON
  21827. },
  21828. {
  21829. identify: intIdentify$1,
  21830. default: true,
  21831. tag: 'tag:yaml.org,2002:int',
  21832. test: /^-?(?:0|[1-9][0-9]*)$/,
  21833. resolve: (str, _onError, { intAsBigInt }) => intAsBigInt ? BigInt(str) : parseInt(str, 10),
  21834. stringify: ({ value }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
  21835. },
  21836. {
  21837. identify: value => typeof value === 'number',
  21838. default: true,
  21839. tag: 'tag:yaml.org,2002:float',
  21840. test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
  21841. resolve: str => parseFloat(str),
  21842. stringify: stringifyJSON
  21843. }
  21844. ];
  21845. const jsonError = {
  21846. default: true,
  21847. tag: '',
  21848. test: /^/,
  21849. resolve(str, onError) {
  21850. onError(`Unresolved plain scalar ${JSON.stringify(str)}`);
  21851. return str;
  21852. }
  21853. };
  21854. const schema$1 = [map$1, seq].concat(jsonScalars, jsonError);
  21855. const binary = {
  21856. identify: value => value instanceof Uint8Array,
  21857. default: false,
  21858. tag: 'tag:yaml.org,2002:binary',
  21859. /**
  21860. * Returns a Buffer in node and an Uint8Array in browsers
  21861. *
  21862. * To use the resulting buffer as an image, you'll want to do something like:
  21863. *
  21864. * const blob = new Blob([buffer], { type: 'image/jpeg' })
  21865. * document.querySelector('#photo').src = URL.createObjectURL(blob)
  21866. */
  21867. resolve(src, onError) {
  21868. if (typeof Buffer === 'function') {
  21869. return Buffer.from(src, 'base64');
  21870. }
  21871. else if (typeof atob === 'function') {
  21872. // On IE 11, atob() can't handle newlines
  21873. const str = atob(src.replace(/[\n\r]/g, ''));
  21874. const buffer = new Uint8Array(str.length);
  21875. for (let i = 0; i < str.length; ++i)
  21876. buffer[i] = str.charCodeAt(i);
  21877. return buffer;
  21878. }
  21879. else {
  21880. onError('This environment does not support reading binary tags; either Buffer or atob is required');
  21881. return src;
  21882. }
  21883. },
  21884. stringify({ comment, type, value }, ctx, onComment, onChompKeep) {
  21885. const buf = value; // checked earlier by binary.identify()
  21886. let str;
  21887. if (typeof Buffer === 'function') {
  21888. str =
  21889. buf instanceof Buffer
  21890. ? buf.toString('base64')
  21891. : Buffer.from(buf.buffer).toString('base64');
  21892. }
  21893. else if (typeof btoa === 'function') {
  21894. let s = '';
  21895. for (let i = 0; i < buf.length; ++i)
  21896. s += String.fromCharCode(buf[i]);
  21897. str = btoa(s);
  21898. }
  21899. else {
  21900. throw new Error('This environment does not support writing binary tags; either Buffer or btoa is required');
  21901. }
  21902. if (!type)
  21903. type = Scalar.BLOCK_LITERAL;
  21904. if (type !== Scalar.QUOTE_DOUBLE) {
  21905. const lineWidth = Math.max(ctx.options.lineWidth - ctx.indent.length, ctx.options.minContentWidth);
  21906. const n = Math.ceil(str.length / lineWidth);
  21907. const lines = new Array(n);
  21908. for (let i = 0, o = 0; i < n; ++i, o += lineWidth) {
  21909. lines[i] = str.substr(o, lineWidth);
  21910. }
  21911. str = lines.join(type === Scalar.BLOCK_LITERAL ? '\n' : ' ');
  21912. }
  21913. return stringifyString({ comment, type, value: str }, ctx, onComment, onChompKeep);
  21914. }
  21915. };
  21916. function resolvePairs(seq, onError) {
  21917. if (isSeq(seq)) {
  21918. for (let i = 0; i < seq.items.length; ++i) {
  21919. let item = seq.items[i];
  21920. if (isPair(item))
  21921. continue;
  21922. else if (isMap(item)) {
  21923. if (item.items.length > 1)
  21924. onError('Each pair must have its own sequence indicator');
  21925. const pair = item.items[0] || new Pair(new Scalar(null));
  21926. if (item.commentBefore)
  21927. pair.key.commentBefore = pair.key.commentBefore
  21928. ? `${item.commentBefore}\n${pair.key.commentBefore}`
  21929. : item.commentBefore;
  21930. if (item.comment) {
  21931. const cn = pair.value ?? pair.key;
  21932. cn.comment = cn.comment
  21933. ? `${item.comment}\n${cn.comment}`
  21934. : item.comment;
  21935. }
  21936. item = pair;
  21937. }
  21938. seq.items[i] = isPair(item) ? item : new Pair(item);
  21939. }
  21940. }
  21941. else
  21942. onError('Expected a sequence for this tag');
  21943. return seq;
  21944. }
  21945. function createPairs(schema, iterable, ctx) {
  21946. const { replacer } = ctx;
  21947. const pairs = new YAMLSeq(schema);
  21948. pairs.tag = 'tag:yaml.org,2002:pairs';
  21949. let i = 0;
  21950. if (iterable && Symbol.iterator in Object(iterable))
  21951. for (let it of iterable) {
  21952. if (typeof replacer === 'function')
  21953. it = replacer.call(iterable, String(i++), it);
  21954. let key, value;
  21955. if (Array.isArray(it)) {
  21956. if (it.length === 2) {
  21957. key = it[0];
  21958. value = it[1];
  21959. }
  21960. else
  21961. throw new TypeError(`Expected [key, value] tuple: ${it}`);
  21962. }
  21963. else if (it && it instanceof Object) {
  21964. const keys = Object.keys(it);
  21965. if (keys.length === 1) {
  21966. key = keys[0];
  21967. value = it[key];
  21968. }
  21969. else {
  21970. throw new TypeError(`Expected tuple with one key, not ${keys.length} keys`);
  21971. }
  21972. }
  21973. else {
  21974. key = it;
  21975. }
  21976. pairs.items.push(createPair(key, value, ctx));
  21977. }
  21978. return pairs;
  21979. }
  21980. const pairs = {
  21981. collection: 'seq',
  21982. default: false,
  21983. tag: 'tag:yaml.org,2002:pairs',
  21984. resolve: resolvePairs,
  21985. createNode: createPairs
  21986. };
  21987. class YAMLOMap extends YAMLSeq {
  21988. constructor() {
  21989. super();
  21990. this.add = YAMLMap.prototype.add.bind(this);
  21991. this.delete = YAMLMap.prototype.delete.bind(this);
  21992. this.get = YAMLMap.prototype.get.bind(this);
  21993. this.has = YAMLMap.prototype.has.bind(this);
  21994. this.set = YAMLMap.prototype.set.bind(this);
  21995. this.tag = YAMLOMap.tag;
  21996. }
  21997. /**
  21998. * If `ctx` is given, the return type is actually `Map<unknown, unknown>`,
  21999. * but TypeScript won't allow widening the signature of a child method.
  22000. */
  22001. toJSON(_, ctx) {
  22002. if (!ctx)
  22003. return super.toJSON(_);
  22004. const map = new Map();
  22005. if (ctx?.onCreate)
  22006. ctx.onCreate(map);
  22007. for (const pair of this.items) {
  22008. let key, value;
  22009. if (isPair(pair)) {
  22010. key = toJS(pair.key, '', ctx);
  22011. value = toJS(pair.value, key, ctx);
  22012. }
  22013. else {
  22014. key = toJS(pair, '', ctx);
  22015. }
  22016. if (map.has(key))
  22017. throw new Error('Ordered maps must not include duplicate keys');
  22018. map.set(key, value);
  22019. }
  22020. return map;
  22021. }
  22022. static from(schema, iterable, ctx) {
  22023. const pairs = createPairs(schema, iterable, ctx);
  22024. const omap = new this();
  22025. omap.items = pairs.items;
  22026. return omap;
  22027. }
  22028. }
  22029. YAMLOMap.tag = 'tag:yaml.org,2002:omap';
  22030. const omap = {
  22031. collection: 'seq',
  22032. identify: value => value instanceof Map,
  22033. nodeClass: YAMLOMap,
  22034. default: false,
  22035. tag: 'tag:yaml.org,2002:omap',
  22036. resolve(seq, onError) {
  22037. const pairs = resolvePairs(seq, onError);
  22038. const seenKeys = [];
  22039. for (const { key } of pairs.items) {
  22040. if (isScalar$1(key)) {
  22041. if (seenKeys.includes(key.value)) {
  22042. onError(`Ordered maps must not include duplicate keys: ${key.value}`);
  22043. }
  22044. else {
  22045. seenKeys.push(key.value);
  22046. }
  22047. }
  22048. }
  22049. return Object.assign(new YAMLOMap(), pairs);
  22050. },
  22051. createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
  22052. };
  22053. function boolStringify({ value, source }, ctx) {
  22054. const boolObj = value ? trueTag : falseTag;
  22055. if (source && boolObj.test.test(source))
  22056. return source;
  22057. return value ? ctx.options.trueStr : ctx.options.falseStr;
  22058. }
  22059. const trueTag = {
  22060. identify: value => value === true,
  22061. default: true,
  22062. tag: 'tag:yaml.org,2002:bool',
  22063. test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
  22064. resolve: () => new Scalar(true),
  22065. stringify: boolStringify
  22066. };
  22067. const falseTag = {
  22068. identify: value => value === false,
  22069. default: true,
  22070. tag: 'tag:yaml.org,2002:bool',
  22071. test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
  22072. resolve: () => new Scalar(false),
  22073. stringify: boolStringify
  22074. };
  22075. const floatNaN = {
  22076. identify: value => typeof value === 'number',
  22077. default: true,
  22078. tag: 'tag:yaml.org,2002:float',
  22079. test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
  22080. resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
  22081. ? NaN
  22082. : str[0] === '-'
  22083. ? Number.NEGATIVE_INFINITY
  22084. : Number.POSITIVE_INFINITY,
  22085. stringify: stringifyNumber
  22086. };
  22087. const floatExp = {
  22088. identify: value => typeof value === 'number',
  22089. default: true,
  22090. tag: 'tag:yaml.org,2002:float',
  22091. format: 'EXP',
  22092. test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
  22093. resolve: (str) => parseFloat(str.replace(/_/g, '')),
  22094. stringify(node) {
  22095. const num = Number(node.value);
  22096. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  22097. }
  22098. };
  22099. const float = {
  22100. identify: value => typeof value === 'number',
  22101. default: true,
  22102. tag: 'tag:yaml.org,2002:float',
  22103. test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
  22104. resolve(str) {
  22105. const node = new Scalar(parseFloat(str.replace(/_/g, '')));
  22106. const dot = str.indexOf('.');
  22107. if (dot !== -1) {
  22108. const f = str.substring(dot + 1).replace(/_/g, '');
  22109. if (f[f.length - 1] === '0')
  22110. node.minFractionDigits = f.length;
  22111. }
  22112. return node;
  22113. },
  22114. stringify: stringifyNumber
  22115. };
  22116. const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
  22117. function intResolve(str, offset, radix, { intAsBigInt }) {
  22118. const sign = str[0];
  22119. if (sign === '-' || sign === '+')
  22120. offset += 1;
  22121. str = str.substring(offset).replace(/_/g, '');
  22122. if (intAsBigInt) {
  22123. switch (radix) {
  22124. case 2:
  22125. str = `0b${str}`;
  22126. break;
  22127. case 8:
  22128. str = `0o${str}`;
  22129. break;
  22130. case 16:
  22131. str = `0x${str}`;
  22132. break;
  22133. }
  22134. const n = BigInt(str);
  22135. return sign === '-' ? BigInt(-1) * n : n;
  22136. }
  22137. const n = parseInt(str, radix);
  22138. return sign === '-' ? -1 * n : n;
  22139. }
  22140. function intStringify(node, radix, prefix) {
  22141. const { value } = node;
  22142. if (intIdentify(value)) {
  22143. const str = value.toString(radix);
  22144. return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
  22145. }
  22146. return stringifyNumber(node);
  22147. }
  22148. const intBin = {
  22149. identify: intIdentify,
  22150. default: true,
  22151. tag: 'tag:yaml.org,2002:int',
  22152. format: 'BIN',
  22153. test: /^[-+]?0b[0-1_]+$/,
  22154. resolve: (str, _onError, opt) => intResolve(str, 2, 2, opt),
  22155. stringify: node => intStringify(node, 2, '0b')
  22156. };
  22157. const intOct = {
  22158. identify: intIdentify,
  22159. default: true,
  22160. tag: 'tag:yaml.org,2002:int',
  22161. format: 'OCT',
  22162. test: /^[-+]?0[0-7_]+$/,
  22163. resolve: (str, _onError, opt) => intResolve(str, 1, 8, opt),
  22164. stringify: node => intStringify(node, 8, '0')
  22165. };
  22166. const int = {
  22167. identify: intIdentify,
  22168. default: true,
  22169. tag: 'tag:yaml.org,2002:int',
  22170. test: /^[-+]?[0-9][0-9_]*$/,
  22171. resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
  22172. stringify: stringifyNumber
  22173. };
  22174. const intHex = {
  22175. identify: intIdentify,
  22176. default: true,
  22177. tag: 'tag:yaml.org,2002:int',
  22178. format: 'HEX',
  22179. test: /^[-+]?0x[0-9a-fA-F_]+$/,
  22180. resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
  22181. stringify: node => intStringify(node, 16, '0x')
  22182. };
  22183. class YAMLSet extends YAMLMap {
  22184. constructor(schema) {
  22185. super(schema);
  22186. this.tag = YAMLSet.tag;
  22187. }
  22188. add(key) {
  22189. let pair;
  22190. if (isPair(key))
  22191. pair = key;
  22192. else if (key &&
  22193. typeof key === 'object' &&
  22194. 'key' in key &&
  22195. 'value' in key &&
  22196. key.value === null)
  22197. pair = new Pair(key.key, null);
  22198. else
  22199. pair = new Pair(key, null);
  22200. const prev = findPair(this.items, pair.key);
  22201. if (!prev)
  22202. this.items.push(pair);
  22203. }
  22204. /**
  22205. * If `keepPair` is `true`, returns the Pair matching `key`.
  22206. * Otherwise, returns the value of that Pair's key.
  22207. */
  22208. get(key, keepPair) {
  22209. const pair = findPair(this.items, key);
  22210. return !keepPair && isPair(pair)
  22211. ? isScalar$1(pair.key)
  22212. ? pair.key.value
  22213. : pair.key
  22214. : pair;
  22215. }
  22216. set(key, value) {
  22217. if (typeof value !== 'boolean')
  22218. throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
  22219. const prev = findPair(this.items, key);
  22220. if (prev && !value) {
  22221. this.items.splice(this.items.indexOf(prev), 1);
  22222. }
  22223. else if (!prev && value) {
  22224. this.items.push(new Pair(key));
  22225. }
  22226. }
  22227. toJSON(_, ctx) {
  22228. return super.toJSON(_, ctx, Set);
  22229. }
  22230. toString(ctx, onComment, onChompKeep) {
  22231. if (!ctx)
  22232. return JSON.stringify(this);
  22233. if (this.hasAllNullValues(true))
  22234. return super.toString(Object.assign({}, ctx, { allNullValues: true }), onComment, onChompKeep);
  22235. else
  22236. throw new Error('Set items must all have null values');
  22237. }
  22238. static from(schema, iterable, ctx) {
  22239. const { replacer } = ctx;
  22240. const set = new this(schema);
  22241. if (iterable && Symbol.iterator in Object(iterable))
  22242. for (let value of iterable) {
  22243. if (typeof replacer === 'function')
  22244. value = replacer.call(iterable, value, value);
  22245. set.items.push(createPair(value, null, ctx));
  22246. }
  22247. return set;
  22248. }
  22249. }
  22250. YAMLSet.tag = 'tag:yaml.org,2002:set';
  22251. const set = {
  22252. collection: 'map',
  22253. identify: value => value instanceof Set,
  22254. nodeClass: YAMLSet,
  22255. default: false,
  22256. tag: 'tag:yaml.org,2002:set',
  22257. createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
  22258. resolve(map, onError) {
  22259. if (isMap(map)) {
  22260. if (map.hasAllNullValues(true))
  22261. return Object.assign(new YAMLSet(), map);
  22262. else
  22263. onError('Set items must all have null values');
  22264. }
  22265. else
  22266. onError('Expected a mapping for this tag');
  22267. return map;
  22268. }
  22269. };
  22270. /** Internal types handle bigint as number, because TS can't figure it out. */
  22271. function parseSexagesimal(str, asBigInt) {
  22272. const sign = str[0];
  22273. const parts = sign === '-' || sign === '+' ? str.substring(1) : str;
  22274. const num = (n) => asBigInt ? BigInt(n) : Number(n);
  22275. const res = parts
  22276. .replace(/_/g, '')
  22277. .split(':')
  22278. .reduce((res, p) => res * num(60) + num(p), num(0));
  22279. return (sign === '-' ? num(-1) * res : res);
  22280. }
  22281. /**
  22282. * hhhh:mm:ss.sss
  22283. *
  22284. * Internal types handle bigint as number, because TS can't figure it out.
  22285. */
  22286. function stringifySexagesimal(node) {
  22287. let { value } = node;
  22288. let num = (n) => n;
  22289. if (typeof value === 'bigint')
  22290. num = n => BigInt(n);
  22291. else if (isNaN(value) || !isFinite(value))
  22292. return stringifyNumber(node);
  22293. let sign = '';
  22294. if (value < 0) {
  22295. sign = '-';
  22296. value *= num(-1);
  22297. }
  22298. const _60 = num(60);
  22299. const parts = [value % _60]; // seconds, including ms
  22300. if (value < 60) {
  22301. parts.unshift(0); // at least one : is required
  22302. }
  22303. else {
  22304. value = (value - parts[0]) / _60;
  22305. parts.unshift(value % _60); // minutes
  22306. if (value >= 60) {
  22307. value = (value - parts[0]) / _60;
  22308. parts.unshift(value); // hours
  22309. }
  22310. }
  22311. return (sign +
  22312. parts
  22313. .map(n => String(n).padStart(2, '0'))
  22314. .join(':')
  22315. .replace(/000000\d*$/, '') // % 60 may introduce error
  22316. );
  22317. }
  22318. const intTime = {
  22319. identify: value => typeof value === 'bigint' || Number.isInteger(value),
  22320. default: true,
  22321. tag: 'tag:yaml.org,2002:int',
  22322. format: 'TIME',
  22323. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+$/,
  22324. resolve: (str, _onError, { intAsBigInt }) => parseSexagesimal(str, intAsBigInt),
  22325. stringify: stringifySexagesimal
  22326. };
  22327. const floatTime = {
  22328. identify: value => typeof value === 'number',
  22329. default: true,
  22330. tag: 'tag:yaml.org,2002:float',
  22331. format: 'TIME',
  22332. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*$/,
  22333. resolve: str => parseSexagesimal(str, false),
  22334. stringify: stringifySexagesimal
  22335. };
  22336. const timestamp = {
  22337. identify: value => value instanceof Date,
  22338. default: true,
  22339. tag: 'tag:yaml.org,2002:timestamp',
  22340. // If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part
  22341. // may be omitted altogether, resulting in a date format. In such a case, the time part is
  22342. // assumed to be 00:00:00Z (start of day, UTC).
  22343. test: RegExp('^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd
  22344. '(?:' + // time is optional
  22345. '(?:t|T|[ \\t]+)' + // t | T | whitespace
  22346. '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)?
  22347. '(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30
  22348. ')?$'),
  22349. resolve(str) {
  22350. const match = str.match(timestamp.test);
  22351. if (!match)
  22352. throw new Error('!!timestamp expects a date, starting with yyyy-mm-dd');
  22353. const [, year, month, day, hour, minute, second] = match.map(Number);
  22354. const millisec = match[7] ? Number((match[7] + '00').substr(1, 3)) : 0;
  22355. let date = Date.UTC(year, month - 1, day, hour || 0, minute || 0, second || 0, millisec);
  22356. const tz = match[8];
  22357. if (tz && tz !== 'Z') {
  22358. let d = parseSexagesimal(tz, false);
  22359. if (Math.abs(d) < 30)
  22360. d *= 60;
  22361. date -= 60000 * d;
  22362. }
  22363. return new Date(date);
  22364. },
  22365. stringify: ({ value }) => value.toISOString().replace(/((T00:00)?:00)?\.000Z$/, '')
  22366. };
  22367. const schema = [
  22368. map$1,
  22369. seq,
  22370. string,
  22371. nullTag,
  22372. trueTag,
  22373. falseTag,
  22374. intBin,
  22375. intOct,
  22376. int,
  22377. intHex,
  22378. floatNaN,
  22379. floatExp,
  22380. float,
  22381. binary,
  22382. omap,
  22383. pairs,
  22384. set,
  22385. intTime,
  22386. floatTime,
  22387. timestamp
  22388. ];
  22389. const schemas = new Map([
  22390. ['core', schema$2],
  22391. ['failsafe', [map$1, seq, string]],
  22392. ['json', schema$1],
  22393. ['yaml11', schema],
  22394. ['yaml-1.1', schema]
  22395. ]);
  22396. const tagsByName = {
  22397. binary,
  22398. bool: boolTag,
  22399. float: float$1,
  22400. floatExp: floatExp$1,
  22401. floatNaN: floatNaN$1,
  22402. floatTime,
  22403. int: int$1,
  22404. intHex: intHex$1,
  22405. intOct: intOct$1,
  22406. intTime,
  22407. map: map$1,
  22408. null: nullTag,
  22409. omap,
  22410. pairs,
  22411. seq,
  22412. set,
  22413. timestamp
  22414. };
  22415. const coreKnownTags = {
  22416. 'tag:yaml.org,2002:binary': binary,
  22417. 'tag:yaml.org,2002:omap': omap,
  22418. 'tag:yaml.org,2002:pairs': pairs,
  22419. 'tag:yaml.org,2002:set': set,
  22420. 'tag:yaml.org,2002:timestamp': timestamp
  22421. };
  22422. function getTags(customTags, schemaName) {
  22423. let tags = schemas.get(schemaName);
  22424. if (!tags) {
  22425. if (Array.isArray(customTags))
  22426. tags = [];
  22427. else {
  22428. const keys = Array.from(schemas.keys())
  22429. .filter(key => key !== 'yaml11')
  22430. .map(key => JSON.stringify(key))
  22431. .join(', ');
  22432. throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
  22433. }
  22434. }
  22435. if (Array.isArray(customTags)) {
  22436. for (const tag of customTags)
  22437. tags = tags.concat(tag);
  22438. }
  22439. else if (typeof customTags === 'function') {
  22440. tags = customTags(tags.slice());
  22441. }
  22442. return tags.map(tag => {
  22443. if (typeof tag !== 'string')
  22444. return tag;
  22445. const tagObj = tagsByName[tag];
  22446. if (tagObj)
  22447. return tagObj;
  22448. const keys = Object.keys(tagsByName)
  22449. .map(key => JSON.stringify(key))
  22450. .join(', ');
  22451. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  22452. });
  22453. }
  22454. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  22455. class Schema {
  22456. constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
  22457. this.compat = Array.isArray(compat)
  22458. ? getTags(compat, 'compat')
  22459. : compat
  22460. ? getTags(null, compat)
  22461. : null;
  22462. this.merge = !!merge;
  22463. this.name = (typeof schema === 'string' && schema) || 'core';
  22464. this.knownTags = resolveKnownTags ? coreKnownTags : {};
  22465. this.tags = getTags(customTags, this.name);
  22466. this.toStringOptions = toStringDefaults ?? null;
  22467. Object.defineProperty(this, MAP, { value: map$1 });
  22468. Object.defineProperty(this, SCALAR$1, { value: string });
  22469. Object.defineProperty(this, SEQ, { value: seq });
  22470. // Used by createMap()
  22471. this.sortMapEntries =
  22472. typeof sortMapEntries === 'function'
  22473. ? sortMapEntries
  22474. : sortMapEntries === true
  22475. ? sortMapEntriesByKey
  22476. : null;
  22477. }
  22478. clone() {
  22479. const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
  22480. copy.tags = this.tags.slice();
  22481. return copy;
  22482. }
  22483. }
  22484. function stringifyDocument(doc, options) {
  22485. const lines = [];
  22486. let hasDirectives = options.directives === true;
  22487. if (options.directives !== false && doc.directives) {
  22488. const dir = doc.directives.toString(doc);
  22489. if (dir) {
  22490. lines.push(dir);
  22491. hasDirectives = true;
  22492. }
  22493. else if (doc.directives.docStart)
  22494. hasDirectives = true;
  22495. }
  22496. if (hasDirectives)
  22497. lines.push('---');
  22498. const ctx = createStringifyContext(doc, options);
  22499. const { commentString } = ctx.options;
  22500. if (doc.commentBefore) {
  22501. if (lines.length !== 1)
  22502. lines.unshift('');
  22503. const cs = commentString(doc.commentBefore);
  22504. lines.unshift(indentComment(cs, ''));
  22505. }
  22506. let chompKeep = false;
  22507. let contentComment = null;
  22508. if (doc.contents) {
  22509. if (isNode$1(doc.contents)) {
  22510. if (doc.contents.spaceBefore && hasDirectives)
  22511. lines.push('');
  22512. if (doc.contents.commentBefore) {
  22513. const cs = commentString(doc.contents.commentBefore);
  22514. lines.push(indentComment(cs, ''));
  22515. }
  22516. // top-level block scalars need to be indented if followed by a comment
  22517. ctx.forceBlockIndent = !!doc.comment;
  22518. contentComment = doc.contents.comment;
  22519. }
  22520. const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
  22521. let body = stringify$2(doc.contents, ctx, () => (contentComment = null), onChompKeep);
  22522. if (contentComment)
  22523. body += lineComment(body, '', commentString(contentComment));
  22524. if ((body[0] === '|' || body[0] === '>') &&
  22525. lines[lines.length - 1] === '---') {
  22526. // Top-level block scalars with a preceding doc marker ought to use the
  22527. // same line for their header.
  22528. lines[lines.length - 1] = `--- ${body}`;
  22529. }
  22530. else
  22531. lines.push(body);
  22532. }
  22533. else {
  22534. lines.push(stringify$2(doc.contents, ctx));
  22535. }
  22536. if (doc.directives?.docEnd) {
  22537. if (doc.comment) {
  22538. const cs = commentString(doc.comment);
  22539. if (cs.includes('\n')) {
  22540. lines.push('...');
  22541. lines.push(indentComment(cs, ''));
  22542. }
  22543. else {
  22544. lines.push(`... ${cs}`);
  22545. }
  22546. }
  22547. else {
  22548. lines.push('...');
  22549. }
  22550. }
  22551. else {
  22552. let dc = doc.comment;
  22553. if (dc && chompKeep)
  22554. dc = dc.replace(/^\n+/, '');
  22555. if (dc) {
  22556. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
  22557. lines.push('');
  22558. lines.push(indentComment(commentString(dc), ''));
  22559. }
  22560. }
  22561. return lines.join('\n') + '\n';
  22562. }
  22563. class Document {
  22564. constructor(value, replacer, options) {
  22565. /** A comment before this Document */
  22566. this.commentBefore = null;
  22567. /** A comment immediately after this Document */
  22568. this.comment = null;
  22569. /** Errors encountered during parsing. */
  22570. this.errors = [];
  22571. /** Warnings encountered during parsing. */
  22572. this.warnings = [];
  22573. Object.defineProperty(this, NODE_TYPE, { value: DOC });
  22574. let _replacer = null;
  22575. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  22576. _replacer = replacer;
  22577. }
  22578. else if (options === undefined && replacer) {
  22579. options = replacer;
  22580. replacer = undefined;
  22581. }
  22582. const opt = Object.assign({
  22583. intAsBigInt: false,
  22584. keepSourceTokens: false,
  22585. logLevel: 'warn',
  22586. prettyErrors: true,
  22587. strict: true,
  22588. uniqueKeys: true,
  22589. version: '1.2'
  22590. }, options);
  22591. this.options = opt;
  22592. let { version } = opt;
  22593. if (options?._directives) {
  22594. this.directives = options._directives.atDocument();
  22595. if (this.directives.yaml.explicit)
  22596. version = this.directives.yaml.version;
  22597. }
  22598. else
  22599. this.directives = new Directives({ version });
  22600. this.setSchema(version, options);
  22601. // @ts-expect-error We can't really know that this matches Contents.
  22602. this.contents =
  22603. value === undefined ? null : this.createNode(value, _replacer, options);
  22604. }
  22605. /**
  22606. * Create a deep copy of this Document and its contents.
  22607. *
  22608. * Custom Node values that inherit from `Object` still refer to their original instances.
  22609. */
  22610. clone() {
  22611. const copy = Object.create(Document.prototype, {
  22612. [NODE_TYPE]: { value: DOC }
  22613. });
  22614. copy.commentBefore = this.commentBefore;
  22615. copy.comment = this.comment;
  22616. copy.errors = this.errors.slice();
  22617. copy.warnings = this.warnings.slice();
  22618. copy.options = Object.assign({}, this.options);
  22619. if (this.directives)
  22620. copy.directives = this.directives.clone();
  22621. copy.schema = this.schema.clone();
  22622. // @ts-expect-error We can't really know that this matches Contents.
  22623. copy.contents = isNode$1(this.contents)
  22624. ? this.contents.clone(copy.schema)
  22625. : this.contents;
  22626. if (this.range)
  22627. copy.range = this.range.slice();
  22628. return copy;
  22629. }
  22630. /** Adds a value to the document. */
  22631. add(value) {
  22632. if (assertCollection(this.contents))
  22633. this.contents.add(value);
  22634. }
  22635. /** Adds a value to the document. */
  22636. addIn(path, value) {
  22637. if (assertCollection(this.contents))
  22638. this.contents.addIn(path, value);
  22639. }
  22640. /**
  22641. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  22642. *
  22643. * If `node` already has an anchor, `name` is ignored.
  22644. * Otherwise, the `node.anchor` value will be set to `name`,
  22645. * or if an anchor with that name is already present in the document,
  22646. * `name` will be used as a prefix for a new unique anchor.
  22647. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  22648. */
  22649. createAlias(node, name) {
  22650. if (!node.anchor) {
  22651. const prev = anchorNames(this);
  22652. node.anchor =
  22653. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  22654. !name || prev.has(name) ? findNewAnchor(name || 'a', prev) : name;
  22655. }
  22656. return new Alias(node.anchor);
  22657. }
  22658. createNode(value, replacer, options) {
  22659. let _replacer = undefined;
  22660. if (typeof replacer === 'function') {
  22661. value = replacer.call({ '': value }, '', value);
  22662. _replacer = replacer;
  22663. }
  22664. else if (Array.isArray(replacer)) {
  22665. const keyToStr = (v) => typeof v === 'number' || v instanceof String || v instanceof Number;
  22666. const asStr = replacer.filter(keyToStr).map(String);
  22667. if (asStr.length > 0)
  22668. replacer = replacer.concat(asStr);
  22669. _replacer = replacer;
  22670. }
  22671. else if (options === undefined && replacer) {
  22672. options = replacer;
  22673. replacer = undefined;
  22674. }
  22675. const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
  22676. const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this,
  22677. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  22678. anchorPrefix || 'a');
  22679. const ctx = {
  22680. aliasDuplicateObjects: aliasDuplicateObjects ?? true,
  22681. keepUndefined: keepUndefined ?? false,
  22682. onAnchor,
  22683. onTagObj,
  22684. replacer: _replacer,
  22685. schema: this.schema,
  22686. sourceObjects
  22687. };
  22688. const node = createNode(value, tag, ctx);
  22689. if (flow && isCollection$1(node))
  22690. node.flow = true;
  22691. setAnchors();
  22692. return node;
  22693. }
  22694. /**
  22695. * Convert a key and a value into a `Pair` using the current schema,
  22696. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  22697. */
  22698. createPair(key, value, options = {}) {
  22699. const k = this.createNode(key, null, options);
  22700. const v = this.createNode(value, null, options);
  22701. return new Pair(k, v);
  22702. }
  22703. /**
  22704. * Removes a value from the document.
  22705. * @returns `true` if the item was found and removed.
  22706. */
  22707. delete(key) {
  22708. return assertCollection(this.contents) ? this.contents.delete(key) : false;
  22709. }
  22710. /**
  22711. * Removes a value from the document.
  22712. * @returns `true` if the item was found and removed.
  22713. */
  22714. deleteIn(path) {
  22715. if (isEmptyPath(path)) {
  22716. if (this.contents == null)
  22717. return false;
  22718. // @ts-expect-error Presumed impossible if Strict extends false
  22719. this.contents = null;
  22720. return true;
  22721. }
  22722. return assertCollection(this.contents)
  22723. ? this.contents.deleteIn(path)
  22724. : false;
  22725. }
  22726. /**
  22727. * Returns item at `key`, or `undefined` if not found. By default unwraps
  22728. * scalar values from their surrounding node; to disable set `keepScalar` to
  22729. * `true` (collections are always returned intact).
  22730. */
  22731. get(key, keepScalar) {
  22732. return isCollection$1(this.contents)
  22733. ? this.contents.get(key, keepScalar)
  22734. : undefined;
  22735. }
  22736. /**
  22737. * Returns item at `path`, or `undefined` if not found. By default unwraps
  22738. * scalar values from their surrounding node; to disable set `keepScalar` to
  22739. * `true` (collections are always returned intact).
  22740. */
  22741. getIn(path, keepScalar) {
  22742. if (isEmptyPath(path))
  22743. return !keepScalar && isScalar$1(this.contents)
  22744. ? this.contents.value
  22745. : this.contents;
  22746. return isCollection$1(this.contents)
  22747. ? this.contents.getIn(path, keepScalar)
  22748. : undefined;
  22749. }
  22750. /**
  22751. * Checks if the document includes a value with the key `key`.
  22752. */
  22753. has(key) {
  22754. return isCollection$1(this.contents) ? this.contents.has(key) : false;
  22755. }
  22756. /**
  22757. * Checks if the document includes a value at `path`.
  22758. */
  22759. hasIn(path) {
  22760. if (isEmptyPath(path))
  22761. return this.contents !== undefined;
  22762. return isCollection$1(this.contents) ? this.contents.hasIn(path) : false;
  22763. }
  22764. /**
  22765. * Sets a value in this document. For `!!set`, `value` needs to be a
  22766. * boolean to add/remove the item from the set.
  22767. */
  22768. set(key, value) {
  22769. if (this.contents == null) {
  22770. // @ts-expect-error We can't really know that this matches Contents.
  22771. this.contents = collectionFromPath(this.schema, [key], value);
  22772. }
  22773. else if (assertCollection(this.contents)) {
  22774. this.contents.set(key, value);
  22775. }
  22776. }
  22777. /**
  22778. * Sets a value in this document. For `!!set`, `value` needs to be a
  22779. * boolean to add/remove the item from the set.
  22780. */
  22781. setIn(path, value) {
  22782. if (isEmptyPath(path)) {
  22783. // @ts-expect-error We can't really know that this matches Contents.
  22784. this.contents = value;
  22785. }
  22786. else if (this.contents == null) {
  22787. // @ts-expect-error We can't really know that this matches Contents.
  22788. this.contents = collectionFromPath(this.schema, Array.from(path), value);
  22789. }
  22790. else if (assertCollection(this.contents)) {
  22791. this.contents.setIn(path, value);
  22792. }
  22793. }
  22794. /**
  22795. * Change the YAML version and schema used by the document.
  22796. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  22797. * It also requires the `schema` option to be given as a `Schema` instance value.
  22798. *
  22799. * Overrides all previously set schema options.
  22800. */
  22801. setSchema(version, options = {}) {
  22802. if (typeof version === 'number')
  22803. version = String(version);
  22804. let opt;
  22805. switch (version) {
  22806. case '1.1':
  22807. if (this.directives)
  22808. this.directives.yaml.version = '1.1';
  22809. else
  22810. this.directives = new Directives({ version: '1.1' });
  22811. opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
  22812. break;
  22813. case '1.2':
  22814. case 'next':
  22815. if (this.directives)
  22816. this.directives.yaml.version = version;
  22817. else
  22818. this.directives = new Directives({ version });
  22819. opt = { merge: false, resolveKnownTags: true, schema: 'core' };
  22820. break;
  22821. case null:
  22822. if (this.directives)
  22823. delete this.directives;
  22824. opt = null;
  22825. break;
  22826. default: {
  22827. const sv = JSON.stringify(version);
  22828. throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
  22829. }
  22830. }
  22831. // Not using `instanceof Schema` to allow for duck typing
  22832. if (options.schema instanceof Object)
  22833. this.schema = options.schema;
  22834. else if (opt)
  22835. this.schema = new Schema(Object.assign(opt, options));
  22836. else
  22837. throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
  22838. }
  22839. // json & jsonArg are only used from toJSON()
  22840. toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  22841. const ctx = {
  22842. anchors: new Map(),
  22843. doc: this,
  22844. keep: !json,
  22845. mapAsMap: mapAsMap === true,
  22846. mapKeyWarned: false,
  22847. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  22848. };
  22849. const res = toJS(this.contents, jsonArg ?? '', ctx);
  22850. if (typeof onAnchor === 'function')
  22851. for (const { count, res } of ctx.anchors.values())
  22852. onAnchor(res, count);
  22853. return typeof reviver === 'function'
  22854. ? applyReviver(reviver, { '': res }, '', res)
  22855. : res;
  22856. }
  22857. /**
  22858. * A JSON representation of the document `contents`.
  22859. *
  22860. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  22861. * property name.
  22862. */
  22863. toJSON(jsonArg, onAnchor) {
  22864. return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor });
  22865. }
  22866. /** A YAML representation of the document. */
  22867. toString(options = {}) {
  22868. if (this.errors.length > 0)
  22869. throw new Error('Document with errors cannot be stringified');
  22870. if ('indent' in options &&
  22871. (!Number.isInteger(options.indent) || Number(options.indent) <= 0)) {
  22872. const s = JSON.stringify(options.indent);
  22873. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  22874. }
  22875. return stringifyDocument(this, options);
  22876. }
  22877. }
  22878. function assertCollection(contents) {
  22879. if (isCollection$1(contents))
  22880. return true;
  22881. throw new Error('Expected a YAML collection as document contents');
  22882. }
  22883. class YAMLError extends Error {
  22884. constructor(name, pos, code, message) {
  22885. super();
  22886. this.name = name;
  22887. this.code = code;
  22888. this.message = message;
  22889. this.pos = pos;
  22890. }
  22891. }
  22892. class YAMLParseError extends YAMLError {
  22893. constructor(pos, code, message) {
  22894. super('YAMLParseError', pos, code, message);
  22895. }
  22896. }
  22897. class YAMLWarning extends YAMLError {
  22898. constructor(pos, code, message) {
  22899. super('YAMLWarning', pos, code, message);
  22900. }
  22901. }
  22902. const prettifyError = (src, lc) => (error) => {
  22903. if (error.pos[0] === -1)
  22904. return;
  22905. error.linePos = error.pos.map(pos => lc.linePos(pos));
  22906. const { line, col } = error.linePos[0];
  22907. error.message += ` at line ${line}, column ${col}`;
  22908. let ci = col - 1;
  22909. let lineStr = src
  22910. .substring(lc.lineStarts[line - 1], lc.lineStarts[line])
  22911. .replace(/[\n\r]+$/, '');
  22912. // Trim to max 80 chars, keeping col position near the middle
  22913. if (ci >= 60 && lineStr.length > 80) {
  22914. const trimStart = Math.min(ci - 39, lineStr.length - 79);
  22915. lineStr = '…' + lineStr.substring(trimStart);
  22916. ci -= trimStart - 1;
  22917. }
  22918. if (lineStr.length > 80)
  22919. lineStr = lineStr.substring(0, 79) + '…';
  22920. // Include previous line in context if pointing at line start
  22921. if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) {
  22922. // Regexp won't match if start is trimmed
  22923. let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]);
  22924. if (prev.length > 80)
  22925. prev = prev.substring(0, 79) + '…\n';
  22926. lineStr = prev + lineStr;
  22927. }
  22928. if (/[^ ]/.test(lineStr)) {
  22929. let count = 1;
  22930. const end = error.linePos[1];
  22931. if (end && end.line === line && end.col > col) {
  22932. count = Math.max(1, Math.min(end.col - col, 80 - ci));
  22933. }
  22934. const pointer = ' '.repeat(ci) + '^'.repeat(count);
  22935. error.message += `:\n\n${lineStr}\n${pointer}\n`;
  22936. }
  22937. };
  22938. function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnNewline }) {
  22939. let spaceBefore = false;
  22940. let atNewline = startOnNewline;
  22941. let hasSpace = startOnNewline;
  22942. let comment = '';
  22943. let commentSep = '';
  22944. let hasNewline = false;
  22945. let hasNewlineAfterProp = false;
  22946. let reqSpace = false;
  22947. let anchor = null;
  22948. let tag = null;
  22949. let comma = null;
  22950. let found = null;
  22951. let start = null;
  22952. for (const token of tokens) {
  22953. if (reqSpace) {
  22954. if (token.type !== 'space' &&
  22955. token.type !== 'newline' &&
  22956. token.type !== 'comma')
  22957. onError(token.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  22958. reqSpace = false;
  22959. }
  22960. switch (token.type) {
  22961. case 'space':
  22962. // At the doc level, tabs at line start may be parsed
  22963. // as leading white space rather than indentation.
  22964. // In a flow collection, only the parser handles indent.
  22965. if (!flow &&
  22966. atNewline &&
  22967. indicator !== 'doc-start' &&
  22968. token.source[0] === '\t')
  22969. onError(token, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
  22970. hasSpace = true;
  22971. break;
  22972. case 'comment': {
  22973. if (!hasSpace)
  22974. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  22975. const cb = token.source.substring(1) || ' ';
  22976. if (!comment)
  22977. comment = cb;
  22978. else
  22979. comment += commentSep + cb;
  22980. commentSep = '';
  22981. atNewline = false;
  22982. break;
  22983. }
  22984. case 'newline':
  22985. if (atNewline) {
  22986. if (comment)
  22987. comment += token.source;
  22988. else
  22989. spaceBefore = true;
  22990. }
  22991. else
  22992. commentSep += token.source;
  22993. atNewline = true;
  22994. hasNewline = true;
  22995. if (anchor || tag)
  22996. hasNewlineAfterProp = true;
  22997. hasSpace = true;
  22998. break;
  22999. case 'anchor':
  23000. if (anchor)
  23001. onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
  23002. if (token.source.endsWith(':'))
  23003. onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true);
  23004. anchor = token;
  23005. if (start === null)
  23006. start = token.offset;
  23007. atNewline = false;
  23008. hasSpace = false;
  23009. reqSpace = true;
  23010. break;
  23011. case 'tag': {
  23012. if (tag)
  23013. onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag');
  23014. tag = token;
  23015. if (start === null)
  23016. start = token.offset;
  23017. atNewline = false;
  23018. hasSpace = false;
  23019. reqSpace = true;
  23020. break;
  23021. }
  23022. case indicator:
  23023. // Could here handle preceding comments differently
  23024. if (anchor || tag)
  23025. onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
  23026. if (found)
  23027. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
  23028. found = token;
  23029. atNewline = false;
  23030. hasSpace = false;
  23031. break;
  23032. case 'comma':
  23033. if (flow) {
  23034. if (comma)
  23035. onError(token, 'UNEXPECTED_TOKEN', `Unexpected , in ${flow}`);
  23036. comma = token;
  23037. atNewline = false;
  23038. hasSpace = false;
  23039. break;
  23040. }
  23041. // else fallthrough
  23042. default:
  23043. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.type} token`);
  23044. atNewline = false;
  23045. hasSpace = false;
  23046. }
  23047. }
  23048. const last = tokens[tokens.length - 1];
  23049. const end = last ? last.offset + last.source.length : offset;
  23050. if (reqSpace &&
  23051. next &&
  23052. next.type !== 'space' &&
  23053. next.type !== 'newline' &&
  23054. next.type !== 'comma' &&
  23055. (next.type !== 'scalar' || next.source !== ''))
  23056. onError(next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  23057. return {
  23058. comma,
  23059. found,
  23060. spaceBefore,
  23061. comment,
  23062. hasNewline,
  23063. hasNewlineAfterProp,
  23064. anchor,
  23065. tag,
  23066. end,
  23067. start: start ?? end
  23068. };
  23069. }
  23070. function containsNewline(key) {
  23071. if (!key)
  23072. return null;
  23073. switch (key.type) {
  23074. case 'alias':
  23075. case 'scalar':
  23076. case 'double-quoted-scalar':
  23077. case 'single-quoted-scalar':
  23078. if (key.source.includes('\n'))
  23079. return true;
  23080. if (key.end)
  23081. for (const st of key.end)
  23082. if (st.type === 'newline')
  23083. return true;
  23084. return false;
  23085. case 'flow-collection':
  23086. for (const it of key.items) {
  23087. for (const st of it.start)
  23088. if (st.type === 'newline')
  23089. return true;
  23090. if (it.sep)
  23091. for (const st of it.sep)
  23092. if (st.type === 'newline')
  23093. return true;
  23094. if (containsNewline(it.key) || containsNewline(it.value))
  23095. return true;
  23096. }
  23097. return false;
  23098. default:
  23099. return true;
  23100. }
  23101. }
  23102. function flowIndentCheck(indent, fc, onError) {
  23103. if (fc?.type === 'flow-collection') {
  23104. const end = fc.end[0];
  23105. if (end.indent === indent &&
  23106. (end.source === ']' || end.source === '}') &&
  23107. containsNewline(fc)) {
  23108. const msg = 'Flow end indicator should be more indented than parent';
  23109. onError(end, 'BAD_INDENT', msg, true);
  23110. }
  23111. }
  23112. }
  23113. function mapIncludes(ctx, items, search) {
  23114. const { uniqueKeys } = ctx.options;
  23115. if (uniqueKeys === false)
  23116. return false;
  23117. const isEqual = typeof uniqueKeys === 'function'
  23118. ? uniqueKeys
  23119. : (a, b) => a === b ||
  23120. (isScalar$1(a) &&
  23121. isScalar$1(b) &&
  23122. a.value === b.value &&
  23123. !(a.value === '<<' && ctx.schema.merge));
  23124. return items.some(pair => isEqual(pair.key, search));
  23125. }
  23126. const startColMsg = 'All mapping items must start at the same column';
  23127. function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
  23128. const NodeClass = tag?.nodeClass ?? YAMLMap;
  23129. const map = new NodeClass(ctx.schema);
  23130. if (ctx.atRoot)
  23131. ctx.atRoot = false;
  23132. let offset = bm.offset;
  23133. let commentEnd = null;
  23134. for (const collItem of bm.items) {
  23135. const { start, key, sep, value } = collItem;
  23136. // key properties
  23137. const keyProps = resolveProps(start, {
  23138. indicator: 'explicit-key-ind',
  23139. next: key ?? sep?.[0],
  23140. offset,
  23141. onError,
  23142. startOnNewline: true
  23143. });
  23144. const implicitKey = !keyProps.found;
  23145. if (implicitKey) {
  23146. if (key) {
  23147. if (key.type === 'block-seq')
  23148. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
  23149. else if ('indent' in key && key.indent !== bm.indent)
  23150. onError(offset, 'BAD_INDENT', startColMsg);
  23151. }
  23152. if (!keyProps.anchor && !keyProps.tag && !sep) {
  23153. commentEnd = keyProps.end;
  23154. if (keyProps.comment) {
  23155. if (map.comment)
  23156. map.comment += '\n' + keyProps.comment;
  23157. else
  23158. map.comment = keyProps.comment;
  23159. }
  23160. continue;
  23161. }
  23162. if (keyProps.hasNewlineAfterProp || containsNewline(key)) {
  23163. onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
  23164. }
  23165. }
  23166. else if (keyProps.found?.indent !== bm.indent) {
  23167. onError(offset, 'BAD_INDENT', startColMsg);
  23168. }
  23169. // key value
  23170. const keyStart = keyProps.end;
  23171. const keyNode = key
  23172. ? composeNode(ctx, key, keyProps, onError)
  23173. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  23174. if (ctx.schema.compat)
  23175. flowIndentCheck(bm.indent, key, onError);
  23176. if (mapIncludes(ctx, map.items, keyNode))
  23177. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  23178. // value properties
  23179. const valueProps = resolveProps(sep ?? [], {
  23180. indicator: 'map-value-ind',
  23181. next: value,
  23182. offset: keyNode.range[2],
  23183. onError,
  23184. startOnNewline: !key || key.type === 'block-scalar'
  23185. });
  23186. offset = valueProps.end;
  23187. if (valueProps.found) {
  23188. if (implicitKey) {
  23189. if (value?.type === 'block-map' && !valueProps.hasNewline)
  23190. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  23191. if (ctx.options.strict &&
  23192. keyProps.start < valueProps.found.offset - 1024)
  23193. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  23194. }
  23195. // value value
  23196. const valueNode = value
  23197. ? composeNode(ctx, value, valueProps, onError)
  23198. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  23199. if (ctx.schema.compat)
  23200. flowIndentCheck(bm.indent, value, onError);
  23201. offset = valueNode.range[2];
  23202. const pair = new Pair(keyNode, valueNode);
  23203. if (ctx.options.keepSourceTokens)
  23204. pair.srcToken = collItem;
  23205. map.items.push(pair);
  23206. }
  23207. else {
  23208. // key with no value
  23209. if (implicitKey)
  23210. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  23211. if (valueProps.comment) {
  23212. if (keyNode.comment)
  23213. keyNode.comment += '\n' + valueProps.comment;
  23214. else
  23215. keyNode.comment = valueProps.comment;
  23216. }
  23217. const pair = new Pair(keyNode);
  23218. if (ctx.options.keepSourceTokens)
  23219. pair.srcToken = collItem;
  23220. map.items.push(pair);
  23221. }
  23222. }
  23223. if (commentEnd && commentEnd < offset)
  23224. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  23225. map.range = [bm.offset, offset, commentEnd ?? offset];
  23226. return map;
  23227. }
  23228. function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
  23229. const NodeClass = tag?.nodeClass ?? YAMLSeq;
  23230. const seq = new NodeClass(ctx.schema);
  23231. if (ctx.atRoot)
  23232. ctx.atRoot = false;
  23233. let offset = bs.offset;
  23234. let commentEnd = null;
  23235. for (const { start, value } of bs.items) {
  23236. const props = resolveProps(start, {
  23237. indicator: 'seq-item-ind',
  23238. next: value,
  23239. offset,
  23240. onError,
  23241. startOnNewline: true
  23242. });
  23243. if (!props.found) {
  23244. if (props.anchor || props.tag || value) {
  23245. if (value && value.type === 'block-seq')
  23246. onError(props.end, 'BAD_INDENT', 'All sequence items must start at the same column');
  23247. else
  23248. onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
  23249. }
  23250. else {
  23251. commentEnd = props.end;
  23252. if (props.comment)
  23253. seq.comment = props.comment;
  23254. continue;
  23255. }
  23256. }
  23257. const node = value
  23258. ? composeNode(ctx, value, props, onError)
  23259. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  23260. if (ctx.schema.compat)
  23261. flowIndentCheck(bs.indent, value, onError);
  23262. offset = node.range[2];
  23263. seq.items.push(node);
  23264. }
  23265. seq.range = [bs.offset, offset, commentEnd ?? offset];
  23266. return seq;
  23267. }
  23268. function resolveEnd(end, offset, reqSpace, onError) {
  23269. let comment = '';
  23270. if (end) {
  23271. let hasSpace = false;
  23272. let sep = '';
  23273. for (const token of end) {
  23274. const { source, type } = token;
  23275. switch (type) {
  23276. case 'space':
  23277. hasSpace = true;
  23278. break;
  23279. case 'comment': {
  23280. if (reqSpace && !hasSpace)
  23281. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  23282. const cb = source.substring(1) || ' ';
  23283. if (!comment)
  23284. comment = cb;
  23285. else
  23286. comment += sep + cb;
  23287. sep = '';
  23288. break;
  23289. }
  23290. case 'newline':
  23291. if (comment)
  23292. sep += source;
  23293. hasSpace = true;
  23294. break;
  23295. default:
  23296. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
  23297. }
  23298. offset += source.length;
  23299. }
  23300. }
  23301. return { comment, offset };
  23302. }
  23303. const blockMsg = 'Block collections are not allowed within flow collections';
  23304. const isBlock$1 = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
  23305. function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
  23306. const isMap = fc.start.source === '{';
  23307. const fcName = isMap ? 'flow map' : 'flow sequence';
  23308. const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap : YAMLSeq));
  23309. const coll = new NodeClass(ctx.schema);
  23310. coll.flow = true;
  23311. const atRoot = ctx.atRoot;
  23312. if (atRoot)
  23313. ctx.atRoot = false;
  23314. let offset = fc.offset + fc.start.source.length;
  23315. for (let i = 0; i < fc.items.length; ++i) {
  23316. const collItem = fc.items[i];
  23317. const { start, key, sep, value } = collItem;
  23318. const props = resolveProps(start, {
  23319. flow: fcName,
  23320. indicator: 'explicit-key-ind',
  23321. next: key ?? sep?.[0],
  23322. offset,
  23323. onError,
  23324. startOnNewline: false
  23325. });
  23326. if (!props.found) {
  23327. if (!props.anchor && !props.tag && !sep && !value) {
  23328. if (i === 0 && props.comma)
  23329. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  23330. else if (i < fc.items.length - 1)
  23331. onError(props.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${fcName}`);
  23332. if (props.comment) {
  23333. if (coll.comment)
  23334. coll.comment += '\n' + props.comment;
  23335. else
  23336. coll.comment = props.comment;
  23337. }
  23338. offset = props.end;
  23339. continue;
  23340. }
  23341. if (!isMap && ctx.options.strict && containsNewline(key))
  23342. onError(key, // checked by containsNewline()
  23343. 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  23344. }
  23345. if (i === 0) {
  23346. if (props.comma)
  23347. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  23348. }
  23349. else {
  23350. if (!props.comma)
  23351. onError(props.start, 'MISSING_CHAR', `Missing , between ${fcName} items`);
  23352. if (props.comment) {
  23353. let prevItemComment = '';
  23354. loop: for (const st of start) {
  23355. switch (st.type) {
  23356. case 'comma':
  23357. case 'space':
  23358. break;
  23359. case 'comment':
  23360. prevItemComment = st.source.substring(1);
  23361. break loop;
  23362. default:
  23363. break loop;
  23364. }
  23365. }
  23366. if (prevItemComment) {
  23367. let prev = coll.items[coll.items.length - 1];
  23368. if (isPair(prev))
  23369. prev = prev.value ?? prev.key;
  23370. if (prev.comment)
  23371. prev.comment += '\n' + prevItemComment;
  23372. else
  23373. prev.comment = prevItemComment;
  23374. props.comment = props.comment.substring(prevItemComment.length + 1);
  23375. }
  23376. }
  23377. }
  23378. if (!isMap && !sep && !props.found) {
  23379. // item is a value in a seq
  23380. // → key & sep are empty, start does not include ? or :
  23381. const valueNode = value
  23382. ? composeNode(ctx, value, props, onError)
  23383. : composeEmptyNode(ctx, props.end, sep, null, props, onError);
  23384. coll.items.push(valueNode);
  23385. offset = valueNode.range[2];
  23386. if (isBlock$1(value))
  23387. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  23388. }
  23389. else {
  23390. // item is a key+value pair
  23391. // key value
  23392. const keyStart = props.end;
  23393. const keyNode = key
  23394. ? composeNode(ctx, key, props, onError)
  23395. : composeEmptyNode(ctx, keyStart, start, null, props, onError);
  23396. if (isBlock$1(key))
  23397. onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
  23398. // value properties
  23399. const valueProps = resolveProps(sep ?? [], {
  23400. flow: fcName,
  23401. indicator: 'map-value-ind',
  23402. next: value,
  23403. offset: keyNode.range[2],
  23404. onError,
  23405. startOnNewline: false
  23406. });
  23407. if (valueProps.found) {
  23408. if (!isMap && !props.found && ctx.options.strict) {
  23409. if (sep)
  23410. for (const st of sep) {
  23411. if (st === valueProps.found)
  23412. break;
  23413. if (st.type === 'newline') {
  23414. onError(st, 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  23415. break;
  23416. }
  23417. }
  23418. if (props.start < valueProps.found.offset - 1024)
  23419. onError(valueProps.found, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
  23420. }
  23421. }
  23422. else if (value) {
  23423. if ('source' in value && value.source && value.source[0] === ':')
  23424. onError(value, 'MISSING_CHAR', `Missing space after : in ${fcName}`);
  23425. else
  23426. onError(valueProps.start, 'MISSING_CHAR', `Missing , or : between ${fcName} items`);
  23427. }
  23428. // value value
  23429. const valueNode = value
  23430. ? composeNode(ctx, value, valueProps, onError)
  23431. : valueProps.found
  23432. ? composeEmptyNode(ctx, valueProps.end, sep, null, valueProps, onError)
  23433. : null;
  23434. if (valueNode) {
  23435. if (isBlock$1(value))
  23436. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  23437. }
  23438. else if (valueProps.comment) {
  23439. if (keyNode.comment)
  23440. keyNode.comment += '\n' + valueProps.comment;
  23441. else
  23442. keyNode.comment = valueProps.comment;
  23443. }
  23444. const pair = new Pair(keyNode, valueNode);
  23445. if (ctx.options.keepSourceTokens)
  23446. pair.srcToken = collItem;
  23447. if (isMap) {
  23448. const map = coll;
  23449. if (mapIncludes(ctx, map.items, keyNode))
  23450. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  23451. map.items.push(pair);
  23452. }
  23453. else {
  23454. const map = new YAMLMap(ctx.schema);
  23455. map.flow = true;
  23456. map.items.push(pair);
  23457. coll.items.push(map);
  23458. }
  23459. offset = valueNode ? valueNode.range[2] : valueProps.end;
  23460. }
  23461. }
  23462. const expectedEnd = isMap ? '}' : ']';
  23463. const [ce, ...ee] = fc.end;
  23464. let cePos = offset;
  23465. if (ce && ce.source === expectedEnd)
  23466. cePos = ce.offset + ce.source.length;
  23467. else {
  23468. const name = fcName[0].toUpperCase() + fcName.substring(1);
  23469. const msg = atRoot
  23470. ? `${name} must end with a ${expectedEnd}`
  23471. : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
  23472. onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
  23473. if (ce && ce.source.length !== 1)
  23474. ee.unshift(ce);
  23475. }
  23476. if (ee.length > 0) {
  23477. const end = resolveEnd(ee, cePos, ctx.options.strict, onError);
  23478. if (end.comment) {
  23479. if (coll.comment)
  23480. coll.comment += '\n' + end.comment;
  23481. else
  23482. coll.comment = end.comment;
  23483. }
  23484. coll.range = [fc.offset, cePos, end.offset];
  23485. }
  23486. else {
  23487. coll.range = [fc.offset, cePos, cePos];
  23488. }
  23489. return coll;
  23490. }
  23491. function resolveCollection(CN, ctx, token, onError, tagName, tag) {
  23492. const coll = token.type === 'block-map'
  23493. ? resolveBlockMap(CN, ctx, token, onError, tag)
  23494. : token.type === 'block-seq'
  23495. ? resolveBlockSeq(CN, ctx, token, onError, tag)
  23496. : resolveFlowCollection(CN, ctx, token, onError, tag);
  23497. const Coll = coll.constructor;
  23498. // If we got a tagName matching the class, or the tag name is '!',
  23499. // then use the tagName from the node class used to create it.
  23500. if (tagName === '!' || tagName === Coll.tagName) {
  23501. coll.tag = Coll.tagName;
  23502. return coll;
  23503. }
  23504. if (tagName)
  23505. coll.tag = tagName;
  23506. return coll;
  23507. }
  23508. function composeCollection(CN, ctx, token, tagToken, onError) {
  23509. const tagName = !tagToken
  23510. ? null
  23511. : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
  23512. const expType = token.type === 'block-map'
  23513. ? 'map'
  23514. : token.type === 'block-seq'
  23515. ? 'seq'
  23516. : token.start.source === '{'
  23517. ? 'map'
  23518. : 'seq';
  23519. // shortcut: check if it's a generic YAMLMap or YAMLSeq
  23520. // before jumping into the custom tag logic.
  23521. if (!tagToken ||
  23522. !tagName ||
  23523. tagName === '!' ||
  23524. (tagName === YAMLMap.tagName && expType === 'map') ||
  23525. (tagName === YAMLSeq.tagName && expType === 'seq') ||
  23526. !expType) {
  23527. return resolveCollection(CN, ctx, token, onError, tagName);
  23528. }
  23529. let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
  23530. if (!tag) {
  23531. const kt = ctx.schema.knownTags[tagName];
  23532. if (kt && kt.collection === expType) {
  23533. ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
  23534. tag = kt;
  23535. }
  23536. else {
  23537. if (kt?.collection) {
  23538. onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
  23539. }
  23540. else {
  23541. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
  23542. }
  23543. return resolveCollection(CN, ctx, token, onError, tagName);
  23544. }
  23545. }
  23546. const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
  23547. const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
  23548. const node = isNode$1(res)
  23549. ? res
  23550. : new Scalar(res);
  23551. node.range = coll.range;
  23552. node.tag = tagName;
  23553. if (tag?.format)
  23554. node.format = tag.format;
  23555. return node;
  23556. }
  23557. function resolveBlockScalar(scalar, strict, onError) {
  23558. const start = scalar.offset;
  23559. const header = parseBlockScalarHeader(scalar, strict, onError);
  23560. if (!header)
  23561. return { value: '', type: null, comment: '', range: [start, start, start] };
  23562. const type = header.mode === '>' ? Scalar.BLOCK_FOLDED : Scalar.BLOCK_LITERAL;
  23563. const lines = scalar.source ? splitLines(scalar.source) : [];
  23564. // determine the end of content & start of chomping
  23565. let chompStart = lines.length;
  23566. for (let i = lines.length - 1; i >= 0; --i) {
  23567. const content = lines[i][1];
  23568. if (content === '' || content === '\r')
  23569. chompStart = i;
  23570. else
  23571. break;
  23572. }
  23573. // shortcut for empty contents
  23574. if (chompStart === 0) {
  23575. const value = header.chomp === '+' && lines.length > 0
  23576. ? '\n'.repeat(Math.max(1, lines.length - 1))
  23577. : '';
  23578. let end = start + header.length;
  23579. if (scalar.source)
  23580. end += scalar.source.length;
  23581. return { value, type, comment: header.comment, range: [start, end, end] };
  23582. }
  23583. // find the indentation level to trim from start
  23584. let trimIndent = scalar.indent + header.indent;
  23585. let offset = scalar.offset + header.length;
  23586. let contentStart = 0;
  23587. for (let i = 0; i < chompStart; ++i) {
  23588. const [indent, content] = lines[i];
  23589. if (content === '' || content === '\r') {
  23590. if (header.indent === 0 && indent.length > trimIndent)
  23591. trimIndent = indent.length;
  23592. }
  23593. else {
  23594. if (indent.length < trimIndent) {
  23595. const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
  23596. onError(offset + indent.length, 'MISSING_CHAR', message);
  23597. }
  23598. if (header.indent === 0)
  23599. trimIndent = indent.length;
  23600. contentStart = i;
  23601. break;
  23602. }
  23603. offset += indent.length + content.length + 1;
  23604. }
  23605. // include trailing more-indented empty lines in content
  23606. for (let i = lines.length - 1; i >= chompStart; --i) {
  23607. if (lines[i][0].length > trimIndent)
  23608. chompStart = i + 1;
  23609. }
  23610. let value = '';
  23611. let sep = '';
  23612. let prevMoreIndented = false;
  23613. // leading whitespace is kept intact
  23614. for (let i = 0; i < contentStart; ++i)
  23615. value += lines[i][0].slice(trimIndent) + '\n';
  23616. for (let i = contentStart; i < chompStart; ++i) {
  23617. let [indent, content] = lines[i];
  23618. offset += indent.length + content.length + 1;
  23619. const crlf = content[content.length - 1] === '\r';
  23620. if (crlf)
  23621. content = content.slice(0, -1);
  23622. /* istanbul ignore if already caught in lexer */
  23623. if (content && indent.length < trimIndent) {
  23624. const src = header.indent
  23625. ? 'explicit indentation indicator'
  23626. : 'first line';
  23627. const message = `Block scalar lines must not be less indented than their ${src}`;
  23628. onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
  23629. indent = '';
  23630. }
  23631. if (type === Scalar.BLOCK_LITERAL) {
  23632. value += sep + indent.slice(trimIndent) + content;
  23633. sep = '\n';
  23634. }
  23635. else if (indent.length > trimIndent || content[0] === '\t') {
  23636. // more-indented content within a folded block
  23637. if (sep === ' ')
  23638. sep = '\n';
  23639. else if (!prevMoreIndented && sep === '\n')
  23640. sep = '\n\n';
  23641. value += sep + indent.slice(trimIndent) + content;
  23642. sep = '\n';
  23643. prevMoreIndented = true;
  23644. }
  23645. else if (content === '') {
  23646. // empty line
  23647. if (sep === '\n')
  23648. value += '\n';
  23649. else
  23650. sep = '\n';
  23651. }
  23652. else {
  23653. value += sep + content;
  23654. sep = ' ';
  23655. prevMoreIndented = false;
  23656. }
  23657. }
  23658. switch (header.chomp) {
  23659. case '-':
  23660. break;
  23661. case '+':
  23662. for (let i = chompStart; i < lines.length; ++i)
  23663. value += '\n' + lines[i][0].slice(trimIndent);
  23664. if (value[value.length - 1] !== '\n')
  23665. value += '\n';
  23666. break;
  23667. default:
  23668. value += '\n';
  23669. }
  23670. const end = start + header.length + scalar.source.length;
  23671. return { value, type, comment: header.comment, range: [start, end, end] };
  23672. }
  23673. function parseBlockScalarHeader({ offset, props }, strict, onError) {
  23674. /* istanbul ignore if should not happen */
  23675. if (props[0].type !== 'block-scalar-header') {
  23676. onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
  23677. return null;
  23678. }
  23679. const { source } = props[0];
  23680. const mode = source[0];
  23681. let indent = 0;
  23682. let chomp = '';
  23683. let error = -1;
  23684. for (let i = 1; i < source.length; ++i) {
  23685. const ch = source[i];
  23686. if (!chomp && (ch === '-' || ch === '+'))
  23687. chomp = ch;
  23688. else {
  23689. const n = Number(ch);
  23690. if (!indent && n)
  23691. indent = n;
  23692. else if (error === -1)
  23693. error = offset + i;
  23694. }
  23695. }
  23696. if (error !== -1)
  23697. onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
  23698. let hasSpace = false;
  23699. let comment = '';
  23700. let length = source.length;
  23701. for (let i = 1; i < props.length; ++i) {
  23702. const token = props[i];
  23703. switch (token.type) {
  23704. case 'space':
  23705. hasSpace = true;
  23706. // fallthrough
  23707. case 'newline':
  23708. length += token.source.length;
  23709. break;
  23710. case 'comment':
  23711. if (strict && !hasSpace) {
  23712. const message = 'Comments must be separated from other tokens by white space characters';
  23713. onError(token, 'MISSING_CHAR', message);
  23714. }
  23715. length += token.source.length;
  23716. comment = token.source.substring(1);
  23717. break;
  23718. case 'error':
  23719. onError(token, 'UNEXPECTED_TOKEN', token.message);
  23720. length += token.source.length;
  23721. break;
  23722. /* istanbul ignore next should not happen */
  23723. default: {
  23724. const message = `Unexpected token in block scalar header: ${token.type}`;
  23725. onError(token, 'UNEXPECTED_TOKEN', message);
  23726. const ts = token.source;
  23727. if (ts && typeof ts === 'string')
  23728. length += ts.length;
  23729. }
  23730. }
  23731. }
  23732. return { mode, indent, chomp, comment, length };
  23733. }
  23734. /** @returns Array of lines split up as `[indent, content]` */
  23735. function splitLines(source) {
  23736. const split = source.split(/\n( *)/);
  23737. const first = split[0];
  23738. const m = first.match(/^( *)/);
  23739. const line0 = m?.[1]
  23740. ? [m[1], first.slice(m[1].length)]
  23741. : ['', first];
  23742. const lines = [line0];
  23743. for (let i = 1; i < split.length; i += 2)
  23744. lines.push([split[i], split[i + 1]]);
  23745. return lines;
  23746. }
  23747. function resolveFlowScalar(scalar, strict, onError) {
  23748. const { offset, type, source, end } = scalar;
  23749. let _type;
  23750. let value;
  23751. const _onError = (rel, code, msg) => onError(offset + rel, code, msg);
  23752. switch (type) {
  23753. case 'scalar':
  23754. _type = Scalar.PLAIN;
  23755. value = plainValue(source, _onError);
  23756. break;
  23757. case 'single-quoted-scalar':
  23758. _type = Scalar.QUOTE_SINGLE;
  23759. value = singleQuotedValue(source, _onError);
  23760. break;
  23761. case 'double-quoted-scalar':
  23762. _type = Scalar.QUOTE_DOUBLE;
  23763. value = doubleQuotedValue(source, _onError);
  23764. break;
  23765. /* istanbul ignore next should not happen */
  23766. default:
  23767. onError(scalar, 'UNEXPECTED_TOKEN', `Expected a flow scalar value, but found: ${type}`);
  23768. return {
  23769. value: '',
  23770. type: null,
  23771. comment: '',
  23772. range: [offset, offset + source.length, offset + source.length]
  23773. };
  23774. }
  23775. const valueEnd = offset + source.length;
  23776. const re = resolveEnd(end, valueEnd, strict, onError);
  23777. return {
  23778. value,
  23779. type: _type,
  23780. comment: re.comment,
  23781. range: [offset, valueEnd, re.offset]
  23782. };
  23783. }
  23784. function plainValue(source, onError) {
  23785. let badChar = '';
  23786. switch (source[0]) {
  23787. /* istanbul ignore next should not happen */
  23788. case '\t':
  23789. badChar = 'a tab character';
  23790. break;
  23791. case ',':
  23792. badChar = 'flow indicator character ,';
  23793. break;
  23794. case '%':
  23795. badChar = 'directive indicator character %';
  23796. break;
  23797. case '|':
  23798. case '>': {
  23799. badChar = `block scalar indicator ${source[0]}`;
  23800. break;
  23801. }
  23802. case '@':
  23803. case '`': {
  23804. badChar = `reserved character ${source[0]}`;
  23805. break;
  23806. }
  23807. }
  23808. if (badChar)
  23809. onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
  23810. return foldLines(source);
  23811. }
  23812. function singleQuotedValue(source, onError) {
  23813. if (source[source.length - 1] !== "'" || source.length === 1)
  23814. onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
  23815. return foldLines(source.slice(1, -1)).replace(/''/g, "'");
  23816. }
  23817. function foldLines(source) {
  23818. /**
  23819. * The negative lookbehind here and in the `re` RegExp is to
  23820. * prevent causing a polynomial search time in certain cases.
  23821. *
  23822. * The try-catch is for Safari, which doesn't support this yet:
  23823. * https://caniuse.com/js-regexp-lookbehind
  23824. */
  23825. let first, line;
  23826. try {
  23827. first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
  23828. line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
  23829. }
  23830. catch (_) {
  23831. first = /(.*?)[ \t]*\r?\n/sy;
  23832. line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
  23833. }
  23834. let match = first.exec(source);
  23835. if (!match)
  23836. return source;
  23837. let res = match[1];
  23838. let sep = ' ';
  23839. let pos = first.lastIndex;
  23840. line.lastIndex = pos;
  23841. while ((match = line.exec(source))) {
  23842. if (match[1] === '') {
  23843. if (sep === '\n')
  23844. res += sep;
  23845. else
  23846. sep = '\n';
  23847. }
  23848. else {
  23849. res += sep + match[1];
  23850. sep = ' ';
  23851. }
  23852. pos = line.lastIndex;
  23853. }
  23854. const last = /[ \t]*(.*)/sy;
  23855. last.lastIndex = pos;
  23856. match = last.exec(source);
  23857. return res + sep + (match?.[1] ?? '');
  23858. }
  23859. function doubleQuotedValue(source, onError) {
  23860. let res = '';
  23861. for (let i = 1; i < source.length - 1; ++i) {
  23862. const ch = source[i];
  23863. if (ch === '\r' && source[i + 1] === '\n')
  23864. continue;
  23865. if (ch === '\n') {
  23866. const { fold, offset } = foldNewline(source, i);
  23867. res += fold;
  23868. i = offset;
  23869. }
  23870. else if (ch === '\\') {
  23871. let next = source[++i];
  23872. const cc = escapeCodes[next];
  23873. if (cc)
  23874. res += cc;
  23875. else if (next === '\n') {
  23876. // skip escaped newlines, but still trim the following line
  23877. next = source[i + 1];
  23878. while (next === ' ' || next === '\t')
  23879. next = source[++i + 1];
  23880. }
  23881. else if (next === '\r' && source[i + 1] === '\n') {
  23882. // skip escaped CRLF newlines, but still trim the following line
  23883. next = source[++i + 1];
  23884. while (next === ' ' || next === '\t')
  23885. next = source[++i + 1];
  23886. }
  23887. else if (next === 'x' || next === 'u' || next === 'U') {
  23888. const length = { x: 2, u: 4, U: 8 }[next];
  23889. res += parseCharCode(source, i + 1, length, onError);
  23890. i += length;
  23891. }
  23892. else {
  23893. const raw = source.substr(i - 1, 2);
  23894. onError(i - 1, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  23895. res += raw;
  23896. }
  23897. }
  23898. else if (ch === ' ' || ch === '\t') {
  23899. // trim trailing whitespace
  23900. const wsStart = i;
  23901. let next = source[i + 1];
  23902. while (next === ' ' || next === '\t')
  23903. next = source[++i + 1];
  23904. if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
  23905. res += i > wsStart ? source.slice(wsStart, i + 1) : ch;
  23906. }
  23907. else {
  23908. res += ch;
  23909. }
  23910. }
  23911. if (source[source.length - 1] !== '"' || source.length === 1)
  23912. onError(source.length, 'MISSING_CHAR', 'Missing closing "quote');
  23913. return res;
  23914. }
  23915. /**
  23916. * Fold a single newline into a space, multiple newlines to N - 1 newlines.
  23917. * Presumes `source[offset] === '\n'`
  23918. */
  23919. function foldNewline(source, offset) {
  23920. let fold = '';
  23921. let ch = source[offset + 1];
  23922. while (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
  23923. if (ch === '\r' && source[offset + 2] !== '\n')
  23924. break;
  23925. if (ch === '\n')
  23926. fold += '\n';
  23927. offset += 1;
  23928. ch = source[offset + 1];
  23929. }
  23930. if (!fold)
  23931. fold = ' ';
  23932. return { fold, offset };
  23933. }
  23934. const escapeCodes = {
  23935. '0': '\0',
  23936. a: '\x07',
  23937. b: '\b',
  23938. e: '\x1b',
  23939. f: '\f',
  23940. n: '\n',
  23941. r: '\r',
  23942. t: '\t',
  23943. v: '\v',
  23944. N: '\u0085',
  23945. _: '\u00a0',
  23946. L: '\u2028',
  23947. P: '\u2029',
  23948. ' ': ' ',
  23949. '"': '"',
  23950. '/': '/',
  23951. '\\': '\\',
  23952. '\t': '\t'
  23953. };
  23954. function parseCharCode(source, offset, length, onError) {
  23955. const cc = source.substr(offset, length);
  23956. const ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc);
  23957. const code = ok ? parseInt(cc, 16) : NaN;
  23958. if (isNaN(code)) {
  23959. const raw = source.substr(offset - 2, length + 2);
  23960. onError(offset - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  23961. return raw;
  23962. }
  23963. return String.fromCodePoint(code);
  23964. }
  23965. function composeScalar(ctx, token, tagToken, onError) {
  23966. const { value, type, comment, range } = token.type === 'block-scalar'
  23967. ? resolveBlockScalar(token, ctx.options.strict, onError)
  23968. : resolveFlowScalar(token, ctx.options.strict, onError);
  23969. const tagName = tagToken
  23970. ? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
  23971. : null;
  23972. const tag = tagToken && tagName
  23973. ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
  23974. : token.type === 'scalar'
  23975. ? findScalarTagByTest(ctx, value, token, onError)
  23976. : ctx.schema[SCALAR$1];
  23977. let scalar;
  23978. try {
  23979. const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
  23980. scalar = isScalar$1(res) ? res : new Scalar(res);
  23981. }
  23982. catch (error) {
  23983. const msg = error instanceof Error ? error.message : String(error);
  23984. onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
  23985. scalar = new Scalar(value);
  23986. }
  23987. scalar.range = range;
  23988. scalar.source = value;
  23989. if (type)
  23990. scalar.type = type;
  23991. if (tagName)
  23992. scalar.tag = tagName;
  23993. if (tag.format)
  23994. scalar.format = tag.format;
  23995. if (comment)
  23996. scalar.comment = comment;
  23997. return scalar;
  23998. }
  23999. function findScalarTagByName(schema, value, tagName, tagToken, onError) {
  24000. if (tagName === '!')
  24001. return schema[SCALAR$1]; // non-specific tag
  24002. const matchWithTest = [];
  24003. for (const tag of schema.tags) {
  24004. if (!tag.collection && tag.tag === tagName) {
  24005. if (tag.default && tag.test)
  24006. matchWithTest.push(tag);
  24007. else
  24008. return tag;
  24009. }
  24010. }
  24011. for (const tag of matchWithTest)
  24012. if (tag.test?.test(value))
  24013. return tag;
  24014. const kt = schema.knownTags[tagName];
  24015. if (kt && !kt.collection) {
  24016. // Ensure that the known tag is available for stringifying,
  24017. // but does not get used by default.
  24018. schema.tags.push(Object.assign({}, kt, { default: false, test: undefined }));
  24019. return kt;
  24020. }
  24021. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
  24022. return schema[SCALAR$1];
  24023. }
  24024. function findScalarTagByTest({ directives, schema }, value, token, onError) {
  24025. const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[SCALAR$1];
  24026. if (schema.compat) {
  24027. const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
  24028. schema[SCALAR$1];
  24029. if (tag.tag !== compat.tag) {
  24030. const ts = directives.tagString(tag.tag);
  24031. const cs = directives.tagString(compat.tag);
  24032. const msg = `Value may be parsed as either ${ts} or ${cs}`;
  24033. onError(token, 'TAG_RESOLVE_FAILED', msg, true);
  24034. }
  24035. }
  24036. return tag;
  24037. }
  24038. function emptyScalarPosition(offset, before, pos) {
  24039. if (before) {
  24040. if (pos === null)
  24041. pos = before.length;
  24042. for (let i = pos - 1; i >= 0; --i) {
  24043. let st = before[i];
  24044. switch (st.type) {
  24045. case 'space':
  24046. case 'comment':
  24047. case 'newline':
  24048. offset -= st.source.length;
  24049. continue;
  24050. }
  24051. // Technically, an empty scalar is immediately after the last non-empty
  24052. // node, but it's more useful to place it after any whitespace.
  24053. st = before[++i];
  24054. while (st?.type === 'space') {
  24055. offset += st.source.length;
  24056. st = before[++i];
  24057. }
  24058. break;
  24059. }
  24060. }
  24061. return offset;
  24062. }
  24063. const CN = { composeNode, composeEmptyNode };
  24064. function composeNode(ctx, token, props, onError) {
  24065. const { spaceBefore, comment, anchor, tag } = props;
  24066. let node;
  24067. let isSrcToken = true;
  24068. switch (token.type) {
  24069. case 'alias':
  24070. node = composeAlias(ctx, token, onError);
  24071. if (anchor || tag)
  24072. onError(token, 'ALIAS_PROPS', 'An alias node must not specify any properties');
  24073. break;
  24074. case 'scalar':
  24075. case 'single-quoted-scalar':
  24076. case 'double-quoted-scalar':
  24077. case 'block-scalar':
  24078. node = composeScalar(ctx, token, tag, onError);
  24079. if (anchor)
  24080. node.anchor = anchor.source.substring(1);
  24081. break;
  24082. case 'block-map':
  24083. case 'block-seq':
  24084. case 'flow-collection':
  24085. node = composeCollection(CN, ctx, token, tag, onError);
  24086. if (anchor)
  24087. node.anchor = anchor.source.substring(1);
  24088. break;
  24089. default: {
  24090. const message = token.type === 'error'
  24091. ? token.message
  24092. : `Unsupported token (type: ${token.type})`;
  24093. onError(token, 'UNEXPECTED_TOKEN', message);
  24094. node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
  24095. isSrcToken = false;
  24096. }
  24097. }
  24098. if (anchor && node.anchor === '')
  24099. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  24100. if (spaceBefore)
  24101. node.spaceBefore = true;
  24102. if (comment) {
  24103. if (token.type === 'scalar' && token.source === '')
  24104. node.comment = comment;
  24105. else
  24106. node.commentBefore = comment;
  24107. }
  24108. // @ts-expect-error Type checking misses meaning of isSrcToken
  24109. if (ctx.options.keepSourceTokens && isSrcToken)
  24110. node.srcToken = token;
  24111. return node;
  24112. }
  24113. function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag, end }, onError) {
  24114. const token = {
  24115. type: 'scalar',
  24116. offset: emptyScalarPosition(offset, before, pos),
  24117. indent: -1,
  24118. source: ''
  24119. };
  24120. const node = composeScalar(ctx, token, tag, onError);
  24121. if (anchor) {
  24122. node.anchor = anchor.source.substring(1);
  24123. if (node.anchor === '')
  24124. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  24125. }
  24126. if (spaceBefore)
  24127. node.spaceBefore = true;
  24128. if (comment) {
  24129. node.comment = comment;
  24130. node.range[2] = end;
  24131. }
  24132. return node;
  24133. }
  24134. function composeAlias({ options }, { offset, source, end }, onError) {
  24135. const alias = new Alias(source.substring(1));
  24136. if (alias.source === '')
  24137. onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
  24138. if (alias.source.endsWith(':'))
  24139. onError(offset + source.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', true);
  24140. const valueEnd = offset + source.length;
  24141. const re = resolveEnd(end, valueEnd, options.strict, onError);
  24142. alias.range = [offset, valueEnd, re.offset];
  24143. if (re.comment)
  24144. alias.comment = re.comment;
  24145. return alias;
  24146. }
  24147. function composeDoc(options, directives, { offset, start, value, end }, onError) {
  24148. const opts = Object.assign({ _directives: directives }, options);
  24149. const doc = new Document(undefined, opts);
  24150. const ctx = {
  24151. atRoot: true,
  24152. directives: doc.directives,
  24153. options: doc.options,
  24154. schema: doc.schema
  24155. };
  24156. const props = resolveProps(start, {
  24157. indicator: 'doc-start',
  24158. next: value ?? end?.[0],
  24159. offset,
  24160. onError,
  24161. startOnNewline: true
  24162. });
  24163. if (props.found) {
  24164. doc.directives.docStart = true;
  24165. if (value &&
  24166. (value.type === 'block-map' || value.type === 'block-seq') &&
  24167. !props.hasNewline)
  24168. onError(props.end, 'MISSING_CHAR', 'Block collection cannot start on same line with directives-end marker');
  24169. }
  24170. // @ts-expect-error If Contents is set, let's trust the user
  24171. doc.contents = value
  24172. ? composeNode(ctx, value, props, onError)
  24173. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  24174. const contentEnd = doc.contents.range[2];
  24175. const re = resolveEnd(end, contentEnd, false, onError);
  24176. if (re.comment)
  24177. doc.comment = re.comment;
  24178. doc.range = [offset, contentEnd, re.offset];
  24179. return doc;
  24180. }
  24181. function getErrorPos(src) {
  24182. if (typeof src === 'number')
  24183. return [src, src + 1];
  24184. if (Array.isArray(src))
  24185. return src.length === 2 ? src : [src[0], src[1]];
  24186. const { offset, source } = src;
  24187. return [offset, offset + (typeof source === 'string' ? source.length : 1)];
  24188. }
  24189. function parsePrelude(prelude) {
  24190. let comment = '';
  24191. let atComment = false;
  24192. let afterEmptyLine = false;
  24193. for (let i = 0; i < prelude.length; ++i) {
  24194. const source = prelude[i];
  24195. switch (source[0]) {
  24196. case '#':
  24197. comment +=
  24198. (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
  24199. (source.substring(1) || ' ');
  24200. atComment = true;
  24201. afterEmptyLine = false;
  24202. break;
  24203. case '%':
  24204. if (prelude[i + 1]?.[0] !== '#')
  24205. i += 1;
  24206. atComment = false;
  24207. break;
  24208. default:
  24209. // This may be wrong after doc-end, but in that case it doesn't matter
  24210. if (!atComment)
  24211. afterEmptyLine = true;
  24212. atComment = false;
  24213. }
  24214. }
  24215. return { comment, afterEmptyLine };
  24216. }
  24217. /**
  24218. * Compose a stream of CST nodes into a stream of YAML Documents.
  24219. *
  24220. * ```ts
  24221. * import { Composer, Parser } from 'yaml'
  24222. *
  24223. * const src: string = ...
  24224. * const tokens = new Parser().parse(src)
  24225. * const docs = new Composer().compose(tokens)
  24226. * ```
  24227. */
  24228. class Composer {
  24229. constructor(options = {}) {
  24230. this.doc = null;
  24231. this.atDirectives = false;
  24232. this.prelude = [];
  24233. this.errors = [];
  24234. this.warnings = [];
  24235. this.onError = (source, code, message, warning) => {
  24236. const pos = getErrorPos(source);
  24237. if (warning)
  24238. this.warnings.push(new YAMLWarning(pos, code, message));
  24239. else
  24240. this.errors.push(new YAMLParseError(pos, code, message));
  24241. };
  24242. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  24243. this.directives = new Directives({ version: options.version || '1.2' });
  24244. this.options = options;
  24245. }
  24246. decorate(doc, afterDoc) {
  24247. const { comment, afterEmptyLine } = parsePrelude(this.prelude);
  24248. //console.log({ dc: doc.comment, prelude, comment })
  24249. if (comment) {
  24250. const dc = doc.contents;
  24251. if (afterDoc) {
  24252. doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
  24253. }
  24254. else if (afterEmptyLine || doc.directives.docStart || !dc) {
  24255. doc.commentBefore = comment;
  24256. }
  24257. else if (isCollection$1(dc) && !dc.flow && dc.items.length > 0) {
  24258. let it = dc.items[0];
  24259. if (isPair(it))
  24260. it = it.key;
  24261. const cb = it.commentBefore;
  24262. it.commentBefore = cb ? `${comment}\n${cb}` : comment;
  24263. }
  24264. else {
  24265. const cb = dc.commentBefore;
  24266. dc.commentBefore = cb ? `${comment}\n${cb}` : comment;
  24267. }
  24268. }
  24269. if (afterDoc) {
  24270. Array.prototype.push.apply(doc.errors, this.errors);
  24271. Array.prototype.push.apply(doc.warnings, this.warnings);
  24272. }
  24273. else {
  24274. doc.errors = this.errors;
  24275. doc.warnings = this.warnings;
  24276. }
  24277. this.prelude = [];
  24278. this.errors = [];
  24279. this.warnings = [];
  24280. }
  24281. /**
  24282. * Current stream status information.
  24283. *
  24284. * Mostly useful at the end of input for an empty stream.
  24285. */
  24286. streamInfo() {
  24287. return {
  24288. comment: parsePrelude(this.prelude).comment,
  24289. directives: this.directives,
  24290. errors: this.errors,
  24291. warnings: this.warnings
  24292. };
  24293. }
  24294. /**
  24295. * Compose tokens into documents.
  24296. *
  24297. * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
  24298. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  24299. */
  24300. *compose(tokens, forceDoc = false, endOffset = -1) {
  24301. for (const token of tokens)
  24302. yield* this.next(token);
  24303. yield* this.end(forceDoc, endOffset);
  24304. }
  24305. /** Advance the composer by one CST token. */
  24306. *next(token) {
  24307. switch (token.type) {
  24308. case 'directive':
  24309. this.directives.add(token.source, (offset, message, warning) => {
  24310. const pos = getErrorPos(token);
  24311. pos[0] += offset;
  24312. this.onError(pos, 'BAD_DIRECTIVE', message, warning);
  24313. });
  24314. this.prelude.push(token.source);
  24315. this.atDirectives = true;
  24316. break;
  24317. case 'document': {
  24318. const doc = composeDoc(this.options, this.directives, token, this.onError);
  24319. if (this.atDirectives && !doc.directives.docStart)
  24320. this.onError(token, 'MISSING_CHAR', 'Missing directives-end/doc-start indicator line');
  24321. this.decorate(doc, false);
  24322. if (this.doc)
  24323. yield this.doc;
  24324. this.doc = doc;
  24325. this.atDirectives = false;
  24326. break;
  24327. }
  24328. case 'byte-order-mark':
  24329. case 'space':
  24330. break;
  24331. case 'comment':
  24332. case 'newline':
  24333. this.prelude.push(token.source);
  24334. break;
  24335. case 'error': {
  24336. const msg = token.source
  24337. ? `${token.message}: ${JSON.stringify(token.source)}`
  24338. : token.message;
  24339. const error = new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg);
  24340. if (this.atDirectives || !this.doc)
  24341. this.errors.push(error);
  24342. else
  24343. this.doc.errors.push(error);
  24344. break;
  24345. }
  24346. case 'doc-end': {
  24347. if (!this.doc) {
  24348. const msg = 'Unexpected doc-end without preceding document';
  24349. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
  24350. break;
  24351. }
  24352. this.doc.directives.docEnd = true;
  24353. const end = resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
  24354. this.decorate(this.doc, true);
  24355. if (end.comment) {
  24356. const dc = this.doc.comment;
  24357. this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
  24358. }
  24359. this.doc.range[2] = end.offset;
  24360. break;
  24361. }
  24362. default:
  24363. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', `Unsupported token ${token.type}`));
  24364. }
  24365. }
  24366. /**
  24367. * Call at end of input to yield any remaining document.
  24368. *
  24369. * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
  24370. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  24371. */
  24372. *end(forceDoc = false, endOffset = -1) {
  24373. if (this.doc) {
  24374. this.decorate(this.doc, true);
  24375. yield this.doc;
  24376. this.doc = null;
  24377. }
  24378. else if (forceDoc) {
  24379. const opts = Object.assign({ _directives: this.directives }, this.options);
  24380. const doc = new Document(undefined, opts);
  24381. if (this.atDirectives)
  24382. this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
  24383. doc.range = [0, endOffset, endOffset];
  24384. this.decorate(doc, false);
  24385. yield doc;
  24386. }
  24387. }
  24388. }
  24389. function resolveAsScalar(token, strict = true, onError) {
  24390. if (token) {
  24391. const _onError = (pos, code, message) => {
  24392. const offset = typeof pos === 'number' ? pos : Array.isArray(pos) ? pos[0] : pos.offset;
  24393. if (onError)
  24394. onError(offset, code, message);
  24395. else
  24396. throw new YAMLParseError([offset, offset + 1], code, message);
  24397. };
  24398. switch (token.type) {
  24399. case 'scalar':
  24400. case 'single-quoted-scalar':
  24401. case 'double-quoted-scalar':
  24402. return resolveFlowScalar(token, strict, _onError);
  24403. case 'block-scalar':
  24404. return resolveBlockScalar(token, strict, _onError);
  24405. }
  24406. }
  24407. return null;
  24408. }
  24409. /**
  24410. * Create a new scalar token with `value`
  24411. *
  24412. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  24413. * as this function does not support any schema operations and won't check for such conflicts.
  24414. *
  24415. * @param value The string representation of the value, which will have its content properly indented.
  24416. * @param context.end Comments and whitespace after the end of the value, or after the block scalar header. If undefined, a newline will be added.
  24417. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  24418. * @param context.indent The indent level of the token.
  24419. * @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
  24420. * @param context.offset The offset position of the token.
  24421. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  24422. */
  24423. function createScalarToken(value, context) {
  24424. const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
  24425. const source = stringifyString({ type, value }, {
  24426. implicitKey,
  24427. indent: indent > 0 ? ' '.repeat(indent) : '',
  24428. inFlow,
  24429. options: { blockQuote: true, lineWidth: -1 }
  24430. });
  24431. const end = context.end ?? [
  24432. { type: 'newline', offset: -1, indent, source: '\n' }
  24433. ];
  24434. switch (source[0]) {
  24435. case '|':
  24436. case '>': {
  24437. const he = source.indexOf('\n');
  24438. const head = source.substring(0, he);
  24439. const body = source.substring(he + 1) + '\n';
  24440. const props = [
  24441. { type: 'block-scalar-header', offset, indent, source: head }
  24442. ];
  24443. if (!addEndtoBlockProps(props, end))
  24444. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  24445. return { type: 'block-scalar', offset, indent, props, source: body };
  24446. }
  24447. case '"':
  24448. return { type: 'double-quoted-scalar', offset, indent, source, end };
  24449. case "'":
  24450. return { type: 'single-quoted-scalar', offset, indent, source, end };
  24451. default:
  24452. return { type: 'scalar', offset, indent, source, end };
  24453. }
  24454. }
  24455. /**
  24456. * Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
  24457. *
  24458. * Best efforts are made to retain any comments previously associated with the `token`,
  24459. * though all contents within a collection's `items` will be overwritten.
  24460. *
  24461. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  24462. * as this function does not support any schema operations and won't check for such conflicts.
  24463. *
  24464. * @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
  24465. * @param value The string representation of the value, which will have its content properly indented.
  24466. * @param context.afterKey In most cases, values after a key should have an additional level of indentation.
  24467. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  24468. * @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
  24469. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  24470. */
  24471. function setScalarValue(token, value, context = {}) {
  24472. let { afterKey = false, implicitKey = false, inFlow = false, type } = context;
  24473. let indent = 'indent' in token ? token.indent : null;
  24474. if (afterKey && typeof indent === 'number')
  24475. indent += 2;
  24476. if (!type)
  24477. switch (token.type) {
  24478. case 'single-quoted-scalar':
  24479. type = 'QUOTE_SINGLE';
  24480. break;
  24481. case 'double-quoted-scalar':
  24482. type = 'QUOTE_DOUBLE';
  24483. break;
  24484. case 'block-scalar': {
  24485. const header = token.props[0];
  24486. if (header.type !== 'block-scalar-header')
  24487. throw new Error('Invalid block scalar header');
  24488. type = header.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
  24489. break;
  24490. }
  24491. default:
  24492. type = 'PLAIN';
  24493. }
  24494. const source = stringifyString({ type, value }, {
  24495. implicitKey: implicitKey || indent === null,
  24496. indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
  24497. inFlow,
  24498. options: { blockQuote: true, lineWidth: -1 }
  24499. });
  24500. switch (source[0]) {
  24501. case '|':
  24502. case '>':
  24503. setBlockScalarValue(token, source);
  24504. break;
  24505. case '"':
  24506. setFlowScalarValue(token, source, 'double-quoted-scalar');
  24507. break;
  24508. case "'":
  24509. setFlowScalarValue(token, source, 'single-quoted-scalar');
  24510. break;
  24511. default:
  24512. setFlowScalarValue(token, source, 'scalar');
  24513. }
  24514. }
  24515. function setBlockScalarValue(token, source) {
  24516. const he = source.indexOf('\n');
  24517. const head = source.substring(0, he);
  24518. const body = source.substring(he + 1) + '\n';
  24519. if (token.type === 'block-scalar') {
  24520. const header = token.props[0];
  24521. if (header.type !== 'block-scalar-header')
  24522. throw new Error('Invalid block scalar header');
  24523. header.source = head;
  24524. token.source = body;
  24525. }
  24526. else {
  24527. const { offset } = token;
  24528. const indent = 'indent' in token ? token.indent : -1;
  24529. const props = [
  24530. { type: 'block-scalar-header', offset, indent, source: head }
  24531. ];
  24532. if (!addEndtoBlockProps(props, 'end' in token ? token.end : undefined))
  24533. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  24534. for (const key of Object.keys(token))
  24535. if (key !== 'type' && key !== 'offset')
  24536. delete token[key];
  24537. Object.assign(token, { type: 'block-scalar', indent, props, source: body });
  24538. }
  24539. }
  24540. /** @returns `true` if last token is a newline */
  24541. function addEndtoBlockProps(props, end) {
  24542. if (end)
  24543. for (const st of end)
  24544. switch (st.type) {
  24545. case 'space':
  24546. case 'comment':
  24547. props.push(st);
  24548. break;
  24549. case 'newline':
  24550. props.push(st);
  24551. return true;
  24552. }
  24553. return false;
  24554. }
  24555. function setFlowScalarValue(token, source, type) {
  24556. switch (token.type) {
  24557. case 'scalar':
  24558. case 'double-quoted-scalar':
  24559. case 'single-quoted-scalar':
  24560. token.type = type;
  24561. token.source = source;
  24562. break;
  24563. case 'block-scalar': {
  24564. const end = token.props.slice(1);
  24565. let oa = source.length;
  24566. if (token.props[0].type === 'block-scalar-header')
  24567. oa -= token.props[0].source.length;
  24568. for (const tok of end)
  24569. tok.offset += oa;
  24570. delete token.props;
  24571. Object.assign(token, { type, source, end });
  24572. break;
  24573. }
  24574. case 'block-map':
  24575. case 'block-seq': {
  24576. const offset = token.offset + source.length;
  24577. const nl = { type: 'newline', offset, indent: token.indent, source: '\n' };
  24578. delete token.items;
  24579. Object.assign(token, { type, source, end: [nl] });
  24580. break;
  24581. }
  24582. default: {
  24583. const indent = 'indent' in token ? token.indent : -1;
  24584. const end = 'end' in token && Array.isArray(token.end)
  24585. ? token.end.filter(st => st.type === 'space' ||
  24586. st.type === 'comment' ||
  24587. st.type === 'newline')
  24588. : [];
  24589. for (const key of Object.keys(token))
  24590. if (key !== 'type' && key !== 'offset')
  24591. delete token[key];
  24592. Object.assign(token, { type, indent, source, end });
  24593. }
  24594. }
  24595. }
  24596. /**
  24597. * Stringify a CST document, token, or collection item
  24598. *
  24599. * Fair warning: This applies no validation whatsoever, and
  24600. * simply concatenates the sources in their logical order.
  24601. */
  24602. const stringify$1 = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
  24603. function stringifyToken(token) {
  24604. switch (token.type) {
  24605. case 'block-scalar': {
  24606. let res = '';
  24607. for (const tok of token.props)
  24608. res += stringifyToken(tok);
  24609. return res + token.source;
  24610. }
  24611. case 'block-map':
  24612. case 'block-seq': {
  24613. let res = '';
  24614. for (const item of token.items)
  24615. res += stringifyItem(item);
  24616. return res;
  24617. }
  24618. case 'flow-collection': {
  24619. let res = token.start.source;
  24620. for (const item of token.items)
  24621. res += stringifyItem(item);
  24622. for (const st of token.end)
  24623. res += st.source;
  24624. return res;
  24625. }
  24626. case 'document': {
  24627. let res = stringifyItem(token);
  24628. if (token.end)
  24629. for (const st of token.end)
  24630. res += st.source;
  24631. return res;
  24632. }
  24633. default: {
  24634. let res = token.source;
  24635. if ('end' in token && token.end)
  24636. for (const st of token.end)
  24637. res += st.source;
  24638. return res;
  24639. }
  24640. }
  24641. }
  24642. function stringifyItem({ start, key, sep, value }) {
  24643. let res = '';
  24644. for (const st of start)
  24645. res += st.source;
  24646. if (key)
  24647. res += stringifyToken(key);
  24648. if (sep)
  24649. for (const st of sep)
  24650. res += st.source;
  24651. if (value)
  24652. res += stringifyToken(value);
  24653. return res;
  24654. }
  24655. const BREAK = Symbol('break visit');
  24656. const SKIP = Symbol('skip children');
  24657. const REMOVE = Symbol('remove item');
  24658. /**
  24659. * Apply a visitor to a CST document or item.
  24660. *
  24661. * Walks through the tree (depth-first) starting from the root, calling a
  24662. * `visitor` function with two arguments when entering each item:
  24663. * - `item`: The current item, which included the following members:
  24664. * - `start: SourceToken[]` – Source tokens before the key or value,
  24665. * possibly including its anchor or tag.
  24666. * - `key?: Token | null` – Set for pair values. May then be `null`, if
  24667. * the key before the `:` separator is empty.
  24668. * - `sep?: SourceToken[]` – Source tokens between the key and the value,
  24669. * which should include the `:` map value indicator if `value` is set.
  24670. * - `value?: Token` – The value of a sequence item, or of a map pair.
  24671. * - `path`: The steps from the root to the current node, as an array of
  24672. * `['key' | 'value', number]` tuples.
  24673. *
  24674. * The return value of the visitor may be used to control the traversal:
  24675. * - `undefined` (default): Do nothing and continue
  24676. * - `visit.SKIP`: Do not visit the children of this token, continue with
  24677. * next sibling
  24678. * - `visit.BREAK`: Terminate traversal completely
  24679. * - `visit.REMOVE`: Remove the current item, then continue with the next one
  24680. * - `number`: Set the index of the next step. This is useful especially if
  24681. * the index of the current token has changed.
  24682. * - `function`: Define the next visitor for this item. After the original
  24683. * visitor is called on item entry, next visitors are called after handling
  24684. * a non-empty `key` and when exiting the item.
  24685. */
  24686. function visit(cst, visitor) {
  24687. if ('type' in cst && cst.type === 'document')
  24688. cst = { start: cst.start, value: cst.value };
  24689. _visit(Object.freeze([]), cst, visitor);
  24690. }
  24691. // Without the `as symbol` casts, TS declares these in the `visit`
  24692. // namespace using `var`, but then complains about that because
  24693. // `unique symbol` must be `const`.
  24694. /** Terminate visit traversal completely */
  24695. visit.BREAK = BREAK;
  24696. /** Do not visit the children of the current item */
  24697. visit.SKIP = SKIP;
  24698. /** Remove the current item */
  24699. visit.REMOVE = REMOVE;
  24700. /** Find the item at `path` from `cst` as the root */
  24701. visit.itemAtPath = (cst, path) => {
  24702. let item = cst;
  24703. for (const [field, index] of path) {
  24704. const tok = item?.[field];
  24705. if (tok && 'items' in tok) {
  24706. item = tok.items[index];
  24707. }
  24708. else
  24709. return undefined;
  24710. }
  24711. return item;
  24712. };
  24713. /**
  24714. * Get the immediate parent collection of the item at `path` from `cst` as the root.
  24715. *
  24716. * Throws an error if the collection is not found, which should never happen if the item itself exists.
  24717. */
  24718. visit.parentCollection = (cst, path) => {
  24719. const parent = visit.itemAtPath(cst, path.slice(0, -1));
  24720. const field = path[path.length - 1][0];
  24721. const coll = parent?.[field];
  24722. if (coll && 'items' in coll)
  24723. return coll;
  24724. throw new Error('Parent collection not found');
  24725. };
  24726. function _visit(path, item, visitor) {
  24727. let ctrl = visitor(item, path);
  24728. if (typeof ctrl === 'symbol')
  24729. return ctrl;
  24730. for (const field of ['key', 'value']) {
  24731. const token = item[field];
  24732. if (token && 'items' in token) {
  24733. for (let i = 0; i < token.items.length; ++i) {
  24734. const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
  24735. if (typeof ci === 'number')
  24736. i = ci - 1;
  24737. else if (ci === BREAK)
  24738. return BREAK;
  24739. else if (ci === REMOVE) {
  24740. token.items.splice(i, 1);
  24741. i -= 1;
  24742. }
  24743. }
  24744. if (typeof ctrl === 'function' && field === 'key')
  24745. ctrl = ctrl(item, path);
  24746. }
  24747. }
  24748. return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
  24749. }
  24750. /** The byte order mark */
  24751. const BOM = '\u{FEFF}';
  24752. /** Start of doc-mode */
  24753. const DOCUMENT = '\x02'; // C0: Start of Text
  24754. /** Unexpected end of flow-mode */
  24755. const FLOW_END = '\x18'; // C0: Cancel
  24756. /** Next token is a scalar value */
  24757. const SCALAR = '\x1f'; // C0: Unit Separator
  24758. /** @returns `true` if `token` is a flow or block collection */
  24759. const isCollection = (token) => !!token && 'items' in token;
  24760. /** @returns `true` if `token` is a flow or block scalar; not an alias */
  24761. const isScalar = (token) => !!token &&
  24762. (token.type === 'scalar' ||
  24763. token.type === 'single-quoted-scalar' ||
  24764. token.type === 'double-quoted-scalar' ||
  24765. token.type === 'block-scalar');
  24766. /* istanbul ignore next */
  24767. /** Get a printable representation of a lexer token */
  24768. function prettyToken(token) {
  24769. switch (token) {
  24770. case BOM:
  24771. return '<BOM>';
  24772. case DOCUMENT:
  24773. return '<DOC>';
  24774. case FLOW_END:
  24775. return '<FLOW_END>';
  24776. case SCALAR:
  24777. return '<SCALAR>';
  24778. default:
  24779. return JSON.stringify(token);
  24780. }
  24781. }
  24782. /** Identify the type of a lexer token. May return `null` for unknown tokens. */
  24783. function tokenType(source) {
  24784. switch (source) {
  24785. case BOM:
  24786. return 'byte-order-mark';
  24787. case DOCUMENT:
  24788. return 'doc-mode';
  24789. case FLOW_END:
  24790. return 'flow-error-end';
  24791. case SCALAR:
  24792. return 'scalar';
  24793. case '---':
  24794. return 'doc-start';
  24795. case '...':
  24796. return 'doc-end';
  24797. case '':
  24798. case '\n':
  24799. case '\r\n':
  24800. return 'newline';
  24801. case '-':
  24802. return 'seq-item-ind';
  24803. case '?':
  24804. return 'explicit-key-ind';
  24805. case ':':
  24806. return 'map-value-ind';
  24807. case '{':
  24808. return 'flow-map-start';
  24809. case '}':
  24810. return 'flow-map-end';
  24811. case '[':
  24812. return 'flow-seq-start';
  24813. case ']':
  24814. return 'flow-seq-end';
  24815. case ',':
  24816. return 'comma';
  24817. }
  24818. switch (source[0]) {
  24819. case ' ':
  24820. case '\t':
  24821. return 'space';
  24822. case '#':
  24823. return 'comment';
  24824. case '%':
  24825. return 'directive-line';
  24826. case '*':
  24827. return 'alias';
  24828. case '&':
  24829. return 'anchor';
  24830. case '!':
  24831. return 'tag';
  24832. case "'":
  24833. return 'single-quoted-scalar';
  24834. case '"':
  24835. return 'double-quoted-scalar';
  24836. case '|':
  24837. case '>':
  24838. return 'block-scalar-header';
  24839. }
  24840. return null;
  24841. }
  24842. var cst = {
  24843. __proto__: null,
  24844. BOM: BOM,
  24845. DOCUMENT: DOCUMENT,
  24846. FLOW_END: FLOW_END,
  24847. SCALAR: SCALAR,
  24848. createScalarToken: createScalarToken,
  24849. isCollection: isCollection,
  24850. isScalar: isScalar,
  24851. prettyToken: prettyToken,
  24852. resolveAsScalar: resolveAsScalar,
  24853. setScalarValue: setScalarValue,
  24854. stringify: stringify$1,
  24855. tokenType: tokenType,
  24856. visit: visit
  24857. };
  24858. /*
  24859. START -> stream
  24860. stream
  24861. directive -> line-end -> stream
  24862. indent + line-end -> stream
  24863. [else] -> line-start
  24864. line-end
  24865. comment -> line-end
  24866. newline -> .
  24867. input-end -> END
  24868. line-start
  24869. doc-start -> doc
  24870. doc-end -> stream
  24871. [else] -> indent -> block-start
  24872. block-start
  24873. seq-item-start -> block-start
  24874. explicit-key-start -> block-start
  24875. map-value-start -> block-start
  24876. [else] -> doc
  24877. doc
  24878. line-end -> line-start
  24879. spaces -> doc
  24880. anchor -> doc
  24881. tag -> doc
  24882. flow-start -> flow -> doc
  24883. flow-end -> error -> doc
  24884. seq-item-start -> error -> doc
  24885. explicit-key-start -> error -> doc
  24886. map-value-start -> doc
  24887. alias -> doc
  24888. quote-start -> quoted-scalar -> doc
  24889. block-scalar-header -> line-end -> block-scalar(min) -> line-start
  24890. [else] -> plain-scalar(false, min) -> doc
  24891. flow
  24892. line-end -> flow
  24893. spaces -> flow
  24894. anchor -> flow
  24895. tag -> flow
  24896. flow-start -> flow -> flow
  24897. flow-end -> .
  24898. seq-item-start -> error -> flow
  24899. explicit-key-start -> flow
  24900. map-value-start -> flow
  24901. alias -> flow
  24902. quote-start -> quoted-scalar -> flow
  24903. comma -> flow
  24904. [else] -> plain-scalar(true, 0) -> flow
  24905. quoted-scalar
  24906. quote-end -> .
  24907. [else] -> quoted-scalar
  24908. block-scalar(min)
  24909. newline + peek(indent < min) -> .
  24910. [else] -> block-scalar(min)
  24911. plain-scalar(is-flow, min)
  24912. scalar-end(is-flow) -> .
  24913. peek(newline + (indent < min)) -> .
  24914. [else] -> plain-scalar(min)
  24915. */
  24916. function isEmpty(ch) {
  24917. switch (ch) {
  24918. case undefined:
  24919. case ' ':
  24920. case '\n':
  24921. case '\r':
  24922. case '\t':
  24923. return true;
  24924. default:
  24925. return false;
  24926. }
  24927. }
  24928. const hexDigits = '0123456789ABCDEFabcdef'.split('');
  24929. const tagChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#;/?:@&=+$_.!~*'()".split('');
  24930. const invalidFlowScalarChars = ',[]{}'.split('');
  24931. const invalidAnchorChars = ' ,[]{}\n\r\t'.split('');
  24932. const isNotAnchorChar = (ch) => !ch || invalidAnchorChars.includes(ch);
  24933. /**
  24934. * Splits an input string into lexical tokens, i.e. smaller strings that are
  24935. * easily identifiable by `tokens.tokenType()`.
  24936. *
  24937. * Lexing starts always in a "stream" context. Incomplete input may be buffered
  24938. * until a complete token can be emitted.
  24939. *
  24940. * In addition to slices of the original input, the following control characters
  24941. * may also be emitted:
  24942. *
  24943. * - `\x02` (Start of Text): A document starts with the next token
  24944. * - `\x18` (Cancel): Unexpected end of flow-mode (indicates an error)
  24945. * - `\x1f` (Unit Separator): Next token is a scalar value
  24946. * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
  24947. */
  24948. class Lexer {
  24949. constructor() {
  24950. /**
  24951. * Flag indicating whether the end of the current buffer marks the end of
  24952. * all input
  24953. */
  24954. this.atEnd = false;
  24955. /**
  24956. * Explicit indent set in block scalar header, as an offset from the current
  24957. * minimum indent, so e.g. set to 1 from a header `|2+`. Set to -1 if not
  24958. * explicitly set.
  24959. */
  24960. this.blockScalarIndent = -1;
  24961. /**
  24962. * Block scalars that include a + (keep) chomping indicator in their header
  24963. * include trailing empty lines, which are otherwise excluded from the
  24964. * scalar's contents.
  24965. */
  24966. this.blockScalarKeep = false;
  24967. /** Current input */
  24968. this.buffer = '';
  24969. /**
  24970. * Flag noting whether the map value indicator : can immediately follow this
  24971. * node within a flow context.
  24972. */
  24973. this.flowKey = false;
  24974. /** Count of surrounding flow collection levels. */
  24975. this.flowLevel = 0;
  24976. /**
  24977. * Minimum level of indentation required for next lines to be parsed as a
  24978. * part of the current scalar value.
  24979. */
  24980. this.indentNext = 0;
  24981. /** Indentation level of the current line. */
  24982. this.indentValue = 0;
  24983. /** Position of the next \n character. */
  24984. this.lineEndPos = null;
  24985. /** Stores the state of the lexer if reaching the end of incpomplete input */
  24986. this.next = null;
  24987. /** A pointer to `buffer`; the current position of the lexer. */
  24988. this.pos = 0;
  24989. }
  24990. /**
  24991. * Generate YAML tokens from the `source` string. If `incomplete`,
  24992. * a part of the last line may be left as a buffer for the next call.
  24993. *
  24994. * @returns A generator of lexical tokens
  24995. */
  24996. *lex(source, incomplete = false) {
  24997. if (source) {
  24998. this.buffer = this.buffer ? this.buffer + source : source;
  24999. this.lineEndPos = null;
  25000. }
  25001. this.atEnd = !incomplete;
  25002. let next = this.next ?? 'stream';
  25003. while (next && (incomplete || this.hasChars(1)))
  25004. next = yield* this.parseNext(next);
  25005. }
  25006. atLineEnd() {
  25007. let i = this.pos;
  25008. let ch = this.buffer[i];
  25009. while (ch === ' ' || ch === '\t')
  25010. ch = this.buffer[++i];
  25011. if (!ch || ch === '#' || ch === '\n')
  25012. return true;
  25013. if (ch === '\r')
  25014. return this.buffer[i + 1] === '\n';
  25015. return false;
  25016. }
  25017. charAt(n) {
  25018. return this.buffer[this.pos + n];
  25019. }
  25020. continueScalar(offset) {
  25021. let ch = this.buffer[offset];
  25022. if (this.indentNext > 0) {
  25023. let indent = 0;
  25024. while (ch === ' ')
  25025. ch = this.buffer[++indent + offset];
  25026. if (ch === '\r') {
  25027. const next = this.buffer[indent + offset + 1];
  25028. if (next === '\n' || (!next && !this.atEnd))
  25029. return offset + indent + 1;
  25030. }
  25031. return ch === '\n' || indent >= this.indentNext || (!ch && !this.atEnd)
  25032. ? offset + indent
  25033. : -1;
  25034. }
  25035. if (ch === '-' || ch === '.') {
  25036. const dt = this.buffer.substr(offset, 3);
  25037. if ((dt === '---' || dt === '...') && isEmpty(this.buffer[offset + 3]))
  25038. return -1;
  25039. }
  25040. return offset;
  25041. }
  25042. getLine() {
  25043. let end = this.lineEndPos;
  25044. if (typeof end !== 'number' || (end !== -1 && end < this.pos)) {
  25045. end = this.buffer.indexOf('\n', this.pos);
  25046. this.lineEndPos = end;
  25047. }
  25048. if (end === -1)
  25049. return this.atEnd ? this.buffer.substring(this.pos) : null;
  25050. if (this.buffer[end - 1] === '\r')
  25051. end -= 1;
  25052. return this.buffer.substring(this.pos, end);
  25053. }
  25054. hasChars(n) {
  25055. return this.pos + n <= this.buffer.length;
  25056. }
  25057. setNext(state) {
  25058. this.buffer = this.buffer.substring(this.pos);
  25059. this.pos = 0;
  25060. this.lineEndPos = null;
  25061. this.next = state;
  25062. return null;
  25063. }
  25064. peek(n) {
  25065. return this.buffer.substr(this.pos, n);
  25066. }
  25067. *parseNext(next) {
  25068. switch (next) {
  25069. case 'stream':
  25070. return yield* this.parseStream();
  25071. case 'line-start':
  25072. return yield* this.parseLineStart();
  25073. case 'block-start':
  25074. return yield* this.parseBlockStart();
  25075. case 'doc':
  25076. return yield* this.parseDocument();
  25077. case 'flow':
  25078. return yield* this.parseFlowCollection();
  25079. case 'quoted-scalar':
  25080. return yield* this.parseQuotedScalar();
  25081. case 'block-scalar':
  25082. return yield* this.parseBlockScalar();
  25083. case 'plain-scalar':
  25084. return yield* this.parsePlainScalar();
  25085. }
  25086. }
  25087. *parseStream() {
  25088. let line = this.getLine();
  25089. if (line === null)
  25090. return this.setNext('stream');
  25091. if (line[0] === BOM) {
  25092. yield* this.pushCount(1);
  25093. line = line.substring(1);
  25094. }
  25095. if (line[0] === '%') {
  25096. let dirEnd = line.length;
  25097. const cs = line.indexOf('#');
  25098. if (cs !== -1) {
  25099. const ch = line[cs - 1];
  25100. if (ch === ' ' || ch === '\t')
  25101. dirEnd = cs - 1;
  25102. }
  25103. while (true) {
  25104. const ch = line[dirEnd - 1];
  25105. if (ch === ' ' || ch === '\t')
  25106. dirEnd -= 1;
  25107. else
  25108. break;
  25109. }
  25110. const n = (yield* this.pushCount(dirEnd)) + (yield* this.pushSpaces(true));
  25111. yield* this.pushCount(line.length - n); // possible comment
  25112. this.pushNewline();
  25113. return 'stream';
  25114. }
  25115. if (this.atLineEnd()) {
  25116. const sp = yield* this.pushSpaces(true);
  25117. yield* this.pushCount(line.length - sp);
  25118. yield* this.pushNewline();
  25119. return 'stream';
  25120. }
  25121. yield DOCUMENT;
  25122. return yield* this.parseLineStart();
  25123. }
  25124. *parseLineStart() {
  25125. const ch = this.charAt(0);
  25126. if (!ch && !this.atEnd)
  25127. return this.setNext('line-start');
  25128. if (ch === '-' || ch === '.') {
  25129. if (!this.atEnd && !this.hasChars(4))
  25130. return this.setNext('line-start');
  25131. const s = this.peek(3);
  25132. if (s === '---' && isEmpty(this.charAt(3))) {
  25133. yield* this.pushCount(3);
  25134. this.indentValue = 0;
  25135. this.indentNext = 0;
  25136. return 'doc';
  25137. }
  25138. else if (s === '...' && isEmpty(this.charAt(3))) {
  25139. yield* this.pushCount(3);
  25140. return 'stream';
  25141. }
  25142. }
  25143. this.indentValue = yield* this.pushSpaces(false);
  25144. if (this.indentNext > this.indentValue && !isEmpty(this.charAt(1)))
  25145. this.indentNext = this.indentValue;
  25146. return yield* this.parseBlockStart();
  25147. }
  25148. *parseBlockStart() {
  25149. const [ch0, ch1] = this.peek(2);
  25150. if (!ch1 && !this.atEnd)
  25151. return this.setNext('block-start');
  25152. if ((ch0 === '-' || ch0 === '?' || ch0 === ':') && isEmpty(ch1)) {
  25153. const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
  25154. this.indentNext = this.indentValue + 1;
  25155. this.indentValue += n;
  25156. return yield* this.parseBlockStart();
  25157. }
  25158. return 'doc';
  25159. }
  25160. *parseDocument() {
  25161. yield* this.pushSpaces(true);
  25162. const line = this.getLine();
  25163. if (line === null)
  25164. return this.setNext('doc');
  25165. let n = yield* this.pushIndicators();
  25166. switch (line[n]) {
  25167. case '#':
  25168. yield* this.pushCount(line.length - n);
  25169. // fallthrough
  25170. case undefined:
  25171. yield* this.pushNewline();
  25172. return yield* this.parseLineStart();
  25173. case '{':
  25174. case '[':
  25175. yield* this.pushCount(1);
  25176. this.flowKey = false;
  25177. this.flowLevel = 1;
  25178. return 'flow';
  25179. case '}':
  25180. case ']':
  25181. // this is an error
  25182. yield* this.pushCount(1);
  25183. return 'doc';
  25184. case '*':
  25185. yield* this.pushUntil(isNotAnchorChar);
  25186. return 'doc';
  25187. case '"':
  25188. case "'":
  25189. return yield* this.parseQuotedScalar();
  25190. case '|':
  25191. case '>':
  25192. n += yield* this.parseBlockScalarHeader();
  25193. n += yield* this.pushSpaces(true);
  25194. yield* this.pushCount(line.length - n);
  25195. yield* this.pushNewline();
  25196. return yield* this.parseBlockScalar();
  25197. default:
  25198. return yield* this.parsePlainScalar();
  25199. }
  25200. }
  25201. *parseFlowCollection() {
  25202. let nl, sp;
  25203. let indent = -1;
  25204. do {
  25205. nl = yield* this.pushNewline();
  25206. if (nl > 0) {
  25207. sp = yield* this.pushSpaces(false);
  25208. this.indentValue = indent = sp;
  25209. }
  25210. else {
  25211. sp = 0;
  25212. }
  25213. sp += yield* this.pushSpaces(true);
  25214. } while (nl + sp > 0);
  25215. const line = this.getLine();
  25216. if (line === null)
  25217. return this.setNext('flow');
  25218. if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') ||
  25219. (indent === 0 &&
  25220. (line.startsWith('---') || line.startsWith('...')) &&
  25221. isEmpty(line[3]))) {
  25222. // Allowing for the terminal ] or } at the same (rather than greater)
  25223. // indent level as the initial [ or { is technically invalid, but
  25224. // failing here would be surprising to users.
  25225. const atFlowEndMarker = indent === this.indentNext - 1 &&
  25226. this.flowLevel === 1 &&
  25227. (line[0] === ']' || line[0] === '}');
  25228. if (!atFlowEndMarker) {
  25229. // this is an error
  25230. this.flowLevel = 0;
  25231. yield FLOW_END;
  25232. return yield* this.parseLineStart();
  25233. }
  25234. }
  25235. let n = 0;
  25236. while (line[n] === ',') {
  25237. n += yield* this.pushCount(1);
  25238. n += yield* this.pushSpaces(true);
  25239. this.flowKey = false;
  25240. }
  25241. n += yield* this.pushIndicators();
  25242. switch (line[n]) {
  25243. case undefined:
  25244. return 'flow';
  25245. case '#':
  25246. yield* this.pushCount(line.length - n);
  25247. return 'flow';
  25248. case '{':
  25249. case '[':
  25250. yield* this.pushCount(1);
  25251. this.flowKey = false;
  25252. this.flowLevel += 1;
  25253. return 'flow';
  25254. case '}':
  25255. case ']':
  25256. yield* this.pushCount(1);
  25257. this.flowKey = true;
  25258. this.flowLevel -= 1;
  25259. return this.flowLevel ? 'flow' : 'doc';
  25260. case '*':
  25261. yield* this.pushUntil(isNotAnchorChar);
  25262. return 'flow';
  25263. case '"':
  25264. case "'":
  25265. this.flowKey = true;
  25266. return yield* this.parseQuotedScalar();
  25267. case ':': {
  25268. const next = this.charAt(1);
  25269. if (this.flowKey || isEmpty(next) || next === ',') {
  25270. this.flowKey = false;
  25271. yield* this.pushCount(1);
  25272. yield* this.pushSpaces(true);
  25273. return 'flow';
  25274. }
  25275. }
  25276. // fallthrough
  25277. default:
  25278. this.flowKey = false;
  25279. return yield* this.parsePlainScalar();
  25280. }
  25281. }
  25282. *parseQuotedScalar() {
  25283. const quote = this.charAt(0);
  25284. let end = this.buffer.indexOf(quote, this.pos + 1);
  25285. if (quote === "'") {
  25286. while (end !== -1 && this.buffer[end + 1] === "'")
  25287. end = this.buffer.indexOf("'", end + 2);
  25288. }
  25289. else {
  25290. // double-quote
  25291. while (end !== -1) {
  25292. let n = 0;
  25293. while (this.buffer[end - 1 - n] === '\\')
  25294. n += 1;
  25295. if (n % 2 === 0)
  25296. break;
  25297. end = this.buffer.indexOf('"', end + 1);
  25298. }
  25299. }
  25300. // Only looking for newlines within the quotes
  25301. const qb = this.buffer.substring(0, end);
  25302. let nl = qb.indexOf('\n', this.pos);
  25303. if (nl !== -1) {
  25304. while (nl !== -1) {
  25305. const cs = this.continueScalar(nl + 1);
  25306. if (cs === -1)
  25307. break;
  25308. nl = qb.indexOf('\n', cs);
  25309. }
  25310. if (nl !== -1) {
  25311. // this is an error caused by an unexpected unindent
  25312. end = nl - (qb[nl - 1] === '\r' ? 2 : 1);
  25313. }
  25314. }
  25315. if (end === -1) {
  25316. if (!this.atEnd)
  25317. return this.setNext('quoted-scalar');
  25318. end = this.buffer.length;
  25319. }
  25320. yield* this.pushToIndex(end + 1, false);
  25321. return this.flowLevel ? 'flow' : 'doc';
  25322. }
  25323. *parseBlockScalarHeader() {
  25324. this.blockScalarIndent = -1;
  25325. this.blockScalarKeep = false;
  25326. let i = this.pos;
  25327. while (true) {
  25328. const ch = this.buffer[++i];
  25329. if (ch === '+')
  25330. this.blockScalarKeep = true;
  25331. else if (ch > '0' && ch <= '9')
  25332. this.blockScalarIndent = Number(ch) - 1;
  25333. else if (ch !== '-')
  25334. break;
  25335. }
  25336. return yield* this.pushUntil(ch => isEmpty(ch) || ch === '#');
  25337. }
  25338. *parseBlockScalar() {
  25339. let nl = this.pos - 1; // may be -1 if this.pos === 0
  25340. let indent = 0;
  25341. let ch;
  25342. loop: for (let i = this.pos; (ch = this.buffer[i]); ++i) {
  25343. switch (ch) {
  25344. case ' ':
  25345. indent += 1;
  25346. break;
  25347. case '\n':
  25348. nl = i;
  25349. indent = 0;
  25350. break;
  25351. case '\r': {
  25352. const next = this.buffer[i + 1];
  25353. if (!next && !this.atEnd)
  25354. return this.setNext('block-scalar');
  25355. if (next === '\n')
  25356. break;
  25357. } // fallthrough
  25358. default:
  25359. break loop;
  25360. }
  25361. }
  25362. if (!ch && !this.atEnd)
  25363. return this.setNext('block-scalar');
  25364. if (indent >= this.indentNext) {
  25365. if (this.blockScalarIndent === -1)
  25366. this.indentNext = indent;
  25367. else
  25368. this.indentNext += this.blockScalarIndent;
  25369. do {
  25370. const cs = this.continueScalar(nl + 1);
  25371. if (cs === -1)
  25372. break;
  25373. nl = this.buffer.indexOf('\n', cs);
  25374. } while (nl !== -1);
  25375. if (nl === -1) {
  25376. if (!this.atEnd)
  25377. return this.setNext('block-scalar');
  25378. nl = this.buffer.length;
  25379. }
  25380. }
  25381. if (!this.blockScalarKeep) {
  25382. do {
  25383. let i = nl - 1;
  25384. let ch = this.buffer[i];
  25385. if (ch === '\r')
  25386. ch = this.buffer[--i];
  25387. const lastChar = i; // Drop the line if last char not more indented
  25388. while (ch === ' ' || ch === '\t')
  25389. ch = this.buffer[--i];
  25390. if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
  25391. nl = i;
  25392. else
  25393. break;
  25394. } while (true);
  25395. }
  25396. yield SCALAR;
  25397. yield* this.pushToIndex(nl + 1, true);
  25398. return yield* this.parseLineStart();
  25399. }
  25400. *parsePlainScalar() {
  25401. const inFlow = this.flowLevel > 0;
  25402. let end = this.pos - 1;
  25403. let i = this.pos - 1;
  25404. let ch;
  25405. while ((ch = this.buffer[++i])) {
  25406. if (ch === ':') {
  25407. const next = this.buffer[i + 1];
  25408. if (isEmpty(next) || (inFlow && next === ','))
  25409. break;
  25410. end = i;
  25411. }
  25412. else if (isEmpty(ch)) {
  25413. let next = this.buffer[i + 1];
  25414. if (ch === '\r') {
  25415. if (next === '\n') {
  25416. i += 1;
  25417. ch = '\n';
  25418. next = this.buffer[i + 1];
  25419. }
  25420. else
  25421. end = i;
  25422. }
  25423. if (next === '#' || (inFlow && invalidFlowScalarChars.includes(next)))
  25424. break;
  25425. if (ch === '\n') {
  25426. const cs = this.continueScalar(i + 1);
  25427. if (cs === -1)
  25428. break;
  25429. i = Math.max(i, cs - 2); // to advance, but still account for ' #'
  25430. }
  25431. }
  25432. else {
  25433. if (inFlow && invalidFlowScalarChars.includes(ch))
  25434. break;
  25435. end = i;
  25436. }
  25437. }
  25438. if (!ch && !this.atEnd)
  25439. return this.setNext('plain-scalar');
  25440. yield SCALAR;
  25441. yield* this.pushToIndex(end + 1, true);
  25442. return inFlow ? 'flow' : 'doc';
  25443. }
  25444. *pushCount(n) {
  25445. if (n > 0) {
  25446. yield this.buffer.substr(this.pos, n);
  25447. this.pos += n;
  25448. return n;
  25449. }
  25450. return 0;
  25451. }
  25452. *pushToIndex(i, allowEmpty) {
  25453. const s = this.buffer.slice(this.pos, i);
  25454. if (s) {
  25455. yield s;
  25456. this.pos += s.length;
  25457. return s.length;
  25458. }
  25459. else if (allowEmpty)
  25460. yield '';
  25461. return 0;
  25462. }
  25463. *pushIndicators() {
  25464. switch (this.charAt(0)) {
  25465. case '!':
  25466. return ((yield* this.pushTag()) +
  25467. (yield* this.pushSpaces(true)) +
  25468. (yield* this.pushIndicators()));
  25469. case '&':
  25470. return ((yield* this.pushUntil(isNotAnchorChar)) +
  25471. (yield* this.pushSpaces(true)) +
  25472. (yield* this.pushIndicators()));
  25473. case '-': // this is an error
  25474. case '?': // this is an error outside flow collections
  25475. case ':': {
  25476. const inFlow = this.flowLevel > 0;
  25477. const ch1 = this.charAt(1);
  25478. if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
  25479. if (!inFlow)
  25480. this.indentNext = this.indentValue + 1;
  25481. else if (this.flowKey)
  25482. this.flowKey = false;
  25483. return ((yield* this.pushCount(1)) +
  25484. (yield* this.pushSpaces(true)) +
  25485. (yield* this.pushIndicators()));
  25486. }
  25487. }
  25488. }
  25489. return 0;
  25490. }
  25491. *pushTag() {
  25492. if (this.charAt(1) === '<') {
  25493. let i = this.pos + 2;
  25494. let ch = this.buffer[i];
  25495. while (!isEmpty(ch) && ch !== '>')
  25496. ch = this.buffer[++i];
  25497. return yield* this.pushToIndex(ch === '>' ? i + 1 : i, false);
  25498. }
  25499. else {
  25500. let i = this.pos + 1;
  25501. let ch = this.buffer[i];
  25502. while (ch) {
  25503. if (tagChars.includes(ch))
  25504. ch = this.buffer[++i];
  25505. else if (ch === '%' &&
  25506. hexDigits.includes(this.buffer[i + 1]) &&
  25507. hexDigits.includes(this.buffer[i + 2])) {
  25508. ch = this.buffer[(i += 3)];
  25509. }
  25510. else
  25511. break;
  25512. }
  25513. return yield* this.pushToIndex(i, false);
  25514. }
  25515. }
  25516. *pushNewline() {
  25517. const ch = this.buffer[this.pos];
  25518. if (ch === '\n')
  25519. return yield* this.pushCount(1);
  25520. else if (ch === '\r' && this.charAt(1) === '\n')
  25521. return yield* this.pushCount(2);
  25522. else
  25523. return 0;
  25524. }
  25525. *pushSpaces(allowTabs) {
  25526. let i = this.pos - 1;
  25527. let ch;
  25528. do {
  25529. ch = this.buffer[++i];
  25530. } while (ch === ' ' || (allowTabs && ch === '\t'));
  25531. const n = i - this.pos;
  25532. if (n > 0) {
  25533. yield this.buffer.substr(this.pos, n);
  25534. this.pos = i;
  25535. }
  25536. return n;
  25537. }
  25538. *pushUntil(test) {
  25539. let i = this.pos;
  25540. let ch = this.buffer[i];
  25541. while (!test(ch))
  25542. ch = this.buffer[++i];
  25543. return yield* this.pushToIndex(i, false);
  25544. }
  25545. }
  25546. /**
  25547. * Tracks newlines during parsing in order to provide an efficient API for
  25548. * determining the one-indexed `{ line, col }` position for any offset
  25549. * within the input.
  25550. */
  25551. class LineCounter {
  25552. constructor() {
  25553. this.lineStarts = [];
  25554. /**
  25555. * Should be called in ascending order. Otherwise, call
  25556. * `lineCounter.lineStarts.sort()` before calling `linePos()`.
  25557. */
  25558. this.addNewLine = (offset) => this.lineStarts.push(offset);
  25559. /**
  25560. * Performs a binary search and returns the 1-indexed { line, col }
  25561. * position of `offset`. If `line === 0`, `addNewLine` has never been
  25562. * called or `offset` is before the first known newline.
  25563. */
  25564. this.linePos = (offset) => {
  25565. let low = 0;
  25566. let high = this.lineStarts.length;
  25567. while (low < high) {
  25568. const mid = (low + high) >> 1; // Math.floor((low + high) / 2)
  25569. if (this.lineStarts[mid] < offset)
  25570. low = mid + 1;
  25571. else
  25572. high = mid;
  25573. }
  25574. if (this.lineStarts[low] === offset)
  25575. return { line: low + 1, col: 1 };
  25576. if (low === 0)
  25577. return { line: 0, col: offset };
  25578. const start = this.lineStarts[low - 1];
  25579. return { line: low, col: offset - start + 1 };
  25580. };
  25581. }
  25582. }
  25583. function includesToken(list, type) {
  25584. for (let i = 0; i < list.length; ++i)
  25585. if (list[i].type === type)
  25586. return true;
  25587. return false;
  25588. }
  25589. function findNonEmptyIndex(list) {
  25590. for (let i = 0; i < list.length; ++i) {
  25591. switch (list[i].type) {
  25592. case 'space':
  25593. case 'comment':
  25594. case 'newline':
  25595. break;
  25596. default:
  25597. return i;
  25598. }
  25599. }
  25600. return -1;
  25601. }
  25602. function isFlowToken(token) {
  25603. switch (token?.type) {
  25604. case 'alias':
  25605. case 'scalar':
  25606. case 'single-quoted-scalar':
  25607. case 'double-quoted-scalar':
  25608. case 'flow-collection':
  25609. return true;
  25610. default:
  25611. return false;
  25612. }
  25613. }
  25614. function getPrevProps(parent) {
  25615. switch (parent.type) {
  25616. case 'document':
  25617. return parent.start;
  25618. case 'block-map': {
  25619. const it = parent.items[parent.items.length - 1];
  25620. return it.sep ?? it.start;
  25621. }
  25622. case 'block-seq':
  25623. return parent.items[parent.items.length - 1].start;
  25624. /* istanbul ignore next should not happen */
  25625. default:
  25626. return [];
  25627. }
  25628. }
  25629. /** Note: May modify input array */
  25630. function getFirstKeyStartProps(prev) {
  25631. if (prev.length === 0)
  25632. return [];
  25633. let i = prev.length;
  25634. loop: while (--i >= 0) {
  25635. switch (prev[i].type) {
  25636. case 'doc-start':
  25637. case 'explicit-key-ind':
  25638. case 'map-value-ind':
  25639. case 'seq-item-ind':
  25640. case 'newline':
  25641. break loop;
  25642. }
  25643. }
  25644. while (prev[++i]?.type === 'space') {
  25645. /* loop */
  25646. }
  25647. return prev.splice(i, prev.length);
  25648. }
  25649. function fixFlowSeqItems(fc) {
  25650. if (fc.start.type === 'flow-seq-start') {
  25651. for (const it of fc.items) {
  25652. if (it.sep &&
  25653. !it.value &&
  25654. !includesToken(it.start, 'explicit-key-ind') &&
  25655. !includesToken(it.sep, 'map-value-ind')) {
  25656. if (it.key)
  25657. it.value = it.key;
  25658. delete it.key;
  25659. if (isFlowToken(it.value)) {
  25660. if (it.value.end)
  25661. Array.prototype.push.apply(it.value.end, it.sep);
  25662. else
  25663. it.value.end = it.sep;
  25664. }
  25665. else
  25666. Array.prototype.push.apply(it.start, it.sep);
  25667. delete it.sep;
  25668. }
  25669. }
  25670. }
  25671. }
  25672. /**
  25673. * A YAML concrete syntax tree (CST) parser
  25674. *
  25675. * ```ts
  25676. * const src: string = ...
  25677. * for (const token of new Parser().parse(src)) {
  25678. * // token: Token
  25679. * }
  25680. * ```
  25681. *
  25682. * To use the parser with a user-provided lexer:
  25683. *
  25684. * ```ts
  25685. * function* parse(source: string, lexer: Lexer) {
  25686. * const parser = new Parser()
  25687. * for (const lexeme of lexer.lex(source))
  25688. * yield* parser.next(lexeme)
  25689. * yield* parser.end()
  25690. * }
  25691. *
  25692. * const src: string = ...
  25693. * const lexer = new Lexer()
  25694. * for (const token of parse(src, lexer)) {
  25695. * // token: Token
  25696. * }
  25697. * ```
  25698. */
  25699. let Parser$1 = class Parser {
  25700. /**
  25701. * @param onNewLine - If defined, called separately with the start position of
  25702. * each new line (in `parse()`, including the start of input).
  25703. */
  25704. constructor(onNewLine) {
  25705. /** If true, space and sequence indicators count as indentation */
  25706. this.atNewLine = true;
  25707. /** If true, next token is a scalar value */
  25708. this.atScalar = false;
  25709. /** Current indentation level */
  25710. this.indent = 0;
  25711. /** Current offset since the start of parsing */
  25712. this.offset = 0;
  25713. /** On the same line with a block map key */
  25714. this.onKeyLine = false;
  25715. /** Top indicates the node that's currently being built */
  25716. this.stack = [];
  25717. /** The source of the current token, set in parse() */
  25718. this.source = '';
  25719. /** The type of the current token, set in parse() */
  25720. this.type = '';
  25721. // Must be defined after `next()`
  25722. this.lexer = new Lexer();
  25723. this.onNewLine = onNewLine;
  25724. }
  25725. /**
  25726. * Parse `source` as a YAML stream.
  25727. * If `incomplete`, a part of the last line may be left as a buffer for the next call.
  25728. *
  25729. * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
  25730. *
  25731. * @returns A generator of tokens representing each directive, document, and other structure.
  25732. */
  25733. *parse(source, incomplete = false) {
  25734. if (this.onNewLine && this.offset === 0)
  25735. this.onNewLine(0);
  25736. for (const lexeme of this.lexer.lex(source, incomplete))
  25737. yield* this.next(lexeme);
  25738. if (!incomplete)
  25739. yield* this.end();
  25740. }
  25741. /**
  25742. * Advance the parser by the `source` of one lexical token.
  25743. */
  25744. *next(source) {
  25745. this.source = source;
  25746. if (this.atScalar) {
  25747. this.atScalar = false;
  25748. yield* this.step();
  25749. this.offset += source.length;
  25750. return;
  25751. }
  25752. const type = tokenType(source);
  25753. if (!type) {
  25754. const message = `Not a YAML token: ${source}`;
  25755. yield* this.pop({ type: 'error', offset: this.offset, message, source });
  25756. this.offset += source.length;
  25757. }
  25758. else if (type === 'scalar') {
  25759. this.atNewLine = false;
  25760. this.atScalar = true;
  25761. this.type = 'scalar';
  25762. }
  25763. else {
  25764. this.type = type;
  25765. yield* this.step();
  25766. switch (type) {
  25767. case 'newline':
  25768. this.atNewLine = true;
  25769. this.indent = 0;
  25770. if (this.onNewLine)
  25771. this.onNewLine(this.offset + source.length);
  25772. break;
  25773. case 'space':
  25774. if (this.atNewLine && source[0] === ' ')
  25775. this.indent += source.length;
  25776. break;
  25777. case 'explicit-key-ind':
  25778. case 'map-value-ind':
  25779. case 'seq-item-ind':
  25780. if (this.atNewLine)
  25781. this.indent += source.length;
  25782. break;
  25783. case 'doc-mode':
  25784. case 'flow-error-end':
  25785. return;
  25786. default:
  25787. this.atNewLine = false;
  25788. }
  25789. this.offset += source.length;
  25790. }
  25791. }
  25792. /** Call at end of input to push out any remaining constructions */
  25793. *end() {
  25794. while (this.stack.length > 0)
  25795. yield* this.pop();
  25796. }
  25797. get sourceToken() {
  25798. const st = {
  25799. type: this.type,
  25800. offset: this.offset,
  25801. indent: this.indent,
  25802. source: this.source
  25803. };
  25804. return st;
  25805. }
  25806. *step() {
  25807. const top = this.peek(1);
  25808. if (this.type === 'doc-end' && (!top || top.type !== 'doc-end')) {
  25809. while (this.stack.length > 0)
  25810. yield* this.pop();
  25811. this.stack.push({
  25812. type: 'doc-end',
  25813. offset: this.offset,
  25814. source: this.source
  25815. });
  25816. return;
  25817. }
  25818. if (!top)
  25819. return yield* this.stream();
  25820. switch (top.type) {
  25821. case 'document':
  25822. return yield* this.document(top);
  25823. case 'alias':
  25824. case 'scalar':
  25825. case 'single-quoted-scalar':
  25826. case 'double-quoted-scalar':
  25827. return yield* this.scalar(top);
  25828. case 'block-scalar':
  25829. return yield* this.blockScalar(top);
  25830. case 'block-map':
  25831. return yield* this.blockMap(top);
  25832. case 'block-seq':
  25833. return yield* this.blockSequence(top);
  25834. case 'flow-collection':
  25835. return yield* this.flowCollection(top);
  25836. case 'doc-end':
  25837. return yield* this.documentEnd(top);
  25838. }
  25839. /* istanbul ignore next should not happen */
  25840. yield* this.pop();
  25841. }
  25842. peek(n) {
  25843. return this.stack[this.stack.length - n];
  25844. }
  25845. *pop(error) {
  25846. const token = error ?? this.stack.pop();
  25847. /* istanbul ignore if should not happen */
  25848. if (!token) {
  25849. const message = 'Tried to pop an empty stack';
  25850. yield { type: 'error', offset: this.offset, source: '', message };
  25851. }
  25852. else if (this.stack.length === 0) {
  25853. yield token;
  25854. }
  25855. else {
  25856. const top = this.peek(1);
  25857. if (token.type === 'block-scalar') {
  25858. // Block scalars use their parent rather than header indent
  25859. token.indent = 'indent' in top ? top.indent : 0;
  25860. }
  25861. else if (token.type === 'flow-collection' && top.type === 'document') {
  25862. // Ignore all indent for top-level flow collections
  25863. token.indent = 0;
  25864. }
  25865. if (token.type === 'flow-collection')
  25866. fixFlowSeqItems(token);
  25867. switch (top.type) {
  25868. case 'document':
  25869. top.value = token;
  25870. break;
  25871. case 'block-scalar':
  25872. top.props.push(token); // error
  25873. break;
  25874. case 'block-map': {
  25875. const it = top.items[top.items.length - 1];
  25876. if (it.value) {
  25877. top.items.push({ start: [], key: token, sep: [] });
  25878. this.onKeyLine = true;
  25879. return;
  25880. }
  25881. else if (it.sep) {
  25882. it.value = token;
  25883. }
  25884. else {
  25885. Object.assign(it, { key: token, sep: [] });
  25886. this.onKeyLine = !includesToken(it.start, 'explicit-key-ind');
  25887. return;
  25888. }
  25889. break;
  25890. }
  25891. case 'block-seq': {
  25892. const it = top.items[top.items.length - 1];
  25893. if (it.value)
  25894. top.items.push({ start: [], value: token });
  25895. else
  25896. it.value = token;
  25897. break;
  25898. }
  25899. case 'flow-collection': {
  25900. const it = top.items[top.items.length - 1];
  25901. if (!it || it.value)
  25902. top.items.push({ start: [], key: token, sep: [] });
  25903. else if (it.sep)
  25904. it.value = token;
  25905. else
  25906. Object.assign(it, { key: token, sep: [] });
  25907. return;
  25908. }
  25909. /* istanbul ignore next should not happen */
  25910. default:
  25911. yield* this.pop();
  25912. yield* this.pop(token);
  25913. }
  25914. if ((top.type === 'document' ||
  25915. top.type === 'block-map' ||
  25916. top.type === 'block-seq') &&
  25917. (token.type === 'block-map' || token.type === 'block-seq')) {
  25918. const last = token.items[token.items.length - 1];
  25919. if (last &&
  25920. !last.sep &&
  25921. !last.value &&
  25922. last.start.length > 0 &&
  25923. findNonEmptyIndex(last.start) === -1 &&
  25924. (token.indent === 0 ||
  25925. last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
  25926. if (top.type === 'document')
  25927. top.end = last.start;
  25928. else
  25929. top.items.push({ start: last.start });
  25930. token.items.splice(-1, 1);
  25931. }
  25932. }
  25933. }
  25934. }
  25935. *stream() {
  25936. switch (this.type) {
  25937. case 'directive-line':
  25938. yield { type: 'directive', offset: this.offset, source: this.source };
  25939. return;
  25940. case 'byte-order-mark':
  25941. case 'space':
  25942. case 'comment':
  25943. case 'newline':
  25944. yield this.sourceToken;
  25945. return;
  25946. case 'doc-mode':
  25947. case 'doc-start': {
  25948. const doc = {
  25949. type: 'document',
  25950. offset: this.offset,
  25951. start: []
  25952. };
  25953. if (this.type === 'doc-start')
  25954. doc.start.push(this.sourceToken);
  25955. this.stack.push(doc);
  25956. return;
  25957. }
  25958. }
  25959. yield {
  25960. type: 'error',
  25961. offset: this.offset,
  25962. message: `Unexpected ${this.type} token in YAML stream`,
  25963. source: this.source
  25964. };
  25965. }
  25966. *document(doc) {
  25967. if (doc.value)
  25968. return yield* this.lineEnd(doc);
  25969. switch (this.type) {
  25970. case 'doc-start': {
  25971. if (findNonEmptyIndex(doc.start) !== -1) {
  25972. yield* this.pop();
  25973. yield* this.step();
  25974. }
  25975. else
  25976. doc.start.push(this.sourceToken);
  25977. return;
  25978. }
  25979. case 'anchor':
  25980. case 'tag':
  25981. case 'space':
  25982. case 'comment':
  25983. case 'newline':
  25984. doc.start.push(this.sourceToken);
  25985. return;
  25986. }
  25987. const bv = this.startBlockValue(doc);
  25988. if (bv)
  25989. this.stack.push(bv);
  25990. else {
  25991. yield {
  25992. type: 'error',
  25993. offset: this.offset,
  25994. message: `Unexpected ${this.type} token in YAML document`,
  25995. source: this.source
  25996. };
  25997. }
  25998. }
  25999. *scalar(scalar) {
  26000. if (this.type === 'map-value-ind') {
  26001. const prev = getPrevProps(this.peek(2));
  26002. const start = getFirstKeyStartProps(prev);
  26003. let sep;
  26004. if (scalar.end) {
  26005. sep = scalar.end;
  26006. sep.push(this.sourceToken);
  26007. delete scalar.end;
  26008. }
  26009. else
  26010. sep = [this.sourceToken];
  26011. const map = {
  26012. type: 'block-map',
  26013. offset: scalar.offset,
  26014. indent: scalar.indent,
  26015. items: [{ start, key: scalar, sep }]
  26016. };
  26017. this.onKeyLine = true;
  26018. this.stack[this.stack.length - 1] = map;
  26019. }
  26020. else
  26021. yield* this.lineEnd(scalar);
  26022. }
  26023. *blockScalar(scalar) {
  26024. switch (this.type) {
  26025. case 'space':
  26026. case 'comment':
  26027. case 'newline':
  26028. scalar.props.push(this.sourceToken);
  26029. return;
  26030. case 'scalar':
  26031. scalar.source = this.source;
  26032. // block-scalar source includes trailing newline
  26033. this.atNewLine = true;
  26034. this.indent = 0;
  26035. if (this.onNewLine) {
  26036. let nl = this.source.indexOf('\n') + 1;
  26037. while (nl !== 0) {
  26038. this.onNewLine(this.offset + nl);
  26039. nl = this.source.indexOf('\n', nl) + 1;
  26040. }
  26041. }
  26042. yield* this.pop();
  26043. break;
  26044. /* istanbul ignore next should not happen */
  26045. default:
  26046. yield* this.pop();
  26047. yield* this.step();
  26048. }
  26049. }
  26050. *blockMap(map) {
  26051. const it = map.items[map.items.length - 1];
  26052. // it.sep is true-ish if pair already has key or : separator
  26053. switch (this.type) {
  26054. case 'newline':
  26055. this.onKeyLine = false;
  26056. if (it.value) {
  26057. const end = 'end' in it.value ? it.value.end : undefined;
  26058. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  26059. if (last?.type === 'comment')
  26060. end?.push(this.sourceToken);
  26061. else
  26062. map.items.push({ start: [this.sourceToken] });
  26063. }
  26064. else if (it.sep) {
  26065. it.sep.push(this.sourceToken);
  26066. }
  26067. else {
  26068. it.start.push(this.sourceToken);
  26069. }
  26070. return;
  26071. case 'space':
  26072. case 'comment':
  26073. if (it.value) {
  26074. map.items.push({ start: [this.sourceToken] });
  26075. }
  26076. else if (it.sep) {
  26077. it.sep.push(this.sourceToken);
  26078. }
  26079. else {
  26080. if (this.atIndentedComment(it.start, map.indent)) {
  26081. const prev = map.items[map.items.length - 2];
  26082. const end = prev?.value?.end;
  26083. if (Array.isArray(end)) {
  26084. Array.prototype.push.apply(end, it.start);
  26085. end.push(this.sourceToken);
  26086. map.items.pop();
  26087. return;
  26088. }
  26089. }
  26090. it.start.push(this.sourceToken);
  26091. }
  26092. return;
  26093. }
  26094. if (this.indent >= map.indent) {
  26095. const atNextItem = !this.onKeyLine && this.indent === map.indent && it.sep;
  26096. // For empty nodes, assign newline-separated not indented empty tokens to following node
  26097. let start = [];
  26098. if (atNextItem && it.sep && !it.value) {
  26099. const nl = [];
  26100. for (let i = 0; i < it.sep.length; ++i) {
  26101. const st = it.sep[i];
  26102. switch (st.type) {
  26103. case 'newline':
  26104. nl.push(i);
  26105. break;
  26106. case 'space':
  26107. break;
  26108. case 'comment':
  26109. if (st.indent > map.indent)
  26110. nl.length = 0;
  26111. break;
  26112. default:
  26113. nl.length = 0;
  26114. }
  26115. }
  26116. if (nl.length >= 2)
  26117. start = it.sep.splice(nl[1]);
  26118. }
  26119. switch (this.type) {
  26120. case 'anchor':
  26121. case 'tag':
  26122. if (atNextItem || it.value) {
  26123. start.push(this.sourceToken);
  26124. map.items.push({ start });
  26125. this.onKeyLine = true;
  26126. }
  26127. else if (it.sep) {
  26128. it.sep.push(this.sourceToken);
  26129. }
  26130. else {
  26131. it.start.push(this.sourceToken);
  26132. }
  26133. return;
  26134. case 'explicit-key-ind':
  26135. if (!it.sep && !includesToken(it.start, 'explicit-key-ind')) {
  26136. it.start.push(this.sourceToken);
  26137. }
  26138. else if (atNextItem || it.value) {
  26139. start.push(this.sourceToken);
  26140. map.items.push({ start });
  26141. }
  26142. else {
  26143. this.stack.push({
  26144. type: 'block-map',
  26145. offset: this.offset,
  26146. indent: this.indent,
  26147. items: [{ start: [this.sourceToken] }]
  26148. });
  26149. }
  26150. this.onKeyLine = true;
  26151. return;
  26152. case 'map-value-ind':
  26153. if (includesToken(it.start, 'explicit-key-ind')) {
  26154. if (!it.sep) {
  26155. if (includesToken(it.start, 'newline')) {
  26156. Object.assign(it, { key: null, sep: [this.sourceToken] });
  26157. }
  26158. else {
  26159. const start = getFirstKeyStartProps(it.start);
  26160. this.stack.push({
  26161. type: 'block-map',
  26162. offset: this.offset,
  26163. indent: this.indent,
  26164. items: [{ start, key: null, sep: [this.sourceToken] }]
  26165. });
  26166. }
  26167. }
  26168. else if (it.value) {
  26169. map.items.push({ start: [], key: null, sep: [this.sourceToken] });
  26170. }
  26171. else if (includesToken(it.sep, 'map-value-ind')) {
  26172. this.stack.push({
  26173. type: 'block-map',
  26174. offset: this.offset,
  26175. indent: this.indent,
  26176. items: [{ start, key: null, sep: [this.sourceToken] }]
  26177. });
  26178. }
  26179. else if (isFlowToken(it.key) &&
  26180. !includesToken(it.sep, 'newline')) {
  26181. const start = getFirstKeyStartProps(it.start);
  26182. const key = it.key;
  26183. const sep = it.sep;
  26184. sep.push(this.sourceToken);
  26185. // @ts-expect-error type guard is wrong here
  26186. delete it.key, delete it.sep;
  26187. this.stack.push({
  26188. type: 'block-map',
  26189. offset: this.offset,
  26190. indent: this.indent,
  26191. items: [{ start, key, sep }]
  26192. });
  26193. }
  26194. else if (start.length > 0) {
  26195. // Not actually at next item
  26196. it.sep = it.sep.concat(start, this.sourceToken);
  26197. }
  26198. else {
  26199. it.sep.push(this.sourceToken);
  26200. }
  26201. }
  26202. else {
  26203. if (!it.sep) {
  26204. Object.assign(it, { key: null, sep: [this.sourceToken] });
  26205. }
  26206. else if (it.value || atNextItem) {
  26207. map.items.push({ start, key: null, sep: [this.sourceToken] });
  26208. }
  26209. else if (includesToken(it.sep, 'map-value-ind')) {
  26210. this.stack.push({
  26211. type: 'block-map',
  26212. offset: this.offset,
  26213. indent: this.indent,
  26214. items: [{ start: [], key: null, sep: [this.sourceToken] }]
  26215. });
  26216. }
  26217. else {
  26218. it.sep.push(this.sourceToken);
  26219. }
  26220. }
  26221. this.onKeyLine = true;
  26222. return;
  26223. case 'alias':
  26224. case 'scalar':
  26225. case 'single-quoted-scalar':
  26226. case 'double-quoted-scalar': {
  26227. const fs = this.flowScalar(this.type);
  26228. if (atNextItem || it.value) {
  26229. map.items.push({ start, key: fs, sep: [] });
  26230. this.onKeyLine = true;
  26231. }
  26232. else if (it.sep) {
  26233. this.stack.push(fs);
  26234. }
  26235. else {
  26236. Object.assign(it, { key: fs, sep: [] });
  26237. this.onKeyLine = true;
  26238. }
  26239. return;
  26240. }
  26241. default: {
  26242. const bv = this.startBlockValue(map);
  26243. if (bv) {
  26244. if (atNextItem &&
  26245. bv.type !== 'block-seq' &&
  26246. includesToken(it.start, 'explicit-key-ind')) {
  26247. map.items.push({ start });
  26248. }
  26249. this.stack.push(bv);
  26250. return;
  26251. }
  26252. }
  26253. }
  26254. }
  26255. yield* this.pop();
  26256. yield* this.step();
  26257. }
  26258. *blockSequence(seq) {
  26259. const it = seq.items[seq.items.length - 1];
  26260. switch (this.type) {
  26261. case 'newline':
  26262. if (it.value) {
  26263. const end = 'end' in it.value ? it.value.end : undefined;
  26264. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  26265. if (last?.type === 'comment')
  26266. end?.push(this.sourceToken);
  26267. else
  26268. seq.items.push({ start: [this.sourceToken] });
  26269. }
  26270. else
  26271. it.start.push(this.sourceToken);
  26272. return;
  26273. case 'space':
  26274. case 'comment':
  26275. if (it.value)
  26276. seq.items.push({ start: [this.sourceToken] });
  26277. else {
  26278. if (this.atIndentedComment(it.start, seq.indent)) {
  26279. const prev = seq.items[seq.items.length - 2];
  26280. const end = prev?.value?.end;
  26281. if (Array.isArray(end)) {
  26282. Array.prototype.push.apply(end, it.start);
  26283. end.push(this.sourceToken);
  26284. seq.items.pop();
  26285. return;
  26286. }
  26287. }
  26288. it.start.push(this.sourceToken);
  26289. }
  26290. return;
  26291. case 'anchor':
  26292. case 'tag':
  26293. if (it.value || this.indent <= seq.indent)
  26294. break;
  26295. it.start.push(this.sourceToken);
  26296. return;
  26297. case 'seq-item-ind':
  26298. if (this.indent !== seq.indent)
  26299. break;
  26300. if (it.value || includesToken(it.start, 'seq-item-ind'))
  26301. seq.items.push({ start: [this.sourceToken] });
  26302. else
  26303. it.start.push(this.sourceToken);
  26304. return;
  26305. }
  26306. if (this.indent > seq.indent) {
  26307. const bv = this.startBlockValue(seq);
  26308. if (bv) {
  26309. this.stack.push(bv);
  26310. return;
  26311. }
  26312. }
  26313. yield* this.pop();
  26314. yield* this.step();
  26315. }
  26316. *flowCollection(fc) {
  26317. const it = fc.items[fc.items.length - 1];
  26318. if (this.type === 'flow-error-end') {
  26319. let top;
  26320. do {
  26321. yield* this.pop();
  26322. top = this.peek(1);
  26323. } while (top && top.type === 'flow-collection');
  26324. }
  26325. else if (fc.end.length === 0) {
  26326. switch (this.type) {
  26327. case 'comma':
  26328. case 'explicit-key-ind':
  26329. if (!it || it.sep)
  26330. fc.items.push({ start: [this.sourceToken] });
  26331. else
  26332. it.start.push(this.sourceToken);
  26333. return;
  26334. case 'map-value-ind':
  26335. if (!it || it.value)
  26336. fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
  26337. else if (it.sep)
  26338. it.sep.push(this.sourceToken);
  26339. else
  26340. Object.assign(it, { key: null, sep: [this.sourceToken] });
  26341. return;
  26342. case 'space':
  26343. case 'comment':
  26344. case 'newline':
  26345. case 'anchor':
  26346. case 'tag':
  26347. if (!it || it.value)
  26348. fc.items.push({ start: [this.sourceToken] });
  26349. else if (it.sep)
  26350. it.sep.push(this.sourceToken);
  26351. else
  26352. it.start.push(this.sourceToken);
  26353. return;
  26354. case 'alias':
  26355. case 'scalar':
  26356. case 'single-quoted-scalar':
  26357. case 'double-quoted-scalar': {
  26358. const fs = this.flowScalar(this.type);
  26359. if (!it || it.value)
  26360. fc.items.push({ start: [], key: fs, sep: [] });
  26361. else if (it.sep)
  26362. this.stack.push(fs);
  26363. else
  26364. Object.assign(it, { key: fs, sep: [] });
  26365. return;
  26366. }
  26367. case 'flow-map-end':
  26368. case 'flow-seq-end':
  26369. fc.end.push(this.sourceToken);
  26370. return;
  26371. }
  26372. const bv = this.startBlockValue(fc);
  26373. /* istanbul ignore else should not happen */
  26374. if (bv)
  26375. this.stack.push(bv);
  26376. else {
  26377. yield* this.pop();
  26378. yield* this.step();
  26379. }
  26380. }
  26381. else {
  26382. const parent = this.peek(2);
  26383. if (parent.type === 'block-map' &&
  26384. ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
  26385. (this.type === 'newline' &&
  26386. !parent.items[parent.items.length - 1].sep))) {
  26387. yield* this.pop();
  26388. yield* this.step();
  26389. }
  26390. else if (this.type === 'map-value-ind' &&
  26391. parent.type !== 'flow-collection') {
  26392. const prev = getPrevProps(parent);
  26393. const start = getFirstKeyStartProps(prev);
  26394. fixFlowSeqItems(fc);
  26395. const sep = fc.end.splice(1, fc.end.length);
  26396. sep.push(this.sourceToken);
  26397. const map = {
  26398. type: 'block-map',
  26399. offset: fc.offset,
  26400. indent: fc.indent,
  26401. items: [{ start, key: fc, sep }]
  26402. };
  26403. this.onKeyLine = true;
  26404. this.stack[this.stack.length - 1] = map;
  26405. }
  26406. else {
  26407. yield* this.lineEnd(fc);
  26408. }
  26409. }
  26410. }
  26411. flowScalar(type) {
  26412. if (this.onNewLine) {
  26413. let nl = this.source.indexOf('\n') + 1;
  26414. while (nl !== 0) {
  26415. this.onNewLine(this.offset + nl);
  26416. nl = this.source.indexOf('\n', nl) + 1;
  26417. }
  26418. }
  26419. return {
  26420. type,
  26421. offset: this.offset,
  26422. indent: this.indent,
  26423. source: this.source
  26424. };
  26425. }
  26426. startBlockValue(parent) {
  26427. switch (this.type) {
  26428. case 'alias':
  26429. case 'scalar':
  26430. case 'single-quoted-scalar':
  26431. case 'double-quoted-scalar':
  26432. return this.flowScalar(this.type);
  26433. case 'block-scalar-header':
  26434. return {
  26435. type: 'block-scalar',
  26436. offset: this.offset,
  26437. indent: this.indent,
  26438. props: [this.sourceToken],
  26439. source: ''
  26440. };
  26441. case 'flow-map-start':
  26442. case 'flow-seq-start':
  26443. return {
  26444. type: 'flow-collection',
  26445. offset: this.offset,
  26446. indent: this.indent,
  26447. start: this.sourceToken,
  26448. items: [],
  26449. end: []
  26450. };
  26451. case 'seq-item-ind':
  26452. return {
  26453. type: 'block-seq',
  26454. offset: this.offset,
  26455. indent: this.indent,
  26456. items: [{ start: [this.sourceToken] }]
  26457. };
  26458. case 'explicit-key-ind': {
  26459. this.onKeyLine = true;
  26460. const prev = getPrevProps(parent);
  26461. const start = getFirstKeyStartProps(prev);
  26462. start.push(this.sourceToken);
  26463. return {
  26464. type: 'block-map',
  26465. offset: this.offset,
  26466. indent: this.indent,
  26467. items: [{ start }]
  26468. };
  26469. }
  26470. case 'map-value-ind': {
  26471. this.onKeyLine = true;
  26472. const prev = getPrevProps(parent);
  26473. const start = getFirstKeyStartProps(prev);
  26474. return {
  26475. type: 'block-map',
  26476. offset: this.offset,
  26477. indent: this.indent,
  26478. items: [{ start, key: null, sep: [this.sourceToken] }]
  26479. };
  26480. }
  26481. }
  26482. return null;
  26483. }
  26484. atIndentedComment(start, indent) {
  26485. if (this.type !== 'comment')
  26486. return false;
  26487. if (this.indent <= indent)
  26488. return false;
  26489. return start.every(st => st.type === 'newline' || st.type === 'space');
  26490. }
  26491. *documentEnd(docEnd) {
  26492. if (this.type !== 'doc-mode') {
  26493. if (docEnd.end)
  26494. docEnd.end.push(this.sourceToken);
  26495. else
  26496. docEnd.end = [this.sourceToken];
  26497. if (this.type === 'newline')
  26498. yield* this.pop();
  26499. }
  26500. }
  26501. *lineEnd(token) {
  26502. switch (this.type) {
  26503. case 'comma':
  26504. case 'doc-start':
  26505. case 'doc-end':
  26506. case 'flow-seq-end':
  26507. case 'flow-map-end':
  26508. case 'map-value-ind':
  26509. yield* this.pop();
  26510. yield* this.step();
  26511. break;
  26512. case 'newline':
  26513. this.onKeyLine = false;
  26514. // fallthrough
  26515. case 'space':
  26516. case 'comment':
  26517. default:
  26518. // all other values are errors
  26519. if (token.end)
  26520. token.end.push(this.sourceToken);
  26521. else
  26522. token.end = [this.sourceToken];
  26523. if (this.type === 'newline')
  26524. yield* this.pop();
  26525. }
  26526. }
  26527. };
  26528. function parseOptions(options) {
  26529. const prettyErrors = options.prettyErrors !== false;
  26530. const lineCounter = options.lineCounter || (prettyErrors && new LineCounter()) || null;
  26531. return { lineCounter, prettyErrors };
  26532. }
  26533. /**
  26534. * Parse the input as a stream of YAML documents.
  26535. *
  26536. * Documents should be separated from each other by `...` or `---` marker lines.
  26537. *
  26538. * @returns If an empty `docs` array is returned, it will be of type
  26539. * EmptyStream and contain additional stream information. In
  26540. * TypeScript, you should use `'empty' in docs` as a type guard for it.
  26541. */
  26542. function parseAllDocuments(source, options = {}) {
  26543. const { lineCounter, prettyErrors } = parseOptions(options);
  26544. const parser = new Parser$1(lineCounter?.addNewLine);
  26545. const composer = new Composer(options);
  26546. const docs = Array.from(composer.compose(parser.parse(source)));
  26547. if (prettyErrors && lineCounter)
  26548. for (const doc of docs) {
  26549. doc.errors.forEach(prettifyError(source, lineCounter));
  26550. doc.warnings.forEach(prettifyError(source, lineCounter));
  26551. }
  26552. if (docs.length > 0)
  26553. return docs;
  26554. return Object.assign([], { empty: true }, composer.streamInfo());
  26555. }
  26556. /** Parse an input string into a single YAML.Document */
  26557. function parseDocument(source, options = {}) {
  26558. const { lineCounter, prettyErrors } = parseOptions(options);
  26559. const parser = new Parser$1(lineCounter?.addNewLine);
  26560. const composer = new Composer(options);
  26561. // `doc` is always set by compose.end(true) at the very latest
  26562. let doc = null;
  26563. for (const _doc of composer.compose(parser.parse(source), true, source.length)) {
  26564. if (!doc)
  26565. doc = _doc;
  26566. else if (doc.options.logLevel !== 'silent') {
  26567. doc.errors.push(new YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
  26568. break;
  26569. }
  26570. }
  26571. if (prettyErrors && lineCounter) {
  26572. doc.errors.forEach(prettifyError(source, lineCounter));
  26573. doc.warnings.forEach(prettifyError(source, lineCounter));
  26574. }
  26575. return doc;
  26576. }
  26577. function parse$b(src, reviver, options) {
  26578. let _reviver = undefined;
  26579. if (typeof reviver === 'function') {
  26580. _reviver = reviver;
  26581. }
  26582. else if (options === undefined && reviver && typeof reviver === 'object') {
  26583. options = reviver;
  26584. }
  26585. const doc = parseDocument(src, options);
  26586. if (!doc)
  26587. return null;
  26588. doc.warnings.forEach(warning => warn(doc.options.logLevel, warning));
  26589. if (doc.errors.length > 0) {
  26590. if (doc.options.logLevel !== 'silent')
  26591. throw doc.errors[0];
  26592. else
  26593. doc.errors = [];
  26594. }
  26595. return doc.toJS(Object.assign({ reviver: _reviver }, options));
  26596. }
  26597. function stringify(value, replacer, options) {
  26598. let _replacer = null;
  26599. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  26600. _replacer = replacer;
  26601. }
  26602. else if (options === undefined && replacer) {
  26603. options = replacer;
  26604. }
  26605. if (typeof options === 'string')
  26606. options = options.length;
  26607. if (typeof options === 'number') {
  26608. const indent = Math.round(options);
  26609. options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
  26610. }
  26611. if (value === undefined) {
  26612. const { keepUndefined } = options ?? replacer ?? {};
  26613. if (!keepUndefined)
  26614. return undefined;
  26615. }
  26616. return new Document(value, _replacer, options).toString(options);
  26617. }
  26618. var YAML = {
  26619. __proto__: null,
  26620. Alias: Alias,
  26621. CST: cst,
  26622. Composer: Composer,
  26623. Document: Document,
  26624. Lexer: Lexer,
  26625. LineCounter: LineCounter,
  26626. Pair: Pair,
  26627. Parser: Parser$1,
  26628. Scalar: Scalar,
  26629. Schema: Schema,
  26630. YAMLError: YAMLError,
  26631. YAMLMap: YAMLMap,
  26632. YAMLParseError: YAMLParseError,
  26633. YAMLSeq: YAMLSeq,
  26634. YAMLWarning: YAMLWarning,
  26635. isAlias: isAlias,
  26636. isCollection: isCollection$1,
  26637. isDocument: isDocument,
  26638. isMap: isMap,
  26639. isNode: isNode$1,
  26640. isPair: isPair,
  26641. isScalar: isScalar$1,
  26642. isSeq: isSeq,
  26643. parse: parse$b,
  26644. parseAllDocuments: parseAllDocuments,
  26645. parseDocument: parseDocument,
  26646. stringify: stringify,
  26647. visit: visit$1,
  26648. visitAsync: visitAsync
  26649. };
  26650. // `export * as default from ...` fails on Webpack v4
  26651. // https://github.com/eemeli/yaml/issues/228
  26652. var browser$2 = {
  26653. __proto__: null,
  26654. Alias: Alias,
  26655. CST: cst,
  26656. Composer: Composer,
  26657. Document: Document,
  26658. Lexer: Lexer,
  26659. LineCounter: LineCounter,
  26660. Pair: Pair,
  26661. Parser: Parser$1,
  26662. Scalar: Scalar,
  26663. Schema: Schema,
  26664. YAMLError: YAMLError,
  26665. YAMLMap: YAMLMap,
  26666. YAMLParseError: YAMLParseError,
  26667. YAMLSeq: YAMLSeq,
  26668. YAMLWarning: YAMLWarning,
  26669. default: YAML,
  26670. isAlias: isAlias,
  26671. isCollection: isCollection$1,
  26672. isDocument: isDocument,
  26673. isMap: isMap,
  26674. isNode: isNode$1,
  26675. isPair: isPair,
  26676. isScalar: isScalar$1,
  26677. isSeq: isSeq,
  26678. parse: parse$b,
  26679. parseAllDocuments: parseAllDocuments,
  26680. parseDocument: parseDocument,
  26681. stringify: stringify,
  26682. visit: visit$1,
  26683. visitAsync: visitAsync
  26684. };
  26685. var require$$3 = /*@__PURE__*/getAugmentedNamespace(browser$2);
  26686. // eslint-disable-next-line n/no-deprecated-api
  26687. const { createRequire, createRequireFromPath } = require$$0$8;
  26688. function req$2 (name, rootFile) {
  26689. const create = createRequire || createRequireFromPath;
  26690. const require = create(rootFile);
  26691. return require(name)
  26692. }
  26693. var req_1 = req$2;
  26694. const req$1 = req_1;
  26695. /**
  26696. * Load Options
  26697. *
  26698. * @private
  26699. * @method options
  26700. *
  26701. * @param {Object} config PostCSS Config
  26702. *
  26703. * @return {Object} options PostCSS Options
  26704. */
  26705. const options = (config, file) => {
  26706. if (config.parser && typeof config.parser === 'string') {
  26707. try {
  26708. config.parser = req$1(config.parser, file);
  26709. } catch (err) {
  26710. throw new Error(`Loading PostCSS Parser failed: ${err.message}\n\n(@${file})`)
  26711. }
  26712. }
  26713. if (config.syntax && typeof config.syntax === 'string') {
  26714. try {
  26715. config.syntax = req$1(config.syntax, file);
  26716. } catch (err) {
  26717. throw new Error(`Loading PostCSS Syntax failed: ${err.message}\n\n(@${file})`)
  26718. }
  26719. }
  26720. if (config.stringifier && typeof config.stringifier === 'string') {
  26721. try {
  26722. config.stringifier = req$1(config.stringifier, file);
  26723. } catch (err) {
  26724. throw new Error(`Loading PostCSS Stringifier failed: ${err.message}\n\n(@${file})`)
  26725. }
  26726. }
  26727. if (config.plugins) {
  26728. delete config.plugins;
  26729. }
  26730. return config
  26731. };
  26732. var options_1 = options;
  26733. const req = req_1;
  26734. /**
  26735. * Plugin Loader
  26736. *
  26737. * @private
  26738. * @method load
  26739. *
  26740. * @param {String} plugin PostCSS Plugin Name
  26741. * @param {Object} options PostCSS Plugin Options
  26742. *
  26743. * @return {Function} PostCSS Plugin
  26744. */
  26745. const load = (plugin, options, file) => {
  26746. try {
  26747. if (
  26748. options === null ||
  26749. options === undefined ||
  26750. Object.keys(options).length === 0
  26751. ) {
  26752. return req(plugin, file)
  26753. } else {
  26754. return req(plugin, file)(options)
  26755. }
  26756. } catch (err) {
  26757. throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
  26758. }
  26759. };
  26760. /**
  26761. * Load Plugins
  26762. *
  26763. * @private
  26764. * @method plugins
  26765. *
  26766. * @param {Object} config PostCSS Config Plugins
  26767. *
  26768. * @return {Array} plugins PostCSS Plugins
  26769. */
  26770. const plugins = (config, file) => {
  26771. let plugins = [];
  26772. if (Array.isArray(config.plugins)) {
  26773. plugins = config.plugins.filter(Boolean);
  26774. } else {
  26775. plugins = Object.keys(config.plugins)
  26776. .filter((plugin) => {
  26777. return config.plugins[plugin] !== false ? plugin : ''
  26778. })
  26779. .map((plugin) => {
  26780. return load(plugin, config.plugins[plugin], file)
  26781. });
  26782. }
  26783. if (plugins.length && plugins.length > 0) {
  26784. plugins.forEach((plugin, i) => {
  26785. if (plugin.default) {
  26786. plugin = plugin.default;
  26787. }
  26788. if (plugin.postcss === true) {
  26789. plugin = plugin();
  26790. } else if (plugin.postcss) {
  26791. plugin = plugin.postcss;
  26792. }
  26793. if (
  26794. // eslint-disable-next-line
  26795. !(
  26796. (typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
  26797. (typeof plugin === 'object' && plugin.postcssPlugin) ||
  26798. (typeof plugin === 'function')
  26799. )
  26800. ) {
  26801. throw new TypeError(`Invalid PostCSS Plugin found at: plugins[${i}]\n\n(@${file})`)
  26802. }
  26803. });
  26804. }
  26805. return plugins
  26806. };
  26807. var plugins_1 = plugins;
  26808. const resolve = require$$0$4.resolve;
  26809. const url$4 = require$$0$9;
  26810. const config$1 = dist;
  26811. const yaml = require$$3;
  26812. const loadOptions = options_1;
  26813. const loadPlugins = plugins_1;
  26814. /* istanbul ignore next */
  26815. const interopRequireDefault = (obj) => obj && obj.__esModule ? obj : { default: obj };
  26816. /**
  26817. * Process the result from cosmiconfig
  26818. *
  26819. * @param {Object} ctx Config Context
  26820. * @param {Object} result Cosmiconfig result
  26821. *
  26822. * @return {Object} PostCSS Config
  26823. */
  26824. const processResult = (ctx, result) => {
  26825. const file = result.filepath || '';
  26826. let config = interopRequireDefault(result.config).default || {};
  26827. if (typeof config === 'function') {
  26828. config = config(ctx);
  26829. } else {
  26830. config = Object.assign({}, config, ctx);
  26831. }
  26832. if (!config.plugins) {
  26833. config.plugins = [];
  26834. }
  26835. return {
  26836. plugins: loadPlugins(config, file),
  26837. options: loadOptions(config, file),
  26838. file
  26839. }
  26840. };
  26841. /**
  26842. * Builds the Config Context
  26843. *
  26844. * @param {Object} ctx Config Context
  26845. *
  26846. * @return {Object} Config Context
  26847. */
  26848. const createContext = (ctx) => {
  26849. /**
  26850. * @type {Object}
  26851. *
  26852. * @prop {String} cwd=process.cwd() Config search start location
  26853. * @prop {String} env=process.env.NODE_ENV Config Enviroment, will be set to `development` by `postcss-load-config` if `process.env.NODE_ENV` is `undefined`
  26854. */
  26855. ctx = Object.assign({
  26856. cwd: process.cwd(),
  26857. env: process.env.NODE_ENV
  26858. }, ctx);
  26859. if (!ctx.env) {
  26860. process.env.NODE_ENV = 'development';
  26861. }
  26862. return ctx
  26863. };
  26864. const importDefault = async filepath => {
  26865. const module = await import(url$4.pathToFileURL(filepath).href);
  26866. return module.default
  26867. };
  26868. const addTypeScriptLoader = (options = {}, loader) => {
  26869. const moduleName = 'postcss';
  26870. return {
  26871. ...options,
  26872. searchPlaces: [
  26873. ...(options.searchPlaces || []),
  26874. 'package.json',
  26875. `.${moduleName}rc`,
  26876. `.${moduleName}rc.json`,
  26877. `.${moduleName}rc.yaml`,
  26878. `.${moduleName}rc.yml`,
  26879. `.${moduleName}rc.ts`,
  26880. `.${moduleName}rc.cts`,
  26881. `.${moduleName}rc.js`,
  26882. `.${moduleName}rc.cjs`,
  26883. `.${moduleName}rc.mjs`,
  26884. `${moduleName}.config.ts`,
  26885. `${moduleName}.config.cts`,
  26886. `${moduleName}.config.js`,
  26887. `${moduleName}.config.cjs`,
  26888. `${moduleName}.config.mjs`
  26889. ],
  26890. loaders: {
  26891. ...options.loaders,
  26892. '.yaml': (filepath, content) => yaml.parse(content),
  26893. '.yml': (filepath, content) => yaml.parse(content),
  26894. '.js': importDefault,
  26895. '.cjs': importDefault,
  26896. '.mjs': importDefault,
  26897. '.ts': loader,
  26898. '.cts': loader
  26899. }
  26900. }
  26901. };
  26902. const withTypeScriptLoader = (rcFunc) => {
  26903. return (ctx, path, options) => {
  26904. return rcFunc(ctx, path, addTypeScriptLoader(options, (configFile) => {
  26905. let registerer = { enabled () {} };
  26906. try {
  26907. // Register TypeScript compiler instance
  26908. registerer = __require('ts-node').register({
  26909. // transpile to cjs even if compilerOptions.module in tsconfig is not Node16/NodeNext.
  26910. moduleTypes: { '**/*.cts': 'cjs' }
  26911. });
  26912. return __require(configFile)
  26913. } catch (err) {
  26914. if (err.code === 'MODULE_NOT_FOUND') {
  26915. throw new Error(
  26916. `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
  26917. )
  26918. }
  26919. throw err
  26920. } finally {
  26921. registerer.enabled(false);
  26922. }
  26923. }))
  26924. }
  26925. };
  26926. /**
  26927. * Load Config
  26928. *
  26929. * @method rc
  26930. *
  26931. * @param {Object} ctx Config Context
  26932. * @param {String} path Config Path
  26933. * @param {Object} options Config Options
  26934. *
  26935. * @return {Promise} config PostCSS Config
  26936. */
  26937. const rc = withTypeScriptLoader((ctx, path, options) => {
  26938. /**
  26939. * @type {Object} The full Config Context
  26940. */
  26941. ctx = createContext(ctx);
  26942. /**
  26943. * @type {String} `process.cwd()`
  26944. */
  26945. path = path ? resolve(path) : process.cwd();
  26946. return config$1.lilconfig('postcss', options)
  26947. .search(path)
  26948. .then((result) => {
  26949. if (!result) {
  26950. throw new Error(`No PostCSS Config found in: ${path}`)
  26951. }
  26952. return processResult(ctx, result)
  26953. })
  26954. });
  26955. /**
  26956. * Autoload Config for PostCSS
  26957. *
  26958. * @author Michael Ciniawsky @michael-ciniawsky <michael.ciniawsky@gmail.com>
  26959. * @license MIT
  26960. *
  26961. * @module postcss-load-config
  26962. * @version 2.1.0
  26963. *
  26964. * @requires comsiconfig
  26965. * @requires ./options
  26966. * @requires ./plugins
  26967. */
  26968. var src$1 = rc;
  26969. var postcssrc = /*@__PURE__*/getDefaultExportFromCjs(src$1);
  26970. // Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Simon Lydell
  26971. // License: MIT.
  26972. var HashbangComment, Identifier, JSXIdentifier, JSXPunctuator, JSXString, JSXText, KeywordsWithExpressionAfter, KeywordsWithNoLineTerminatorAfter, LineTerminatorSequence, MultiLineComment, Newline, NumericLiteral, Punctuator, RegularExpressionLiteral, SingleLineComment, StringLiteral, Template, TokensNotPrecedingObjectLiteral, TokensPrecedingExpression, WhiteSpace;
  26973. RegularExpressionLiteral = /\/(?![*\/])(?:\[(?:[^\]\\\n\r\u2028\u2029]+|\\.)*\]|[^\/\\\n\r\u2028\u2029]+|\\.)*(\/[$_\u200C\u200D\p{ID_Continue}]*|\\)?/yu;
  26974. Punctuator = /--|\+\+|=>|\.{3}|\??\.(?!\d)|(?:&&|\|\||\?\?|[+\-%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2}|\/(?![\/*]))=?|[?~,:;[\](){}]/y;
  26975. Identifier = /(\x23?)(?=[$_\p{ID_Start}\\])(?:[$_\u200C\u200D\p{ID_Continue}]+|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+/yu;
  26976. StringLiteral = /(['"])(?:[^'"\\\n\r]+|(?!\1)['"]|\\(?:\r\n|[^]))*(\1)?/y;
  26977. NumericLiteral = /(?:0[xX][\da-fA-F](?:_?[\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|0n|[1-9](?:_?\d)*n|(?:(?:0(?!\d)|0\d*[89]\d*|[1-9](?:_?\d)*)(?:\.(?:\d(?:_?\d)*)?)?|\.\d(?:_?\d)*)(?:[eE][+-]?\d(?:_?\d)*)?|0[0-7]+/y;
  26978. Template = /[`}](?:[^`\\$]+|\\[^]|\$(?!\{))*(`|\$\{)?/y;
  26979. WhiteSpace = /[\t\v\f\ufeff\p{Zs}]+/yu;
  26980. LineTerminatorSequence = /\r?\n|[\r\u2028\u2029]/y;
  26981. MultiLineComment = /\/\*(?:[^*]+|\*(?!\/))*(\*\/)?/y;
  26982. SingleLineComment = /\/\/.*/y;
  26983. HashbangComment = /^#!.*/;
  26984. JSXPunctuator = /[<>.:={}]|\/(?![\/*])/y;
  26985. JSXIdentifier = /[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}-]*/yu;
  26986. JSXString = /(['"])(?:[^'"]+|(?!\1)['"])*(\1)?/y;
  26987. JSXText = /[^<>{}]+/y;
  26988. TokensPrecedingExpression = /^(?:[\/+-]|\.{3}|\?(?:InterpolationIn(?:JSX|Template)|NoLineTerminatorHere|NonExpressionParenEnd|UnaryIncDec))?$|[{}([,;<>=*%&|^!~?:]$/;
  26989. TokensNotPrecedingObjectLiteral = /^(?:=>|[;\]){}]|else|\?(?:NoLineTerminatorHere|NonExpressionParenEnd))?$/;
  26990. KeywordsWithExpressionAfter = /^(?:await|case|default|delete|do|else|instanceof|new|return|throw|typeof|void|yield)$/;
  26991. KeywordsWithNoLineTerminatorAfter = /^(?:return|throw|yield)$/;
  26992. Newline = RegExp(LineTerminatorSequence.source);
  26993. var jsTokens_1 = function*(input, {jsx = false} = {}) {
  26994. var braces, firstCodePoint, isExpression, lastIndex, lastSignificantToken, length, match, mode, nextLastIndex, nextLastSignificantToken, parenNesting, postfixIncDec, punctuator, stack;
  26995. ({length} = input);
  26996. lastIndex = 0;
  26997. lastSignificantToken = "";
  26998. stack = [
  26999. {tag: "JS"}
  27000. ];
  27001. braces = [];
  27002. parenNesting = 0;
  27003. postfixIncDec = false;
  27004. if (match = HashbangComment.exec(input)) {
  27005. yield ({
  27006. type: "HashbangComment",
  27007. value: match[0]
  27008. });
  27009. lastIndex = match[0].length;
  27010. }
  27011. while (lastIndex < length) {
  27012. mode = stack[stack.length - 1];
  27013. switch (mode.tag) {
  27014. case "JS":
  27015. case "JSNonExpressionParen":
  27016. case "InterpolationInTemplate":
  27017. case "InterpolationInJSX":
  27018. if (input[lastIndex] === "/" && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  27019. RegularExpressionLiteral.lastIndex = lastIndex;
  27020. if (match = RegularExpressionLiteral.exec(input)) {
  27021. lastIndex = RegularExpressionLiteral.lastIndex;
  27022. lastSignificantToken = match[0];
  27023. postfixIncDec = true;
  27024. yield ({
  27025. type: "RegularExpressionLiteral",
  27026. value: match[0],
  27027. closed: match[1] !== void 0 && match[1] !== "\\"
  27028. });
  27029. continue;
  27030. }
  27031. }
  27032. Punctuator.lastIndex = lastIndex;
  27033. if (match = Punctuator.exec(input)) {
  27034. punctuator = match[0];
  27035. nextLastIndex = Punctuator.lastIndex;
  27036. nextLastSignificantToken = punctuator;
  27037. switch (punctuator) {
  27038. case "(":
  27039. if (lastSignificantToken === "?NonExpressionParenKeyword") {
  27040. stack.push({
  27041. tag: "JSNonExpressionParen",
  27042. nesting: parenNesting
  27043. });
  27044. }
  27045. parenNesting++;
  27046. postfixIncDec = false;
  27047. break;
  27048. case ")":
  27049. parenNesting--;
  27050. postfixIncDec = true;
  27051. if (mode.tag === "JSNonExpressionParen" && parenNesting === mode.nesting) {
  27052. stack.pop();
  27053. nextLastSignificantToken = "?NonExpressionParenEnd";
  27054. postfixIncDec = false;
  27055. }
  27056. break;
  27057. case "{":
  27058. Punctuator.lastIndex = 0;
  27059. isExpression = !TokensNotPrecedingObjectLiteral.test(lastSignificantToken) && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken));
  27060. braces.push(isExpression);
  27061. postfixIncDec = false;
  27062. break;
  27063. case "}":
  27064. switch (mode.tag) {
  27065. case "InterpolationInTemplate":
  27066. if (braces.length === mode.nesting) {
  27067. Template.lastIndex = lastIndex;
  27068. match = Template.exec(input);
  27069. lastIndex = Template.lastIndex;
  27070. lastSignificantToken = match[0];
  27071. if (match[1] === "${") {
  27072. lastSignificantToken = "?InterpolationInTemplate";
  27073. postfixIncDec = false;
  27074. yield ({
  27075. type: "TemplateMiddle",
  27076. value: match[0]
  27077. });
  27078. } else {
  27079. stack.pop();
  27080. postfixIncDec = true;
  27081. yield ({
  27082. type: "TemplateTail",
  27083. value: match[0],
  27084. closed: match[1] === "`"
  27085. });
  27086. }
  27087. continue;
  27088. }
  27089. break;
  27090. case "InterpolationInJSX":
  27091. if (braces.length === mode.nesting) {
  27092. stack.pop();
  27093. lastIndex += 1;
  27094. lastSignificantToken = "}";
  27095. yield ({
  27096. type: "JSXPunctuator",
  27097. value: "}"
  27098. });
  27099. continue;
  27100. }
  27101. }
  27102. postfixIncDec = braces.pop();
  27103. nextLastSignificantToken = postfixIncDec ? "?ExpressionBraceEnd" : "}";
  27104. break;
  27105. case "]":
  27106. postfixIncDec = true;
  27107. break;
  27108. case "++":
  27109. case "--":
  27110. nextLastSignificantToken = postfixIncDec ? "?PostfixIncDec" : "?UnaryIncDec";
  27111. break;
  27112. case "<":
  27113. if (jsx && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  27114. stack.push({tag: "JSXTag"});
  27115. lastIndex += 1;
  27116. lastSignificantToken = "<";
  27117. yield ({
  27118. type: "JSXPunctuator",
  27119. value: punctuator
  27120. });
  27121. continue;
  27122. }
  27123. postfixIncDec = false;
  27124. break;
  27125. default:
  27126. postfixIncDec = false;
  27127. }
  27128. lastIndex = nextLastIndex;
  27129. lastSignificantToken = nextLastSignificantToken;
  27130. yield ({
  27131. type: "Punctuator",
  27132. value: punctuator
  27133. });
  27134. continue;
  27135. }
  27136. Identifier.lastIndex = lastIndex;
  27137. if (match = Identifier.exec(input)) {
  27138. lastIndex = Identifier.lastIndex;
  27139. nextLastSignificantToken = match[0];
  27140. switch (match[0]) {
  27141. case "for":
  27142. case "if":
  27143. case "while":
  27144. case "with":
  27145. if (lastSignificantToken !== "." && lastSignificantToken !== "?.") {
  27146. nextLastSignificantToken = "?NonExpressionParenKeyword";
  27147. }
  27148. }
  27149. lastSignificantToken = nextLastSignificantToken;
  27150. postfixIncDec = !KeywordsWithExpressionAfter.test(match[0]);
  27151. yield ({
  27152. type: match[1] === "#" ? "PrivateIdentifier" : "IdentifierName",
  27153. value: match[0]
  27154. });
  27155. continue;
  27156. }
  27157. StringLiteral.lastIndex = lastIndex;
  27158. if (match = StringLiteral.exec(input)) {
  27159. lastIndex = StringLiteral.lastIndex;
  27160. lastSignificantToken = match[0];
  27161. postfixIncDec = true;
  27162. yield ({
  27163. type: "StringLiteral",
  27164. value: match[0],
  27165. closed: match[2] !== void 0
  27166. });
  27167. continue;
  27168. }
  27169. NumericLiteral.lastIndex = lastIndex;
  27170. if (match = NumericLiteral.exec(input)) {
  27171. lastIndex = NumericLiteral.lastIndex;
  27172. lastSignificantToken = match[0];
  27173. postfixIncDec = true;
  27174. yield ({
  27175. type: "NumericLiteral",
  27176. value: match[0]
  27177. });
  27178. continue;
  27179. }
  27180. Template.lastIndex = lastIndex;
  27181. if (match = Template.exec(input)) {
  27182. lastIndex = Template.lastIndex;
  27183. lastSignificantToken = match[0];
  27184. if (match[1] === "${") {
  27185. lastSignificantToken = "?InterpolationInTemplate";
  27186. stack.push({
  27187. tag: "InterpolationInTemplate",
  27188. nesting: braces.length
  27189. });
  27190. postfixIncDec = false;
  27191. yield ({
  27192. type: "TemplateHead",
  27193. value: match[0]
  27194. });
  27195. } else {
  27196. postfixIncDec = true;
  27197. yield ({
  27198. type: "NoSubstitutionTemplate",
  27199. value: match[0],
  27200. closed: match[1] === "`"
  27201. });
  27202. }
  27203. continue;
  27204. }
  27205. break;
  27206. case "JSXTag":
  27207. case "JSXTagEnd":
  27208. JSXPunctuator.lastIndex = lastIndex;
  27209. if (match = JSXPunctuator.exec(input)) {
  27210. lastIndex = JSXPunctuator.lastIndex;
  27211. nextLastSignificantToken = match[0];
  27212. switch (match[0]) {
  27213. case "<":
  27214. stack.push({tag: "JSXTag"});
  27215. break;
  27216. case ">":
  27217. stack.pop();
  27218. if (lastSignificantToken === "/" || mode.tag === "JSXTagEnd") {
  27219. nextLastSignificantToken = "?JSX";
  27220. postfixIncDec = true;
  27221. } else {
  27222. stack.push({tag: "JSXChildren"});
  27223. }
  27224. break;
  27225. case "{":
  27226. stack.push({
  27227. tag: "InterpolationInJSX",
  27228. nesting: braces.length
  27229. });
  27230. nextLastSignificantToken = "?InterpolationInJSX";
  27231. postfixIncDec = false;
  27232. break;
  27233. case "/":
  27234. if (lastSignificantToken === "<") {
  27235. stack.pop();
  27236. if (stack[stack.length - 1].tag === "JSXChildren") {
  27237. stack.pop();
  27238. }
  27239. stack.push({tag: "JSXTagEnd"});
  27240. }
  27241. }
  27242. lastSignificantToken = nextLastSignificantToken;
  27243. yield ({
  27244. type: "JSXPunctuator",
  27245. value: match[0]
  27246. });
  27247. continue;
  27248. }
  27249. JSXIdentifier.lastIndex = lastIndex;
  27250. if (match = JSXIdentifier.exec(input)) {
  27251. lastIndex = JSXIdentifier.lastIndex;
  27252. lastSignificantToken = match[0];
  27253. yield ({
  27254. type: "JSXIdentifier",
  27255. value: match[0]
  27256. });
  27257. continue;
  27258. }
  27259. JSXString.lastIndex = lastIndex;
  27260. if (match = JSXString.exec(input)) {
  27261. lastIndex = JSXString.lastIndex;
  27262. lastSignificantToken = match[0];
  27263. yield ({
  27264. type: "JSXString",
  27265. value: match[0],
  27266. closed: match[2] !== void 0
  27267. });
  27268. continue;
  27269. }
  27270. break;
  27271. case "JSXChildren":
  27272. JSXText.lastIndex = lastIndex;
  27273. if (match = JSXText.exec(input)) {
  27274. lastIndex = JSXText.lastIndex;
  27275. lastSignificantToken = match[0];
  27276. yield ({
  27277. type: "JSXText",
  27278. value: match[0]
  27279. });
  27280. continue;
  27281. }
  27282. switch (input[lastIndex]) {
  27283. case "<":
  27284. stack.push({tag: "JSXTag"});
  27285. lastIndex++;
  27286. lastSignificantToken = "<";
  27287. yield ({
  27288. type: "JSXPunctuator",
  27289. value: "<"
  27290. });
  27291. continue;
  27292. case "{":
  27293. stack.push({
  27294. tag: "InterpolationInJSX",
  27295. nesting: braces.length
  27296. });
  27297. lastIndex++;
  27298. lastSignificantToken = "?InterpolationInJSX";
  27299. postfixIncDec = false;
  27300. yield ({
  27301. type: "JSXPunctuator",
  27302. value: "{"
  27303. });
  27304. continue;
  27305. }
  27306. }
  27307. WhiteSpace.lastIndex = lastIndex;
  27308. if (match = WhiteSpace.exec(input)) {
  27309. lastIndex = WhiteSpace.lastIndex;
  27310. yield ({
  27311. type: "WhiteSpace",
  27312. value: match[0]
  27313. });
  27314. continue;
  27315. }
  27316. LineTerminatorSequence.lastIndex = lastIndex;
  27317. if (match = LineTerminatorSequence.exec(input)) {
  27318. lastIndex = LineTerminatorSequence.lastIndex;
  27319. postfixIncDec = false;
  27320. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  27321. lastSignificantToken = "?NoLineTerminatorHere";
  27322. }
  27323. yield ({
  27324. type: "LineTerminatorSequence",
  27325. value: match[0]
  27326. });
  27327. continue;
  27328. }
  27329. MultiLineComment.lastIndex = lastIndex;
  27330. if (match = MultiLineComment.exec(input)) {
  27331. lastIndex = MultiLineComment.lastIndex;
  27332. if (Newline.test(match[0])) {
  27333. postfixIncDec = false;
  27334. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  27335. lastSignificantToken = "?NoLineTerminatorHere";
  27336. }
  27337. }
  27338. yield ({
  27339. type: "MultiLineComment",
  27340. value: match[0],
  27341. closed: match[1] !== void 0
  27342. });
  27343. continue;
  27344. }
  27345. SingleLineComment.lastIndex = lastIndex;
  27346. if (match = SingleLineComment.exec(input)) {
  27347. lastIndex = SingleLineComment.lastIndex;
  27348. postfixIncDec = false;
  27349. yield ({
  27350. type: "SingleLineComment",
  27351. value: match[0]
  27352. });
  27353. continue;
  27354. }
  27355. firstCodePoint = String.fromCodePoint(input.codePointAt(lastIndex));
  27356. lastIndex += firstCodePoint.length;
  27357. lastSignificantToken = firstCodePoint;
  27358. postfixIncDec = false;
  27359. yield ({
  27360. type: mode.tag.startsWith("JSX") ? "JSXInvalid" : "Invalid",
  27361. value: firstCodePoint
  27362. });
  27363. }
  27364. return void 0;
  27365. };
  27366. var jsTokens = /*@__PURE__*/getDefaultExportFromCjs(jsTokens_1);
  27367. function stripLiteralJsTokens(code, options) {
  27368. const FILL = options?.fillChar ?? " ";
  27369. const FILL_COMMENT = " ";
  27370. let result = "";
  27371. const filter = options?.filter ?? (() => true);
  27372. const tokens = [];
  27373. for (const token of jsTokens(code, { jsx: false })) {
  27374. tokens.push(token);
  27375. if (token.type === "SingleLineComment") {
  27376. result += FILL_COMMENT.repeat(token.value.length);
  27377. continue;
  27378. }
  27379. if (token.type === "MultiLineComment") {
  27380. result += token.value.replace(/[^\n]/g, FILL_COMMENT);
  27381. continue;
  27382. }
  27383. if (token.type === "StringLiteral") {
  27384. if (!token.closed) {
  27385. result += token.value;
  27386. continue;
  27387. }
  27388. const body = token.value.slice(1, -1);
  27389. if (filter(body)) {
  27390. result += token.value[0] + FILL.repeat(body.length) + token.value[token.value.length - 1];
  27391. continue;
  27392. }
  27393. }
  27394. if (token.type === "NoSubstitutionTemplate") {
  27395. const body = token.value.slice(1, -1);
  27396. if (filter(body)) {
  27397. result += `\`${body.replace(/[^\n]/g, FILL)}\``;
  27398. continue;
  27399. }
  27400. }
  27401. if (token.type === "RegularExpressionLiteral") {
  27402. const body = token.value;
  27403. if (filter(body)) {
  27404. result += body.replace(/\/(.*)\/(\w?)$/g, (_, $1, $2) => `/${FILL.repeat($1.length)}/${$2}`);
  27405. continue;
  27406. }
  27407. }
  27408. if (token.type === "TemplateHead") {
  27409. const body = token.value.slice(1, -2);
  27410. if (filter(body)) {
  27411. result += `\`${body.replace(/[^\n]/g, FILL)}\${`;
  27412. continue;
  27413. }
  27414. }
  27415. if (token.type === "TemplateTail") {
  27416. const body = token.value.slice(0, -2);
  27417. if (filter(body)) {
  27418. result += `}${body.replace(/[^\n]/g, FILL)}\``;
  27419. continue;
  27420. }
  27421. }
  27422. if (token.type === "TemplateMiddle") {
  27423. const body = token.value.slice(1, -2);
  27424. if (filter(body)) {
  27425. result += `}${body.replace(/[^\n]/g, FILL)}\${`;
  27426. continue;
  27427. }
  27428. }
  27429. result += token.value;
  27430. }
  27431. return {
  27432. result,
  27433. tokens
  27434. };
  27435. }
  27436. function stripLiteral(code, options) {
  27437. return stripLiteralDetailed(code, options).result;
  27438. }
  27439. function stripLiteralDetailed(code, options) {
  27440. return stripLiteralJsTokens(code, options);
  27441. }
  27442. var main$1 = {exports: {}};
  27443. var name = "dotenv";
  27444. var version$2 = "16.4.5";
  27445. var description = "Loads environment variables from .env file";
  27446. var main = "lib/main.js";
  27447. var types$2 = "lib/main.d.ts";
  27448. var exports = {
  27449. ".": {
  27450. types: "./lib/main.d.ts",
  27451. require: "./lib/main.js",
  27452. "default": "./lib/main.js"
  27453. },
  27454. "./config": "./config.js",
  27455. "./config.js": "./config.js",
  27456. "./lib/env-options": "./lib/env-options.js",
  27457. "./lib/env-options.js": "./lib/env-options.js",
  27458. "./lib/cli-options": "./lib/cli-options.js",
  27459. "./lib/cli-options.js": "./lib/cli-options.js",
  27460. "./package.json": "./package.json"
  27461. };
  27462. var scripts = {
  27463. "dts-check": "tsc --project tests/types/tsconfig.json",
  27464. lint: "standard",
  27465. "lint-readme": "standard-markdown",
  27466. pretest: "npm run lint && npm run dts-check",
  27467. test: "tap tests/*.js --100 -Rspec",
  27468. "test:coverage": "tap --coverage-report=lcov",
  27469. prerelease: "npm test",
  27470. release: "standard-version"
  27471. };
  27472. var repository = {
  27473. type: "git",
  27474. url: "git://github.com/motdotla/dotenv.git"
  27475. };
  27476. var funding = "https://dotenvx.com";
  27477. var keywords$2 = [
  27478. "dotenv",
  27479. "env",
  27480. ".env",
  27481. "environment",
  27482. "variables",
  27483. "config",
  27484. "settings"
  27485. ];
  27486. var readmeFilename = "README.md";
  27487. var license = "BSD-2-Clause";
  27488. var devDependencies = {
  27489. "@definitelytyped/dtslint": "^0.0.133",
  27490. "@types/node": "^18.11.3",
  27491. decache: "^4.6.1",
  27492. sinon: "^14.0.1",
  27493. standard: "^17.0.0",
  27494. "standard-markdown": "^7.1.0",
  27495. "standard-version": "^9.5.0",
  27496. tap: "^16.3.0",
  27497. tar: "^6.1.11",
  27498. typescript: "^4.8.4"
  27499. };
  27500. var engines = {
  27501. node: ">=12"
  27502. };
  27503. var browser$1 = {
  27504. fs: false
  27505. };
  27506. var require$$4 = {
  27507. name: name,
  27508. version: version$2,
  27509. description: description,
  27510. main: main,
  27511. types: types$2,
  27512. exports: exports,
  27513. scripts: scripts,
  27514. repository: repository,
  27515. funding: funding,
  27516. keywords: keywords$2,
  27517. readmeFilename: readmeFilename,
  27518. license: license,
  27519. devDependencies: devDependencies,
  27520. engines: engines,
  27521. browser: browser$1
  27522. };
  27523. const fs$9 = require$$0__default;
  27524. const path$9 = require$$0$4;
  27525. const os$2 = require$$2;
  27526. const crypto$1 = require$$3$1;
  27527. const packageJson = require$$4;
  27528. const version$1 = packageJson.version;
  27529. const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
  27530. // Parse src into an Object
  27531. function parse$a (src) {
  27532. const obj = {};
  27533. // Convert buffer to string
  27534. let lines = src.toString();
  27535. // Convert line breaks to same format
  27536. lines = lines.replace(/\r\n?/mg, '\n');
  27537. let match;
  27538. while ((match = LINE.exec(lines)) != null) {
  27539. const key = match[1];
  27540. // Default undefined or null to empty string
  27541. let value = (match[2] || '');
  27542. // Remove whitespace
  27543. value = value.trim();
  27544. // Check if double quoted
  27545. const maybeQuote = value[0];
  27546. // Remove surrounding quotes
  27547. value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2');
  27548. // Expand newlines if double quoted
  27549. if (maybeQuote === '"') {
  27550. value = value.replace(/\\n/g, '\n');
  27551. value = value.replace(/\\r/g, '\r');
  27552. }
  27553. // Add to object
  27554. obj[key] = value;
  27555. }
  27556. return obj
  27557. }
  27558. function _parseVault (options) {
  27559. const vaultPath = _vaultPath(options);
  27560. // Parse .env.vault
  27561. const result = DotenvModule.configDotenv({ path: vaultPath });
  27562. if (!result.parsed) {
  27563. const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
  27564. err.code = 'MISSING_DATA';
  27565. throw err
  27566. }
  27567. // handle scenario for comma separated keys - for use with key rotation
  27568. // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
  27569. const keys = _dotenvKey(options).split(',');
  27570. const length = keys.length;
  27571. let decrypted;
  27572. for (let i = 0; i < length; i++) {
  27573. try {
  27574. // Get full key
  27575. const key = keys[i].trim();
  27576. // Get instructions for decrypt
  27577. const attrs = _instructions(result, key);
  27578. // Decrypt
  27579. decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
  27580. break
  27581. } catch (error) {
  27582. // last key
  27583. if (i + 1 >= length) {
  27584. throw error
  27585. }
  27586. // try next key
  27587. }
  27588. }
  27589. // Parse decrypted .env string
  27590. return DotenvModule.parse(decrypted)
  27591. }
  27592. function _log (message) {
  27593. console.log(`[dotenv@${version$1}][INFO] ${message}`);
  27594. }
  27595. function _warn (message) {
  27596. console.log(`[dotenv@${version$1}][WARN] ${message}`);
  27597. }
  27598. function _debug (message) {
  27599. console.log(`[dotenv@${version$1}][DEBUG] ${message}`);
  27600. }
  27601. function _dotenvKey (options) {
  27602. // prioritize developer directly setting options.DOTENV_KEY
  27603. if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
  27604. return options.DOTENV_KEY
  27605. }
  27606. // secondary infra already contains a DOTENV_KEY environment variable
  27607. if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
  27608. return process.env.DOTENV_KEY
  27609. }
  27610. // fallback to empty string
  27611. return ''
  27612. }
  27613. function _instructions (result, dotenvKey) {
  27614. // Parse DOTENV_KEY. Format is a URI
  27615. let uri;
  27616. try {
  27617. uri = new URL(dotenvKey);
  27618. } catch (error) {
  27619. if (error.code === 'ERR_INVALID_URL') {
  27620. const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development');
  27621. err.code = 'INVALID_DOTENV_KEY';
  27622. throw err
  27623. }
  27624. throw error
  27625. }
  27626. // Get decrypt key
  27627. const key = uri.password;
  27628. if (!key) {
  27629. const err = new Error('INVALID_DOTENV_KEY: Missing key part');
  27630. err.code = 'INVALID_DOTENV_KEY';
  27631. throw err
  27632. }
  27633. // Get environment
  27634. const environment = uri.searchParams.get('environment');
  27635. if (!environment) {
  27636. const err = new Error('INVALID_DOTENV_KEY: Missing environment part');
  27637. err.code = 'INVALID_DOTENV_KEY';
  27638. throw err
  27639. }
  27640. // Get ciphertext payload
  27641. const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
  27642. const ciphertext = result.parsed[environmentKey]; // DOTENV_VAULT_PRODUCTION
  27643. if (!ciphertext) {
  27644. const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
  27645. err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT';
  27646. throw err
  27647. }
  27648. return { ciphertext, key }
  27649. }
  27650. function _vaultPath (options) {
  27651. let possibleVaultPath = null;
  27652. if (options && options.path && options.path.length > 0) {
  27653. if (Array.isArray(options.path)) {
  27654. for (const filepath of options.path) {
  27655. if (fs$9.existsSync(filepath)) {
  27656. possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`;
  27657. }
  27658. }
  27659. } else {
  27660. possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`;
  27661. }
  27662. } else {
  27663. possibleVaultPath = path$9.resolve(process.cwd(), '.env.vault');
  27664. }
  27665. if (fs$9.existsSync(possibleVaultPath)) {
  27666. return possibleVaultPath
  27667. }
  27668. return null
  27669. }
  27670. function _resolveHome (envPath) {
  27671. return envPath[0] === '~' ? path$9.join(os$2.homedir(), envPath.slice(1)) : envPath
  27672. }
  27673. function _configVault (options) {
  27674. _log('Loading env from encrypted .env.vault');
  27675. const parsed = DotenvModule._parseVault(options);
  27676. let processEnv = process.env;
  27677. if (options && options.processEnv != null) {
  27678. processEnv = options.processEnv;
  27679. }
  27680. DotenvModule.populate(processEnv, parsed, options);
  27681. return { parsed }
  27682. }
  27683. function configDotenv (options) {
  27684. const dotenvPath = path$9.resolve(process.cwd(), '.env');
  27685. let encoding = 'utf8';
  27686. const debug = Boolean(options && options.debug);
  27687. if (options && options.encoding) {
  27688. encoding = options.encoding;
  27689. } else {
  27690. if (debug) {
  27691. _debug('No encoding is specified. UTF-8 is used by default');
  27692. }
  27693. }
  27694. let optionPaths = [dotenvPath]; // default, look for .env
  27695. if (options && options.path) {
  27696. if (!Array.isArray(options.path)) {
  27697. optionPaths = [_resolveHome(options.path)];
  27698. } else {
  27699. optionPaths = []; // reset default
  27700. for (const filepath of options.path) {
  27701. optionPaths.push(_resolveHome(filepath));
  27702. }
  27703. }
  27704. }
  27705. // Build the parsed data in a temporary object (because we need to return it). Once we have the final
  27706. // parsed data, we will combine it with process.env (or options.processEnv if provided).
  27707. let lastError;
  27708. const parsedAll = {};
  27709. for (const path of optionPaths) {
  27710. try {
  27711. // Specifying an encoding returns a string instead of a buffer
  27712. const parsed = DotenvModule.parse(fs$9.readFileSync(path, { encoding }));
  27713. DotenvModule.populate(parsedAll, parsed, options);
  27714. } catch (e) {
  27715. if (debug) {
  27716. _debug(`Failed to load ${path} ${e.message}`);
  27717. }
  27718. lastError = e;
  27719. }
  27720. }
  27721. let processEnv = process.env;
  27722. if (options && options.processEnv != null) {
  27723. processEnv = options.processEnv;
  27724. }
  27725. DotenvModule.populate(processEnv, parsedAll, options);
  27726. if (lastError) {
  27727. return { parsed: parsedAll, error: lastError }
  27728. } else {
  27729. return { parsed: parsedAll }
  27730. }
  27731. }
  27732. // Populates process.env from .env file
  27733. function config (options) {
  27734. // fallback to original dotenv if DOTENV_KEY is not set
  27735. if (_dotenvKey(options).length === 0) {
  27736. return DotenvModule.configDotenv(options)
  27737. }
  27738. const vaultPath = _vaultPath(options);
  27739. // dotenvKey exists but .env.vault file does not exist
  27740. if (!vaultPath) {
  27741. _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
  27742. return DotenvModule.configDotenv(options)
  27743. }
  27744. return DotenvModule._configVault(options)
  27745. }
  27746. function decrypt (encrypted, keyStr) {
  27747. const key = Buffer.from(keyStr.slice(-64), 'hex');
  27748. let ciphertext = Buffer.from(encrypted, 'base64');
  27749. const nonce = ciphertext.subarray(0, 12);
  27750. const authTag = ciphertext.subarray(-16);
  27751. ciphertext = ciphertext.subarray(12, -16);
  27752. try {
  27753. const aesgcm = crypto$1.createDecipheriv('aes-256-gcm', key, nonce);
  27754. aesgcm.setAuthTag(authTag);
  27755. return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
  27756. } catch (error) {
  27757. const isRange = error instanceof RangeError;
  27758. const invalidKeyLength = error.message === 'Invalid key length';
  27759. const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data';
  27760. if (isRange || invalidKeyLength) {
  27761. const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)');
  27762. err.code = 'INVALID_DOTENV_KEY';
  27763. throw err
  27764. } else if (decryptionFailed) {
  27765. const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY');
  27766. err.code = 'DECRYPTION_FAILED';
  27767. throw err
  27768. } else {
  27769. throw error
  27770. }
  27771. }
  27772. }
  27773. // Populate process.env with parsed values
  27774. function populate (processEnv, parsed, options = {}) {
  27775. const debug = Boolean(options && options.debug);
  27776. const override = Boolean(options && options.override);
  27777. if (typeof parsed !== 'object') {
  27778. const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate');
  27779. err.code = 'OBJECT_REQUIRED';
  27780. throw err
  27781. }
  27782. // Set process.env
  27783. for (const key of Object.keys(parsed)) {
  27784. if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
  27785. if (override === true) {
  27786. processEnv[key] = parsed[key];
  27787. }
  27788. if (debug) {
  27789. if (override === true) {
  27790. _debug(`"${key}" is already defined and WAS overwritten`);
  27791. } else {
  27792. _debug(`"${key}" is already defined and was NOT overwritten`);
  27793. }
  27794. }
  27795. } else {
  27796. processEnv[key] = parsed[key];
  27797. }
  27798. }
  27799. }
  27800. const DotenvModule = {
  27801. configDotenv,
  27802. _configVault,
  27803. _parseVault,
  27804. config,
  27805. decrypt,
  27806. parse: parse$a,
  27807. populate
  27808. };
  27809. main$1.exports.configDotenv = DotenvModule.configDotenv;
  27810. main$1.exports._configVault = DotenvModule._configVault;
  27811. main$1.exports._parseVault = DotenvModule._parseVault;
  27812. main$1.exports.config = DotenvModule.config;
  27813. main$1.exports.decrypt = DotenvModule.decrypt;
  27814. var parse_1$1 = main$1.exports.parse = DotenvModule.parse;
  27815. main$1.exports.populate = DotenvModule.populate;
  27816. main$1.exports = DotenvModule;
  27817. // * /
  27818. // * (\\)? # is it escaped with a backslash?
  27819. // * (\$) # literal $
  27820. // * (?!\() # shouldnt be followed by parenthesis
  27821. // * (\{?) # first brace wrap opening
  27822. // * ([\w.]+) # key
  27823. // * (?::-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))? # optional default nested 3 times
  27824. // * (\}?) # last brace warp closing
  27825. // * /xi
  27826. const DOTENV_SUBSTITUTION_REGEX = /(\\)?(\$)(?!\()(\{?)([\w.]+)(?::?-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))?(\}?)/gi;
  27827. function _resolveEscapeSequences (value) {
  27828. return value.replace(/\\\$/g, '$')
  27829. }
  27830. function interpolate (value, processEnv, parsed) {
  27831. return value.replace(DOTENV_SUBSTITUTION_REGEX, (match, escaped, dollarSign, openBrace, key, defaultValue, closeBrace) => {
  27832. if (escaped === '\\') {
  27833. return match.slice(1)
  27834. } else {
  27835. if (processEnv[key]) {
  27836. if (processEnv[key] === parsed[key]) {
  27837. return processEnv[key]
  27838. } else {
  27839. // scenario: PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
  27840. return interpolate(processEnv[key], processEnv, parsed)
  27841. }
  27842. }
  27843. if (parsed[key]) {
  27844. // avoid recursion from EXPAND_SELF=$EXPAND_SELF
  27845. if (parsed[key] === value) {
  27846. return parsed[key]
  27847. } else {
  27848. return interpolate(parsed[key], processEnv, parsed)
  27849. }
  27850. }
  27851. if (defaultValue) {
  27852. if (defaultValue.startsWith('$')) {
  27853. return interpolate(defaultValue, processEnv, parsed)
  27854. } else {
  27855. return defaultValue
  27856. }
  27857. }
  27858. return ''
  27859. }
  27860. })
  27861. }
  27862. function expand (options) {
  27863. let processEnv = process.env;
  27864. if (options && options.processEnv != null) {
  27865. processEnv = options.processEnv;
  27866. }
  27867. for (const key in options.parsed) {
  27868. let value = options.parsed[key];
  27869. const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key);
  27870. if (inProcessEnv) {
  27871. if (processEnv[key] === options.parsed[key]) {
  27872. // assume was set to processEnv from the .env file if the values match and therefore interpolate
  27873. value = interpolate(value, processEnv, options.parsed);
  27874. } else {
  27875. // do not interpolate - assume processEnv had the intended value even if containing a $.
  27876. value = processEnv[key];
  27877. }
  27878. } else {
  27879. // not inProcessEnv so assume interpolation for this .env key
  27880. value = interpolate(value, processEnv, options.parsed);
  27881. }
  27882. options.parsed[key] = _resolveEscapeSequences(value);
  27883. }
  27884. for (const processKey in options.parsed) {
  27885. processEnv[processKey] = options.parsed[processKey];
  27886. }
  27887. return options
  27888. }
  27889. var expand_1 = expand;
  27890. function getEnvFilesForMode(mode, envDir) {
  27891. return [
  27892. /** default file */ `.env`,
  27893. /** local file */ `.env.local`,
  27894. /** mode file */ `.env.${mode}`,
  27895. /** mode local file */ `.env.${mode}.local`,
  27896. ].map((file) => normalizePath$3(path$o.join(envDir, file)));
  27897. }
  27898. function loadEnv(mode, envDir, prefixes = 'VITE_') {
  27899. if (mode === 'local') {
  27900. throw new Error(`"local" cannot be used as a mode name because it conflicts with ` +
  27901. `the .local postfix for .env files.`);
  27902. }
  27903. prefixes = arraify(prefixes);
  27904. const env = {};
  27905. const envFiles = getEnvFilesForMode(mode, envDir);
  27906. const parsed = Object.fromEntries(envFiles.flatMap((filePath) => {
  27907. if (!tryStatSync(filePath)?.isFile())
  27908. return [];
  27909. return Object.entries(parse_1$1(fs$l.readFileSync(filePath)));
  27910. }));
  27911. // test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
  27912. if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
  27913. process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV;
  27914. }
  27915. // support BROWSER and BROWSER_ARGS env variables
  27916. if (parsed.BROWSER && process.env.BROWSER === undefined) {
  27917. process.env.BROWSER = parsed.BROWSER;
  27918. }
  27919. if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === undefined) {
  27920. process.env.BROWSER_ARGS = parsed.BROWSER_ARGS;
  27921. }
  27922. // let environment variables use each other. make a copy of `process.env` so that `dotenv-expand`
  27923. // doesn't re-assign the expanded values to the global `process.env`.
  27924. const processEnv = { ...process.env };
  27925. expand_1({ parsed, processEnv });
  27926. // only keys that start with prefix are exposed to client
  27927. for (const [key, value] of Object.entries(parsed)) {
  27928. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  27929. env[key] = value;
  27930. }
  27931. }
  27932. // check if there are actual env variables starting with VITE_*
  27933. // these are typically provided inline and should be prioritized
  27934. for (const key in process.env) {
  27935. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  27936. env[key] = process.env[key];
  27937. }
  27938. }
  27939. return env;
  27940. }
  27941. function resolveEnvPrefix({ envPrefix = 'VITE_', }) {
  27942. envPrefix = arraify(envPrefix);
  27943. if (envPrefix.includes('')) {
  27944. throw new Error(`envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`);
  27945. }
  27946. return envPrefix;
  27947. }
  27948. const modulePreloadPolyfillId = 'vite/modulepreload-polyfill';
  27949. const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js';
  27950. function modulePreloadPolyfillPlugin(config) {
  27951. // `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
  27952. const skip = config.command !== 'build' || config.build.ssr;
  27953. let polyfillString;
  27954. return {
  27955. name: 'vite:modulepreload-polyfill',
  27956. resolveId(id) {
  27957. if (id === modulePreloadPolyfillId) {
  27958. return resolvedModulePreloadPolyfillId;
  27959. }
  27960. },
  27961. load(id) {
  27962. if (id === resolvedModulePreloadPolyfillId) {
  27963. if (skip) {
  27964. return '';
  27965. }
  27966. if (!polyfillString) {
  27967. polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`;
  27968. }
  27969. return { code: polyfillString, moduleSideEffects: true };
  27970. }
  27971. },
  27972. };
  27973. }
  27974. function polyfill() {
  27975. const relList = document.createElement('link').relList;
  27976. if (relList && relList.supports && relList.supports('modulepreload')) {
  27977. return;
  27978. }
  27979. for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
  27980. processPreload(link);
  27981. }
  27982. new MutationObserver((mutations) => {
  27983. for (const mutation of mutations) {
  27984. if (mutation.type !== 'childList') {
  27985. continue;
  27986. }
  27987. for (const node of mutation.addedNodes) {
  27988. if (node.tagName === 'LINK' && node.rel === 'modulepreload')
  27989. processPreload(node);
  27990. }
  27991. }
  27992. }).observe(document, { childList: true, subtree: true });
  27993. function getFetchOpts(link) {
  27994. const fetchOpts = {};
  27995. if (link.integrity)
  27996. fetchOpts.integrity = link.integrity;
  27997. if (link.referrerPolicy)
  27998. fetchOpts.referrerPolicy = link.referrerPolicy;
  27999. if (link.crossOrigin === 'use-credentials')
  28000. fetchOpts.credentials = 'include';
  28001. else if (link.crossOrigin === 'anonymous')
  28002. fetchOpts.credentials = 'omit';
  28003. else
  28004. fetchOpts.credentials = 'same-origin';
  28005. return fetchOpts;
  28006. }
  28007. function processPreload(link) {
  28008. if (link.ep)
  28009. // ep marker = processed
  28010. return;
  28011. link.ep = true;
  28012. // prepopulate the load record
  28013. const fetchOpts = getFetchOpts(link);
  28014. fetch(link.href, fetchOpts);
  28015. }
  28016. }
  28017. const htmlProxyRE$1 = /\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(js|css)$/;
  28018. const isHtmlProxyRE = /\?html-proxy\b/;
  28019. const inlineCSSRE$1 = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g;
  28020. // Do not allow preceding '.', but do allow preceding '...' for spread operations
  28021. const inlineImportRE = /(?<!(?<!\.\.)\.)\bimport\s*\(("(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*')\)/dg;
  28022. const htmlLangRE = /\.(?:html|htm)$/;
  28023. const spaceRe = /[\t\n\f\r ]/;
  28024. const importMapRE = /[ \t]*<script[^>]*type\s*=\s*(?:"importmap"|'importmap'|importmap)[^>]*>.*?<\/script>/is;
  28025. const moduleScriptRE = /[ \t]*<script[^>]*type\s*=\s*(?:"module"|'module'|module)[^>]*>/i;
  28026. const modulePreloadLinkRE = /[ \t]*<link[^>]*rel\s*=\s*(?:"modulepreload"|'modulepreload'|modulepreload)[\s\S]*?\/>/i;
  28027. const importMapAppendRE = new RegExp([moduleScriptRE, modulePreloadLinkRE].map((r) => r.source).join('|'), 'i');
  28028. const isHTMLProxy = (id) => isHtmlProxyRE.test(id);
  28029. const isHTMLRequest = (request) => htmlLangRE.test(request);
  28030. // HTML Proxy Caches are stored by config -> filePath -> index
  28031. const htmlProxyMap = new WeakMap();
  28032. // HTML Proxy Transform result are stored by config
  28033. // `${hash(importer)}_${query.index}` -> transformed css code
  28034. // PS: key like `hash(/vite/playground/assets/index.html)_1`)
  28035. const htmlProxyResult = new Map();
  28036. function htmlInlineProxyPlugin(config) {
  28037. // Should do this when `constructor` rather than when `buildStart`,
  28038. // `buildStart` will be triggered multiple times then the cached result will be emptied.
  28039. // https://github.com/vitejs/vite/issues/6372
  28040. htmlProxyMap.set(config, new Map());
  28041. return {
  28042. name: 'vite:html-inline-proxy',
  28043. resolveId(id) {
  28044. if (isHTMLProxy(id)) {
  28045. return id;
  28046. }
  28047. },
  28048. load(id) {
  28049. const proxyMatch = id.match(htmlProxyRE$1);
  28050. if (proxyMatch) {
  28051. const index = Number(proxyMatch[1]);
  28052. const file = cleanUrl(id);
  28053. const url = file.replace(normalizePath$3(config.root), '');
  28054. const result = htmlProxyMap.get(config).get(url)?.[index];
  28055. if (result) {
  28056. return result;
  28057. }
  28058. else {
  28059. throw new Error(`No matching HTML proxy module found from ${id}`);
  28060. }
  28061. }
  28062. },
  28063. };
  28064. }
  28065. function addToHTMLProxyCache(config, filePath, index, result) {
  28066. if (!htmlProxyMap.get(config)) {
  28067. htmlProxyMap.set(config, new Map());
  28068. }
  28069. if (!htmlProxyMap.get(config).get(filePath)) {
  28070. htmlProxyMap.get(config).set(filePath, []);
  28071. }
  28072. htmlProxyMap.get(config).get(filePath)[index] = result;
  28073. }
  28074. function addToHTMLProxyTransformResult(hash, code) {
  28075. htmlProxyResult.set(hash, code);
  28076. }
  28077. // this extends the config in @vue/compiler-sfc with <link href>
  28078. const assetAttrsConfig = {
  28079. link: ['href'],
  28080. video: ['src', 'poster'],
  28081. source: ['src', 'srcset'],
  28082. img: ['src', 'srcset'],
  28083. image: ['xlink:href', 'href'],
  28084. use: ['xlink:href', 'href'],
  28085. };
  28086. // Some `<link rel>` elements should not be inlined in build. Excluding:
  28087. // - `shortcut` : only valid for IE <9, use `icon`
  28088. // - `mask-icon` : deprecated since Safari 12 (for pinned tabs)
  28089. // - `apple-touch-icon-precomposed` : only valid for iOS <7 (for avoiding gloss effect)
  28090. const noInlineLinkRels = new Set([
  28091. 'icon',
  28092. 'apple-touch-icon',
  28093. 'apple-touch-startup-image',
  28094. 'manifest',
  28095. ]);
  28096. const isAsyncScriptMap = new WeakMap();
  28097. function nodeIsElement(node) {
  28098. return node.nodeName[0] !== '#';
  28099. }
  28100. function traverseNodes(node, visitor) {
  28101. visitor(node);
  28102. if (nodeIsElement(node) ||
  28103. node.nodeName === '#document' ||
  28104. node.nodeName === '#document-fragment') {
  28105. node.childNodes.forEach((childNode) => traverseNodes(childNode, visitor));
  28106. }
  28107. }
  28108. async function traverseHtml(html, filePath, visitor) {
  28109. // lazy load compiler
  28110. const { parse } = await import('./dep-CrWVpuYf.js');
  28111. const ast = parse(html, {
  28112. scriptingEnabled: false,
  28113. sourceCodeLocationInfo: true,
  28114. onParseError: (e) => {
  28115. handleParseError(e, html, filePath);
  28116. },
  28117. });
  28118. traverseNodes(ast, visitor);
  28119. }
  28120. function getScriptInfo(node) {
  28121. let src;
  28122. let sourceCodeLocation;
  28123. let isModule = false;
  28124. let isAsync = false;
  28125. for (const p of node.attrs) {
  28126. if (p.prefix !== undefined)
  28127. continue;
  28128. if (p.name === 'src') {
  28129. if (!src) {
  28130. src = p;
  28131. sourceCodeLocation = node.sourceCodeLocation?.attrs['src'];
  28132. }
  28133. }
  28134. else if (p.name === 'type' && p.value && p.value === 'module') {
  28135. isModule = true;
  28136. }
  28137. else if (p.name === 'async') {
  28138. isAsync = true;
  28139. }
  28140. }
  28141. return { src, sourceCodeLocation, isModule, isAsync };
  28142. }
  28143. const attrValueStartRE = /=\s*(.)/;
  28144. function overwriteAttrValue(s, sourceCodeLocation, newValue) {
  28145. const srcString = s.slice(sourceCodeLocation.startOffset, sourceCodeLocation.endOffset);
  28146. const valueStart = srcString.match(attrValueStartRE);
  28147. if (!valueStart) {
  28148. // overwrite attr value can only be called for a well-defined value
  28149. throw new Error(`[vite:html] internal error, failed to overwrite attribute value`);
  28150. }
  28151. const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0;
  28152. const valueOffset = valueStart.index + valueStart[0].length - 1;
  28153. s.update(sourceCodeLocation.startOffset + valueOffset + wrapOffset, sourceCodeLocation.endOffset - wrapOffset, newValue);
  28154. return s;
  28155. }
  28156. /**
  28157. * Format parse5 @type {ParserError} to @type {RollupError}
  28158. */
  28159. function formatParseError(parserError, id, html) {
  28160. const formattedError = {
  28161. code: parserError.code,
  28162. message: `parse5 error code ${parserError.code}`,
  28163. frame: generateCodeFrame(html, parserError.startOffset, parserError.endOffset),
  28164. loc: {
  28165. file: id,
  28166. line: parserError.startLine,
  28167. column: parserError.startCol,
  28168. },
  28169. };
  28170. return formattedError;
  28171. }
  28172. function handleParseError(parserError, html, filePath) {
  28173. switch (parserError.code) {
  28174. case 'missing-doctype':
  28175. // ignore missing DOCTYPE
  28176. return;
  28177. case 'abandoned-head-element-child':
  28178. // Accept elements without closing tag in <head>
  28179. return;
  28180. case 'duplicate-attribute':
  28181. // Accept duplicate attributes #9566
  28182. // The first attribute is used, browsers silently ignore duplicates
  28183. return;
  28184. case 'non-void-html-element-start-tag-with-trailing-solidus':
  28185. // Allow self closing on non-void elements #10439
  28186. return;
  28187. }
  28188. const parseError = formatParseError(parserError, filePath, html);
  28189. throw new Error(`Unable to parse HTML; ${parseError.message}\n` +
  28190. ` at ${parseError.loc.file}:${parseError.loc.line}:${parseError.loc.column}\n` +
  28191. `${parseError.frame}`);
  28192. }
  28193. /**
  28194. * Compiles index.html into an entry js module
  28195. */
  28196. function buildHtmlPlugin(config) {
  28197. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(config.plugins, config.logger);
  28198. preHooks.unshift(injectCspNonceMetaTagHook(config));
  28199. preHooks.unshift(preImportMapHook(config));
  28200. preHooks.push(htmlEnvHook(config));
  28201. postHooks.push(injectNonceAttributeTagHook(config));
  28202. postHooks.push(postImportMapHook());
  28203. const processedHtml = new Map();
  28204. const isExcludedUrl = (url) => url[0] === '#' || isExternalUrl(url) || isDataUrl(url);
  28205. // Same reason with `htmlInlineProxyPlugin`
  28206. isAsyncScriptMap.set(config, new Map());
  28207. return {
  28208. name: 'vite:build-html',
  28209. async transform(html, id) {
  28210. if (id.endsWith('.html')) {
  28211. id = normalizePath$3(id);
  28212. const relativeUrlPath = path$o.posix.relative(config.root, id);
  28213. const publicPath = `/${relativeUrlPath}`;
  28214. const publicBase = getBaseInHTML(relativeUrlPath, config);
  28215. const publicToRelative = (filename, importer) => publicBase + filename;
  28216. const toOutputPublicFilePath = (url) => toOutputFilePathInHtml(url.slice(1), 'public', relativeUrlPath, 'html', config, publicToRelative);
  28217. // Determines true start position for the node, either the < character
  28218. // position, or the newline at the end of the previous line's node.
  28219. const nodeStartWithLeadingWhitespace = (node) => {
  28220. const startOffset = node.sourceCodeLocation.startOffset;
  28221. if (startOffset === 0)
  28222. return 0;
  28223. // Gets the offset for the start of the line including the
  28224. // newline trailing the previous node
  28225. const lineStartOffset = startOffset - node.sourceCodeLocation.startCol;
  28226. // <previous-line-node></previous-line-node>
  28227. // <target-node></target-node>
  28228. //
  28229. // Here we want to target the newline at the end of the previous line
  28230. // as the start position for our target.
  28231. //
  28232. // <previous-node></previous-node>
  28233. // <doubled-up-node></doubled-up-node><target-node></target-node>
  28234. //
  28235. // However, if there is content between our target node start and the
  28236. // previous newline, we cannot strip it out without risking content deletion.
  28237. let isLineEmpty = false;
  28238. try {
  28239. const line = s.slice(Math.max(0, lineStartOffset), startOffset);
  28240. isLineEmpty = !line.trim();
  28241. }
  28242. catch {
  28243. // magic-string may throw if there's some content removed in the sliced string,
  28244. // which we ignore and assume the line is not empty
  28245. }
  28246. return isLineEmpty ? lineStartOffset : startOffset;
  28247. };
  28248. // pre-transform
  28249. html = await applyHtmlTransforms(html, preHooks, {
  28250. path: publicPath,
  28251. filename: id,
  28252. });
  28253. let js = '';
  28254. const s = new MagicString(html);
  28255. const scriptUrls = [];
  28256. const styleUrls = [];
  28257. let inlineModuleIndex = -1;
  28258. let everyScriptIsAsync = true;
  28259. let someScriptsAreAsync = false;
  28260. let someScriptsAreDefer = false;
  28261. const assetUrlsPromises = [];
  28262. // for each encountered asset url, rewrite original html so that it
  28263. // references the post-build location, ignoring empty attributes and
  28264. // attributes that directly reference named output.
  28265. const namedOutput = Object.keys(config?.build?.rollupOptions?.input || {});
  28266. const processAssetUrl = async (url, shouldInline) => {
  28267. if (url !== '' && // Empty attribute
  28268. !namedOutput.includes(url) && // Direct reference to named output
  28269. !namedOutput.includes(removeLeadingSlash(url)) // Allow for absolute references as named output can't be an absolute path
  28270. ) {
  28271. try {
  28272. return await urlToBuiltUrl(url, id, config, this, shouldInline);
  28273. }
  28274. catch (e) {
  28275. if (e.code !== 'ENOENT') {
  28276. throw e;
  28277. }
  28278. }
  28279. }
  28280. return url;
  28281. };
  28282. await traverseHtml(html, id, (node) => {
  28283. if (!nodeIsElement(node)) {
  28284. return;
  28285. }
  28286. let shouldRemove = false;
  28287. // script tags
  28288. if (node.nodeName === 'script') {
  28289. const { src, sourceCodeLocation, isModule, isAsync } = getScriptInfo(node);
  28290. const url = src && src.value;
  28291. const isPublicFile = !!(url && checkPublicFile(url, config));
  28292. if (isPublicFile) {
  28293. // referencing public dir url, prefix with base
  28294. overwriteAttrValue(s, sourceCodeLocation, partialEncodeURIPath(toOutputPublicFilePath(url)));
  28295. }
  28296. if (isModule) {
  28297. inlineModuleIndex++;
  28298. if (url && !isExcludedUrl(url) && !isPublicFile) {
  28299. // <script type="module" src="..."/>
  28300. // add it as an import
  28301. js += `\nimport ${JSON.stringify(url)}`;
  28302. shouldRemove = true;
  28303. }
  28304. else if (node.childNodes.length) {
  28305. const scriptNode = node.childNodes.pop();
  28306. const contents = scriptNode.value;
  28307. // <script type="module">...</script>
  28308. const filePath = id.replace(normalizePath$3(config.root), '');
  28309. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  28310. code: contents,
  28311. });
  28312. js += `\nimport "${id}?html-proxy&index=${inlineModuleIndex}.js"`;
  28313. shouldRemove = true;
  28314. }
  28315. everyScriptIsAsync &&= isAsync;
  28316. someScriptsAreAsync ||= isAsync;
  28317. someScriptsAreDefer ||= !isAsync;
  28318. }
  28319. else if (url && !isPublicFile) {
  28320. if (!isExcludedUrl(url)) {
  28321. config.logger.warn(`<script src="${url}"> in "${publicPath}" can't be bundled without type="module" attribute`);
  28322. }
  28323. }
  28324. else if (node.childNodes.length) {
  28325. const scriptNode = node.childNodes.pop();
  28326. scriptUrls.push(...extractImportExpressionFromClassicScript(scriptNode));
  28327. }
  28328. }
  28329. // For asset references in index.html, also generate an import
  28330. // statement for each - this will be handled by the asset plugin
  28331. const assetAttrs = assetAttrsConfig[node.nodeName];
  28332. if (assetAttrs) {
  28333. for (const p of node.attrs) {
  28334. const attrKey = getAttrKey(p);
  28335. if (p.value && assetAttrs.includes(attrKey)) {
  28336. if (attrKey === 'srcset') {
  28337. assetUrlsPromises.push((async () => {
  28338. const processedEncodedUrl = await processSrcSet(p.value, async ({ url }) => {
  28339. const decodedUrl = decodeURI(url);
  28340. if (!isExcludedUrl(decodedUrl)) {
  28341. const result = await processAssetUrl(url);
  28342. return result !== decodedUrl
  28343. ? encodeURIPath(result)
  28344. : url;
  28345. }
  28346. return url;
  28347. });
  28348. if (processedEncodedUrl !== p.value) {
  28349. overwriteAttrValue(s, getAttrSourceCodeLocation(node, attrKey), processedEncodedUrl);
  28350. }
  28351. })());
  28352. }
  28353. else {
  28354. const url = decodeURI(p.value);
  28355. if (checkPublicFile(url, config)) {
  28356. overwriteAttrValue(s, getAttrSourceCodeLocation(node, attrKey), partialEncodeURIPath(toOutputPublicFilePath(url)));
  28357. }
  28358. else if (!isExcludedUrl(url)) {
  28359. if (node.nodeName === 'link' &&
  28360. isCSSRequest(url) &&
  28361. // should not be converted if following attributes are present (#6748)
  28362. !node.attrs.some((p) => p.prefix === undefined &&
  28363. (p.name === 'media' || p.name === 'disabled'))) {
  28364. // CSS references, convert to import
  28365. const importExpression = `\nimport ${JSON.stringify(url)}`;
  28366. styleUrls.push({
  28367. url,
  28368. start: nodeStartWithLeadingWhitespace(node),
  28369. end: node.sourceCodeLocation.endOffset,
  28370. });
  28371. js += importExpression;
  28372. }
  28373. else {
  28374. // If the node is a link, check if it can be inlined. If not, set `shouldInline`
  28375. // to `false` to force no inline. If `undefined`, it leaves to the default heuristics.
  28376. const isNoInlineLink = node.nodeName === 'link' &&
  28377. node.attrs.some((p) => p.name === 'rel' &&
  28378. parseRelAttr(p.value).some((v) => noInlineLinkRels.has(v)));
  28379. const shouldInline = isNoInlineLink ? false : undefined;
  28380. assetUrlsPromises.push((async () => {
  28381. const processedUrl = await processAssetUrl(url, shouldInline);
  28382. if (processedUrl !== url) {
  28383. overwriteAttrValue(s, getAttrSourceCodeLocation(node, attrKey), partialEncodeURIPath(processedUrl));
  28384. }
  28385. })());
  28386. }
  28387. }
  28388. }
  28389. }
  28390. }
  28391. }
  28392. const inlineStyle = findNeedTransformStyleAttribute(node);
  28393. if (inlineStyle) {
  28394. inlineModuleIndex++;
  28395. // replace `inline style` with __VITE_INLINE_CSS__**_**__
  28396. // and import css in js code
  28397. const code = inlineStyle.attr.value;
  28398. const filePath = id.replace(normalizePath$3(config.root), '');
  28399. addToHTMLProxyCache(config, filePath, inlineModuleIndex, { code });
  28400. // will transform with css plugin and cache result with css-post plugin
  28401. js += `\nimport "${id}?html-proxy&inline-css&style-attr&index=${inlineModuleIndex}.css"`;
  28402. const hash = getHash(cleanUrl(id));
  28403. // will transform in `applyHtmlTransforms`
  28404. overwriteAttrValue(s, inlineStyle.location, `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`);
  28405. }
  28406. // <style>...</style>
  28407. if (node.nodeName === 'style' && node.childNodes.length) {
  28408. const styleNode = node.childNodes.pop();
  28409. const filePath = id.replace(normalizePath$3(config.root), '');
  28410. inlineModuleIndex++;
  28411. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  28412. code: styleNode.value,
  28413. });
  28414. js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`;
  28415. const hash = getHash(cleanUrl(id));
  28416. // will transform in `applyHtmlTransforms`
  28417. s.update(styleNode.sourceCodeLocation.startOffset, styleNode.sourceCodeLocation.endOffset, `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`);
  28418. }
  28419. if (shouldRemove) {
  28420. // remove the script tag from the html. we are going to inject new
  28421. // ones in the end.
  28422. s.remove(nodeStartWithLeadingWhitespace(node), node.sourceCodeLocation.endOffset);
  28423. }
  28424. });
  28425. isAsyncScriptMap.get(config).set(id, everyScriptIsAsync);
  28426. if (someScriptsAreAsync && someScriptsAreDefer) {
  28427. config.logger.warn(`\nMixed async and defer script modules in ${id}, output script will fallback to defer. Every script, including inline ones, need to be marked as async for your output script to be async.`);
  28428. }
  28429. await Promise.all(assetUrlsPromises);
  28430. // emit <script>import("./aaa")</script> asset
  28431. for (const { start, end, url } of scriptUrls) {
  28432. if (checkPublicFile(url, config)) {
  28433. s.update(start, end, partialEncodeURIPath(toOutputPublicFilePath(url)));
  28434. }
  28435. else if (!isExcludedUrl(url)) {
  28436. s.update(start, end, partialEncodeURIPath(await urlToBuiltUrl(url, id, config, this)));
  28437. }
  28438. }
  28439. // ignore <link rel="stylesheet"> if its url can't be resolved
  28440. const resolvedStyleUrls = await Promise.all(styleUrls.map(async (styleUrl) => ({
  28441. ...styleUrl,
  28442. resolved: await this.resolve(styleUrl.url, id),
  28443. })));
  28444. for (const { start, end, url, resolved } of resolvedStyleUrls) {
  28445. if (resolved == null) {
  28446. config.logger.warnOnce(`\n${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime`);
  28447. const importExpression = `\nimport ${JSON.stringify(url)}`;
  28448. js = js.replace(importExpression, '');
  28449. }
  28450. else {
  28451. s.remove(start, end);
  28452. }
  28453. }
  28454. processedHtml.set(id, s.toString());
  28455. // inject module preload polyfill only when configured and needed
  28456. const { modulePreload } = config.build;
  28457. if (modulePreload !== false &&
  28458. modulePreload.polyfill &&
  28459. (someScriptsAreAsync || someScriptsAreDefer)) {
  28460. js = `import "${modulePreloadPolyfillId}";\n${js}`;
  28461. }
  28462. // Force rollup to keep this module from being shared between other entry points.
  28463. // If the resulting chunk is empty, it will be removed in generateBundle.
  28464. return { code: js, moduleSideEffects: 'no-treeshake' };
  28465. }
  28466. },
  28467. async generateBundle(options, bundle) {
  28468. const analyzedChunk = new Map();
  28469. const inlineEntryChunk = new Set();
  28470. const getImportedChunks = (chunk, seen = new Set()) => {
  28471. const chunks = [];
  28472. chunk.imports.forEach((file) => {
  28473. const importee = bundle[file];
  28474. if (importee?.type === 'chunk' && !seen.has(file)) {
  28475. seen.add(file);
  28476. // post-order traversal
  28477. chunks.push(...getImportedChunks(importee, seen));
  28478. chunks.push(importee);
  28479. }
  28480. });
  28481. return chunks;
  28482. };
  28483. const toScriptTag = (chunk, toOutputPath, isAsync) => ({
  28484. tag: 'script',
  28485. attrs: {
  28486. ...(isAsync ? { async: true } : {}),
  28487. type: 'module',
  28488. // crossorigin must be set not only for serving assets in a different origin
  28489. // but also to make it possible to preload the script using `<link rel="preload">`.
  28490. // `<script type="module">` used to fetch the script with credential mode `omit`,
  28491. // however `crossorigin` attribute cannot specify that value.
  28492. // https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules:~:text=For%20%3Cscript%3E,of%20other%20modules.
  28493. // Now `<script type="module">` uses `same origin`: https://github.com/whatwg/html/pull/3656#:~:text=Module%20scripts%20are%20always%20fetched%20with%20credentials%20mode%20%22same%2Dorigin%22%20by%20default%20and%20can%20no%20longer%0Ause%20%22omit%22
  28494. crossorigin: true,
  28495. src: toOutputPath(chunk.fileName),
  28496. },
  28497. });
  28498. const toPreloadTag = (filename, toOutputPath) => ({
  28499. tag: 'link',
  28500. attrs: {
  28501. rel: 'modulepreload',
  28502. crossorigin: true,
  28503. href: toOutputPath(filename),
  28504. },
  28505. });
  28506. const getCssTagsForChunk = (chunk, toOutputPath, seen = new Set()) => {
  28507. const tags = [];
  28508. if (!analyzedChunk.has(chunk)) {
  28509. analyzedChunk.set(chunk, 1);
  28510. chunk.imports.forEach((file) => {
  28511. const importee = bundle[file];
  28512. if (importee?.type === 'chunk') {
  28513. tags.push(...getCssTagsForChunk(importee, toOutputPath, seen));
  28514. }
  28515. });
  28516. }
  28517. chunk.viteMetadata.importedCss.forEach((file) => {
  28518. if (!seen.has(file)) {
  28519. seen.add(file);
  28520. tags.push({
  28521. tag: 'link',
  28522. attrs: {
  28523. rel: 'stylesheet',
  28524. crossorigin: true,
  28525. href: toOutputPath(file),
  28526. },
  28527. });
  28528. }
  28529. });
  28530. return tags;
  28531. };
  28532. for (const [id, html] of processedHtml) {
  28533. const relativeUrlPath = path$o.posix.relative(config.root, normalizePath$3(id));
  28534. const assetsBase = getBaseInHTML(relativeUrlPath, config);
  28535. const toOutputFilePath = (filename, type) => {
  28536. if (isExternalUrl(filename)) {
  28537. return filename;
  28538. }
  28539. else {
  28540. return toOutputFilePathInHtml(filename, type, relativeUrlPath, 'html', config, (filename, importer) => assetsBase + filename);
  28541. }
  28542. };
  28543. const toOutputAssetFilePath = (filename) => toOutputFilePath(filename, 'asset');
  28544. const toOutputPublicAssetFilePath = (filename) => toOutputFilePath(filename, 'public');
  28545. const isAsync = isAsyncScriptMap.get(config).get(id);
  28546. let result = html;
  28547. // find corresponding entry chunk
  28548. const chunk = Object.values(bundle).find((chunk) => chunk.type === 'chunk' &&
  28549. chunk.isEntry &&
  28550. chunk.facadeModuleId === id);
  28551. let canInlineEntry = false;
  28552. // inject chunk asset links
  28553. if (chunk) {
  28554. // an entry chunk can be inlined if
  28555. // - it's an ES module (e.g. not generated by the legacy plugin)
  28556. // - it contains no meaningful code other than import statements
  28557. if (options.format === 'es' && isEntirelyImport(chunk.code)) {
  28558. canInlineEntry = true;
  28559. }
  28560. // when not inlined, inject <script> for entry and modulepreload its dependencies
  28561. // when inlined, discard entry chunk and inject <script> for everything in post-order
  28562. const imports = getImportedChunks(chunk);
  28563. let assetTags;
  28564. if (canInlineEntry) {
  28565. assetTags = imports.map((chunk) => toScriptTag(chunk, toOutputAssetFilePath, isAsync));
  28566. }
  28567. else {
  28568. assetTags = [toScriptTag(chunk, toOutputAssetFilePath, isAsync)];
  28569. const { modulePreload } = config.build;
  28570. if (modulePreload !== false) {
  28571. const resolveDependencies = typeof modulePreload === 'object' &&
  28572. modulePreload.resolveDependencies;
  28573. const importsFileNames = imports.map((chunk) => chunk.fileName);
  28574. const resolvedDeps = resolveDependencies
  28575. ? resolveDependencies(chunk.fileName, importsFileNames, {
  28576. hostId: relativeUrlPath,
  28577. hostType: 'html',
  28578. })
  28579. : importsFileNames;
  28580. assetTags.push(...resolvedDeps.map((i) => toPreloadTag(i, toOutputAssetFilePath)));
  28581. }
  28582. }
  28583. assetTags.push(...getCssTagsForChunk(chunk, toOutputAssetFilePath));
  28584. result = injectToHead(result, assetTags);
  28585. }
  28586. // inject css link when cssCodeSplit is false
  28587. if (!config.build.cssCodeSplit) {
  28588. const cssChunk = Object.values(bundle).find((chunk) => chunk.type === 'asset' && chunk.name === 'style.css');
  28589. if (cssChunk) {
  28590. result = injectToHead(result, [
  28591. {
  28592. tag: 'link',
  28593. attrs: {
  28594. rel: 'stylesheet',
  28595. crossorigin: true,
  28596. href: toOutputAssetFilePath(cssChunk.fileName),
  28597. },
  28598. },
  28599. ]);
  28600. }
  28601. }
  28602. // no use assets plugin because it will emit file
  28603. let match;
  28604. let s;
  28605. inlineCSSRE$1.lastIndex = 0;
  28606. while ((match = inlineCSSRE$1.exec(result))) {
  28607. s ||= new MagicString(result);
  28608. const { 0: full, 1: scopedName } = match;
  28609. const cssTransformedCode = htmlProxyResult.get(scopedName);
  28610. s.update(match.index, match.index + full.length, cssTransformedCode);
  28611. }
  28612. if (s) {
  28613. result = s.toString();
  28614. }
  28615. result = await applyHtmlTransforms(result, [...normalHooks, ...postHooks], {
  28616. path: '/' + relativeUrlPath,
  28617. filename: id,
  28618. bundle,
  28619. chunk,
  28620. });
  28621. // resolve asset url references
  28622. result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => {
  28623. const file = this.getFileName(fileHash);
  28624. if (chunk) {
  28625. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  28626. }
  28627. return encodeURIPath(toOutputAssetFilePath(file)) + postfix;
  28628. });
  28629. result = result.replace(publicAssetUrlRE, (_, fileHash) => {
  28630. const publicAssetPath = toOutputPublicAssetFilePath(getPublicAssetFilename(fileHash, config));
  28631. return encodeURIPath(urlCanParse(publicAssetPath)
  28632. ? publicAssetPath
  28633. : normalizePath$3(publicAssetPath));
  28634. });
  28635. if (chunk && canInlineEntry) {
  28636. inlineEntryChunk.add(chunk.fileName);
  28637. }
  28638. const shortEmitName = normalizePath$3(path$o.relative(config.root, id));
  28639. this.emitFile({
  28640. type: 'asset',
  28641. fileName: shortEmitName,
  28642. source: result,
  28643. });
  28644. }
  28645. for (const fileName of inlineEntryChunk) {
  28646. // all imports from entry have been inlined to html, prevent rollup from outputting it
  28647. delete bundle[fileName];
  28648. }
  28649. },
  28650. };
  28651. }
  28652. function parseRelAttr(attr) {
  28653. return attr.split(spaceRe).map((v) => v.toLowerCase());
  28654. }
  28655. // <tag style="... url(...) or image-set(...) ..."></tag>
  28656. // extract inline styles as virtual css
  28657. function findNeedTransformStyleAttribute(node) {
  28658. const attr = node.attrs.find((prop) => prop.prefix === undefined &&
  28659. prop.name === 'style' &&
  28660. // only url(...) or image-set(...) in css need to emit file
  28661. (prop.value.includes('url(') || prop.value.includes('image-set(')));
  28662. if (!attr)
  28663. return undefined;
  28664. const location = node.sourceCodeLocation?.attrs?.['style'];
  28665. return { attr, location };
  28666. }
  28667. function extractImportExpressionFromClassicScript(scriptTextNode) {
  28668. const startOffset = scriptTextNode.sourceCodeLocation.startOffset;
  28669. const cleanCode = stripLiteral(scriptTextNode.value);
  28670. const scriptUrls = [];
  28671. let match;
  28672. inlineImportRE.lastIndex = 0;
  28673. while ((match = inlineImportRE.exec(cleanCode))) {
  28674. const [, [urlStart, urlEnd]] = match.indices;
  28675. const start = urlStart + 1;
  28676. const end = urlEnd - 1;
  28677. scriptUrls.push({
  28678. start: start + startOffset,
  28679. end: end + startOffset,
  28680. url: scriptTextNode.value.slice(start, end),
  28681. });
  28682. }
  28683. return scriptUrls;
  28684. }
  28685. function preImportMapHook(config) {
  28686. return (html, ctx) => {
  28687. const importMapIndex = html.search(importMapRE);
  28688. if (importMapIndex < 0)
  28689. return;
  28690. const importMapAppendIndex = html.search(importMapAppendRE);
  28691. if (importMapAppendIndex < 0)
  28692. return;
  28693. if (importMapAppendIndex < importMapIndex) {
  28694. const relativeHtml = normalizePath$3(path$o.relative(config.root, ctx.filename));
  28695. config.logger.warnOnce(colors$1.yellow(colors$1.bold(`(!) <script type="importmap"> should come before <script type="module"> and <link rel="modulepreload"> in /${relativeHtml}`)));
  28696. }
  28697. };
  28698. }
  28699. /**
  28700. * Move importmap before the first module script and modulepreload link
  28701. */
  28702. function postImportMapHook() {
  28703. return (html) => {
  28704. if (!importMapAppendRE.test(html))
  28705. return;
  28706. let importMap;
  28707. html = html.replace(importMapRE, (match) => {
  28708. importMap = match;
  28709. return '';
  28710. });
  28711. if (importMap) {
  28712. html = html.replace(importMapAppendRE, (match) => `${importMap}\n${match}`);
  28713. }
  28714. return html;
  28715. };
  28716. }
  28717. function injectCspNonceMetaTagHook(config) {
  28718. return () => {
  28719. if (!config.html?.cspNonce)
  28720. return;
  28721. return [
  28722. {
  28723. tag: 'meta',
  28724. injectTo: 'head',
  28725. // use nonce attribute so that it's hidden
  28726. // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#accessing_nonces_and_nonce_hiding
  28727. attrs: { property: 'csp-nonce', nonce: config.html.cspNonce },
  28728. },
  28729. ];
  28730. };
  28731. }
  28732. /**
  28733. * Support `%ENV_NAME%` syntax in html files
  28734. */
  28735. function htmlEnvHook(config) {
  28736. const pattern = /%(\S+?)%/g;
  28737. const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix });
  28738. const env = { ...config.env };
  28739. // account for user env defines
  28740. for (const key in config.define) {
  28741. if (key.startsWith(`import.meta.env.`)) {
  28742. const val = config.define[key];
  28743. if (typeof val === 'string') {
  28744. try {
  28745. const parsed = JSON.parse(val);
  28746. env[key.slice(16)] = typeof parsed === 'string' ? parsed : val;
  28747. }
  28748. catch {
  28749. env[key.slice(16)] = val;
  28750. }
  28751. }
  28752. else {
  28753. env[key.slice(16)] = JSON.stringify(val);
  28754. }
  28755. }
  28756. }
  28757. return (html, ctx) => {
  28758. return html.replace(pattern, (text, key) => {
  28759. if (key in env) {
  28760. return env[key];
  28761. }
  28762. else {
  28763. if (envPrefix.some((prefix) => key.startsWith(prefix))) {
  28764. const relativeHtml = normalizePath$3(path$o.relative(config.root, ctx.filename));
  28765. config.logger.warn(colors$1.yellow(colors$1.bold(`(!) ${text} is not defined in env variables found in /${relativeHtml}. ` +
  28766. `Is the variable mistyped?`)));
  28767. }
  28768. return text;
  28769. }
  28770. });
  28771. };
  28772. }
  28773. function injectNonceAttributeTagHook(config) {
  28774. const processRelType = new Set(['stylesheet', 'modulepreload', 'preload']);
  28775. return async (html, { filename }) => {
  28776. const nonce = config.html?.cspNonce;
  28777. if (!nonce)
  28778. return;
  28779. const s = new MagicString(html);
  28780. await traverseHtml(html, filename, (node) => {
  28781. if (!nodeIsElement(node)) {
  28782. return;
  28783. }
  28784. const { nodeName, attrs, sourceCodeLocation } = node;
  28785. if (nodeName === 'script' ||
  28786. nodeName === 'style' ||
  28787. (nodeName === 'link' &&
  28788. attrs.some((attr) => attr.name === 'rel' &&
  28789. parseRelAttr(attr.value).some((a) => processRelType.has(a))))) {
  28790. // If we already have a nonce attribute, we don't need to add another one
  28791. if (attrs.some(({ name }) => name === 'nonce')) {
  28792. return;
  28793. }
  28794. const startTagEndOffset = sourceCodeLocation.startTag.endOffset;
  28795. // if the closing of the start tag includes a `/`, the offset should be 2 so the nonce
  28796. // is appended prior to the `/`
  28797. const appendOffset = html[startTagEndOffset - 2] === '/' ? 2 : 1;
  28798. s.appendRight(startTagEndOffset - appendOffset, ` nonce="${nonce}"`);
  28799. }
  28800. });
  28801. return s.toString();
  28802. };
  28803. }
  28804. function resolveHtmlTransforms(plugins, logger) {
  28805. const preHooks = [];
  28806. const normalHooks = [];
  28807. const postHooks = [];
  28808. for (const plugin of plugins) {
  28809. const hook = plugin.transformIndexHtml;
  28810. if (!hook)
  28811. continue;
  28812. if (typeof hook === 'function') {
  28813. normalHooks.push(hook);
  28814. }
  28815. else {
  28816. if (!('order' in hook) && 'enforce' in hook) {
  28817. logger.warnOnce(colors$1.yellow(`plugin '${plugin.name}' uses deprecated 'enforce' option. Use 'order' option instead.`));
  28818. }
  28819. if (!('handler' in hook) && 'transform' in hook) {
  28820. logger.warnOnce(colors$1.yellow(`plugin '${plugin.name}' uses deprecated 'transform' option. Use 'handler' option instead.`));
  28821. }
  28822. // `enforce` had only two possible values for the `transformIndexHtml` hook
  28823. // `'pre'` and `'post'` (the default). `order` now works with three values
  28824. // to align with other hooks (`'pre'`, normal, and `'post'`). We map
  28825. // both `enforce: 'post'` to `order: undefined` to avoid a breaking change
  28826. const order = hook.order ?? (hook.enforce === 'pre' ? 'pre' : undefined);
  28827. // @ts-expect-error union type
  28828. const handler = hook.handler ?? hook.transform;
  28829. if (order === 'pre') {
  28830. preHooks.push(handler);
  28831. }
  28832. else if (order === 'post') {
  28833. postHooks.push(handler);
  28834. }
  28835. else {
  28836. normalHooks.push(handler);
  28837. }
  28838. }
  28839. }
  28840. return [preHooks, normalHooks, postHooks];
  28841. }
  28842. async function applyHtmlTransforms(html, hooks, ctx) {
  28843. for (const hook of hooks) {
  28844. const res = await hook(html, ctx);
  28845. if (!res) {
  28846. continue;
  28847. }
  28848. if (typeof res === 'string') {
  28849. html = res;
  28850. }
  28851. else {
  28852. let tags;
  28853. if (Array.isArray(res)) {
  28854. tags = res;
  28855. }
  28856. else {
  28857. html = res.html || html;
  28858. tags = res.tags;
  28859. }
  28860. let headTags;
  28861. let headPrependTags;
  28862. let bodyTags;
  28863. let bodyPrependTags;
  28864. for (const tag of tags) {
  28865. switch (tag.injectTo) {
  28866. case 'body':
  28867. (bodyTags ??= []).push(tag);
  28868. break;
  28869. case 'body-prepend':
  28870. (bodyPrependTags ??= []).push(tag);
  28871. break;
  28872. case 'head':
  28873. (headTags ??= []).push(tag);
  28874. break;
  28875. default:
  28876. (headPrependTags ??= []).push(tag);
  28877. }
  28878. }
  28879. if (headPrependTags)
  28880. html = injectToHead(html, headPrependTags, true);
  28881. if (headTags)
  28882. html = injectToHead(html, headTags);
  28883. if (bodyPrependTags)
  28884. html = injectToBody(html, bodyPrependTags, true);
  28885. if (bodyTags)
  28886. html = injectToBody(html, bodyTags);
  28887. }
  28888. }
  28889. return html;
  28890. }
  28891. const importRE = /\bimport\s*("[^"]*[^\\]"|'[^']*[^\\]');*/g;
  28892. const commentRE$1 = /\/\*[\s\S]*?\*\/|\/\/.*$/gm;
  28893. function isEntirelyImport(code) {
  28894. // only consider "side-effect" imports, which match <script type=module> semantics exactly
  28895. // the regexes will remove too little in some exotic cases, but false-negatives are alright
  28896. return !code.replace(importRE, '').replace(commentRE$1, '').trim().length;
  28897. }
  28898. function getBaseInHTML(urlRelativePath, config) {
  28899. // Prefer explicit URL if defined for linking to assets and public files from HTML,
  28900. // even when base relative is specified
  28901. return config.base === './' || config.base === ''
  28902. ? path$o.posix.join(path$o.posix.relative(urlRelativePath, '').slice(0, -2), './')
  28903. : config.base;
  28904. }
  28905. const headInjectRE = /([ \t]*)<\/head>/i;
  28906. const headPrependInjectRE = /([ \t]*)<head[^>]*>/i;
  28907. const htmlInjectRE = /<\/html>/i;
  28908. const htmlPrependInjectRE = /([ \t]*)<html[^>]*>/i;
  28909. const bodyInjectRE = /([ \t]*)<\/body>/i;
  28910. const bodyPrependInjectRE = /([ \t]*)<body[^>]*>/i;
  28911. const doctypePrependInjectRE = /<!doctype html>/i;
  28912. function injectToHead(html, tags, prepend = false) {
  28913. if (tags.length === 0)
  28914. return html;
  28915. if (prepend) {
  28916. // inject as the first element of head
  28917. if (headPrependInjectRE.test(html)) {
  28918. return html.replace(headPrependInjectRE, (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`);
  28919. }
  28920. }
  28921. else {
  28922. // inject before head close
  28923. if (headInjectRE.test(html)) {
  28924. // respect indentation of head tag
  28925. return html.replace(headInjectRE, (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`);
  28926. }
  28927. // try to inject before the body tag
  28928. if (bodyPrependInjectRE.test(html)) {
  28929. return html.replace(bodyPrependInjectRE, (match, p1) => `${serializeTags(tags, p1)}\n${match}`);
  28930. }
  28931. }
  28932. // if no head tag is present, we prepend the tag for both prepend and append
  28933. return prependInjectFallback(html, tags);
  28934. }
  28935. function injectToBody(html, tags, prepend = false) {
  28936. if (tags.length === 0)
  28937. return html;
  28938. if (prepend) {
  28939. // inject after body open
  28940. if (bodyPrependInjectRE.test(html)) {
  28941. return html.replace(bodyPrependInjectRE, (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`);
  28942. }
  28943. // if no there is no body tag, inject after head or fallback to prepend in html
  28944. if (headInjectRE.test(html)) {
  28945. return html.replace(headInjectRE, (match, p1) => `${match}\n${serializeTags(tags, p1)}`);
  28946. }
  28947. return prependInjectFallback(html, tags);
  28948. }
  28949. else {
  28950. // inject before body close
  28951. if (bodyInjectRE.test(html)) {
  28952. return html.replace(bodyInjectRE, (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`);
  28953. }
  28954. // if no body tag is present, append to the html tag, or at the end of the file
  28955. if (htmlInjectRE.test(html)) {
  28956. return html.replace(htmlInjectRE, `${serializeTags(tags)}\n$&`);
  28957. }
  28958. return html + `\n` + serializeTags(tags);
  28959. }
  28960. }
  28961. function prependInjectFallback(html, tags) {
  28962. // prepend to the html tag, append after doctype, or the document start
  28963. if (htmlPrependInjectRE.test(html)) {
  28964. return html.replace(htmlPrependInjectRE, `$&\n${serializeTags(tags)}`);
  28965. }
  28966. if (doctypePrependInjectRE.test(html)) {
  28967. return html.replace(doctypePrependInjectRE, `$&\n${serializeTags(tags)}`);
  28968. }
  28969. return serializeTags(tags) + html;
  28970. }
  28971. const unaryTags = new Set(['link', 'meta', 'base']);
  28972. function serializeTag({ tag, attrs, children }, indent = '') {
  28973. if (unaryTags.has(tag)) {
  28974. return `<${tag}${serializeAttrs(attrs)}>`;
  28975. }
  28976. else {
  28977. return `<${tag}${serializeAttrs(attrs)}>${serializeTags(children, incrementIndent(indent))}</${tag}>`;
  28978. }
  28979. }
  28980. function serializeTags(tags, indent = '') {
  28981. if (typeof tags === 'string') {
  28982. return tags;
  28983. }
  28984. else if (tags && tags.length) {
  28985. return tags.map((tag) => `${indent}${serializeTag(tag, indent)}\n`).join('');
  28986. }
  28987. return '';
  28988. }
  28989. function serializeAttrs(attrs) {
  28990. let res = '';
  28991. for (const key in attrs) {
  28992. if (typeof attrs[key] === 'boolean') {
  28993. res += attrs[key] ? ` ${key}` : ``;
  28994. }
  28995. else {
  28996. res += ` ${key}=${JSON.stringify(attrs[key])}`;
  28997. }
  28998. }
  28999. return res;
  29000. }
  29001. function incrementIndent(indent = '') {
  29002. return `${indent}${indent[0] === '\t' ? '\t' : ' '}`;
  29003. }
  29004. function getAttrKey(attr) {
  29005. return attr.prefix === undefined ? attr.name : `${attr.prefix}:${attr.name}`;
  29006. }
  29007. function getAttrSourceCodeLocation(node, attrKey) {
  29008. return node.sourceCodeLocation.attrs[attrKey];
  29009. }
  29010. function resolveCSSOptions(options) {
  29011. if (options?.transformer === 'lightningcss') {
  29012. return {
  29013. ...options,
  29014. lightningcss: {
  29015. ...options.lightningcss,
  29016. targets: options.lightningcss?.targets ??
  29017. convertTargets(ESBUILD_MODULES_TARGET),
  29018. },
  29019. };
  29020. }
  29021. return { ...options, lightningcss: undefined };
  29022. }
  29023. const cssModuleRE = new RegExp(`\\.module${CSS_LANGS_RE.source}`);
  29024. const directRequestRE = /[?&]direct\b/;
  29025. const htmlProxyRE = /[?&]html-proxy\b/;
  29026. const htmlProxyIndexRE = /&index=(\d+)/;
  29027. const commonjsProxyRE = /\?commonjs-proxy/;
  29028. const inlineRE$1 = /[?&]inline\b/;
  29029. const inlineCSSRE = /[?&]inline-css\b/;
  29030. const styleAttrRE = /[?&]style-attr\b/;
  29031. const functionCallRE = /^[A-Z_][\w-]*\(/i;
  29032. const transformOnlyRE = /[?&]transform-only\b/;
  29033. const nonEscapedDoubleQuoteRe = /(?<!\\)(")/g;
  29034. const cssBundleName = 'style.css';
  29035. const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
  29036. const isModuleCSSRequest = (request) => cssModuleRE.test(request);
  29037. const isDirectCSSRequest = (request) => CSS_LANGS_RE.test(request) && directRequestRE.test(request);
  29038. const isDirectRequest = (request) => directRequestRE.test(request);
  29039. const cssModulesCache = new WeakMap();
  29040. const removedPureCssFilesCache = new WeakMap();
  29041. const postcssConfigCache = new WeakMap();
  29042. function encodePublicUrlsInCSS(config) {
  29043. return config.command === 'build';
  29044. }
  29045. const cssUrlAssetRE = /__VITE_CSS_URL__([\da-f]+)__/g;
  29046. /**
  29047. * Plugin applied before user plugins
  29048. */
  29049. function cssPlugin(config) {
  29050. const isBuild = config.command === 'build';
  29051. let moduleCache;
  29052. const resolveUrl = config.createResolver({
  29053. preferRelative: true,
  29054. tryIndex: false,
  29055. extensions: [],
  29056. });
  29057. let preprocessorWorkerController;
  29058. // warm up cache for resolved postcss config
  29059. if (config.css?.transformer !== 'lightningcss') {
  29060. resolvePostcssConfig(config);
  29061. }
  29062. return {
  29063. name: 'vite:css',
  29064. buildStart() {
  29065. // Ensure a new cache for every build (i.e. rebuilding in watch mode)
  29066. moduleCache = new Map();
  29067. cssModulesCache.set(config, moduleCache);
  29068. removedPureCssFilesCache.set(config, new Map());
  29069. preprocessorWorkerController = createPreprocessorWorkerController(normalizeMaxWorkers(config.css.preprocessorMaxWorkers));
  29070. preprocessorWorkerControllerCache.set(config, preprocessorWorkerController);
  29071. },
  29072. buildEnd() {
  29073. preprocessorWorkerController?.close();
  29074. },
  29075. async load(id) {
  29076. if (!isCSSRequest(id))
  29077. return;
  29078. if (urlRE.test(id)) {
  29079. if (isModuleCSSRequest(id)) {
  29080. throw new Error(`?url is not supported with CSS modules. (tried to import ${JSON.stringify(id)})`);
  29081. }
  29082. // *.css?url
  29083. // in dev, it's handled by assets plugin.
  29084. if (isBuild) {
  29085. id = injectQuery(removeUrlQuery(id), 'transform-only');
  29086. return (`import ${JSON.stringify(id)};` +
  29087. `export default "__VITE_CSS_URL__${Buffer.from(id).toString('hex')}__"`);
  29088. }
  29089. }
  29090. },
  29091. async transform(raw, id) {
  29092. if (!isCSSRequest(id) ||
  29093. commonjsProxyRE.test(id) ||
  29094. SPECIAL_QUERY_RE.test(id)) {
  29095. return;
  29096. }
  29097. const urlReplacer = async (url, importer) => {
  29098. const decodedUrl = decodeURI(url);
  29099. if (checkPublicFile(decodedUrl, config)) {
  29100. if (encodePublicUrlsInCSS(config)) {
  29101. return publicFileToBuiltUrl(decodedUrl, config);
  29102. }
  29103. else {
  29104. return joinUrlSegments(config.base, decodedUrl);
  29105. }
  29106. }
  29107. const resolved = await resolveUrl(decodedUrl, importer);
  29108. if (resolved) {
  29109. return fileToUrl$1(resolved, config, this);
  29110. }
  29111. if (config.command === 'build') {
  29112. const isExternal = config.build.rollupOptions.external
  29113. ? resolveUserExternal(config.build.rollupOptions.external, decodedUrl, // use URL as id since id could not be resolved
  29114. id, false)
  29115. : false;
  29116. if (!isExternal) {
  29117. // #9800 If we cannot resolve the css url, leave a warning.
  29118. config.logger.warnOnce(`\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`);
  29119. }
  29120. }
  29121. return url;
  29122. };
  29123. const { code: css, modules, deps, map, } = await compileCSS(id, raw, config, preprocessorWorkerController, urlReplacer);
  29124. if (modules) {
  29125. moduleCache.set(id, modules);
  29126. }
  29127. if (deps) {
  29128. for (const file of deps) {
  29129. this.addWatchFile(file);
  29130. }
  29131. }
  29132. return {
  29133. code: css,
  29134. map,
  29135. };
  29136. },
  29137. };
  29138. }
  29139. /**
  29140. * Plugin applied after user plugins
  29141. */
  29142. function cssPostPlugin(config) {
  29143. // styles initialization in buildStart causes a styling loss in watch
  29144. const styles = new Map();
  29145. // queue to emit css serially to guarantee the files are emitted in a deterministic order
  29146. let codeSplitEmitQueue = createSerialPromiseQueue();
  29147. const urlEmitQueue = createSerialPromiseQueue();
  29148. let pureCssChunks;
  29149. // when there are multiple rollup outputs and extracting CSS, only emit once,
  29150. // since output formats have no effect on the generated CSS.
  29151. let hasEmitted = false;
  29152. let chunkCSSMap;
  29153. const rollupOptionsOutput = config.build.rollupOptions.output;
  29154. const assetFileNames = (Array.isArray(rollupOptionsOutput)
  29155. ? rollupOptionsOutput[0]
  29156. : rollupOptionsOutput)?.assetFileNames;
  29157. const getCssAssetDirname = (cssAssetName) => {
  29158. const cssAssetNameDir = path$o.dirname(cssAssetName);
  29159. if (!assetFileNames) {
  29160. return path$o.join(config.build.assetsDir, cssAssetNameDir);
  29161. }
  29162. else if (typeof assetFileNames === 'string') {
  29163. return path$o.join(path$o.dirname(assetFileNames), cssAssetNameDir);
  29164. }
  29165. else {
  29166. return path$o.dirname(assetFileNames({
  29167. name: cssAssetName,
  29168. type: 'asset',
  29169. source: '/* vite internal call, ignore */',
  29170. }));
  29171. }
  29172. };
  29173. return {
  29174. name: 'vite:css-post',
  29175. renderStart() {
  29176. // Ensure new caches for every build (i.e. rebuilding in watch mode)
  29177. pureCssChunks = new Set();
  29178. hasEmitted = false;
  29179. chunkCSSMap = new Map();
  29180. codeSplitEmitQueue = createSerialPromiseQueue();
  29181. },
  29182. async transform(css, id, options) {
  29183. if (!isCSSRequest(id) ||
  29184. commonjsProxyRE.test(id) ||
  29185. SPECIAL_QUERY_RE.test(id)) {
  29186. return;
  29187. }
  29188. css = stripBomTag(css);
  29189. // cache css compile result to map
  29190. // and then use the cache replace inline-style-flag
  29191. // when `generateBundle` in vite:build-html plugin and devHtmlHook
  29192. const inlineCSS = inlineCSSRE.test(id);
  29193. const isHTMLProxy = htmlProxyRE.test(id);
  29194. if (inlineCSS && isHTMLProxy) {
  29195. if (styleAttrRE.test(id)) {
  29196. css = css.replace(/"/g, '&quot;');
  29197. }
  29198. const index = htmlProxyIndexRE.exec(id)?.[1];
  29199. if (index == null) {
  29200. throw new Error(`HTML proxy index in "${id}" not found`);
  29201. }
  29202. addToHTMLProxyTransformResult(`${getHash(cleanUrl(id))}_${Number.parseInt(index)}`, css);
  29203. return `export default ''`;
  29204. }
  29205. const inlined = inlineRE$1.test(id);
  29206. const modules = cssModulesCache.get(config).get(id);
  29207. // #6984, #7552
  29208. // `foo.module.css` => modulesCode
  29209. // `foo.module.css?inline` => cssContent
  29210. const modulesCode = modules &&
  29211. !inlined &&
  29212. dataToEsm(modules, { namedExports: true, preferConst: true });
  29213. if (config.command === 'serve') {
  29214. const getContentWithSourcemap = async (content) => {
  29215. if (config.css?.devSourcemap) {
  29216. const sourcemap = this.getCombinedSourcemap();
  29217. if (sourcemap.mappings) {
  29218. await injectSourcesContent(sourcemap, cleanUrl(id), config.logger);
  29219. }
  29220. return getCodeWithSourcemap('css', content, sourcemap);
  29221. }
  29222. return content;
  29223. };
  29224. if (isDirectCSSRequest(id)) {
  29225. return null;
  29226. }
  29227. // server only
  29228. if (options?.ssr) {
  29229. return modulesCode || `export default ${JSON.stringify(css)}`;
  29230. }
  29231. if (inlined) {
  29232. return `export default ${JSON.stringify(css)}`;
  29233. }
  29234. const cssContent = await getContentWithSourcemap(css);
  29235. const code = [
  29236. `import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(path$o.posix.join(config.base, CLIENT_PUBLIC_PATH))}`,
  29237. `const __vite__id = ${JSON.stringify(id)}`,
  29238. `const __vite__css = ${JSON.stringify(cssContent)}`,
  29239. `__vite__updateStyle(__vite__id, __vite__css)`,
  29240. // css modules exports change on edit so it can't self accept
  29241. `${modulesCode || 'import.meta.hot.accept()'}`,
  29242. `import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`,
  29243. ].join('\n');
  29244. return { code, map: { mappings: '' } };
  29245. }
  29246. // build CSS handling ----------------------------------------------------
  29247. // record css
  29248. if (!inlined) {
  29249. styles.set(id, css);
  29250. }
  29251. let code;
  29252. if (modulesCode) {
  29253. code = modulesCode;
  29254. }
  29255. else if (inlined) {
  29256. let content = css;
  29257. if (config.build.cssMinify) {
  29258. content = await minifyCSS(content, config, true);
  29259. }
  29260. code = `export default ${JSON.stringify(content)}`;
  29261. }
  29262. else {
  29263. // empty module when it's not a CSS module nor `?inline`
  29264. code = '';
  29265. }
  29266. return {
  29267. code,
  29268. map: { mappings: '' },
  29269. // avoid the css module from being tree-shaken so that we can retrieve
  29270. // it in renderChunk()
  29271. moduleSideEffects: modulesCode || inlined ? false : 'no-treeshake',
  29272. };
  29273. },
  29274. async renderChunk(code, chunk, opts) {
  29275. let chunkCSS = '';
  29276. let isPureCssChunk = true;
  29277. const ids = Object.keys(chunk.modules);
  29278. for (const id of ids) {
  29279. if (styles.has(id)) {
  29280. // ?transform-only is used for ?url and shouldn't be included in normal CSS chunks
  29281. if (!transformOnlyRE.test(id)) {
  29282. chunkCSS += styles.get(id);
  29283. // a css module contains JS, so it makes this not a pure css chunk
  29284. if (cssModuleRE.test(id)) {
  29285. isPureCssChunk = false;
  29286. }
  29287. }
  29288. }
  29289. else {
  29290. // if the module does not have a style, then it's not a pure css chunk.
  29291. // this is true because in the `transform` hook above, only modules
  29292. // that are css gets added to the `styles` map.
  29293. isPureCssChunk = false;
  29294. }
  29295. }
  29296. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  29297. // resolve asset URL placeholders to their built file URLs
  29298. const resolveAssetUrlsInCss = (chunkCSS, cssAssetName) => {
  29299. const encodedPublicUrls = encodePublicUrlsInCSS(config);
  29300. const relative = config.base === './' || config.base === '';
  29301. const cssAssetDirname = encodedPublicUrls || relative
  29302. ? slash$1(getCssAssetDirname(cssAssetName))
  29303. : undefined;
  29304. const toRelative = (filename) => {
  29305. // relative base + extracted CSS
  29306. const relativePath = path$o.posix.relative(cssAssetDirname, filename);
  29307. return relativePath[0] === '.' ? relativePath : './' + relativePath;
  29308. };
  29309. // replace asset url references with resolved url.
  29310. chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
  29311. const filename = this.getFileName(fileHash) + postfix;
  29312. chunk.viteMetadata.importedAssets.add(cleanUrl(filename));
  29313. return encodeURIPath(toOutputFilePathInCss(filename, 'asset', cssAssetName, 'css', config, toRelative));
  29314. });
  29315. // resolve public URL from CSS paths
  29316. if (encodedPublicUrls) {
  29317. const relativePathToPublicFromCSS = path$o.posix.relative(cssAssetDirname, '');
  29318. chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => {
  29319. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  29320. return encodeURIPath(toOutputFilePathInCss(publicUrl, 'public', cssAssetName, 'css', config, () => `${relativePathToPublicFromCSS}/${publicUrl}`));
  29321. });
  29322. }
  29323. return chunkCSS;
  29324. };
  29325. function ensureFileExt(name, ext) {
  29326. return normalizePath$3(path$o.format({ ...path$o.parse(name), base: undefined, ext }));
  29327. }
  29328. let s;
  29329. const urlEmitTasks = [];
  29330. if (code.includes('__VITE_CSS_URL__')) {
  29331. let match;
  29332. cssUrlAssetRE.lastIndex = 0;
  29333. while ((match = cssUrlAssetRE.exec(code))) {
  29334. const [full, idHex] = match;
  29335. const id = Buffer.from(idHex, 'hex').toString();
  29336. const originalFilename = cleanUrl(id);
  29337. const cssAssetName = ensureFileExt(path$o.basename(originalFilename), '.css');
  29338. if (!styles.has(id)) {
  29339. throw new Error(`css content for ${JSON.stringify(id)} was not found`);
  29340. }
  29341. let cssContent = styles.get(id);
  29342. cssContent = resolveAssetUrlsInCss(cssContent, cssAssetName);
  29343. urlEmitTasks.push({
  29344. cssAssetName,
  29345. originalFilename,
  29346. content: cssContent,
  29347. start: match.index,
  29348. end: match.index + full.length,
  29349. });
  29350. }
  29351. }
  29352. // should await even if this chunk does not include __VITE_CSS_URL__
  29353. // so that code after this line runs in the same order
  29354. await urlEmitQueue.run(async () => Promise.all(urlEmitTasks.map(async (info) => {
  29355. info.content = await finalizeCss(info.content, true, config);
  29356. })));
  29357. if (urlEmitTasks.length > 0) {
  29358. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(opts.format, config.isWorker);
  29359. s ||= new MagicString(code);
  29360. for (const { cssAssetName, originalFilename, content, start, end, } of urlEmitTasks) {
  29361. const referenceId = this.emitFile({
  29362. name: cssAssetName,
  29363. type: 'asset',
  29364. source: content,
  29365. });
  29366. generatedAssets
  29367. .get(config)
  29368. .set(referenceId, { originalName: originalFilename });
  29369. const replacement = toOutputFilePathInJS(this.getFileName(referenceId), 'asset', chunk.fileName, 'js', config, toRelativeRuntime);
  29370. const replacementString = typeof replacement === 'string'
  29371. ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
  29372. : `"+${replacement.runtime}+"`;
  29373. s.update(start, end, replacementString);
  29374. }
  29375. }
  29376. if (chunkCSS) {
  29377. if (config.build.cssCodeSplit) {
  29378. if (opts.format === 'es' || opts.format === 'cjs') {
  29379. if (isPureCssChunk) {
  29380. // this is a shared CSS-only chunk that is empty.
  29381. pureCssChunks.add(chunk);
  29382. }
  29383. const isEntry = chunk.isEntry && isPureCssChunk;
  29384. const cssFullAssetName = ensureFileExt(chunk.name, '.css');
  29385. // if facadeModuleId doesn't exist or doesn't have a CSS extension,
  29386. // that means a JS entry file imports a CSS file.
  29387. // in this case, only use the filename for the CSS chunk name like JS chunks.
  29388. const cssAssetName = chunk.isEntry &&
  29389. (!chunk.facadeModuleId || !isCSSRequest(chunk.facadeModuleId))
  29390. ? path$o.basename(cssFullAssetName)
  29391. : cssFullAssetName;
  29392. const originalFilename = getChunkOriginalFileName(chunk, config.root, opts.format);
  29393. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName);
  29394. // wait for previous tasks as well
  29395. chunkCSS = await codeSplitEmitQueue.run(async () => {
  29396. return finalizeCss(chunkCSS, true, config);
  29397. });
  29398. // emit corresponding css file
  29399. const referenceId = this.emitFile({
  29400. name: cssAssetName,
  29401. type: 'asset',
  29402. source: chunkCSS,
  29403. });
  29404. generatedAssets
  29405. .get(config)
  29406. .set(referenceId, { originalName: originalFilename, isEntry });
  29407. chunk.viteMetadata.importedCss.add(this.getFileName(referenceId));
  29408. }
  29409. else if (!config.build.ssr) {
  29410. // legacy build and inline css
  29411. // Entry chunk CSS will be collected into `chunk.viteMetadata.importedCss`
  29412. // and injected later by the `'vite:build-html'` plugin into the `index.html`
  29413. // so it will be duplicated. (https://github.com/vitejs/vite/issues/2062#issuecomment-782388010)
  29414. // But because entry chunk can be imported by dynamic import,
  29415. // we shouldn't remove the inlined CSS. (#10285)
  29416. chunkCSS = await finalizeCss(chunkCSS, true, config);
  29417. let cssString = JSON.stringify(chunkCSS);
  29418. cssString =
  29419. renderAssetUrlInJS(this, config, chunk, opts, cssString)?.toString() || cssString;
  29420. const style = `__vite_style__`;
  29421. const injectCode = `var ${style} = document.createElement('style');` +
  29422. `${style}.textContent = ${cssString};` +
  29423. `document.head.appendChild(${style});`;
  29424. let injectionPoint;
  29425. const wrapIdx = code.indexOf('System.register');
  29426. if (wrapIdx >= 0) {
  29427. const executeFnStart = code.indexOf('execute:', wrapIdx);
  29428. injectionPoint = code.indexOf('{', executeFnStart) + 1;
  29429. }
  29430. else {
  29431. const insertMark = "'use strict';";
  29432. injectionPoint = code.indexOf(insertMark) + insertMark.length;
  29433. }
  29434. s ||= new MagicString(code);
  29435. s.appendRight(injectionPoint, injectCode);
  29436. }
  29437. }
  29438. else {
  29439. // resolve public URL from CSS paths, we need to use absolute paths
  29440. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssBundleName);
  29441. // finalizeCss is called for the aggregated chunk in generateBundle
  29442. chunkCSSMap.set(chunk.fileName, chunkCSS);
  29443. }
  29444. }
  29445. if (s) {
  29446. if (config.build.sourcemap) {
  29447. return {
  29448. code: s.toString(),
  29449. map: s.generateMap({ hires: 'boundary' }),
  29450. };
  29451. }
  29452. else {
  29453. return { code: s.toString() };
  29454. }
  29455. }
  29456. return null;
  29457. },
  29458. augmentChunkHash(chunk) {
  29459. if (chunk.viteMetadata?.importedCss.size) {
  29460. let hash = '';
  29461. for (const id of chunk.viteMetadata.importedCss) {
  29462. hash += id;
  29463. }
  29464. return hash;
  29465. }
  29466. },
  29467. async generateBundle(opts, bundle) {
  29468. // @ts-expect-error asset emits are skipped in legacy bundle
  29469. if (opts.__vite_skip_asset_emit__) {
  29470. return;
  29471. }
  29472. // remove empty css chunks and their imports
  29473. if (pureCssChunks.size) {
  29474. // map each pure css chunk (rendered chunk) to it's corresponding bundle
  29475. // chunk. we check that by `preliminaryFileName` as they have different
  29476. // `filename`s (rendered chunk has the !~{XXX}~ placeholder)
  29477. const prelimaryNameToChunkMap = Object.fromEntries(Object.values(bundle)
  29478. .filter((chunk) => chunk.type === 'chunk')
  29479. .map((chunk) => [chunk.preliminaryFileName, chunk.fileName]));
  29480. const pureCssChunkNames = [...pureCssChunks].map((pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName]);
  29481. const replaceEmptyChunk = getEmptyChunkReplacer(pureCssChunkNames, opts.format);
  29482. for (const file in bundle) {
  29483. const chunk = bundle[file];
  29484. if (chunk.type === 'chunk') {
  29485. let chunkImportsPureCssChunk = false;
  29486. // remove pure css chunk from other chunk's imports,
  29487. // and also register the emitted CSS files under the importer
  29488. // chunks instead.
  29489. chunk.imports = chunk.imports.filter((file) => {
  29490. if (pureCssChunkNames.includes(file)) {
  29491. const { importedCss, importedAssets } = bundle[file].viteMetadata;
  29492. importedCss.forEach((file) => chunk.viteMetadata.importedCss.add(file));
  29493. importedAssets.forEach((file) => chunk.viteMetadata.importedAssets.add(file));
  29494. chunkImportsPureCssChunk = true;
  29495. return false;
  29496. }
  29497. return true;
  29498. });
  29499. if (chunkImportsPureCssChunk) {
  29500. chunk.code = replaceEmptyChunk(chunk.code);
  29501. }
  29502. }
  29503. }
  29504. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  29505. pureCssChunkNames.forEach((fileName) => {
  29506. removedPureCssFiles.set(fileName, bundle[fileName]);
  29507. delete bundle[fileName];
  29508. delete bundle[`${fileName}.map`];
  29509. });
  29510. }
  29511. function extractCss() {
  29512. let css = '';
  29513. const collected = new Set();
  29514. const prelimaryNameToChunkMap = new Map(Object.values(bundle)
  29515. .filter((chunk) => chunk.type === 'chunk')
  29516. .map((chunk) => [chunk.preliminaryFileName, chunk]));
  29517. function collect(fileName) {
  29518. const chunk = bundle[fileName];
  29519. if (!chunk || chunk.type !== 'chunk' || collected.has(chunk))
  29520. return;
  29521. collected.add(chunk);
  29522. chunk.imports.forEach(collect);
  29523. css += chunkCSSMap.get(chunk.preliminaryFileName) ?? '';
  29524. }
  29525. for (const chunkName of chunkCSSMap.keys())
  29526. collect(prelimaryNameToChunkMap.get(chunkName)?.fileName ?? '');
  29527. return css;
  29528. }
  29529. let extractedCss = !hasEmitted && extractCss();
  29530. if (extractedCss) {
  29531. hasEmitted = true;
  29532. extractedCss = await finalizeCss(extractedCss, true, config);
  29533. this.emitFile({
  29534. name: cssBundleName,
  29535. type: 'asset',
  29536. source: extractedCss,
  29537. });
  29538. }
  29539. },
  29540. };
  29541. }
  29542. function cssAnalysisPlugin(config) {
  29543. let server;
  29544. return {
  29545. name: 'vite:css-analysis',
  29546. configureServer(_server) {
  29547. server = _server;
  29548. },
  29549. async transform(_, id, options) {
  29550. if (!isCSSRequest(id) ||
  29551. commonjsProxyRE.test(id) ||
  29552. SPECIAL_QUERY_RE.test(id)) {
  29553. return;
  29554. }
  29555. const ssr = options?.ssr === true;
  29556. const { moduleGraph } = server;
  29557. const thisModule = moduleGraph.getModuleById(id);
  29558. // Handle CSS @import dependency HMR and other added modules via this.addWatchFile.
  29559. // JS-related HMR is handled in the import-analysis plugin.
  29560. if (thisModule) {
  29561. // CSS modules cannot self-accept since it exports values
  29562. const isSelfAccepting = !cssModulesCache.get(config)?.get(id) &&
  29563. !inlineRE$1.test(id) &&
  29564. !htmlProxyRE.test(id);
  29565. // attached by pluginContainer.addWatchFile
  29566. const pluginImports = this._addedImports;
  29567. if (pluginImports) {
  29568. // record deps in the module graph so edits to @import css can trigger
  29569. // main import to hot update
  29570. const depModules = new Set();
  29571. const devBase = config.base;
  29572. for (const file of pluginImports) {
  29573. depModules.add(isCSSRequest(file)
  29574. ? moduleGraph.createFileOnlyEntry(file)
  29575. : await moduleGraph.ensureEntryFromUrl(stripBase(await fileToUrl$1(file, config, this), (config.server?.origin ?? '') + devBase), ssr));
  29576. }
  29577. moduleGraph.updateModuleInfo(thisModule, depModules, null,
  29578. // The root CSS proxy module is self-accepting and should not
  29579. // have an explicit accept list
  29580. new Set(), null, isSelfAccepting, ssr);
  29581. }
  29582. else {
  29583. thisModule.isSelfAccepting = isSelfAccepting;
  29584. }
  29585. }
  29586. },
  29587. };
  29588. }
  29589. /**
  29590. * Create a replacer function that takes code and replaces given pure CSS chunk imports
  29591. * @param pureCssChunkNames The chunks that only contain pure CSS and should be replaced
  29592. * @param outputFormat The module output format to decide whether to replace `import` or `require`
  29593. */
  29594. function getEmptyChunkReplacer(pureCssChunkNames, outputFormat) {
  29595. const emptyChunkFiles = pureCssChunkNames
  29596. .map((file) => path$o.basename(file))
  29597. .join('|')
  29598. .replace(/\./g, '\\.');
  29599. // for cjs, require calls might be chained by minifier using the comma operator.
  29600. // in this case we have to keep one comma if a next require is chained
  29601. // or add a semicolon to terminate the chain.
  29602. const emptyChunkRE = new RegExp(outputFormat === 'es'
  29603. ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];`
  29604. : `(\\b|,\\s*)require\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\)(;|,)`, 'g');
  29605. return (code) => code.replace(emptyChunkRE,
  29606. // remove css import while preserving source map location
  29607. (m) => outputFormat === 'es'
  29608. ? `/* empty css ${''.padEnd(m.length - 15)}*/`
  29609. : `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`);
  29610. }
  29611. function createCSSResolvers(config) {
  29612. let cssResolve;
  29613. let sassResolve;
  29614. let lessResolve;
  29615. return {
  29616. get css() {
  29617. return (cssResolve ||
  29618. (cssResolve = config.createResolver({
  29619. extensions: ['.css'],
  29620. mainFields: ['style'],
  29621. conditions: ['style'],
  29622. tryIndex: false,
  29623. preferRelative: true,
  29624. })));
  29625. },
  29626. get sass() {
  29627. return (sassResolve ||
  29628. (sassResolve = config.createResolver({
  29629. extensions: ['.scss', '.sass', '.css'],
  29630. mainFields: ['sass', 'style'],
  29631. conditions: ['sass', 'style'],
  29632. tryIndex: true,
  29633. tryPrefix: '_',
  29634. preferRelative: true,
  29635. })));
  29636. },
  29637. get less() {
  29638. return (lessResolve ||
  29639. (lessResolve = config.createResolver({
  29640. extensions: ['.less', '.css'],
  29641. mainFields: ['less', 'style'],
  29642. conditions: ['less', 'style'],
  29643. tryIndex: false,
  29644. preferRelative: true,
  29645. })));
  29646. },
  29647. };
  29648. }
  29649. function getCssResolversKeys(resolvers) {
  29650. return Object.keys(resolvers);
  29651. }
  29652. async function compileCSSPreprocessors(id, lang, code, config, workerController) {
  29653. const { preprocessorOptions, devSourcemap } = config.css ?? {};
  29654. const atImportResolvers = getAtImportResolvers(config);
  29655. const preProcessor = workerController[lang];
  29656. let opts = (preprocessorOptions && preprocessorOptions[lang]) || {};
  29657. // support @import from node dependencies by default
  29658. switch (lang) {
  29659. case "scss" /* PreprocessLang.scss */:
  29660. case "sass" /* PreprocessLang.sass */:
  29661. opts = {
  29662. includePaths: ['node_modules'],
  29663. alias: config.resolve.alias,
  29664. ...opts,
  29665. };
  29666. break;
  29667. case "less" /* PreprocessLang.less */:
  29668. case "styl" /* PreprocessLang.styl */:
  29669. case "stylus" /* PreprocessLang.stylus */:
  29670. opts = {
  29671. paths: ['node_modules'],
  29672. alias: config.resolve.alias,
  29673. ...opts,
  29674. };
  29675. }
  29676. // important: set this for relative import resolving
  29677. opts.filename = cleanUrl(id);
  29678. opts.enableSourcemap = devSourcemap ?? false;
  29679. const preprocessResult = await preProcessor(code, config.root, opts, atImportResolvers);
  29680. if (preprocessResult.error) {
  29681. throw preprocessResult.error;
  29682. }
  29683. let deps;
  29684. if (preprocessResult.deps) {
  29685. const normalizedFilename = normalizePath$3(opts.filename);
  29686. // sometimes sass registers the file itself as a dep
  29687. deps = new Set([...preprocessResult.deps].filter((dep) => normalizePath$3(dep) !== normalizedFilename));
  29688. }
  29689. return {
  29690. code: preprocessResult.code,
  29691. map: combineSourcemapsIfExists(opts.filename, preprocessResult.map, preprocessResult.additionalMap),
  29692. deps,
  29693. };
  29694. }
  29695. const configToAtImportResolvers = new WeakMap();
  29696. function getAtImportResolvers(config) {
  29697. let atImportResolvers = configToAtImportResolvers.get(config);
  29698. if (!atImportResolvers) {
  29699. atImportResolvers = createCSSResolvers(config);
  29700. configToAtImportResolvers.set(config, atImportResolvers);
  29701. }
  29702. return atImportResolvers;
  29703. }
  29704. async function compileCSS(id, code, config, workerController, urlReplacer) {
  29705. if (config.css?.transformer === 'lightningcss') {
  29706. return compileLightningCSS(id, code, config, urlReplacer);
  29707. }
  29708. const { modules: modulesOptions, devSourcemap } = config.css || {};
  29709. const isModule = modulesOptions !== false && cssModuleRE.test(id);
  29710. // although at serve time it can work without processing, we do need to
  29711. // crawl them in order to register watch dependencies.
  29712. const needInlineImport = code.includes('@import');
  29713. const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code);
  29714. const lang = id.match(CSS_LANGS_RE)?.[1];
  29715. const postcssConfig = await resolvePostcssConfig(config);
  29716. // 1. plain css that needs no processing
  29717. if (lang === 'css' &&
  29718. !postcssConfig &&
  29719. !isModule &&
  29720. !needInlineImport &&
  29721. !hasUrl) {
  29722. return { code, map: null };
  29723. }
  29724. let modules;
  29725. const deps = new Set();
  29726. // 2. pre-processors: sass etc.
  29727. let preprocessorMap;
  29728. if (isPreProcessor(lang)) {
  29729. const preprocessorResult = await compileCSSPreprocessors(id, lang, code, config, workerController);
  29730. code = preprocessorResult.code;
  29731. preprocessorMap = preprocessorResult.map;
  29732. preprocessorResult.deps?.forEach((dep) => deps.add(dep));
  29733. }
  29734. // 3. postcss
  29735. const atImportResolvers = getAtImportResolvers(config);
  29736. const postcssOptions = (postcssConfig && postcssConfig.options) || {};
  29737. const postcssPlugins = postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : [];
  29738. if (needInlineImport) {
  29739. postcssPlugins.unshift((await importPostcssImport()).default({
  29740. async resolve(id, basedir) {
  29741. const publicFile = checkPublicFile(id, config);
  29742. if (publicFile) {
  29743. return publicFile;
  29744. }
  29745. const resolved = await atImportResolvers.css(id, path$o.join(basedir, '*'));
  29746. if (resolved) {
  29747. return path$o.resolve(resolved);
  29748. }
  29749. // postcss-import falls back to `resolve` dep if this is unresolved,
  29750. // but we've shimmed to remove the `resolve` dep to cut on bundle size.
  29751. // warn here to provide a better error message.
  29752. if (!path$o.isAbsolute(id)) {
  29753. config.logger.error(colors$1.red(`Unable to resolve \`@import "${id}"\` from ${basedir}`));
  29754. }
  29755. return id;
  29756. },
  29757. async load(id) {
  29758. const code = await fs$l.promises.readFile(id, 'utf-8');
  29759. const lang = id.match(CSS_LANGS_RE)?.[1];
  29760. if (isPreProcessor(lang)) {
  29761. const result = await compileCSSPreprocessors(id, lang, code, config, workerController);
  29762. result.deps?.forEach((dep) => deps.add(dep));
  29763. // TODO: support source map
  29764. return result.code;
  29765. }
  29766. return code;
  29767. },
  29768. nameLayer(index) {
  29769. return `vite--anon-layer-${getHash(id)}-${index}`;
  29770. },
  29771. }));
  29772. }
  29773. if (urlReplacer) {
  29774. postcssPlugins.push(UrlRewritePostcssPlugin({
  29775. replacer: urlReplacer,
  29776. logger: config.logger,
  29777. }));
  29778. }
  29779. if (isModule) {
  29780. postcssPlugins.unshift((await importPostcssModules()).default({
  29781. ...modulesOptions,
  29782. localsConvention: modulesOptions?.localsConvention,
  29783. getJSON(cssFileName, _modules, outputFileName) {
  29784. modules = _modules;
  29785. if (modulesOptions && typeof modulesOptions.getJSON === 'function') {
  29786. modulesOptions.getJSON(cssFileName, _modules, outputFileName);
  29787. }
  29788. },
  29789. async resolve(id, importer) {
  29790. for (const key of getCssResolversKeys(atImportResolvers)) {
  29791. const resolved = await atImportResolvers[key](id, importer);
  29792. if (resolved) {
  29793. return path$o.resolve(resolved);
  29794. }
  29795. }
  29796. return id;
  29797. },
  29798. }));
  29799. }
  29800. if (!postcssPlugins.length) {
  29801. return {
  29802. code,
  29803. map: preprocessorMap,
  29804. deps,
  29805. };
  29806. }
  29807. let postcssResult;
  29808. try {
  29809. const source = removeDirectQuery(id);
  29810. const postcss = await importPostcss();
  29811. // postcss is an unbundled dep and should be lazy imported
  29812. postcssResult = await postcss.default(postcssPlugins).process(code, {
  29813. ...postcssOptions,
  29814. parser: lang === 'sss' ? loadSss(config.root) : postcssOptions.parser,
  29815. to: source,
  29816. from: source,
  29817. ...(devSourcemap
  29818. ? {
  29819. map: {
  29820. inline: false,
  29821. annotation: false,
  29822. // postcss may return virtual files
  29823. // we cannot obtain content of them, so this needs to be enabled
  29824. sourcesContent: true,
  29825. // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
  29826. // prev: preprocessorMap,
  29827. },
  29828. }
  29829. : {}),
  29830. });
  29831. // record CSS dependencies from @imports
  29832. for (const message of postcssResult.messages) {
  29833. if (message.type === 'dependency') {
  29834. deps.add(normalizePath$3(message.file));
  29835. }
  29836. else if (message.type === 'dir-dependency') {
  29837. // https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
  29838. const { dir, glob: globPattern = '**' } = message;
  29839. const pattern = glob.escapePath(normalizePath$3(path$o.resolve(path$o.dirname(id), dir))) +
  29840. `/` +
  29841. globPattern;
  29842. const files = glob.sync(pattern, {
  29843. ignore: ['**/node_modules/**'],
  29844. });
  29845. for (let i = 0; i < files.length; i++) {
  29846. deps.add(files[i]);
  29847. }
  29848. }
  29849. else if (message.type === 'warning') {
  29850. const warning = message;
  29851. let msg = `[vite:css] ${warning.text}`;
  29852. msg += `\n${generateCodeFrame(code, {
  29853. line: warning.line,
  29854. column: warning.column - 1, // 1-based
  29855. }, warning.endLine !== undefined && warning.endColumn !== undefined
  29856. ? {
  29857. line: warning.endLine,
  29858. column: warning.endColumn - 1, // 1-based
  29859. }
  29860. : undefined)}`;
  29861. config.logger.warn(colors$1.yellow(msg));
  29862. }
  29863. }
  29864. }
  29865. catch (e) {
  29866. e.message = `[postcss] ${e.message}`;
  29867. e.code = code;
  29868. e.loc = {
  29869. file: e.file,
  29870. line: e.line,
  29871. column: e.column - 1, // 1-based
  29872. };
  29873. throw e;
  29874. }
  29875. if (!devSourcemap) {
  29876. return {
  29877. ast: postcssResult,
  29878. code: postcssResult.css,
  29879. map: { mappings: '' },
  29880. modules,
  29881. deps,
  29882. };
  29883. }
  29884. const rawPostcssMap = postcssResult.map.toJSON();
  29885. const postcssMap = await formatPostcssSourceMap(
  29886. // version property of rawPostcssMap is declared as string
  29887. // but actually it is a number
  29888. rawPostcssMap, cleanUrl(id));
  29889. return {
  29890. ast: postcssResult,
  29891. code: postcssResult.css,
  29892. map: combineSourcemapsIfExists(cleanUrl(id), postcssMap, preprocessorMap),
  29893. modules,
  29894. deps,
  29895. };
  29896. }
  29897. function createCachedImport(imp) {
  29898. let cached;
  29899. return () => {
  29900. if (!cached) {
  29901. cached = imp().then((module) => {
  29902. cached = module;
  29903. return module;
  29904. });
  29905. }
  29906. return cached;
  29907. };
  29908. }
  29909. const importPostcssImport = createCachedImport(() => import('./dep-irZ_Evzk.js').then(function (n) { return n.i; }));
  29910. const importPostcssModules = createCachedImport(() => import('./dep-DI-l0F6S.js').then(function (n) { return n.i; }));
  29911. const importPostcss = createCachedImport(() => import('postcss'));
  29912. const preprocessorWorkerControllerCache = new WeakMap();
  29913. let alwaysFakeWorkerWorkerControllerCache;
  29914. /**
  29915. * @experimental
  29916. */
  29917. async function preprocessCSS(code, filename, config) {
  29918. let workerController = preprocessorWorkerControllerCache.get(config);
  29919. if (!workerController) {
  29920. // if workerController doesn't exist, create a workerController that always uses fake workers
  29921. // because fake workers doesn't require calling `.close` unlike real workers
  29922. alwaysFakeWorkerWorkerControllerCache ||=
  29923. createPreprocessorWorkerController(0);
  29924. workerController = alwaysFakeWorkerWorkerControllerCache;
  29925. }
  29926. return await compileCSS(filename, code, config, workerController);
  29927. }
  29928. async function formatPostcssSourceMap(rawMap, file) {
  29929. const inputFileDir = path$o.dirname(file);
  29930. const sources = rawMap.sources.map((source) => {
  29931. const cleanSource = cleanUrl(decodeURIComponent(source));
  29932. // postcss virtual files
  29933. if (cleanSource[0] === '<' && cleanSource[cleanSource.length - 1] === '>') {
  29934. return `\0${cleanSource}`;
  29935. }
  29936. return normalizePath$3(path$o.resolve(inputFileDir, cleanSource));
  29937. });
  29938. return {
  29939. file,
  29940. mappings: rawMap.mappings,
  29941. names: rawMap.names,
  29942. sources,
  29943. sourcesContent: rawMap.sourcesContent,
  29944. version: rawMap.version,
  29945. };
  29946. }
  29947. function combineSourcemapsIfExists(filename, map1, map2) {
  29948. return map1 && map2
  29949. ? combineSourcemaps(filename, [
  29950. // type of version property of ExistingRawSourceMap is number
  29951. // but it is always 3
  29952. map1,
  29953. map2,
  29954. ])
  29955. : map1;
  29956. }
  29957. async function finalizeCss(css, minify, config) {
  29958. // hoist external @imports and @charset to the top of the CSS chunk per spec (#1845 and #6333)
  29959. if (css.includes('@import') || css.includes('@charset')) {
  29960. css = await hoistAtRules(css);
  29961. }
  29962. if (minify && config.build.cssMinify) {
  29963. css = await minifyCSS(css, config, false);
  29964. }
  29965. return css;
  29966. }
  29967. async function resolvePostcssConfig(config) {
  29968. let result = postcssConfigCache.get(config);
  29969. if (result !== undefined) {
  29970. return await result;
  29971. }
  29972. // inline postcss config via vite config
  29973. const inlineOptions = config.css?.postcss;
  29974. if (isObject$1(inlineOptions)) {
  29975. const options = { ...inlineOptions };
  29976. delete options.plugins;
  29977. result = {
  29978. options,
  29979. plugins: inlineOptions.plugins || [],
  29980. };
  29981. }
  29982. else {
  29983. const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
  29984. result = postcssrc({}, searchPath).catch((e) => {
  29985. if (!e.message.includes('No PostCSS Config found')) {
  29986. if (e instanceof Error) {
  29987. const { name, message, stack } = e;
  29988. e.name = 'Failed to load PostCSS config';
  29989. e.message = `Failed to load PostCSS config (searchPath: ${searchPath}): [${name}] ${message}\n${stack}`;
  29990. e.stack = ''; // add stack to message to retain stack
  29991. throw e;
  29992. }
  29993. else {
  29994. throw new Error(`Failed to load PostCSS config: ${e}`);
  29995. }
  29996. }
  29997. return null;
  29998. });
  29999. // replace cached promise to result object when finished
  30000. result.then((resolved) => {
  30001. postcssConfigCache.set(config, resolved);
  30002. });
  30003. }
  30004. postcssConfigCache.set(config, result);
  30005. return result;
  30006. }
  30007. // https://drafts.csswg.org/css-syntax-3/#identifier-code-point
  30008. const cssUrlRE = /(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  30009. const cssDataUriRE = /(?<=^|[^\w\-\u0080-\uffff])data-uri\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  30010. const importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/;
  30011. // Assuming a function name won't be longer than 256 chars
  30012. // eslint-disable-next-line regexp/no-unused-capturing-group -- doesn't detect asyncReplace usage
  30013. const cssImageSetRE = /(?<=image-set\()((?:[\w\-]{1,256}\([^)]*\)|[^)])*)(?=\))/;
  30014. const UrlRewritePostcssPlugin = (opts) => {
  30015. if (!opts) {
  30016. throw new Error('base or replace is required');
  30017. }
  30018. return {
  30019. postcssPlugin: 'vite-url-rewrite',
  30020. Once(root) {
  30021. const promises = [];
  30022. root.walkDecls((declaration) => {
  30023. const importer = declaration.source?.input.file;
  30024. if (!importer) {
  30025. opts.logger.warnOnce('\nA PostCSS plugin did not pass the `from` option to `postcss.parse`. ' +
  30026. 'This may cause imported assets to be incorrectly transformed. ' +
  30027. "If you've recently added a PostCSS plugin that raised this warning, " +
  30028. 'please contact the package author to fix the issue.');
  30029. }
  30030. const isCssUrl = cssUrlRE.test(declaration.value);
  30031. const isCssImageSet = cssImageSetRE.test(declaration.value);
  30032. if (isCssUrl || isCssImageSet) {
  30033. const replacerForDeclaration = (rawUrl) => {
  30034. return opts.replacer(rawUrl, importer);
  30035. };
  30036. const rewriterToUse = isCssImageSet
  30037. ? rewriteCssImageSet
  30038. : rewriteCssUrls;
  30039. promises.push(rewriterToUse(declaration.value, replacerForDeclaration).then((url) => {
  30040. declaration.value = url;
  30041. }));
  30042. }
  30043. });
  30044. if (promises.length) {
  30045. return Promise.all(promises);
  30046. }
  30047. },
  30048. };
  30049. };
  30050. UrlRewritePostcssPlugin.postcss = true;
  30051. function rewriteCssUrls(css, replacer) {
  30052. return asyncReplace(css, cssUrlRE, async (match) => {
  30053. const [matched, rawUrl] = match;
  30054. return await doUrlReplace(rawUrl.trim(), matched, replacer);
  30055. });
  30056. }
  30057. function rewriteCssDataUris(css, replacer) {
  30058. return asyncReplace(css, cssDataUriRE, async (match) => {
  30059. const [matched, rawUrl] = match;
  30060. return await doUrlReplace(rawUrl.trim(), matched, replacer, 'data-uri');
  30061. });
  30062. }
  30063. function rewriteImportCss(css, replacer) {
  30064. return asyncReplace(css, importCssRE, async (match) => {
  30065. const [matched, rawUrl] = match;
  30066. return await doImportCSSReplace(rawUrl, matched, replacer);
  30067. });
  30068. }
  30069. // TODO: image and cross-fade could contain a "url" that needs to be processed
  30070. // https://drafts.csswg.org/css-images-4/#image-notation
  30071. // https://drafts.csswg.org/css-images-4/#cross-fade-function
  30072. const cssNotProcessedRE = /(?:gradient|element|cross-fade|image)\(/;
  30073. async function rewriteCssImageSet(css, replacer) {
  30074. return await asyncReplace(css, cssImageSetRE, async (match) => {
  30075. const [, rawUrl] = match;
  30076. const url = await processSrcSet(rawUrl, async ({ url }) => {
  30077. // the url maybe url(...)
  30078. if (cssUrlRE.test(url)) {
  30079. return await rewriteCssUrls(url, replacer);
  30080. }
  30081. if (!cssNotProcessedRE.test(url)) {
  30082. return await doUrlReplace(url, url, replacer);
  30083. }
  30084. return url;
  30085. });
  30086. return url;
  30087. });
  30088. }
  30089. function skipUrlReplacer(rawUrl) {
  30090. return (isExternalUrl(rawUrl) ||
  30091. isDataUrl(rawUrl) ||
  30092. rawUrl[0] === '#' ||
  30093. functionCallRE.test(rawUrl));
  30094. }
  30095. async function doUrlReplace(rawUrl, matched, replacer, funcName = 'url') {
  30096. let wrap = '';
  30097. const first = rawUrl[0];
  30098. if (first === `"` || first === `'`) {
  30099. wrap = first;
  30100. rawUrl = rawUrl.slice(1, -1);
  30101. }
  30102. if (skipUrlReplacer(rawUrl)) {
  30103. return matched;
  30104. }
  30105. let newUrl = await replacer(rawUrl);
  30106. // The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement
  30107. if (wrap === '' && newUrl !== encodeURI(newUrl)) {
  30108. wrap = '"';
  30109. }
  30110. // If wrapping in single quotes and newUrl also contains single quotes, switch to double quotes.
  30111. // Give preference to double quotes since SVG inlining converts double quotes to single quotes.
  30112. if (wrap === "'" && newUrl.includes("'")) {
  30113. wrap = '"';
  30114. }
  30115. // Escape double quotes if they exist (they also tend to be rarer than single quotes)
  30116. if (wrap === '"' && newUrl.includes('"')) {
  30117. newUrl = newUrl.replace(nonEscapedDoubleQuoteRe, '\\"');
  30118. }
  30119. return `${funcName}(${wrap}${newUrl}${wrap})`;
  30120. }
  30121. async function doImportCSSReplace(rawUrl, matched, replacer) {
  30122. let wrap = '';
  30123. const first = rawUrl[0];
  30124. if (first === `"` || first === `'`) {
  30125. wrap = first;
  30126. rawUrl = rawUrl.slice(1, -1);
  30127. }
  30128. if (isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === '#') {
  30129. return matched;
  30130. }
  30131. return `@import ${wrap}${await replacer(rawUrl)}${wrap}`;
  30132. }
  30133. async function minifyCSS(css, config, inlined) {
  30134. // We want inlined CSS to not end with a linebreak, while ensuring that
  30135. // regular CSS assets do end with a linebreak.
  30136. // See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198
  30137. if (config.build.cssMinify === 'lightningcss') {
  30138. const { code, warnings } = (await importLightningCSS()).transform({
  30139. ...config.css?.lightningcss,
  30140. targets: convertTargets(config.build.cssTarget),
  30141. cssModules: undefined,
  30142. filename: cssBundleName,
  30143. code: Buffer.from(css),
  30144. minify: true,
  30145. });
  30146. if (warnings.length) {
  30147. config.logger.warn(colors$1.yellow(`warnings when minifying css:\n${warnings
  30148. .map((w) => w.message)
  30149. .join('\n')}`));
  30150. }
  30151. // LightningCSS output does not return a linebreak at the end
  30152. return code.toString() + (inlined ? '' : '\n');
  30153. }
  30154. try {
  30155. const { code, warnings } = await transform$1(css, {
  30156. loader: 'css',
  30157. target: config.build.cssTarget || undefined,
  30158. ...resolveMinifyCssEsbuildOptions(config.esbuild || {}),
  30159. });
  30160. if (warnings.length) {
  30161. const msgs = await formatMessages(warnings, { kind: 'warning' });
  30162. config.logger.warn(colors$1.yellow(`warnings when minifying css:\n${msgs.join('\n')}`));
  30163. }
  30164. // esbuild output does return a linebreak at the end
  30165. return inlined ? code.trimEnd() : code;
  30166. }
  30167. catch (e) {
  30168. if (e.errors) {
  30169. e.message = '[esbuild css minify] ' + e.message;
  30170. const msgs = await formatMessages(e.errors, { kind: 'error' });
  30171. e.frame = '\n' + msgs.join('\n');
  30172. e.loc = e.errors[0].location;
  30173. }
  30174. throw e;
  30175. }
  30176. }
  30177. function resolveMinifyCssEsbuildOptions(options) {
  30178. const base = {
  30179. charset: options.charset ?? 'utf8',
  30180. logLevel: options.logLevel,
  30181. logLimit: options.logLimit,
  30182. logOverride: options.logOverride,
  30183. legalComments: options.legalComments,
  30184. };
  30185. if (options.minifyIdentifiers != null ||
  30186. options.minifySyntax != null ||
  30187. options.minifyWhitespace != null) {
  30188. return {
  30189. ...base,
  30190. minifyIdentifiers: options.minifyIdentifiers ?? true,
  30191. minifySyntax: options.minifySyntax ?? true,
  30192. minifyWhitespace: options.minifyWhitespace ?? true,
  30193. };
  30194. }
  30195. else {
  30196. return { ...base, minify: true };
  30197. }
  30198. }
  30199. const atImportRE = /@import(?:\s*(?:url\([^)]*\)|"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  30200. const atCharsetRE = /@charset(?:\s*(?:"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  30201. async function hoistAtRules(css) {
  30202. const s = new MagicString(css);
  30203. const cleanCss = emptyCssComments(css);
  30204. let match;
  30205. // #1845
  30206. // CSS @import can only appear at top of the file. We need to hoist all @import
  30207. // to top when multiple files are concatenated.
  30208. // match until semicolon that's not in quotes
  30209. atImportRE.lastIndex = 0;
  30210. while ((match = atImportRE.exec(cleanCss))) {
  30211. s.remove(match.index, match.index + match[0].length);
  30212. // Use `appendLeft` instead of `prepend` to preserve original @import order
  30213. s.appendLeft(0, match[0]);
  30214. }
  30215. // #6333
  30216. // CSS @charset must be the top-first in the file, hoist the first to top
  30217. atCharsetRE.lastIndex = 0;
  30218. let foundCharset = false;
  30219. while ((match = atCharsetRE.exec(cleanCss))) {
  30220. s.remove(match.index, match.index + match[0].length);
  30221. if (!foundCharset) {
  30222. s.prepend(match[0]);
  30223. foundCharset = true;
  30224. }
  30225. }
  30226. return s.toString();
  30227. }
  30228. const loadedPreprocessorPath = {};
  30229. function loadPreprocessorPath(lang, root) {
  30230. const cached = loadedPreprocessorPath[lang];
  30231. if (cached) {
  30232. return cached;
  30233. }
  30234. try {
  30235. const resolved = requireResolveFromRootWithFallback(root, lang);
  30236. return (loadedPreprocessorPath[lang] = resolved);
  30237. }
  30238. catch (e) {
  30239. if (e.code === 'MODULE_NOT_FOUND') {
  30240. const installCommand = getPackageManagerCommand('install');
  30241. throw new Error(`Preprocessor dependency "${lang}" not found. Did you install it? Try \`${installCommand} -D ${lang}\`.`);
  30242. }
  30243. else {
  30244. const message = new Error(`Preprocessor dependency "${lang}" failed to load:\n${e.message}`);
  30245. message.stack = e.stack + '\n' + message.stack;
  30246. throw message;
  30247. }
  30248. }
  30249. }
  30250. let cachedSss;
  30251. function loadSss(root) {
  30252. if (cachedSss)
  30253. return cachedSss;
  30254. const sssPath = loadPreprocessorPath("sugarss" /* PostCssDialectLang.sss */, root);
  30255. cachedSss = createRequire$1(import.meta.url)(sssPath);
  30256. return cachedSss;
  30257. }
  30258. // in unix, scss might append `location.href` in environments that shim `location`
  30259. // see https://github.com/sass/dart-sass/issues/710
  30260. function cleanScssBugUrl(url) {
  30261. if (
  30262. // check bug via `window` and `location` global
  30263. typeof window !== 'undefined' &&
  30264. typeof location !== 'undefined' &&
  30265. typeof location?.href === 'string') {
  30266. const prefix = location.href.replace(/\/$/, '');
  30267. return url.replace(prefix, '');
  30268. }
  30269. else {
  30270. return url;
  30271. }
  30272. }
  30273. function fixScssBugImportValue(data) {
  30274. // the scss bug doesn't load files properly so we have to load it ourselves
  30275. // to prevent internal error when it loads itself
  30276. if (
  30277. // check bug via `window` and `location` global
  30278. typeof window !== 'undefined' &&
  30279. typeof location !== 'undefined' &&
  30280. data &&
  30281. 'file' in data &&
  30282. (!('contents' in data) || data.contents == null)) {
  30283. // @ts-expect-error we need to preserve file property for HMR
  30284. data.contents = fs$l.readFileSync(data.file, 'utf-8');
  30285. }
  30286. return data;
  30287. }
  30288. // .scss/.sass processor
  30289. const makeScssWorker = (resolvers, alias, maxWorkers) => {
  30290. const internalImporter = async (url, importer, filename) => {
  30291. importer = cleanScssBugUrl(importer);
  30292. const resolved = await resolvers.sass(url, importer);
  30293. if (resolved) {
  30294. try {
  30295. const data = await rebaseUrls(resolved, filename, alias, '$', resolvers.sass);
  30296. return fixScssBugImportValue(data);
  30297. }
  30298. catch (data) {
  30299. return data;
  30300. }
  30301. }
  30302. else {
  30303. return null;
  30304. }
  30305. };
  30306. const worker = new WorkerWithFallback(() => async (sassPath, data,
  30307. // additionalData can a function that is not cloneable but it won't be used
  30308. options) => {
  30309. // eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
  30310. const sass = require(sassPath);
  30311. // eslint-disable-next-line no-restricted-globals
  30312. const path = require('node:path');
  30313. // NOTE: `sass` always runs it's own importer first, and only falls back to
  30314. // the `importer` option when it can't resolve a path
  30315. const _internalImporter = (url, importer, done) => {
  30316. internalImporter(url, importer, options.filename).then((data) => done?.(data));
  30317. };
  30318. const importer = [_internalImporter];
  30319. if (options.importer) {
  30320. Array.isArray(options.importer)
  30321. ? importer.unshift(...options.importer)
  30322. : importer.unshift(options.importer);
  30323. }
  30324. const finalOptions = {
  30325. ...options,
  30326. data,
  30327. file: options.filename,
  30328. outFile: options.filename,
  30329. importer,
  30330. ...(options.enableSourcemap
  30331. ? {
  30332. sourceMap: true,
  30333. omitSourceMapUrl: true,
  30334. sourceMapRoot: path.dirname(options.filename),
  30335. }
  30336. : {}),
  30337. };
  30338. return new Promise((resolve, reject) => {
  30339. sass.render(finalOptions, (err, res) => {
  30340. if (err) {
  30341. reject(err);
  30342. }
  30343. else {
  30344. resolve({
  30345. css: res.css.toString(),
  30346. map: res.map?.toString(),
  30347. stats: res.stats,
  30348. });
  30349. }
  30350. });
  30351. });
  30352. }, {
  30353. parentFunctions: { internalImporter },
  30354. shouldUseFake(_sassPath, _data, options) {
  30355. // functions and importer is a function and is not serializable
  30356. // in that case, fallback to running in main thread
  30357. return !!((options.functions && Object.keys(options.functions).length > 0) ||
  30358. (options.importer &&
  30359. (!Array.isArray(options.importer) || options.importer.length > 0)));
  30360. },
  30361. max: maxWorkers,
  30362. });
  30363. return worker;
  30364. };
  30365. const scssProcessor = (maxWorkers) => {
  30366. const workerMap = new Map();
  30367. return {
  30368. close() {
  30369. for (const worker of workerMap.values()) {
  30370. worker.stop();
  30371. }
  30372. },
  30373. async process(source, root, options, resolvers) {
  30374. const sassPath = loadPreprocessorPath("sass" /* PreprocessLang.sass */, root);
  30375. if (!workerMap.has(options.alias)) {
  30376. workerMap.set(options.alias, makeScssWorker(resolvers, options.alias, maxWorkers));
  30377. }
  30378. const worker = workerMap.get(options.alias);
  30379. const { content: data, map: additionalMap } = await getSource(source, options.filename, options.additionalData, options.enableSourcemap);
  30380. const optionsWithoutAdditionalData = {
  30381. ...options,
  30382. additionalData: undefined,
  30383. };
  30384. try {
  30385. const result = await worker.run(sassPath, data, optionsWithoutAdditionalData);
  30386. const deps = result.stats.includedFiles.map((f) => cleanScssBugUrl(f));
  30387. const map = result.map
  30388. ? JSON.parse(result.map.toString())
  30389. : undefined;
  30390. return {
  30391. code: result.css.toString(),
  30392. map,
  30393. additionalMap,
  30394. deps,
  30395. };
  30396. }
  30397. catch (e) {
  30398. // normalize SASS error
  30399. e.message = `[sass] ${e.message}`;
  30400. e.id = e.file;
  30401. e.frame = e.formatted;
  30402. return { code: '', error: e, deps: [] };
  30403. }
  30404. },
  30405. };
  30406. };
  30407. /**
  30408. * relative url() inside \@imported sass and less files must be rebased to use
  30409. * root file as base.
  30410. */
  30411. async function rebaseUrls(file, rootFile, alias, variablePrefix, resolver) {
  30412. file = path$o.resolve(file); // ensure os-specific flashes
  30413. // in the same dir, no need to rebase
  30414. const fileDir = path$o.dirname(file);
  30415. const rootDir = path$o.dirname(rootFile);
  30416. if (fileDir === rootDir) {
  30417. return { file };
  30418. }
  30419. const content = await fsp.readFile(file, 'utf-8');
  30420. // no url()
  30421. const hasUrls = cssUrlRE.test(content);
  30422. // data-uri() calls
  30423. const hasDataUris = cssDataUriRE.test(content);
  30424. // no @import xxx.css
  30425. const hasImportCss = importCssRE.test(content);
  30426. if (!hasUrls && !hasDataUris && !hasImportCss) {
  30427. return { file };
  30428. }
  30429. let rebased;
  30430. const rebaseFn = async (url) => {
  30431. if (url[0] === '/')
  30432. return url;
  30433. // ignore url's starting with variable
  30434. if (url.startsWith(variablePrefix))
  30435. return url;
  30436. // match alias, no need to rewrite
  30437. for (const { find } of alias) {
  30438. const matches = typeof find === 'string' ? url.startsWith(find) : find.test(url);
  30439. if (matches) {
  30440. return url;
  30441. }
  30442. }
  30443. const absolute = (await resolver(url, file)) || path$o.resolve(fileDir, url);
  30444. const relative = path$o.relative(rootDir, absolute);
  30445. return normalizePath$3(relative);
  30446. };
  30447. // fix css imports in less such as `@import "foo.css"`
  30448. if (hasImportCss) {
  30449. rebased = await rewriteImportCss(content, rebaseFn);
  30450. }
  30451. if (hasUrls) {
  30452. rebased = await rewriteCssUrls(rebased || content, rebaseFn);
  30453. }
  30454. if (hasDataUris) {
  30455. rebased = await rewriteCssDataUris(rebased || content, rebaseFn);
  30456. }
  30457. return {
  30458. file,
  30459. contents: rebased,
  30460. };
  30461. }
  30462. // .less
  30463. const makeLessWorker = (resolvers, alias, maxWorkers) => {
  30464. const viteLessResolve = async (filename, dir, rootFile) => {
  30465. const resolved = await resolvers.less(filename, path$o.join(dir, '*'));
  30466. if (!resolved)
  30467. return undefined;
  30468. const result = await rebaseUrls(resolved, rootFile, alias, '@', resolvers.less);
  30469. if (result) {
  30470. return {
  30471. resolved,
  30472. contents: 'contents' in result ? result.contents : undefined,
  30473. };
  30474. }
  30475. return result;
  30476. };
  30477. const worker = new WorkerWithFallback(() => {
  30478. // eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
  30479. const fsp = require('node:fs/promises');
  30480. // eslint-disable-next-line no-restricted-globals
  30481. const path = require('node:path');
  30482. let ViteLessManager;
  30483. const createViteLessPlugin = (less, rootFile) => {
  30484. const { FileManager } = less;
  30485. ViteLessManager ??= class ViteManager extends FileManager {
  30486. rootFile;
  30487. constructor(rootFile) {
  30488. super();
  30489. this.rootFile = rootFile;
  30490. }
  30491. supports(filename) {
  30492. return !/^(?:https?:)?\/\//.test(filename);
  30493. }
  30494. supportsSync() {
  30495. return false;
  30496. }
  30497. async loadFile(filename, dir, opts, env) {
  30498. const result = await viteLessResolve(filename, dir, this.rootFile);
  30499. if (result) {
  30500. return {
  30501. filename: path.resolve(result.resolved),
  30502. contents: result.contents ??
  30503. (await fsp.readFile(result.resolved, 'utf-8')),
  30504. };
  30505. }
  30506. else {
  30507. return super.loadFile(filename, dir, opts, env);
  30508. }
  30509. }
  30510. };
  30511. return {
  30512. install(_, pluginManager) {
  30513. pluginManager.addFileManager(new ViteLessManager(rootFile));
  30514. },
  30515. minVersion: [3, 0, 0],
  30516. };
  30517. };
  30518. return async (lessPath, content,
  30519. // additionalData can a function that is not cloneable but it won't be used
  30520. options) => {
  30521. // eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
  30522. const nodeLess = require(lessPath);
  30523. const viteResolverPlugin = createViteLessPlugin(nodeLess, options.filename);
  30524. const result = await nodeLess.render(content, {
  30525. ...options,
  30526. plugins: [viteResolverPlugin, ...(options.plugins || [])],
  30527. ...(options.enableSourcemap
  30528. ? {
  30529. sourceMap: {
  30530. outputSourceFiles: true,
  30531. sourceMapFileInline: false,
  30532. },
  30533. }
  30534. : {}),
  30535. });
  30536. return result;
  30537. };
  30538. }, {
  30539. parentFunctions: { viteLessResolve },
  30540. shouldUseFake(_lessPath, _content, options) {
  30541. // plugins are a function and is not serializable
  30542. // in that case, fallback to running in main thread
  30543. return options.plugins?.length > 0;
  30544. },
  30545. max: maxWorkers,
  30546. });
  30547. return worker;
  30548. };
  30549. const lessProcessor = (maxWorkers) => {
  30550. const workerMap = new Map();
  30551. return {
  30552. close() {
  30553. for (const worker of workerMap.values()) {
  30554. worker.stop();
  30555. }
  30556. },
  30557. async process(source, root, options, resolvers) {
  30558. const lessPath = loadPreprocessorPath("less" /* PreprocessLang.less */, root);
  30559. if (!workerMap.has(options.alias)) {
  30560. workerMap.set(options.alias, makeLessWorker(resolvers, options.alias, maxWorkers));
  30561. }
  30562. const worker = workerMap.get(options.alias);
  30563. const { content, map: additionalMap } = await getSource(source, options.filename, options.additionalData, options.enableSourcemap);
  30564. let result;
  30565. const optionsWithoutAdditionalData = {
  30566. ...options,
  30567. additionalData: undefined,
  30568. };
  30569. try {
  30570. result = await worker.run(lessPath, content, optionsWithoutAdditionalData);
  30571. }
  30572. catch (e) {
  30573. const error = e;
  30574. // normalize error info
  30575. const normalizedError = new Error(`[less] ${error.message || error.type}`);
  30576. normalizedError.loc = {
  30577. file: error.filename || options.filename,
  30578. line: error.line,
  30579. column: error.column,
  30580. };
  30581. return { code: '', error: normalizedError, deps: [] };
  30582. }
  30583. const map = result.map && JSON.parse(result.map);
  30584. if (map) {
  30585. delete map.sourcesContent;
  30586. }
  30587. return {
  30588. code: result.css.toString(),
  30589. map,
  30590. additionalMap,
  30591. deps: result.imports,
  30592. };
  30593. },
  30594. };
  30595. };
  30596. // .styl
  30597. const makeStylWorker = (maxWorkers) => {
  30598. const worker = new WorkerWithFallback(() => {
  30599. return async (stylusPath, content, root,
  30600. // additionalData can a function that is not cloneable but it won't be used
  30601. options) => {
  30602. // eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
  30603. const nodeStylus = require(stylusPath);
  30604. const ref = nodeStylus(content, options);
  30605. if (options.define) {
  30606. for (const key in options.define) {
  30607. ref.define(key, options.define[key]);
  30608. }
  30609. }
  30610. if (options.enableSourcemap) {
  30611. ref.set('sourcemap', {
  30612. comment: false,
  30613. inline: false,
  30614. basePath: root,
  30615. });
  30616. }
  30617. return {
  30618. code: ref.render(),
  30619. // @ts-expect-error sourcemap exists
  30620. map: ref.sourcemap,
  30621. deps: ref.deps(),
  30622. };
  30623. };
  30624. }, {
  30625. shouldUseFake(_stylusPath, _content, _root, options) {
  30626. // define can include functions and those are not serializable
  30627. // in that case, fallback to running in main thread
  30628. return !!(options.define &&
  30629. Object.values(options.define).some((d) => typeof d === 'function'));
  30630. },
  30631. max: maxWorkers,
  30632. });
  30633. return worker;
  30634. };
  30635. const stylProcessor = (maxWorkers) => {
  30636. const workerMap = new Map();
  30637. return {
  30638. close() {
  30639. for (const worker of workerMap.values()) {
  30640. worker.stop();
  30641. }
  30642. },
  30643. async process(source, root, options, resolvers) {
  30644. const stylusPath = loadPreprocessorPath("stylus" /* PreprocessLang.stylus */, root);
  30645. if (!workerMap.has(options.alias)) {
  30646. workerMap.set(options.alias, makeStylWorker(maxWorkers));
  30647. }
  30648. const worker = workerMap.get(options.alias);
  30649. // Get source with preprocessor options.additionalData. Make sure a new line separator
  30650. // is added to avoid any render error, as added stylus content may not have semi-colon separators
  30651. const { content, map: additionalMap } = await getSource(source, options.filename, options.additionalData, options.enableSourcemap, '\n');
  30652. // Get preprocessor options.imports dependencies as stylus
  30653. // does not return them with its builtin `.deps()` method
  30654. const importsDeps = (options.imports ?? []).map((dep) => path$o.resolve(dep));
  30655. const optionsWithoutAdditionalData = {
  30656. ...options,
  30657. additionalData: undefined,
  30658. };
  30659. try {
  30660. const { code, map, deps } = await worker.run(stylusPath, content, root, optionsWithoutAdditionalData);
  30661. return {
  30662. code,
  30663. map: formatStylusSourceMap(map, root),
  30664. additionalMap,
  30665. // Concat imports deps with computed deps
  30666. deps: [...deps, ...importsDeps],
  30667. };
  30668. }
  30669. catch (e) {
  30670. const wrapped = new Error(`[stylus] ${e.message}`);
  30671. wrapped.name = e.name;
  30672. wrapped.stack = e.stack;
  30673. return { code: '', error: wrapped, deps: [] };
  30674. }
  30675. },
  30676. };
  30677. };
  30678. function formatStylusSourceMap(mapBefore, root) {
  30679. if (!mapBefore)
  30680. return undefined;
  30681. const map = { ...mapBefore };
  30682. const resolveFromRoot = (p) => normalizePath$3(path$o.resolve(root, p));
  30683. if (map.file) {
  30684. map.file = resolveFromRoot(map.file);
  30685. }
  30686. map.sources = map.sources.map(resolveFromRoot);
  30687. return map;
  30688. }
  30689. async function getSource(source, filename, additionalData, enableSourcemap, sep = '') {
  30690. if (!additionalData)
  30691. return { content: source };
  30692. if (typeof additionalData === 'function') {
  30693. const newContent = await additionalData(source, filename);
  30694. if (typeof newContent === 'string') {
  30695. return { content: newContent };
  30696. }
  30697. return newContent;
  30698. }
  30699. if (!enableSourcemap) {
  30700. return { content: additionalData + sep + source };
  30701. }
  30702. const ms = new MagicString(source);
  30703. ms.appendLeft(0, sep);
  30704. ms.appendLeft(0, additionalData);
  30705. const map = ms.generateMap({ hires: 'boundary' });
  30706. map.file = filename;
  30707. map.sources = [filename];
  30708. return {
  30709. content: ms.toString(),
  30710. map,
  30711. };
  30712. }
  30713. const createPreprocessorWorkerController = (maxWorkers) => {
  30714. const scss = scssProcessor(maxWorkers);
  30715. const less = lessProcessor(maxWorkers);
  30716. const styl = stylProcessor(maxWorkers);
  30717. const sassProcess = (source, root, options, resolvers) => {
  30718. return scss.process(source, root, { ...options, indentedSyntax: true }, resolvers);
  30719. };
  30720. const close = () => {
  30721. less.close();
  30722. scss.close();
  30723. styl.close();
  30724. };
  30725. return {
  30726. ["less" /* PreprocessLang.less */]: less.process,
  30727. ["scss" /* PreprocessLang.scss */]: scss.process,
  30728. ["sass" /* PreprocessLang.sass */]: sassProcess,
  30729. ["styl" /* PreprocessLang.styl */]: styl.process,
  30730. ["stylus" /* PreprocessLang.stylus */]: styl.process,
  30731. close,
  30732. };
  30733. };
  30734. const normalizeMaxWorkers = (maxWorker) => {
  30735. if (maxWorker === undefined)
  30736. return 0;
  30737. if (maxWorker === true)
  30738. return undefined;
  30739. return maxWorker;
  30740. };
  30741. const preprocessorSet = new Set([
  30742. "less" /* PreprocessLang.less */,
  30743. "sass" /* PreprocessLang.sass */,
  30744. "scss" /* PreprocessLang.scss */,
  30745. "styl" /* PreprocessLang.styl */,
  30746. "stylus" /* PreprocessLang.stylus */,
  30747. ]);
  30748. function isPreProcessor(lang) {
  30749. return lang && preprocessorSet.has(lang);
  30750. }
  30751. const importLightningCSS = createCachedImport(() => import('lightningcss'));
  30752. async function compileLightningCSS(id, src, config, urlReplacer) {
  30753. const deps = new Set();
  30754. // Relative path is needed to get stable hash when using CSS modules
  30755. const filename = cleanUrl(path$o.relative(config.root, id));
  30756. const toAbsolute = (filePath) => path$o.isAbsolute(filePath) ? filePath : path$o.join(config.root, filePath);
  30757. const res = styleAttrRE.test(id)
  30758. ? (await importLightningCSS()).transformStyleAttribute({
  30759. filename,
  30760. code: Buffer.from(src),
  30761. targets: config.css?.lightningcss?.targets,
  30762. minify: config.isProduction && !!config.build.cssMinify,
  30763. analyzeDependencies: true,
  30764. })
  30765. : await (await importLightningCSS()).bundleAsync({
  30766. ...config.css?.lightningcss,
  30767. filename,
  30768. resolver: {
  30769. read(filePath) {
  30770. if (filePath === filename) {
  30771. return src;
  30772. }
  30773. // This happens with html-proxy (#13776)
  30774. if (!filePath.endsWith('.css')) {
  30775. return src;
  30776. }
  30777. return fs$l.readFileSync(toAbsolute(filePath), 'utf-8');
  30778. },
  30779. async resolve(id, from) {
  30780. const publicFile = checkPublicFile(id, config);
  30781. if (publicFile) {
  30782. return publicFile;
  30783. }
  30784. const resolved = await getAtImportResolvers(config).css(id, toAbsolute(from));
  30785. if (resolved) {
  30786. deps.add(resolved);
  30787. return resolved;
  30788. }
  30789. return id;
  30790. },
  30791. },
  30792. minify: config.isProduction && !!config.build.cssMinify,
  30793. sourceMap: config.command === 'build'
  30794. ? !!config.build.sourcemap
  30795. : config.css?.devSourcemap,
  30796. analyzeDependencies: true,
  30797. cssModules: cssModuleRE.test(id)
  30798. ? config.css?.lightningcss?.cssModules ?? true
  30799. : undefined,
  30800. });
  30801. let css = res.code.toString();
  30802. for (const dep of res.dependencies) {
  30803. switch (dep.type) {
  30804. case 'url':
  30805. if (skipUrlReplacer(dep.url)) {
  30806. css = css.replace(dep.placeholder, () => dep.url);
  30807. break;
  30808. }
  30809. deps.add(dep.url);
  30810. if (urlReplacer) {
  30811. const replaceUrl = await urlReplacer(dep.url, id);
  30812. css = css.replace(dep.placeholder, () => replaceUrl);
  30813. }
  30814. break;
  30815. default:
  30816. throw new Error(`Unsupported dependency type: ${dep.type}`);
  30817. }
  30818. }
  30819. let modules;
  30820. if ('exports' in res && res.exports) {
  30821. modules = {};
  30822. // https://github.com/parcel-bundler/lightningcss/issues/291
  30823. const sortedEntries = Object.entries(res.exports).sort((a, b) => a[0].localeCompare(b[0]));
  30824. for (const [key, value] of sortedEntries) {
  30825. modules[key] = value.name;
  30826. // https://lightningcss.dev/css-modules.html#class-composition
  30827. for (const c of value.composes) {
  30828. modules[key] += ' ' + c.name;
  30829. }
  30830. }
  30831. }
  30832. return {
  30833. code: css,
  30834. map: 'map' in res ? res.map?.toString() : undefined,
  30835. deps,
  30836. modules,
  30837. };
  30838. }
  30839. // Convert https://esbuild.github.io/api/#target
  30840. // To https://github.com/parcel-bundler/lightningcss/blob/master/node/targets.d.ts
  30841. const map = {
  30842. chrome: 'chrome',
  30843. edge: 'edge',
  30844. firefox: 'firefox',
  30845. hermes: false,
  30846. ie: 'ie',
  30847. ios: 'ios_saf',
  30848. node: false,
  30849. opera: 'opera',
  30850. rhino: false,
  30851. safari: 'safari',
  30852. };
  30853. const esMap = {
  30854. // https://caniuse.com/?search=es2015
  30855. 2015: ['chrome49', 'edge13', 'safari10', 'firefox44', 'opera36'],
  30856. // https://caniuse.com/?search=es2016
  30857. 2016: ['chrome50', 'edge13', 'safari10', 'firefox43', 'opera37'],
  30858. // https://caniuse.com/?search=es2017
  30859. 2017: ['chrome58', 'edge15', 'safari11', 'firefox52', 'opera45'],
  30860. // https://caniuse.com/?search=es2018
  30861. 2018: ['chrome63', 'edge79', 'safari12', 'firefox58', 'opera50'],
  30862. // https://caniuse.com/?search=es2019
  30863. 2019: ['chrome73', 'edge79', 'safari12.1', 'firefox64', 'opera60'],
  30864. // https://caniuse.com/?search=es2020
  30865. 2020: ['chrome80', 'edge80', 'safari14.1', 'firefox80', 'opera67'],
  30866. // https://caniuse.com/?search=es2021
  30867. 2021: ['chrome85', 'edge85', 'safari14.1', 'firefox80', 'opera71'],
  30868. // https://caniuse.com/?search=es2022
  30869. 2022: ['chrome94', 'edge94', 'safari16.4', 'firefox93', 'opera80'],
  30870. };
  30871. const esRE = /es(\d{4})/;
  30872. const versionRE = /\d/;
  30873. const convertTargetsCache = new Map();
  30874. const convertTargets = (esbuildTarget) => {
  30875. if (!esbuildTarget)
  30876. return {};
  30877. const cached = convertTargetsCache.get(esbuildTarget);
  30878. if (cached)
  30879. return cached;
  30880. const targets = {};
  30881. const entriesWithoutES = arraify(esbuildTarget).flatMap((e) => {
  30882. const match = e.match(esRE);
  30883. if (!match)
  30884. return e;
  30885. const year = Number(match[1]);
  30886. if (!esMap[year])
  30887. throw new Error(`Unsupported target "${e}"`);
  30888. return esMap[year];
  30889. });
  30890. for (const entry of entriesWithoutES) {
  30891. if (entry === 'esnext')
  30892. continue;
  30893. const index = entry.search(versionRE);
  30894. if (index >= 0) {
  30895. const browser = map[entry.slice(0, index)];
  30896. if (browser === false)
  30897. continue; // No mapping available
  30898. if (browser) {
  30899. const [major, minor = 0] = entry
  30900. .slice(index)
  30901. .split('.')
  30902. .map((v) => parseInt(v, 10));
  30903. if (!isNaN(major) && !isNaN(minor)) {
  30904. const version = (major << 16) | (minor << 8);
  30905. if (!targets[browser] || version < targets[browser]) {
  30906. targets[browser] = version;
  30907. }
  30908. continue;
  30909. }
  30910. }
  30911. }
  30912. throw new Error(`Unsupported target "${entry}"`);
  30913. }
  30914. convertTargetsCache.set(esbuildTarget, targets);
  30915. return targets;
  30916. };
  30917. // This file was generated. Do not modify manually!
  30918. var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  30919. // This file was generated. Do not modify manually!
  30920. var astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191];
  30921. // This file was generated. Do not modify manually!
  30922. var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65";
  30923. // This file was generated. Do not modify manually!
  30924. var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  30925. // These are a run-length and offset encoded representation of the
  30926. // >0xffff code points that are a valid part of identifiers. The
  30927. // offset starts at 0x10000, and each pair of numbers represents an
  30928. // offset to the next range, and then a size of the range.
  30929. // Reserved word lists for various dialects of the language
  30930. var reservedWords = {
  30931. 3: "abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile",
  30932. 5: "class enum extends super const export import",
  30933. 6: "enum",
  30934. strict: "implements interface let package private protected public static yield",
  30935. strictBind: "eval arguments"
  30936. };
  30937. // And the keywords
  30938. var ecma5AndLessKeywords = "break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this";
  30939. var keywords$1 = {
  30940. 5: ecma5AndLessKeywords,
  30941. "5module": ecma5AndLessKeywords + " export import",
  30942. 6: ecma5AndLessKeywords + " const class extends export import super"
  30943. };
  30944. var keywordRelationalOperator = /^in(stanceof)?$/;
  30945. // ## Character categories
  30946. var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  30947. var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  30948. // This has a complexity linear to the value of the code. The
  30949. // assumption is that looking up astral identifier characters is
  30950. // rare.
  30951. function isInAstralSet(code, set) {
  30952. var pos = 0x10000;
  30953. for (var i = 0; i < set.length; i += 2) {
  30954. pos += set[i];
  30955. if (pos > code) { return false }
  30956. pos += set[i + 1];
  30957. if (pos >= code) { return true }
  30958. }
  30959. return false
  30960. }
  30961. // Test whether a given character code starts an identifier.
  30962. function isIdentifierStart(code, astral) {
  30963. if (code < 65) { return code === 36 }
  30964. if (code < 91) { return true }
  30965. if (code < 97) { return code === 95 }
  30966. if (code < 123) { return true }
  30967. if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) }
  30968. if (astral === false) { return false }
  30969. return isInAstralSet(code, astralIdentifierStartCodes)
  30970. }
  30971. // Test whether a given character is part of an identifier.
  30972. function isIdentifierChar(code, astral) {
  30973. if (code < 48) { return code === 36 }
  30974. if (code < 58) { return true }
  30975. if (code < 65) { return false }
  30976. if (code < 91) { return true }
  30977. if (code < 97) { return code === 95 }
  30978. if (code < 123) { return true }
  30979. if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)) }
  30980. if (astral === false) { return false }
  30981. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
  30982. }
  30983. // ## Token types
  30984. // The assignment of fine-grained, information-carrying type objects
  30985. // allows the tokenizer to store the information it has about a
  30986. // token in a way that is very cheap for the parser to look up.
  30987. // All token type variables start with an underscore, to make them
  30988. // easy to recognize.
  30989. // The `beforeExpr` property is used to disambiguate between regular
  30990. // expressions and divisions. It is set on all token types that can
  30991. // be followed by an expression (thus, a slash after them would be a
  30992. // regular expression).
  30993. //
  30994. // The `startsExpr` property is used to check if the token ends a
  30995. // `yield` expression. It is set on all token types that either can
  30996. // directly start an expression (like a quotation mark) or can
  30997. // continue an expression (like the body of a string).
  30998. //
  30999. // `isLoop` marks a keyword as starting a loop, which is important
  31000. // to know when parsing a label, in order to allow or disallow
  31001. // continue jumps to that label.
  31002. var TokenType = function TokenType(label, conf) {
  31003. if ( conf === void 0 ) conf = {};
  31004. this.label = label;
  31005. this.keyword = conf.keyword;
  31006. this.beforeExpr = !!conf.beforeExpr;
  31007. this.startsExpr = !!conf.startsExpr;
  31008. this.isLoop = !!conf.isLoop;
  31009. this.isAssign = !!conf.isAssign;
  31010. this.prefix = !!conf.prefix;
  31011. this.postfix = !!conf.postfix;
  31012. this.binop = conf.binop || null;
  31013. this.updateContext = null;
  31014. };
  31015. function binop(name, prec) {
  31016. return new TokenType(name, {beforeExpr: true, binop: prec})
  31017. }
  31018. var beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true};
  31019. // Map keyword names to token types.
  31020. var keywords = {};
  31021. // Succinct definitions of keyword token types
  31022. function kw(name, options) {
  31023. if ( options === void 0 ) options = {};
  31024. options.keyword = name;
  31025. return keywords[name] = new TokenType(name, options)
  31026. }
  31027. var types$1 = {
  31028. num: new TokenType("num", startsExpr),
  31029. regexp: new TokenType("regexp", startsExpr),
  31030. string: new TokenType("string", startsExpr),
  31031. name: new TokenType("name", startsExpr),
  31032. privateId: new TokenType("privateId", startsExpr),
  31033. eof: new TokenType("eof"),
  31034. // Punctuation token types.
  31035. bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
  31036. bracketR: new TokenType("]"),
  31037. braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
  31038. braceR: new TokenType("}"),
  31039. parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
  31040. parenR: new TokenType(")"),
  31041. comma: new TokenType(",", beforeExpr),
  31042. semi: new TokenType(";", beforeExpr),
  31043. colon: new TokenType(":", beforeExpr),
  31044. dot: new TokenType("."),
  31045. question: new TokenType("?", beforeExpr),
  31046. questionDot: new TokenType("?."),
  31047. arrow: new TokenType("=>", beforeExpr),
  31048. template: new TokenType("template"),
  31049. invalidTemplate: new TokenType("invalidTemplate"),
  31050. ellipsis: new TokenType("...", beforeExpr),
  31051. backQuote: new TokenType("`", startsExpr),
  31052. dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
  31053. // Operators. These carry several kinds of properties to help the
  31054. // parser use them properly (the presence of these properties is
  31055. // what categorizes them as operators).
  31056. //
  31057. // `binop`, when present, specifies that this operator is a binary
  31058. // operator, and will refer to its precedence.
  31059. //
  31060. // `prefix` and `postfix` mark the operator as a prefix or postfix
  31061. // unary operator.
  31062. //
  31063. // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
  31064. // binary operators with a very low precedence, that should result
  31065. // in AssignmentExpression nodes.
  31066. eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
  31067. assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
  31068. incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
  31069. prefix: new TokenType("!/~", {beforeExpr: true, prefix: true, startsExpr: true}),
  31070. logicalOR: binop("||", 1),
  31071. logicalAND: binop("&&", 2),
  31072. bitwiseOR: binop("|", 3),
  31073. bitwiseXOR: binop("^", 4),
  31074. bitwiseAND: binop("&", 5),
  31075. equality: binop("==/!=/===/!==", 6),
  31076. relational: binop("</>/<=/>=", 7),
  31077. bitShift: binop("<</>>/>>>", 8),
  31078. plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
  31079. modulo: binop("%", 10),
  31080. star: binop("*", 10),
  31081. slash: binop("/", 10),
  31082. starstar: new TokenType("**", {beforeExpr: true}),
  31083. coalesce: binop("??", 1),
  31084. // Keyword token types.
  31085. _break: kw("break"),
  31086. _case: kw("case", beforeExpr),
  31087. _catch: kw("catch"),
  31088. _continue: kw("continue"),
  31089. _debugger: kw("debugger"),
  31090. _default: kw("default", beforeExpr),
  31091. _do: kw("do", {isLoop: true, beforeExpr: true}),
  31092. _else: kw("else", beforeExpr),
  31093. _finally: kw("finally"),
  31094. _for: kw("for", {isLoop: true}),
  31095. _function: kw("function", startsExpr),
  31096. _if: kw("if"),
  31097. _return: kw("return", beforeExpr),
  31098. _switch: kw("switch"),
  31099. _throw: kw("throw", beforeExpr),
  31100. _try: kw("try"),
  31101. _var: kw("var"),
  31102. _const: kw("const"),
  31103. _while: kw("while", {isLoop: true}),
  31104. _with: kw("with"),
  31105. _new: kw("new", {beforeExpr: true, startsExpr: true}),
  31106. _this: kw("this", startsExpr),
  31107. _super: kw("super", startsExpr),
  31108. _class: kw("class", startsExpr),
  31109. _extends: kw("extends", beforeExpr),
  31110. _export: kw("export"),
  31111. _import: kw("import", startsExpr),
  31112. _null: kw("null", startsExpr),
  31113. _true: kw("true", startsExpr),
  31114. _false: kw("false", startsExpr),
  31115. _in: kw("in", {beforeExpr: true, binop: 7}),
  31116. _instanceof: kw("instanceof", {beforeExpr: true, binop: 7}),
  31117. _typeof: kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true}),
  31118. _void: kw("void", {beforeExpr: true, prefix: true, startsExpr: true}),
  31119. _delete: kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})
  31120. };
  31121. // Matches a whole line break (where CRLF is considered a single
  31122. // line break). Used to count lines.
  31123. var lineBreak = /\r\n?|\n|\u2028|\u2029/;
  31124. var lineBreakG = new RegExp(lineBreak.source, "g");
  31125. function isNewLine(code) {
  31126. return code === 10 || code === 13 || code === 0x2028 || code === 0x2029
  31127. }
  31128. function nextLineBreak(code, from, end) {
  31129. if ( end === void 0 ) end = code.length;
  31130. for (var i = from; i < end; i++) {
  31131. var next = code.charCodeAt(i);
  31132. if (isNewLine(next))
  31133. { return i < end - 1 && next === 13 && code.charCodeAt(i + 1) === 10 ? i + 2 : i + 1 }
  31134. }
  31135. return -1
  31136. }
  31137. var nonASCIIwhitespace = /[\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
  31138. var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  31139. var ref = Object.prototype;
  31140. var hasOwnProperty$1 = ref.hasOwnProperty;
  31141. var toString$1 = ref.toString;
  31142. var hasOwn = Object.hasOwn || (function (obj, propName) { return (
  31143. hasOwnProperty$1.call(obj, propName)
  31144. ); });
  31145. var isArray = Array.isArray || (function (obj) { return (
  31146. toString$1.call(obj) === "[object Array]"
  31147. ); });
  31148. var regexpCache = Object.create(null);
  31149. function wordsRegexp(words) {
  31150. return regexpCache[words] || (regexpCache[words] = new RegExp("^(?:" + words.replace(/ /g, "|") + ")$"))
  31151. }
  31152. function codePointToString(code) {
  31153. // UTF-16 Decoding
  31154. if (code <= 0xFFFF) { return String.fromCharCode(code) }
  31155. code -= 0x10000;
  31156. return String.fromCharCode((code >> 10) + 0xD800, (code & 1023) + 0xDC00)
  31157. }
  31158. var loneSurrogate = /(?:[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/;
  31159. // These are used when `options.locations` is on, for the
  31160. // `startLoc` and `endLoc` properties.
  31161. var Position = function Position(line, col) {
  31162. this.line = line;
  31163. this.column = col;
  31164. };
  31165. Position.prototype.offset = function offset (n) {
  31166. return new Position(this.line, this.column + n)
  31167. };
  31168. var SourceLocation = function SourceLocation(p, start, end) {
  31169. this.start = start;
  31170. this.end = end;
  31171. if (p.sourceFile !== null) { this.source = p.sourceFile; }
  31172. };
  31173. // The `getLineInfo` function is mostly useful when the
  31174. // `locations` option is off (for performance reasons) and you
  31175. // want to find the line/column position for a given character
  31176. // offset. `input` should be the code string that the offset refers
  31177. // into.
  31178. function getLineInfo(input, offset) {
  31179. for (var line = 1, cur = 0;;) {
  31180. var nextBreak = nextLineBreak(input, cur, offset);
  31181. if (nextBreak < 0) { return new Position(line, offset - cur) }
  31182. ++line;
  31183. cur = nextBreak;
  31184. }
  31185. }
  31186. // A second argument must be given to configure the parser process.
  31187. // These options are recognized (only `ecmaVersion` is required):
  31188. var defaultOptions = {
  31189. // `ecmaVersion` indicates the ECMAScript version to parse. Must be
  31190. // either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10
  31191. // (2019), 11 (2020), 12 (2021), 13 (2022), 14 (2023), or `"latest"`
  31192. // (the latest version the library supports). This influences
  31193. // support for strict mode, the set of reserved words, and support
  31194. // for new syntax features.
  31195. ecmaVersion: null,
  31196. // `sourceType` indicates the mode the code should be parsed in.
  31197. // Can be either `"script"` or `"module"`. This influences global
  31198. // strict mode and parsing of `import` and `export` declarations.
  31199. sourceType: "script",
  31200. // `onInsertedSemicolon` can be a callback that will be called when
  31201. // a semicolon is automatically inserted. It will be passed the
  31202. // position of the inserted semicolon as an offset, and if
  31203. // `locations` is enabled, it is given the location as a `{line,
  31204. // column}` object as second argument.
  31205. onInsertedSemicolon: null,
  31206. // `onTrailingComma` is similar to `onInsertedSemicolon`, but for
  31207. // trailing commas.
  31208. onTrailingComma: null,
  31209. // By default, reserved words are only enforced if ecmaVersion >= 5.
  31210. // Set `allowReserved` to a boolean value to explicitly turn this on
  31211. // an off. When this option has the value "never", reserved words
  31212. // and keywords can also not be used as property names.
  31213. allowReserved: null,
  31214. // When enabled, a return at the top level is not considered an
  31215. // error.
  31216. allowReturnOutsideFunction: false,
  31217. // When enabled, import/export statements are not constrained to
  31218. // appearing at the top of the program, and an import.meta expression
  31219. // in a script isn't considered an error.
  31220. allowImportExportEverywhere: false,
  31221. // By default, await identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022.
  31222. // When enabled, await identifiers are allowed to appear at the top-level scope,
  31223. // but they are still not allowed in non-async functions.
  31224. allowAwaitOutsideFunction: null,
  31225. // When enabled, super identifiers are not constrained to
  31226. // appearing in methods and do not raise an error when they appear elsewhere.
  31227. allowSuperOutsideMethod: null,
  31228. // When enabled, hashbang directive in the beginning of file is
  31229. // allowed and treated as a line comment. Enabled by default when
  31230. // `ecmaVersion` >= 2023.
  31231. allowHashBang: false,
  31232. // By default, the parser will verify that private properties are
  31233. // only used in places where they are valid and have been declared.
  31234. // Set this to false to turn such checks off.
  31235. checkPrivateFields: true,
  31236. // When `locations` is on, `loc` properties holding objects with
  31237. // `start` and `end` properties in `{line, column}` form (with
  31238. // line being 1-based and column 0-based) will be attached to the
  31239. // nodes.
  31240. locations: false,
  31241. // A function can be passed as `onToken` option, which will
  31242. // cause Acorn to call that function with object in the same
  31243. // format as tokens returned from `tokenizer().getToken()`. Note
  31244. // that you are not allowed to call the parser from the
  31245. // callback—that will corrupt its internal state.
  31246. onToken: null,
  31247. // A function can be passed as `onComment` option, which will
  31248. // cause Acorn to call that function with `(block, text, start,
  31249. // end)` parameters whenever a comment is skipped. `block` is a
  31250. // boolean indicating whether this is a block (`/* */`) comment,
  31251. // `text` is the content of the comment, and `start` and `end` are
  31252. // character offsets that denote the start and end of the comment.
  31253. // When the `locations` option is on, two more parameters are
  31254. // passed, the full `{line, column}` locations of the start and
  31255. // end of the comments. Note that you are not allowed to call the
  31256. // parser from the callback—that will corrupt its internal state.
  31257. // When this option has an array as value, objects representing the
  31258. // comments are pushed to it.
  31259. onComment: null,
  31260. // Nodes have their start and end characters offsets recorded in
  31261. // `start` and `end` properties (directly on the node, rather than
  31262. // the `loc` object, which holds line/column data. To also add a
  31263. // [semi-standardized][range] `range` property holding a `[start,
  31264. // end]` array with the same numbers, set the `ranges` option to
  31265. // `true`.
  31266. //
  31267. // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
  31268. ranges: false,
  31269. // It is possible to parse multiple files into a single AST by
  31270. // passing the tree produced by parsing the first file as
  31271. // `program` option in subsequent parses. This will add the
  31272. // toplevel forms of the parsed file to the `Program` (top) node
  31273. // of an existing parse tree.
  31274. program: null,
  31275. // When `locations` is on, you can pass this to record the source
  31276. // file in every node's `loc` object.
  31277. sourceFile: null,
  31278. // This value, if given, is stored in every node, whether
  31279. // `locations` is on or off.
  31280. directSourceFile: null,
  31281. // When enabled, parenthesized expressions are represented by
  31282. // (non-standard) ParenthesizedExpression nodes
  31283. preserveParens: false
  31284. };
  31285. // Interpret and default an options object
  31286. var warnedAboutEcmaVersion = false;
  31287. function getOptions(opts) {
  31288. var options = {};
  31289. for (var opt in defaultOptions)
  31290. { options[opt] = opts && hasOwn(opts, opt) ? opts[opt] : defaultOptions[opt]; }
  31291. if (options.ecmaVersion === "latest") {
  31292. options.ecmaVersion = 1e8;
  31293. } else if (options.ecmaVersion == null) {
  31294. if (!warnedAboutEcmaVersion && typeof console === "object" && console.warn) {
  31295. warnedAboutEcmaVersion = true;
  31296. console.warn("Since Acorn 8.0.0, options.ecmaVersion is required.\nDefaulting to 2020, but this will stop working in the future.");
  31297. }
  31298. options.ecmaVersion = 11;
  31299. } else if (options.ecmaVersion >= 2015) {
  31300. options.ecmaVersion -= 2009;
  31301. }
  31302. if (options.allowReserved == null)
  31303. { options.allowReserved = options.ecmaVersion < 5; }
  31304. if (!opts || opts.allowHashBang == null)
  31305. { options.allowHashBang = options.ecmaVersion >= 14; }
  31306. if (isArray(options.onToken)) {
  31307. var tokens = options.onToken;
  31308. options.onToken = function (token) { return tokens.push(token); };
  31309. }
  31310. if (isArray(options.onComment))
  31311. { options.onComment = pushComment(options, options.onComment); }
  31312. return options
  31313. }
  31314. function pushComment(options, array) {
  31315. return function(block, text, start, end, startLoc, endLoc) {
  31316. var comment = {
  31317. type: block ? "Block" : "Line",
  31318. value: text,
  31319. start: start,
  31320. end: end
  31321. };
  31322. if (options.locations)
  31323. { comment.loc = new SourceLocation(this, startLoc, endLoc); }
  31324. if (options.ranges)
  31325. { comment.range = [start, end]; }
  31326. array.push(comment);
  31327. }
  31328. }
  31329. // Each scope gets a bitset that may contain these flags
  31330. var
  31331. SCOPE_TOP = 1,
  31332. SCOPE_FUNCTION = 2,
  31333. SCOPE_ASYNC = 4,
  31334. SCOPE_GENERATOR = 8,
  31335. SCOPE_ARROW = 16,
  31336. SCOPE_SIMPLE_CATCH = 32,
  31337. SCOPE_SUPER = 64,
  31338. SCOPE_DIRECT_SUPER = 128,
  31339. SCOPE_CLASS_STATIC_BLOCK = 256,
  31340. SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK;
  31341. function functionFlags(async, generator) {
  31342. return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0)
  31343. }
  31344. // Used in checkLVal* and declareName to determine the type of a binding
  31345. var
  31346. BIND_NONE = 0, // Not a binding
  31347. BIND_VAR = 1, // Var-style binding
  31348. BIND_LEXICAL = 2, // Let- or const-style binding
  31349. BIND_FUNCTION = 3, // Function declaration
  31350. BIND_SIMPLE_CATCH = 4, // Simple (identifier pattern) catch binding
  31351. BIND_OUTSIDE = 5; // Special case for function names as bound inside the function
  31352. var Parser = function Parser(options, input, startPos) {
  31353. this.options = options = getOptions(options);
  31354. this.sourceFile = options.sourceFile;
  31355. this.keywords = wordsRegexp(keywords$1[options.ecmaVersion >= 6 ? 6 : options.sourceType === "module" ? "5module" : 5]);
  31356. var reserved = "";
  31357. if (options.allowReserved !== true) {
  31358. reserved = reservedWords[options.ecmaVersion >= 6 ? 6 : options.ecmaVersion === 5 ? 5 : 3];
  31359. if (options.sourceType === "module") { reserved += " await"; }
  31360. }
  31361. this.reservedWords = wordsRegexp(reserved);
  31362. var reservedStrict = (reserved ? reserved + " " : "") + reservedWords.strict;
  31363. this.reservedWordsStrict = wordsRegexp(reservedStrict);
  31364. this.reservedWordsStrictBind = wordsRegexp(reservedStrict + " " + reservedWords.strictBind);
  31365. this.input = String(input);
  31366. // Used to signal to callers of `readWord1` whether the word
  31367. // contained any escape sequences. This is needed because words with
  31368. // escape sequences must not be interpreted as keywords.
  31369. this.containsEsc = false;
  31370. // Set up token state
  31371. // The current position of the tokenizer in the input.
  31372. if (startPos) {
  31373. this.pos = startPos;
  31374. this.lineStart = this.input.lastIndexOf("\n", startPos - 1) + 1;
  31375. this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length;
  31376. } else {
  31377. this.pos = this.lineStart = 0;
  31378. this.curLine = 1;
  31379. }
  31380. // Properties of the current token:
  31381. // Its type
  31382. this.type = types$1.eof;
  31383. // For tokens that include more information than their type, the value
  31384. this.value = null;
  31385. // Its start and end offset
  31386. this.start = this.end = this.pos;
  31387. // And, if locations are used, the {line, column} object
  31388. // corresponding to those offsets
  31389. this.startLoc = this.endLoc = this.curPosition();
  31390. // Position information for the previous token
  31391. this.lastTokEndLoc = this.lastTokStartLoc = null;
  31392. this.lastTokStart = this.lastTokEnd = this.pos;
  31393. // The context stack is used to superficially track syntactic
  31394. // context to predict whether a regular expression is allowed in a
  31395. // given position.
  31396. this.context = this.initialContext();
  31397. this.exprAllowed = true;
  31398. // Figure out if it's a module code.
  31399. this.inModule = options.sourceType === "module";
  31400. this.strict = this.inModule || this.strictDirective(this.pos);
  31401. // Used to signify the start of a potential arrow function
  31402. this.potentialArrowAt = -1;
  31403. this.potentialArrowInForAwait = false;
  31404. // Positions to delayed-check that yield/await does not exist in default parameters.
  31405. this.yieldPos = this.awaitPos = this.awaitIdentPos = 0;
  31406. // Labels in scope.
  31407. this.labels = [];
  31408. // Thus-far undefined exports.
  31409. this.undefinedExports = Object.create(null);
  31410. // If enabled, skip leading hashbang line.
  31411. if (this.pos === 0 && options.allowHashBang && this.input.slice(0, 2) === "#!")
  31412. { this.skipLineComment(2); }
  31413. // Scope tracking for duplicate variable names (see scope.js)
  31414. this.scopeStack = [];
  31415. this.enterScope(SCOPE_TOP);
  31416. // For RegExp validation
  31417. this.regexpState = null;
  31418. // The stack of private names.
  31419. // Each element has two properties: 'declared' and 'used'.
  31420. // When it exited from the outermost class definition, all used private names must be declared.
  31421. this.privateNameStack = [];
  31422. };
  31423. var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },inClassStaticBlock: { configurable: true } };
  31424. Parser.prototype.parse = function parse () {
  31425. var node = this.options.program || this.startNode();
  31426. this.nextToken();
  31427. return this.parseTopLevel(node)
  31428. };
  31429. prototypeAccessors.inFunction.get = function () { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 };
  31430. prototypeAccessors.inGenerator.get = function () { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentVarScope().inClassFieldInit };
  31431. prototypeAccessors.inAsync.get = function () { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit };
  31432. prototypeAccessors.canAwait.get = function () {
  31433. for (var i = this.scopeStack.length - 1; i >= 0; i--) {
  31434. var scope = this.scopeStack[i];
  31435. if (scope.inClassFieldInit || scope.flags & SCOPE_CLASS_STATIC_BLOCK) { return false }
  31436. if (scope.flags & SCOPE_FUNCTION) { return (scope.flags & SCOPE_ASYNC) > 0 }
  31437. }
  31438. return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction
  31439. };
  31440. prototypeAccessors.allowSuper.get = function () {
  31441. var ref = this.currentThisScope();
  31442. var flags = ref.flags;
  31443. var inClassFieldInit = ref.inClassFieldInit;
  31444. return (flags & SCOPE_SUPER) > 0 || inClassFieldInit || this.options.allowSuperOutsideMethod
  31445. };
  31446. prototypeAccessors.allowDirectSuper.get = function () { return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0 };
  31447. prototypeAccessors.treatFunctionsAsVar.get = function () { return this.treatFunctionsAsVarInScope(this.currentScope()) };
  31448. prototypeAccessors.allowNewDotTarget.get = function () {
  31449. var ref = this.currentThisScope();
  31450. var flags = ref.flags;
  31451. var inClassFieldInit = ref.inClassFieldInit;
  31452. return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || inClassFieldInit
  31453. };
  31454. prototypeAccessors.inClassStaticBlock.get = function () {
  31455. return (this.currentVarScope().flags & SCOPE_CLASS_STATIC_BLOCK) > 0
  31456. };
  31457. Parser.extend = function extend () {
  31458. var plugins = [], len = arguments.length;
  31459. while ( len-- ) plugins[ len ] = arguments[ len ];
  31460. var cls = this;
  31461. for (var i = 0; i < plugins.length; i++) { cls = plugins[i](cls); }
  31462. return cls
  31463. };
  31464. Parser.parse = function parse (input, options) {
  31465. return new this(options, input).parse()
  31466. };
  31467. Parser.parseExpressionAt = function parseExpressionAt (input, pos, options) {
  31468. var parser = new this(options, input, pos);
  31469. parser.nextToken();
  31470. return parser.parseExpression()
  31471. };
  31472. Parser.tokenizer = function tokenizer (input, options) {
  31473. return new this(options, input)
  31474. };
  31475. Object.defineProperties( Parser.prototype, prototypeAccessors );
  31476. var pp$9 = Parser.prototype;
  31477. // ## Parser utilities
  31478. var literal = /^(?:'((?:\\.|[^'\\])*?)'|"((?:\\.|[^"\\])*?)")/;
  31479. pp$9.strictDirective = function(start) {
  31480. if (this.options.ecmaVersion < 5) { return false }
  31481. for (;;) {
  31482. // Try to find string literal.
  31483. skipWhiteSpace.lastIndex = start;
  31484. start += skipWhiteSpace.exec(this.input)[0].length;
  31485. var match = literal.exec(this.input.slice(start));
  31486. if (!match) { return false }
  31487. if ((match[1] || match[2]) === "use strict") {
  31488. skipWhiteSpace.lastIndex = start + match[0].length;
  31489. var spaceAfter = skipWhiteSpace.exec(this.input), end = spaceAfter.index + spaceAfter[0].length;
  31490. var next = this.input.charAt(end);
  31491. return next === ";" || next === "}" ||
  31492. (lineBreak.test(spaceAfter[0]) &&
  31493. !(/[(`.[+\-/*%<>=,?^&]/.test(next) || next === "!" && this.input.charAt(end + 1) === "="))
  31494. }
  31495. start += match[0].length;
  31496. // Skip semicolon, if any.
  31497. skipWhiteSpace.lastIndex = start;
  31498. start += skipWhiteSpace.exec(this.input)[0].length;
  31499. if (this.input[start] === ";")
  31500. { start++; }
  31501. }
  31502. };
  31503. // Predicate that tests whether the next token is of the given
  31504. // type, and if yes, consumes it as a side effect.
  31505. pp$9.eat = function(type) {
  31506. if (this.type === type) {
  31507. this.next();
  31508. return true
  31509. } else {
  31510. return false
  31511. }
  31512. };
  31513. // Tests whether parsed token is a contextual keyword.
  31514. pp$9.isContextual = function(name) {
  31515. return this.type === types$1.name && this.value === name && !this.containsEsc
  31516. };
  31517. // Consumes contextual keyword if possible.
  31518. pp$9.eatContextual = function(name) {
  31519. if (!this.isContextual(name)) { return false }
  31520. this.next();
  31521. return true
  31522. };
  31523. // Asserts that following token is given contextual keyword.
  31524. pp$9.expectContextual = function(name) {
  31525. if (!this.eatContextual(name)) { this.unexpected(); }
  31526. };
  31527. // Test whether a semicolon can be inserted at the current position.
  31528. pp$9.canInsertSemicolon = function() {
  31529. return this.type === types$1.eof ||
  31530. this.type === types$1.braceR ||
  31531. lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
  31532. };
  31533. pp$9.insertSemicolon = function() {
  31534. if (this.canInsertSemicolon()) {
  31535. if (this.options.onInsertedSemicolon)
  31536. { this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc); }
  31537. return true
  31538. }
  31539. };
  31540. // Consume a semicolon, or, failing that, see if we are allowed to
  31541. // pretend that there is a semicolon at this position.
  31542. pp$9.semicolon = function() {
  31543. if (!this.eat(types$1.semi) && !this.insertSemicolon()) { this.unexpected(); }
  31544. };
  31545. pp$9.afterTrailingComma = function(tokType, notNext) {
  31546. if (this.type === tokType) {
  31547. if (this.options.onTrailingComma)
  31548. { this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc); }
  31549. if (!notNext)
  31550. { this.next(); }
  31551. return true
  31552. }
  31553. };
  31554. // Expect a token of a given type. If found, consume it, otherwise,
  31555. // raise an unexpected token error.
  31556. pp$9.expect = function(type) {
  31557. this.eat(type) || this.unexpected();
  31558. };
  31559. // Raise an unexpected token error.
  31560. pp$9.unexpected = function(pos) {
  31561. this.raise(pos != null ? pos : this.start, "Unexpected token");
  31562. };
  31563. var DestructuringErrors = function DestructuringErrors() {
  31564. this.shorthandAssign =
  31565. this.trailingComma =
  31566. this.parenthesizedAssign =
  31567. this.parenthesizedBind =
  31568. this.doubleProto =
  31569. -1;
  31570. };
  31571. pp$9.checkPatternErrors = function(refDestructuringErrors, isAssign) {
  31572. if (!refDestructuringErrors) { return }
  31573. if (refDestructuringErrors.trailingComma > -1)
  31574. { this.raiseRecoverable(refDestructuringErrors.trailingComma, "Comma is not permitted after the rest element"); }
  31575. var parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind;
  31576. if (parens > -1) { this.raiseRecoverable(parens, isAssign ? "Assigning to rvalue" : "Parenthesized pattern"); }
  31577. };
  31578. pp$9.checkExpressionErrors = function(refDestructuringErrors, andThrow) {
  31579. if (!refDestructuringErrors) { return false }
  31580. var shorthandAssign = refDestructuringErrors.shorthandAssign;
  31581. var doubleProto = refDestructuringErrors.doubleProto;
  31582. if (!andThrow) { return shorthandAssign >= 0 || doubleProto >= 0 }
  31583. if (shorthandAssign >= 0)
  31584. { this.raise(shorthandAssign, "Shorthand property assignments are valid only in destructuring patterns"); }
  31585. if (doubleProto >= 0)
  31586. { this.raiseRecoverable(doubleProto, "Redefinition of __proto__ property"); }
  31587. };
  31588. pp$9.checkYieldAwaitInDefaultParams = function() {
  31589. if (this.yieldPos && (!this.awaitPos || this.yieldPos < this.awaitPos))
  31590. { this.raise(this.yieldPos, "Yield expression cannot be a default value"); }
  31591. if (this.awaitPos)
  31592. { this.raise(this.awaitPos, "Await expression cannot be a default value"); }
  31593. };
  31594. pp$9.isSimpleAssignTarget = function(expr) {
  31595. if (expr.type === "ParenthesizedExpression")
  31596. { return this.isSimpleAssignTarget(expr.expression) }
  31597. return expr.type === "Identifier" || expr.type === "MemberExpression"
  31598. };
  31599. var pp$8 = Parser.prototype;
  31600. // ### Statement parsing
  31601. // Parse a program. Initializes the parser, reads any number of
  31602. // statements, and wraps them in a Program node. Optionally takes a
  31603. // `program` argument. If present, the statements will be appended
  31604. // to its body instead of creating a new node.
  31605. pp$8.parseTopLevel = function(node) {
  31606. var exports = Object.create(null);
  31607. if (!node.body) { node.body = []; }
  31608. while (this.type !== types$1.eof) {
  31609. var stmt = this.parseStatement(null, true, exports);
  31610. node.body.push(stmt);
  31611. }
  31612. if (this.inModule)
  31613. { for (var i = 0, list = Object.keys(this.undefinedExports); i < list.length; i += 1)
  31614. {
  31615. var name = list[i];
  31616. this.raiseRecoverable(this.undefinedExports[name].start, ("Export '" + name + "' is not defined"));
  31617. } }
  31618. this.adaptDirectivePrologue(node.body);
  31619. this.next();
  31620. node.sourceType = this.options.sourceType;
  31621. return this.finishNode(node, "Program")
  31622. };
  31623. var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
  31624. pp$8.isLet = function(context) {
  31625. if (this.options.ecmaVersion < 6 || !this.isContextual("let")) { return false }
  31626. skipWhiteSpace.lastIndex = this.pos;
  31627. var skip = skipWhiteSpace.exec(this.input);
  31628. var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
  31629. // For ambiguous cases, determine if a LexicalDeclaration (or only a
  31630. // Statement) is allowed here. If context is not empty then only a Statement
  31631. // is allowed. However, `let [` is an explicit negative lookahead for
  31632. // ExpressionStatement, so special-case it first.
  31633. if (nextCh === 91 || nextCh === 92) { return true } // '[', '/'
  31634. if (context) { return false }
  31635. if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } // '{', astral
  31636. if (isIdentifierStart(nextCh, true)) {
  31637. var pos = next + 1;
  31638. while (isIdentifierChar(nextCh = this.input.charCodeAt(pos), true)) { ++pos; }
  31639. if (nextCh === 92 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true }
  31640. var ident = this.input.slice(next, pos);
  31641. if (!keywordRelationalOperator.test(ident)) { return true }
  31642. }
  31643. return false
  31644. };
  31645. // check 'async [no LineTerminator here] function'
  31646. // - 'async /*foo*/ function' is OK.
  31647. // - 'async /*\n*/ function' is invalid.
  31648. pp$8.isAsyncFunction = function() {
  31649. if (this.options.ecmaVersion < 8 || !this.isContextual("async"))
  31650. { return false }
  31651. skipWhiteSpace.lastIndex = this.pos;
  31652. var skip = skipWhiteSpace.exec(this.input);
  31653. var next = this.pos + skip[0].length, after;
  31654. return !lineBreak.test(this.input.slice(this.pos, next)) &&
  31655. this.input.slice(next, next + 8) === "function" &&
  31656. (next + 8 === this.input.length ||
  31657. !(isIdentifierChar(after = this.input.charCodeAt(next + 8)) || after > 0xd7ff && after < 0xdc00))
  31658. };
  31659. // Parse a single statement.
  31660. //
  31661. // If expecting a statement and finding a slash operator, parse a
  31662. // regular expression literal. This is to handle cases like
  31663. // `if (foo) /blah/.exec(foo)`, where looking at the previous token
  31664. // does not help.
  31665. pp$8.parseStatement = function(context, topLevel, exports) {
  31666. var starttype = this.type, node = this.startNode(), kind;
  31667. if (this.isLet(context)) {
  31668. starttype = types$1._var;
  31669. kind = "let";
  31670. }
  31671. // Most types of statements are recognized by the keyword they
  31672. // start with. Many are trivial to parse, some require a bit of
  31673. // complexity.
  31674. switch (starttype) {
  31675. case types$1._break: case types$1._continue: return this.parseBreakContinueStatement(node, starttype.keyword)
  31676. case types$1._debugger: return this.parseDebuggerStatement(node)
  31677. case types$1._do: return this.parseDoStatement(node)
  31678. case types$1._for: return this.parseForStatement(node)
  31679. case types$1._function:
  31680. // Function as sole body of either an if statement or a labeled statement
  31681. // works, but not when it is part of a labeled statement that is the sole
  31682. // body of an if statement.
  31683. if ((context && (this.strict || context !== "if" && context !== "label")) && this.options.ecmaVersion >= 6) { this.unexpected(); }
  31684. return this.parseFunctionStatement(node, false, !context)
  31685. case types$1._class:
  31686. if (context) { this.unexpected(); }
  31687. return this.parseClass(node, true)
  31688. case types$1._if: return this.parseIfStatement(node)
  31689. case types$1._return: return this.parseReturnStatement(node)
  31690. case types$1._switch: return this.parseSwitchStatement(node)
  31691. case types$1._throw: return this.parseThrowStatement(node)
  31692. case types$1._try: return this.parseTryStatement(node)
  31693. case types$1._const: case types$1._var:
  31694. kind = kind || this.value;
  31695. if (context && kind !== "var") { this.unexpected(); }
  31696. return this.parseVarStatement(node, kind)
  31697. case types$1._while: return this.parseWhileStatement(node)
  31698. case types$1._with: return this.parseWithStatement(node)
  31699. case types$1.braceL: return this.parseBlock(true, node)
  31700. case types$1.semi: return this.parseEmptyStatement(node)
  31701. case types$1._export:
  31702. case types$1._import:
  31703. if (this.options.ecmaVersion > 10 && starttype === types$1._import) {
  31704. skipWhiteSpace.lastIndex = this.pos;
  31705. var skip = skipWhiteSpace.exec(this.input);
  31706. var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
  31707. if (nextCh === 40 || nextCh === 46) // '(' or '.'
  31708. { return this.parseExpressionStatement(node, this.parseExpression()) }
  31709. }
  31710. if (!this.options.allowImportExportEverywhere) {
  31711. if (!topLevel)
  31712. { this.raise(this.start, "'import' and 'export' may only appear at the top level"); }
  31713. if (!this.inModule)
  31714. { this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'"); }
  31715. }
  31716. return starttype === types$1._import ? this.parseImport(node) : this.parseExport(node, exports)
  31717. // If the statement does not start with a statement keyword or a
  31718. // brace, it's an ExpressionStatement or LabeledStatement. We
  31719. // simply start parsing an expression, and afterwards, if the
  31720. // next token is a colon and the expression was a simple
  31721. // Identifier node, we switch to interpreting it as a label.
  31722. default:
  31723. if (this.isAsyncFunction()) {
  31724. if (context) { this.unexpected(); }
  31725. this.next();
  31726. return this.parseFunctionStatement(node, true, !context)
  31727. }
  31728. var maybeName = this.value, expr = this.parseExpression();
  31729. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon))
  31730. { return this.parseLabeledStatement(node, maybeName, expr, context) }
  31731. else { return this.parseExpressionStatement(node, expr) }
  31732. }
  31733. };
  31734. pp$8.parseBreakContinueStatement = function(node, keyword) {
  31735. var isBreak = keyword === "break";
  31736. this.next();
  31737. if (this.eat(types$1.semi) || this.insertSemicolon()) { node.label = null; }
  31738. else if (this.type !== types$1.name) { this.unexpected(); }
  31739. else {
  31740. node.label = this.parseIdent();
  31741. this.semicolon();
  31742. }
  31743. // Verify that there is an actual destination to break or
  31744. // continue to.
  31745. var i = 0;
  31746. for (; i < this.labels.length; ++i) {
  31747. var lab = this.labels[i];
  31748. if (node.label == null || lab.name === node.label.name) {
  31749. if (lab.kind != null && (isBreak || lab.kind === "loop")) { break }
  31750. if (node.label && isBreak) { break }
  31751. }
  31752. }
  31753. if (i === this.labels.length) { this.raise(node.start, "Unsyntactic " + keyword); }
  31754. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
  31755. };
  31756. pp$8.parseDebuggerStatement = function(node) {
  31757. this.next();
  31758. this.semicolon();
  31759. return this.finishNode(node, "DebuggerStatement")
  31760. };
  31761. pp$8.parseDoStatement = function(node) {
  31762. this.next();
  31763. this.labels.push(loopLabel);
  31764. node.body = this.parseStatement("do");
  31765. this.labels.pop();
  31766. this.expect(types$1._while);
  31767. node.test = this.parseParenExpression();
  31768. if (this.options.ecmaVersion >= 6)
  31769. { this.eat(types$1.semi); }
  31770. else
  31771. { this.semicolon(); }
  31772. return this.finishNode(node, "DoWhileStatement")
  31773. };
  31774. // Disambiguating between a `for` and a `for`/`in` or `for`/`of`
  31775. // loop is non-trivial. Basically, we have to parse the init `var`
  31776. // statement or expression, disallowing the `in` operator (see
  31777. // the second parameter to `parseExpression`), and then check
  31778. // whether the next token is `in` or `of`. When there is no init
  31779. // part (semicolon immediately after the opening parenthesis), it
  31780. // is a regular `for` loop.
  31781. pp$8.parseForStatement = function(node) {
  31782. this.next();
  31783. var awaitAt = (this.options.ecmaVersion >= 9 && this.canAwait && this.eatContextual("await")) ? this.lastTokStart : -1;
  31784. this.labels.push(loopLabel);
  31785. this.enterScope(0);
  31786. this.expect(types$1.parenL);
  31787. if (this.type === types$1.semi) {
  31788. if (awaitAt > -1) { this.unexpected(awaitAt); }
  31789. return this.parseFor(node, null)
  31790. }
  31791. var isLet = this.isLet();
  31792. if (this.type === types$1._var || this.type === types$1._const || isLet) {
  31793. var init$1 = this.startNode(), kind = isLet ? "let" : this.value;
  31794. this.next();
  31795. this.parseVar(init$1, true, kind);
  31796. this.finishNode(init$1, "VariableDeclaration");
  31797. if ((this.type === types$1._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init$1.declarations.length === 1) {
  31798. if (this.options.ecmaVersion >= 9) {
  31799. if (this.type === types$1._in) {
  31800. if (awaitAt > -1) { this.unexpected(awaitAt); }
  31801. } else { node.await = awaitAt > -1; }
  31802. }
  31803. return this.parseForIn(node, init$1)
  31804. }
  31805. if (awaitAt > -1) { this.unexpected(awaitAt); }
  31806. return this.parseFor(node, init$1)
  31807. }
  31808. var startsWithLet = this.isContextual("let"), isForOf = false;
  31809. var refDestructuringErrors = new DestructuringErrors;
  31810. var init = this.parseExpression(awaitAt > -1 ? "await" : true, refDestructuringErrors);
  31811. if (this.type === types$1._in || (isForOf = this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
  31812. if (this.options.ecmaVersion >= 9) {
  31813. if (this.type === types$1._in) {
  31814. if (awaitAt > -1) { this.unexpected(awaitAt); }
  31815. } else { node.await = awaitAt > -1; }
  31816. }
  31817. if (startsWithLet && isForOf) { this.raise(init.start, "The left-hand side of a for-of loop may not start with 'let'."); }
  31818. this.toAssignable(init, false, refDestructuringErrors);
  31819. this.checkLValPattern(init);
  31820. return this.parseForIn(node, init)
  31821. } else {
  31822. this.checkExpressionErrors(refDestructuringErrors, true);
  31823. }
  31824. if (awaitAt > -1) { this.unexpected(awaitAt); }
  31825. return this.parseFor(node, init)
  31826. };
  31827. pp$8.parseFunctionStatement = function(node, isAsync, declarationPosition) {
  31828. this.next();
  31829. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), false, isAsync)
  31830. };
  31831. pp$8.parseIfStatement = function(node) {
  31832. this.next();
  31833. node.test = this.parseParenExpression();
  31834. // allow function declarations in branches, but only in non-strict mode
  31835. node.consequent = this.parseStatement("if");
  31836. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  31837. return this.finishNode(node, "IfStatement")
  31838. };
  31839. pp$8.parseReturnStatement = function(node) {
  31840. if (!this.inFunction && !this.options.allowReturnOutsideFunction)
  31841. { this.raise(this.start, "'return' outside of function"); }
  31842. this.next();
  31843. // In `return` (and `break`/`continue`), the keywords with
  31844. // optional arguments, we eagerly look for a semicolon or the
  31845. // possibility to insert one.
  31846. if (this.eat(types$1.semi) || this.insertSemicolon()) { node.argument = null; }
  31847. else { node.argument = this.parseExpression(); this.semicolon(); }
  31848. return this.finishNode(node, "ReturnStatement")
  31849. };
  31850. pp$8.parseSwitchStatement = function(node) {
  31851. this.next();
  31852. node.discriminant = this.parseParenExpression();
  31853. node.cases = [];
  31854. this.expect(types$1.braceL);
  31855. this.labels.push(switchLabel);
  31856. this.enterScope(0);
  31857. // Statements under must be grouped (by label) in SwitchCase
  31858. // nodes. `cur` is used to keep the node that we are currently
  31859. // adding statements to.
  31860. var cur;
  31861. for (var sawDefault = false; this.type !== types$1.braceR;) {
  31862. if (this.type === types$1._case || this.type === types$1._default) {
  31863. var isCase = this.type === types$1._case;
  31864. if (cur) { this.finishNode(cur, "SwitchCase"); }
  31865. node.cases.push(cur = this.startNode());
  31866. cur.consequent = [];
  31867. this.next();
  31868. if (isCase) {
  31869. cur.test = this.parseExpression();
  31870. } else {
  31871. if (sawDefault) { this.raiseRecoverable(this.lastTokStart, "Multiple default clauses"); }
  31872. sawDefault = true;
  31873. cur.test = null;
  31874. }
  31875. this.expect(types$1.colon);
  31876. } else {
  31877. if (!cur) { this.unexpected(); }
  31878. cur.consequent.push(this.parseStatement(null));
  31879. }
  31880. }
  31881. this.exitScope();
  31882. if (cur) { this.finishNode(cur, "SwitchCase"); }
  31883. this.next(); // Closing brace
  31884. this.labels.pop();
  31885. return this.finishNode(node, "SwitchStatement")
  31886. };
  31887. pp$8.parseThrowStatement = function(node) {
  31888. this.next();
  31889. if (lineBreak.test(this.input.slice(this.lastTokEnd, this.start)))
  31890. { this.raise(this.lastTokEnd, "Illegal newline after throw"); }
  31891. node.argument = this.parseExpression();
  31892. this.semicolon();
  31893. return this.finishNode(node, "ThrowStatement")
  31894. };
  31895. // Reused empty array added for node fields that are always empty.
  31896. var empty$1 = [];
  31897. pp$8.parseCatchClauseParam = function() {
  31898. var param = this.parseBindingAtom();
  31899. var simple = param.type === "Identifier";
  31900. this.enterScope(simple ? SCOPE_SIMPLE_CATCH : 0);
  31901. this.checkLValPattern(param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL);
  31902. this.expect(types$1.parenR);
  31903. return param
  31904. };
  31905. pp$8.parseTryStatement = function(node) {
  31906. this.next();
  31907. node.block = this.parseBlock();
  31908. node.handler = null;
  31909. if (this.type === types$1._catch) {
  31910. var clause = this.startNode();
  31911. this.next();
  31912. if (this.eat(types$1.parenL)) {
  31913. clause.param = this.parseCatchClauseParam();
  31914. } else {
  31915. if (this.options.ecmaVersion < 10) { this.unexpected(); }
  31916. clause.param = null;
  31917. this.enterScope(0);
  31918. }
  31919. clause.body = this.parseBlock(false);
  31920. this.exitScope();
  31921. node.handler = this.finishNode(clause, "CatchClause");
  31922. }
  31923. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  31924. if (!node.handler && !node.finalizer)
  31925. { this.raise(node.start, "Missing catch or finally clause"); }
  31926. return this.finishNode(node, "TryStatement")
  31927. };
  31928. pp$8.parseVarStatement = function(node, kind, allowMissingInitializer) {
  31929. this.next();
  31930. this.parseVar(node, false, kind, allowMissingInitializer);
  31931. this.semicolon();
  31932. return this.finishNode(node, "VariableDeclaration")
  31933. };
  31934. pp$8.parseWhileStatement = function(node) {
  31935. this.next();
  31936. node.test = this.parseParenExpression();
  31937. this.labels.push(loopLabel);
  31938. node.body = this.parseStatement("while");
  31939. this.labels.pop();
  31940. return this.finishNode(node, "WhileStatement")
  31941. };
  31942. pp$8.parseWithStatement = function(node) {
  31943. if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
  31944. this.next();
  31945. node.object = this.parseParenExpression();
  31946. node.body = this.parseStatement("with");
  31947. return this.finishNode(node, "WithStatement")
  31948. };
  31949. pp$8.parseEmptyStatement = function(node) {
  31950. this.next();
  31951. return this.finishNode(node, "EmptyStatement")
  31952. };
  31953. pp$8.parseLabeledStatement = function(node, maybeName, expr, context) {
  31954. for (var i$1 = 0, list = this.labels; i$1 < list.length; i$1 += 1)
  31955. {
  31956. var label = list[i$1];
  31957. if (label.name === maybeName)
  31958. { this.raise(expr.start, "Label '" + maybeName + "' is already declared");
  31959. } }
  31960. var kind = this.type.isLoop ? "loop" : this.type === types$1._switch ? "switch" : null;
  31961. for (var i = this.labels.length - 1; i >= 0; i--) {
  31962. var label$1 = this.labels[i];
  31963. if (label$1.statementStart === node.start) {
  31964. // Update information about previous labels on this node
  31965. label$1.statementStart = this.start;
  31966. label$1.kind = kind;
  31967. } else { break }
  31968. }
  31969. this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
  31970. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  31971. this.labels.pop();
  31972. node.label = expr;
  31973. return this.finishNode(node, "LabeledStatement")
  31974. };
  31975. pp$8.parseExpressionStatement = function(node, expr) {
  31976. node.expression = expr;
  31977. this.semicolon();
  31978. return this.finishNode(node, "ExpressionStatement")
  31979. };
  31980. // Parse a semicolon-enclosed block of statements, handling `"use
  31981. // strict"` declarations when `allowStrict` is true (used for
  31982. // function bodies).
  31983. pp$8.parseBlock = function(createNewLexicalScope, node, exitStrict) {
  31984. if ( createNewLexicalScope === void 0 ) createNewLexicalScope = true;
  31985. if ( node === void 0 ) node = this.startNode();
  31986. node.body = [];
  31987. this.expect(types$1.braceL);
  31988. if (createNewLexicalScope) { this.enterScope(0); }
  31989. while (this.type !== types$1.braceR) {
  31990. var stmt = this.parseStatement(null);
  31991. node.body.push(stmt);
  31992. }
  31993. if (exitStrict) { this.strict = false; }
  31994. this.next();
  31995. if (createNewLexicalScope) { this.exitScope(); }
  31996. return this.finishNode(node, "BlockStatement")
  31997. };
  31998. // Parse a regular `for` loop. The disambiguation code in
  31999. // `parseStatement` will already have parsed the init statement or
  32000. // expression.
  32001. pp$8.parseFor = function(node, init) {
  32002. node.init = init;
  32003. this.expect(types$1.semi);
  32004. node.test = this.type === types$1.semi ? null : this.parseExpression();
  32005. this.expect(types$1.semi);
  32006. node.update = this.type === types$1.parenR ? null : this.parseExpression();
  32007. this.expect(types$1.parenR);
  32008. node.body = this.parseStatement("for");
  32009. this.exitScope();
  32010. this.labels.pop();
  32011. return this.finishNode(node, "ForStatement")
  32012. };
  32013. // Parse a `for`/`in` and `for`/`of` loop, which are almost
  32014. // same from parser's perspective.
  32015. pp$8.parseForIn = function(node, init) {
  32016. var isForIn = this.type === types$1._in;
  32017. this.next();
  32018. if (
  32019. init.type === "VariableDeclaration" &&
  32020. init.declarations[0].init != null &&
  32021. (
  32022. !isForIn ||
  32023. this.options.ecmaVersion < 8 ||
  32024. this.strict ||
  32025. init.kind !== "var" ||
  32026. init.declarations[0].id.type !== "Identifier"
  32027. )
  32028. ) {
  32029. this.raise(
  32030. init.start,
  32031. ((isForIn ? "for-in" : "for-of") + " loop variable declaration may not have an initializer")
  32032. );
  32033. }
  32034. node.left = init;
  32035. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
  32036. this.expect(types$1.parenR);
  32037. node.body = this.parseStatement("for");
  32038. this.exitScope();
  32039. this.labels.pop();
  32040. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement")
  32041. };
  32042. // Parse a list of variable declarations.
  32043. pp$8.parseVar = function(node, isFor, kind, allowMissingInitializer) {
  32044. node.declarations = [];
  32045. node.kind = kind;
  32046. for (;;) {
  32047. var decl = this.startNode();
  32048. this.parseVarId(decl, kind);
  32049. if (this.eat(types$1.eq)) {
  32050. decl.init = this.parseMaybeAssign(isFor);
  32051. } else if (!allowMissingInitializer && kind === "const" && !(this.type === types$1._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
  32052. this.unexpected();
  32053. } else if (!allowMissingInitializer && decl.id.type !== "Identifier" && !(isFor && (this.type === types$1._in || this.isContextual("of")))) {
  32054. this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value");
  32055. } else {
  32056. decl.init = null;
  32057. }
  32058. node.declarations.push(this.finishNode(decl, "VariableDeclarator"));
  32059. if (!this.eat(types$1.comma)) { break }
  32060. }
  32061. return node
  32062. };
  32063. pp$8.parseVarId = function(decl, kind) {
  32064. decl.id = this.parseBindingAtom();
  32065. this.checkLValPattern(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false);
  32066. };
  32067. var FUNC_STATEMENT = 1, FUNC_HANGING_STATEMENT = 2, FUNC_NULLABLE_ID = 4;
  32068. // Parse a function declaration or literal (depending on the
  32069. // `statement & FUNC_STATEMENT`).
  32070. // Remove `allowExpressionBody` for 7.0.0, as it is only called with false
  32071. pp$8.parseFunction = function(node, statement, allowExpressionBody, isAsync, forInit) {
  32072. this.initFunction(node);
  32073. if (this.options.ecmaVersion >= 9 || this.options.ecmaVersion >= 6 && !isAsync) {
  32074. if (this.type === types$1.star && (statement & FUNC_HANGING_STATEMENT))
  32075. { this.unexpected(); }
  32076. node.generator = this.eat(types$1.star);
  32077. }
  32078. if (this.options.ecmaVersion >= 8)
  32079. { node.async = !!isAsync; }
  32080. if (statement & FUNC_STATEMENT) {
  32081. node.id = (statement & FUNC_NULLABLE_ID) && this.type !== types$1.name ? null : this.parseIdent();
  32082. if (node.id && !(statement & FUNC_HANGING_STATEMENT))
  32083. // If it is a regular function declaration in sloppy mode, then it is
  32084. // subject to Annex B semantics (BIND_FUNCTION). Otherwise, the binding
  32085. // mode depends on properties of the current scope (see
  32086. // treatFunctionsAsVar).
  32087. { this.checkLValSimple(node.id, (this.strict || node.generator || node.async) ? this.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION); }
  32088. }
  32089. var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
  32090. this.yieldPos = 0;
  32091. this.awaitPos = 0;
  32092. this.awaitIdentPos = 0;
  32093. this.enterScope(functionFlags(node.async, node.generator));
  32094. if (!(statement & FUNC_STATEMENT))
  32095. { node.id = this.type === types$1.name ? this.parseIdent() : null; }
  32096. this.parseFunctionParams(node);
  32097. this.parseFunctionBody(node, allowExpressionBody, false, forInit);
  32098. this.yieldPos = oldYieldPos;
  32099. this.awaitPos = oldAwaitPos;
  32100. this.awaitIdentPos = oldAwaitIdentPos;
  32101. return this.finishNode(node, (statement & FUNC_STATEMENT) ? "FunctionDeclaration" : "FunctionExpression")
  32102. };
  32103. pp$8.parseFunctionParams = function(node) {
  32104. this.expect(types$1.parenL);
  32105. node.params = this.parseBindingList(types$1.parenR, false, this.options.ecmaVersion >= 8);
  32106. this.checkYieldAwaitInDefaultParams();
  32107. };
  32108. // Parse a class declaration or literal (depending on the
  32109. // `isStatement` parameter).
  32110. pp$8.parseClass = function(node, isStatement) {
  32111. this.next();
  32112. // ecma-262 14.6 Class Definitions
  32113. // A class definition is always strict mode code.
  32114. var oldStrict = this.strict;
  32115. this.strict = true;
  32116. this.parseClassId(node, isStatement);
  32117. this.parseClassSuper(node);
  32118. var privateNameMap = this.enterClassBody();
  32119. var classBody = this.startNode();
  32120. var hadConstructor = false;
  32121. classBody.body = [];
  32122. this.expect(types$1.braceL);
  32123. while (this.type !== types$1.braceR) {
  32124. var element = this.parseClassElement(node.superClass !== null);
  32125. if (element) {
  32126. classBody.body.push(element);
  32127. if (element.type === "MethodDefinition" && element.kind === "constructor") {
  32128. if (hadConstructor) { this.raiseRecoverable(element.start, "Duplicate constructor in the same class"); }
  32129. hadConstructor = true;
  32130. } else if (element.key && element.key.type === "PrivateIdentifier" && isPrivateNameConflicted(privateNameMap, element)) {
  32131. this.raiseRecoverable(element.key.start, ("Identifier '#" + (element.key.name) + "' has already been declared"));
  32132. }
  32133. }
  32134. }
  32135. this.strict = oldStrict;
  32136. this.next();
  32137. node.body = this.finishNode(classBody, "ClassBody");
  32138. this.exitClassBody();
  32139. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
  32140. };
  32141. pp$8.parseClassElement = function(constructorAllowsSuper) {
  32142. if (this.eat(types$1.semi)) { return null }
  32143. var ecmaVersion = this.options.ecmaVersion;
  32144. var node = this.startNode();
  32145. var keyName = "";
  32146. var isGenerator = false;
  32147. var isAsync = false;
  32148. var kind = "method";
  32149. var isStatic = false;
  32150. if (this.eatContextual("static")) {
  32151. // Parse static init block
  32152. if (ecmaVersion >= 13 && this.eat(types$1.braceL)) {
  32153. this.parseClassStaticBlock(node);
  32154. return node
  32155. }
  32156. if (this.isClassElementNameStart() || this.type === types$1.star) {
  32157. isStatic = true;
  32158. } else {
  32159. keyName = "static";
  32160. }
  32161. }
  32162. node.static = isStatic;
  32163. if (!keyName && ecmaVersion >= 8 && this.eatContextual("async")) {
  32164. if ((this.isClassElementNameStart() || this.type === types$1.star) && !this.canInsertSemicolon()) {
  32165. isAsync = true;
  32166. } else {
  32167. keyName = "async";
  32168. }
  32169. }
  32170. if (!keyName && (ecmaVersion >= 9 || !isAsync) && this.eat(types$1.star)) {
  32171. isGenerator = true;
  32172. }
  32173. if (!keyName && !isAsync && !isGenerator) {
  32174. var lastValue = this.value;
  32175. if (this.eatContextual("get") || this.eatContextual("set")) {
  32176. if (this.isClassElementNameStart()) {
  32177. kind = lastValue;
  32178. } else {
  32179. keyName = lastValue;
  32180. }
  32181. }
  32182. }
  32183. // Parse element name
  32184. if (keyName) {
  32185. // 'async', 'get', 'set', or 'static' were not a keyword contextually.
  32186. // The last token is any of those. Make it the element name.
  32187. node.computed = false;
  32188. node.key = this.startNodeAt(this.lastTokStart, this.lastTokStartLoc);
  32189. node.key.name = keyName;
  32190. this.finishNode(node.key, "Identifier");
  32191. } else {
  32192. this.parseClassElementName(node);
  32193. }
  32194. // Parse element value
  32195. if (ecmaVersion < 13 || this.type === types$1.parenL || kind !== "method" || isGenerator || isAsync) {
  32196. var isConstructor = !node.static && checkKeyName(node, "constructor");
  32197. var allowsDirectSuper = isConstructor && constructorAllowsSuper;
  32198. // Couldn't move this check into the 'parseClassMethod' method for backward compatibility.
  32199. if (isConstructor && kind !== "method") { this.raise(node.key.start, "Constructor can't have get/set modifier"); }
  32200. node.kind = isConstructor ? "constructor" : kind;
  32201. this.parseClassMethod(node, isGenerator, isAsync, allowsDirectSuper);
  32202. } else {
  32203. this.parseClassField(node);
  32204. }
  32205. return node
  32206. };
  32207. pp$8.isClassElementNameStart = function() {
  32208. return (
  32209. this.type === types$1.name ||
  32210. this.type === types$1.privateId ||
  32211. this.type === types$1.num ||
  32212. this.type === types$1.string ||
  32213. this.type === types$1.bracketL ||
  32214. this.type.keyword
  32215. )
  32216. };
  32217. pp$8.parseClassElementName = function(element) {
  32218. if (this.type === types$1.privateId) {
  32219. if (this.value === "constructor") {
  32220. this.raise(this.start, "Classes can't have an element named '#constructor'");
  32221. }
  32222. element.computed = false;
  32223. element.key = this.parsePrivateIdent();
  32224. } else {
  32225. this.parsePropertyName(element);
  32226. }
  32227. };
  32228. pp$8.parseClassMethod = function(method, isGenerator, isAsync, allowsDirectSuper) {
  32229. // Check key and flags
  32230. var key = method.key;
  32231. if (method.kind === "constructor") {
  32232. if (isGenerator) { this.raise(key.start, "Constructor can't be a generator"); }
  32233. if (isAsync) { this.raise(key.start, "Constructor can't be an async method"); }
  32234. } else if (method.static && checkKeyName(method, "prototype")) {
  32235. this.raise(key.start, "Classes may not have a static property named prototype");
  32236. }
  32237. // Parse value
  32238. var value = method.value = this.parseMethod(isGenerator, isAsync, allowsDirectSuper);
  32239. // Check value
  32240. if (method.kind === "get" && value.params.length !== 0)
  32241. { this.raiseRecoverable(value.start, "getter should have no params"); }
  32242. if (method.kind === "set" && value.params.length !== 1)
  32243. { this.raiseRecoverable(value.start, "setter should have exactly one param"); }
  32244. if (method.kind === "set" && value.params[0].type === "RestElement")
  32245. { this.raiseRecoverable(value.params[0].start, "Setter cannot use rest params"); }
  32246. return this.finishNode(method, "MethodDefinition")
  32247. };
  32248. pp$8.parseClassField = function(field) {
  32249. if (checkKeyName(field, "constructor")) {
  32250. this.raise(field.key.start, "Classes can't have a field named 'constructor'");
  32251. } else if (field.static && checkKeyName(field, "prototype")) {
  32252. this.raise(field.key.start, "Classes can't have a static field named 'prototype'");
  32253. }
  32254. if (this.eat(types$1.eq)) {
  32255. // To raise SyntaxError if 'arguments' exists in the initializer.
  32256. var scope = this.currentThisScope();
  32257. var inClassFieldInit = scope.inClassFieldInit;
  32258. scope.inClassFieldInit = true;
  32259. field.value = this.parseMaybeAssign();
  32260. scope.inClassFieldInit = inClassFieldInit;
  32261. } else {
  32262. field.value = null;
  32263. }
  32264. this.semicolon();
  32265. return this.finishNode(field, "PropertyDefinition")
  32266. };
  32267. pp$8.parseClassStaticBlock = function(node) {
  32268. node.body = [];
  32269. var oldLabels = this.labels;
  32270. this.labels = [];
  32271. this.enterScope(SCOPE_CLASS_STATIC_BLOCK | SCOPE_SUPER);
  32272. while (this.type !== types$1.braceR) {
  32273. var stmt = this.parseStatement(null);
  32274. node.body.push(stmt);
  32275. }
  32276. this.next();
  32277. this.exitScope();
  32278. this.labels = oldLabels;
  32279. return this.finishNode(node, "StaticBlock")
  32280. };
  32281. pp$8.parseClassId = function(node, isStatement) {
  32282. if (this.type === types$1.name) {
  32283. node.id = this.parseIdent();
  32284. if (isStatement)
  32285. { this.checkLValSimple(node.id, BIND_LEXICAL, false); }
  32286. } else {
  32287. if (isStatement === true)
  32288. { this.unexpected(); }
  32289. node.id = null;
  32290. }
  32291. };
  32292. pp$8.parseClassSuper = function(node) {
  32293. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts(null, false) : null;
  32294. };
  32295. pp$8.enterClassBody = function() {
  32296. var element = {declared: Object.create(null), used: []};
  32297. this.privateNameStack.push(element);
  32298. return element.declared
  32299. };
  32300. pp$8.exitClassBody = function() {
  32301. var ref = this.privateNameStack.pop();
  32302. var declared = ref.declared;
  32303. var used = ref.used;
  32304. if (!this.options.checkPrivateFields) { return }
  32305. var len = this.privateNameStack.length;
  32306. var parent = len === 0 ? null : this.privateNameStack[len - 1];
  32307. for (var i = 0; i < used.length; ++i) {
  32308. var id = used[i];
  32309. if (!hasOwn(declared, id.name)) {
  32310. if (parent) {
  32311. parent.used.push(id);
  32312. } else {
  32313. this.raiseRecoverable(id.start, ("Private field '#" + (id.name) + "' must be declared in an enclosing class"));
  32314. }
  32315. }
  32316. }
  32317. };
  32318. function isPrivateNameConflicted(privateNameMap, element) {
  32319. var name = element.key.name;
  32320. var curr = privateNameMap[name];
  32321. var next = "true";
  32322. if (element.type === "MethodDefinition" && (element.kind === "get" || element.kind === "set")) {
  32323. next = (element.static ? "s" : "i") + element.kind;
  32324. }
  32325. // `class { get #a(){}; static set #a(_){} }` is also conflict.
  32326. if (
  32327. curr === "iget" && next === "iset" ||
  32328. curr === "iset" && next === "iget" ||
  32329. curr === "sget" && next === "sset" ||
  32330. curr === "sset" && next === "sget"
  32331. ) {
  32332. privateNameMap[name] = "true";
  32333. return false
  32334. } else if (!curr) {
  32335. privateNameMap[name] = next;
  32336. return false
  32337. } else {
  32338. return true
  32339. }
  32340. }
  32341. function checkKeyName(node, name) {
  32342. var computed = node.computed;
  32343. var key = node.key;
  32344. return !computed && (
  32345. key.type === "Identifier" && key.name === name ||
  32346. key.type === "Literal" && key.value === name
  32347. )
  32348. }
  32349. // Parses module export declaration.
  32350. pp$8.parseExportAllDeclaration = function(node, exports) {
  32351. if (this.options.ecmaVersion >= 11) {
  32352. if (this.eatContextual("as")) {
  32353. node.exported = this.parseModuleExportName();
  32354. this.checkExport(exports, node.exported, this.lastTokStart);
  32355. } else {
  32356. node.exported = null;
  32357. }
  32358. }
  32359. this.expectContextual("from");
  32360. if (this.type !== types$1.string) { this.unexpected(); }
  32361. node.source = this.parseExprAtom();
  32362. this.semicolon();
  32363. return this.finishNode(node, "ExportAllDeclaration")
  32364. };
  32365. pp$8.parseExport = function(node, exports) {
  32366. this.next();
  32367. // export * from '...'
  32368. if (this.eat(types$1.star)) {
  32369. return this.parseExportAllDeclaration(node, exports)
  32370. }
  32371. if (this.eat(types$1._default)) { // export default ...
  32372. this.checkExport(exports, "default", this.lastTokStart);
  32373. node.declaration = this.parseExportDefaultDeclaration();
  32374. return this.finishNode(node, "ExportDefaultDeclaration")
  32375. }
  32376. // export var|const|let|function|class ...
  32377. if (this.shouldParseExportStatement()) {
  32378. node.declaration = this.parseExportDeclaration(node);
  32379. if (node.declaration.type === "VariableDeclaration")
  32380. { this.checkVariableExport(exports, node.declaration.declarations); }
  32381. else
  32382. { this.checkExport(exports, node.declaration.id, node.declaration.id.start); }
  32383. node.specifiers = [];
  32384. node.source = null;
  32385. } else { // export { x, y as z } [from '...']
  32386. node.declaration = null;
  32387. node.specifiers = this.parseExportSpecifiers(exports);
  32388. if (this.eatContextual("from")) {
  32389. if (this.type !== types$1.string) { this.unexpected(); }
  32390. node.source = this.parseExprAtom();
  32391. } else {
  32392. for (var i = 0, list = node.specifiers; i < list.length; i += 1) {
  32393. // check for keywords used as local names
  32394. var spec = list[i];
  32395. this.checkUnreserved(spec.local);
  32396. // check if export is defined
  32397. this.checkLocalExport(spec.local);
  32398. if (spec.local.type === "Literal") {
  32399. this.raise(spec.local.start, "A string literal cannot be used as an exported binding without `from`.");
  32400. }
  32401. }
  32402. node.source = null;
  32403. }
  32404. this.semicolon();
  32405. }
  32406. return this.finishNode(node, "ExportNamedDeclaration")
  32407. };
  32408. pp$8.parseExportDeclaration = function(node) {
  32409. return this.parseStatement(null)
  32410. };
  32411. pp$8.parseExportDefaultDeclaration = function() {
  32412. var isAsync;
  32413. if (this.type === types$1._function || (isAsync = this.isAsyncFunction())) {
  32414. var fNode = this.startNode();
  32415. this.next();
  32416. if (isAsync) { this.next(); }
  32417. return this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync)
  32418. } else if (this.type === types$1._class) {
  32419. var cNode = this.startNode();
  32420. return this.parseClass(cNode, "nullableID")
  32421. } else {
  32422. var declaration = this.parseMaybeAssign();
  32423. this.semicolon();
  32424. return declaration
  32425. }
  32426. };
  32427. pp$8.checkExport = function(exports, name, pos) {
  32428. if (!exports) { return }
  32429. if (typeof name !== "string")
  32430. { name = name.type === "Identifier" ? name.name : name.value; }
  32431. if (hasOwn(exports, name))
  32432. { this.raiseRecoverable(pos, "Duplicate export '" + name + "'"); }
  32433. exports[name] = true;
  32434. };
  32435. pp$8.checkPatternExport = function(exports, pat) {
  32436. var type = pat.type;
  32437. if (type === "Identifier")
  32438. { this.checkExport(exports, pat, pat.start); }
  32439. else if (type === "ObjectPattern")
  32440. { for (var i = 0, list = pat.properties; i < list.length; i += 1)
  32441. {
  32442. var prop = list[i];
  32443. this.checkPatternExport(exports, prop);
  32444. } }
  32445. else if (type === "ArrayPattern")
  32446. { for (var i$1 = 0, list$1 = pat.elements; i$1 < list$1.length; i$1 += 1) {
  32447. var elt = list$1[i$1];
  32448. if (elt) { this.checkPatternExport(exports, elt); }
  32449. } }
  32450. else if (type === "Property")
  32451. { this.checkPatternExport(exports, pat.value); }
  32452. else if (type === "AssignmentPattern")
  32453. { this.checkPatternExport(exports, pat.left); }
  32454. else if (type === "RestElement")
  32455. { this.checkPatternExport(exports, pat.argument); }
  32456. };
  32457. pp$8.checkVariableExport = function(exports, decls) {
  32458. if (!exports) { return }
  32459. for (var i = 0, list = decls; i < list.length; i += 1)
  32460. {
  32461. var decl = list[i];
  32462. this.checkPatternExport(exports, decl.id);
  32463. }
  32464. };
  32465. pp$8.shouldParseExportStatement = function() {
  32466. return this.type.keyword === "var" ||
  32467. this.type.keyword === "const" ||
  32468. this.type.keyword === "class" ||
  32469. this.type.keyword === "function" ||
  32470. this.isLet() ||
  32471. this.isAsyncFunction()
  32472. };
  32473. // Parses a comma-separated list of module exports.
  32474. pp$8.parseExportSpecifier = function(exports) {
  32475. var node = this.startNode();
  32476. node.local = this.parseModuleExportName();
  32477. node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local;
  32478. this.checkExport(
  32479. exports,
  32480. node.exported,
  32481. node.exported.start
  32482. );
  32483. return this.finishNode(node, "ExportSpecifier")
  32484. };
  32485. pp$8.parseExportSpecifiers = function(exports) {
  32486. var nodes = [], first = true;
  32487. // export { x, y as z } [from '...']
  32488. this.expect(types$1.braceL);
  32489. while (!this.eat(types$1.braceR)) {
  32490. if (!first) {
  32491. this.expect(types$1.comma);
  32492. if (this.afterTrailingComma(types$1.braceR)) { break }
  32493. } else { first = false; }
  32494. nodes.push(this.parseExportSpecifier(exports));
  32495. }
  32496. return nodes
  32497. };
  32498. // Parses import declaration.
  32499. pp$8.parseImport = function(node) {
  32500. this.next();
  32501. // import '...'
  32502. if (this.type === types$1.string) {
  32503. node.specifiers = empty$1;
  32504. node.source = this.parseExprAtom();
  32505. } else {
  32506. node.specifiers = this.parseImportSpecifiers();
  32507. this.expectContextual("from");
  32508. node.source = this.type === types$1.string ? this.parseExprAtom() : this.unexpected();
  32509. }
  32510. this.semicolon();
  32511. return this.finishNode(node, "ImportDeclaration")
  32512. };
  32513. // Parses a comma-separated list of module imports.
  32514. pp$8.parseImportSpecifier = function() {
  32515. var node = this.startNode();
  32516. node.imported = this.parseModuleExportName();
  32517. if (this.eatContextual("as")) {
  32518. node.local = this.parseIdent();
  32519. } else {
  32520. this.checkUnreserved(node.imported);
  32521. node.local = node.imported;
  32522. }
  32523. this.checkLValSimple(node.local, BIND_LEXICAL);
  32524. return this.finishNode(node, "ImportSpecifier")
  32525. };
  32526. pp$8.parseImportDefaultSpecifier = function() {
  32527. // import defaultObj, { x, y as z } from '...'
  32528. var node = this.startNode();
  32529. node.local = this.parseIdent();
  32530. this.checkLValSimple(node.local, BIND_LEXICAL);
  32531. return this.finishNode(node, "ImportDefaultSpecifier")
  32532. };
  32533. pp$8.parseImportNamespaceSpecifier = function() {
  32534. var node = this.startNode();
  32535. this.next();
  32536. this.expectContextual("as");
  32537. node.local = this.parseIdent();
  32538. this.checkLValSimple(node.local, BIND_LEXICAL);
  32539. return this.finishNode(node, "ImportNamespaceSpecifier")
  32540. };
  32541. pp$8.parseImportSpecifiers = function() {
  32542. var nodes = [], first = true;
  32543. if (this.type === types$1.name) {
  32544. nodes.push(this.parseImportDefaultSpecifier());
  32545. if (!this.eat(types$1.comma)) { return nodes }
  32546. }
  32547. if (this.type === types$1.star) {
  32548. nodes.push(this.parseImportNamespaceSpecifier());
  32549. return nodes
  32550. }
  32551. this.expect(types$1.braceL);
  32552. while (!this.eat(types$1.braceR)) {
  32553. if (!first) {
  32554. this.expect(types$1.comma);
  32555. if (this.afterTrailingComma(types$1.braceR)) { break }
  32556. } else { first = false; }
  32557. nodes.push(this.parseImportSpecifier());
  32558. }
  32559. return nodes
  32560. };
  32561. pp$8.parseModuleExportName = function() {
  32562. if (this.options.ecmaVersion >= 13 && this.type === types$1.string) {
  32563. var stringLiteral = this.parseLiteral(this.value);
  32564. if (loneSurrogate.test(stringLiteral.value)) {
  32565. this.raise(stringLiteral.start, "An export name cannot include a lone surrogate.");
  32566. }
  32567. return stringLiteral
  32568. }
  32569. return this.parseIdent(true)
  32570. };
  32571. // Set `ExpressionStatement#directive` property for directive prologues.
  32572. pp$8.adaptDirectivePrologue = function(statements) {
  32573. for (var i = 0; i < statements.length && this.isDirectiveCandidate(statements[i]); ++i) {
  32574. statements[i].directive = statements[i].expression.raw.slice(1, -1);
  32575. }
  32576. };
  32577. pp$8.isDirectiveCandidate = function(statement) {
  32578. return (
  32579. this.options.ecmaVersion >= 5 &&
  32580. statement.type === "ExpressionStatement" &&
  32581. statement.expression.type === "Literal" &&
  32582. typeof statement.expression.value === "string" &&
  32583. // Reject parenthesized strings.
  32584. (this.input[statement.start] === "\"" || this.input[statement.start] === "'")
  32585. )
  32586. };
  32587. var pp$7 = Parser.prototype;
  32588. // Convert existing expression atom to assignable pattern
  32589. // if possible.
  32590. pp$7.toAssignable = function(node, isBinding, refDestructuringErrors) {
  32591. if (this.options.ecmaVersion >= 6 && node) {
  32592. switch (node.type) {
  32593. case "Identifier":
  32594. if (this.inAsync && node.name === "await")
  32595. { this.raise(node.start, "Cannot use 'await' as identifier inside an async function"); }
  32596. break
  32597. case "ObjectPattern":
  32598. case "ArrayPattern":
  32599. case "AssignmentPattern":
  32600. case "RestElement":
  32601. break
  32602. case "ObjectExpression":
  32603. node.type = "ObjectPattern";
  32604. if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
  32605. for (var i = 0, list = node.properties; i < list.length; i += 1) {
  32606. var prop = list[i];
  32607. this.toAssignable(prop, isBinding);
  32608. // Early error:
  32609. // AssignmentRestProperty[Yield, Await] :
  32610. // `...` DestructuringAssignmentTarget[Yield, Await]
  32611. //
  32612. // It is a Syntax Error if |DestructuringAssignmentTarget| is an |ArrayLiteral| or an |ObjectLiteral|.
  32613. if (
  32614. prop.type === "RestElement" &&
  32615. (prop.argument.type === "ArrayPattern" || prop.argument.type === "ObjectPattern")
  32616. ) {
  32617. this.raise(prop.argument.start, "Unexpected token");
  32618. }
  32619. }
  32620. break
  32621. case "Property":
  32622. // AssignmentProperty has type === "Property"
  32623. if (node.kind !== "init") { this.raise(node.key.start, "Object pattern can't contain getter or setter"); }
  32624. this.toAssignable(node.value, isBinding);
  32625. break
  32626. case "ArrayExpression":
  32627. node.type = "ArrayPattern";
  32628. if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
  32629. this.toAssignableList(node.elements, isBinding);
  32630. break
  32631. case "SpreadElement":
  32632. node.type = "RestElement";
  32633. this.toAssignable(node.argument, isBinding);
  32634. if (node.argument.type === "AssignmentPattern")
  32635. { this.raise(node.argument.start, "Rest elements cannot have a default value"); }
  32636. break
  32637. case "AssignmentExpression":
  32638. if (node.operator !== "=") { this.raise(node.left.end, "Only '=' operator can be used for specifying default value."); }
  32639. node.type = "AssignmentPattern";
  32640. delete node.operator;
  32641. this.toAssignable(node.left, isBinding);
  32642. break
  32643. case "ParenthesizedExpression":
  32644. this.toAssignable(node.expression, isBinding, refDestructuringErrors);
  32645. break
  32646. case "ChainExpression":
  32647. this.raiseRecoverable(node.start, "Optional chaining cannot appear in left-hand side");
  32648. break
  32649. case "MemberExpression":
  32650. if (!isBinding) { break }
  32651. default:
  32652. this.raise(node.start, "Assigning to rvalue");
  32653. }
  32654. } else if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
  32655. return node
  32656. };
  32657. // Convert list of expression atoms to binding list.
  32658. pp$7.toAssignableList = function(exprList, isBinding) {
  32659. var end = exprList.length;
  32660. for (var i = 0; i < end; i++) {
  32661. var elt = exprList[i];
  32662. if (elt) { this.toAssignable(elt, isBinding); }
  32663. }
  32664. if (end) {
  32665. var last = exprList[end - 1];
  32666. if (this.options.ecmaVersion === 6 && isBinding && last && last.type === "RestElement" && last.argument.type !== "Identifier")
  32667. { this.unexpected(last.argument.start); }
  32668. }
  32669. return exprList
  32670. };
  32671. // Parses spread element.
  32672. pp$7.parseSpread = function(refDestructuringErrors) {
  32673. var node = this.startNode();
  32674. this.next();
  32675. node.argument = this.parseMaybeAssign(false, refDestructuringErrors);
  32676. return this.finishNode(node, "SpreadElement")
  32677. };
  32678. pp$7.parseRestBinding = function() {
  32679. var node = this.startNode();
  32680. this.next();
  32681. // RestElement inside of a function parameter must be an identifier
  32682. if (this.options.ecmaVersion === 6 && this.type !== types$1.name)
  32683. { this.unexpected(); }
  32684. node.argument = this.parseBindingAtom();
  32685. return this.finishNode(node, "RestElement")
  32686. };
  32687. // Parses lvalue (assignable) atom.
  32688. pp$7.parseBindingAtom = function() {
  32689. if (this.options.ecmaVersion >= 6) {
  32690. switch (this.type) {
  32691. case types$1.bracketL:
  32692. var node = this.startNode();
  32693. this.next();
  32694. node.elements = this.parseBindingList(types$1.bracketR, true, true);
  32695. return this.finishNode(node, "ArrayPattern")
  32696. case types$1.braceL:
  32697. return this.parseObj(true)
  32698. }
  32699. }
  32700. return this.parseIdent()
  32701. };
  32702. pp$7.parseBindingList = function(close, allowEmpty, allowTrailingComma, allowModifiers) {
  32703. var elts = [], first = true;
  32704. while (!this.eat(close)) {
  32705. if (first) { first = false; }
  32706. else { this.expect(types$1.comma); }
  32707. if (allowEmpty && this.type === types$1.comma) {
  32708. elts.push(null);
  32709. } else if (allowTrailingComma && this.afterTrailingComma(close)) {
  32710. break
  32711. } else if (this.type === types$1.ellipsis) {
  32712. var rest = this.parseRestBinding();
  32713. this.parseBindingListItem(rest);
  32714. elts.push(rest);
  32715. if (this.type === types$1.comma) { this.raiseRecoverable(this.start, "Comma is not permitted after the rest element"); }
  32716. this.expect(close);
  32717. break
  32718. } else {
  32719. elts.push(this.parseAssignableListItem(allowModifiers));
  32720. }
  32721. }
  32722. return elts
  32723. };
  32724. pp$7.parseAssignableListItem = function(allowModifiers) {
  32725. var elem = this.parseMaybeDefault(this.start, this.startLoc);
  32726. this.parseBindingListItem(elem);
  32727. return elem
  32728. };
  32729. pp$7.parseBindingListItem = function(param) {
  32730. return param
  32731. };
  32732. // Parses assignment pattern around given atom if possible.
  32733. pp$7.parseMaybeDefault = function(startPos, startLoc, left) {
  32734. left = left || this.parseBindingAtom();
  32735. if (this.options.ecmaVersion < 6 || !this.eat(types$1.eq)) { return left }
  32736. var node = this.startNodeAt(startPos, startLoc);
  32737. node.left = left;
  32738. node.right = this.parseMaybeAssign();
  32739. return this.finishNode(node, "AssignmentPattern")
  32740. };
  32741. // The following three functions all verify that a node is an lvalue —
  32742. // something that can be bound, or assigned to. In order to do so, they perform
  32743. // a variety of checks:
  32744. //
  32745. // - Check that none of the bound/assigned-to identifiers are reserved words.
  32746. // - Record name declarations for bindings in the appropriate scope.
  32747. // - Check duplicate argument names, if checkClashes is set.
  32748. //
  32749. // If a complex binding pattern is encountered (e.g., object and array
  32750. // destructuring), the entire pattern is recursively checked.
  32751. //
  32752. // There are three versions of checkLVal*() appropriate for different
  32753. // circumstances:
  32754. //
  32755. // - checkLValSimple() shall be used if the syntactic construct supports
  32756. // nothing other than identifiers and member expressions. Parenthesized
  32757. // expressions are also correctly handled. This is generally appropriate for
  32758. // constructs for which the spec says
  32759. //
  32760. // > It is a Syntax Error if AssignmentTargetType of [the production] is not
  32761. // > simple.
  32762. //
  32763. // It is also appropriate for checking if an identifier is valid and not
  32764. // defined elsewhere, like import declarations or function/class identifiers.
  32765. //
  32766. // Examples where this is used include:
  32767. // a += …;
  32768. // import a from '…';
  32769. // where a is the node to be checked.
  32770. //
  32771. // - checkLValPattern() shall be used if the syntactic construct supports
  32772. // anything checkLValSimple() supports, as well as object and array
  32773. // destructuring patterns. This is generally appropriate for constructs for
  32774. // which the spec says
  32775. //
  32776. // > It is a Syntax Error if [the production] is neither an ObjectLiteral nor
  32777. // > an ArrayLiteral and AssignmentTargetType of [the production] is not
  32778. // > simple.
  32779. //
  32780. // Examples where this is used include:
  32781. // (a = …);
  32782. // const a = …;
  32783. // try { … } catch (a) { … }
  32784. // where a is the node to be checked.
  32785. //
  32786. // - checkLValInnerPattern() shall be used if the syntactic construct supports
  32787. // anything checkLValPattern() supports, as well as default assignment
  32788. // patterns, rest elements, and other constructs that may appear within an
  32789. // object or array destructuring pattern.
  32790. //
  32791. // As a special case, function parameters also use checkLValInnerPattern(),
  32792. // as they also support defaults and rest constructs.
  32793. //
  32794. // These functions deliberately support both assignment and binding constructs,
  32795. // as the logic for both is exceedingly similar. If the node is the target of
  32796. // an assignment, then bindingType should be set to BIND_NONE. Otherwise, it
  32797. // should be set to the appropriate BIND_* constant, like BIND_VAR or
  32798. // BIND_LEXICAL.
  32799. //
  32800. // If the function is called with a non-BIND_NONE bindingType, then
  32801. // additionally a checkClashes object may be specified to allow checking for
  32802. // duplicate argument names. checkClashes is ignored if the provided construct
  32803. // is an assignment (i.e., bindingType is BIND_NONE).
  32804. pp$7.checkLValSimple = function(expr, bindingType, checkClashes) {
  32805. if ( bindingType === void 0 ) bindingType = BIND_NONE;
  32806. var isBind = bindingType !== BIND_NONE;
  32807. switch (expr.type) {
  32808. case "Identifier":
  32809. if (this.strict && this.reservedWordsStrictBind.test(expr.name))
  32810. { this.raiseRecoverable(expr.start, (isBind ? "Binding " : "Assigning to ") + expr.name + " in strict mode"); }
  32811. if (isBind) {
  32812. if (bindingType === BIND_LEXICAL && expr.name === "let")
  32813. { this.raiseRecoverable(expr.start, "let is disallowed as a lexically bound name"); }
  32814. if (checkClashes) {
  32815. if (hasOwn(checkClashes, expr.name))
  32816. { this.raiseRecoverable(expr.start, "Argument name clash"); }
  32817. checkClashes[expr.name] = true;
  32818. }
  32819. if (bindingType !== BIND_OUTSIDE) { this.declareName(expr.name, bindingType, expr.start); }
  32820. }
  32821. break
  32822. case "ChainExpression":
  32823. this.raiseRecoverable(expr.start, "Optional chaining cannot appear in left-hand side");
  32824. break
  32825. case "MemberExpression":
  32826. if (isBind) { this.raiseRecoverable(expr.start, "Binding member expression"); }
  32827. break
  32828. case "ParenthesizedExpression":
  32829. if (isBind) { this.raiseRecoverable(expr.start, "Binding parenthesized expression"); }
  32830. return this.checkLValSimple(expr.expression, bindingType, checkClashes)
  32831. default:
  32832. this.raise(expr.start, (isBind ? "Binding" : "Assigning to") + " rvalue");
  32833. }
  32834. };
  32835. pp$7.checkLValPattern = function(expr, bindingType, checkClashes) {
  32836. if ( bindingType === void 0 ) bindingType = BIND_NONE;
  32837. switch (expr.type) {
  32838. case "ObjectPattern":
  32839. for (var i = 0, list = expr.properties; i < list.length; i += 1) {
  32840. var prop = list[i];
  32841. this.checkLValInnerPattern(prop, bindingType, checkClashes);
  32842. }
  32843. break
  32844. case "ArrayPattern":
  32845. for (var i$1 = 0, list$1 = expr.elements; i$1 < list$1.length; i$1 += 1) {
  32846. var elem = list$1[i$1];
  32847. if (elem) { this.checkLValInnerPattern(elem, bindingType, checkClashes); }
  32848. }
  32849. break
  32850. default:
  32851. this.checkLValSimple(expr, bindingType, checkClashes);
  32852. }
  32853. };
  32854. pp$7.checkLValInnerPattern = function(expr, bindingType, checkClashes) {
  32855. if ( bindingType === void 0 ) bindingType = BIND_NONE;
  32856. switch (expr.type) {
  32857. case "Property":
  32858. // AssignmentProperty has type === "Property"
  32859. this.checkLValInnerPattern(expr.value, bindingType, checkClashes);
  32860. break
  32861. case "AssignmentPattern":
  32862. this.checkLValPattern(expr.left, bindingType, checkClashes);
  32863. break
  32864. case "RestElement":
  32865. this.checkLValPattern(expr.argument, bindingType, checkClashes);
  32866. break
  32867. default:
  32868. this.checkLValPattern(expr, bindingType, checkClashes);
  32869. }
  32870. };
  32871. // The algorithm used to determine whether a regexp can appear at a
  32872. // given point in the program is loosely based on sweet.js' approach.
  32873. // See https://github.com/mozilla/sweet.js/wiki/design
  32874. var TokContext = function TokContext(token, isExpr, preserveSpace, override, generator) {
  32875. this.token = token;
  32876. this.isExpr = !!isExpr;
  32877. this.preserveSpace = !!preserveSpace;
  32878. this.override = override;
  32879. this.generator = !!generator;
  32880. };
  32881. var types = {
  32882. b_stat: new TokContext("{", false),
  32883. b_expr: new TokContext("{", true),
  32884. b_tmpl: new TokContext("${", false),
  32885. p_stat: new TokContext("(", false),
  32886. p_expr: new TokContext("(", true),
  32887. q_tmpl: new TokContext("`", true, true, function (p) { return p.tryReadTemplateToken(); }),
  32888. f_stat: new TokContext("function", false),
  32889. f_expr: new TokContext("function", true),
  32890. f_expr_gen: new TokContext("function", true, false, null, true),
  32891. f_gen: new TokContext("function", false, false, null, true)
  32892. };
  32893. var pp$6 = Parser.prototype;
  32894. pp$6.initialContext = function() {
  32895. return [types.b_stat]
  32896. };
  32897. pp$6.curContext = function() {
  32898. return this.context[this.context.length - 1]
  32899. };
  32900. pp$6.braceIsBlock = function(prevType) {
  32901. var parent = this.curContext();
  32902. if (parent === types.f_expr || parent === types.f_stat)
  32903. { return true }
  32904. if (prevType === types$1.colon && (parent === types.b_stat || parent === types.b_expr))
  32905. { return !parent.isExpr }
  32906. // The check for `tt.name && exprAllowed` detects whether we are
  32907. // after a `yield` or `of` construct. See the `updateContext` for
  32908. // `tt.name`.
  32909. if (prevType === types$1._return || prevType === types$1.name && this.exprAllowed)
  32910. { return lineBreak.test(this.input.slice(this.lastTokEnd, this.start)) }
  32911. if (prevType === types$1._else || prevType === types$1.semi || prevType === types$1.eof || prevType === types$1.parenR || prevType === types$1.arrow)
  32912. { return true }
  32913. if (prevType === types$1.braceL)
  32914. { return parent === types.b_stat }
  32915. if (prevType === types$1._var || prevType === types$1._const || prevType === types$1.name)
  32916. { return false }
  32917. return !this.exprAllowed
  32918. };
  32919. pp$6.inGeneratorContext = function() {
  32920. for (var i = this.context.length - 1; i >= 1; i--) {
  32921. var context = this.context[i];
  32922. if (context.token === "function")
  32923. { return context.generator }
  32924. }
  32925. return false
  32926. };
  32927. pp$6.updateContext = function(prevType) {
  32928. var update, type = this.type;
  32929. if (type.keyword && prevType === types$1.dot)
  32930. { this.exprAllowed = false; }
  32931. else if (update = type.updateContext)
  32932. { update.call(this, prevType); }
  32933. else
  32934. { this.exprAllowed = type.beforeExpr; }
  32935. };
  32936. // Used to handle edge cases when token context could not be inferred correctly during tokenization phase
  32937. pp$6.overrideContext = function(tokenCtx) {
  32938. if (this.curContext() !== tokenCtx) {
  32939. this.context[this.context.length - 1] = tokenCtx;
  32940. }
  32941. };
  32942. // Token-specific context update code
  32943. types$1.parenR.updateContext = types$1.braceR.updateContext = function() {
  32944. if (this.context.length === 1) {
  32945. this.exprAllowed = true;
  32946. return
  32947. }
  32948. var out = this.context.pop();
  32949. if (out === types.b_stat && this.curContext().token === "function") {
  32950. out = this.context.pop();
  32951. }
  32952. this.exprAllowed = !out.isExpr;
  32953. };
  32954. types$1.braceL.updateContext = function(prevType) {
  32955. this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr);
  32956. this.exprAllowed = true;
  32957. };
  32958. types$1.dollarBraceL.updateContext = function() {
  32959. this.context.push(types.b_tmpl);
  32960. this.exprAllowed = true;
  32961. };
  32962. types$1.parenL.updateContext = function(prevType) {
  32963. var statementParens = prevType === types$1._if || prevType === types$1._for || prevType === types$1._with || prevType === types$1._while;
  32964. this.context.push(statementParens ? types.p_stat : types.p_expr);
  32965. this.exprAllowed = true;
  32966. };
  32967. types$1.incDec.updateContext = function() {
  32968. // tokExprAllowed stays unchanged
  32969. };
  32970. types$1._function.updateContext = types$1._class.updateContext = function(prevType) {
  32971. if (prevType.beforeExpr && prevType !== types$1._else &&
  32972. !(prevType === types$1.semi && this.curContext() !== types.p_stat) &&
  32973. !(prevType === types$1._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) &&
  32974. !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat))
  32975. { this.context.push(types.f_expr); }
  32976. else
  32977. { this.context.push(types.f_stat); }
  32978. this.exprAllowed = false;
  32979. };
  32980. types$1.colon.updateContext = function() {
  32981. if (this.curContext().token === "function") { this.context.pop(); }
  32982. this.exprAllowed = true;
  32983. };
  32984. types$1.backQuote.updateContext = function() {
  32985. if (this.curContext() === types.q_tmpl)
  32986. { this.context.pop(); }
  32987. else
  32988. { this.context.push(types.q_tmpl); }
  32989. this.exprAllowed = false;
  32990. };
  32991. types$1.star.updateContext = function(prevType) {
  32992. if (prevType === types$1._function) {
  32993. var index = this.context.length - 1;
  32994. if (this.context[index] === types.f_expr)
  32995. { this.context[index] = types.f_expr_gen; }
  32996. else
  32997. { this.context[index] = types.f_gen; }
  32998. }
  32999. this.exprAllowed = true;
  33000. };
  33001. types$1.name.updateContext = function(prevType) {
  33002. var allowed = false;
  33003. if (this.options.ecmaVersion >= 6 && prevType !== types$1.dot) {
  33004. if (this.value === "of" && !this.exprAllowed ||
  33005. this.value === "yield" && this.inGeneratorContext())
  33006. { allowed = true; }
  33007. }
  33008. this.exprAllowed = allowed;
  33009. };
  33010. // A recursive descent parser operates by defining functions for all
  33011. // syntactic elements, and recursively calling those, each function
  33012. // advancing the input stream and returning an AST node. Precedence
  33013. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  33014. // instead of `(!x)[1]` is handled by the fact that the parser
  33015. // function that parses unary prefix operators is called first, and
  33016. // in turn calls the function that parses `[]` subscripts — that
  33017. // way, it'll receive the node for `x[1]` already parsed, and wraps
  33018. // *that* in the unary operator node.
  33019. //
  33020. // Acorn uses an [operator precedence parser][opp] to handle binary
  33021. // operator precedence, because it is much more compact than using
  33022. // the technique outlined above, which uses different, nesting
  33023. // functions to specify precedence, for all of the ten binary
  33024. // precedence levels that JavaScript defines.
  33025. //
  33026. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  33027. var pp$5 = Parser.prototype;
  33028. // Check if property name clashes with already added.
  33029. // Object/class getters and setters are not allowed to clash —
  33030. // either with each other or with an init property — and in
  33031. // strict mode, init properties are also not allowed to be repeated.
  33032. pp$5.checkPropClash = function(prop, propHash, refDestructuringErrors) {
  33033. if (this.options.ecmaVersion >= 9 && prop.type === "SpreadElement")
  33034. { return }
  33035. if (this.options.ecmaVersion >= 6 && (prop.computed || prop.method || prop.shorthand))
  33036. { return }
  33037. var key = prop.key;
  33038. var name;
  33039. switch (key.type) {
  33040. case "Identifier": name = key.name; break
  33041. case "Literal": name = String(key.value); break
  33042. default: return
  33043. }
  33044. var kind = prop.kind;
  33045. if (this.options.ecmaVersion >= 6) {
  33046. if (name === "__proto__" && kind === "init") {
  33047. if (propHash.proto) {
  33048. if (refDestructuringErrors) {
  33049. if (refDestructuringErrors.doubleProto < 0) {
  33050. refDestructuringErrors.doubleProto = key.start;
  33051. }
  33052. } else {
  33053. this.raiseRecoverable(key.start, "Redefinition of __proto__ property");
  33054. }
  33055. }
  33056. propHash.proto = true;
  33057. }
  33058. return
  33059. }
  33060. name = "$" + name;
  33061. var other = propHash[name];
  33062. if (other) {
  33063. var redefinition;
  33064. if (kind === "init") {
  33065. redefinition = this.strict && other.init || other.get || other.set;
  33066. } else {
  33067. redefinition = other.init || other[kind];
  33068. }
  33069. if (redefinition)
  33070. { this.raiseRecoverable(key.start, "Redefinition of property"); }
  33071. } else {
  33072. other = propHash[name] = {
  33073. init: false,
  33074. get: false,
  33075. set: false
  33076. };
  33077. }
  33078. other[kind] = true;
  33079. };
  33080. // ### Expression parsing
  33081. // These nest, from the most general expression type at the top to
  33082. // 'atomic', nondivisible expression types at the bottom. Most of
  33083. // the functions will simply let the function(s) below them parse,
  33084. // and, *if* the syntactic construct they handle is present, wrap
  33085. // the AST node that the inner parser gave them in another node.
  33086. // Parse a full expression. The optional arguments are used to
  33087. // forbid the `in` operator (in for loops initalization expressions)
  33088. // and provide reference for storing '=' operator inside shorthand
  33089. // property assignment in contexts where both object expression
  33090. // and object pattern might appear (so it's possible to raise
  33091. // delayed syntax error at correct position).
  33092. pp$5.parseExpression = function(forInit, refDestructuringErrors) {
  33093. var startPos = this.start, startLoc = this.startLoc;
  33094. var expr = this.parseMaybeAssign(forInit, refDestructuringErrors);
  33095. if (this.type === types$1.comma) {
  33096. var node = this.startNodeAt(startPos, startLoc);
  33097. node.expressions = [expr];
  33098. while (this.eat(types$1.comma)) { node.expressions.push(this.parseMaybeAssign(forInit, refDestructuringErrors)); }
  33099. return this.finishNode(node, "SequenceExpression")
  33100. }
  33101. return expr
  33102. };
  33103. // Parse an assignment expression. This includes applications of
  33104. // operators like `+=`.
  33105. pp$5.parseMaybeAssign = function(forInit, refDestructuringErrors, afterLeftParse) {
  33106. if (this.isContextual("yield")) {
  33107. if (this.inGenerator) { return this.parseYield(forInit) }
  33108. // The tokenizer will assume an expression is allowed after
  33109. // `yield`, but this isn't that kind of yield
  33110. else { this.exprAllowed = false; }
  33111. }
  33112. var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldDoubleProto = -1;
  33113. if (refDestructuringErrors) {
  33114. oldParenAssign = refDestructuringErrors.parenthesizedAssign;
  33115. oldTrailingComma = refDestructuringErrors.trailingComma;
  33116. oldDoubleProto = refDestructuringErrors.doubleProto;
  33117. refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1;
  33118. } else {
  33119. refDestructuringErrors = new DestructuringErrors;
  33120. ownDestructuringErrors = true;
  33121. }
  33122. var startPos = this.start, startLoc = this.startLoc;
  33123. if (this.type === types$1.parenL || this.type === types$1.name) {
  33124. this.potentialArrowAt = this.start;
  33125. this.potentialArrowInForAwait = forInit === "await";
  33126. }
  33127. var left = this.parseMaybeConditional(forInit, refDestructuringErrors);
  33128. if (afterLeftParse) { left = afterLeftParse.call(this, left, startPos, startLoc); }
  33129. if (this.type.isAssign) {
  33130. var node = this.startNodeAt(startPos, startLoc);
  33131. node.operator = this.value;
  33132. if (this.type === types$1.eq)
  33133. { left = this.toAssignable(left, false, refDestructuringErrors); }
  33134. if (!ownDestructuringErrors) {
  33135. refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.doubleProto = -1;
  33136. }
  33137. if (refDestructuringErrors.shorthandAssign >= left.start)
  33138. { refDestructuringErrors.shorthandAssign = -1; } // reset because shorthand default was used correctly
  33139. if (this.type === types$1.eq)
  33140. { this.checkLValPattern(left); }
  33141. else
  33142. { this.checkLValSimple(left); }
  33143. node.left = left;
  33144. this.next();
  33145. node.right = this.parseMaybeAssign(forInit);
  33146. if (oldDoubleProto > -1) { refDestructuringErrors.doubleProto = oldDoubleProto; }
  33147. return this.finishNode(node, "AssignmentExpression")
  33148. } else {
  33149. if (ownDestructuringErrors) { this.checkExpressionErrors(refDestructuringErrors, true); }
  33150. }
  33151. if (oldParenAssign > -1) { refDestructuringErrors.parenthesizedAssign = oldParenAssign; }
  33152. if (oldTrailingComma > -1) { refDestructuringErrors.trailingComma = oldTrailingComma; }
  33153. return left
  33154. };
  33155. // Parse a ternary conditional (`?:`) operator.
  33156. pp$5.parseMaybeConditional = function(forInit, refDestructuringErrors) {
  33157. var startPos = this.start, startLoc = this.startLoc;
  33158. var expr = this.parseExprOps(forInit, refDestructuringErrors);
  33159. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  33160. if (this.eat(types$1.question)) {
  33161. var node = this.startNodeAt(startPos, startLoc);
  33162. node.test = expr;
  33163. node.consequent = this.parseMaybeAssign();
  33164. this.expect(types$1.colon);
  33165. node.alternate = this.parseMaybeAssign(forInit);
  33166. return this.finishNode(node, "ConditionalExpression")
  33167. }
  33168. return expr
  33169. };
  33170. // Start the precedence parser.
  33171. pp$5.parseExprOps = function(forInit, refDestructuringErrors) {
  33172. var startPos = this.start, startLoc = this.startLoc;
  33173. var expr = this.parseMaybeUnary(refDestructuringErrors, false, false, forInit);
  33174. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  33175. return expr.start === startPos && expr.type === "ArrowFunctionExpression" ? expr : this.parseExprOp(expr, startPos, startLoc, -1, forInit)
  33176. };
  33177. // Parse binary operators with the operator precedence parsing
  33178. // algorithm. `left` is the left-hand side of the operator.
  33179. // `minPrec` provides context that allows the function to stop and
  33180. // defer further parser to one of its callers when it encounters an
  33181. // operator that has a lower precedence than the set it is parsing.
  33182. pp$5.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, forInit) {
  33183. var prec = this.type.binop;
  33184. if (prec != null && (!forInit || this.type !== types$1._in)) {
  33185. if (prec > minPrec) {
  33186. var logical = this.type === types$1.logicalOR || this.type === types$1.logicalAND;
  33187. var coalesce = this.type === types$1.coalesce;
  33188. if (coalesce) {
  33189. // Handle the precedence of `tt.coalesce` as equal to the range of logical expressions.
  33190. // In other words, `node.right` shouldn't contain logical expressions in order to check the mixed error.
  33191. prec = types$1.logicalAND.binop;
  33192. }
  33193. var op = this.value;
  33194. this.next();
  33195. var startPos = this.start, startLoc = this.startLoc;
  33196. var right = this.parseExprOp(this.parseMaybeUnary(null, false, false, forInit), startPos, startLoc, prec, forInit);
  33197. var node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical || coalesce);
  33198. if ((logical && this.type === types$1.coalesce) || (coalesce && (this.type === types$1.logicalOR || this.type === types$1.logicalAND))) {
  33199. this.raiseRecoverable(this.start, "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses");
  33200. }
  33201. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, forInit)
  33202. }
  33203. }
  33204. return left
  33205. };
  33206. pp$5.buildBinary = function(startPos, startLoc, left, right, op, logical) {
  33207. if (right.type === "PrivateIdentifier") { this.raise(right.start, "Private identifier can only be left side of binary expression"); }
  33208. var node = this.startNodeAt(startPos, startLoc);
  33209. node.left = left;
  33210. node.operator = op;
  33211. node.right = right;
  33212. return this.finishNode(node, logical ? "LogicalExpression" : "BinaryExpression")
  33213. };
  33214. // Parse unary operators, both prefix and postfix.
  33215. pp$5.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit) {
  33216. var startPos = this.start, startLoc = this.startLoc, expr;
  33217. if (this.isContextual("await") && this.canAwait) {
  33218. expr = this.parseAwait(forInit);
  33219. sawUnary = true;
  33220. } else if (this.type.prefix) {
  33221. var node = this.startNode(), update = this.type === types$1.incDec;
  33222. node.operator = this.value;
  33223. node.prefix = true;
  33224. this.next();
  33225. node.argument = this.parseMaybeUnary(null, true, update, forInit);
  33226. this.checkExpressionErrors(refDestructuringErrors, true);
  33227. if (update) { this.checkLValSimple(node.argument); }
  33228. else if (this.strict && node.operator === "delete" &&
  33229. node.argument.type === "Identifier")
  33230. { this.raiseRecoverable(node.start, "Deleting local variable in strict mode"); }
  33231. else if (node.operator === "delete" && isPrivateFieldAccess(node.argument))
  33232. { this.raiseRecoverable(node.start, "Private fields can not be deleted"); }
  33233. else { sawUnary = true; }
  33234. expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  33235. } else if (!sawUnary && this.type === types$1.privateId) {
  33236. if ((forInit || this.privateNameStack.length === 0) && this.options.checkPrivateFields) { this.unexpected(); }
  33237. expr = this.parsePrivateIdent();
  33238. // only could be private fields in 'in', such as #x in obj
  33239. if (this.type !== types$1._in) { this.unexpected(); }
  33240. } else {
  33241. expr = this.parseExprSubscripts(refDestructuringErrors, forInit);
  33242. if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
  33243. while (this.type.postfix && !this.canInsertSemicolon()) {
  33244. var node$1 = this.startNodeAt(startPos, startLoc);
  33245. node$1.operator = this.value;
  33246. node$1.prefix = false;
  33247. node$1.argument = expr;
  33248. this.checkLValSimple(expr);
  33249. this.next();
  33250. expr = this.finishNode(node$1, "UpdateExpression");
  33251. }
  33252. }
  33253. if (!incDec && this.eat(types$1.starstar)) {
  33254. if (sawUnary)
  33255. { this.unexpected(this.lastTokStart); }
  33256. else
  33257. { return this.buildBinary(startPos, startLoc, expr, this.parseMaybeUnary(null, false, false, forInit), "**", false) }
  33258. } else {
  33259. return expr
  33260. }
  33261. };
  33262. function isPrivateFieldAccess(node) {
  33263. return (
  33264. node.type === "MemberExpression" && node.property.type === "PrivateIdentifier" ||
  33265. node.type === "ChainExpression" && isPrivateFieldAccess(node.expression)
  33266. )
  33267. }
  33268. // Parse call, dot, and `[]`-subscript expressions.
  33269. pp$5.parseExprSubscripts = function(refDestructuringErrors, forInit) {
  33270. var startPos = this.start, startLoc = this.startLoc;
  33271. var expr = this.parseExprAtom(refDestructuringErrors, forInit);
  33272. if (expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")")
  33273. { return expr }
  33274. var result = this.parseSubscripts(expr, startPos, startLoc, false, forInit);
  33275. if (refDestructuringErrors && result.type === "MemberExpression") {
  33276. if (refDestructuringErrors.parenthesizedAssign >= result.start) { refDestructuringErrors.parenthesizedAssign = -1; }
  33277. if (refDestructuringErrors.parenthesizedBind >= result.start) { refDestructuringErrors.parenthesizedBind = -1; }
  33278. if (refDestructuringErrors.trailingComma >= result.start) { refDestructuringErrors.trailingComma = -1; }
  33279. }
  33280. return result
  33281. };
  33282. pp$5.parseSubscripts = function(base, startPos, startLoc, noCalls, forInit) {
  33283. var maybeAsyncArrow = this.options.ecmaVersion >= 8 && base.type === "Identifier" && base.name === "async" &&
  33284. this.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 &&
  33285. this.potentialArrowAt === base.start;
  33286. var optionalChained = false;
  33287. while (true) {
  33288. var element = this.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit);
  33289. if (element.optional) { optionalChained = true; }
  33290. if (element === base || element.type === "ArrowFunctionExpression") {
  33291. if (optionalChained) {
  33292. var chainNode = this.startNodeAt(startPos, startLoc);
  33293. chainNode.expression = element;
  33294. element = this.finishNode(chainNode, "ChainExpression");
  33295. }
  33296. return element
  33297. }
  33298. base = element;
  33299. }
  33300. };
  33301. pp$5.shouldParseAsyncArrow = function() {
  33302. return !this.canInsertSemicolon() && this.eat(types$1.arrow)
  33303. };
  33304. pp$5.parseSubscriptAsyncArrow = function(startPos, startLoc, exprList, forInit) {
  33305. return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList, true, forInit)
  33306. };
  33307. pp$5.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
  33308. var optionalSupported = this.options.ecmaVersion >= 11;
  33309. var optional = optionalSupported && this.eat(types$1.questionDot);
  33310. if (noCalls && optional) { this.raise(this.lastTokStart, "Optional chaining cannot appear in the callee of new expressions"); }
  33311. var computed = this.eat(types$1.bracketL);
  33312. if (computed || (optional && this.type !== types$1.parenL && this.type !== types$1.backQuote) || this.eat(types$1.dot)) {
  33313. var node = this.startNodeAt(startPos, startLoc);
  33314. node.object = base;
  33315. if (computed) {
  33316. node.property = this.parseExpression();
  33317. this.expect(types$1.bracketR);
  33318. } else if (this.type === types$1.privateId && base.type !== "Super") {
  33319. node.property = this.parsePrivateIdent();
  33320. } else {
  33321. node.property = this.parseIdent(this.options.allowReserved !== "never");
  33322. }
  33323. node.computed = !!computed;
  33324. if (optionalSupported) {
  33325. node.optional = optional;
  33326. }
  33327. base = this.finishNode(node, "MemberExpression");
  33328. } else if (!noCalls && this.eat(types$1.parenL)) {
  33329. var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
  33330. this.yieldPos = 0;
  33331. this.awaitPos = 0;
  33332. this.awaitIdentPos = 0;
  33333. var exprList = this.parseExprList(types$1.parenR, this.options.ecmaVersion >= 8, false, refDestructuringErrors);
  33334. if (maybeAsyncArrow && !optional && this.shouldParseAsyncArrow()) {
  33335. this.checkPatternErrors(refDestructuringErrors, false);
  33336. this.checkYieldAwaitInDefaultParams();
  33337. if (this.awaitIdentPos > 0)
  33338. { this.raise(this.awaitIdentPos, "Cannot use 'await' as identifier inside an async function"); }
  33339. this.yieldPos = oldYieldPos;
  33340. this.awaitPos = oldAwaitPos;
  33341. this.awaitIdentPos = oldAwaitIdentPos;
  33342. return this.parseSubscriptAsyncArrow(startPos, startLoc, exprList, forInit)
  33343. }
  33344. this.checkExpressionErrors(refDestructuringErrors, true);
  33345. this.yieldPos = oldYieldPos || this.yieldPos;
  33346. this.awaitPos = oldAwaitPos || this.awaitPos;
  33347. this.awaitIdentPos = oldAwaitIdentPos || this.awaitIdentPos;
  33348. var node$1 = this.startNodeAt(startPos, startLoc);
  33349. node$1.callee = base;
  33350. node$1.arguments = exprList;
  33351. if (optionalSupported) {
  33352. node$1.optional = optional;
  33353. }
  33354. base = this.finishNode(node$1, "CallExpression");
  33355. } else if (this.type === types$1.backQuote) {
  33356. if (optional || optionalChained) {
  33357. this.raise(this.start, "Optional chaining cannot appear in the tag of tagged template expressions");
  33358. }
  33359. var node$2 = this.startNodeAt(startPos, startLoc);
  33360. node$2.tag = base;
  33361. node$2.quasi = this.parseTemplate({isTagged: true});
  33362. base = this.finishNode(node$2, "TaggedTemplateExpression");
  33363. }
  33364. return base
  33365. };
  33366. // Parse an atomic expression — either a single token that is an
  33367. // expression, an expression started by a keyword like `function` or
  33368. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  33369. // or `{}`.
  33370. pp$5.parseExprAtom = function(refDestructuringErrors, forInit, forNew) {
  33371. // If a division operator appears in an expression position, the
  33372. // tokenizer got confused, and we force it to read a regexp instead.
  33373. if (this.type === types$1.slash) { this.readRegexp(); }
  33374. var node, canBeArrow = this.potentialArrowAt === this.start;
  33375. switch (this.type) {
  33376. case types$1._super:
  33377. if (!this.allowSuper)
  33378. { this.raise(this.start, "'super' keyword outside a method"); }
  33379. node = this.startNode();
  33380. this.next();
  33381. if (this.type === types$1.parenL && !this.allowDirectSuper)
  33382. { this.raise(node.start, "super() call outside constructor of a subclass"); }
  33383. // The `super` keyword can appear at below:
  33384. // SuperProperty:
  33385. // super [ Expression ]
  33386. // super . IdentifierName
  33387. // SuperCall:
  33388. // super ( Arguments )
  33389. if (this.type !== types$1.dot && this.type !== types$1.bracketL && this.type !== types$1.parenL)
  33390. { this.unexpected(); }
  33391. return this.finishNode(node, "Super")
  33392. case types$1._this:
  33393. node = this.startNode();
  33394. this.next();
  33395. return this.finishNode(node, "ThisExpression")
  33396. case types$1.name:
  33397. var startPos = this.start, startLoc = this.startLoc, containsEsc = this.containsEsc;
  33398. var id = this.parseIdent(false);
  33399. if (this.options.ecmaVersion >= 8 && !containsEsc && id.name === "async" && !this.canInsertSemicolon() && this.eat(types$1._function)) {
  33400. this.overrideContext(types.f_expr);
  33401. return this.parseFunction(this.startNodeAt(startPos, startLoc), 0, false, true, forInit)
  33402. }
  33403. if (canBeArrow && !this.canInsertSemicolon()) {
  33404. if (this.eat(types$1.arrow))
  33405. { return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false, forInit) }
  33406. if (this.options.ecmaVersion >= 8 && id.name === "async" && this.type === types$1.name && !containsEsc &&
  33407. (!this.potentialArrowInForAwait || this.value !== "of" || this.containsEsc)) {
  33408. id = this.parseIdent(false);
  33409. if (this.canInsertSemicolon() || !this.eat(types$1.arrow))
  33410. { this.unexpected(); }
  33411. return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], true, forInit)
  33412. }
  33413. }
  33414. return id
  33415. case types$1.regexp:
  33416. var value = this.value;
  33417. node = this.parseLiteral(value.value);
  33418. node.regex = {pattern: value.pattern, flags: value.flags};
  33419. return node
  33420. case types$1.num: case types$1.string:
  33421. return this.parseLiteral(this.value)
  33422. case types$1._null: case types$1._true: case types$1._false:
  33423. node = this.startNode();
  33424. node.value = this.type === types$1._null ? null : this.type === types$1._true;
  33425. node.raw = this.type.keyword;
  33426. this.next();
  33427. return this.finishNode(node, "Literal")
  33428. case types$1.parenL:
  33429. var start = this.start, expr = this.parseParenAndDistinguishExpression(canBeArrow, forInit);
  33430. if (refDestructuringErrors) {
  33431. if (refDestructuringErrors.parenthesizedAssign < 0 && !this.isSimpleAssignTarget(expr))
  33432. { refDestructuringErrors.parenthesizedAssign = start; }
  33433. if (refDestructuringErrors.parenthesizedBind < 0)
  33434. { refDestructuringErrors.parenthesizedBind = start; }
  33435. }
  33436. return expr
  33437. case types$1.bracketL:
  33438. node = this.startNode();
  33439. this.next();
  33440. node.elements = this.parseExprList(types$1.bracketR, true, true, refDestructuringErrors);
  33441. return this.finishNode(node, "ArrayExpression")
  33442. case types$1.braceL:
  33443. this.overrideContext(types.b_expr);
  33444. return this.parseObj(false, refDestructuringErrors)
  33445. case types$1._function:
  33446. node = this.startNode();
  33447. this.next();
  33448. return this.parseFunction(node, 0)
  33449. case types$1._class:
  33450. return this.parseClass(this.startNode(), false)
  33451. case types$1._new:
  33452. return this.parseNew()
  33453. case types$1.backQuote:
  33454. return this.parseTemplate()
  33455. case types$1._import:
  33456. if (this.options.ecmaVersion >= 11) {
  33457. return this.parseExprImport(forNew)
  33458. } else {
  33459. return this.unexpected()
  33460. }
  33461. default:
  33462. return this.parseExprAtomDefault()
  33463. }
  33464. };
  33465. pp$5.parseExprAtomDefault = function() {
  33466. this.unexpected();
  33467. };
  33468. pp$5.parseExprImport = function(forNew) {
  33469. var node = this.startNode();
  33470. // Consume `import` as an identifier for `import.meta`.
  33471. // Because `this.parseIdent(true)` doesn't check escape sequences, it needs the check of `this.containsEsc`.
  33472. if (this.containsEsc) { this.raiseRecoverable(this.start, "Escape sequence in keyword import"); }
  33473. this.next();
  33474. if (this.type === types$1.parenL && !forNew) {
  33475. return this.parseDynamicImport(node)
  33476. } else if (this.type === types$1.dot) {
  33477. var meta = this.startNodeAt(node.start, node.loc && node.loc.start);
  33478. meta.name = "import";
  33479. node.meta = this.finishNode(meta, "Identifier");
  33480. return this.parseImportMeta(node)
  33481. } else {
  33482. this.unexpected();
  33483. }
  33484. };
  33485. pp$5.parseDynamicImport = function(node) {
  33486. this.next(); // skip `(`
  33487. // Parse node.source.
  33488. node.source = this.parseMaybeAssign();
  33489. // Verify ending.
  33490. if (!this.eat(types$1.parenR)) {
  33491. var errorPos = this.start;
  33492. if (this.eat(types$1.comma) && this.eat(types$1.parenR)) {
  33493. this.raiseRecoverable(errorPos, "Trailing comma is not allowed in import()");
  33494. } else {
  33495. this.unexpected(errorPos);
  33496. }
  33497. }
  33498. return this.finishNode(node, "ImportExpression")
  33499. };
  33500. pp$5.parseImportMeta = function(node) {
  33501. this.next(); // skip `.`
  33502. var containsEsc = this.containsEsc;
  33503. node.property = this.parseIdent(true);
  33504. if (node.property.name !== "meta")
  33505. { this.raiseRecoverable(node.property.start, "The only valid meta property for import is 'import.meta'"); }
  33506. if (containsEsc)
  33507. { this.raiseRecoverable(node.start, "'import.meta' must not contain escaped characters"); }
  33508. if (this.options.sourceType !== "module" && !this.options.allowImportExportEverywhere)
  33509. { this.raiseRecoverable(node.start, "Cannot use 'import.meta' outside a module"); }
  33510. return this.finishNode(node, "MetaProperty")
  33511. };
  33512. pp$5.parseLiteral = function(value) {
  33513. var node = this.startNode();
  33514. node.value = value;
  33515. node.raw = this.input.slice(this.start, this.end);
  33516. if (node.raw.charCodeAt(node.raw.length - 1) === 110) { node.bigint = node.raw.slice(0, -1).replace(/_/g, ""); }
  33517. this.next();
  33518. return this.finishNode(node, "Literal")
  33519. };
  33520. pp$5.parseParenExpression = function() {
  33521. this.expect(types$1.parenL);
  33522. var val = this.parseExpression();
  33523. this.expect(types$1.parenR);
  33524. return val
  33525. };
  33526. pp$5.shouldParseArrow = function(exprList) {
  33527. return !this.canInsertSemicolon()
  33528. };
  33529. pp$5.parseParenAndDistinguishExpression = function(canBeArrow, forInit) {
  33530. var startPos = this.start, startLoc = this.startLoc, val, allowTrailingComma = this.options.ecmaVersion >= 8;
  33531. if (this.options.ecmaVersion >= 6) {
  33532. this.next();
  33533. var innerStartPos = this.start, innerStartLoc = this.startLoc;
  33534. var exprList = [], first = true, lastIsComma = false;
  33535. var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, spreadStart;
  33536. this.yieldPos = 0;
  33537. this.awaitPos = 0;
  33538. // Do not save awaitIdentPos to allow checking awaits nested in parameters
  33539. while (this.type !== types$1.parenR) {
  33540. first ? first = false : this.expect(types$1.comma);
  33541. if (allowTrailingComma && this.afterTrailingComma(types$1.parenR, true)) {
  33542. lastIsComma = true;
  33543. break
  33544. } else if (this.type === types$1.ellipsis) {
  33545. spreadStart = this.start;
  33546. exprList.push(this.parseParenItem(this.parseRestBinding()));
  33547. if (this.type === types$1.comma) {
  33548. this.raiseRecoverable(
  33549. this.start,
  33550. "Comma is not permitted after the rest element"
  33551. );
  33552. }
  33553. break
  33554. } else {
  33555. exprList.push(this.parseMaybeAssign(false, refDestructuringErrors, this.parseParenItem));
  33556. }
  33557. }
  33558. var innerEndPos = this.lastTokEnd, innerEndLoc = this.lastTokEndLoc;
  33559. this.expect(types$1.parenR);
  33560. if (canBeArrow && this.shouldParseArrow(exprList) && this.eat(types$1.arrow)) {
  33561. this.checkPatternErrors(refDestructuringErrors, false);
  33562. this.checkYieldAwaitInDefaultParams();
  33563. this.yieldPos = oldYieldPos;
  33564. this.awaitPos = oldAwaitPos;
  33565. return this.parseParenArrowList(startPos, startLoc, exprList, forInit)
  33566. }
  33567. if (!exprList.length || lastIsComma) { this.unexpected(this.lastTokStart); }
  33568. if (spreadStart) { this.unexpected(spreadStart); }
  33569. this.checkExpressionErrors(refDestructuringErrors, true);
  33570. this.yieldPos = oldYieldPos || this.yieldPos;
  33571. this.awaitPos = oldAwaitPos || this.awaitPos;
  33572. if (exprList.length > 1) {
  33573. val = this.startNodeAt(innerStartPos, innerStartLoc);
  33574. val.expressions = exprList;
  33575. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  33576. } else {
  33577. val = exprList[0];
  33578. }
  33579. } else {
  33580. val = this.parseParenExpression();
  33581. }
  33582. if (this.options.preserveParens) {
  33583. var par = this.startNodeAt(startPos, startLoc);
  33584. par.expression = val;
  33585. return this.finishNode(par, "ParenthesizedExpression")
  33586. } else {
  33587. return val
  33588. }
  33589. };
  33590. pp$5.parseParenItem = function(item) {
  33591. return item
  33592. };
  33593. pp$5.parseParenArrowList = function(startPos, startLoc, exprList, forInit) {
  33594. return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList, false, forInit)
  33595. };
  33596. // New's precedence is slightly tricky. It must allow its argument to
  33597. // be a `[]` or dot subscript expression, but not a call — at least,
  33598. // not without wrapping it in parentheses. Thus, it uses the noCalls
  33599. // argument to parseSubscripts to prevent it from consuming the
  33600. // argument list.
  33601. var empty = [];
  33602. pp$5.parseNew = function() {
  33603. if (this.containsEsc) { this.raiseRecoverable(this.start, "Escape sequence in keyword new"); }
  33604. var node = this.startNode();
  33605. this.next();
  33606. if (this.options.ecmaVersion >= 6 && this.type === types$1.dot) {
  33607. var meta = this.startNodeAt(node.start, node.loc && node.loc.start);
  33608. meta.name = "new";
  33609. node.meta = this.finishNode(meta, "Identifier");
  33610. this.next();
  33611. var containsEsc = this.containsEsc;
  33612. node.property = this.parseIdent(true);
  33613. if (node.property.name !== "target")
  33614. { this.raiseRecoverable(node.property.start, "The only valid meta property for new is 'new.target'"); }
  33615. if (containsEsc)
  33616. { this.raiseRecoverable(node.start, "'new.target' must not contain escaped characters"); }
  33617. if (!this.allowNewDotTarget)
  33618. { this.raiseRecoverable(node.start, "'new.target' can only be used in functions and class static block"); }
  33619. return this.finishNode(node, "MetaProperty")
  33620. }
  33621. var startPos = this.start, startLoc = this.startLoc;
  33622. node.callee = this.parseSubscripts(this.parseExprAtom(null, false, true), startPos, startLoc, true, false);
  33623. if (this.eat(types$1.parenL)) { node.arguments = this.parseExprList(types$1.parenR, this.options.ecmaVersion >= 8, false); }
  33624. else { node.arguments = empty; }
  33625. return this.finishNode(node, "NewExpression")
  33626. };
  33627. // Parse template expression.
  33628. pp$5.parseTemplateElement = function(ref) {
  33629. var isTagged = ref.isTagged;
  33630. var elem = this.startNode();
  33631. if (this.type === types$1.invalidTemplate) {
  33632. if (!isTagged) {
  33633. this.raiseRecoverable(this.start, "Bad escape sequence in untagged template literal");
  33634. }
  33635. elem.value = {
  33636. raw: this.value,
  33637. cooked: null
  33638. };
  33639. } else {
  33640. elem.value = {
  33641. raw: this.input.slice(this.start, this.end).replace(/\r\n?/g, "\n"),
  33642. cooked: this.value
  33643. };
  33644. }
  33645. this.next();
  33646. elem.tail = this.type === types$1.backQuote;
  33647. return this.finishNode(elem, "TemplateElement")
  33648. };
  33649. pp$5.parseTemplate = function(ref) {
  33650. if ( ref === void 0 ) ref = {};
  33651. var isTagged = ref.isTagged; if ( isTagged === void 0 ) isTagged = false;
  33652. var node = this.startNode();
  33653. this.next();
  33654. node.expressions = [];
  33655. var curElt = this.parseTemplateElement({isTagged: isTagged});
  33656. node.quasis = [curElt];
  33657. while (!curElt.tail) {
  33658. if (this.type === types$1.eof) { this.raise(this.pos, "Unterminated template literal"); }
  33659. this.expect(types$1.dollarBraceL);
  33660. node.expressions.push(this.parseExpression());
  33661. this.expect(types$1.braceR);
  33662. node.quasis.push(curElt = this.parseTemplateElement({isTagged: isTagged}));
  33663. }
  33664. this.next();
  33665. return this.finishNode(node, "TemplateLiteral")
  33666. };
  33667. pp$5.isAsyncProp = function(prop) {
  33668. return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" &&
  33669. (this.type === types$1.name || this.type === types$1.num || this.type === types$1.string || this.type === types$1.bracketL || this.type.keyword || (this.options.ecmaVersion >= 9 && this.type === types$1.star)) &&
  33670. !lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
  33671. };
  33672. // Parse an object literal or binding pattern.
  33673. pp$5.parseObj = function(isPattern, refDestructuringErrors) {
  33674. var node = this.startNode(), first = true, propHash = {};
  33675. node.properties = [];
  33676. this.next();
  33677. while (!this.eat(types$1.braceR)) {
  33678. if (!first) {
  33679. this.expect(types$1.comma);
  33680. if (this.options.ecmaVersion >= 5 && this.afterTrailingComma(types$1.braceR)) { break }
  33681. } else { first = false; }
  33682. var prop = this.parseProperty(isPattern, refDestructuringErrors);
  33683. if (!isPattern) { this.checkPropClash(prop, propHash, refDestructuringErrors); }
  33684. node.properties.push(prop);
  33685. }
  33686. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
  33687. };
  33688. pp$5.parseProperty = function(isPattern, refDestructuringErrors) {
  33689. var prop = this.startNode(), isGenerator, isAsync, startPos, startLoc;
  33690. if (this.options.ecmaVersion >= 9 && this.eat(types$1.ellipsis)) {
  33691. if (isPattern) {
  33692. prop.argument = this.parseIdent(false);
  33693. if (this.type === types$1.comma) {
  33694. this.raiseRecoverable(this.start, "Comma is not permitted after the rest element");
  33695. }
  33696. return this.finishNode(prop, "RestElement")
  33697. }
  33698. // Parse argument.
  33699. prop.argument = this.parseMaybeAssign(false, refDestructuringErrors);
  33700. // To disallow trailing comma via `this.toAssignable()`.
  33701. if (this.type === types$1.comma && refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
  33702. refDestructuringErrors.trailingComma = this.start;
  33703. }
  33704. // Finish
  33705. return this.finishNode(prop, "SpreadElement")
  33706. }
  33707. if (this.options.ecmaVersion >= 6) {
  33708. prop.method = false;
  33709. prop.shorthand = false;
  33710. if (isPattern || refDestructuringErrors) {
  33711. startPos = this.start;
  33712. startLoc = this.startLoc;
  33713. }
  33714. if (!isPattern)
  33715. { isGenerator = this.eat(types$1.star); }
  33716. }
  33717. var containsEsc = this.containsEsc;
  33718. this.parsePropertyName(prop);
  33719. if (!isPattern && !containsEsc && this.options.ecmaVersion >= 8 && !isGenerator && this.isAsyncProp(prop)) {
  33720. isAsync = true;
  33721. isGenerator = this.options.ecmaVersion >= 9 && this.eat(types$1.star);
  33722. this.parsePropertyName(prop);
  33723. } else {
  33724. isAsync = false;
  33725. }
  33726. this.parsePropertyValue(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors, containsEsc);
  33727. return this.finishNode(prop, "Property")
  33728. };
  33729. pp$5.parseGetterSetter = function(prop) {
  33730. prop.kind = prop.key.name;
  33731. this.parsePropertyName(prop);
  33732. prop.value = this.parseMethod(false);
  33733. var paramCount = prop.kind === "get" ? 0 : 1;
  33734. if (prop.value.params.length !== paramCount) {
  33735. var start = prop.value.start;
  33736. if (prop.kind === "get")
  33737. { this.raiseRecoverable(start, "getter should have no params"); }
  33738. else
  33739. { this.raiseRecoverable(start, "setter should have exactly one param"); }
  33740. } else {
  33741. if (prop.kind === "set" && prop.value.params[0].type === "RestElement")
  33742. { this.raiseRecoverable(prop.value.params[0].start, "Setter cannot use rest params"); }
  33743. }
  33744. };
  33745. pp$5.parsePropertyValue = function(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors, containsEsc) {
  33746. if ((isGenerator || isAsync) && this.type === types$1.colon)
  33747. { this.unexpected(); }
  33748. if (this.eat(types$1.colon)) {
  33749. prop.value = isPattern ? this.parseMaybeDefault(this.start, this.startLoc) : this.parseMaybeAssign(false, refDestructuringErrors);
  33750. prop.kind = "init";
  33751. } else if (this.options.ecmaVersion >= 6 && this.type === types$1.parenL) {
  33752. if (isPattern) { this.unexpected(); }
  33753. prop.kind = "init";
  33754. prop.method = true;
  33755. prop.value = this.parseMethod(isGenerator, isAsync);
  33756. } else if (!isPattern && !containsEsc &&
  33757. this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
  33758. (prop.key.name === "get" || prop.key.name === "set") &&
  33759. (this.type !== types$1.comma && this.type !== types$1.braceR && this.type !== types$1.eq)) {
  33760. if (isGenerator || isAsync) { this.unexpected(); }
  33761. this.parseGetterSetter(prop);
  33762. } else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
  33763. if (isGenerator || isAsync) { this.unexpected(); }
  33764. this.checkUnreserved(prop.key);
  33765. if (prop.key.name === "await" && !this.awaitIdentPos)
  33766. { this.awaitIdentPos = startPos; }
  33767. prop.kind = "init";
  33768. if (isPattern) {
  33769. prop.value = this.parseMaybeDefault(startPos, startLoc, this.copyNode(prop.key));
  33770. } else if (this.type === types$1.eq && refDestructuringErrors) {
  33771. if (refDestructuringErrors.shorthandAssign < 0)
  33772. { refDestructuringErrors.shorthandAssign = this.start; }
  33773. prop.value = this.parseMaybeDefault(startPos, startLoc, this.copyNode(prop.key));
  33774. } else {
  33775. prop.value = this.copyNode(prop.key);
  33776. }
  33777. prop.shorthand = true;
  33778. } else { this.unexpected(); }
  33779. };
  33780. pp$5.parsePropertyName = function(prop) {
  33781. if (this.options.ecmaVersion >= 6) {
  33782. if (this.eat(types$1.bracketL)) {
  33783. prop.computed = true;
  33784. prop.key = this.parseMaybeAssign();
  33785. this.expect(types$1.bracketR);
  33786. return prop.key
  33787. } else {
  33788. prop.computed = false;
  33789. }
  33790. }
  33791. return prop.key = this.type === types$1.num || this.type === types$1.string ? this.parseExprAtom() : this.parseIdent(this.options.allowReserved !== "never")
  33792. };
  33793. // Initialize empty function node.
  33794. pp$5.initFunction = function(node) {
  33795. node.id = null;
  33796. if (this.options.ecmaVersion >= 6) { node.generator = node.expression = false; }
  33797. if (this.options.ecmaVersion >= 8) { node.async = false; }
  33798. };
  33799. // Parse object or class method.
  33800. pp$5.parseMethod = function(isGenerator, isAsync, allowDirectSuper) {
  33801. var node = this.startNode(), oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
  33802. this.initFunction(node);
  33803. if (this.options.ecmaVersion >= 6)
  33804. { node.generator = isGenerator; }
  33805. if (this.options.ecmaVersion >= 8)
  33806. { node.async = !!isAsync; }
  33807. this.yieldPos = 0;
  33808. this.awaitPos = 0;
  33809. this.awaitIdentPos = 0;
  33810. this.enterScope(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  33811. this.expect(types$1.parenL);
  33812. node.params = this.parseBindingList(types$1.parenR, false, this.options.ecmaVersion >= 8);
  33813. this.checkYieldAwaitInDefaultParams();
  33814. this.parseFunctionBody(node, false, true, false);
  33815. this.yieldPos = oldYieldPos;
  33816. this.awaitPos = oldAwaitPos;
  33817. this.awaitIdentPos = oldAwaitIdentPos;
  33818. return this.finishNode(node, "FunctionExpression")
  33819. };
  33820. // Parse arrow function expression with given parameters.
  33821. pp$5.parseArrowExpression = function(node, params, isAsync, forInit) {
  33822. var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
  33823. this.enterScope(functionFlags(isAsync, false) | SCOPE_ARROW);
  33824. this.initFunction(node);
  33825. if (this.options.ecmaVersion >= 8) { node.async = !!isAsync; }
  33826. this.yieldPos = 0;
  33827. this.awaitPos = 0;
  33828. this.awaitIdentPos = 0;
  33829. node.params = this.toAssignableList(params, true);
  33830. this.parseFunctionBody(node, true, false, forInit);
  33831. this.yieldPos = oldYieldPos;
  33832. this.awaitPos = oldAwaitPos;
  33833. this.awaitIdentPos = oldAwaitIdentPos;
  33834. return this.finishNode(node, "ArrowFunctionExpression")
  33835. };
  33836. // Parse function body and check parameters.
  33837. pp$5.parseFunctionBody = function(node, isArrowFunction, isMethod, forInit) {
  33838. var isExpression = isArrowFunction && this.type !== types$1.braceL;
  33839. var oldStrict = this.strict, useStrict = false;
  33840. if (isExpression) {
  33841. node.body = this.parseMaybeAssign(forInit);
  33842. node.expression = true;
  33843. this.checkParams(node, false);
  33844. } else {
  33845. var nonSimple = this.options.ecmaVersion >= 7 && !this.isSimpleParamList(node.params);
  33846. if (!oldStrict || nonSimple) {
  33847. useStrict = this.strictDirective(this.end);
  33848. // If this is a strict mode function, verify that argument names
  33849. // are not repeated, and it does not try to bind the words `eval`
  33850. // or `arguments`.
  33851. if (useStrict && nonSimple)
  33852. { this.raiseRecoverable(node.start, "Illegal 'use strict' directive in function with non-simple parameter list"); }
  33853. }
  33854. // Start a new scope with regard to labels and the `inFunction`
  33855. // flag (restore them to their old value afterwards).
  33856. var oldLabels = this.labels;
  33857. this.labels = [];
  33858. if (useStrict) { this.strict = true; }
  33859. // Add the params to varDeclaredNames to ensure that an error is thrown
  33860. // if a let/const declaration in the function clashes with one of the params.
  33861. this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && !isMethod && this.isSimpleParamList(node.params));
  33862. // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
  33863. if (this.strict && node.id) { this.checkLValSimple(node.id, BIND_OUTSIDE); }
  33864. node.body = this.parseBlock(false, undefined, useStrict && !oldStrict);
  33865. node.expression = false;
  33866. this.adaptDirectivePrologue(node.body.body);
  33867. this.labels = oldLabels;
  33868. }
  33869. this.exitScope();
  33870. };
  33871. pp$5.isSimpleParamList = function(params) {
  33872. for (var i = 0, list = params; i < list.length; i += 1)
  33873. {
  33874. var param = list[i];
  33875. if (param.type !== "Identifier") { return false
  33876. } }
  33877. return true
  33878. };
  33879. // Checks function params for various disallowed patterns such as using "eval"
  33880. // or "arguments" and duplicate parameters.
  33881. pp$5.checkParams = function(node, allowDuplicates) {
  33882. var nameHash = Object.create(null);
  33883. for (var i = 0, list = node.params; i < list.length; i += 1)
  33884. {
  33885. var param = list[i];
  33886. this.checkLValInnerPattern(param, BIND_VAR, allowDuplicates ? null : nameHash);
  33887. }
  33888. };
  33889. // Parses a comma-separated list of expressions, and returns them as
  33890. // an array. `close` is the token type that ends the list, and
  33891. // `allowEmpty` can be turned on to allow subsequent commas with
  33892. // nothing in between them to be parsed as `null` (which is needed
  33893. // for array literals).
  33894. pp$5.parseExprList = function(close, allowTrailingComma, allowEmpty, refDestructuringErrors) {
  33895. var elts = [], first = true;
  33896. while (!this.eat(close)) {
  33897. if (!first) {
  33898. this.expect(types$1.comma);
  33899. if (allowTrailingComma && this.afterTrailingComma(close)) { break }
  33900. } else { first = false; }
  33901. var elt = (void 0);
  33902. if (allowEmpty && this.type === types$1.comma)
  33903. { elt = null; }
  33904. else if (this.type === types$1.ellipsis) {
  33905. elt = this.parseSpread(refDestructuringErrors);
  33906. if (refDestructuringErrors && this.type === types$1.comma && refDestructuringErrors.trailingComma < 0)
  33907. { refDestructuringErrors.trailingComma = this.start; }
  33908. } else {
  33909. elt = this.parseMaybeAssign(false, refDestructuringErrors);
  33910. }
  33911. elts.push(elt);
  33912. }
  33913. return elts
  33914. };
  33915. pp$5.checkUnreserved = function(ref) {
  33916. var start = ref.start;
  33917. var end = ref.end;
  33918. var name = ref.name;
  33919. if (this.inGenerator && name === "yield")
  33920. { this.raiseRecoverable(start, "Cannot use 'yield' as identifier inside a generator"); }
  33921. if (this.inAsync && name === "await")
  33922. { this.raiseRecoverable(start, "Cannot use 'await' as identifier inside an async function"); }
  33923. if (this.currentThisScope().inClassFieldInit && name === "arguments")
  33924. { this.raiseRecoverable(start, "Cannot use 'arguments' in class field initializer"); }
  33925. if (this.inClassStaticBlock && (name === "arguments" || name === "await"))
  33926. { this.raise(start, ("Cannot use " + name + " in class static initialization block")); }
  33927. if (this.keywords.test(name))
  33928. { this.raise(start, ("Unexpected keyword '" + name + "'")); }
  33929. if (this.options.ecmaVersion < 6 &&
  33930. this.input.slice(start, end).indexOf("\\") !== -1) { return }
  33931. var re = this.strict ? this.reservedWordsStrict : this.reservedWords;
  33932. if (re.test(name)) {
  33933. if (!this.inAsync && name === "await")
  33934. { this.raiseRecoverable(start, "Cannot use keyword 'await' outside an async function"); }
  33935. this.raiseRecoverable(start, ("The keyword '" + name + "' is reserved"));
  33936. }
  33937. };
  33938. // Parse the next token as an identifier. If `liberal` is true (used
  33939. // when parsing properties), it will also convert keywords into
  33940. // identifiers.
  33941. pp$5.parseIdent = function(liberal) {
  33942. var node = this.parseIdentNode();
  33943. this.next(!!liberal);
  33944. this.finishNode(node, "Identifier");
  33945. if (!liberal) {
  33946. this.checkUnreserved(node);
  33947. if (node.name === "await" && !this.awaitIdentPos)
  33948. { this.awaitIdentPos = node.start; }
  33949. }
  33950. return node
  33951. };
  33952. pp$5.parseIdentNode = function() {
  33953. var node = this.startNode();
  33954. if (this.type === types$1.name) {
  33955. node.name = this.value;
  33956. } else if (this.type.keyword) {
  33957. node.name = this.type.keyword;
  33958. // To fix https://github.com/acornjs/acorn/issues/575
  33959. // `class` and `function` keywords push new context into this.context.
  33960. // But there is no chance to pop the context if the keyword is consumed as an identifier such as a property name.
  33961. // If the previous token is a dot, this does not apply because the context-managing code already ignored the keyword
  33962. if ((node.name === "class" || node.name === "function") &&
  33963. (this.lastTokEnd !== this.lastTokStart + 1 || this.input.charCodeAt(this.lastTokStart) !== 46)) {
  33964. this.context.pop();
  33965. }
  33966. this.type = types$1.name;
  33967. } else {
  33968. this.unexpected();
  33969. }
  33970. return node
  33971. };
  33972. pp$5.parsePrivateIdent = function() {
  33973. var node = this.startNode();
  33974. if (this.type === types$1.privateId) {
  33975. node.name = this.value;
  33976. } else {
  33977. this.unexpected();
  33978. }
  33979. this.next();
  33980. this.finishNode(node, "PrivateIdentifier");
  33981. // For validating existence
  33982. if (this.options.checkPrivateFields) {
  33983. if (this.privateNameStack.length === 0) {
  33984. this.raise(node.start, ("Private field '#" + (node.name) + "' must be declared in an enclosing class"));
  33985. } else {
  33986. this.privateNameStack[this.privateNameStack.length - 1].used.push(node);
  33987. }
  33988. }
  33989. return node
  33990. };
  33991. // Parses yield expression inside generator.
  33992. pp$5.parseYield = function(forInit) {
  33993. if (!this.yieldPos) { this.yieldPos = this.start; }
  33994. var node = this.startNode();
  33995. this.next();
  33996. if (this.type === types$1.semi || this.canInsertSemicolon() || (this.type !== types$1.star && !this.type.startsExpr)) {
  33997. node.delegate = false;
  33998. node.argument = null;
  33999. } else {
  34000. node.delegate = this.eat(types$1.star);
  34001. node.argument = this.parseMaybeAssign(forInit);
  34002. }
  34003. return this.finishNode(node, "YieldExpression")
  34004. };
  34005. pp$5.parseAwait = function(forInit) {
  34006. if (!this.awaitPos) { this.awaitPos = this.start; }
  34007. var node = this.startNode();
  34008. this.next();
  34009. node.argument = this.parseMaybeUnary(null, true, false, forInit);
  34010. return this.finishNode(node, "AwaitExpression")
  34011. };
  34012. var pp$4 = Parser.prototype;
  34013. // This function is used to raise exceptions on parse errors. It
  34014. // takes an offset integer (into the current `input`) to indicate
  34015. // the location of the error, attaches the position to the end
  34016. // of the error message, and then raises a `SyntaxError` with that
  34017. // message.
  34018. pp$4.raise = function(pos, message) {
  34019. var loc = getLineInfo(this.input, pos);
  34020. message += " (" + loc.line + ":" + loc.column + ")";
  34021. var err = new SyntaxError(message);
  34022. err.pos = pos; err.loc = loc; err.raisedAt = this.pos;
  34023. throw err
  34024. };
  34025. pp$4.raiseRecoverable = pp$4.raise;
  34026. pp$4.curPosition = function() {
  34027. if (this.options.locations) {
  34028. return new Position(this.curLine, this.pos - this.lineStart)
  34029. }
  34030. };
  34031. var pp$3 = Parser.prototype;
  34032. var Scope = function Scope(flags) {
  34033. this.flags = flags;
  34034. // A list of var-declared names in the current lexical scope
  34035. this.var = [];
  34036. // A list of lexically-declared names in the current lexical scope
  34037. this.lexical = [];
  34038. // A list of lexically-declared FunctionDeclaration names in the current lexical scope
  34039. this.functions = [];
  34040. // A switch to disallow the identifier reference 'arguments'
  34041. this.inClassFieldInit = false;
  34042. };
  34043. // The functions in this module keep track of declared variables in the current scope in order to detect duplicate variable names.
  34044. pp$3.enterScope = function(flags) {
  34045. this.scopeStack.push(new Scope(flags));
  34046. };
  34047. pp$3.exitScope = function() {
  34048. this.scopeStack.pop();
  34049. };
  34050. // The spec says:
  34051. // > At the top level of a function, or script, function declarations are
  34052. // > treated like var declarations rather than like lexical declarations.
  34053. pp$3.treatFunctionsAsVarInScope = function(scope) {
  34054. return (scope.flags & SCOPE_FUNCTION) || !this.inModule && (scope.flags & SCOPE_TOP)
  34055. };
  34056. pp$3.declareName = function(name, bindingType, pos) {
  34057. var redeclared = false;
  34058. if (bindingType === BIND_LEXICAL) {
  34059. var scope = this.currentScope();
  34060. redeclared = scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  34061. scope.lexical.push(name);
  34062. if (this.inModule && (scope.flags & SCOPE_TOP))
  34063. { delete this.undefinedExports[name]; }
  34064. } else if (bindingType === BIND_SIMPLE_CATCH) {
  34065. var scope$1 = this.currentScope();
  34066. scope$1.lexical.push(name);
  34067. } else if (bindingType === BIND_FUNCTION) {
  34068. var scope$2 = this.currentScope();
  34069. if (this.treatFunctionsAsVar)
  34070. { redeclared = scope$2.lexical.indexOf(name) > -1; }
  34071. else
  34072. { redeclared = scope$2.lexical.indexOf(name) > -1 || scope$2.var.indexOf(name) > -1; }
  34073. scope$2.functions.push(name);
  34074. } else {
  34075. for (var i = this.scopeStack.length - 1; i >= 0; --i) {
  34076. var scope$3 = this.scopeStack[i];
  34077. if (scope$3.lexical.indexOf(name) > -1 && !((scope$3.flags & SCOPE_SIMPLE_CATCH) && scope$3.lexical[0] === name) ||
  34078. !this.treatFunctionsAsVarInScope(scope$3) && scope$3.functions.indexOf(name) > -1) {
  34079. redeclared = true;
  34080. break
  34081. }
  34082. scope$3.var.push(name);
  34083. if (this.inModule && (scope$3.flags & SCOPE_TOP))
  34084. { delete this.undefinedExports[name]; }
  34085. if (scope$3.flags & SCOPE_VAR) { break }
  34086. }
  34087. }
  34088. if (redeclared) { this.raiseRecoverable(pos, ("Identifier '" + name + "' has already been declared")); }
  34089. };
  34090. pp$3.checkLocalExport = function(id) {
  34091. // scope.functions must be empty as Module code is always strict.
  34092. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 &&
  34093. this.scopeStack[0].var.indexOf(id.name) === -1) {
  34094. this.undefinedExports[id.name] = id;
  34095. }
  34096. };
  34097. pp$3.currentScope = function() {
  34098. return this.scopeStack[this.scopeStack.length - 1]
  34099. };
  34100. pp$3.currentVarScope = function() {
  34101. for (var i = this.scopeStack.length - 1;; i--) {
  34102. var scope = this.scopeStack[i];
  34103. if (scope.flags & SCOPE_VAR) { return scope }
  34104. }
  34105. };
  34106. // Could be useful for `this`, `new.target`, `super()`, `super.property`, and `super[property]`.
  34107. pp$3.currentThisScope = function() {
  34108. for (var i = this.scopeStack.length - 1;; i--) {
  34109. var scope = this.scopeStack[i];
  34110. if (scope.flags & SCOPE_VAR && !(scope.flags & SCOPE_ARROW)) { return scope }
  34111. }
  34112. };
  34113. var Node = function Node(parser, pos, loc) {
  34114. this.type = "";
  34115. this.start = pos;
  34116. this.end = 0;
  34117. if (parser.options.locations)
  34118. { this.loc = new SourceLocation(parser, loc); }
  34119. if (parser.options.directSourceFile)
  34120. { this.sourceFile = parser.options.directSourceFile; }
  34121. if (parser.options.ranges)
  34122. { this.range = [pos, 0]; }
  34123. };
  34124. // Start an AST node, attaching a start offset.
  34125. var pp$2 = Parser.prototype;
  34126. pp$2.startNode = function() {
  34127. return new Node(this, this.start, this.startLoc)
  34128. };
  34129. pp$2.startNodeAt = function(pos, loc) {
  34130. return new Node(this, pos, loc)
  34131. };
  34132. // Finish an AST node, adding `type` and `end` properties.
  34133. function finishNodeAt(node, type, pos, loc) {
  34134. node.type = type;
  34135. node.end = pos;
  34136. if (this.options.locations)
  34137. { node.loc.end = loc; }
  34138. if (this.options.ranges)
  34139. { node.range[1] = pos; }
  34140. return node
  34141. }
  34142. pp$2.finishNode = function(node, type) {
  34143. return finishNodeAt.call(this, node, type, this.lastTokEnd, this.lastTokEndLoc)
  34144. };
  34145. // Finish node at given position
  34146. pp$2.finishNodeAt = function(node, type, pos, loc) {
  34147. return finishNodeAt.call(this, node, type, pos, loc)
  34148. };
  34149. pp$2.copyNode = function(node) {
  34150. var newNode = new Node(this, node.start, this.startLoc);
  34151. for (var prop in node) { newNode[prop] = node[prop]; }
  34152. return newNode
  34153. };
  34154. // This file contains Unicode properties extracted from the ECMAScript specification.
  34155. // The lists are extracted like so:
  34156. // $$('#table-binary-unicode-properties > figure > table > tbody > tr > td:nth-child(1) code').map(el => el.innerText)
  34157. // #table-binary-unicode-properties
  34158. var ecma9BinaryProperties = "ASCII ASCII_Hex_Digit AHex Alphabetic Alpha Any Assigned Bidi_Control Bidi_C Bidi_Mirrored Bidi_M Case_Ignorable CI Cased Changes_When_Casefolded CWCF Changes_When_Casemapped CWCM Changes_When_Lowercased CWL Changes_When_NFKC_Casefolded CWKCF Changes_When_Titlecased CWT Changes_When_Uppercased CWU Dash Default_Ignorable_Code_Point DI Deprecated Dep Diacritic Dia Emoji Emoji_Component Emoji_Modifier Emoji_Modifier_Base Emoji_Presentation Extender Ext Grapheme_Base Gr_Base Grapheme_Extend Gr_Ext Hex_Digit Hex IDS_Binary_Operator IDSB IDS_Trinary_Operator IDST ID_Continue IDC ID_Start IDS Ideographic Ideo Join_Control Join_C Logical_Order_Exception LOE Lowercase Lower Math Noncharacter_Code_Point NChar Pattern_Syntax Pat_Syn Pattern_White_Space Pat_WS Quotation_Mark QMark Radical Regional_Indicator RI Sentence_Terminal STerm Soft_Dotted SD Terminal_Punctuation Term Unified_Ideograph UIdeo Uppercase Upper Variation_Selector VS White_Space space XID_Continue XIDC XID_Start XIDS";
  34159. var ecma10BinaryProperties = ecma9BinaryProperties + " Extended_Pictographic";
  34160. var ecma11BinaryProperties = ecma10BinaryProperties;
  34161. var ecma12BinaryProperties = ecma11BinaryProperties + " EBase EComp EMod EPres ExtPict";
  34162. var ecma13BinaryProperties = ecma12BinaryProperties;
  34163. var ecma14BinaryProperties = ecma13BinaryProperties;
  34164. var unicodeBinaryProperties = {
  34165. 9: ecma9BinaryProperties,
  34166. 10: ecma10BinaryProperties,
  34167. 11: ecma11BinaryProperties,
  34168. 12: ecma12BinaryProperties,
  34169. 13: ecma13BinaryProperties,
  34170. 14: ecma14BinaryProperties
  34171. };
  34172. // #table-binary-unicode-properties-of-strings
  34173. var ecma14BinaryPropertiesOfStrings = "Basic_Emoji Emoji_Keycap_Sequence RGI_Emoji_Modifier_Sequence RGI_Emoji_Flag_Sequence RGI_Emoji_Tag_Sequence RGI_Emoji_ZWJ_Sequence RGI_Emoji";
  34174. var unicodeBinaryPropertiesOfStrings = {
  34175. 9: "",
  34176. 10: "",
  34177. 11: "",
  34178. 12: "",
  34179. 13: "",
  34180. 14: ecma14BinaryPropertiesOfStrings
  34181. };
  34182. // #table-unicode-general-category-values
  34183. var unicodeGeneralCategoryValues = "Cased_Letter LC Close_Punctuation Pe Connector_Punctuation Pc Control Cc cntrl Currency_Symbol Sc Dash_Punctuation Pd Decimal_Number Nd digit Enclosing_Mark Me Final_Punctuation Pf Format Cf Initial_Punctuation Pi Letter L Letter_Number Nl Line_Separator Zl Lowercase_Letter Ll Mark M Combining_Mark Math_Symbol Sm Modifier_Letter Lm Modifier_Symbol Sk Nonspacing_Mark Mn Number N Open_Punctuation Ps Other C Other_Letter Lo Other_Number No Other_Punctuation Po Other_Symbol So Paragraph_Separator Zp Private_Use Co Punctuation P punct Separator Z Space_Separator Zs Spacing_Mark Mc Surrogate Cs Symbol S Titlecase_Letter Lt Unassigned Cn Uppercase_Letter Lu";
  34184. // #table-unicode-script-values
  34185. var ecma9ScriptValues = "Adlam Adlm Ahom Anatolian_Hieroglyphs Hluw Arabic Arab Armenian Armn Avestan Avst Balinese Bali Bamum Bamu Bassa_Vah Bass Batak Batk Bengali Beng Bhaiksuki Bhks Bopomofo Bopo Brahmi Brah Braille Brai Buginese Bugi Buhid Buhd Canadian_Aboriginal Cans Carian Cari Caucasian_Albanian Aghb Chakma Cakm Cham Cham Cherokee Cher Common Zyyy Coptic Copt Qaac Cuneiform Xsux Cypriot Cprt Cyrillic Cyrl Deseret Dsrt Devanagari Deva Duployan Dupl Egyptian_Hieroglyphs Egyp Elbasan Elba Ethiopic Ethi Georgian Geor Glagolitic Glag Gothic Goth Grantha Gran Greek Grek Gujarati Gujr Gurmukhi Guru Han Hani Hangul Hang Hanunoo Hano Hatran Hatr Hebrew Hebr Hiragana Hira Imperial_Aramaic Armi Inherited Zinh Qaai Inscriptional_Pahlavi Phli Inscriptional_Parthian Prti Javanese Java Kaithi Kthi Kannada Knda Katakana Kana Kayah_Li Kali Kharoshthi Khar Khmer Khmr Khojki Khoj Khudawadi Sind Lao Laoo Latin Latn Lepcha Lepc Limbu Limb Linear_A Lina Linear_B Linb Lisu Lisu Lycian Lyci Lydian Lydi Mahajani Mahj Malayalam Mlym Mandaic Mand Manichaean Mani Marchen Marc Masaram_Gondi Gonm Meetei_Mayek Mtei Mende_Kikakui Mend Meroitic_Cursive Merc Meroitic_Hieroglyphs Mero Miao Plrd Modi Mongolian Mong Mro Mroo Multani Mult Myanmar Mymr Nabataean Nbat New_Tai_Lue Talu Newa Newa Nko Nkoo Nushu Nshu Ogham Ogam Ol_Chiki Olck Old_Hungarian Hung Old_Italic Ital Old_North_Arabian Narb Old_Permic Perm Old_Persian Xpeo Old_South_Arabian Sarb Old_Turkic Orkh Oriya Orya Osage Osge Osmanya Osma Pahawh_Hmong Hmng Palmyrene Palm Pau_Cin_Hau Pauc Phags_Pa Phag Phoenician Phnx Psalter_Pahlavi Phlp Rejang Rjng Runic Runr Samaritan Samr Saurashtra Saur Sharada Shrd Shavian Shaw Siddham Sidd SignWriting Sgnw Sinhala Sinh Sora_Sompeng Sora Soyombo Soyo Sundanese Sund Syloti_Nagri Sylo Syriac Syrc Tagalog Tglg Tagbanwa Tagb Tai_Le Tale Tai_Tham Lana Tai_Viet Tavt Takri Takr Tamil Taml Tangut Tang Telugu Telu Thaana Thaa Thai Thai Tibetan Tibt Tifinagh Tfng Tirhuta Tirh Ugaritic Ugar Vai Vaii Warang_Citi Wara Yi Yiii Zanabazar_Square Zanb";
  34186. var ecma10ScriptValues = ecma9ScriptValues + " Dogra Dogr Gunjala_Gondi Gong Hanifi_Rohingya Rohg Makasar Maka Medefaidrin Medf Old_Sogdian Sogo Sogdian Sogd";
  34187. var ecma11ScriptValues = ecma10ScriptValues + " Elymaic Elym Nandinagari Nand Nyiakeng_Puachue_Hmong Hmnp Wancho Wcho";
  34188. var ecma12ScriptValues = ecma11ScriptValues + " Chorasmian Chrs Diak Dives_Akuru Khitan_Small_Script Kits Yezi Yezidi";
  34189. var ecma13ScriptValues = ecma12ScriptValues + " Cypro_Minoan Cpmn Old_Uyghur Ougr Tangsa Tnsa Toto Vithkuqi Vith";
  34190. var ecma14ScriptValues = ecma13ScriptValues + " Hrkt Katakana_Or_Hiragana Kawi Nag_Mundari Nagm Unknown Zzzz";
  34191. var unicodeScriptValues = {
  34192. 9: ecma9ScriptValues,
  34193. 10: ecma10ScriptValues,
  34194. 11: ecma11ScriptValues,
  34195. 12: ecma12ScriptValues,
  34196. 13: ecma13ScriptValues,
  34197. 14: ecma14ScriptValues
  34198. };
  34199. var data = {};
  34200. function buildUnicodeData(ecmaVersion) {
  34201. var d = data[ecmaVersion] = {
  34202. binary: wordsRegexp(unicodeBinaryProperties[ecmaVersion] + " " + unicodeGeneralCategoryValues),
  34203. binaryOfStrings: wordsRegexp(unicodeBinaryPropertiesOfStrings[ecmaVersion]),
  34204. nonBinary: {
  34205. General_Category: wordsRegexp(unicodeGeneralCategoryValues),
  34206. Script: wordsRegexp(unicodeScriptValues[ecmaVersion])
  34207. }
  34208. };
  34209. d.nonBinary.Script_Extensions = d.nonBinary.Script;
  34210. d.nonBinary.gc = d.nonBinary.General_Category;
  34211. d.nonBinary.sc = d.nonBinary.Script;
  34212. d.nonBinary.scx = d.nonBinary.Script_Extensions;
  34213. }
  34214. for (var i$1 = 0, list = [9, 10, 11, 12, 13, 14]; i$1 < list.length; i$1 += 1) {
  34215. var ecmaVersion = list[i$1];
  34216. buildUnicodeData(ecmaVersion);
  34217. }
  34218. var pp$1 = Parser.prototype;
  34219. var RegExpValidationState = function RegExpValidationState(parser) {
  34220. this.parser = parser;
  34221. this.validFlags = "gim" + (parser.options.ecmaVersion >= 6 ? "uy" : "") + (parser.options.ecmaVersion >= 9 ? "s" : "") + (parser.options.ecmaVersion >= 13 ? "d" : "") + (parser.options.ecmaVersion >= 15 ? "v" : "");
  34222. this.unicodeProperties = data[parser.options.ecmaVersion >= 14 ? 14 : parser.options.ecmaVersion];
  34223. this.source = "";
  34224. this.flags = "";
  34225. this.start = 0;
  34226. this.switchU = false;
  34227. this.switchV = false;
  34228. this.switchN = false;
  34229. this.pos = 0;
  34230. this.lastIntValue = 0;
  34231. this.lastStringValue = "";
  34232. this.lastAssertionIsQuantifiable = false;
  34233. this.numCapturingParens = 0;
  34234. this.maxBackReference = 0;
  34235. this.groupNames = [];
  34236. this.backReferenceNames = [];
  34237. };
  34238. RegExpValidationState.prototype.reset = function reset (start, pattern, flags) {
  34239. var unicodeSets = flags.indexOf("v") !== -1;
  34240. var unicode = flags.indexOf("u") !== -1;
  34241. this.start = start | 0;
  34242. this.source = pattern + "";
  34243. this.flags = flags;
  34244. if (unicodeSets && this.parser.options.ecmaVersion >= 15) {
  34245. this.switchU = true;
  34246. this.switchV = true;
  34247. this.switchN = true;
  34248. } else {
  34249. this.switchU = unicode && this.parser.options.ecmaVersion >= 6;
  34250. this.switchV = false;
  34251. this.switchN = unicode && this.parser.options.ecmaVersion >= 9;
  34252. }
  34253. };
  34254. RegExpValidationState.prototype.raise = function raise (message) {
  34255. this.parser.raiseRecoverable(this.start, ("Invalid regular expression: /" + (this.source) + "/: " + message));
  34256. };
  34257. // If u flag is given, this returns the code point at the index (it combines a surrogate pair).
  34258. // Otherwise, this returns the code unit of the index (can be a part of a surrogate pair).
  34259. RegExpValidationState.prototype.at = function at (i, forceU) {
  34260. if ( forceU === void 0 ) forceU = false;
  34261. var s = this.source;
  34262. var l = s.length;
  34263. if (i >= l) {
  34264. return -1
  34265. }
  34266. var c = s.charCodeAt(i);
  34267. if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
  34268. return c
  34269. }
  34270. var next = s.charCodeAt(i + 1);
  34271. return next >= 0xDC00 && next <= 0xDFFF ? (c << 10) + next - 0x35FDC00 : c
  34272. };
  34273. RegExpValidationState.prototype.nextIndex = function nextIndex (i, forceU) {
  34274. if ( forceU === void 0 ) forceU = false;
  34275. var s = this.source;
  34276. var l = s.length;
  34277. if (i >= l) {
  34278. return l
  34279. }
  34280. var c = s.charCodeAt(i), next;
  34281. if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l ||
  34282. (next = s.charCodeAt(i + 1)) < 0xDC00 || next > 0xDFFF) {
  34283. return i + 1
  34284. }
  34285. return i + 2
  34286. };
  34287. RegExpValidationState.prototype.current = function current (forceU) {
  34288. if ( forceU === void 0 ) forceU = false;
  34289. return this.at(this.pos, forceU)
  34290. };
  34291. RegExpValidationState.prototype.lookahead = function lookahead (forceU) {
  34292. if ( forceU === void 0 ) forceU = false;
  34293. return this.at(this.nextIndex(this.pos, forceU), forceU)
  34294. };
  34295. RegExpValidationState.prototype.advance = function advance (forceU) {
  34296. if ( forceU === void 0 ) forceU = false;
  34297. this.pos = this.nextIndex(this.pos, forceU);
  34298. };
  34299. RegExpValidationState.prototype.eat = function eat (ch, forceU) {
  34300. if ( forceU === void 0 ) forceU = false;
  34301. if (this.current(forceU) === ch) {
  34302. this.advance(forceU);
  34303. return true
  34304. }
  34305. return false
  34306. };
  34307. RegExpValidationState.prototype.eatChars = function eatChars (chs, forceU) {
  34308. if ( forceU === void 0 ) forceU = false;
  34309. var pos = this.pos;
  34310. for (var i = 0, list = chs; i < list.length; i += 1) {
  34311. var ch = list[i];
  34312. var current = this.at(pos, forceU);
  34313. if (current === -1 || current !== ch) {
  34314. return false
  34315. }
  34316. pos = this.nextIndex(pos, forceU);
  34317. }
  34318. this.pos = pos;
  34319. return true
  34320. };
  34321. /**
  34322. * Validate the flags part of a given RegExpLiteral.
  34323. *
  34324. * @param {RegExpValidationState} state The state to validate RegExp.
  34325. * @returns {void}
  34326. */
  34327. pp$1.validateRegExpFlags = function(state) {
  34328. var validFlags = state.validFlags;
  34329. var flags = state.flags;
  34330. var u = false;
  34331. var v = false;
  34332. for (var i = 0; i < flags.length; i++) {
  34333. var flag = flags.charAt(i);
  34334. if (validFlags.indexOf(flag) === -1) {
  34335. this.raise(state.start, "Invalid regular expression flag");
  34336. }
  34337. if (flags.indexOf(flag, i + 1) > -1) {
  34338. this.raise(state.start, "Duplicate regular expression flag");
  34339. }
  34340. if (flag === "u") { u = true; }
  34341. if (flag === "v") { v = true; }
  34342. }
  34343. if (this.options.ecmaVersion >= 15 && u && v) {
  34344. this.raise(state.start, "Invalid regular expression flag");
  34345. }
  34346. };
  34347. /**
  34348. * Validate the pattern part of a given RegExpLiteral.
  34349. *
  34350. * @param {RegExpValidationState} state The state to validate RegExp.
  34351. * @returns {void}
  34352. */
  34353. pp$1.validateRegExpPattern = function(state) {
  34354. this.regexp_pattern(state);
  34355. // The goal symbol for the parse is |Pattern[~U, ~N]|. If the result of
  34356. // parsing contains a |GroupName|, reparse with the goal symbol
  34357. // |Pattern[~U, +N]| and use this result instead. Throw a *SyntaxError*
  34358. // exception if _P_ did not conform to the grammar, if any elements of _P_
  34359. // were not matched by the parse, or if any Early Error conditions exist.
  34360. if (!state.switchN && this.options.ecmaVersion >= 9 && state.groupNames.length > 0) {
  34361. state.switchN = true;
  34362. this.regexp_pattern(state);
  34363. }
  34364. };
  34365. // https://www.ecma-international.org/ecma-262/8.0/#prod-Pattern
  34366. pp$1.regexp_pattern = function(state) {
  34367. state.pos = 0;
  34368. state.lastIntValue = 0;
  34369. state.lastStringValue = "";
  34370. state.lastAssertionIsQuantifiable = false;
  34371. state.numCapturingParens = 0;
  34372. state.maxBackReference = 0;
  34373. state.groupNames.length = 0;
  34374. state.backReferenceNames.length = 0;
  34375. this.regexp_disjunction(state);
  34376. if (state.pos !== state.source.length) {
  34377. // Make the same messages as V8.
  34378. if (state.eat(0x29 /* ) */)) {
  34379. state.raise("Unmatched ')'");
  34380. }
  34381. if (state.eat(0x5D /* ] */) || state.eat(0x7D /* } */)) {
  34382. state.raise("Lone quantifier brackets");
  34383. }
  34384. }
  34385. if (state.maxBackReference > state.numCapturingParens) {
  34386. state.raise("Invalid escape");
  34387. }
  34388. for (var i = 0, list = state.backReferenceNames; i < list.length; i += 1) {
  34389. var name = list[i];
  34390. if (state.groupNames.indexOf(name) === -1) {
  34391. state.raise("Invalid named capture referenced");
  34392. }
  34393. }
  34394. };
  34395. // https://www.ecma-international.org/ecma-262/8.0/#prod-Disjunction
  34396. pp$1.regexp_disjunction = function(state) {
  34397. this.regexp_alternative(state);
  34398. while (state.eat(0x7C /* | */)) {
  34399. this.regexp_alternative(state);
  34400. }
  34401. // Make the same message as V8.
  34402. if (this.regexp_eatQuantifier(state, true)) {
  34403. state.raise("Nothing to repeat");
  34404. }
  34405. if (state.eat(0x7B /* { */)) {
  34406. state.raise("Lone quantifier brackets");
  34407. }
  34408. };
  34409. // https://www.ecma-international.org/ecma-262/8.0/#prod-Alternative
  34410. pp$1.regexp_alternative = function(state) {
  34411. while (state.pos < state.source.length && this.regexp_eatTerm(state))
  34412. { }
  34413. };
  34414. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-Term
  34415. pp$1.regexp_eatTerm = function(state) {
  34416. if (this.regexp_eatAssertion(state)) {
  34417. // Handle `QuantifiableAssertion Quantifier` alternative.
  34418. // `state.lastAssertionIsQuantifiable` is true if the last eaten Assertion
  34419. // is a QuantifiableAssertion.
  34420. if (state.lastAssertionIsQuantifiable && this.regexp_eatQuantifier(state)) {
  34421. // Make the same message as V8.
  34422. if (state.switchU) {
  34423. state.raise("Invalid quantifier");
  34424. }
  34425. }
  34426. return true
  34427. }
  34428. if (state.switchU ? this.regexp_eatAtom(state) : this.regexp_eatExtendedAtom(state)) {
  34429. this.regexp_eatQuantifier(state);
  34430. return true
  34431. }
  34432. return false
  34433. };
  34434. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-Assertion
  34435. pp$1.regexp_eatAssertion = function(state) {
  34436. var start = state.pos;
  34437. state.lastAssertionIsQuantifiable = false;
  34438. // ^, $
  34439. if (state.eat(0x5E /* ^ */) || state.eat(0x24 /* $ */)) {
  34440. return true
  34441. }
  34442. // \b \B
  34443. if (state.eat(0x5C /* \ */)) {
  34444. if (state.eat(0x42 /* B */) || state.eat(0x62 /* b */)) {
  34445. return true
  34446. }
  34447. state.pos = start;
  34448. }
  34449. // Lookahead / Lookbehind
  34450. if (state.eat(0x28 /* ( */) && state.eat(0x3F /* ? */)) {
  34451. var lookbehind = false;
  34452. if (this.options.ecmaVersion >= 9) {
  34453. lookbehind = state.eat(0x3C /* < */);
  34454. }
  34455. if (state.eat(0x3D /* = */) || state.eat(0x21 /* ! */)) {
  34456. this.regexp_disjunction(state);
  34457. if (!state.eat(0x29 /* ) */)) {
  34458. state.raise("Unterminated group");
  34459. }
  34460. state.lastAssertionIsQuantifiable = !lookbehind;
  34461. return true
  34462. }
  34463. }
  34464. state.pos = start;
  34465. return false
  34466. };
  34467. // https://www.ecma-international.org/ecma-262/8.0/#prod-Quantifier
  34468. pp$1.regexp_eatQuantifier = function(state, noError) {
  34469. if ( noError === void 0 ) noError = false;
  34470. if (this.regexp_eatQuantifierPrefix(state, noError)) {
  34471. state.eat(0x3F /* ? */);
  34472. return true
  34473. }
  34474. return false
  34475. };
  34476. // https://www.ecma-international.org/ecma-262/8.0/#prod-QuantifierPrefix
  34477. pp$1.regexp_eatQuantifierPrefix = function(state, noError) {
  34478. return (
  34479. state.eat(0x2A /* * */) ||
  34480. state.eat(0x2B /* + */) ||
  34481. state.eat(0x3F /* ? */) ||
  34482. this.regexp_eatBracedQuantifier(state, noError)
  34483. )
  34484. };
  34485. pp$1.regexp_eatBracedQuantifier = function(state, noError) {
  34486. var start = state.pos;
  34487. if (state.eat(0x7B /* { */)) {
  34488. var min = 0, max = -1;
  34489. if (this.regexp_eatDecimalDigits(state)) {
  34490. min = state.lastIntValue;
  34491. if (state.eat(0x2C /* , */) && this.regexp_eatDecimalDigits(state)) {
  34492. max = state.lastIntValue;
  34493. }
  34494. if (state.eat(0x7D /* } */)) {
  34495. // SyntaxError in https://www.ecma-international.org/ecma-262/8.0/#sec-term
  34496. if (max !== -1 && max < min && !noError) {
  34497. state.raise("numbers out of order in {} quantifier");
  34498. }
  34499. return true
  34500. }
  34501. }
  34502. if (state.switchU && !noError) {
  34503. state.raise("Incomplete quantifier");
  34504. }
  34505. state.pos = start;
  34506. }
  34507. return false
  34508. };
  34509. // https://www.ecma-international.org/ecma-262/8.0/#prod-Atom
  34510. pp$1.regexp_eatAtom = function(state) {
  34511. return (
  34512. this.regexp_eatPatternCharacters(state) ||
  34513. state.eat(0x2E /* . */) ||
  34514. this.regexp_eatReverseSolidusAtomEscape(state) ||
  34515. this.regexp_eatCharacterClass(state) ||
  34516. this.regexp_eatUncapturingGroup(state) ||
  34517. this.regexp_eatCapturingGroup(state)
  34518. )
  34519. };
  34520. pp$1.regexp_eatReverseSolidusAtomEscape = function(state) {
  34521. var start = state.pos;
  34522. if (state.eat(0x5C /* \ */)) {
  34523. if (this.regexp_eatAtomEscape(state)) {
  34524. return true
  34525. }
  34526. state.pos = start;
  34527. }
  34528. return false
  34529. };
  34530. pp$1.regexp_eatUncapturingGroup = function(state) {
  34531. var start = state.pos;
  34532. if (state.eat(0x28 /* ( */)) {
  34533. if (state.eat(0x3F /* ? */) && state.eat(0x3A /* : */)) {
  34534. this.regexp_disjunction(state);
  34535. if (state.eat(0x29 /* ) */)) {
  34536. return true
  34537. }
  34538. state.raise("Unterminated group");
  34539. }
  34540. state.pos = start;
  34541. }
  34542. return false
  34543. };
  34544. pp$1.regexp_eatCapturingGroup = function(state) {
  34545. if (state.eat(0x28 /* ( */)) {
  34546. if (this.options.ecmaVersion >= 9) {
  34547. this.regexp_groupSpecifier(state);
  34548. } else if (state.current() === 0x3F /* ? */) {
  34549. state.raise("Invalid group");
  34550. }
  34551. this.regexp_disjunction(state);
  34552. if (state.eat(0x29 /* ) */)) {
  34553. state.numCapturingParens += 1;
  34554. return true
  34555. }
  34556. state.raise("Unterminated group");
  34557. }
  34558. return false
  34559. };
  34560. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ExtendedAtom
  34561. pp$1.regexp_eatExtendedAtom = function(state) {
  34562. return (
  34563. state.eat(0x2E /* . */) ||
  34564. this.regexp_eatReverseSolidusAtomEscape(state) ||
  34565. this.regexp_eatCharacterClass(state) ||
  34566. this.regexp_eatUncapturingGroup(state) ||
  34567. this.regexp_eatCapturingGroup(state) ||
  34568. this.regexp_eatInvalidBracedQuantifier(state) ||
  34569. this.regexp_eatExtendedPatternCharacter(state)
  34570. )
  34571. };
  34572. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-InvalidBracedQuantifier
  34573. pp$1.regexp_eatInvalidBracedQuantifier = function(state) {
  34574. if (this.regexp_eatBracedQuantifier(state, true)) {
  34575. state.raise("Nothing to repeat");
  34576. }
  34577. return false
  34578. };
  34579. // https://www.ecma-international.org/ecma-262/8.0/#prod-SyntaxCharacter
  34580. pp$1.regexp_eatSyntaxCharacter = function(state) {
  34581. var ch = state.current();
  34582. if (isSyntaxCharacter(ch)) {
  34583. state.lastIntValue = ch;
  34584. state.advance();
  34585. return true
  34586. }
  34587. return false
  34588. };
  34589. function isSyntaxCharacter(ch) {
  34590. return (
  34591. ch === 0x24 /* $ */ ||
  34592. ch >= 0x28 /* ( */ && ch <= 0x2B /* + */ ||
  34593. ch === 0x2E /* . */ ||
  34594. ch === 0x3F /* ? */ ||
  34595. ch >= 0x5B /* [ */ && ch <= 0x5E /* ^ */ ||
  34596. ch >= 0x7B /* { */ && ch <= 0x7D /* } */
  34597. )
  34598. }
  34599. // https://www.ecma-international.org/ecma-262/8.0/#prod-PatternCharacter
  34600. // But eat eager.
  34601. pp$1.regexp_eatPatternCharacters = function(state) {
  34602. var start = state.pos;
  34603. var ch = 0;
  34604. while ((ch = state.current()) !== -1 && !isSyntaxCharacter(ch)) {
  34605. state.advance();
  34606. }
  34607. return state.pos !== start
  34608. };
  34609. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ExtendedPatternCharacter
  34610. pp$1.regexp_eatExtendedPatternCharacter = function(state) {
  34611. var ch = state.current();
  34612. if (
  34613. ch !== -1 &&
  34614. ch !== 0x24 /* $ */ &&
  34615. !(ch >= 0x28 /* ( */ && ch <= 0x2B /* + */) &&
  34616. ch !== 0x2E /* . */ &&
  34617. ch !== 0x3F /* ? */ &&
  34618. ch !== 0x5B /* [ */ &&
  34619. ch !== 0x5E /* ^ */ &&
  34620. ch !== 0x7C /* | */
  34621. ) {
  34622. state.advance();
  34623. return true
  34624. }
  34625. return false
  34626. };
  34627. // GroupSpecifier ::
  34628. // [empty]
  34629. // `?` GroupName
  34630. pp$1.regexp_groupSpecifier = function(state) {
  34631. if (state.eat(0x3F /* ? */)) {
  34632. if (this.regexp_eatGroupName(state)) {
  34633. if (state.groupNames.indexOf(state.lastStringValue) !== -1) {
  34634. state.raise("Duplicate capture group name");
  34635. }
  34636. state.groupNames.push(state.lastStringValue);
  34637. return
  34638. }
  34639. state.raise("Invalid group");
  34640. }
  34641. };
  34642. // GroupName ::
  34643. // `<` RegExpIdentifierName `>`
  34644. // Note: this updates `state.lastStringValue` property with the eaten name.
  34645. pp$1.regexp_eatGroupName = function(state) {
  34646. state.lastStringValue = "";
  34647. if (state.eat(0x3C /* < */)) {
  34648. if (this.regexp_eatRegExpIdentifierName(state) && state.eat(0x3E /* > */)) {
  34649. return true
  34650. }
  34651. state.raise("Invalid capture group name");
  34652. }
  34653. return false
  34654. };
  34655. // RegExpIdentifierName ::
  34656. // RegExpIdentifierStart
  34657. // RegExpIdentifierName RegExpIdentifierPart
  34658. // Note: this updates `state.lastStringValue` property with the eaten name.
  34659. pp$1.regexp_eatRegExpIdentifierName = function(state) {
  34660. state.lastStringValue = "";
  34661. if (this.regexp_eatRegExpIdentifierStart(state)) {
  34662. state.lastStringValue += codePointToString(state.lastIntValue);
  34663. while (this.regexp_eatRegExpIdentifierPart(state)) {
  34664. state.lastStringValue += codePointToString(state.lastIntValue);
  34665. }
  34666. return true
  34667. }
  34668. return false
  34669. };
  34670. // RegExpIdentifierStart ::
  34671. // UnicodeIDStart
  34672. // `$`
  34673. // `_`
  34674. // `\` RegExpUnicodeEscapeSequence[+U]
  34675. pp$1.regexp_eatRegExpIdentifierStart = function(state) {
  34676. var start = state.pos;
  34677. var forceU = this.options.ecmaVersion >= 11;
  34678. var ch = state.current(forceU);
  34679. state.advance(forceU);
  34680. if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) {
  34681. ch = state.lastIntValue;
  34682. }
  34683. if (isRegExpIdentifierStart(ch)) {
  34684. state.lastIntValue = ch;
  34685. return true
  34686. }
  34687. state.pos = start;
  34688. return false
  34689. };
  34690. function isRegExpIdentifierStart(ch) {
  34691. return isIdentifierStart(ch, true) || ch === 0x24 /* $ */ || ch === 0x5F /* _ */
  34692. }
  34693. // RegExpIdentifierPart ::
  34694. // UnicodeIDContinue
  34695. // `$`
  34696. // `_`
  34697. // `\` RegExpUnicodeEscapeSequence[+U]
  34698. // <ZWNJ>
  34699. // <ZWJ>
  34700. pp$1.regexp_eatRegExpIdentifierPart = function(state) {
  34701. var start = state.pos;
  34702. var forceU = this.options.ecmaVersion >= 11;
  34703. var ch = state.current(forceU);
  34704. state.advance(forceU);
  34705. if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) {
  34706. ch = state.lastIntValue;
  34707. }
  34708. if (isRegExpIdentifierPart(ch)) {
  34709. state.lastIntValue = ch;
  34710. return true
  34711. }
  34712. state.pos = start;
  34713. return false
  34714. };
  34715. function isRegExpIdentifierPart(ch) {
  34716. return isIdentifierChar(ch, true) || ch === 0x24 /* $ */ || ch === 0x5F /* _ */ || ch === 0x200C /* <ZWNJ> */ || ch === 0x200D /* <ZWJ> */
  34717. }
  34718. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-AtomEscape
  34719. pp$1.regexp_eatAtomEscape = function(state) {
  34720. if (
  34721. this.regexp_eatBackReference(state) ||
  34722. this.regexp_eatCharacterClassEscape(state) ||
  34723. this.regexp_eatCharacterEscape(state) ||
  34724. (state.switchN && this.regexp_eatKGroupName(state))
  34725. ) {
  34726. return true
  34727. }
  34728. if (state.switchU) {
  34729. // Make the same message as V8.
  34730. if (state.current() === 0x63 /* c */) {
  34731. state.raise("Invalid unicode escape");
  34732. }
  34733. state.raise("Invalid escape");
  34734. }
  34735. return false
  34736. };
  34737. pp$1.regexp_eatBackReference = function(state) {
  34738. var start = state.pos;
  34739. if (this.regexp_eatDecimalEscape(state)) {
  34740. var n = state.lastIntValue;
  34741. if (state.switchU) {
  34742. // For SyntaxError in https://www.ecma-international.org/ecma-262/8.0/#sec-atomescape
  34743. if (n > state.maxBackReference) {
  34744. state.maxBackReference = n;
  34745. }
  34746. return true
  34747. }
  34748. if (n <= state.numCapturingParens) {
  34749. return true
  34750. }
  34751. state.pos = start;
  34752. }
  34753. return false
  34754. };
  34755. pp$1.regexp_eatKGroupName = function(state) {
  34756. if (state.eat(0x6B /* k */)) {
  34757. if (this.regexp_eatGroupName(state)) {
  34758. state.backReferenceNames.push(state.lastStringValue);
  34759. return true
  34760. }
  34761. state.raise("Invalid named reference");
  34762. }
  34763. return false
  34764. };
  34765. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-CharacterEscape
  34766. pp$1.regexp_eatCharacterEscape = function(state) {
  34767. return (
  34768. this.regexp_eatControlEscape(state) ||
  34769. this.regexp_eatCControlLetter(state) ||
  34770. this.regexp_eatZero(state) ||
  34771. this.regexp_eatHexEscapeSequence(state) ||
  34772. this.regexp_eatRegExpUnicodeEscapeSequence(state, false) ||
  34773. (!state.switchU && this.regexp_eatLegacyOctalEscapeSequence(state)) ||
  34774. this.regexp_eatIdentityEscape(state)
  34775. )
  34776. };
  34777. pp$1.regexp_eatCControlLetter = function(state) {
  34778. var start = state.pos;
  34779. if (state.eat(0x63 /* c */)) {
  34780. if (this.regexp_eatControlLetter(state)) {
  34781. return true
  34782. }
  34783. state.pos = start;
  34784. }
  34785. return false
  34786. };
  34787. pp$1.regexp_eatZero = function(state) {
  34788. if (state.current() === 0x30 /* 0 */ && !isDecimalDigit(state.lookahead())) {
  34789. state.lastIntValue = 0;
  34790. state.advance();
  34791. return true
  34792. }
  34793. return false
  34794. };
  34795. // https://www.ecma-international.org/ecma-262/8.0/#prod-ControlEscape
  34796. pp$1.regexp_eatControlEscape = function(state) {
  34797. var ch = state.current();
  34798. if (ch === 0x74 /* t */) {
  34799. state.lastIntValue = 0x09; /* \t */
  34800. state.advance();
  34801. return true
  34802. }
  34803. if (ch === 0x6E /* n */) {
  34804. state.lastIntValue = 0x0A; /* \n */
  34805. state.advance();
  34806. return true
  34807. }
  34808. if (ch === 0x76 /* v */) {
  34809. state.lastIntValue = 0x0B; /* \v */
  34810. state.advance();
  34811. return true
  34812. }
  34813. if (ch === 0x66 /* f */) {
  34814. state.lastIntValue = 0x0C; /* \f */
  34815. state.advance();
  34816. return true
  34817. }
  34818. if (ch === 0x72 /* r */) {
  34819. state.lastIntValue = 0x0D; /* \r */
  34820. state.advance();
  34821. return true
  34822. }
  34823. return false
  34824. };
  34825. // https://www.ecma-international.org/ecma-262/8.0/#prod-ControlLetter
  34826. pp$1.regexp_eatControlLetter = function(state) {
  34827. var ch = state.current();
  34828. if (isControlLetter(ch)) {
  34829. state.lastIntValue = ch % 0x20;
  34830. state.advance();
  34831. return true
  34832. }
  34833. return false
  34834. };
  34835. function isControlLetter(ch) {
  34836. return (
  34837. (ch >= 0x41 /* A */ && ch <= 0x5A /* Z */) ||
  34838. (ch >= 0x61 /* a */ && ch <= 0x7A /* z */)
  34839. )
  34840. }
  34841. // https://www.ecma-international.org/ecma-262/8.0/#prod-RegExpUnicodeEscapeSequence
  34842. pp$1.regexp_eatRegExpUnicodeEscapeSequence = function(state, forceU) {
  34843. if ( forceU === void 0 ) forceU = false;
  34844. var start = state.pos;
  34845. var switchU = forceU || state.switchU;
  34846. if (state.eat(0x75 /* u */)) {
  34847. if (this.regexp_eatFixedHexDigits(state, 4)) {
  34848. var lead = state.lastIntValue;
  34849. if (switchU && lead >= 0xD800 && lead <= 0xDBFF) {
  34850. var leadSurrogateEnd = state.pos;
  34851. if (state.eat(0x5C /* \ */) && state.eat(0x75 /* u */) && this.regexp_eatFixedHexDigits(state, 4)) {
  34852. var trail = state.lastIntValue;
  34853. if (trail >= 0xDC00 && trail <= 0xDFFF) {
  34854. state.lastIntValue = (lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000;
  34855. return true
  34856. }
  34857. }
  34858. state.pos = leadSurrogateEnd;
  34859. state.lastIntValue = lead;
  34860. }
  34861. return true
  34862. }
  34863. if (
  34864. switchU &&
  34865. state.eat(0x7B /* { */) &&
  34866. this.regexp_eatHexDigits(state) &&
  34867. state.eat(0x7D /* } */) &&
  34868. isValidUnicode(state.lastIntValue)
  34869. ) {
  34870. return true
  34871. }
  34872. if (switchU) {
  34873. state.raise("Invalid unicode escape");
  34874. }
  34875. state.pos = start;
  34876. }
  34877. return false
  34878. };
  34879. function isValidUnicode(ch) {
  34880. return ch >= 0 && ch <= 0x10FFFF
  34881. }
  34882. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-IdentityEscape
  34883. pp$1.regexp_eatIdentityEscape = function(state) {
  34884. if (state.switchU) {
  34885. if (this.regexp_eatSyntaxCharacter(state)) {
  34886. return true
  34887. }
  34888. if (state.eat(0x2F /* / */)) {
  34889. state.lastIntValue = 0x2F; /* / */
  34890. return true
  34891. }
  34892. return false
  34893. }
  34894. var ch = state.current();
  34895. if (ch !== 0x63 /* c */ && (!state.switchN || ch !== 0x6B /* k */)) {
  34896. state.lastIntValue = ch;
  34897. state.advance();
  34898. return true
  34899. }
  34900. return false
  34901. };
  34902. // https://www.ecma-international.org/ecma-262/8.0/#prod-DecimalEscape
  34903. pp$1.regexp_eatDecimalEscape = function(state) {
  34904. state.lastIntValue = 0;
  34905. var ch = state.current();
  34906. if (ch >= 0x31 /* 1 */ && ch <= 0x39 /* 9 */) {
  34907. do {
  34908. state.lastIntValue = 10 * state.lastIntValue + (ch - 0x30 /* 0 */);
  34909. state.advance();
  34910. } while ((ch = state.current()) >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */)
  34911. return true
  34912. }
  34913. return false
  34914. };
  34915. // Return values used by character set parsing methods, needed to
  34916. // forbid negation of sets that can match strings.
  34917. var CharSetNone = 0; // Nothing parsed
  34918. var CharSetOk = 1; // Construct parsed, cannot contain strings
  34919. var CharSetString = 2; // Construct parsed, can contain strings
  34920. // https://www.ecma-international.org/ecma-262/8.0/#prod-CharacterClassEscape
  34921. pp$1.regexp_eatCharacterClassEscape = function(state) {
  34922. var ch = state.current();
  34923. if (isCharacterClassEscape(ch)) {
  34924. state.lastIntValue = -1;
  34925. state.advance();
  34926. return CharSetOk
  34927. }
  34928. var negate = false;
  34929. if (
  34930. state.switchU &&
  34931. this.options.ecmaVersion >= 9 &&
  34932. ((negate = ch === 0x50 /* P */) || ch === 0x70 /* p */)
  34933. ) {
  34934. state.lastIntValue = -1;
  34935. state.advance();
  34936. var result;
  34937. if (
  34938. state.eat(0x7B /* { */) &&
  34939. (result = this.regexp_eatUnicodePropertyValueExpression(state)) &&
  34940. state.eat(0x7D /* } */)
  34941. ) {
  34942. if (negate && result === CharSetString) { state.raise("Invalid property name"); }
  34943. return result
  34944. }
  34945. state.raise("Invalid property name");
  34946. }
  34947. return CharSetNone
  34948. };
  34949. function isCharacterClassEscape(ch) {
  34950. return (
  34951. ch === 0x64 /* d */ ||
  34952. ch === 0x44 /* D */ ||
  34953. ch === 0x73 /* s */ ||
  34954. ch === 0x53 /* S */ ||
  34955. ch === 0x77 /* w */ ||
  34956. ch === 0x57 /* W */
  34957. )
  34958. }
  34959. // UnicodePropertyValueExpression ::
  34960. // UnicodePropertyName `=` UnicodePropertyValue
  34961. // LoneUnicodePropertyNameOrValue
  34962. pp$1.regexp_eatUnicodePropertyValueExpression = function(state) {
  34963. var start = state.pos;
  34964. // UnicodePropertyName `=` UnicodePropertyValue
  34965. if (this.regexp_eatUnicodePropertyName(state) && state.eat(0x3D /* = */)) {
  34966. var name = state.lastStringValue;
  34967. if (this.regexp_eatUnicodePropertyValue(state)) {
  34968. var value = state.lastStringValue;
  34969. this.regexp_validateUnicodePropertyNameAndValue(state, name, value);
  34970. return CharSetOk
  34971. }
  34972. }
  34973. state.pos = start;
  34974. // LoneUnicodePropertyNameOrValue
  34975. if (this.regexp_eatLoneUnicodePropertyNameOrValue(state)) {
  34976. var nameOrValue = state.lastStringValue;
  34977. return this.regexp_validateUnicodePropertyNameOrValue(state, nameOrValue)
  34978. }
  34979. return CharSetNone
  34980. };
  34981. pp$1.regexp_validateUnicodePropertyNameAndValue = function(state, name, value) {
  34982. if (!hasOwn(state.unicodeProperties.nonBinary, name))
  34983. { state.raise("Invalid property name"); }
  34984. if (!state.unicodeProperties.nonBinary[name].test(value))
  34985. { state.raise("Invalid property value"); }
  34986. };
  34987. pp$1.regexp_validateUnicodePropertyNameOrValue = function(state, nameOrValue) {
  34988. if (state.unicodeProperties.binary.test(nameOrValue)) { return CharSetOk }
  34989. if (state.switchV && state.unicodeProperties.binaryOfStrings.test(nameOrValue)) { return CharSetString }
  34990. state.raise("Invalid property name");
  34991. };
  34992. // UnicodePropertyName ::
  34993. // UnicodePropertyNameCharacters
  34994. pp$1.regexp_eatUnicodePropertyName = function(state) {
  34995. var ch = 0;
  34996. state.lastStringValue = "";
  34997. while (isUnicodePropertyNameCharacter(ch = state.current())) {
  34998. state.lastStringValue += codePointToString(ch);
  34999. state.advance();
  35000. }
  35001. return state.lastStringValue !== ""
  35002. };
  35003. function isUnicodePropertyNameCharacter(ch) {
  35004. return isControlLetter(ch) || ch === 0x5F /* _ */
  35005. }
  35006. // UnicodePropertyValue ::
  35007. // UnicodePropertyValueCharacters
  35008. pp$1.regexp_eatUnicodePropertyValue = function(state) {
  35009. var ch = 0;
  35010. state.lastStringValue = "";
  35011. while (isUnicodePropertyValueCharacter(ch = state.current())) {
  35012. state.lastStringValue += codePointToString(ch);
  35013. state.advance();
  35014. }
  35015. return state.lastStringValue !== ""
  35016. };
  35017. function isUnicodePropertyValueCharacter(ch) {
  35018. return isUnicodePropertyNameCharacter(ch) || isDecimalDigit(ch)
  35019. }
  35020. // LoneUnicodePropertyNameOrValue ::
  35021. // UnicodePropertyValueCharacters
  35022. pp$1.regexp_eatLoneUnicodePropertyNameOrValue = function(state) {
  35023. return this.regexp_eatUnicodePropertyValue(state)
  35024. };
  35025. // https://www.ecma-international.org/ecma-262/8.0/#prod-CharacterClass
  35026. pp$1.regexp_eatCharacterClass = function(state) {
  35027. if (state.eat(0x5B /* [ */)) {
  35028. var negate = state.eat(0x5E /* ^ */);
  35029. var result = this.regexp_classContents(state);
  35030. if (!state.eat(0x5D /* ] */))
  35031. { state.raise("Unterminated character class"); }
  35032. if (negate && result === CharSetString)
  35033. { state.raise("Negated character class may contain strings"); }
  35034. return true
  35035. }
  35036. return false
  35037. };
  35038. // https://tc39.es/ecma262/#prod-ClassContents
  35039. // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassRanges
  35040. pp$1.regexp_classContents = function(state) {
  35041. if (state.current() === 0x5D /* ] */) { return CharSetOk }
  35042. if (state.switchV) { return this.regexp_classSetExpression(state) }
  35043. this.regexp_nonEmptyClassRanges(state);
  35044. return CharSetOk
  35045. };
  35046. // https://www.ecma-international.org/ecma-262/8.0/#prod-NonemptyClassRanges
  35047. // https://www.ecma-international.org/ecma-262/8.0/#prod-NonemptyClassRangesNoDash
  35048. pp$1.regexp_nonEmptyClassRanges = function(state) {
  35049. while (this.regexp_eatClassAtom(state)) {
  35050. var left = state.lastIntValue;
  35051. if (state.eat(0x2D /* - */) && this.regexp_eatClassAtom(state)) {
  35052. var right = state.lastIntValue;
  35053. if (state.switchU && (left === -1 || right === -1)) {
  35054. state.raise("Invalid character class");
  35055. }
  35056. if (left !== -1 && right !== -1 && left > right) {
  35057. state.raise("Range out of order in character class");
  35058. }
  35059. }
  35060. }
  35061. };
  35062. // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassAtom
  35063. // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassAtomNoDash
  35064. pp$1.regexp_eatClassAtom = function(state) {
  35065. var start = state.pos;
  35066. if (state.eat(0x5C /* \ */)) {
  35067. if (this.regexp_eatClassEscape(state)) {
  35068. return true
  35069. }
  35070. if (state.switchU) {
  35071. // Make the same message as V8.
  35072. var ch$1 = state.current();
  35073. if (ch$1 === 0x63 /* c */ || isOctalDigit(ch$1)) {
  35074. state.raise("Invalid class escape");
  35075. }
  35076. state.raise("Invalid escape");
  35077. }
  35078. state.pos = start;
  35079. }
  35080. var ch = state.current();
  35081. if (ch !== 0x5D /* ] */) {
  35082. state.lastIntValue = ch;
  35083. state.advance();
  35084. return true
  35085. }
  35086. return false
  35087. };
  35088. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ClassEscape
  35089. pp$1.regexp_eatClassEscape = function(state) {
  35090. var start = state.pos;
  35091. if (state.eat(0x62 /* b */)) {
  35092. state.lastIntValue = 0x08; /* <BS> */
  35093. return true
  35094. }
  35095. if (state.switchU && state.eat(0x2D /* - */)) {
  35096. state.lastIntValue = 0x2D; /* - */
  35097. return true
  35098. }
  35099. if (!state.switchU && state.eat(0x63 /* c */)) {
  35100. if (this.regexp_eatClassControlLetter(state)) {
  35101. return true
  35102. }
  35103. state.pos = start;
  35104. }
  35105. return (
  35106. this.regexp_eatCharacterClassEscape(state) ||
  35107. this.regexp_eatCharacterEscape(state)
  35108. )
  35109. };
  35110. // https://tc39.es/ecma262/#prod-ClassSetExpression
  35111. // https://tc39.es/ecma262/#prod-ClassUnion
  35112. // https://tc39.es/ecma262/#prod-ClassIntersection
  35113. // https://tc39.es/ecma262/#prod-ClassSubtraction
  35114. pp$1.regexp_classSetExpression = function(state) {
  35115. var result = CharSetOk, subResult;
  35116. if (this.regexp_eatClassSetRange(state)) ; else if (subResult = this.regexp_eatClassSetOperand(state)) {
  35117. if (subResult === CharSetString) { result = CharSetString; }
  35118. // https://tc39.es/ecma262/#prod-ClassIntersection
  35119. var start = state.pos;
  35120. while (state.eatChars([0x26, 0x26] /* && */)) {
  35121. if (
  35122. state.current() !== 0x26 /* & */ &&
  35123. (subResult = this.regexp_eatClassSetOperand(state))
  35124. ) {
  35125. if (subResult !== CharSetString) { result = CharSetOk; }
  35126. continue
  35127. }
  35128. state.raise("Invalid character in character class");
  35129. }
  35130. if (start !== state.pos) { return result }
  35131. // https://tc39.es/ecma262/#prod-ClassSubtraction
  35132. while (state.eatChars([0x2D, 0x2D] /* -- */)) {
  35133. if (this.regexp_eatClassSetOperand(state)) { continue }
  35134. state.raise("Invalid character in character class");
  35135. }
  35136. if (start !== state.pos) { return result }
  35137. } else {
  35138. state.raise("Invalid character in character class");
  35139. }
  35140. // https://tc39.es/ecma262/#prod-ClassUnion
  35141. for (;;) {
  35142. if (this.regexp_eatClassSetRange(state)) { continue }
  35143. subResult = this.regexp_eatClassSetOperand(state);
  35144. if (!subResult) { return result }
  35145. if (subResult === CharSetString) { result = CharSetString; }
  35146. }
  35147. };
  35148. // https://tc39.es/ecma262/#prod-ClassSetRange
  35149. pp$1.regexp_eatClassSetRange = function(state) {
  35150. var start = state.pos;
  35151. if (this.regexp_eatClassSetCharacter(state)) {
  35152. var left = state.lastIntValue;
  35153. if (state.eat(0x2D /* - */) && this.regexp_eatClassSetCharacter(state)) {
  35154. var right = state.lastIntValue;
  35155. if (left !== -1 && right !== -1 && left > right) {
  35156. state.raise("Range out of order in character class");
  35157. }
  35158. return true
  35159. }
  35160. state.pos = start;
  35161. }
  35162. return false
  35163. };
  35164. // https://tc39.es/ecma262/#prod-ClassSetOperand
  35165. pp$1.regexp_eatClassSetOperand = function(state) {
  35166. if (this.regexp_eatClassSetCharacter(state)) { return CharSetOk }
  35167. return this.regexp_eatClassStringDisjunction(state) || this.regexp_eatNestedClass(state)
  35168. };
  35169. // https://tc39.es/ecma262/#prod-NestedClass
  35170. pp$1.regexp_eatNestedClass = function(state) {
  35171. var start = state.pos;
  35172. if (state.eat(0x5B /* [ */)) {
  35173. var negate = state.eat(0x5E /* ^ */);
  35174. var result = this.regexp_classContents(state);
  35175. if (state.eat(0x5D /* ] */)) {
  35176. if (negate && result === CharSetString) {
  35177. state.raise("Negated character class may contain strings");
  35178. }
  35179. return result
  35180. }
  35181. state.pos = start;
  35182. }
  35183. if (state.eat(0x5C /* \ */)) {
  35184. var result$1 = this.regexp_eatCharacterClassEscape(state);
  35185. if (result$1) {
  35186. return result$1
  35187. }
  35188. state.pos = start;
  35189. }
  35190. return null
  35191. };
  35192. // https://tc39.es/ecma262/#prod-ClassStringDisjunction
  35193. pp$1.regexp_eatClassStringDisjunction = function(state) {
  35194. var start = state.pos;
  35195. if (state.eatChars([0x5C, 0x71] /* \q */)) {
  35196. if (state.eat(0x7B /* { */)) {
  35197. var result = this.regexp_classStringDisjunctionContents(state);
  35198. if (state.eat(0x7D /* } */)) {
  35199. return result
  35200. }
  35201. } else {
  35202. // Make the same message as V8.
  35203. state.raise("Invalid escape");
  35204. }
  35205. state.pos = start;
  35206. }
  35207. return null
  35208. };
  35209. // https://tc39.es/ecma262/#prod-ClassStringDisjunctionContents
  35210. pp$1.regexp_classStringDisjunctionContents = function(state) {
  35211. var result = this.regexp_classString(state);
  35212. while (state.eat(0x7C /* | */)) {
  35213. if (this.regexp_classString(state) === CharSetString) { result = CharSetString; }
  35214. }
  35215. return result
  35216. };
  35217. // https://tc39.es/ecma262/#prod-ClassString
  35218. // https://tc39.es/ecma262/#prod-NonEmptyClassString
  35219. pp$1.regexp_classString = function(state) {
  35220. var count = 0;
  35221. while (this.regexp_eatClassSetCharacter(state)) { count++; }
  35222. return count === 1 ? CharSetOk : CharSetString
  35223. };
  35224. // https://tc39.es/ecma262/#prod-ClassSetCharacter
  35225. pp$1.regexp_eatClassSetCharacter = function(state) {
  35226. var start = state.pos;
  35227. if (state.eat(0x5C /* \ */)) {
  35228. if (
  35229. this.regexp_eatCharacterEscape(state) ||
  35230. this.regexp_eatClassSetReservedPunctuator(state)
  35231. ) {
  35232. return true
  35233. }
  35234. if (state.eat(0x62 /* b */)) {
  35235. state.lastIntValue = 0x08; /* <BS> */
  35236. return true
  35237. }
  35238. state.pos = start;
  35239. return false
  35240. }
  35241. var ch = state.current();
  35242. if (ch < 0 || ch === state.lookahead() && isClassSetReservedDoublePunctuatorCharacter(ch)) { return false }
  35243. if (isClassSetSyntaxCharacter(ch)) { return false }
  35244. state.advance();
  35245. state.lastIntValue = ch;
  35246. return true
  35247. };
  35248. // https://tc39.es/ecma262/#prod-ClassSetReservedDoublePunctuator
  35249. function isClassSetReservedDoublePunctuatorCharacter(ch) {
  35250. return (
  35251. ch === 0x21 /* ! */ ||
  35252. ch >= 0x23 /* # */ && ch <= 0x26 /* & */ ||
  35253. ch >= 0x2A /* * */ && ch <= 0x2C /* , */ ||
  35254. ch === 0x2E /* . */ ||
  35255. ch >= 0x3A /* : */ && ch <= 0x40 /* @ */ ||
  35256. ch === 0x5E /* ^ */ ||
  35257. ch === 0x60 /* ` */ ||
  35258. ch === 0x7E /* ~ */
  35259. )
  35260. }
  35261. // https://tc39.es/ecma262/#prod-ClassSetSyntaxCharacter
  35262. function isClassSetSyntaxCharacter(ch) {
  35263. return (
  35264. ch === 0x28 /* ( */ ||
  35265. ch === 0x29 /* ) */ ||
  35266. ch === 0x2D /* - */ ||
  35267. ch === 0x2F /* / */ ||
  35268. ch >= 0x5B /* [ */ && ch <= 0x5D /* ] */ ||
  35269. ch >= 0x7B /* { */ && ch <= 0x7D /* } */
  35270. )
  35271. }
  35272. // https://tc39.es/ecma262/#prod-ClassSetReservedPunctuator
  35273. pp$1.regexp_eatClassSetReservedPunctuator = function(state) {
  35274. var ch = state.current();
  35275. if (isClassSetReservedPunctuator(ch)) {
  35276. state.lastIntValue = ch;
  35277. state.advance();
  35278. return true
  35279. }
  35280. return false
  35281. };
  35282. // https://tc39.es/ecma262/#prod-ClassSetReservedPunctuator
  35283. function isClassSetReservedPunctuator(ch) {
  35284. return (
  35285. ch === 0x21 /* ! */ ||
  35286. ch === 0x23 /* # */ ||
  35287. ch === 0x25 /* % */ ||
  35288. ch === 0x26 /* & */ ||
  35289. ch === 0x2C /* , */ ||
  35290. ch === 0x2D /* - */ ||
  35291. ch >= 0x3A /* : */ && ch <= 0x3E /* > */ ||
  35292. ch === 0x40 /* @ */ ||
  35293. ch === 0x60 /* ` */ ||
  35294. ch === 0x7E /* ~ */
  35295. )
  35296. }
  35297. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ClassControlLetter
  35298. pp$1.regexp_eatClassControlLetter = function(state) {
  35299. var ch = state.current();
  35300. if (isDecimalDigit(ch) || ch === 0x5F /* _ */) {
  35301. state.lastIntValue = ch % 0x20;
  35302. state.advance();
  35303. return true
  35304. }
  35305. return false
  35306. };
  35307. // https://www.ecma-international.org/ecma-262/8.0/#prod-HexEscapeSequence
  35308. pp$1.regexp_eatHexEscapeSequence = function(state) {
  35309. var start = state.pos;
  35310. if (state.eat(0x78 /* x */)) {
  35311. if (this.regexp_eatFixedHexDigits(state, 2)) {
  35312. return true
  35313. }
  35314. if (state.switchU) {
  35315. state.raise("Invalid escape");
  35316. }
  35317. state.pos = start;
  35318. }
  35319. return false
  35320. };
  35321. // https://www.ecma-international.org/ecma-262/8.0/#prod-DecimalDigits
  35322. pp$1.regexp_eatDecimalDigits = function(state) {
  35323. var start = state.pos;
  35324. var ch = 0;
  35325. state.lastIntValue = 0;
  35326. while (isDecimalDigit(ch = state.current())) {
  35327. state.lastIntValue = 10 * state.lastIntValue + (ch - 0x30 /* 0 */);
  35328. state.advance();
  35329. }
  35330. return state.pos !== start
  35331. };
  35332. function isDecimalDigit(ch) {
  35333. return ch >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */
  35334. }
  35335. // https://www.ecma-international.org/ecma-262/8.0/#prod-HexDigits
  35336. pp$1.regexp_eatHexDigits = function(state) {
  35337. var start = state.pos;
  35338. var ch = 0;
  35339. state.lastIntValue = 0;
  35340. while (isHexDigit(ch = state.current())) {
  35341. state.lastIntValue = 16 * state.lastIntValue + hexToInt(ch);
  35342. state.advance();
  35343. }
  35344. return state.pos !== start
  35345. };
  35346. function isHexDigit(ch) {
  35347. return (
  35348. (ch >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */) ||
  35349. (ch >= 0x41 /* A */ && ch <= 0x46 /* F */) ||
  35350. (ch >= 0x61 /* a */ && ch <= 0x66 /* f */)
  35351. )
  35352. }
  35353. function hexToInt(ch) {
  35354. if (ch >= 0x41 /* A */ && ch <= 0x46 /* F */) {
  35355. return 10 + (ch - 0x41 /* A */)
  35356. }
  35357. if (ch >= 0x61 /* a */ && ch <= 0x66 /* f */) {
  35358. return 10 + (ch - 0x61 /* a */)
  35359. }
  35360. return ch - 0x30 /* 0 */
  35361. }
  35362. // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-LegacyOctalEscapeSequence
  35363. // Allows only 0-377(octal) i.e. 0-255(decimal).
  35364. pp$1.regexp_eatLegacyOctalEscapeSequence = function(state) {
  35365. if (this.regexp_eatOctalDigit(state)) {
  35366. var n1 = state.lastIntValue;
  35367. if (this.regexp_eatOctalDigit(state)) {
  35368. var n2 = state.lastIntValue;
  35369. if (n1 <= 3 && this.regexp_eatOctalDigit(state)) {
  35370. state.lastIntValue = n1 * 64 + n2 * 8 + state.lastIntValue;
  35371. } else {
  35372. state.lastIntValue = n1 * 8 + n2;
  35373. }
  35374. } else {
  35375. state.lastIntValue = n1;
  35376. }
  35377. return true
  35378. }
  35379. return false
  35380. };
  35381. // https://www.ecma-international.org/ecma-262/8.0/#prod-OctalDigit
  35382. pp$1.regexp_eatOctalDigit = function(state) {
  35383. var ch = state.current();
  35384. if (isOctalDigit(ch)) {
  35385. state.lastIntValue = ch - 0x30; /* 0 */
  35386. state.advance();
  35387. return true
  35388. }
  35389. state.lastIntValue = 0;
  35390. return false
  35391. };
  35392. function isOctalDigit(ch) {
  35393. return ch >= 0x30 /* 0 */ && ch <= 0x37 /* 7 */
  35394. }
  35395. // https://www.ecma-international.org/ecma-262/8.0/#prod-Hex4Digits
  35396. // https://www.ecma-international.org/ecma-262/8.0/#prod-HexDigit
  35397. // And HexDigit HexDigit in https://www.ecma-international.org/ecma-262/8.0/#prod-HexEscapeSequence
  35398. pp$1.regexp_eatFixedHexDigits = function(state, length) {
  35399. var start = state.pos;
  35400. state.lastIntValue = 0;
  35401. for (var i = 0; i < length; ++i) {
  35402. var ch = state.current();
  35403. if (!isHexDigit(ch)) {
  35404. state.pos = start;
  35405. return false
  35406. }
  35407. state.lastIntValue = 16 * state.lastIntValue + hexToInt(ch);
  35408. state.advance();
  35409. }
  35410. return true
  35411. };
  35412. // Object type used to represent tokens. Note that normally, tokens
  35413. // simply exist as properties on the parser object. This is only
  35414. // used for the onToken callback and the external tokenizer.
  35415. var Token = function Token(p) {
  35416. this.type = p.type;
  35417. this.value = p.value;
  35418. this.start = p.start;
  35419. this.end = p.end;
  35420. if (p.options.locations)
  35421. { this.loc = new SourceLocation(p, p.startLoc, p.endLoc); }
  35422. if (p.options.ranges)
  35423. { this.range = [p.start, p.end]; }
  35424. };
  35425. // ## Tokenizer
  35426. var pp = Parser.prototype;
  35427. // Move to the next token
  35428. pp.next = function(ignoreEscapeSequenceInKeyword) {
  35429. if (!ignoreEscapeSequenceInKeyword && this.type.keyword && this.containsEsc)
  35430. { this.raiseRecoverable(this.start, "Escape sequence in keyword " + this.type.keyword); }
  35431. if (this.options.onToken)
  35432. { this.options.onToken(new Token(this)); }
  35433. this.lastTokEnd = this.end;
  35434. this.lastTokStart = this.start;
  35435. this.lastTokEndLoc = this.endLoc;
  35436. this.lastTokStartLoc = this.startLoc;
  35437. this.nextToken();
  35438. };
  35439. pp.getToken = function() {
  35440. this.next();
  35441. return new Token(this)
  35442. };
  35443. // If we're in an ES6 environment, make parsers iterable
  35444. if (typeof Symbol !== "undefined")
  35445. { pp[Symbol.iterator] = function() {
  35446. var this$1$1 = this;
  35447. return {
  35448. next: function () {
  35449. var token = this$1$1.getToken();
  35450. return {
  35451. done: token.type === types$1.eof,
  35452. value: token
  35453. }
  35454. }
  35455. }
  35456. }; }
  35457. // Toggle strict mode. Re-reads the next number or string to please
  35458. // pedantic tests (`"use strict"; 010;` should fail).
  35459. // Read a single token, updating the parser object's token-related
  35460. // properties.
  35461. pp.nextToken = function() {
  35462. var curContext = this.curContext();
  35463. if (!curContext || !curContext.preserveSpace) { this.skipSpace(); }
  35464. this.start = this.pos;
  35465. if (this.options.locations) { this.startLoc = this.curPosition(); }
  35466. if (this.pos >= this.input.length) { return this.finishToken(types$1.eof) }
  35467. if (curContext.override) { return curContext.override(this) }
  35468. else { this.readToken(this.fullCharCodeAtPos()); }
  35469. };
  35470. pp.readToken = function(code) {
  35471. // Identifier or keyword. '\uXXXX' sequences are allowed in
  35472. // identifiers, so '\' also dispatches to that.
  35473. if (isIdentifierStart(code, this.options.ecmaVersion >= 6) || code === 92 /* '\' */)
  35474. { return this.readWord() }
  35475. return this.getTokenFromCode(code)
  35476. };
  35477. pp.fullCharCodeAtPos = function() {
  35478. var code = this.input.charCodeAt(this.pos);
  35479. if (code <= 0xd7ff || code >= 0xdc00) { return code }
  35480. var next = this.input.charCodeAt(this.pos + 1);
  35481. return next <= 0xdbff || next >= 0xe000 ? code : (code << 10) + next - 0x35fdc00
  35482. };
  35483. pp.skipBlockComment = function() {
  35484. var startLoc = this.options.onComment && this.curPosition();
  35485. var start = this.pos, end = this.input.indexOf("*/", this.pos += 2);
  35486. if (end === -1) { this.raise(this.pos - 2, "Unterminated comment"); }
  35487. this.pos = end + 2;
  35488. if (this.options.locations) {
  35489. for (var nextBreak = (void 0), pos = start; (nextBreak = nextLineBreak(this.input, pos, this.pos)) > -1;) {
  35490. ++this.curLine;
  35491. pos = this.lineStart = nextBreak;
  35492. }
  35493. }
  35494. if (this.options.onComment)
  35495. { this.options.onComment(true, this.input.slice(start + 2, end), start, this.pos,
  35496. startLoc, this.curPosition()); }
  35497. };
  35498. pp.skipLineComment = function(startSkip) {
  35499. var start = this.pos;
  35500. var startLoc = this.options.onComment && this.curPosition();
  35501. var ch = this.input.charCodeAt(this.pos += startSkip);
  35502. while (this.pos < this.input.length && !isNewLine(ch)) {
  35503. ch = this.input.charCodeAt(++this.pos);
  35504. }
  35505. if (this.options.onComment)
  35506. { this.options.onComment(false, this.input.slice(start + startSkip, this.pos), start, this.pos,
  35507. startLoc, this.curPosition()); }
  35508. };
  35509. // Called at the start of the parse and after every token. Skips
  35510. // whitespace and comments, and.
  35511. pp.skipSpace = function() {
  35512. loop: while (this.pos < this.input.length) {
  35513. var ch = this.input.charCodeAt(this.pos);
  35514. switch (ch) {
  35515. case 32: case 160: // ' '
  35516. ++this.pos;
  35517. break
  35518. case 13:
  35519. if (this.input.charCodeAt(this.pos + 1) === 10) {
  35520. ++this.pos;
  35521. }
  35522. case 10: case 8232: case 8233:
  35523. ++this.pos;
  35524. if (this.options.locations) {
  35525. ++this.curLine;
  35526. this.lineStart = this.pos;
  35527. }
  35528. break
  35529. case 47: // '/'
  35530. switch (this.input.charCodeAt(this.pos + 1)) {
  35531. case 42: // '*'
  35532. this.skipBlockComment();
  35533. break
  35534. case 47:
  35535. this.skipLineComment(2);
  35536. break
  35537. default:
  35538. break loop
  35539. }
  35540. break
  35541. default:
  35542. if (ch > 8 && ch < 14 || ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
  35543. ++this.pos;
  35544. } else {
  35545. break loop
  35546. }
  35547. }
  35548. }
  35549. };
  35550. // Called at the end of every token. Sets `end`, `val`, and
  35551. // maintains `context` and `exprAllowed`, and skips the space after
  35552. // the token, so that the next one's `start` will point at the
  35553. // right position.
  35554. pp.finishToken = function(type, val) {
  35555. this.end = this.pos;
  35556. if (this.options.locations) { this.endLoc = this.curPosition(); }
  35557. var prevType = this.type;
  35558. this.type = type;
  35559. this.value = val;
  35560. this.updateContext(prevType);
  35561. };
  35562. // ### Token reading
  35563. // This is the function that is called to fetch the next token. It
  35564. // is somewhat obscure, because it works in character codes rather
  35565. // than characters, and because operator parsing has been inlined
  35566. // into it.
  35567. //
  35568. // All in the name of speed.
  35569. //
  35570. pp.readToken_dot = function() {
  35571. var next = this.input.charCodeAt(this.pos + 1);
  35572. if (next >= 48 && next <= 57) { return this.readNumber(true) }
  35573. var next2 = this.input.charCodeAt(this.pos + 2);
  35574. if (this.options.ecmaVersion >= 6 && next === 46 && next2 === 46) { // 46 = dot '.'
  35575. this.pos += 3;
  35576. return this.finishToken(types$1.ellipsis)
  35577. } else {
  35578. ++this.pos;
  35579. return this.finishToken(types$1.dot)
  35580. }
  35581. };
  35582. pp.readToken_slash = function() { // '/'
  35583. var next = this.input.charCodeAt(this.pos + 1);
  35584. if (this.exprAllowed) { ++this.pos; return this.readRegexp() }
  35585. if (next === 61) { return this.finishOp(types$1.assign, 2) }
  35586. return this.finishOp(types$1.slash, 1)
  35587. };
  35588. pp.readToken_mult_modulo_exp = function(code) { // '%*'
  35589. var next = this.input.charCodeAt(this.pos + 1);
  35590. var size = 1;
  35591. var tokentype = code === 42 ? types$1.star : types$1.modulo;
  35592. // exponentiation operator ** and **=
  35593. if (this.options.ecmaVersion >= 7 && code === 42 && next === 42) {
  35594. ++size;
  35595. tokentype = types$1.starstar;
  35596. next = this.input.charCodeAt(this.pos + 2);
  35597. }
  35598. if (next === 61) { return this.finishOp(types$1.assign, size + 1) }
  35599. return this.finishOp(tokentype, size)
  35600. };
  35601. pp.readToken_pipe_amp = function(code) { // '|&'
  35602. var next = this.input.charCodeAt(this.pos + 1);
  35603. if (next === code) {
  35604. if (this.options.ecmaVersion >= 12) {
  35605. var next2 = this.input.charCodeAt(this.pos + 2);
  35606. if (next2 === 61) { return this.finishOp(types$1.assign, 3) }
  35607. }
  35608. return this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2)
  35609. }
  35610. if (next === 61) { return this.finishOp(types$1.assign, 2) }
  35611. return this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1)
  35612. };
  35613. pp.readToken_caret = function() { // '^'
  35614. var next = this.input.charCodeAt(this.pos + 1);
  35615. if (next === 61) { return this.finishOp(types$1.assign, 2) }
  35616. return this.finishOp(types$1.bitwiseXOR, 1)
  35617. };
  35618. pp.readToken_plus_min = function(code) { // '+-'
  35619. var next = this.input.charCodeAt(this.pos + 1);
  35620. if (next === code) {
  35621. if (next === 45 && !this.inModule && this.input.charCodeAt(this.pos + 2) === 62 &&
  35622. (this.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.lastTokEnd, this.pos)))) {
  35623. // A `-->` line comment
  35624. this.skipLineComment(3);
  35625. this.skipSpace();
  35626. return this.nextToken()
  35627. }
  35628. return this.finishOp(types$1.incDec, 2)
  35629. }
  35630. if (next === 61) { return this.finishOp(types$1.assign, 2) }
  35631. return this.finishOp(types$1.plusMin, 1)
  35632. };
  35633. pp.readToken_lt_gt = function(code) { // '<>'
  35634. var next = this.input.charCodeAt(this.pos + 1);
  35635. var size = 1;
  35636. if (next === code) {
  35637. size = code === 62 && this.input.charCodeAt(this.pos + 2) === 62 ? 3 : 2;
  35638. if (this.input.charCodeAt(this.pos + size) === 61) { return this.finishOp(types$1.assign, size + 1) }
  35639. return this.finishOp(types$1.bitShift, size)
  35640. }
  35641. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.pos + 2) === 45 &&
  35642. this.input.charCodeAt(this.pos + 3) === 45) {
  35643. // `<!--`, an XML-style comment that should be interpreted as a line comment
  35644. this.skipLineComment(4);
  35645. this.skipSpace();
  35646. return this.nextToken()
  35647. }
  35648. if (next === 61) { size = 2; }
  35649. return this.finishOp(types$1.relational, size)
  35650. };
  35651. pp.readToken_eq_excl = function(code) { // '=!'
  35652. var next = this.input.charCodeAt(this.pos + 1);
  35653. if (next === 61) { return this.finishOp(types$1.equality, this.input.charCodeAt(this.pos + 2) === 61 ? 3 : 2) }
  35654. if (code === 61 && next === 62 && this.options.ecmaVersion >= 6) { // '=>'
  35655. this.pos += 2;
  35656. return this.finishToken(types$1.arrow)
  35657. }
  35658. return this.finishOp(code === 61 ? types$1.eq : types$1.prefix, 1)
  35659. };
  35660. pp.readToken_question = function() { // '?'
  35661. var ecmaVersion = this.options.ecmaVersion;
  35662. if (ecmaVersion >= 11) {
  35663. var next = this.input.charCodeAt(this.pos + 1);
  35664. if (next === 46) {
  35665. var next2 = this.input.charCodeAt(this.pos + 2);
  35666. if (next2 < 48 || next2 > 57) { return this.finishOp(types$1.questionDot, 2) }
  35667. }
  35668. if (next === 63) {
  35669. if (ecmaVersion >= 12) {
  35670. var next2$1 = this.input.charCodeAt(this.pos + 2);
  35671. if (next2$1 === 61) { return this.finishOp(types$1.assign, 3) }
  35672. }
  35673. return this.finishOp(types$1.coalesce, 2)
  35674. }
  35675. }
  35676. return this.finishOp(types$1.question, 1)
  35677. };
  35678. pp.readToken_numberSign = function() { // '#'
  35679. var ecmaVersion = this.options.ecmaVersion;
  35680. var code = 35; // '#'
  35681. if (ecmaVersion >= 13) {
  35682. ++this.pos;
  35683. code = this.fullCharCodeAtPos();
  35684. if (isIdentifierStart(code, true) || code === 92 /* '\' */) {
  35685. return this.finishToken(types$1.privateId, this.readWord1())
  35686. }
  35687. }
  35688. this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'");
  35689. };
  35690. pp.getTokenFromCode = function(code) {
  35691. switch (code) {
  35692. // The interpretation of a dot depends on whether it is followed
  35693. // by a digit or another two dots.
  35694. case 46: // '.'
  35695. return this.readToken_dot()
  35696. // Punctuation tokens.
  35697. case 40: ++this.pos; return this.finishToken(types$1.parenL)
  35698. case 41: ++this.pos; return this.finishToken(types$1.parenR)
  35699. case 59: ++this.pos; return this.finishToken(types$1.semi)
  35700. case 44: ++this.pos; return this.finishToken(types$1.comma)
  35701. case 91: ++this.pos; return this.finishToken(types$1.bracketL)
  35702. case 93: ++this.pos; return this.finishToken(types$1.bracketR)
  35703. case 123: ++this.pos; return this.finishToken(types$1.braceL)
  35704. case 125: ++this.pos; return this.finishToken(types$1.braceR)
  35705. case 58: ++this.pos; return this.finishToken(types$1.colon)
  35706. case 96: // '`'
  35707. if (this.options.ecmaVersion < 6) { break }
  35708. ++this.pos;
  35709. return this.finishToken(types$1.backQuote)
  35710. case 48: // '0'
  35711. var next = this.input.charCodeAt(this.pos + 1);
  35712. if (next === 120 || next === 88) { return this.readRadixNumber(16) } // '0x', '0X' - hex number
  35713. if (this.options.ecmaVersion >= 6) {
  35714. if (next === 111 || next === 79) { return this.readRadixNumber(8) } // '0o', '0O' - octal number
  35715. if (next === 98 || next === 66) { return this.readRadixNumber(2) } // '0b', '0B' - binary number
  35716. }
  35717. // Anything else beginning with a digit is an integer, octal
  35718. // number, or float.
  35719. case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
  35720. return this.readNumber(false)
  35721. // Quotes produce strings.
  35722. case 34: case 39: // '"', "'"
  35723. return this.readString(code)
  35724. // Operators are parsed inline in tiny state machines. '=' (61) is
  35725. // often referred to. `finishOp` simply skips the amount of
  35726. // characters it is given as second argument, and returns a token
  35727. // of the type given by its first argument.
  35728. case 47: // '/'
  35729. return this.readToken_slash()
  35730. case 37: case 42: // '%*'
  35731. return this.readToken_mult_modulo_exp(code)
  35732. case 124: case 38: // '|&'
  35733. return this.readToken_pipe_amp(code)
  35734. case 94: // '^'
  35735. return this.readToken_caret()
  35736. case 43: case 45: // '+-'
  35737. return this.readToken_plus_min(code)
  35738. case 60: case 62: // '<>'
  35739. return this.readToken_lt_gt(code)
  35740. case 61: case 33: // '=!'
  35741. return this.readToken_eq_excl(code)
  35742. case 63: // '?'
  35743. return this.readToken_question()
  35744. case 126: // '~'
  35745. return this.finishOp(types$1.prefix, 1)
  35746. case 35: // '#'
  35747. return this.readToken_numberSign()
  35748. }
  35749. this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'");
  35750. };
  35751. pp.finishOp = function(type, size) {
  35752. var str = this.input.slice(this.pos, this.pos + size);
  35753. this.pos += size;
  35754. return this.finishToken(type, str)
  35755. };
  35756. pp.readRegexp = function() {
  35757. var escaped, inClass, start = this.pos;
  35758. for (;;) {
  35759. if (this.pos >= this.input.length) { this.raise(start, "Unterminated regular expression"); }
  35760. var ch = this.input.charAt(this.pos);
  35761. if (lineBreak.test(ch)) { this.raise(start, "Unterminated regular expression"); }
  35762. if (!escaped) {
  35763. if (ch === "[") { inClass = true; }
  35764. else if (ch === "]" && inClass) { inClass = false; }
  35765. else if (ch === "/" && !inClass) { break }
  35766. escaped = ch === "\\";
  35767. } else { escaped = false; }
  35768. ++this.pos;
  35769. }
  35770. var pattern = this.input.slice(start, this.pos);
  35771. ++this.pos;
  35772. var flagsStart = this.pos;
  35773. var flags = this.readWord1();
  35774. if (this.containsEsc) { this.unexpected(flagsStart); }
  35775. // Validate pattern
  35776. var state = this.regexpState || (this.regexpState = new RegExpValidationState(this));
  35777. state.reset(start, pattern, flags);
  35778. this.validateRegExpFlags(state);
  35779. this.validateRegExpPattern(state);
  35780. // Create Literal#value property value.
  35781. var value = null;
  35782. try {
  35783. value = new RegExp(pattern, flags);
  35784. } catch (e) {
  35785. // ESTree requires null if it failed to instantiate RegExp object.
  35786. // https://github.com/estree/estree/blob/a27003adf4fd7bfad44de9cef372a2eacd527b1c/es5.md#regexpliteral
  35787. }
  35788. return this.finishToken(types$1.regexp, {pattern: pattern, flags: flags, value: value})
  35789. };
  35790. // Read an integer in the given radix. Return null if zero digits
  35791. // were read, the integer value otherwise. When `len` is given, this
  35792. // will return `null` unless the integer has exactly `len` digits.
  35793. pp.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) {
  35794. // `len` is used for character escape sequences. In that case, disallow separators.
  35795. var allowSeparators = this.options.ecmaVersion >= 12 && len === undefined;
  35796. // `maybeLegacyOctalNumericLiteral` is true if it doesn't have prefix (0x,0o,0b)
  35797. // and isn't fraction part nor exponent part. In that case, if the first digit
  35798. // is zero then disallow separators.
  35799. var isLegacyOctalNumericLiteral = maybeLegacyOctalNumericLiteral && this.input.charCodeAt(this.pos) === 48;
  35800. var start = this.pos, total = 0, lastCode = 0;
  35801. for (var i = 0, e = len == null ? Infinity : len; i < e; ++i, ++this.pos) {
  35802. var code = this.input.charCodeAt(this.pos), val = (void 0);
  35803. if (allowSeparators && code === 95) {
  35804. if (isLegacyOctalNumericLiteral) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed in legacy octal numeric literals"); }
  35805. if (lastCode === 95) { this.raiseRecoverable(this.pos, "Numeric separator must be exactly one underscore"); }
  35806. if (i === 0) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed at the first of digits"); }
  35807. lastCode = code;
  35808. continue
  35809. }
  35810. if (code >= 97) { val = code - 97 + 10; } // a
  35811. else if (code >= 65) { val = code - 65 + 10; } // A
  35812. else if (code >= 48 && code <= 57) { val = code - 48; } // 0-9
  35813. else { val = Infinity; }
  35814. if (val >= radix) { break }
  35815. lastCode = code;
  35816. total = total * radix + val;
  35817. }
  35818. if (allowSeparators && lastCode === 95) { this.raiseRecoverable(this.pos - 1, "Numeric separator is not allowed at the last of digits"); }
  35819. if (this.pos === start || len != null && this.pos - start !== len) { return null }
  35820. return total
  35821. };
  35822. function stringToNumber(str, isLegacyOctalNumericLiteral) {
  35823. if (isLegacyOctalNumericLiteral) {
  35824. return parseInt(str, 8)
  35825. }
  35826. // `parseFloat(value)` stops parsing at the first numeric separator then returns a wrong value.
  35827. return parseFloat(str.replace(/_/g, ""))
  35828. }
  35829. function stringToBigInt(str) {
  35830. if (typeof BigInt !== "function") {
  35831. return null
  35832. }
  35833. // `BigInt(value)` throws syntax error if the string contains numeric separators.
  35834. return BigInt(str.replace(/_/g, ""))
  35835. }
  35836. pp.readRadixNumber = function(radix) {
  35837. var start = this.pos;
  35838. this.pos += 2; // 0x
  35839. var val = this.readInt(radix);
  35840. if (val == null) { this.raise(this.start + 2, "Expected number in radix " + radix); }
  35841. if (this.options.ecmaVersion >= 11 && this.input.charCodeAt(this.pos) === 110) {
  35842. val = stringToBigInt(this.input.slice(start, this.pos));
  35843. ++this.pos;
  35844. } else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
  35845. return this.finishToken(types$1.num, val)
  35846. };
  35847. // Read an integer, octal integer, or floating-point number.
  35848. pp.readNumber = function(startsWithDot) {
  35849. var start = this.pos;
  35850. if (!startsWithDot && this.readInt(10, undefined, true) === null) { this.raise(start, "Invalid number"); }
  35851. var octal = this.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  35852. if (octal && this.strict) { this.raise(start, "Invalid number"); }
  35853. var next = this.input.charCodeAt(this.pos);
  35854. if (!octal && !startsWithDot && this.options.ecmaVersion >= 11 && next === 110) {
  35855. var val$1 = stringToBigInt(this.input.slice(start, this.pos));
  35856. ++this.pos;
  35857. if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
  35858. return this.finishToken(types$1.num, val$1)
  35859. }
  35860. if (octal && /[89]/.test(this.input.slice(start, this.pos))) { octal = false; }
  35861. if (next === 46 && !octal) { // '.'
  35862. ++this.pos;
  35863. this.readInt(10);
  35864. next = this.input.charCodeAt(this.pos);
  35865. }
  35866. if ((next === 69 || next === 101) && !octal) { // 'eE'
  35867. next = this.input.charCodeAt(++this.pos);
  35868. if (next === 43 || next === 45) { ++this.pos; } // '+-'
  35869. if (this.readInt(10) === null) { this.raise(start, "Invalid number"); }
  35870. }
  35871. if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
  35872. var val = stringToNumber(this.input.slice(start, this.pos), octal);
  35873. return this.finishToken(types$1.num, val)
  35874. };
  35875. // Read a string value, interpreting backslash-escapes.
  35876. pp.readCodePoint = function() {
  35877. var ch = this.input.charCodeAt(this.pos), code;
  35878. if (ch === 123) { // '{'
  35879. if (this.options.ecmaVersion < 6) { this.unexpected(); }
  35880. var codePos = ++this.pos;
  35881. code = this.readHexChar(this.input.indexOf("}", this.pos) - this.pos);
  35882. ++this.pos;
  35883. if (code > 0x10FFFF) { this.invalidStringToken(codePos, "Code point out of bounds"); }
  35884. } else {
  35885. code = this.readHexChar(4);
  35886. }
  35887. return code
  35888. };
  35889. pp.readString = function(quote) {
  35890. var out = "", chunkStart = ++this.pos;
  35891. for (;;) {
  35892. if (this.pos >= this.input.length) { this.raise(this.start, "Unterminated string constant"); }
  35893. var ch = this.input.charCodeAt(this.pos);
  35894. if (ch === quote) { break }
  35895. if (ch === 92) { // '\'
  35896. out += this.input.slice(chunkStart, this.pos);
  35897. out += this.readEscapedChar(false);
  35898. chunkStart = this.pos;
  35899. } else if (ch === 0x2028 || ch === 0x2029) {
  35900. if (this.options.ecmaVersion < 10) { this.raise(this.start, "Unterminated string constant"); }
  35901. ++this.pos;
  35902. if (this.options.locations) {
  35903. this.curLine++;
  35904. this.lineStart = this.pos;
  35905. }
  35906. } else {
  35907. if (isNewLine(ch)) { this.raise(this.start, "Unterminated string constant"); }
  35908. ++this.pos;
  35909. }
  35910. }
  35911. out += this.input.slice(chunkStart, this.pos++);
  35912. return this.finishToken(types$1.string, out)
  35913. };
  35914. // Reads template string tokens.
  35915. var INVALID_TEMPLATE_ESCAPE_ERROR = {};
  35916. pp.tryReadTemplateToken = function() {
  35917. this.inTemplateElement = true;
  35918. try {
  35919. this.readTmplToken();
  35920. } catch (err) {
  35921. if (err === INVALID_TEMPLATE_ESCAPE_ERROR) {
  35922. this.readInvalidTemplateToken();
  35923. } else {
  35924. throw err
  35925. }
  35926. }
  35927. this.inTemplateElement = false;
  35928. };
  35929. pp.invalidStringToken = function(position, message) {
  35930. if (this.inTemplateElement && this.options.ecmaVersion >= 9) {
  35931. throw INVALID_TEMPLATE_ESCAPE_ERROR
  35932. } else {
  35933. this.raise(position, message);
  35934. }
  35935. };
  35936. pp.readTmplToken = function() {
  35937. var out = "", chunkStart = this.pos;
  35938. for (;;) {
  35939. if (this.pos >= this.input.length) { this.raise(this.start, "Unterminated template"); }
  35940. var ch = this.input.charCodeAt(this.pos);
  35941. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.pos + 1) === 123) { // '`', '${'
  35942. if (this.pos === this.start && (this.type === types$1.template || this.type === types$1.invalidTemplate)) {
  35943. if (ch === 36) {
  35944. this.pos += 2;
  35945. return this.finishToken(types$1.dollarBraceL)
  35946. } else {
  35947. ++this.pos;
  35948. return this.finishToken(types$1.backQuote)
  35949. }
  35950. }
  35951. out += this.input.slice(chunkStart, this.pos);
  35952. return this.finishToken(types$1.template, out)
  35953. }
  35954. if (ch === 92) { // '\'
  35955. out += this.input.slice(chunkStart, this.pos);
  35956. out += this.readEscapedChar(true);
  35957. chunkStart = this.pos;
  35958. } else if (isNewLine(ch)) {
  35959. out += this.input.slice(chunkStart, this.pos);
  35960. ++this.pos;
  35961. switch (ch) {
  35962. case 13:
  35963. if (this.input.charCodeAt(this.pos) === 10) { ++this.pos; }
  35964. case 10:
  35965. out += "\n";
  35966. break
  35967. default:
  35968. out += String.fromCharCode(ch);
  35969. break
  35970. }
  35971. if (this.options.locations) {
  35972. ++this.curLine;
  35973. this.lineStart = this.pos;
  35974. }
  35975. chunkStart = this.pos;
  35976. } else {
  35977. ++this.pos;
  35978. }
  35979. }
  35980. };
  35981. // Reads a template token to search for the end, without validating any escape sequences
  35982. pp.readInvalidTemplateToken = function() {
  35983. for (; this.pos < this.input.length; this.pos++) {
  35984. switch (this.input[this.pos]) {
  35985. case "\\":
  35986. ++this.pos;
  35987. break
  35988. case "$":
  35989. if (this.input[this.pos + 1] !== "{") {
  35990. break
  35991. }
  35992. // falls through
  35993. case "`":
  35994. return this.finishToken(types$1.invalidTemplate, this.input.slice(this.start, this.pos))
  35995. // no default
  35996. }
  35997. }
  35998. this.raise(this.start, "Unterminated template");
  35999. };
  36000. // Used to read escaped characters
  36001. pp.readEscapedChar = function(inTemplate) {
  36002. var ch = this.input.charCodeAt(++this.pos);
  36003. ++this.pos;
  36004. switch (ch) {
  36005. case 110: return "\n" // 'n' -> '\n'
  36006. case 114: return "\r" // 'r' -> '\r'
  36007. case 120: return String.fromCharCode(this.readHexChar(2)) // 'x'
  36008. case 117: return codePointToString(this.readCodePoint()) // 'u'
  36009. case 116: return "\t" // 't' -> '\t'
  36010. case 98: return "\b" // 'b' -> '\b'
  36011. case 118: return "\u000b" // 'v' -> '\u000b'
  36012. case 102: return "\f" // 'f' -> '\f'
  36013. case 13: if (this.input.charCodeAt(this.pos) === 10) { ++this.pos; } // '\r\n'
  36014. case 10: // ' \n'
  36015. if (this.options.locations) { this.lineStart = this.pos; ++this.curLine; }
  36016. return ""
  36017. case 56:
  36018. case 57:
  36019. if (this.strict) {
  36020. this.invalidStringToken(
  36021. this.pos - 1,
  36022. "Invalid escape sequence"
  36023. );
  36024. }
  36025. if (inTemplate) {
  36026. var codePos = this.pos - 1;
  36027. this.invalidStringToken(
  36028. codePos,
  36029. "Invalid escape sequence in template string"
  36030. );
  36031. }
  36032. default:
  36033. if (ch >= 48 && ch <= 55) {
  36034. var octalStr = this.input.substr(this.pos - 1, 3).match(/^[0-7]+/)[0];
  36035. var octal = parseInt(octalStr, 8);
  36036. if (octal > 255) {
  36037. octalStr = octalStr.slice(0, -1);
  36038. octal = parseInt(octalStr, 8);
  36039. }
  36040. this.pos += octalStr.length - 1;
  36041. ch = this.input.charCodeAt(this.pos);
  36042. if ((octalStr !== "0" || ch === 56 || ch === 57) && (this.strict || inTemplate)) {
  36043. this.invalidStringToken(
  36044. this.pos - 1 - octalStr.length,
  36045. inTemplate
  36046. ? "Octal literal in template string"
  36047. : "Octal literal in strict mode"
  36048. );
  36049. }
  36050. return String.fromCharCode(octal)
  36051. }
  36052. if (isNewLine(ch)) {
  36053. // Unicode new line characters after \ get removed from output in both
  36054. // template literals and strings
  36055. return ""
  36056. }
  36057. return String.fromCharCode(ch)
  36058. }
  36059. };
  36060. // Used to read character escape sequences ('\x', '\u', '\U').
  36061. pp.readHexChar = function(len) {
  36062. var codePos = this.pos;
  36063. var n = this.readInt(16, len);
  36064. if (n === null) { this.invalidStringToken(codePos, "Bad character escape sequence"); }
  36065. return n
  36066. };
  36067. // Read an identifier, and return it as a string. Sets `this.containsEsc`
  36068. // to whether the word contained a '\u' escape.
  36069. //
  36070. // Incrementally adds only escaped chars, adding other chunks as-is
  36071. // as a micro-optimization.
  36072. pp.readWord1 = function() {
  36073. this.containsEsc = false;
  36074. var word = "", first = true, chunkStart = this.pos;
  36075. var astral = this.options.ecmaVersion >= 6;
  36076. while (this.pos < this.input.length) {
  36077. var ch = this.fullCharCodeAtPos();
  36078. if (isIdentifierChar(ch, astral)) {
  36079. this.pos += ch <= 0xffff ? 1 : 2;
  36080. } else if (ch === 92) { // "\"
  36081. this.containsEsc = true;
  36082. word += this.input.slice(chunkStart, this.pos);
  36083. var escStart = this.pos;
  36084. if (this.input.charCodeAt(++this.pos) !== 117) // "u"
  36085. { this.invalidStringToken(this.pos, "Expecting Unicode escape sequence \\uXXXX"); }
  36086. ++this.pos;
  36087. var esc = this.readCodePoint();
  36088. if (!(first ? isIdentifierStart : isIdentifierChar)(esc, astral))
  36089. { this.invalidStringToken(escStart, "Invalid Unicode escape"); }
  36090. word += codePointToString(esc);
  36091. chunkStart = this.pos;
  36092. } else {
  36093. break
  36094. }
  36095. first = false;
  36096. }
  36097. return word + this.input.slice(chunkStart, this.pos)
  36098. };
  36099. // Read an identifier or keyword token. Will check for reserved
  36100. // words when necessary.
  36101. pp.readWord = function() {
  36102. var word = this.readWord1();
  36103. var type = types$1.name;
  36104. if (this.keywords.test(word)) {
  36105. type = keywords[word];
  36106. }
  36107. return this.finishToken(type, word)
  36108. };
  36109. // Acorn is a tiny, fast JavaScript parser written in JavaScript.
  36110. //
  36111. // Acorn was written by Marijn Haverbeke, Ingvar Stepanyan, and
  36112. // various contributors and released under an MIT license.
  36113. //
  36114. // Git repositories for Acorn are available at
  36115. //
  36116. // http://marijnhaverbeke.nl/git/acorn
  36117. // https://github.com/acornjs/acorn.git
  36118. //
  36119. // Please use the [github bug tracker][ghbt] to report issues.
  36120. //
  36121. // [ghbt]: https://github.com/acornjs/acorn/issues
  36122. //
  36123. // [walk]: util/walk.js
  36124. var version = "8.11.3";
  36125. Parser.acorn = {
  36126. Parser: Parser,
  36127. version: version,
  36128. defaultOptions: defaultOptions,
  36129. Position: Position,
  36130. SourceLocation: SourceLocation,
  36131. getLineInfo: getLineInfo,
  36132. Node: Node,
  36133. TokenType: TokenType,
  36134. tokTypes: types$1,
  36135. keywordTypes: keywords,
  36136. TokContext: TokContext,
  36137. tokContexts: types,
  36138. isIdentifierChar: isIdentifierChar,
  36139. isIdentifierStart: isIdentifierStart,
  36140. Token: Token,
  36141. isNewLine: isNewLine,
  36142. lineBreak: lineBreak,
  36143. lineBreakG: lineBreakG,
  36144. nonASCIIwhitespace: nonASCIIwhitespace
  36145. };
  36146. // The main exported interface (under `self.acorn` when in the
  36147. // browser) is a `parse` function that takes a code string and returns
  36148. // an abstract syntax tree as specified by the [ESTree spec][estree].
  36149. //
  36150. // [estree]: https://github.com/estree/estree
  36151. function parse$9(input, options) {
  36152. return Parser.parse(input, options)
  36153. }
  36154. // This function tries to parse a single expression at a given
  36155. // offset in a string. Useful for parsing mixed-language formats
  36156. // that embed JavaScript expressions.
  36157. function parseExpressionAt(input, pos, options) {
  36158. return Parser.parseExpressionAt(input, pos, options)
  36159. }
  36160. // Acorn is organized as a tokenizer and a recursive-descent parser.
  36161. // The `tokenizer` export provides an interface to the tokenizer.
  36162. function tokenizer(input, options) {
  36163. return Parser.tokenizer(input, options)
  36164. }
  36165. const HASH_RE = /#/g;
  36166. const AMPERSAND_RE = /&/g;
  36167. const SLASH_RE = /\//g;
  36168. const EQUAL_RE = /=/g;
  36169. const PLUS_RE = /\+/g;
  36170. const ENC_CARET_RE = /%5e/gi;
  36171. const ENC_BACKTICK_RE = /%60/gi;
  36172. const ENC_PIPE_RE = /%7c/gi;
  36173. const ENC_SPACE_RE = /%20/gi;
  36174. function encode(text) {
  36175. return encodeURI("" + text).replace(ENC_PIPE_RE, "|");
  36176. }
  36177. function encodeQueryValue(input) {
  36178. return encode(typeof input === "string" ? input : JSON.stringify(input)).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CARET_RE, "^").replace(SLASH_RE, "%2F");
  36179. }
  36180. function encodeQueryKey(text) {
  36181. return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
  36182. }
  36183. function encodeQueryItem(key, value) {
  36184. if (typeof value === "number" || typeof value === "boolean") {
  36185. value = String(value);
  36186. }
  36187. if (!value) {
  36188. return encodeQueryKey(key);
  36189. }
  36190. if (Array.isArray(value)) {
  36191. return value.map((_value) => `${encodeQueryKey(key)}=${encodeQueryValue(_value)}`).join("&");
  36192. }
  36193. return `${encodeQueryKey(key)}=${encodeQueryValue(value)}`;
  36194. }
  36195. function stringifyQuery(query) {
  36196. return Object.keys(query).filter((k) => query[k] !== void 0).map((k) => encodeQueryItem(k, query[k])).filter(Boolean).join("&");
  36197. }
  36198. new Set(builtinModules);
  36199. function matchAll$1(regex, string, addition) {
  36200. const matches = [];
  36201. for (const match of string.matchAll(regex)) {
  36202. matches.push({
  36203. ...addition,
  36204. ...match.groups,
  36205. code: match[0],
  36206. start: match.index,
  36207. end: (match.index || 0) + match[0].length
  36208. });
  36209. }
  36210. return matches;
  36211. }
  36212. function clearImports(imports) {
  36213. return (imports || "").replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, "").replace(/\s+/g, " ");
  36214. }
  36215. function getImportNames(cleanedImports) {
  36216. const topLevelImports = cleanedImports.replace(/{([^}]*)}/, "");
  36217. const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
  36218. const defaultImport = topLevelImports.split(",").find((index) => !/[*{}]/.test(index))?.trim() || void 0;
  36219. return {
  36220. namespacedImport,
  36221. defaultImport
  36222. };
  36223. }
  36224. /**
  36225. * @typedef ErrnoExceptionFields
  36226. * @property {number | undefined} [errnode]
  36227. * @property {string | undefined} [code]
  36228. * @property {string | undefined} [path]
  36229. * @property {string | undefined} [syscall]
  36230. * @property {string | undefined} [url]
  36231. *
  36232. * @typedef {Error & ErrnoExceptionFields} ErrnoException
  36233. */
  36234. const own$1 = {}.hasOwnProperty;
  36235. const classRegExp = /^([A-Z][a-z\d]*)+$/;
  36236. // Sorted by a rough estimate on most frequently used entries.
  36237. const kTypes = new Set([
  36238. 'string',
  36239. 'function',
  36240. 'number',
  36241. 'object',
  36242. // Accept 'Function' and 'Object' as alternative to the lower cased version.
  36243. 'Function',
  36244. 'Object',
  36245. 'boolean',
  36246. 'bigint',
  36247. 'symbol'
  36248. ]);
  36249. /**
  36250. * Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.
  36251. * We cannot use Intl.ListFormat because it's not available in
  36252. * --without-intl builds.
  36253. *
  36254. * @param {Array<string>} array
  36255. * An array of strings.
  36256. * @param {string} [type]
  36257. * The list type to be inserted before the last element.
  36258. * @returns {string}
  36259. */
  36260. function formatList(array, type = 'and') {
  36261. return array.length < 3
  36262. ? array.join(` ${type} `)
  36263. : `${array.slice(0, -1).join(', ')}, ${type} ${array[array.length - 1]}`
  36264. }
  36265. /** @type {Map<string, MessageFunction | string>} */
  36266. const messages = new Map();
  36267. const nodeInternalPrefix = '__node_internal_';
  36268. /** @type {number} */
  36269. let userStackTraceLimit;
  36270. createError(
  36271. 'ERR_INVALID_ARG_TYPE',
  36272. /**
  36273. * @param {string} name
  36274. * @param {Array<string> | string} expected
  36275. * @param {unknown} actual
  36276. */
  36277. (name, expected, actual) => {
  36278. assert$1(typeof name === 'string', "'name' must be a string");
  36279. if (!Array.isArray(expected)) {
  36280. expected = [expected];
  36281. }
  36282. let message = 'The ';
  36283. if (name.endsWith(' argument')) {
  36284. // For cases like 'first argument'
  36285. message += `${name} `;
  36286. } else {
  36287. const type = name.includes('.') ? 'property' : 'argument';
  36288. message += `"${name}" ${type} `;
  36289. }
  36290. message += 'must be ';
  36291. /** @type {Array<string>} */
  36292. const types = [];
  36293. /** @type {Array<string>} */
  36294. const instances = [];
  36295. /** @type {Array<string>} */
  36296. const other = [];
  36297. for (const value of expected) {
  36298. assert$1(
  36299. typeof value === 'string',
  36300. 'All expected entries have to be of type string'
  36301. );
  36302. if (kTypes.has(value)) {
  36303. types.push(value.toLowerCase());
  36304. } else if (classRegExp.exec(value) === null) {
  36305. assert$1(
  36306. value !== 'object',
  36307. 'The value "object" should be written as "Object"'
  36308. );
  36309. other.push(value);
  36310. } else {
  36311. instances.push(value);
  36312. }
  36313. }
  36314. // Special handle `object` in case other instances are allowed to outline
  36315. // the differences between each other.
  36316. if (instances.length > 0) {
  36317. const pos = types.indexOf('object');
  36318. if (pos !== -1) {
  36319. types.slice(pos, 1);
  36320. instances.push('Object');
  36321. }
  36322. }
  36323. if (types.length > 0) {
  36324. message += `${types.length > 1 ? 'one of type' : 'of type'} ${formatList(
  36325. types,
  36326. 'or'
  36327. )}`;
  36328. if (instances.length > 0 || other.length > 0) message += ' or ';
  36329. }
  36330. if (instances.length > 0) {
  36331. message += `an instance of ${formatList(instances, 'or')}`;
  36332. if (other.length > 0) message += ' or ';
  36333. }
  36334. if (other.length > 0) {
  36335. if (other.length > 1) {
  36336. message += `one of ${formatList(other, 'or')}`;
  36337. } else {
  36338. if (other[0].toLowerCase() !== other[0]) message += 'an ';
  36339. message += `${other[0]}`;
  36340. }
  36341. }
  36342. message += `. Received ${determineSpecificType(actual)}`;
  36343. return message
  36344. },
  36345. TypeError
  36346. );
  36347. createError(
  36348. 'ERR_INVALID_MODULE_SPECIFIER',
  36349. /**
  36350. * @param {string} request
  36351. * @param {string} reason
  36352. * @param {string} [base]
  36353. */
  36354. (request, reason, base = undefined) => {
  36355. return `Invalid module "${request}" ${reason}${
  36356. base ? ` imported from ${base}` : ''
  36357. }`
  36358. },
  36359. TypeError
  36360. );
  36361. createError(
  36362. 'ERR_INVALID_PACKAGE_CONFIG',
  36363. /**
  36364. * @param {string} path
  36365. * @param {string} [base]
  36366. * @param {string} [message]
  36367. */
  36368. (path, base, message) => {
  36369. return `Invalid package config ${path}${
  36370. base ? ` while importing ${base}` : ''
  36371. }${message ? `. ${message}` : ''}`
  36372. },
  36373. Error
  36374. );
  36375. createError(
  36376. 'ERR_INVALID_PACKAGE_TARGET',
  36377. /**
  36378. * @param {string} pkgPath
  36379. * @param {string} key
  36380. * @param {unknown} target
  36381. * @param {boolean} [isImport=false]
  36382. * @param {string} [base]
  36383. */
  36384. (pkgPath, key, target, isImport = false, base = undefined) => {
  36385. const relError =
  36386. typeof target === 'string' &&
  36387. !isImport &&
  36388. target.length > 0 &&
  36389. !target.startsWith('./');
  36390. if (key === '.') {
  36391. assert$1(isImport === false);
  36392. return (
  36393. `Invalid "exports" main target ${JSON.stringify(target)} defined ` +
  36394. `in the package config ${pkgPath}package.json${
  36395. base ? ` imported from ${base}` : ''
  36396. }${relError ? '; targets must start with "./"' : ''}`
  36397. )
  36398. }
  36399. return `Invalid "${
  36400. isImport ? 'imports' : 'exports'
  36401. }" target ${JSON.stringify(
  36402. target
  36403. )} defined for '${key}' in the package config ${pkgPath}package.json${
  36404. base ? ` imported from ${base}` : ''
  36405. }${relError ? '; targets must start with "./"' : ''}`
  36406. },
  36407. Error
  36408. );
  36409. createError(
  36410. 'ERR_MODULE_NOT_FOUND',
  36411. /**
  36412. * @param {string} path
  36413. * @param {string} base
  36414. * @param {boolean} [exactUrl]
  36415. */
  36416. (path, base, exactUrl = false) => {
  36417. return `Cannot find ${
  36418. exactUrl ? 'module' : 'package'
  36419. } '${path}' imported from ${base}`
  36420. },
  36421. Error
  36422. );
  36423. createError(
  36424. 'ERR_NETWORK_IMPORT_DISALLOWED',
  36425. "import of '%s' by %s is not supported: %s",
  36426. Error
  36427. );
  36428. createError(
  36429. 'ERR_PACKAGE_IMPORT_NOT_DEFINED',
  36430. /**
  36431. * @param {string} specifier
  36432. * @param {string} packagePath
  36433. * @param {string} base
  36434. */
  36435. (specifier, packagePath, base) => {
  36436. return `Package import specifier "${specifier}" is not defined${
  36437. packagePath ? ` in package ${packagePath}package.json` : ''
  36438. } imported from ${base}`
  36439. },
  36440. TypeError
  36441. );
  36442. createError(
  36443. 'ERR_PACKAGE_PATH_NOT_EXPORTED',
  36444. /**
  36445. * @param {string} pkgPath
  36446. * @param {string} subpath
  36447. * @param {string} [base]
  36448. */
  36449. (pkgPath, subpath, base = undefined) => {
  36450. if (subpath === '.')
  36451. return `No "exports" main defined in ${pkgPath}package.json${
  36452. base ? ` imported from ${base}` : ''
  36453. }`
  36454. return `Package subpath '${subpath}' is not defined by "exports" in ${pkgPath}package.json${
  36455. base ? ` imported from ${base}` : ''
  36456. }`
  36457. },
  36458. Error
  36459. );
  36460. createError(
  36461. 'ERR_UNSUPPORTED_DIR_IMPORT',
  36462. "Directory import '%s' is not supported " +
  36463. 'resolving ES modules imported from %s',
  36464. Error
  36465. );
  36466. createError(
  36467. 'ERR_UNKNOWN_FILE_EXTENSION',
  36468. /**
  36469. * @param {string} ext
  36470. * @param {string} path
  36471. */
  36472. (ext, path) => {
  36473. return `Unknown file extension "${ext}" for ${path}`
  36474. },
  36475. TypeError
  36476. );
  36477. createError(
  36478. 'ERR_INVALID_ARG_VALUE',
  36479. /**
  36480. * @param {string} name
  36481. * @param {unknown} value
  36482. * @param {string} [reason='is invalid']
  36483. */
  36484. (name, value, reason = 'is invalid') => {
  36485. let inspected = inspect(value);
  36486. if (inspected.length > 128) {
  36487. inspected = `${inspected.slice(0, 128)}...`;
  36488. }
  36489. const type = name.includes('.') ? 'property' : 'argument';
  36490. return `The ${type} '${name}' ${reason}. Received ${inspected}`
  36491. },
  36492. TypeError
  36493. // Note: extra classes have been shaken out.
  36494. // , RangeError
  36495. );
  36496. /**
  36497. * Utility function for registering the error codes. Only used here. Exported
  36498. * *only* to allow for testing.
  36499. * @param {string} sym
  36500. * @param {MessageFunction | string} value
  36501. * @param {ErrorConstructor} def
  36502. * @returns {new (...args: Array<any>) => Error}
  36503. */
  36504. function createError(sym, value, def) {
  36505. // Special case for SystemError that formats the error message differently
  36506. // The SystemErrors only have SystemError as their base classes.
  36507. messages.set(sym, value);
  36508. return makeNodeErrorWithCode(def, sym)
  36509. }
  36510. /**
  36511. * @param {ErrorConstructor} Base
  36512. * @param {string} key
  36513. * @returns {ErrorConstructor}
  36514. */
  36515. function makeNodeErrorWithCode(Base, key) {
  36516. // @ts-expect-error It’s a Node error.
  36517. return NodeError
  36518. /**
  36519. * @param {Array<unknown>} args
  36520. */
  36521. function NodeError(...args) {
  36522. const limit = Error.stackTraceLimit;
  36523. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
  36524. const error = new Base();
  36525. // Reset the limit and setting the name property.
  36526. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = limit;
  36527. const message = getMessage(key, args, error);
  36528. Object.defineProperties(error, {
  36529. // Note: no need to implement `kIsNodeError` symbol, would be hard,
  36530. // probably.
  36531. message: {
  36532. value: message,
  36533. enumerable: false,
  36534. writable: true,
  36535. configurable: true
  36536. },
  36537. toString: {
  36538. /** @this {Error} */
  36539. value() {
  36540. return `${this.name} [${key}]: ${this.message}`
  36541. },
  36542. enumerable: false,
  36543. writable: true,
  36544. configurable: true
  36545. }
  36546. });
  36547. captureLargerStackTrace(error);
  36548. // @ts-expect-error It’s a Node error.
  36549. error.code = key;
  36550. return error
  36551. }
  36552. }
  36553. /**
  36554. * @returns {boolean}
  36555. */
  36556. function isErrorStackTraceLimitWritable() {
  36557. // Do no touch Error.stackTraceLimit as V8 would attempt to install
  36558. // it again during deserialization.
  36559. try {
  36560. // @ts-expect-error: not in types?
  36561. if (v8.startupSnapshot.isBuildingSnapshot()) {
  36562. return false
  36563. }
  36564. } catch {}
  36565. const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit');
  36566. if (desc === undefined) {
  36567. return Object.isExtensible(Error)
  36568. }
  36569. return own$1.call(desc, 'writable') && desc.writable !== undefined
  36570. ? desc.writable
  36571. : desc.set !== undefined
  36572. }
  36573. /**
  36574. * This function removes unnecessary frames from Node.js core errors.
  36575. * @template {(...args: unknown[]) => unknown} T
  36576. * @param {T} fn
  36577. * @returns {T}
  36578. */
  36579. function hideStackFrames(fn) {
  36580. // We rename the functions that will be hidden to cut off the stacktrace
  36581. // at the outermost one
  36582. const hidden = nodeInternalPrefix + fn.name;
  36583. Object.defineProperty(fn, 'name', {value: hidden});
  36584. return fn
  36585. }
  36586. const captureLargerStackTrace = hideStackFrames(
  36587. /**
  36588. * @param {Error} error
  36589. * @returns {Error}
  36590. */
  36591. // @ts-expect-error: fine
  36592. function (error) {
  36593. const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
  36594. if (stackTraceLimitIsWritable) {
  36595. userStackTraceLimit = Error.stackTraceLimit;
  36596. Error.stackTraceLimit = Number.POSITIVE_INFINITY;
  36597. }
  36598. Error.captureStackTrace(error);
  36599. // Reset the limit
  36600. if (stackTraceLimitIsWritable) Error.stackTraceLimit = userStackTraceLimit;
  36601. return error
  36602. }
  36603. );
  36604. /**
  36605. * @param {string} key
  36606. * @param {Array<unknown>} args
  36607. * @param {Error} self
  36608. * @returns {string}
  36609. */
  36610. function getMessage(key, args, self) {
  36611. const message = messages.get(key);
  36612. assert$1(message !== undefined, 'expected `message` to be found');
  36613. if (typeof message === 'function') {
  36614. assert$1(
  36615. message.length <= args.length, // Default options do not count.
  36616. `Code: ${key}; The provided arguments length (${args.length}) does not ` +
  36617. `match the required ones (${message.length}).`
  36618. );
  36619. return Reflect.apply(message, self, args)
  36620. }
  36621. const regex = /%[dfijoOs]/g;
  36622. let expectedLength = 0;
  36623. while (regex.exec(message) !== null) expectedLength++;
  36624. assert$1(
  36625. expectedLength === args.length,
  36626. `Code: ${key}; The provided arguments length (${args.length}) does not ` +
  36627. `match the required ones (${expectedLength}).`
  36628. );
  36629. if (args.length === 0) return message
  36630. args.unshift(message);
  36631. return Reflect.apply(format$2, null, args)
  36632. }
  36633. /**
  36634. * Determine the specific type of a value for type-mismatch errors.
  36635. * @param {unknown} value
  36636. * @returns {string}
  36637. */
  36638. function determineSpecificType(value) {
  36639. if (value === null || value === undefined) {
  36640. return String(value)
  36641. }
  36642. if (typeof value === 'function' && value.name) {
  36643. return `function ${value.name}`
  36644. }
  36645. if (typeof value === 'object') {
  36646. if (value.constructor && value.constructor.name) {
  36647. return `an instance of ${value.constructor.name}`
  36648. }
  36649. return `${inspect(value, {depth: -1})}`
  36650. }
  36651. let inspected = inspect(value, {colors: false});
  36652. if (inspected.length > 28) {
  36653. inspected = `${inspected.slice(0, 25)}...`;
  36654. }
  36655. return `type ${typeof value} (${inspected})`
  36656. }
  36657. const ESM_STATIC_IMPORT_RE = /(?<=\s|^|;|\})import\s*([\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu;
  36658. const TYPE_RE = /^\s*?type\s/;
  36659. function findStaticImports(code) {
  36660. return _filterStatement(
  36661. _tryGetLocations(code, "import"),
  36662. matchAll$1(ESM_STATIC_IMPORT_RE, code, { type: "static" })
  36663. );
  36664. }
  36665. function parseStaticImport(matched) {
  36666. const cleanedImports = clearImports(matched.imports);
  36667. const namedImports = {};
  36668. const _matches = cleanedImports.match(/{([^}]*)}/)?.[1]?.split(",") || [];
  36669. for (const namedImport of _matches) {
  36670. const _match = namedImport.match(/^\s*(\S*) as (\S*)\s*$/);
  36671. const source = _match?.[1] || namedImport.trim();
  36672. const importName = _match?.[2] || source;
  36673. if (source && !TYPE_RE.test(source)) {
  36674. namedImports[source] = importName;
  36675. }
  36676. }
  36677. const { namespacedImport, defaultImport } = getImportNames(cleanedImports);
  36678. return {
  36679. ...matched,
  36680. defaultImport,
  36681. namespacedImport,
  36682. namedImports
  36683. };
  36684. }
  36685. function _filterStatement(locations, statements) {
  36686. return statements.filter((exp) => {
  36687. return !locations || locations.some((location) => {
  36688. return exp.start <= location.start && exp.end >= location.end;
  36689. });
  36690. });
  36691. }
  36692. function _tryGetLocations(code, label) {
  36693. try {
  36694. return _getLocations(code, label);
  36695. } catch {
  36696. }
  36697. }
  36698. function _getLocations(code, label) {
  36699. const tokens = tokenizer(code, {
  36700. ecmaVersion: "latest",
  36701. sourceType: "module",
  36702. allowHashBang: true,
  36703. allowAwaitOutsideFunction: true,
  36704. allowImportExportEverywhere: true
  36705. });
  36706. const locations = [];
  36707. for (const token of tokens) {
  36708. if (token.type.label === label) {
  36709. locations.push({
  36710. start: token.start,
  36711. end: token.end
  36712. });
  36713. }
  36714. }
  36715. return locations;
  36716. }
  36717. const ESM_RE = /([\s;]|^)(import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
  36718. const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g;
  36719. function hasESMSyntax(code, opts = {}) {
  36720. if (opts.stripComments) {
  36721. code = code.replace(COMMENT_RE, "");
  36722. }
  36723. return ESM_RE.test(code);
  36724. }
  36725. // AST walker module for ESTree compatible trees
  36726. function makeTest(test) {
  36727. if (typeof test === "string")
  36728. { return function (type) { return type === test; } }
  36729. else if (!test)
  36730. { return function () { return true; } }
  36731. else
  36732. { return test }
  36733. }
  36734. var Found = function Found(node, state) { this.node = node; this.state = state; };
  36735. // Find a node with a given start, end, and type (all are optional,
  36736. // null can be used as wildcard). Returns a {node, state} object, or
  36737. // undefined when it doesn't find a matching node.
  36738. function findNodeAt(node, start, end, test, baseVisitor, state) {
  36739. if (!baseVisitor) { baseVisitor = base; }
  36740. test = makeTest(test);
  36741. try {
  36742. (function c(node, st, override) {
  36743. var type = override || node.type;
  36744. if ((start == null || node.start <= start) &&
  36745. (end == null || node.end >= end))
  36746. { baseVisitor[type](node, st, c); }
  36747. if ((start == null || node.start === start) &&
  36748. (end == null || node.end === end) &&
  36749. test(type, node))
  36750. { throw new Found(node, st) }
  36751. })(node, state);
  36752. } catch (e) {
  36753. if (e instanceof Found) { return e }
  36754. throw e
  36755. }
  36756. }
  36757. function skipThrough(node, st, c) { c(node, st); }
  36758. function ignore(_node, _st, _c) {}
  36759. // Node walkers.
  36760. var base = {};
  36761. base.Program = base.BlockStatement = base.StaticBlock = function (node, st, c) {
  36762. for (var i = 0, list = node.body; i < list.length; i += 1)
  36763. {
  36764. var stmt = list[i];
  36765. c(stmt, st, "Statement");
  36766. }
  36767. };
  36768. base.Statement = skipThrough;
  36769. base.EmptyStatement = ignore;
  36770. base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =
  36771. function (node, st, c) { return c(node.expression, st, "Expression"); };
  36772. base.IfStatement = function (node, st, c) {
  36773. c(node.test, st, "Expression");
  36774. c(node.consequent, st, "Statement");
  36775. if (node.alternate) { c(node.alternate, st, "Statement"); }
  36776. };
  36777. base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
  36778. base.BreakStatement = base.ContinueStatement = ignore;
  36779. base.WithStatement = function (node, st, c) {
  36780. c(node.object, st, "Expression");
  36781. c(node.body, st, "Statement");
  36782. };
  36783. base.SwitchStatement = function (node, st, c) {
  36784. c(node.discriminant, st, "Expression");
  36785. for (var i$1 = 0, list$1 = node.cases; i$1 < list$1.length; i$1 += 1) {
  36786. var cs = list$1[i$1];
  36787. if (cs.test) { c(cs.test, st, "Expression"); }
  36788. for (var i = 0, list = cs.consequent; i < list.length; i += 1)
  36789. {
  36790. var cons = list[i];
  36791. c(cons, st, "Statement");
  36792. }
  36793. }
  36794. };
  36795. base.SwitchCase = function (node, st, c) {
  36796. if (node.test) { c(node.test, st, "Expression"); }
  36797. for (var i = 0, list = node.consequent; i < list.length; i += 1)
  36798. {
  36799. var cons = list[i];
  36800. c(cons, st, "Statement");
  36801. }
  36802. };
  36803. base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
  36804. if (node.argument) { c(node.argument, st, "Expression"); }
  36805. };
  36806. base.ThrowStatement = base.SpreadElement =
  36807. function (node, st, c) { return c(node.argument, st, "Expression"); };
  36808. base.TryStatement = function (node, st, c) {
  36809. c(node.block, st, "Statement");
  36810. if (node.handler) { c(node.handler, st); }
  36811. if (node.finalizer) { c(node.finalizer, st, "Statement"); }
  36812. };
  36813. base.CatchClause = function (node, st, c) {
  36814. if (node.param) { c(node.param, st, "Pattern"); }
  36815. c(node.body, st, "Statement");
  36816. };
  36817. base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
  36818. c(node.test, st, "Expression");
  36819. c(node.body, st, "Statement");
  36820. };
  36821. base.ForStatement = function (node, st, c) {
  36822. if (node.init) { c(node.init, st, "ForInit"); }
  36823. if (node.test) { c(node.test, st, "Expression"); }
  36824. if (node.update) { c(node.update, st, "Expression"); }
  36825. c(node.body, st, "Statement");
  36826. };
  36827. base.ForInStatement = base.ForOfStatement = function (node, st, c) {
  36828. c(node.left, st, "ForInit");
  36829. c(node.right, st, "Expression");
  36830. c(node.body, st, "Statement");
  36831. };
  36832. base.ForInit = function (node, st, c) {
  36833. if (node.type === "VariableDeclaration") { c(node, st); }
  36834. else { c(node, st, "Expression"); }
  36835. };
  36836. base.DebuggerStatement = ignore;
  36837. base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
  36838. base.VariableDeclaration = function (node, st, c) {
  36839. for (var i = 0, list = node.declarations; i < list.length; i += 1)
  36840. {
  36841. var decl = list[i];
  36842. c(decl, st);
  36843. }
  36844. };
  36845. base.VariableDeclarator = function (node, st, c) {
  36846. c(node.id, st, "Pattern");
  36847. if (node.init) { c(node.init, st, "Expression"); }
  36848. };
  36849. base.Function = function (node, st, c) {
  36850. if (node.id) { c(node.id, st, "Pattern"); }
  36851. for (var i = 0, list = node.params; i < list.length; i += 1)
  36852. {
  36853. var param = list[i];
  36854. c(param, st, "Pattern");
  36855. }
  36856. c(node.body, st, node.expression ? "Expression" : "Statement");
  36857. };
  36858. base.Pattern = function (node, st, c) {
  36859. if (node.type === "Identifier")
  36860. { c(node, st, "VariablePattern"); }
  36861. else if (node.type === "MemberExpression")
  36862. { c(node, st, "MemberPattern"); }
  36863. else
  36864. { c(node, st); }
  36865. };
  36866. base.VariablePattern = ignore;
  36867. base.MemberPattern = skipThrough;
  36868. base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
  36869. base.ArrayPattern = function (node, st, c) {
  36870. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  36871. var elt = list[i];
  36872. if (elt) { c(elt, st, "Pattern"); }
  36873. }
  36874. };
  36875. base.ObjectPattern = function (node, st, c) {
  36876. for (var i = 0, list = node.properties; i < list.length; i += 1) {
  36877. var prop = list[i];
  36878. if (prop.type === "Property") {
  36879. if (prop.computed) { c(prop.key, st, "Expression"); }
  36880. c(prop.value, st, "Pattern");
  36881. } else if (prop.type === "RestElement") {
  36882. c(prop.argument, st, "Pattern");
  36883. }
  36884. }
  36885. };
  36886. base.Expression = skipThrough;
  36887. base.ThisExpression = base.Super = base.MetaProperty = ignore;
  36888. base.ArrayExpression = function (node, st, c) {
  36889. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  36890. var elt = list[i];
  36891. if (elt) { c(elt, st, "Expression"); }
  36892. }
  36893. };
  36894. base.ObjectExpression = function (node, st, c) {
  36895. for (var i = 0, list = node.properties; i < list.length; i += 1)
  36896. {
  36897. var prop = list[i];
  36898. c(prop, st);
  36899. }
  36900. };
  36901. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
  36902. base.SequenceExpression = function (node, st, c) {
  36903. for (var i = 0, list = node.expressions; i < list.length; i += 1)
  36904. {
  36905. var expr = list[i];
  36906. c(expr, st, "Expression");
  36907. }
  36908. };
  36909. base.TemplateLiteral = function (node, st, c) {
  36910. for (var i = 0, list = node.quasis; i < list.length; i += 1)
  36911. {
  36912. var quasi = list[i];
  36913. c(quasi, st);
  36914. }
  36915. for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
  36916. {
  36917. var expr = list$1[i$1];
  36918. c(expr, st, "Expression");
  36919. }
  36920. };
  36921. base.TemplateElement = ignore;
  36922. base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
  36923. c(node.argument, st, "Expression");
  36924. };
  36925. base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
  36926. c(node.left, st, "Expression");
  36927. c(node.right, st, "Expression");
  36928. };
  36929. base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
  36930. c(node.left, st, "Pattern");
  36931. c(node.right, st, "Expression");
  36932. };
  36933. base.ConditionalExpression = function (node, st, c) {
  36934. c(node.test, st, "Expression");
  36935. c(node.consequent, st, "Expression");
  36936. c(node.alternate, st, "Expression");
  36937. };
  36938. base.NewExpression = base.CallExpression = function (node, st, c) {
  36939. c(node.callee, st, "Expression");
  36940. if (node.arguments)
  36941. { for (var i = 0, list = node.arguments; i < list.length; i += 1)
  36942. {
  36943. var arg = list[i];
  36944. c(arg, st, "Expression");
  36945. } }
  36946. };
  36947. base.MemberExpression = function (node, st, c) {
  36948. c(node.object, st, "Expression");
  36949. if (node.computed) { c(node.property, st, "Expression"); }
  36950. };
  36951. base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
  36952. if (node.declaration)
  36953. { c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
  36954. if (node.source) { c(node.source, st, "Expression"); }
  36955. };
  36956. base.ExportAllDeclaration = function (node, st, c) {
  36957. if (node.exported)
  36958. { c(node.exported, st); }
  36959. c(node.source, st, "Expression");
  36960. };
  36961. base.ImportDeclaration = function (node, st, c) {
  36962. for (var i = 0, list = node.specifiers; i < list.length; i += 1)
  36963. {
  36964. var spec = list[i];
  36965. c(spec, st);
  36966. }
  36967. c(node.source, st, "Expression");
  36968. };
  36969. base.ImportExpression = function (node, st, c) {
  36970. c(node.source, st, "Expression");
  36971. };
  36972. base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.PrivateIdentifier = base.Literal = ignore;
  36973. base.TaggedTemplateExpression = function (node, st, c) {
  36974. c(node.tag, st, "Expression");
  36975. c(node.quasi, st, "Expression");
  36976. };
  36977. base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
  36978. base.Class = function (node, st, c) {
  36979. if (node.id) { c(node.id, st, "Pattern"); }
  36980. if (node.superClass) { c(node.superClass, st, "Expression"); }
  36981. c(node.body, st);
  36982. };
  36983. base.ClassBody = function (node, st, c) {
  36984. for (var i = 0, list = node.body; i < list.length; i += 1)
  36985. {
  36986. var elt = list[i];
  36987. c(elt, st);
  36988. }
  36989. };
  36990. base.MethodDefinition = base.PropertyDefinition = base.Property = function (node, st, c) {
  36991. if (node.computed) { c(node.key, st, "Expression"); }
  36992. if (node.value) { c(node.value, st, "Expression"); }
  36993. };
  36994. const { isMatch: isMatch$1, scan } = micromatch$2;
  36995. function getAffectedGlobModules(file, server) {
  36996. const modules = [];
  36997. for (const [id, allGlobs] of server._importGlobMap) {
  36998. // (glob1 || glob2) && !glob3 && !glob4...
  36999. if (allGlobs.some(({ affirmed, negated }) => (!affirmed.length || affirmed.some((glob) => isMatch$1(file, glob))) &&
  37000. (!negated.length || negated.every((glob) => isMatch$1(file, glob))))) {
  37001. const mod = server.moduleGraph.getModuleById(id);
  37002. if (mod)
  37003. modules.push(mod);
  37004. }
  37005. }
  37006. modules.forEach((i) => {
  37007. if (i?.file)
  37008. server.moduleGraph.onFileChange(i.file);
  37009. });
  37010. return modules;
  37011. }
  37012. function importGlobPlugin(config) {
  37013. let server;
  37014. return {
  37015. name: 'vite:import-glob',
  37016. configureServer(_server) {
  37017. server = _server;
  37018. server._importGlobMap.clear();
  37019. },
  37020. async transform(code, id) {
  37021. if (!code.includes('import.meta.glob'))
  37022. return;
  37023. const result = await transformGlobImport(code, id, config.root, (im, _, options) => this.resolve(im, id, options).then((i) => i?.id || im), config.experimental.importGlobRestoreExtension, config.logger);
  37024. if (result) {
  37025. if (server) {
  37026. const allGlobs = result.matches.map((i) => i.globsResolved);
  37027. server._importGlobMap.set(id, allGlobs.map((globs) => {
  37028. const affirmed = [];
  37029. const negated = [];
  37030. for (const glob of globs) {
  37031. (glob[0] === '!' ? negated : affirmed).push(glob);
  37032. }
  37033. return { affirmed, negated };
  37034. }));
  37035. }
  37036. return transformStableResult(result.s, id, config);
  37037. }
  37038. },
  37039. };
  37040. }
  37041. const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g;
  37042. const knownOptions = {
  37043. as: ['string'],
  37044. eager: ['boolean'],
  37045. import: ['string'],
  37046. exhaustive: ['boolean'],
  37047. query: ['object', 'string'],
  37048. };
  37049. const forceDefaultAs = ['raw', 'url'];
  37050. function err$1(e, pos) {
  37051. const error = new Error(e);
  37052. error.pos = pos;
  37053. return error;
  37054. }
  37055. function parseGlobOptions(rawOpts, optsStartIndex, logger) {
  37056. let opts = {};
  37057. try {
  37058. opts = evalValue(rawOpts);
  37059. }
  37060. catch {
  37061. throw err$1('Vite is unable to parse the glob options as the value is not static', optsStartIndex);
  37062. }
  37063. if (opts == null) {
  37064. return {};
  37065. }
  37066. for (const key in opts) {
  37067. if (!(key in knownOptions)) {
  37068. throw err$1(`Unknown glob option "${key}"`, optsStartIndex);
  37069. }
  37070. const allowedTypes = knownOptions[key];
  37071. const valueType = typeof opts[key];
  37072. if (!allowedTypes.includes(valueType)) {
  37073. throw err$1(`Expected glob option "${key}" to be of type ${allowedTypes.join(' or ')}, but got ${valueType}`, optsStartIndex);
  37074. }
  37075. }
  37076. if (typeof opts.query === 'object') {
  37077. for (const key in opts.query) {
  37078. const value = opts.query[key];
  37079. if (!['string', 'number', 'boolean'].includes(typeof value)) {
  37080. throw err$1(`Expected glob option "query.${key}" to be of type string, number, or boolean, but got ${typeof value}`, optsStartIndex);
  37081. }
  37082. }
  37083. // normalize query as string so it's easier to handle later
  37084. opts.query = stringifyQuery(opts.query);
  37085. }
  37086. if (opts.as && logger) {
  37087. const importSuggestion = forceDefaultAs.includes(opts.as)
  37088. ? `, import: 'default'`
  37089. : '';
  37090. logger.warn(colors$1.yellow(`The glob option "as" has been deprecated in favour of "query". Please update \`as: '${opts.as}'\` to \`query: '?${opts.as}'${importSuggestion}\`.`));
  37091. }
  37092. // validate `import` option based on `as` option
  37093. if (opts.as && forceDefaultAs.includes(opts.as)) {
  37094. if (opts.import && opts.import !== 'default' && opts.import !== '*')
  37095. throw err$1(`Option "import" can only be "default" or "*" when "as" is "${opts.as}", but got "${opts.import}"`, optsStartIndex);
  37096. opts.import = opts.import || 'default';
  37097. }
  37098. if (opts.as && opts.query)
  37099. throw err$1('Options "as" and "query" cannot be used together', optsStartIndex);
  37100. if (opts.as)
  37101. opts.query = opts.as;
  37102. if (opts.query && opts.query[0] !== '?')
  37103. opts.query = `?${opts.query}`;
  37104. return opts;
  37105. }
  37106. async function parseImportGlob(code, importer, root, resolveId, logger) {
  37107. let cleanCode;
  37108. try {
  37109. cleanCode = stripLiteral(code);
  37110. }
  37111. catch (e) {
  37112. // skip invalid js code
  37113. return [];
  37114. }
  37115. const matches = Array.from(cleanCode.matchAll(importGlobRE));
  37116. const tasks = matches.map(async (match, index) => {
  37117. const start = match.index;
  37118. const err = (msg) => {
  37119. const e = new Error(`Invalid glob import syntax: ${msg}`);
  37120. e.pos = start;
  37121. return e;
  37122. };
  37123. let ast;
  37124. let lastTokenPos;
  37125. try {
  37126. ast = parseExpressionAt(code, start, {
  37127. ecmaVersion: 'latest',
  37128. sourceType: 'module',
  37129. ranges: true,
  37130. onToken: (token) => {
  37131. lastTokenPos = token.end;
  37132. },
  37133. });
  37134. }
  37135. catch (e) {
  37136. const _e = e;
  37137. if (_e.message && _e.message.startsWith('Unterminated string constant'))
  37138. return undefined;
  37139. if (lastTokenPos == null || lastTokenPos <= start)
  37140. throw _e;
  37141. // tailing comma in object or array will make the parser think it's a comma operation
  37142. // we try to parse again removing the comma
  37143. try {
  37144. const statement = code.slice(start, lastTokenPos).replace(/[,\s]*$/, '');
  37145. ast = parseExpressionAt(' '.repeat(start) + statement, // to keep the ast position
  37146. start, {
  37147. ecmaVersion: 'latest',
  37148. sourceType: 'module',
  37149. ranges: true,
  37150. });
  37151. }
  37152. catch {
  37153. throw _e;
  37154. }
  37155. }
  37156. const found = findNodeAt(ast, start, undefined, 'CallExpression');
  37157. if (!found)
  37158. throw err(`Expect CallExpression, got ${ast.type}`);
  37159. ast = found.node;
  37160. if (ast.arguments.length < 1 || ast.arguments.length > 2)
  37161. throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`);
  37162. const arg1 = ast.arguments[0];
  37163. const arg2 = ast.arguments[1];
  37164. const globs = [];
  37165. const validateLiteral = (element) => {
  37166. if (!element)
  37167. return;
  37168. if (element.type === 'Literal') {
  37169. if (typeof element.value !== 'string')
  37170. throw err(`Expected glob to be a string, but got "${typeof element.value}"`);
  37171. globs.push(element.value);
  37172. }
  37173. else if (element.type === 'TemplateLiteral') {
  37174. if (element.expressions.length !== 0) {
  37175. throw err(`Expected glob to be a string, but got dynamic template literal`);
  37176. }
  37177. globs.push(element.quasis[0].value.raw);
  37178. }
  37179. else {
  37180. throw err('Could only use literals');
  37181. }
  37182. };
  37183. if (arg1.type === 'ArrayExpression') {
  37184. for (const element of arg1.elements) {
  37185. validateLiteral(element);
  37186. }
  37187. }
  37188. else {
  37189. validateLiteral(arg1);
  37190. }
  37191. // arg2
  37192. let options = {};
  37193. if (arg2) {
  37194. if (arg2.type !== 'ObjectExpression')
  37195. throw err(`Expected the second argument to be an object literal, but got "${arg2.type}"`);
  37196. options = parseGlobOptions(code.slice(arg2.range[0], arg2.range[1]), arg2.range[0], logger);
  37197. }
  37198. const end = ast.range[1];
  37199. const globsResolved = await Promise.all(globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)));
  37200. const isRelative = globs.every((i) => '.!'.includes(i[0]));
  37201. return {
  37202. index,
  37203. globs,
  37204. globsResolved,
  37205. isRelative,
  37206. options,
  37207. start,
  37208. end,
  37209. };
  37210. });
  37211. return (await Promise.all(tasks)).filter(Boolean);
  37212. }
  37213. const importPrefix = '__vite_glob_';
  37214. const { basename, dirname, relative, join } = posix$1;
  37215. /**
  37216. * @param optimizeExport for dynamicImportVar plugin don't need to optimize export.
  37217. */
  37218. async function transformGlobImport(code, id, root, resolveId, restoreQueryExtension = false, logger) {
  37219. id = slash$1(id);
  37220. root = slash$1(root);
  37221. const isVirtual = isVirtualModule(id);
  37222. const dir = isVirtual ? undefined : dirname(id);
  37223. const matches = await parseImportGlob(code, isVirtual ? undefined : id, root, resolveId, logger);
  37224. const matchedFiles = new Set();
  37225. if (!matches.length)
  37226. return null;
  37227. const s = new MagicString(code);
  37228. const staticImports = (await Promise.all(matches.map(async ({ globsResolved, isRelative, options, index, start, end }) => {
  37229. const cwd = getCommonBase(globsResolved) ?? root;
  37230. const files = (await glob(globsResolved, {
  37231. cwd,
  37232. absolute: true,
  37233. dot: !!options.exhaustive,
  37234. ignore: options.exhaustive
  37235. ? []
  37236. : [join(cwd, '**/node_modules/**')],
  37237. }))
  37238. .filter((file) => file !== id)
  37239. .sort();
  37240. const objectProps = [];
  37241. const staticImports = [];
  37242. const resolvePaths = (file) => {
  37243. if (!dir) {
  37244. if (isRelative)
  37245. throw new Error("In virtual modules, all globs must start with '/'");
  37246. const filePath = `/${relative(root, file)}`;
  37247. return { filePath, importPath: filePath };
  37248. }
  37249. let importPath = relative(dir, file);
  37250. if (importPath[0] !== '.')
  37251. importPath = `./${importPath}`;
  37252. let filePath;
  37253. if (isRelative) {
  37254. filePath = importPath;
  37255. }
  37256. else {
  37257. filePath = relative(root, file);
  37258. if (filePath[0] !== '.')
  37259. filePath = `/${filePath}`;
  37260. }
  37261. return { filePath, importPath };
  37262. };
  37263. files.forEach((file, i) => {
  37264. const paths = resolvePaths(file);
  37265. const filePath = paths.filePath;
  37266. let importPath = paths.importPath;
  37267. let importQuery = options.query ?? '';
  37268. if (importQuery && importQuery !== '?raw') {
  37269. const fileExtension = basename(file).split('.').slice(-1)[0];
  37270. if (fileExtension && restoreQueryExtension)
  37271. importQuery = `${importQuery}&lang.${fileExtension}`;
  37272. }
  37273. importPath = `${importPath}${importQuery}`;
  37274. const importKey = options.import && options.import !== '*'
  37275. ? options.import
  37276. : undefined;
  37277. if (options.eager) {
  37278. const variableName = `${importPrefix}${index}_${i}`;
  37279. const expression = importKey
  37280. ? `{ ${importKey} as ${variableName} }`
  37281. : `* as ${variableName}`;
  37282. staticImports.push(`import ${expression} from ${JSON.stringify(importPath)}`);
  37283. objectProps.push(`${JSON.stringify(filePath)}: ${variableName}`);
  37284. }
  37285. else {
  37286. let importStatement = `import(${JSON.stringify(importPath)})`;
  37287. if (importKey)
  37288. importStatement += `.then(m => m[${JSON.stringify(importKey)}])`;
  37289. objectProps.push(`${JSON.stringify(filePath)}: () => ${importStatement}`);
  37290. }
  37291. });
  37292. files.forEach((i) => matchedFiles.add(i));
  37293. const originalLineBreakCount = code.slice(start, end).match(/\n/g)?.length ?? 0;
  37294. const lineBreaks = originalLineBreakCount > 0
  37295. ? '\n'.repeat(originalLineBreakCount)
  37296. : '';
  37297. const replacement = `/* #__PURE__ */ Object.assign({${objectProps.join(',')}${lineBreaks}})`;
  37298. s.overwrite(start, end, replacement);
  37299. return staticImports;
  37300. }))).flat();
  37301. if (staticImports.length)
  37302. s.prepend(`${staticImports.join(';')};`);
  37303. return {
  37304. s,
  37305. matches,
  37306. files: matchedFiles,
  37307. };
  37308. }
  37309. function globSafePath(path) {
  37310. // slash path to ensure \ is converted to / as \ could lead to a double escape scenario
  37311. // see https://github.com/mrmlnc/fast-glob#advanced-syntax
  37312. return glob.escapePath(normalizePath$3(path));
  37313. }
  37314. function lastNthChar(str, n) {
  37315. return str.charAt(str.length - 1 - n);
  37316. }
  37317. function globSafeResolvedPath(resolved, glob) {
  37318. // we have to escape special glob characters in the resolved path, but keep the user specified globby suffix
  37319. // walk back both strings until a character difference is found
  37320. // then slice up the resolved path at that pos and escape the first part
  37321. let numEqual = 0;
  37322. const maxEqual = Math.min(resolved.length, glob.length);
  37323. while (numEqual < maxEqual &&
  37324. lastNthChar(resolved, numEqual) === lastNthChar(glob, numEqual)) {
  37325. numEqual += 1;
  37326. }
  37327. const staticPartEnd = resolved.length - numEqual;
  37328. const staticPart = resolved.slice(0, staticPartEnd);
  37329. const dynamicPart = resolved.slice(staticPartEnd);
  37330. return globSafePath(staticPart) + dynamicPart;
  37331. }
  37332. async function toAbsoluteGlob(glob, root, importer, resolveId) {
  37333. let pre = '';
  37334. if (glob[0] === '!') {
  37335. pre = '!';
  37336. glob = glob.slice(1);
  37337. }
  37338. root = globSafePath(root);
  37339. const dir = importer ? globSafePath(dirname(importer)) : root;
  37340. if (glob[0] === '/')
  37341. return pre + posix$1.join(root, glob.slice(1));
  37342. if (glob.startsWith('./'))
  37343. return pre + posix$1.join(dir, glob.slice(2));
  37344. if (glob.startsWith('../'))
  37345. return pre + posix$1.join(dir, glob);
  37346. if (glob.startsWith('**'))
  37347. return pre + glob;
  37348. const isSubImportsPattern = glob[0] === '#' && glob.includes('*');
  37349. const resolved = normalizePath$3((await resolveId(glob, importer, {
  37350. custom: { 'vite:import-glob': { isSubImportsPattern } },
  37351. })) || glob);
  37352. if (isSubImportsPattern) {
  37353. return join(root, resolved);
  37354. }
  37355. if (isAbsolute$2(resolved)) {
  37356. return pre + globSafeResolvedPath(resolved, glob);
  37357. }
  37358. throw new Error(`Invalid glob: "${glob}" (resolved: "${resolved}"). It must start with '/' or './'`);
  37359. }
  37360. function getCommonBase(globsResolved) {
  37361. const bases = globsResolved
  37362. .filter((g) => g[0] !== '!')
  37363. .map((glob) => {
  37364. let { base } = scan(glob);
  37365. // `scan('a/foo.js')` returns `base: 'a/foo.js'`
  37366. if (posix$1.basename(base).includes('.'))
  37367. base = posix$1.dirname(base);
  37368. return base;
  37369. });
  37370. if (!bases.length)
  37371. return null;
  37372. let commonAncestor = '';
  37373. const dirS = bases[0].split('/');
  37374. for (let i = 0; i < dirS.length; i++) {
  37375. const candidate = dirS.slice(0, i + 1).join('/');
  37376. if (bases.every((base) => base.startsWith(candidate)))
  37377. commonAncestor = candidate;
  37378. else
  37379. break;
  37380. }
  37381. if (!commonAncestor)
  37382. commonAncestor = '/';
  37383. return commonAncestor;
  37384. }
  37385. function isVirtualModule(id) {
  37386. // https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
  37387. return id.startsWith('virtual:') || id[0] === '\0' || !id.includes('/');
  37388. }
  37389. var src = {exports: {}};
  37390. var browser = {exports: {}};
  37391. var debug$f = {exports: {}};
  37392. /**
  37393. * Helpers.
  37394. */
  37395. var ms;
  37396. var hasRequiredMs;
  37397. function requireMs () {
  37398. if (hasRequiredMs) return ms;
  37399. hasRequiredMs = 1;
  37400. var s = 1000;
  37401. var m = s * 60;
  37402. var h = m * 60;
  37403. var d = h * 24;
  37404. var y = d * 365.25;
  37405. /**
  37406. * Parse or format the given `val`.
  37407. *
  37408. * Options:
  37409. *
  37410. * - `long` verbose formatting [false]
  37411. *
  37412. * @param {String|Number} val
  37413. * @param {Object} [options]
  37414. * @throws {Error} throw an error if val is not a non-empty string or a number
  37415. * @return {String|Number}
  37416. * @api public
  37417. */
  37418. ms = function(val, options) {
  37419. options = options || {};
  37420. var type = typeof val;
  37421. if (type === 'string' && val.length > 0) {
  37422. return parse(val);
  37423. } else if (type === 'number' && isNaN(val) === false) {
  37424. return options.long ? fmtLong(val) : fmtShort(val);
  37425. }
  37426. throw new Error(
  37427. 'val is not a non-empty string or a valid number. val=' +
  37428. JSON.stringify(val)
  37429. );
  37430. };
  37431. /**
  37432. * Parse the given `str` and return milliseconds.
  37433. *
  37434. * @param {String} str
  37435. * @return {Number}
  37436. * @api private
  37437. */
  37438. function parse(str) {
  37439. str = String(str);
  37440. if (str.length > 100) {
  37441. return;
  37442. }
  37443. var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
  37444. str
  37445. );
  37446. if (!match) {
  37447. return;
  37448. }
  37449. var n = parseFloat(match[1]);
  37450. var type = (match[2] || 'ms').toLowerCase();
  37451. switch (type) {
  37452. case 'years':
  37453. case 'year':
  37454. case 'yrs':
  37455. case 'yr':
  37456. case 'y':
  37457. return n * y;
  37458. case 'days':
  37459. case 'day':
  37460. case 'd':
  37461. return n * d;
  37462. case 'hours':
  37463. case 'hour':
  37464. case 'hrs':
  37465. case 'hr':
  37466. case 'h':
  37467. return n * h;
  37468. case 'minutes':
  37469. case 'minute':
  37470. case 'mins':
  37471. case 'min':
  37472. case 'm':
  37473. return n * m;
  37474. case 'seconds':
  37475. case 'second':
  37476. case 'secs':
  37477. case 'sec':
  37478. case 's':
  37479. return n * s;
  37480. case 'milliseconds':
  37481. case 'millisecond':
  37482. case 'msecs':
  37483. case 'msec':
  37484. case 'ms':
  37485. return n;
  37486. default:
  37487. return undefined;
  37488. }
  37489. }
  37490. /**
  37491. * Short format for `ms`.
  37492. *
  37493. * @param {Number} ms
  37494. * @return {String}
  37495. * @api private
  37496. */
  37497. function fmtShort(ms) {
  37498. if (ms >= d) {
  37499. return Math.round(ms / d) + 'd';
  37500. }
  37501. if (ms >= h) {
  37502. return Math.round(ms / h) + 'h';
  37503. }
  37504. if (ms >= m) {
  37505. return Math.round(ms / m) + 'm';
  37506. }
  37507. if (ms >= s) {
  37508. return Math.round(ms / s) + 's';
  37509. }
  37510. return ms + 'ms';
  37511. }
  37512. /**
  37513. * Long format for `ms`.
  37514. *
  37515. * @param {Number} ms
  37516. * @return {String}
  37517. * @api private
  37518. */
  37519. function fmtLong(ms) {
  37520. return plural(ms, d, 'day') ||
  37521. plural(ms, h, 'hour') ||
  37522. plural(ms, m, 'minute') ||
  37523. plural(ms, s, 'second') ||
  37524. ms + ' ms';
  37525. }
  37526. /**
  37527. * Pluralization helper.
  37528. */
  37529. function plural(ms, n, name) {
  37530. if (ms < n) {
  37531. return;
  37532. }
  37533. if (ms < n * 1.5) {
  37534. return Math.floor(ms / n) + ' ' + name;
  37535. }
  37536. return Math.ceil(ms / n) + ' ' + name + 's';
  37537. }
  37538. return ms;
  37539. }
  37540. var hasRequiredDebug;
  37541. function requireDebug () {
  37542. if (hasRequiredDebug) return debug$f.exports;
  37543. hasRequiredDebug = 1;
  37544. (function (module, exports) {
  37545. /**
  37546. * This is the common logic for both the Node.js and web browser
  37547. * implementations of `debug()`.
  37548. *
  37549. * Expose `debug()` as the module.
  37550. */
  37551. exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
  37552. exports.coerce = coerce;
  37553. exports.disable = disable;
  37554. exports.enable = enable;
  37555. exports.enabled = enabled;
  37556. exports.humanize = requireMs();
  37557. /**
  37558. * The currently active debug mode names, and names to skip.
  37559. */
  37560. exports.names = [];
  37561. exports.skips = [];
  37562. /**
  37563. * Map of special "%n" handling functions, for the debug "format" argument.
  37564. *
  37565. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  37566. */
  37567. exports.formatters = {};
  37568. /**
  37569. * Previous log timestamp.
  37570. */
  37571. var prevTime;
  37572. /**
  37573. * Select a color.
  37574. * @param {String} namespace
  37575. * @return {Number}
  37576. * @api private
  37577. */
  37578. function selectColor(namespace) {
  37579. var hash = 0, i;
  37580. for (i in namespace) {
  37581. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  37582. hash |= 0; // Convert to 32bit integer
  37583. }
  37584. return exports.colors[Math.abs(hash) % exports.colors.length];
  37585. }
  37586. /**
  37587. * Create a debugger with the given `namespace`.
  37588. *
  37589. * @param {String} namespace
  37590. * @return {Function}
  37591. * @api public
  37592. */
  37593. function createDebug(namespace) {
  37594. function debug() {
  37595. // disabled?
  37596. if (!debug.enabled) return;
  37597. var self = debug;
  37598. // set `diff` timestamp
  37599. var curr = +new Date();
  37600. var ms = curr - (prevTime || curr);
  37601. self.diff = ms;
  37602. self.prev = prevTime;
  37603. self.curr = curr;
  37604. prevTime = curr;
  37605. // turn the `arguments` into a proper Array
  37606. var args = new Array(arguments.length);
  37607. for (var i = 0; i < args.length; i++) {
  37608. args[i] = arguments[i];
  37609. }
  37610. args[0] = exports.coerce(args[0]);
  37611. if ('string' !== typeof args[0]) {
  37612. // anything else let's inspect with %O
  37613. args.unshift('%O');
  37614. }
  37615. // apply any `formatters` transformations
  37616. var index = 0;
  37617. args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
  37618. // if we encounter an escaped % then don't increase the array index
  37619. if (match === '%%') return match;
  37620. index++;
  37621. var formatter = exports.formatters[format];
  37622. if ('function' === typeof formatter) {
  37623. var val = args[index];
  37624. match = formatter.call(self, val);
  37625. // now we need to remove `args[index]` since it's inlined in the `format`
  37626. args.splice(index, 1);
  37627. index--;
  37628. }
  37629. return match;
  37630. });
  37631. // apply env-specific formatting (colors, etc.)
  37632. exports.formatArgs.call(self, args);
  37633. var logFn = debug.log || exports.log || console.log.bind(console);
  37634. logFn.apply(self, args);
  37635. }
  37636. debug.namespace = namespace;
  37637. debug.enabled = exports.enabled(namespace);
  37638. debug.useColors = exports.useColors();
  37639. debug.color = selectColor(namespace);
  37640. // env-specific initialization logic for debug instances
  37641. if ('function' === typeof exports.init) {
  37642. exports.init(debug);
  37643. }
  37644. return debug;
  37645. }
  37646. /**
  37647. * Enables a debug mode by namespaces. This can include modes
  37648. * separated by a colon and wildcards.
  37649. *
  37650. * @param {String} namespaces
  37651. * @api public
  37652. */
  37653. function enable(namespaces) {
  37654. exports.save(namespaces);
  37655. exports.names = [];
  37656. exports.skips = [];
  37657. var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  37658. var len = split.length;
  37659. for (var i = 0; i < len; i++) {
  37660. if (!split[i]) continue; // ignore empty strings
  37661. namespaces = split[i].replace(/\*/g, '.*?');
  37662. if (namespaces[0] === '-') {
  37663. exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
  37664. } else {
  37665. exports.names.push(new RegExp('^' + namespaces + '$'));
  37666. }
  37667. }
  37668. }
  37669. /**
  37670. * Disable debug output.
  37671. *
  37672. * @api public
  37673. */
  37674. function disable() {
  37675. exports.enable('');
  37676. }
  37677. /**
  37678. * Returns true if the given mode name is enabled, false otherwise.
  37679. *
  37680. * @param {String} name
  37681. * @return {Boolean}
  37682. * @api public
  37683. */
  37684. function enabled(name) {
  37685. var i, len;
  37686. for (i = 0, len = exports.skips.length; i < len; i++) {
  37687. if (exports.skips[i].test(name)) {
  37688. return false;
  37689. }
  37690. }
  37691. for (i = 0, len = exports.names.length; i < len; i++) {
  37692. if (exports.names[i].test(name)) {
  37693. return true;
  37694. }
  37695. }
  37696. return false;
  37697. }
  37698. /**
  37699. * Coerce `val`.
  37700. *
  37701. * @param {Mixed} val
  37702. * @return {Mixed}
  37703. * @api private
  37704. */
  37705. function coerce(val) {
  37706. if (val instanceof Error) return val.stack || val.message;
  37707. return val;
  37708. }
  37709. } (debug$f, debug$f.exports));
  37710. return debug$f.exports;
  37711. }
  37712. /**
  37713. * This is the web browser implementation of `debug()`.
  37714. *
  37715. * Expose `debug()` as the module.
  37716. */
  37717. var hasRequiredBrowser;
  37718. function requireBrowser () {
  37719. if (hasRequiredBrowser) return browser.exports;
  37720. hasRequiredBrowser = 1;
  37721. (function (module, exports) {
  37722. exports = module.exports = requireDebug();
  37723. exports.log = log;
  37724. exports.formatArgs = formatArgs;
  37725. exports.save = save;
  37726. exports.load = load;
  37727. exports.useColors = useColors;
  37728. exports.storage = 'undefined' != typeof chrome
  37729. && 'undefined' != typeof chrome.storage
  37730. ? chrome.storage.local
  37731. : localstorage();
  37732. /**
  37733. * Colors.
  37734. */
  37735. exports.colors = [
  37736. 'lightseagreen',
  37737. 'forestgreen',
  37738. 'goldenrod',
  37739. 'dodgerblue',
  37740. 'darkorchid',
  37741. 'crimson'
  37742. ];
  37743. /**
  37744. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  37745. * and the Firebug extension (any Firefox version) are known
  37746. * to support "%c" CSS customizations.
  37747. *
  37748. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  37749. */
  37750. function useColors() {
  37751. // NB: In an Electron preload script, document will be defined but not fully
  37752. // initialized. Since we know we're in Chrome, we'll just detect this case
  37753. // explicitly
  37754. if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
  37755. return true;
  37756. }
  37757. // is webkit? http://stackoverflow.com/a/16459606/376773
  37758. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  37759. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  37760. // is firebug? http://stackoverflow.com/a/398120/376773
  37761. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  37762. // is firefox >= v31?
  37763. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  37764. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
  37765. // double check webkit in userAgent just in case we are in a worker
  37766. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  37767. }
  37768. /**
  37769. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  37770. */
  37771. exports.formatters.j = function(v) {
  37772. try {
  37773. return JSON.stringify(v);
  37774. } catch (err) {
  37775. return '[UnexpectedJSONParseError]: ' + err.message;
  37776. }
  37777. };
  37778. /**
  37779. * Colorize log arguments if enabled.
  37780. *
  37781. * @api public
  37782. */
  37783. function formatArgs(args) {
  37784. var useColors = this.useColors;
  37785. args[0] = (useColors ? '%c' : '')
  37786. + this.namespace
  37787. + (useColors ? ' %c' : ' ')
  37788. + args[0]
  37789. + (useColors ? '%c ' : ' ')
  37790. + '+' + exports.humanize(this.diff);
  37791. if (!useColors) return;
  37792. var c = 'color: ' + this.color;
  37793. args.splice(1, 0, c, 'color: inherit');
  37794. // the final "%c" is somewhat tricky, because there could be other
  37795. // arguments passed either before or after the %c, so we need to
  37796. // figure out the correct index to insert the CSS into
  37797. var index = 0;
  37798. var lastC = 0;
  37799. args[0].replace(/%[a-zA-Z%]/g, function(match) {
  37800. if ('%%' === match) return;
  37801. index++;
  37802. if ('%c' === match) {
  37803. // we only are interested in the *last* %c
  37804. // (the user may have provided their own)
  37805. lastC = index;
  37806. }
  37807. });
  37808. args.splice(lastC, 0, c);
  37809. }
  37810. /**
  37811. * Invokes `console.log()` when available.
  37812. * No-op when `console.log` is not a "function".
  37813. *
  37814. * @api public
  37815. */
  37816. function log() {
  37817. // this hackery is required for IE8/9, where
  37818. // the `console.log` function doesn't have 'apply'
  37819. return 'object' === typeof console
  37820. && console.log
  37821. && Function.prototype.apply.call(console.log, console, arguments);
  37822. }
  37823. /**
  37824. * Save `namespaces`.
  37825. *
  37826. * @param {String} namespaces
  37827. * @api private
  37828. */
  37829. function save(namespaces) {
  37830. try {
  37831. if (null == namespaces) {
  37832. exports.storage.removeItem('debug');
  37833. } else {
  37834. exports.storage.debug = namespaces;
  37835. }
  37836. } catch(e) {}
  37837. }
  37838. /**
  37839. * Load `namespaces`.
  37840. *
  37841. * @return {String} returns the previously persisted debug modes
  37842. * @api private
  37843. */
  37844. function load() {
  37845. var r;
  37846. try {
  37847. r = exports.storage.debug;
  37848. } catch(e) {}
  37849. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  37850. if (!r && typeof process !== 'undefined' && 'env' in process) {
  37851. r = process.env.DEBUG;
  37852. }
  37853. return r;
  37854. }
  37855. /**
  37856. * Enable namespaces listed in `localStorage.debug` initially.
  37857. */
  37858. exports.enable(load());
  37859. /**
  37860. * Localstorage attempts to return the localstorage.
  37861. *
  37862. * This is necessary because safari throws
  37863. * when a user disables cookies/localstorage
  37864. * and you attempt to access it.
  37865. *
  37866. * @return {LocalStorage}
  37867. * @api private
  37868. */
  37869. function localstorage() {
  37870. try {
  37871. return window.localStorage;
  37872. } catch (e) {}
  37873. }
  37874. } (browser, browser.exports));
  37875. return browser.exports;
  37876. }
  37877. var node = {exports: {}};
  37878. /**
  37879. * Module dependencies.
  37880. */
  37881. var hasRequiredNode;
  37882. function requireNode () {
  37883. if (hasRequiredNode) return node.exports;
  37884. hasRequiredNode = 1;
  37885. (function (module, exports) {
  37886. var tty = require$$0$3;
  37887. var util = require$$0$6;
  37888. /**
  37889. * This is the Node.js implementation of `debug()`.
  37890. *
  37891. * Expose `debug()` as the module.
  37892. */
  37893. exports = module.exports = requireDebug();
  37894. exports.init = init;
  37895. exports.log = log;
  37896. exports.formatArgs = formatArgs;
  37897. exports.save = save;
  37898. exports.load = load;
  37899. exports.useColors = useColors;
  37900. /**
  37901. * Colors.
  37902. */
  37903. exports.colors = [6, 2, 3, 4, 5, 1];
  37904. /**
  37905. * Build up the default `inspectOpts` object from the environment variables.
  37906. *
  37907. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  37908. */
  37909. exports.inspectOpts = Object.keys(process.env).filter(function (key) {
  37910. return /^debug_/i.test(key);
  37911. }).reduce(function (obj, key) {
  37912. // camel-case
  37913. var prop = key
  37914. .substring(6)
  37915. .toLowerCase()
  37916. .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
  37917. // coerce string value into JS value
  37918. var val = process.env[key];
  37919. if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
  37920. else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
  37921. else if (val === 'null') val = null;
  37922. else val = Number(val);
  37923. obj[prop] = val;
  37924. return obj;
  37925. }, {});
  37926. /**
  37927. * The file descriptor to write the `debug()` calls to.
  37928. * Set the `DEBUG_FD` env variable to override with another value. i.e.:
  37929. *
  37930. * $ DEBUG_FD=3 node script.js 3>debug.log
  37931. */
  37932. var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
  37933. if (1 !== fd && 2 !== fd) {
  37934. util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')();
  37935. }
  37936. var stream = 1 === fd ? process.stdout :
  37937. 2 === fd ? process.stderr :
  37938. createWritableStdioStream(fd);
  37939. /**
  37940. * Is stdout a TTY? Colored output is enabled when `true`.
  37941. */
  37942. function useColors() {
  37943. return 'colors' in exports.inspectOpts
  37944. ? Boolean(exports.inspectOpts.colors)
  37945. : tty.isatty(fd);
  37946. }
  37947. /**
  37948. * Map %o to `util.inspect()`, all on a single line.
  37949. */
  37950. exports.formatters.o = function(v) {
  37951. this.inspectOpts.colors = this.useColors;
  37952. return util.inspect(v, this.inspectOpts)
  37953. .split('\n').map(function(str) {
  37954. return str.trim()
  37955. }).join(' ');
  37956. };
  37957. /**
  37958. * Map %o to `util.inspect()`, allowing multiple lines if needed.
  37959. */
  37960. exports.formatters.O = function(v) {
  37961. this.inspectOpts.colors = this.useColors;
  37962. return util.inspect(v, this.inspectOpts);
  37963. };
  37964. /**
  37965. * Adds ANSI color escape codes if enabled.
  37966. *
  37967. * @api public
  37968. */
  37969. function formatArgs(args) {
  37970. var name = this.namespace;
  37971. var useColors = this.useColors;
  37972. if (useColors) {
  37973. var c = this.color;
  37974. var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
  37975. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  37976. args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
  37977. } else {
  37978. args[0] = new Date().toUTCString()
  37979. + ' ' + name + ' ' + args[0];
  37980. }
  37981. }
  37982. /**
  37983. * Invokes `util.format()` with the specified arguments and writes to `stream`.
  37984. */
  37985. function log() {
  37986. return stream.write(util.format.apply(util, arguments) + '\n');
  37987. }
  37988. /**
  37989. * Save `namespaces`.
  37990. *
  37991. * @param {String} namespaces
  37992. * @api private
  37993. */
  37994. function save(namespaces) {
  37995. if (null == namespaces) {
  37996. // If you set a process.env field to null or undefined, it gets cast to the
  37997. // string 'null' or 'undefined'. Just delete instead.
  37998. delete process.env.DEBUG;
  37999. } else {
  38000. process.env.DEBUG = namespaces;
  38001. }
  38002. }
  38003. /**
  38004. * Load `namespaces`.
  38005. *
  38006. * @return {String} returns the previously persisted debug modes
  38007. * @api private
  38008. */
  38009. function load() {
  38010. return process.env.DEBUG;
  38011. }
  38012. /**
  38013. * Copied from `node/src/node.js`.
  38014. *
  38015. * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
  38016. * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
  38017. */
  38018. function createWritableStdioStream (fd) {
  38019. var stream;
  38020. var tty_wrap = process.binding('tty_wrap');
  38021. // Note stream._type is used for test-module-load-list.js
  38022. switch (tty_wrap.guessHandleType(fd)) {
  38023. case 'TTY':
  38024. stream = new tty.WriteStream(fd);
  38025. stream._type = 'tty';
  38026. // Hack to have stream not keep the event loop alive.
  38027. // See https://github.com/joyent/node/issues/1726
  38028. if (stream._handle && stream._handle.unref) {
  38029. stream._handle.unref();
  38030. }
  38031. break;
  38032. case 'FILE':
  38033. var fs = require$$0__default;
  38034. stream = new fs.SyncWriteStream(fd, { autoClose: false });
  38035. stream._type = 'fs';
  38036. break;
  38037. case 'PIPE':
  38038. case 'TCP':
  38039. var net = require$$4$1;
  38040. stream = new net.Socket({
  38041. fd: fd,
  38042. readable: false,
  38043. writable: true
  38044. });
  38045. // FIXME Should probably have an option in net.Socket to create a
  38046. // stream from an existing fd which is writable only. But for now
  38047. // we'll just add this hack and set the `readable` member to false.
  38048. // Test: ./node test/fixtures/echo.js < /etc/passwd
  38049. stream.readable = false;
  38050. stream.read = null;
  38051. stream._type = 'pipe';
  38052. // FIXME Hack to have stream not keep the event loop alive.
  38053. // See https://github.com/joyent/node/issues/1726
  38054. if (stream._handle && stream._handle.unref) {
  38055. stream._handle.unref();
  38056. }
  38057. break;
  38058. default:
  38059. // Probably an error on in uv_guess_handle()
  38060. throw new Error('Implement me. Unknown stream file type!');
  38061. }
  38062. // For supporting legacy API we put the FD here.
  38063. stream.fd = fd;
  38064. stream._isStdio = true;
  38065. return stream;
  38066. }
  38067. /**
  38068. * Init logic for `debug` instances.
  38069. *
  38070. * Create a new `inspectOpts` object in case `useColors` is set
  38071. * differently for a particular `debug` instance.
  38072. */
  38073. function init (debug) {
  38074. debug.inspectOpts = {};
  38075. var keys = Object.keys(exports.inspectOpts);
  38076. for (var i = 0; i < keys.length; i++) {
  38077. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  38078. }
  38079. }
  38080. /**
  38081. * Enable namespaces listed in `process.env.DEBUG` initially.
  38082. */
  38083. exports.enable(load());
  38084. } (node, node.exports));
  38085. return node.exports;
  38086. }
  38087. /**
  38088. * Detect Electron renderer process, which is node, but we should
  38089. * treat as a browser.
  38090. */
  38091. if (typeof process !== 'undefined' && process.type === 'renderer') {
  38092. src.exports = requireBrowser();
  38093. } else {
  38094. src.exports = requireNode();
  38095. }
  38096. var srcExports = src.exports;
  38097. /*!
  38098. * encodeurl
  38099. * Copyright(c) 2016 Douglas Christopher Wilson
  38100. * MIT Licensed
  38101. */
  38102. /**
  38103. * Module exports.
  38104. * @public
  38105. */
  38106. var encodeurl = encodeUrl$1;
  38107. /**
  38108. * RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
  38109. * and including invalid escape sequences.
  38110. * @private
  38111. */
  38112. var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g;
  38113. /**
  38114. * RegExp to match unmatched surrogate pair.
  38115. * @private
  38116. */
  38117. var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
  38118. /**
  38119. * String to replace unmatched surrogate pair with.
  38120. * @private
  38121. */
  38122. var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2';
  38123. /**
  38124. * Encode a URL to a percent-encoded form, excluding already-encoded sequences.
  38125. *
  38126. * This function will take an already-encoded URL and encode all the non-URL
  38127. * code points. This function will not encode the "%" character unless it is
  38128. * not part of a valid sequence (`%20` will be left as-is, but `%foo` will
  38129. * be encoded as `%25foo`).
  38130. *
  38131. * This encode is meant to be "safe" and does not throw errors. It will try as
  38132. * hard as it can to properly encode the given URL, including replacing any raw,
  38133. * unpaired surrogate pairs with the Unicode replacement character prior to
  38134. * encoding.
  38135. *
  38136. * @param {string} url
  38137. * @return {string}
  38138. * @public
  38139. */
  38140. function encodeUrl$1 (url) {
  38141. return String(url)
  38142. .replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
  38143. .replace(ENCODE_CHARS_REGEXP, encodeURI)
  38144. }
  38145. /*!
  38146. * escape-html
  38147. * Copyright(c) 2012-2013 TJ Holowaychuk
  38148. * Copyright(c) 2015 Andreas Lubbe
  38149. * Copyright(c) 2015 Tiancheng "Timothy" Gu
  38150. * MIT Licensed
  38151. */
  38152. /**
  38153. * Module variables.
  38154. * @private
  38155. */
  38156. var matchHtmlRegExp = /["'&<>]/;
  38157. /**
  38158. * Module exports.
  38159. * @public
  38160. */
  38161. var escapeHtml_1 = escapeHtml$1;
  38162. /**
  38163. * Escape special characters in the given string of html.
  38164. *
  38165. * @param {string} string The string to escape for inserting into HTML
  38166. * @return {string}
  38167. * @public
  38168. */
  38169. function escapeHtml$1(string) {
  38170. var str = '' + string;
  38171. var match = matchHtmlRegExp.exec(str);
  38172. if (!match) {
  38173. return str;
  38174. }
  38175. var escape;
  38176. var html = '';
  38177. var index = 0;
  38178. var lastIndex = 0;
  38179. for (index = match.index; index < str.length; index++) {
  38180. switch (str.charCodeAt(index)) {
  38181. case 34: // "
  38182. escape = '&quot;';
  38183. break;
  38184. case 38: // &
  38185. escape = '&amp;';
  38186. break;
  38187. case 39: // '
  38188. escape = '&#39;';
  38189. break;
  38190. case 60: // <
  38191. escape = '&lt;';
  38192. break;
  38193. case 62: // >
  38194. escape = '&gt;';
  38195. break;
  38196. default:
  38197. continue;
  38198. }
  38199. if (lastIndex !== index) {
  38200. html += str.substring(lastIndex, index);
  38201. }
  38202. lastIndex = index + 1;
  38203. html += escape;
  38204. }
  38205. return lastIndex !== index
  38206. ? html + str.substring(lastIndex, index)
  38207. : html;
  38208. }
  38209. var escapeHtml$2 = /*@__PURE__*/getDefaultExportFromCjs(escapeHtml_1);
  38210. var onFinished$2 = {exports: {}};
  38211. /*!
  38212. * ee-first
  38213. * Copyright(c) 2014 Jonathan Ong
  38214. * MIT Licensed
  38215. */
  38216. /**
  38217. * Module exports.
  38218. * @public
  38219. */
  38220. var eeFirst = first$1;
  38221. /**
  38222. * Get the first event in a set of event emitters and event pairs.
  38223. *
  38224. * @param {array} stuff
  38225. * @param {function} done
  38226. * @public
  38227. */
  38228. function first$1(stuff, done) {
  38229. if (!Array.isArray(stuff))
  38230. throw new TypeError('arg must be an array of [ee, events...] arrays')
  38231. var cleanups = [];
  38232. for (var i = 0; i < stuff.length; i++) {
  38233. var arr = stuff[i];
  38234. if (!Array.isArray(arr) || arr.length < 2)
  38235. throw new TypeError('each array member must be [ee, events...]')
  38236. var ee = arr[0];
  38237. for (var j = 1; j < arr.length; j++) {
  38238. var event = arr[j];
  38239. var fn = listener(event, callback);
  38240. // listen to the event
  38241. ee.on(event, fn);
  38242. // push this listener to the list of cleanups
  38243. cleanups.push({
  38244. ee: ee,
  38245. event: event,
  38246. fn: fn,
  38247. });
  38248. }
  38249. }
  38250. function callback() {
  38251. cleanup();
  38252. done.apply(null, arguments);
  38253. }
  38254. function cleanup() {
  38255. var x;
  38256. for (var i = 0; i < cleanups.length; i++) {
  38257. x = cleanups[i];
  38258. x.ee.removeListener(x.event, x.fn);
  38259. }
  38260. }
  38261. function thunk(fn) {
  38262. done = fn;
  38263. }
  38264. thunk.cancel = cleanup;
  38265. return thunk
  38266. }
  38267. /**
  38268. * Create the event listener.
  38269. * @private
  38270. */
  38271. function listener(event, done) {
  38272. return function onevent(arg1) {
  38273. var args = new Array(arguments.length);
  38274. var ee = this;
  38275. var err = event === 'error'
  38276. ? arg1
  38277. : null;
  38278. // copy args to prevent arguments escaping scope
  38279. for (var i = 0; i < args.length; i++) {
  38280. args[i] = arguments[i];
  38281. }
  38282. done(err, ee, event, args);
  38283. }
  38284. }
  38285. /*!
  38286. * on-finished
  38287. * Copyright(c) 2013 Jonathan Ong
  38288. * Copyright(c) 2014 Douglas Christopher Wilson
  38289. * MIT Licensed
  38290. */
  38291. /**
  38292. * Module exports.
  38293. * @public
  38294. */
  38295. onFinished$2.exports = onFinished$1;
  38296. onFinished$2.exports.isFinished = isFinished$1;
  38297. /**
  38298. * Module dependencies.
  38299. * @private
  38300. */
  38301. var first = eeFirst;
  38302. /**
  38303. * Variables.
  38304. * @private
  38305. */
  38306. /* istanbul ignore next */
  38307. var defer$2 = typeof setImmediate === 'function'
  38308. ? setImmediate
  38309. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  38310. /**
  38311. * Invoke callback when the response has finished, useful for
  38312. * cleaning up resources afterwards.
  38313. *
  38314. * @param {object} msg
  38315. * @param {function} listener
  38316. * @return {object}
  38317. * @public
  38318. */
  38319. function onFinished$1(msg, listener) {
  38320. if (isFinished$1(msg) !== false) {
  38321. defer$2(listener, null, msg);
  38322. return msg
  38323. }
  38324. // attach the listener to the message
  38325. attachListener(msg, listener);
  38326. return msg
  38327. }
  38328. /**
  38329. * Determine if message is already finished.
  38330. *
  38331. * @param {object} msg
  38332. * @return {boolean}
  38333. * @public
  38334. */
  38335. function isFinished$1(msg) {
  38336. var socket = msg.socket;
  38337. if (typeof msg.finished === 'boolean') {
  38338. // OutgoingMessage
  38339. return Boolean(msg.finished || (socket && !socket.writable))
  38340. }
  38341. if (typeof msg.complete === 'boolean') {
  38342. // IncomingMessage
  38343. return Boolean(msg.upgrade || !socket || !socket.readable || (msg.complete && !msg.readable))
  38344. }
  38345. // don't know
  38346. return undefined
  38347. }
  38348. /**
  38349. * Attach a finished listener to the message.
  38350. *
  38351. * @param {object} msg
  38352. * @param {function} callback
  38353. * @private
  38354. */
  38355. function attachFinishedListener(msg, callback) {
  38356. var eeMsg;
  38357. var eeSocket;
  38358. var finished = false;
  38359. function onFinish(error) {
  38360. eeMsg.cancel();
  38361. eeSocket.cancel();
  38362. finished = true;
  38363. callback(error);
  38364. }
  38365. // finished on first message event
  38366. eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish);
  38367. function onSocket(socket) {
  38368. // remove listener
  38369. msg.removeListener('socket', onSocket);
  38370. if (finished) return
  38371. if (eeMsg !== eeSocket) return
  38372. // finished on first socket event
  38373. eeSocket = first([[socket, 'error', 'close']], onFinish);
  38374. }
  38375. if (msg.socket) {
  38376. // socket already assigned
  38377. onSocket(msg.socket);
  38378. return
  38379. }
  38380. // wait for socket to be assigned
  38381. msg.on('socket', onSocket);
  38382. if (msg.socket === undefined) {
  38383. // node.js 0.8 patch
  38384. patchAssignSocket(msg, onSocket);
  38385. }
  38386. }
  38387. /**
  38388. * Attach the listener to the message.
  38389. *
  38390. * @param {object} msg
  38391. * @return {function}
  38392. * @private
  38393. */
  38394. function attachListener(msg, listener) {
  38395. var attached = msg.__onFinished;
  38396. // create a private single listener with queue
  38397. if (!attached || !attached.queue) {
  38398. attached = msg.__onFinished = createListener(msg);
  38399. attachFinishedListener(msg, attached);
  38400. }
  38401. attached.queue.push(listener);
  38402. }
  38403. /**
  38404. * Create listener on message.
  38405. *
  38406. * @param {object} msg
  38407. * @return {function}
  38408. * @private
  38409. */
  38410. function createListener(msg) {
  38411. function listener(err) {
  38412. if (msg.__onFinished === listener) msg.__onFinished = null;
  38413. if (!listener.queue) return
  38414. var queue = listener.queue;
  38415. listener.queue = null;
  38416. for (var i = 0; i < queue.length; i++) {
  38417. queue[i](err, msg);
  38418. }
  38419. }
  38420. listener.queue = [];
  38421. return listener
  38422. }
  38423. /**
  38424. * Patch ServerResponse.prototype.assignSocket for node.js 0.8.
  38425. *
  38426. * @param {ServerResponse} res
  38427. * @param {function} callback
  38428. * @private
  38429. */
  38430. function patchAssignSocket(res, callback) {
  38431. var assignSocket = res.assignSocket;
  38432. if (typeof assignSocket !== 'function') return
  38433. // res.on('socket', callback) is broken in 0.8
  38434. res.assignSocket = function _assignSocket(socket) {
  38435. assignSocket.call(this, socket);
  38436. callback(socket);
  38437. };
  38438. }
  38439. var onFinishedExports = onFinished$2.exports;
  38440. var parseurl$1 = {exports: {}};
  38441. /*!
  38442. * parseurl
  38443. * Copyright(c) 2014 Jonathan Ong
  38444. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38445. * MIT Licensed
  38446. */
  38447. /**
  38448. * Module dependencies.
  38449. * @private
  38450. */
  38451. var url$3 = require$$0$9;
  38452. var parse$8 = url$3.parse;
  38453. var Url = url$3.Url;
  38454. /**
  38455. * Module exports.
  38456. * @public
  38457. */
  38458. parseurl$1.exports = parseurl;
  38459. parseurl$1.exports.original = originalurl;
  38460. /**
  38461. * Parse the `req` url with memoization.
  38462. *
  38463. * @param {ServerRequest} req
  38464. * @return {Object}
  38465. * @public
  38466. */
  38467. function parseurl (req) {
  38468. var url = req.url;
  38469. if (url === undefined) {
  38470. // URL is undefined
  38471. return undefined
  38472. }
  38473. var parsed = req._parsedUrl;
  38474. if (fresh(url, parsed)) {
  38475. // Return cached URL parse
  38476. return parsed
  38477. }
  38478. // Parse the URL
  38479. parsed = fastparse(url);
  38480. parsed._raw = url;
  38481. return (req._parsedUrl = parsed)
  38482. }
  38483. /**
  38484. * Parse the `req` original url with fallback and memoization.
  38485. *
  38486. * @param {ServerRequest} req
  38487. * @return {Object}
  38488. * @public
  38489. */
  38490. function originalurl (req) {
  38491. var url = req.originalUrl;
  38492. if (typeof url !== 'string') {
  38493. // Fallback
  38494. return parseurl(req)
  38495. }
  38496. var parsed = req._parsedOriginalUrl;
  38497. if (fresh(url, parsed)) {
  38498. // Return cached URL parse
  38499. return parsed
  38500. }
  38501. // Parse the URL
  38502. parsed = fastparse(url);
  38503. parsed._raw = url;
  38504. return (req._parsedOriginalUrl = parsed)
  38505. }
  38506. /**
  38507. * Parse the `str` url with fast-path short-cut.
  38508. *
  38509. * @param {string} str
  38510. * @return {Object}
  38511. * @private
  38512. */
  38513. function fastparse (str) {
  38514. if (typeof str !== 'string' || str.charCodeAt(0) !== 0x2f /* / */) {
  38515. return parse$8(str)
  38516. }
  38517. var pathname = str;
  38518. var query = null;
  38519. var search = null;
  38520. // This takes the regexp from https://github.com/joyent/node/pull/7878
  38521. // Which is /^(\/[^?#\s]*)(\?[^#\s]*)?$/
  38522. // And unrolls it into a for loop
  38523. for (var i = 1; i < str.length; i++) {
  38524. switch (str.charCodeAt(i)) {
  38525. case 0x3f: /* ? */
  38526. if (search === null) {
  38527. pathname = str.substring(0, i);
  38528. query = str.substring(i + 1);
  38529. search = str.substring(i);
  38530. }
  38531. break
  38532. case 0x09: /* \t */
  38533. case 0x0a: /* \n */
  38534. case 0x0c: /* \f */
  38535. case 0x0d: /* \r */
  38536. case 0x20: /* */
  38537. case 0x23: /* # */
  38538. case 0xa0:
  38539. case 0xfeff:
  38540. return parse$8(str)
  38541. }
  38542. }
  38543. var url = Url !== undefined
  38544. ? new Url()
  38545. : {};
  38546. url.path = str;
  38547. url.href = str;
  38548. url.pathname = pathname;
  38549. if (search !== null) {
  38550. url.query = query;
  38551. url.search = search;
  38552. }
  38553. return url
  38554. }
  38555. /**
  38556. * Determine if parsed is still fresh for url.
  38557. *
  38558. * @param {string} url
  38559. * @param {object} parsedUrl
  38560. * @return {boolean}
  38561. * @private
  38562. */
  38563. function fresh (url, parsedUrl) {
  38564. return typeof parsedUrl === 'object' &&
  38565. parsedUrl !== null &&
  38566. (Url === undefined || parsedUrl instanceof Url) &&
  38567. parsedUrl._raw === url
  38568. }
  38569. var parseurlExports = parseurl$1.exports;
  38570. var require$$0$1 = {
  38571. "100": "Continue",
  38572. "101": "Switching Protocols",
  38573. "102": "Processing",
  38574. "103": "Early Hints",
  38575. "200": "OK",
  38576. "201": "Created",
  38577. "202": "Accepted",
  38578. "203": "Non-Authoritative Information",
  38579. "204": "No Content",
  38580. "205": "Reset Content",
  38581. "206": "Partial Content",
  38582. "207": "Multi-Status",
  38583. "208": "Already Reported",
  38584. "226": "IM Used",
  38585. "300": "Multiple Choices",
  38586. "301": "Moved Permanently",
  38587. "302": "Found",
  38588. "303": "See Other",
  38589. "304": "Not Modified",
  38590. "305": "Use Proxy",
  38591. "306": "(Unused)",
  38592. "307": "Temporary Redirect",
  38593. "308": "Permanent Redirect",
  38594. "400": "Bad Request",
  38595. "401": "Unauthorized",
  38596. "402": "Payment Required",
  38597. "403": "Forbidden",
  38598. "404": "Not Found",
  38599. "405": "Method Not Allowed",
  38600. "406": "Not Acceptable",
  38601. "407": "Proxy Authentication Required",
  38602. "408": "Request Timeout",
  38603. "409": "Conflict",
  38604. "410": "Gone",
  38605. "411": "Length Required",
  38606. "412": "Precondition Failed",
  38607. "413": "Payload Too Large",
  38608. "414": "URI Too Long",
  38609. "415": "Unsupported Media Type",
  38610. "416": "Range Not Satisfiable",
  38611. "417": "Expectation Failed",
  38612. "418": "I'm a teapot",
  38613. "421": "Misdirected Request",
  38614. "422": "Unprocessable Entity",
  38615. "423": "Locked",
  38616. "424": "Failed Dependency",
  38617. "425": "Unordered Collection",
  38618. "426": "Upgrade Required",
  38619. "428": "Precondition Required",
  38620. "429": "Too Many Requests",
  38621. "431": "Request Header Fields Too Large",
  38622. "451": "Unavailable For Legal Reasons",
  38623. "500": "Internal Server Error",
  38624. "501": "Not Implemented",
  38625. "502": "Bad Gateway",
  38626. "503": "Service Unavailable",
  38627. "504": "Gateway Timeout",
  38628. "505": "HTTP Version Not Supported",
  38629. "506": "Variant Also Negotiates",
  38630. "507": "Insufficient Storage",
  38631. "508": "Loop Detected",
  38632. "509": "Bandwidth Limit Exceeded",
  38633. "510": "Not Extended",
  38634. "511": "Network Authentication Required"
  38635. };
  38636. /*!
  38637. * statuses
  38638. * Copyright(c) 2014 Jonathan Ong
  38639. * Copyright(c) 2016 Douglas Christopher Wilson
  38640. * MIT Licensed
  38641. */
  38642. /**
  38643. * Module dependencies.
  38644. * @private
  38645. */
  38646. var codes = require$$0$1;
  38647. /**
  38648. * Module exports.
  38649. * @public
  38650. */
  38651. var statuses$1 = status;
  38652. // status code to message map
  38653. status.STATUS_CODES = codes;
  38654. // array of status codes
  38655. status.codes = populateStatusesMap(status, codes);
  38656. // status codes for redirects
  38657. status.redirect = {
  38658. 300: true,
  38659. 301: true,
  38660. 302: true,
  38661. 303: true,
  38662. 305: true,
  38663. 307: true,
  38664. 308: true
  38665. };
  38666. // status codes for empty bodies
  38667. status.empty = {
  38668. 204: true,
  38669. 205: true,
  38670. 304: true
  38671. };
  38672. // status codes for when you should retry the request
  38673. status.retry = {
  38674. 502: true,
  38675. 503: true,
  38676. 504: true
  38677. };
  38678. /**
  38679. * Populate the statuses map for given codes.
  38680. * @private
  38681. */
  38682. function populateStatusesMap (statuses, codes) {
  38683. var arr = [];
  38684. Object.keys(codes).forEach(function forEachCode (code) {
  38685. var message = codes[code];
  38686. var status = Number(code);
  38687. // Populate properties
  38688. statuses[status] = message;
  38689. statuses[message] = status;
  38690. statuses[message.toLowerCase()] = status;
  38691. // Add to array
  38692. arr.push(status);
  38693. });
  38694. return arr
  38695. }
  38696. /**
  38697. * Get the status code.
  38698. *
  38699. * Given a number, this will throw if it is not a known status
  38700. * code, otherwise the code will be returned. Given a string,
  38701. * the string will be parsed for a number and return the code
  38702. * if valid, otherwise will lookup the code assuming this is
  38703. * the status message.
  38704. *
  38705. * @param {string|number} code
  38706. * @returns {number}
  38707. * @public
  38708. */
  38709. function status (code) {
  38710. if (typeof code === 'number') {
  38711. if (!status[code]) throw new Error('invalid status code: ' + code)
  38712. return code
  38713. }
  38714. if (typeof code !== 'string') {
  38715. throw new TypeError('code must be a number or string')
  38716. }
  38717. // '403'
  38718. var n = parseInt(code, 10);
  38719. if (!isNaN(n)) {
  38720. if (!status[n]) throw new Error('invalid status code: ' + n)
  38721. return n
  38722. }
  38723. n = status[code.toLowerCase()];
  38724. if (!n) throw new Error('invalid status message: "' + code + '"')
  38725. return n
  38726. }
  38727. /*!
  38728. * unpipe
  38729. * Copyright(c) 2015 Douglas Christopher Wilson
  38730. * MIT Licensed
  38731. */
  38732. /**
  38733. * Module exports.
  38734. * @public
  38735. */
  38736. var unpipe_1 = unpipe$1;
  38737. /**
  38738. * Determine if there are Node.js pipe-like data listeners.
  38739. * @private
  38740. */
  38741. function hasPipeDataListeners(stream) {
  38742. var listeners = stream.listeners('data');
  38743. for (var i = 0; i < listeners.length; i++) {
  38744. if (listeners[i].name === 'ondata') {
  38745. return true
  38746. }
  38747. }
  38748. return false
  38749. }
  38750. /**
  38751. * Unpipe a stream from all destinations.
  38752. *
  38753. * @param {object} stream
  38754. * @public
  38755. */
  38756. function unpipe$1(stream) {
  38757. if (!stream) {
  38758. throw new TypeError('argument stream is required')
  38759. }
  38760. if (typeof stream.unpipe === 'function') {
  38761. // new-style
  38762. stream.unpipe();
  38763. return
  38764. }
  38765. // Node.js 0.8 hack
  38766. if (!hasPipeDataListeners(stream)) {
  38767. return
  38768. }
  38769. var listener;
  38770. var listeners = stream.listeners('close');
  38771. for (var i = 0; i < listeners.length; i++) {
  38772. listener = listeners[i];
  38773. if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
  38774. continue
  38775. }
  38776. // invoke the listener
  38777. listener.call(stream);
  38778. }
  38779. }
  38780. /*!
  38781. * finalhandler
  38782. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38783. * MIT Licensed
  38784. */
  38785. /**
  38786. * Module dependencies.
  38787. * @private
  38788. */
  38789. var debug$e = srcExports('finalhandler');
  38790. var encodeUrl = encodeurl;
  38791. var escapeHtml = escapeHtml_1;
  38792. var onFinished = onFinishedExports;
  38793. var parseUrl$2 = parseurlExports;
  38794. var statuses = statuses$1;
  38795. var unpipe = unpipe_1;
  38796. /**
  38797. * Module variables.
  38798. * @private
  38799. */
  38800. var DOUBLE_SPACE_REGEXP = /\x20{2}/g;
  38801. var NEWLINE_REGEXP = /\n/g;
  38802. /* istanbul ignore next */
  38803. var defer$1 = typeof setImmediate === 'function'
  38804. ? setImmediate
  38805. : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)); };
  38806. var isFinished = onFinished.isFinished;
  38807. /**
  38808. * Create a minimal HTML document.
  38809. *
  38810. * @param {string} message
  38811. * @private
  38812. */
  38813. function createHtmlDocument (message) {
  38814. var body = escapeHtml(message)
  38815. .replace(NEWLINE_REGEXP, '<br>')
  38816. .replace(DOUBLE_SPACE_REGEXP, ' &nbsp;');
  38817. return '<!DOCTYPE html>\n' +
  38818. '<html lang="en">\n' +
  38819. '<head>\n' +
  38820. '<meta charset="utf-8">\n' +
  38821. '<title>Error</title>\n' +
  38822. '</head>\n' +
  38823. '<body>\n' +
  38824. '<pre>' + body + '</pre>\n' +
  38825. '</body>\n' +
  38826. '</html>\n'
  38827. }
  38828. /**
  38829. * Module exports.
  38830. * @public
  38831. */
  38832. var finalhandler_1 = finalhandler$1;
  38833. /**
  38834. * Create a function to handle the final response.
  38835. *
  38836. * @param {Request} req
  38837. * @param {Response} res
  38838. * @param {Object} [options]
  38839. * @return {Function}
  38840. * @public
  38841. */
  38842. function finalhandler$1 (req, res, options) {
  38843. var opts = options || {};
  38844. // get environment
  38845. var env = opts.env || process.env.NODE_ENV || 'development';
  38846. // get error callback
  38847. var onerror = opts.onerror;
  38848. return function (err) {
  38849. var headers;
  38850. var msg;
  38851. var status;
  38852. // ignore 404 on in-flight response
  38853. if (!err && headersSent(res)) {
  38854. debug$e('cannot 404 after headers sent');
  38855. return
  38856. }
  38857. // unhandled error
  38858. if (err) {
  38859. // respect status code from error
  38860. status = getErrorStatusCode(err);
  38861. if (status === undefined) {
  38862. // fallback to status code on response
  38863. status = getResponseStatusCode(res);
  38864. } else {
  38865. // respect headers from error
  38866. headers = getErrorHeaders(err);
  38867. }
  38868. // get error message
  38869. msg = getErrorMessage(err, status, env);
  38870. } else {
  38871. // not found
  38872. status = 404;
  38873. msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req));
  38874. }
  38875. debug$e('default %s', status);
  38876. // schedule onerror callback
  38877. if (err && onerror) {
  38878. defer$1(onerror, err, req, res);
  38879. }
  38880. // cannot actually respond
  38881. if (headersSent(res)) {
  38882. debug$e('cannot %d after headers sent', status);
  38883. req.socket.destroy();
  38884. return
  38885. }
  38886. // send response
  38887. send$2(req, res, status, headers, msg);
  38888. }
  38889. }
  38890. /**
  38891. * Get headers from Error object.
  38892. *
  38893. * @param {Error} err
  38894. * @return {object}
  38895. * @private
  38896. */
  38897. function getErrorHeaders (err) {
  38898. if (!err.headers || typeof err.headers !== 'object') {
  38899. return undefined
  38900. }
  38901. var headers = Object.create(null);
  38902. var keys = Object.keys(err.headers);
  38903. for (var i = 0; i < keys.length; i++) {
  38904. var key = keys[i];
  38905. headers[key] = err.headers[key];
  38906. }
  38907. return headers
  38908. }
  38909. /**
  38910. * Get message from Error object, fallback to status message.
  38911. *
  38912. * @param {Error} err
  38913. * @param {number} status
  38914. * @param {string} env
  38915. * @return {string}
  38916. * @private
  38917. */
  38918. function getErrorMessage (err, status, env) {
  38919. var msg;
  38920. if (env !== 'production') {
  38921. // use err.stack, which typically includes err.message
  38922. msg = err.stack;
  38923. // fallback to err.toString() when possible
  38924. if (!msg && typeof err.toString === 'function') {
  38925. msg = err.toString();
  38926. }
  38927. }
  38928. return msg || statuses[status]
  38929. }
  38930. /**
  38931. * Get status code from Error object.
  38932. *
  38933. * @param {Error} err
  38934. * @return {number}
  38935. * @private
  38936. */
  38937. function getErrorStatusCode (err) {
  38938. // check err.status
  38939. if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) {
  38940. return err.status
  38941. }
  38942. // check err.statusCode
  38943. if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) {
  38944. return err.statusCode
  38945. }
  38946. return undefined
  38947. }
  38948. /**
  38949. * Get resource name for the request.
  38950. *
  38951. * This is typically just the original pathname of the request
  38952. * but will fallback to "resource" is that cannot be determined.
  38953. *
  38954. * @param {IncomingMessage} req
  38955. * @return {string}
  38956. * @private
  38957. */
  38958. function getResourceName (req) {
  38959. try {
  38960. return parseUrl$2.original(req).pathname
  38961. } catch (e) {
  38962. return 'resource'
  38963. }
  38964. }
  38965. /**
  38966. * Get status code from response.
  38967. *
  38968. * @param {OutgoingMessage} res
  38969. * @return {number}
  38970. * @private
  38971. */
  38972. function getResponseStatusCode (res) {
  38973. var status = res.statusCode;
  38974. // default status code to 500 if outside valid range
  38975. if (typeof status !== 'number' || status < 400 || status > 599) {
  38976. status = 500;
  38977. }
  38978. return status
  38979. }
  38980. /**
  38981. * Determine if the response headers have been sent.
  38982. *
  38983. * @param {object} res
  38984. * @returns {boolean}
  38985. * @private
  38986. */
  38987. function headersSent (res) {
  38988. return typeof res.headersSent !== 'boolean'
  38989. ? Boolean(res._header)
  38990. : res.headersSent
  38991. }
  38992. /**
  38993. * Send response.
  38994. *
  38995. * @param {IncomingMessage} req
  38996. * @param {OutgoingMessage} res
  38997. * @param {number} status
  38998. * @param {object} headers
  38999. * @param {string} message
  39000. * @private
  39001. */
  39002. function send$2 (req, res, status, headers, message) {
  39003. function write () {
  39004. // response body
  39005. var body = createHtmlDocument(message);
  39006. // response status
  39007. res.statusCode = status;
  39008. res.statusMessage = statuses[status];
  39009. // response headers
  39010. setHeaders(res, headers);
  39011. // security headers
  39012. res.setHeader('Content-Security-Policy', "default-src 'none'");
  39013. res.setHeader('X-Content-Type-Options', 'nosniff');
  39014. // standard headers
  39015. res.setHeader('Content-Type', 'text/html; charset=utf-8');
  39016. res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'));
  39017. if (req.method === 'HEAD') {
  39018. res.end();
  39019. return
  39020. }
  39021. res.end(body, 'utf8');
  39022. }
  39023. if (isFinished(req)) {
  39024. write();
  39025. return
  39026. }
  39027. // unpipe everything from the request
  39028. unpipe(req);
  39029. // flush the request
  39030. onFinished(req, write);
  39031. req.resume();
  39032. }
  39033. /**
  39034. * Set response headers from an object.
  39035. *
  39036. * @param {OutgoingMessage} res
  39037. * @param {object} headers
  39038. * @private
  39039. */
  39040. function setHeaders (res, headers) {
  39041. if (!headers) {
  39042. return
  39043. }
  39044. var keys = Object.keys(headers);
  39045. for (var i = 0; i < keys.length; i++) {
  39046. var key = keys[i];
  39047. res.setHeader(key, headers[key]);
  39048. }
  39049. }
  39050. var utilsMerge = {exports: {}};
  39051. /**
  39052. * Merge object b with object a.
  39053. *
  39054. * var a = { foo: 'bar' }
  39055. * , b = { bar: 'baz' };
  39056. *
  39057. * merge(a, b);
  39058. * // => { foo: 'bar', bar: 'baz' }
  39059. *
  39060. * @param {Object} a
  39061. * @param {Object} b
  39062. * @return {Object}
  39063. * @api public
  39064. */
  39065. (function (module, exports) {
  39066. module.exports = function(a, b){
  39067. if (a && b) {
  39068. for (var key in b) {
  39069. a[key] = b[key];
  39070. }
  39071. }
  39072. return a;
  39073. };
  39074. } (utilsMerge));
  39075. var utilsMergeExports = utilsMerge.exports;
  39076. /*!
  39077. * connect
  39078. * Copyright(c) 2010 Sencha Inc.
  39079. * Copyright(c) 2011 TJ Holowaychuk
  39080. * Copyright(c) 2015 Douglas Christopher Wilson
  39081. * MIT Licensed
  39082. */
  39083. /**
  39084. * Module dependencies.
  39085. * @private
  39086. */
  39087. var debug$d = srcExports('connect:dispatcher');
  39088. var EventEmitter$3 = require$$0$5.EventEmitter;
  39089. var finalhandler = finalhandler_1;
  39090. var http$4 = require$$1;
  39091. var merge = utilsMergeExports;
  39092. var parseUrl$1 = parseurlExports;
  39093. /**
  39094. * Module exports.
  39095. * @public
  39096. */
  39097. var connect = createServer$1;
  39098. /**
  39099. * Module variables.
  39100. * @private
  39101. */
  39102. var env = process.env.NODE_ENV || 'development';
  39103. var proto = {};
  39104. /* istanbul ignore next */
  39105. var defer = typeof setImmediate === 'function'
  39106. ? setImmediate
  39107. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  39108. /**
  39109. * Create a new connect server.
  39110. *
  39111. * @return {function}
  39112. * @public
  39113. */
  39114. function createServer$1() {
  39115. function app(req, res, next){ app.handle(req, res, next); }
  39116. merge(app, proto);
  39117. merge(app, EventEmitter$3.prototype);
  39118. app.route = '/';
  39119. app.stack = [];
  39120. return app;
  39121. }
  39122. /**
  39123. * Utilize the given middleware `handle` to the given `route`,
  39124. * defaulting to _/_. This "route" is the mount-point for the
  39125. * middleware, when given a value other than _/_ the middleware
  39126. * is only effective when that segment is present in the request's
  39127. * pathname.
  39128. *
  39129. * For example if we were to mount a function at _/admin_, it would
  39130. * be invoked on _/admin_, and _/admin/settings_, however it would
  39131. * not be invoked for _/_, or _/posts_.
  39132. *
  39133. * @param {String|Function|Server} route, callback or server
  39134. * @param {Function|Server} callback or server
  39135. * @return {Server} for chaining
  39136. * @public
  39137. */
  39138. proto.use = function use(route, fn) {
  39139. var handle = fn;
  39140. var path = route;
  39141. // default route to '/'
  39142. if (typeof route !== 'string') {
  39143. handle = route;
  39144. path = '/';
  39145. }
  39146. // wrap sub-apps
  39147. if (typeof handle.handle === 'function') {
  39148. var server = handle;
  39149. server.route = path;
  39150. handle = function (req, res, next) {
  39151. server.handle(req, res, next);
  39152. };
  39153. }
  39154. // wrap vanilla http.Servers
  39155. if (handle instanceof http$4.Server) {
  39156. handle = handle.listeners('request')[0];
  39157. }
  39158. // strip trailing slash
  39159. if (path[path.length - 1] === '/') {
  39160. path = path.slice(0, -1);
  39161. }
  39162. // add the middleware
  39163. debug$d('use %s %s', path || '/', handle.name || 'anonymous');
  39164. this.stack.push({ route: path, handle: handle });
  39165. return this;
  39166. };
  39167. /**
  39168. * Handle server requests, punting them down
  39169. * the middleware stack.
  39170. *
  39171. * @private
  39172. */
  39173. proto.handle = function handle(req, res, out) {
  39174. var index = 0;
  39175. var protohost = getProtohost(req.url) || '';
  39176. var removed = '';
  39177. var slashAdded = false;
  39178. var stack = this.stack;
  39179. // final function handler
  39180. var done = out || finalhandler(req, res, {
  39181. env: env,
  39182. onerror: logerror
  39183. });
  39184. // store the original URL
  39185. req.originalUrl = req.originalUrl || req.url;
  39186. function next(err) {
  39187. if (slashAdded) {
  39188. req.url = req.url.substr(1);
  39189. slashAdded = false;
  39190. }
  39191. if (removed.length !== 0) {
  39192. req.url = protohost + removed + req.url.substr(protohost.length);
  39193. removed = '';
  39194. }
  39195. // next callback
  39196. var layer = stack[index++];
  39197. // all done
  39198. if (!layer) {
  39199. defer(done, err);
  39200. return;
  39201. }
  39202. // route data
  39203. var path = parseUrl$1(req).pathname || '/';
  39204. var route = layer.route;
  39205. // skip this layer if the route doesn't match
  39206. if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) {
  39207. return next(err);
  39208. }
  39209. // skip if route match does not border "/", ".", or end
  39210. var c = path.length > route.length && path[route.length];
  39211. if (c && c !== '/' && c !== '.') {
  39212. return next(err);
  39213. }
  39214. // trim off the part of the url that matches the route
  39215. if (route.length !== 0 && route !== '/') {
  39216. removed = route;
  39217. req.url = protohost + req.url.substr(protohost.length + removed.length);
  39218. // ensure leading slash
  39219. if (!protohost && req.url[0] !== '/') {
  39220. req.url = '/' + req.url;
  39221. slashAdded = true;
  39222. }
  39223. }
  39224. // call the layer handle
  39225. call(layer.handle, route, err, req, res, next);
  39226. }
  39227. next();
  39228. };
  39229. /**
  39230. * Listen for connections.
  39231. *
  39232. * This method takes the same arguments
  39233. * as node's `http.Server#listen()`.
  39234. *
  39235. * HTTP and HTTPS:
  39236. *
  39237. * If you run your application both as HTTP
  39238. * and HTTPS you may wrap them individually,
  39239. * since your Connect "server" is really just
  39240. * a JavaScript `Function`.
  39241. *
  39242. * var connect = require('connect')
  39243. * , http = require('http')
  39244. * , https = require('https');
  39245. *
  39246. * var app = connect();
  39247. *
  39248. * http.createServer(app).listen(80);
  39249. * https.createServer(options, app).listen(443);
  39250. *
  39251. * @return {http.Server}
  39252. * @api public
  39253. */
  39254. proto.listen = function listen() {
  39255. var server = http$4.createServer(this);
  39256. return server.listen.apply(server, arguments);
  39257. };
  39258. /**
  39259. * Invoke a route handle.
  39260. * @private
  39261. */
  39262. function call(handle, route, err, req, res, next) {
  39263. var arity = handle.length;
  39264. var error = err;
  39265. var hasError = Boolean(err);
  39266. debug$d('%s %s : %s', handle.name || '<anonymous>', route, req.originalUrl);
  39267. try {
  39268. if (hasError && arity === 4) {
  39269. // error-handling middleware
  39270. handle(err, req, res, next);
  39271. return;
  39272. } else if (!hasError && arity < 4) {
  39273. // request-handling middleware
  39274. handle(req, res, next);
  39275. return;
  39276. }
  39277. } catch (e) {
  39278. // replace the error
  39279. error = e;
  39280. }
  39281. // continue
  39282. next(error);
  39283. }
  39284. /**
  39285. * Log error using console.error.
  39286. *
  39287. * @param {Error} err
  39288. * @private
  39289. */
  39290. function logerror(err) {
  39291. if (env !== 'test') console.error(err.stack || err.toString());
  39292. }
  39293. /**
  39294. * Get get protocol + host for a URL.
  39295. *
  39296. * @param {string} url
  39297. * @private
  39298. */
  39299. function getProtohost(url) {
  39300. if (url.length === 0 || url[0] === '/') {
  39301. return undefined;
  39302. }
  39303. var fqdnIndex = url.indexOf('://');
  39304. return fqdnIndex !== -1 && url.lastIndexOf('?', fqdnIndex) === -1
  39305. ? url.substr(0, url.indexOf('/', 3 + fqdnIndex))
  39306. : undefined;
  39307. }
  39308. var connect$1 = /*@__PURE__*/getDefaultExportFromCjs(connect);
  39309. var lib = {exports: {}};
  39310. /*
  39311. object-assign
  39312. (c) Sindre Sorhus
  39313. @license MIT
  39314. */
  39315. /* eslint-disable no-unused-vars */
  39316. var getOwnPropertySymbols = Object.getOwnPropertySymbols;
  39317. var hasOwnProperty = Object.prototype.hasOwnProperty;
  39318. var propIsEnumerable = Object.prototype.propertyIsEnumerable;
  39319. function toObject(val) {
  39320. if (val === null || val === undefined) {
  39321. throw new TypeError('Object.assign cannot be called with null or undefined');
  39322. }
  39323. return Object(val);
  39324. }
  39325. function shouldUseNative() {
  39326. try {
  39327. if (!Object.assign) {
  39328. return false;
  39329. }
  39330. // Detect buggy property enumeration order in older V8 versions.
  39331. // https://bugs.chromium.org/p/v8/issues/detail?id=4118
  39332. var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
  39333. test1[5] = 'de';
  39334. if (Object.getOwnPropertyNames(test1)[0] === '5') {
  39335. return false;
  39336. }
  39337. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  39338. var test2 = {};
  39339. for (var i = 0; i < 10; i++) {
  39340. test2['_' + String.fromCharCode(i)] = i;
  39341. }
  39342. var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
  39343. return test2[n];
  39344. });
  39345. if (order2.join('') !== '0123456789') {
  39346. return false;
  39347. }
  39348. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  39349. var test3 = {};
  39350. 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
  39351. test3[letter] = letter;
  39352. });
  39353. if (Object.keys(Object.assign({}, test3)).join('') !==
  39354. 'abcdefghijklmnopqrst') {
  39355. return false;
  39356. }
  39357. return true;
  39358. } catch (err) {
  39359. // We don't expect any of the above to throw, but better to be safe.
  39360. return false;
  39361. }
  39362. }
  39363. var objectAssign = shouldUseNative() ? Object.assign : function (target, source) {
  39364. var from;
  39365. var to = toObject(target);
  39366. var symbols;
  39367. for (var s = 1; s < arguments.length; s++) {
  39368. from = Object(arguments[s]);
  39369. for (var key in from) {
  39370. if (hasOwnProperty.call(from, key)) {
  39371. to[key] = from[key];
  39372. }
  39373. }
  39374. if (getOwnPropertySymbols) {
  39375. symbols = getOwnPropertySymbols(from);
  39376. for (var i = 0; i < symbols.length; i++) {
  39377. if (propIsEnumerable.call(from, symbols[i])) {
  39378. to[symbols[i]] = from[symbols[i]];
  39379. }
  39380. }
  39381. }
  39382. }
  39383. return to;
  39384. };
  39385. var vary$1 = {exports: {}};
  39386. /*!
  39387. * vary
  39388. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  39389. * MIT Licensed
  39390. */
  39391. /**
  39392. * Module exports.
  39393. */
  39394. vary$1.exports = vary;
  39395. vary$1.exports.append = append;
  39396. /**
  39397. * RegExp to match field-name in RFC 7230 sec 3.2
  39398. *
  39399. * field-name = token
  39400. * token = 1*tchar
  39401. * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
  39402. * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
  39403. * / DIGIT / ALPHA
  39404. * ; any VCHAR, except delimiters
  39405. */
  39406. var FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
  39407. /**
  39408. * Append a field to a vary header.
  39409. *
  39410. * @param {String} header
  39411. * @param {String|Array} field
  39412. * @return {String}
  39413. * @public
  39414. */
  39415. function append (header, field) {
  39416. if (typeof header !== 'string') {
  39417. throw new TypeError('header argument is required')
  39418. }
  39419. if (!field) {
  39420. throw new TypeError('field argument is required')
  39421. }
  39422. // get fields array
  39423. var fields = !Array.isArray(field)
  39424. ? parse$7(String(field))
  39425. : field;
  39426. // assert on invalid field names
  39427. for (var j = 0; j < fields.length; j++) {
  39428. if (!FIELD_NAME_REGEXP.test(fields[j])) {
  39429. throw new TypeError('field argument contains an invalid header name')
  39430. }
  39431. }
  39432. // existing, unspecified vary
  39433. if (header === '*') {
  39434. return header
  39435. }
  39436. // enumerate current values
  39437. var val = header;
  39438. var vals = parse$7(header.toLowerCase());
  39439. // unspecified vary
  39440. if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) {
  39441. return '*'
  39442. }
  39443. for (var i = 0; i < fields.length; i++) {
  39444. var fld = fields[i].toLowerCase();
  39445. // append value (case-preserving)
  39446. if (vals.indexOf(fld) === -1) {
  39447. vals.push(fld);
  39448. val = val
  39449. ? val + ', ' + fields[i]
  39450. : fields[i];
  39451. }
  39452. }
  39453. return val
  39454. }
  39455. /**
  39456. * Parse a vary header into an array.
  39457. *
  39458. * @param {String} header
  39459. * @return {Array}
  39460. * @private
  39461. */
  39462. function parse$7 (header) {
  39463. var end = 0;
  39464. var list = [];
  39465. var start = 0;
  39466. // gather tokens
  39467. for (var i = 0, len = header.length; i < len; i++) {
  39468. switch (header.charCodeAt(i)) {
  39469. case 0x20: /* */
  39470. if (start === end) {
  39471. start = end = i + 1;
  39472. }
  39473. break
  39474. case 0x2c: /* , */
  39475. list.push(header.substring(start, end));
  39476. start = end = i + 1;
  39477. break
  39478. default:
  39479. end = i + 1;
  39480. break
  39481. }
  39482. }
  39483. // final token
  39484. list.push(header.substring(start, end));
  39485. return list
  39486. }
  39487. /**
  39488. * Mark that a request is varied on a header field.
  39489. *
  39490. * @param {Object} res
  39491. * @param {String|Array} field
  39492. * @public
  39493. */
  39494. function vary (res, field) {
  39495. if (!res || !res.getHeader || !res.setHeader) {
  39496. // quack quack
  39497. throw new TypeError('res argument is required')
  39498. }
  39499. // get existing header
  39500. var val = res.getHeader('Vary') || '';
  39501. var header = Array.isArray(val)
  39502. ? val.join(', ')
  39503. : String(val);
  39504. // set new header
  39505. if ((val = append(header, field))) {
  39506. res.setHeader('Vary', val);
  39507. }
  39508. }
  39509. var varyExports = vary$1.exports;
  39510. (function () {
  39511. var assign = objectAssign;
  39512. var vary = varyExports;
  39513. var defaults = {
  39514. origin: '*',
  39515. methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  39516. preflightContinue: false,
  39517. optionsSuccessStatus: 204
  39518. };
  39519. function isString(s) {
  39520. return typeof s === 'string' || s instanceof String;
  39521. }
  39522. function isOriginAllowed(origin, allowedOrigin) {
  39523. if (Array.isArray(allowedOrigin)) {
  39524. for (var i = 0; i < allowedOrigin.length; ++i) {
  39525. if (isOriginAllowed(origin, allowedOrigin[i])) {
  39526. return true;
  39527. }
  39528. }
  39529. return false;
  39530. } else if (isString(allowedOrigin)) {
  39531. return origin === allowedOrigin;
  39532. } else if (allowedOrigin instanceof RegExp) {
  39533. return allowedOrigin.test(origin);
  39534. } else {
  39535. return !!allowedOrigin;
  39536. }
  39537. }
  39538. function configureOrigin(options, req) {
  39539. var requestOrigin = req.headers.origin,
  39540. headers = [],
  39541. isAllowed;
  39542. if (!options.origin || options.origin === '*') {
  39543. // allow any origin
  39544. headers.push([{
  39545. key: 'Access-Control-Allow-Origin',
  39546. value: '*'
  39547. }]);
  39548. } else if (isString(options.origin)) {
  39549. // fixed origin
  39550. headers.push([{
  39551. key: 'Access-Control-Allow-Origin',
  39552. value: options.origin
  39553. }]);
  39554. headers.push([{
  39555. key: 'Vary',
  39556. value: 'Origin'
  39557. }]);
  39558. } else {
  39559. isAllowed = isOriginAllowed(requestOrigin, options.origin);
  39560. // reflect origin
  39561. headers.push([{
  39562. key: 'Access-Control-Allow-Origin',
  39563. value: isAllowed ? requestOrigin : false
  39564. }]);
  39565. headers.push([{
  39566. key: 'Vary',
  39567. value: 'Origin'
  39568. }]);
  39569. }
  39570. return headers;
  39571. }
  39572. function configureMethods(options) {
  39573. var methods = options.methods;
  39574. if (methods.join) {
  39575. methods = options.methods.join(','); // .methods is an array, so turn it into a string
  39576. }
  39577. return {
  39578. key: 'Access-Control-Allow-Methods',
  39579. value: methods
  39580. };
  39581. }
  39582. function configureCredentials(options) {
  39583. if (options.credentials === true) {
  39584. return {
  39585. key: 'Access-Control-Allow-Credentials',
  39586. value: 'true'
  39587. };
  39588. }
  39589. return null;
  39590. }
  39591. function configureAllowedHeaders(options, req) {
  39592. var allowedHeaders = options.allowedHeaders || options.headers;
  39593. var headers = [];
  39594. if (!allowedHeaders) {
  39595. allowedHeaders = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
  39596. headers.push([{
  39597. key: 'Vary',
  39598. value: 'Access-Control-Request-Headers'
  39599. }]);
  39600. } else if (allowedHeaders.join) {
  39601. allowedHeaders = allowedHeaders.join(','); // .headers is an array, so turn it into a string
  39602. }
  39603. if (allowedHeaders && allowedHeaders.length) {
  39604. headers.push([{
  39605. key: 'Access-Control-Allow-Headers',
  39606. value: allowedHeaders
  39607. }]);
  39608. }
  39609. return headers;
  39610. }
  39611. function configureExposedHeaders(options) {
  39612. var headers = options.exposedHeaders;
  39613. if (!headers) {
  39614. return null;
  39615. } else if (headers.join) {
  39616. headers = headers.join(','); // .headers is an array, so turn it into a string
  39617. }
  39618. if (headers && headers.length) {
  39619. return {
  39620. key: 'Access-Control-Expose-Headers',
  39621. value: headers
  39622. };
  39623. }
  39624. return null;
  39625. }
  39626. function configureMaxAge(options) {
  39627. var maxAge = (typeof options.maxAge === 'number' || options.maxAge) && options.maxAge.toString();
  39628. if (maxAge && maxAge.length) {
  39629. return {
  39630. key: 'Access-Control-Max-Age',
  39631. value: maxAge
  39632. };
  39633. }
  39634. return null;
  39635. }
  39636. function applyHeaders(headers, res) {
  39637. for (var i = 0, n = headers.length; i < n; i++) {
  39638. var header = headers[i];
  39639. if (header) {
  39640. if (Array.isArray(header)) {
  39641. applyHeaders(header, res);
  39642. } else if (header.key === 'Vary' && header.value) {
  39643. vary(res, header.value);
  39644. } else if (header.value) {
  39645. res.setHeader(header.key, header.value);
  39646. }
  39647. }
  39648. }
  39649. }
  39650. function cors(options, req, res, next) {
  39651. var headers = [],
  39652. method = req.method && req.method.toUpperCase && req.method.toUpperCase();
  39653. if (method === 'OPTIONS') {
  39654. // preflight
  39655. headers.push(configureOrigin(options, req));
  39656. headers.push(configureCredentials(options));
  39657. headers.push(configureMethods(options));
  39658. headers.push(configureAllowedHeaders(options, req));
  39659. headers.push(configureMaxAge(options));
  39660. headers.push(configureExposedHeaders(options));
  39661. applyHeaders(headers, res);
  39662. if (options.preflightContinue) {
  39663. next();
  39664. } else {
  39665. // Safari (and potentially other browsers) need content-length 0,
  39666. // for 204 or they just hang waiting for a body
  39667. res.statusCode = options.optionsSuccessStatus;
  39668. res.setHeader('Content-Length', '0');
  39669. res.end();
  39670. }
  39671. } else {
  39672. // actual response
  39673. headers.push(configureOrigin(options, req));
  39674. headers.push(configureCredentials(options));
  39675. headers.push(configureExposedHeaders(options));
  39676. applyHeaders(headers, res);
  39677. next();
  39678. }
  39679. }
  39680. function middlewareWrapper(o) {
  39681. // if options are static (either via defaults or custom options passed in), wrap in a function
  39682. var optionsCallback = null;
  39683. if (typeof o === 'function') {
  39684. optionsCallback = o;
  39685. } else {
  39686. optionsCallback = function (req, cb) {
  39687. cb(null, o);
  39688. };
  39689. }
  39690. return function corsMiddleware(req, res, next) {
  39691. optionsCallback(req, function (err, options) {
  39692. if (err) {
  39693. next(err);
  39694. } else {
  39695. var corsOptions = assign({}, defaults, options);
  39696. var originCallback = null;
  39697. if (corsOptions.origin && typeof corsOptions.origin === 'function') {
  39698. originCallback = corsOptions.origin;
  39699. } else if (corsOptions.origin) {
  39700. originCallback = function (origin, cb) {
  39701. cb(null, corsOptions.origin);
  39702. };
  39703. }
  39704. if (originCallback) {
  39705. originCallback(req.headers.origin, function (err2, origin) {
  39706. if (err2 || !origin) {
  39707. next(err2);
  39708. } else {
  39709. corsOptions.origin = origin;
  39710. cors(corsOptions, req, res, next);
  39711. }
  39712. });
  39713. } else {
  39714. next();
  39715. }
  39716. }
  39717. });
  39718. };
  39719. }
  39720. // can pass either an options hash, an options delegate, or nothing
  39721. lib.exports = middlewareWrapper;
  39722. }());
  39723. var libExports = lib.exports;
  39724. var corsMiddleware = /*@__PURE__*/getDefaultExportFromCjs(libExports);
  39725. var chokidar = {};
  39726. const fs$8 = require$$0__default;
  39727. const { Readable } = require$$0$7;
  39728. const sysPath$3 = require$$0$4;
  39729. const { promisify: promisify$3 } = require$$0$6;
  39730. const picomatch$1 = picomatch$3;
  39731. const readdir$1 = promisify$3(fs$8.readdir);
  39732. const stat$3 = promisify$3(fs$8.stat);
  39733. const lstat$2 = promisify$3(fs$8.lstat);
  39734. const realpath$1 = promisify$3(fs$8.realpath);
  39735. /**
  39736. * @typedef {Object} EntryInfo
  39737. * @property {String} path
  39738. * @property {String} fullPath
  39739. * @property {fs.Stats=} stats
  39740. * @property {fs.Dirent=} dirent
  39741. * @property {String} basename
  39742. */
  39743. const BANG$2 = '!';
  39744. const RECURSIVE_ERROR_CODE = 'READDIRP_RECURSIVE_ERROR';
  39745. const NORMAL_FLOW_ERRORS = new Set(['ENOENT', 'EPERM', 'EACCES', 'ELOOP', RECURSIVE_ERROR_CODE]);
  39746. const FILE_TYPE = 'files';
  39747. const DIR_TYPE = 'directories';
  39748. const FILE_DIR_TYPE = 'files_directories';
  39749. const EVERYTHING_TYPE = 'all';
  39750. const ALL_TYPES = [FILE_TYPE, DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE];
  39751. const isNormalFlowError = error => NORMAL_FLOW_ERRORS.has(error.code);
  39752. const [maj, min] = process.versions.node.split('.').slice(0, 2).map(n => Number.parseInt(n, 10));
  39753. const wantBigintFsStats = process.platform === 'win32' && (maj > 10 || (maj === 10 && min >= 5));
  39754. const normalizeFilter = filter => {
  39755. if (filter === undefined) return;
  39756. if (typeof filter === 'function') return filter;
  39757. if (typeof filter === 'string') {
  39758. const glob = picomatch$1(filter.trim());
  39759. return entry => glob(entry.basename);
  39760. }
  39761. if (Array.isArray(filter)) {
  39762. const positive = [];
  39763. const negative = [];
  39764. for (const item of filter) {
  39765. const trimmed = item.trim();
  39766. if (trimmed.charAt(0) === BANG$2) {
  39767. negative.push(picomatch$1(trimmed.slice(1)));
  39768. } else {
  39769. positive.push(picomatch$1(trimmed));
  39770. }
  39771. }
  39772. if (negative.length > 0) {
  39773. if (positive.length > 0) {
  39774. return entry =>
  39775. positive.some(f => f(entry.basename)) && !negative.some(f => f(entry.basename));
  39776. }
  39777. return entry => !negative.some(f => f(entry.basename));
  39778. }
  39779. return entry => positive.some(f => f(entry.basename));
  39780. }
  39781. };
  39782. class ReaddirpStream extends Readable {
  39783. static get defaultOptions() {
  39784. return {
  39785. root: '.',
  39786. /* eslint-disable no-unused-vars */
  39787. fileFilter: (path) => true,
  39788. directoryFilter: (path) => true,
  39789. /* eslint-enable no-unused-vars */
  39790. type: FILE_TYPE,
  39791. lstat: false,
  39792. depth: 2147483648,
  39793. alwaysStat: false
  39794. };
  39795. }
  39796. constructor(options = {}) {
  39797. super({
  39798. objectMode: true,
  39799. autoDestroy: true,
  39800. highWaterMark: options.highWaterMark || 4096
  39801. });
  39802. const opts = { ...ReaddirpStream.defaultOptions, ...options };
  39803. const { root, type } = opts;
  39804. this._fileFilter = normalizeFilter(opts.fileFilter);
  39805. this._directoryFilter = normalizeFilter(opts.directoryFilter);
  39806. const statMethod = opts.lstat ? lstat$2 : stat$3;
  39807. // Use bigint stats if it's windows and stat() supports options (node 10+).
  39808. if (wantBigintFsStats) {
  39809. this._stat = path => statMethod(path, { bigint: true });
  39810. } else {
  39811. this._stat = statMethod;
  39812. }
  39813. this._maxDepth = opts.depth;
  39814. this._wantsDir = [DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39815. this._wantsFile = [FILE_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39816. this._wantsEverything = type === EVERYTHING_TYPE;
  39817. this._root = sysPath$3.resolve(root);
  39818. this._isDirent = ('Dirent' in fs$8) && !opts.alwaysStat;
  39819. this._statsProp = this._isDirent ? 'dirent' : 'stats';
  39820. this._rdOptions = { encoding: 'utf8', withFileTypes: this._isDirent };
  39821. // Launch stream with one parent, the root dir.
  39822. this.parents = [this._exploreDir(root, 1)];
  39823. this.reading = false;
  39824. this.parent = undefined;
  39825. }
  39826. async _read(batch) {
  39827. if (this.reading) return;
  39828. this.reading = true;
  39829. try {
  39830. while (!this.destroyed && batch > 0) {
  39831. const { path, depth, files = [] } = this.parent || {};
  39832. if (files.length > 0) {
  39833. const slice = files.splice(0, batch).map(dirent => this._formatEntry(dirent, path));
  39834. for (const entry of await Promise.all(slice)) {
  39835. if (this.destroyed) return;
  39836. const entryType = await this._getEntryType(entry);
  39837. if (entryType === 'directory' && this._directoryFilter(entry)) {
  39838. if (depth <= this._maxDepth) {
  39839. this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
  39840. }
  39841. if (this._wantsDir) {
  39842. this.push(entry);
  39843. batch--;
  39844. }
  39845. } else if ((entryType === 'file' || this._includeAsFile(entry)) && this._fileFilter(entry)) {
  39846. if (this._wantsFile) {
  39847. this.push(entry);
  39848. batch--;
  39849. }
  39850. }
  39851. }
  39852. } else {
  39853. const parent = this.parents.pop();
  39854. if (!parent) {
  39855. this.push(null);
  39856. break;
  39857. }
  39858. this.parent = await parent;
  39859. if (this.destroyed) return;
  39860. }
  39861. }
  39862. } catch (error) {
  39863. this.destroy(error);
  39864. } finally {
  39865. this.reading = false;
  39866. }
  39867. }
  39868. async _exploreDir(path, depth) {
  39869. let files;
  39870. try {
  39871. files = await readdir$1(path, this._rdOptions);
  39872. } catch (error) {
  39873. this._onError(error);
  39874. }
  39875. return { files, depth, path };
  39876. }
  39877. async _formatEntry(dirent, path) {
  39878. let entry;
  39879. try {
  39880. const basename = this._isDirent ? dirent.name : dirent;
  39881. const fullPath = sysPath$3.resolve(sysPath$3.join(path, basename));
  39882. entry = { path: sysPath$3.relative(this._root, fullPath), fullPath, basename };
  39883. entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
  39884. } catch (err) {
  39885. this._onError(err);
  39886. }
  39887. return entry;
  39888. }
  39889. _onError(err) {
  39890. if (isNormalFlowError(err) && !this.destroyed) {
  39891. this.emit('warn', err);
  39892. } else {
  39893. this.destroy(err);
  39894. }
  39895. }
  39896. async _getEntryType(entry) {
  39897. // entry may be undefined, because a warning or an error were emitted
  39898. // and the statsProp is undefined
  39899. const stats = entry && entry[this._statsProp];
  39900. if (!stats) {
  39901. return;
  39902. }
  39903. if (stats.isFile()) {
  39904. return 'file';
  39905. }
  39906. if (stats.isDirectory()) {
  39907. return 'directory';
  39908. }
  39909. if (stats && stats.isSymbolicLink()) {
  39910. const full = entry.fullPath;
  39911. try {
  39912. const entryRealPath = await realpath$1(full);
  39913. const entryRealPathStats = await lstat$2(entryRealPath);
  39914. if (entryRealPathStats.isFile()) {
  39915. return 'file';
  39916. }
  39917. if (entryRealPathStats.isDirectory()) {
  39918. const len = entryRealPath.length;
  39919. if (full.startsWith(entryRealPath) && full.substr(len, 1) === sysPath$3.sep) {
  39920. const recursiveError = new Error(
  39921. `Circular symlink detected: "${full}" points to "${entryRealPath}"`
  39922. );
  39923. recursiveError.code = RECURSIVE_ERROR_CODE;
  39924. return this._onError(recursiveError);
  39925. }
  39926. return 'directory';
  39927. }
  39928. } catch (error) {
  39929. this._onError(error);
  39930. }
  39931. }
  39932. }
  39933. _includeAsFile(entry) {
  39934. const stats = entry && entry[this._statsProp];
  39935. return stats && this._wantsEverything && !stats.isDirectory();
  39936. }
  39937. }
  39938. /**
  39939. * @typedef {Object} ReaddirpArguments
  39940. * @property {Function=} fileFilter
  39941. * @property {Function=} directoryFilter
  39942. * @property {String=} type
  39943. * @property {Number=} depth
  39944. * @property {String=} root
  39945. * @property {Boolean=} lstat
  39946. * @property {Boolean=} bigint
  39947. */
  39948. /**
  39949. * Main function which ends up calling readdirRec and reads all files and directories in given root recursively.
  39950. * @param {String} root Root directory
  39951. * @param {ReaddirpArguments=} options Options to specify root (start directory), filters and recursion depth
  39952. */
  39953. const readdirp$1 = (root, options = {}) => {
  39954. let type = options.entryType || options.type;
  39955. if (type === 'both') type = FILE_DIR_TYPE; // backwards-compatibility
  39956. if (type) options.type = type;
  39957. if (!root) {
  39958. throw new Error('readdirp: root argument is required. Usage: readdirp(root, options)');
  39959. } else if (typeof root !== 'string') {
  39960. throw new TypeError('readdirp: root argument must be a string. Usage: readdirp(root, options)');
  39961. } else if (type && !ALL_TYPES.includes(type)) {
  39962. throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(', ')}`);
  39963. }
  39964. options.root = root;
  39965. return new ReaddirpStream(options);
  39966. };
  39967. const readdirpPromise = (root, options = {}) => {
  39968. return new Promise((resolve, reject) => {
  39969. const files = [];
  39970. readdirp$1(root, options)
  39971. .on('data', entry => files.push(entry))
  39972. .on('end', () => resolve(files))
  39973. .on('error', error => reject(error));
  39974. });
  39975. };
  39976. readdirp$1.promise = readdirpPromise;
  39977. readdirp$1.ReaddirpStream = ReaddirpStream;
  39978. readdirp$1.default = readdirp$1;
  39979. var readdirp_1 = readdirp$1;
  39980. var anymatch$2 = {exports: {}};
  39981. /*!
  39982. * normalize-path <https://github.com/jonschlinkert/normalize-path>
  39983. *
  39984. * Copyright (c) 2014-2018, Jon Schlinkert.
  39985. * Released under the MIT License.
  39986. */
  39987. var normalizePath$2 = function(path, stripTrailing) {
  39988. if (typeof path !== 'string') {
  39989. throw new TypeError('expected path to be a string');
  39990. }
  39991. if (path === '\\' || path === '/') return '/';
  39992. var len = path.length;
  39993. if (len <= 1) return path;
  39994. // ensure that win32 namespaces has two leading slashes, so that the path is
  39995. // handled properly by the win32 version of path.parse() after being normalized
  39996. // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
  39997. var prefix = '';
  39998. if (len > 4 && path[3] === '\\') {
  39999. var ch = path[2];
  40000. if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
  40001. path = path.slice(2);
  40002. prefix = '//';
  40003. }
  40004. }
  40005. var segs = path.split(/[/\\]+/);
  40006. if (stripTrailing !== false && segs[segs.length - 1] === '') {
  40007. segs.pop();
  40008. }
  40009. return prefix + segs.join('/');
  40010. };
  40011. var anymatch_1 = anymatch$2.exports;
  40012. Object.defineProperty(anymatch_1, "__esModule", { value: true });
  40013. const picomatch = picomatch$3;
  40014. const normalizePath$1 = normalizePath$2;
  40015. /**
  40016. * @typedef {(testString: string) => boolean} AnymatchFn
  40017. * @typedef {string|RegExp|AnymatchFn} AnymatchPattern
  40018. * @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
  40019. */
  40020. const BANG$1 = '!';
  40021. const DEFAULT_OPTIONS = {returnIndex: false};
  40022. const arrify$1 = (item) => Array.isArray(item) ? item : [item];
  40023. /**
  40024. * @param {AnymatchPattern} matcher
  40025. * @param {object} options
  40026. * @returns {AnymatchFn}
  40027. */
  40028. const createPattern = (matcher, options) => {
  40029. if (typeof matcher === 'function') {
  40030. return matcher;
  40031. }
  40032. if (typeof matcher === 'string') {
  40033. const glob = picomatch(matcher, options);
  40034. return (string) => matcher === string || glob(string);
  40035. }
  40036. if (matcher instanceof RegExp) {
  40037. return (string) => matcher.test(string);
  40038. }
  40039. return (string) => false;
  40040. };
  40041. /**
  40042. * @param {Array<Function>} patterns
  40043. * @param {Array<Function>} negPatterns
  40044. * @param {String|Array} args
  40045. * @param {Boolean} returnIndex
  40046. * @returns {boolean|number}
  40047. */
  40048. const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
  40049. const isList = Array.isArray(args);
  40050. const _path = isList ? args[0] : args;
  40051. if (!isList && typeof _path !== 'string') {
  40052. throw new TypeError('anymatch: second argument must be a string: got ' +
  40053. Object.prototype.toString.call(_path))
  40054. }
  40055. const path = normalizePath$1(_path);
  40056. for (let index = 0; index < negPatterns.length; index++) {
  40057. const nglob = negPatterns[index];
  40058. if (nglob(path)) {
  40059. return returnIndex ? -1 : false;
  40060. }
  40061. }
  40062. const applied = isList && [path].concat(args.slice(1));
  40063. for (let index = 0; index < patterns.length; index++) {
  40064. const pattern = patterns[index];
  40065. if (isList ? pattern(...applied) : pattern(path)) {
  40066. return returnIndex ? index : true;
  40067. }
  40068. }
  40069. return returnIndex ? -1 : false;
  40070. };
  40071. /**
  40072. * @param {AnymatchMatcher} matchers
  40073. * @param {Array|string} testString
  40074. * @param {object} options
  40075. * @returns {boolean|number|Function}
  40076. */
  40077. const anymatch$1 = (matchers, testString, options = DEFAULT_OPTIONS) => {
  40078. if (matchers == null) {
  40079. throw new TypeError('anymatch: specify first argument');
  40080. }
  40081. const opts = typeof options === 'boolean' ? {returnIndex: options} : options;
  40082. const returnIndex = opts.returnIndex || false;
  40083. // Early cache for matchers.
  40084. const mtchers = arrify$1(matchers);
  40085. const negatedGlobs = mtchers
  40086. .filter(item => typeof item === 'string' && item.charAt(0) === BANG$1)
  40087. .map(item => item.slice(1))
  40088. .map(item => picomatch(item, opts));
  40089. const patterns = mtchers
  40090. .filter(item => typeof item !== 'string' || (typeof item === 'string' && item.charAt(0) !== BANG$1))
  40091. .map(matcher => createPattern(matcher, opts));
  40092. if (testString == null) {
  40093. return (testString, ri = false) => {
  40094. const returnIndex = typeof ri === 'boolean' ? ri : false;
  40095. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  40096. }
  40097. }
  40098. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  40099. };
  40100. anymatch$1.default = anymatch$1;
  40101. anymatch$2.exports = anymatch$1;
  40102. var anymatchExports = anymatch$2.exports;
  40103. var require$$0 = [
  40104. "3dm",
  40105. "3ds",
  40106. "3g2",
  40107. "3gp",
  40108. "7z",
  40109. "a",
  40110. "aac",
  40111. "adp",
  40112. "ai",
  40113. "aif",
  40114. "aiff",
  40115. "alz",
  40116. "ape",
  40117. "apk",
  40118. "appimage",
  40119. "ar",
  40120. "arj",
  40121. "asf",
  40122. "au",
  40123. "avi",
  40124. "bak",
  40125. "baml",
  40126. "bh",
  40127. "bin",
  40128. "bk",
  40129. "bmp",
  40130. "btif",
  40131. "bz2",
  40132. "bzip2",
  40133. "cab",
  40134. "caf",
  40135. "cgm",
  40136. "class",
  40137. "cmx",
  40138. "cpio",
  40139. "cr2",
  40140. "cur",
  40141. "dat",
  40142. "dcm",
  40143. "deb",
  40144. "dex",
  40145. "djvu",
  40146. "dll",
  40147. "dmg",
  40148. "dng",
  40149. "doc",
  40150. "docm",
  40151. "docx",
  40152. "dot",
  40153. "dotm",
  40154. "dra",
  40155. "DS_Store",
  40156. "dsk",
  40157. "dts",
  40158. "dtshd",
  40159. "dvb",
  40160. "dwg",
  40161. "dxf",
  40162. "ecelp4800",
  40163. "ecelp7470",
  40164. "ecelp9600",
  40165. "egg",
  40166. "eol",
  40167. "eot",
  40168. "epub",
  40169. "exe",
  40170. "f4v",
  40171. "fbs",
  40172. "fh",
  40173. "fla",
  40174. "flac",
  40175. "flatpak",
  40176. "fli",
  40177. "flv",
  40178. "fpx",
  40179. "fst",
  40180. "fvt",
  40181. "g3",
  40182. "gh",
  40183. "gif",
  40184. "graffle",
  40185. "gz",
  40186. "gzip",
  40187. "h261",
  40188. "h263",
  40189. "h264",
  40190. "icns",
  40191. "ico",
  40192. "ief",
  40193. "img",
  40194. "ipa",
  40195. "iso",
  40196. "jar",
  40197. "jpeg",
  40198. "jpg",
  40199. "jpgv",
  40200. "jpm",
  40201. "jxr",
  40202. "key",
  40203. "ktx",
  40204. "lha",
  40205. "lib",
  40206. "lvp",
  40207. "lz",
  40208. "lzh",
  40209. "lzma",
  40210. "lzo",
  40211. "m3u",
  40212. "m4a",
  40213. "m4v",
  40214. "mar",
  40215. "mdi",
  40216. "mht",
  40217. "mid",
  40218. "midi",
  40219. "mj2",
  40220. "mka",
  40221. "mkv",
  40222. "mmr",
  40223. "mng",
  40224. "mobi",
  40225. "mov",
  40226. "movie",
  40227. "mp3",
  40228. "mp4",
  40229. "mp4a",
  40230. "mpeg",
  40231. "mpg",
  40232. "mpga",
  40233. "mxu",
  40234. "nef",
  40235. "npx",
  40236. "numbers",
  40237. "nupkg",
  40238. "o",
  40239. "odp",
  40240. "ods",
  40241. "odt",
  40242. "oga",
  40243. "ogg",
  40244. "ogv",
  40245. "otf",
  40246. "ott",
  40247. "pages",
  40248. "pbm",
  40249. "pcx",
  40250. "pdb",
  40251. "pdf",
  40252. "pea",
  40253. "pgm",
  40254. "pic",
  40255. "png",
  40256. "pnm",
  40257. "pot",
  40258. "potm",
  40259. "potx",
  40260. "ppa",
  40261. "ppam",
  40262. "ppm",
  40263. "pps",
  40264. "ppsm",
  40265. "ppsx",
  40266. "ppt",
  40267. "pptm",
  40268. "pptx",
  40269. "psd",
  40270. "pya",
  40271. "pyc",
  40272. "pyo",
  40273. "pyv",
  40274. "qt",
  40275. "rar",
  40276. "ras",
  40277. "raw",
  40278. "resources",
  40279. "rgb",
  40280. "rip",
  40281. "rlc",
  40282. "rmf",
  40283. "rmvb",
  40284. "rpm",
  40285. "rtf",
  40286. "rz",
  40287. "s3m",
  40288. "s7z",
  40289. "scpt",
  40290. "sgi",
  40291. "shar",
  40292. "snap",
  40293. "sil",
  40294. "sketch",
  40295. "slk",
  40296. "smv",
  40297. "snk",
  40298. "so",
  40299. "stl",
  40300. "suo",
  40301. "sub",
  40302. "swf",
  40303. "tar",
  40304. "tbz",
  40305. "tbz2",
  40306. "tga",
  40307. "tgz",
  40308. "thmx",
  40309. "tif",
  40310. "tiff",
  40311. "tlz",
  40312. "ttc",
  40313. "ttf",
  40314. "txz",
  40315. "udf",
  40316. "uvh",
  40317. "uvi",
  40318. "uvm",
  40319. "uvp",
  40320. "uvs",
  40321. "uvu",
  40322. "viv",
  40323. "vob",
  40324. "war",
  40325. "wav",
  40326. "wax",
  40327. "wbmp",
  40328. "wdp",
  40329. "weba",
  40330. "webm",
  40331. "webp",
  40332. "whl",
  40333. "wim",
  40334. "wm",
  40335. "wma",
  40336. "wmv",
  40337. "wmx",
  40338. "woff",
  40339. "woff2",
  40340. "wrm",
  40341. "wvx",
  40342. "xbm",
  40343. "xif",
  40344. "xla",
  40345. "xlam",
  40346. "xls",
  40347. "xlsb",
  40348. "xlsm",
  40349. "xlsx",
  40350. "xlt",
  40351. "xltm",
  40352. "xltx",
  40353. "xm",
  40354. "xmind",
  40355. "xpi",
  40356. "xpm",
  40357. "xwd",
  40358. "xz",
  40359. "z",
  40360. "zip",
  40361. "zipx"
  40362. ];
  40363. var binaryExtensions$1 = require$$0;
  40364. const path$8 = require$$0$4;
  40365. const binaryExtensions = binaryExtensions$1;
  40366. const extensions = new Set(binaryExtensions);
  40367. var isBinaryPath$1 = filePath => extensions.has(path$8.extname(filePath).slice(1).toLowerCase());
  40368. var constants$1 = {};
  40369. (function (exports) {
  40370. const {sep} = require$$0$4;
  40371. const {platform} = process;
  40372. const os = require$$2;
  40373. exports.EV_ALL = 'all';
  40374. exports.EV_READY = 'ready';
  40375. exports.EV_ADD = 'add';
  40376. exports.EV_CHANGE = 'change';
  40377. exports.EV_ADD_DIR = 'addDir';
  40378. exports.EV_UNLINK = 'unlink';
  40379. exports.EV_UNLINK_DIR = 'unlinkDir';
  40380. exports.EV_RAW = 'raw';
  40381. exports.EV_ERROR = 'error';
  40382. exports.STR_DATA = 'data';
  40383. exports.STR_END = 'end';
  40384. exports.STR_CLOSE = 'close';
  40385. exports.FSEVENT_CREATED = 'created';
  40386. exports.FSEVENT_MODIFIED = 'modified';
  40387. exports.FSEVENT_DELETED = 'deleted';
  40388. exports.FSEVENT_MOVED = 'moved';
  40389. exports.FSEVENT_CLONED = 'cloned';
  40390. exports.FSEVENT_UNKNOWN = 'unknown';
  40391. exports.FSEVENT_FLAG_MUST_SCAN_SUBDIRS = 1;
  40392. exports.FSEVENT_TYPE_FILE = 'file';
  40393. exports.FSEVENT_TYPE_DIRECTORY = 'directory';
  40394. exports.FSEVENT_TYPE_SYMLINK = 'symlink';
  40395. exports.KEY_LISTENERS = 'listeners';
  40396. exports.KEY_ERR = 'errHandlers';
  40397. exports.KEY_RAW = 'rawEmitters';
  40398. exports.HANDLER_KEYS = [exports.KEY_LISTENERS, exports.KEY_ERR, exports.KEY_RAW];
  40399. exports.DOT_SLASH = `.${sep}`;
  40400. exports.BACK_SLASH_RE = /\\/g;
  40401. exports.DOUBLE_SLASH_RE = /\/\//;
  40402. exports.SLASH_OR_BACK_SLASH_RE = /[/\\]/;
  40403. exports.DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
  40404. exports.REPLACER_RE = /^\.[/\\]/;
  40405. exports.SLASH = '/';
  40406. exports.SLASH_SLASH = '//';
  40407. exports.BRACE_START = '{';
  40408. exports.BANG = '!';
  40409. exports.ONE_DOT = '.';
  40410. exports.TWO_DOTS = '..';
  40411. exports.STAR = '*';
  40412. exports.GLOBSTAR = '**';
  40413. exports.ROOT_GLOBSTAR = '/**/*';
  40414. exports.SLASH_GLOBSTAR = '/**';
  40415. exports.DIR_SUFFIX = 'Dir';
  40416. exports.ANYMATCH_OPTS = {dot: true};
  40417. exports.STRING_TYPE = 'string';
  40418. exports.FUNCTION_TYPE = 'function';
  40419. exports.EMPTY_STR = '';
  40420. exports.EMPTY_FN = () => {};
  40421. exports.IDENTITY_FN = val => val;
  40422. exports.isWindows = platform === 'win32';
  40423. exports.isMacos = platform === 'darwin';
  40424. exports.isLinux = platform === 'linux';
  40425. exports.isIBMi = os.type() === 'OS400';
  40426. } (constants$1));
  40427. const fs$7 = require$$0__default;
  40428. const sysPath$2 = require$$0$4;
  40429. const { promisify: promisify$2 } = require$$0$6;
  40430. const isBinaryPath = isBinaryPath$1;
  40431. const {
  40432. isWindows: isWindows$2,
  40433. isLinux,
  40434. EMPTY_FN: EMPTY_FN$2,
  40435. EMPTY_STR: EMPTY_STR$1,
  40436. KEY_LISTENERS,
  40437. KEY_ERR,
  40438. KEY_RAW,
  40439. HANDLER_KEYS,
  40440. EV_CHANGE: EV_CHANGE$2,
  40441. EV_ADD: EV_ADD$2,
  40442. EV_ADD_DIR: EV_ADD_DIR$2,
  40443. EV_ERROR: EV_ERROR$2,
  40444. STR_DATA: STR_DATA$1,
  40445. STR_END: STR_END$2,
  40446. BRACE_START: BRACE_START$1,
  40447. STAR
  40448. } = constants$1;
  40449. const THROTTLE_MODE_WATCH = 'watch';
  40450. const open$2 = promisify$2(fs$7.open);
  40451. const stat$2 = promisify$2(fs$7.stat);
  40452. const lstat$1 = promisify$2(fs$7.lstat);
  40453. const close = promisify$2(fs$7.close);
  40454. const fsrealpath = promisify$2(fs$7.realpath);
  40455. const statMethods$1 = { lstat: lstat$1, stat: stat$2 };
  40456. // TODO: emit errors properly. Example: EMFILE on Macos.
  40457. const foreach = (val, fn) => {
  40458. if (val instanceof Set) {
  40459. val.forEach(fn);
  40460. } else {
  40461. fn(val);
  40462. }
  40463. };
  40464. const addAndConvert = (main, prop, item) => {
  40465. let container = main[prop];
  40466. if (!(container instanceof Set)) {
  40467. main[prop] = container = new Set([container]);
  40468. }
  40469. container.add(item);
  40470. };
  40471. const clearItem = cont => key => {
  40472. const set = cont[key];
  40473. if (set instanceof Set) {
  40474. set.clear();
  40475. } else {
  40476. delete cont[key];
  40477. }
  40478. };
  40479. const delFromSet = (main, prop, item) => {
  40480. const container = main[prop];
  40481. if (container instanceof Set) {
  40482. container.delete(item);
  40483. } else if (container === item) {
  40484. delete main[prop];
  40485. }
  40486. };
  40487. const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
  40488. /**
  40489. * @typedef {String} Path
  40490. */
  40491. // fs_watch helpers
  40492. // object to hold per-process fs_watch instances
  40493. // (may be shared across chokidar FSWatcher instances)
  40494. /**
  40495. * @typedef {Object} FsWatchContainer
  40496. * @property {Set} listeners
  40497. * @property {Set} errHandlers
  40498. * @property {Set} rawEmitters
  40499. * @property {fs.FSWatcher=} watcher
  40500. * @property {Boolean=} watcherUnusable
  40501. */
  40502. /**
  40503. * @type {Map<String,FsWatchContainer>}
  40504. */
  40505. const FsWatchInstances = new Map();
  40506. /**
  40507. * Instantiates the fs_watch interface
  40508. * @param {String} path to be watched
  40509. * @param {Object} options to be passed to fs_watch
  40510. * @param {Function} listener main event handler
  40511. * @param {Function} errHandler emits info about errors
  40512. * @param {Function} emitRaw emits raw event data
  40513. * @returns {fs.FSWatcher} new fsevents instance
  40514. */
  40515. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  40516. const handleEvent = (rawEvent, evPath) => {
  40517. listener(path);
  40518. emitRaw(rawEvent, evPath, {watchedPath: path});
  40519. // emit based on events occurring for files from a directory's watcher in
  40520. // case the file's watcher misses it (and rely on throttling to de-dupe)
  40521. if (evPath && path !== evPath) {
  40522. fsWatchBroadcast(
  40523. sysPath$2.resolve(path, evPath), KEY_LISTENERS, sysPath$2.join(path, evPath)
  40524. );
  40525. }
  40526. };
  40527. try {
  40528. return fs$7.watch(path, options, handleEvent);
  40529. } catch (error) {
  40530. errHandler(error);
  40531. }
  40532. }
  40533. /**
  40534. * Helper for passing fs_watch event data to a collection of listeners
  40535. * @param {Path} fullPath absolute path bound to fs_watch instance
  40536. * @param {String} type listener type
  40537. * @param {*=} val1 arguments to be passed to listeners
  40538. * @param {*=} val2
  40539. * @param {*=} val3
  40540. */
  40541. const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
  40542. const cont = FsWatchInstances.get(fullPath);
  40543. if (!cont) return;
  40544. foreach(cont[type], (listener) => {
  40545. listener(val1, val2, val3);
  40546. });
  40547. };
  40548. /**
  40549. * Instantiates the fs_watch interface or binds listeners
  40550. * to an existing one covering the same file system entry
  40551. * @param {String} path
  40552. * @param {String} fullPath absolute path
  40553. * @param {Object} options to be passed to fs_watch
  40554. * @param {Object} handlers container for event listener functions
  40555. */
  40556. const setFsWatchListener = (path, fullPath, options, handlers) => {
  40557. const {listener, errHandler, rawEmitter} = handlers;
  40558. let cont = FsWatchInstances.get(fullPath);
  40559. /** @type {fs.FSWatcher=} */
  40560. let watcher;
  40561. if (!options.persistent) {
  40562. watcher = createFsWatchInstance(
  40563. path, options, listener, errHandler, rawEmitter
  40564. );
  40565. return watcher.close.bind(watcher);
  40566. }
  40567. if (cont) {
  40568. addAndConvert(cont, KEY_LISTENERS, listener);
  40569. addAndConvert(cont, KEY_ERR, errHandler);
  40570. addAndConvert(cont, KEY_RAW, rawEmitter);
  40571. } else {
  40572. watcher = createFsWatchInstance(
  40573. path,
  40574. options,
  40575. fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
  40576. errHandler, // no need to use broadcast here
  40577. fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
  40578. );
  40579. if (!watcher) return;
  40580. watcher.on(EV_ERROR$2, async (error) => {
  40581. const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
  40582. cont.watcherUnusable = true; // documented since Node 10.4.1
  40583. // Workaround for https://github.com/joyent/node/issues/4337
  40584. if (isWindows$2 && error.code === 'EPERM') {
  40585. try {
  40586. const fd = await open$2(path, 'r');
  40587. await close(fd);
  40588. broadcastErr(error);
  40589. } catch (err) {}
  40590. } else {
  40591. broadcastErr(error);
  40592. }
  40593. });
  40594. cont = {
  40595. listeners: listener,
  40596. errHandlers: errHandler,
  40597. rawEmitters: rawEmitter,
  40598. watcher
  40599. };
  40600. FsWatchInstances.set(fullPath, cont);
  40601. }
  40602. // const index = cont.listeners.indexOf(listener);
  40603. // removes this instance's listeners and closes the underlying fs_watch
  40604. // instance if there are no more listeners left
  40605. return () => {
  40606. delFromSet(cont, KEY_LISTENERS, listener);
  40607. delFromSet(cont, KEY_ERR, errHandler);
  40608. delFromSet(cont, KEY_RAW, rawEmitter);
  40609. if (isEmptySet(cont.listeners)) {
  40610. // Check to protect against issue gh-730.
  40611. // if (cont.watcherUnusable) {
  40612. cont.watcher.close();
  40613. // }
  40614. FsWatchInstances.delete(fullPath);
  40615. HANDLER_KEYS.forEach(clearItem(cont));
  40616. cont.watcher = undefined;
  40617. Object.freeze(cont);
  40618. }
  40619. };
  40620. };
  40621. // fs_watchFile helpers
  40622. // object to hold per-process fs_watchFile instances
  40623. // (may be shared across chokidar FSWatcher instances)
  40624. const FsWatchFileInstances = new Map();
  40625. /**
  40626. * Instantiates the fs_watchFile interface or binds listeners
  40627. * to an existing one covering the same file system entry
  40628. * @param {String} path to be watched
  40629. * @param {String} fullPath absolute path
  40630. * @param {Object} options options to be passed to fs_watchFile
  40631. * @param {Object} handlers container for event listener functions
  40632. * @returns {Function} closer
  40633. */
  40634. const setFsWatchFileListener = (path, fullPath, options, handlers) => {
  40635. const {listener, rawEmitter} = handlers;
  40636. let cont = FsWatchFileInstances.get(fullPath);
  40637. const copts = cont && cont.options;
  40638. if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
  40639. fs$7.unwatchFile(fullPath);
  40640. cont = undefined;
  40641. }
  40642. /* eslint-enable no-unused-vars, prefer-destructuring */
  40643. if (cont) {
  40644. addAndConvert(cont, KEY_LISTENERS, listener);
  40645. addAndConvert(cont, KEY_RAW, rawEmitter);
  40646. } else {
  40647. // TODO
  40648. // listeners.add(listener);
  40649. // rawEmitters.add(rawEmitter);
  40650. cont = {
  40651. listeners: listener,
  40652. rawEmitters: rawEmitter,
  40653. options,
  40654. watcher: fs$7.watchFile(fullPath, options, (curr, prev) => {
  40655. foreach(cont.rawEmitters, (rawEmitter) => {
  40656. rawEmitter(EV_CHANGE$2, fullPath, {curr, prev});
  40657. });
  40658. const currmtime = curr.mtimeMs;
  40659. if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
  40660. foreach(cont.listeners, (listener) => listener(path, curr));
  40661. }
  40662. })
  40663. };
  40664. FsWatchFileInstances.set(fullPath, cont);
  40665. }
  40666. // const index = cont.listeners.indexOf(listener);
  40667. // Removes this instance's listeners and closes the underlying fs_watchFile
  40668. // instance if there are no more listeners left.
  40669. return () => {
  40670. delFromSet(cont, KEY_LISTENERS, listener);
  40671. delFromSet(cont, KEY_RAW, rawEmitter);
  40672. if (isEmptySet(cont.listeners)) {
  40673. FsWatchFileInstances.delete(fullPath);
  40674. fs$7.unwatchFile(fullPath);
  40675. cont.options = cont.watcher = undefined;
  40676. Object.freeze(cont);
  40677. }
  40678. };
  40679. };
  40680. /**
  40681. * @mixin
  40682. */
  40683. let NodeFsHandler$1 = class NodeFsHandler {
  40684. /**
  40685. * @param {import("../index").FSWatcher} fsW
  40686. */
  40687. constructor(fsW) {
  40688. this.fsw = fsW;
  40689. this._boundHandleError = (error) => fsW._handleError(error);
  40690. }
  40691. /**
  40692. * Watch file for changes with fs_watchFile or fs_watch.
  40693. * @param {String} path to file or dir
  40694. * @param {Function} listener on fs change
  40695. * @returns {Function} closer for the watcher instance
  40696. */
  40697. _watchWithNodeFs(path, listener) {
  40698. const opts = this.fsw.options;
  40699. const directory = sysPath$2.dirname(path);
  40700. const basename = sysPath$2.basename(path);
  40701. const parent = this.fsw._getWatchedDir(directory);
  40702. parent.add(basename);
  40703. const absolutePath = sysPath$2.resolve(path);
  40704. const options = {persistent: opts.persistent};
  40705. if (!listener) listener = EMPTY_FN$2;
  40706. let closer;
  40707. if (opts.usePolling) {
  40708. options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
  40709. opts.binaryInterval : opts.interval;
  40710. closer = setFsWatchFileListener(path, absolutePath, options, {
  40711. listener,
  40712. rawEmitter: this.fsw._emitRaw
  40713. });
  40714. } else {
  40715. closer = setFsWatchListener(path, absolutePath, options, {
  40716. listener,
  40717. errHandler: this._boundHandleError,
  40718. rawEmitter: this.fsw._emitRaw
  40719. });
  40720. }
  40721. return closer;
  40722. }
  40723. /**
  40724. * Watch a file and emit add event if warranted.
  40725. * @param {Path} file Path
  40726. * @param {fs.Stats} stats result of fs_stat
  40727. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40728. * @returns {Function} closer for the watcher instance
  40729. */
  40730. _handleFile(file, stats, initialAdd) {
  40731. if (this.fsw.closed) {
  40732. return;
  40733. }
  40734. const dirname = sysPath$2.dirname(file);
  40735. const basename = sysPath$2.basename(file);
  40736. const parent = this.fsw._getWatchedDir(dirname);
  40737. // stats is always present
  40738. let prevStats = stats;
  40739. // if the file is already being watched, do nothing
  40740. if (parent.has(basename)) return;
  40741. const listener = async (path, newStats) => {
  40742. if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
  40743. if (!newStats || newStats.mtimeMs === 0) {
  40744. try {
  40745. const newStats = await stat$2(file);
  40746. if (this.fsw.closed) return;
  40747. // Check that change event was not fired because of changed only accessTime.
  40748. const at = newStats.atimeMs;
  40749. const mt = newStats.mtimeMs;
  40750. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40751. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40752. }
  40753. if (isLinux && prevStats.ino !== newStats.ino) {
  40754. this.fsw._closeFile(path);
  40755. prevStats = newStats;
  40756. this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
  40757. } else {
  40758. prevStats = newStats;
  40759. }
  40760. } catch (error) {
  40761. // Fix issues where mtime is null but file is still present
  40762. this.fsw._remove(dirname, basename);
  40763. }
  40764. // add is about to be emitted if file not already tracked in parent
  40765. } else if (parent.has(basename)) {
  40766. // Check that change event was not fired because of changed only accessTime.
  40767. const at = newStats.atimeMs;
  40768. const mt = newStats.mtimeMs;
  40769. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40770. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40771. }
  40772. prevStats = newStats;
  40773. }
  40774. };
  40775. // kick off the watcher
  40776. const closer = this._watchWithNodeFs(file, listener);
  40777. // emit an add event if we're supposed to
  40778. if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
  40779. if (!this.fsw._throttle(EV_ADD$2, file, 0)) return;
  40780. this.fsw._emit(EV_ADD$2, file, stats);
  40781. }
  40782. return closer;
  40783. }
  40784. /**
  40785. * Handle symlinks encountered while reading a dir.
  40786. * @param {Object} entry returned by readdirp
  40787. * @param {String} directory path of dir being read
  40788. * @param {String} path of this item
  40789. * @param {String} item basename of this item
  40790. * @returns {Promise<Boolean>} true if no more processing is needed for this entry.
  40791. */
  40792. async _handleSymlink(entry, directory, path, item) {
  40793. if (this.fsw.closed) {
  40794. return;
  40795. }
  40796. const full = entry.fullPath;
  40797. const dir = this.fsw._getWatchedDir(directory);
  40798. if (!this.fsw.options.followSymlinks) {
  40799. // watch symlink directly (don't follow) and detect changes
  40800. this.fsw._incrReadyCount();
  40801. let linkPath;
  40802. try {
  40803. linkPath = await fsrealpath(path);
  40804. } catch (e) {
  40805. this.fsw._emitReady();
  40806. return true;
  40807. }
  40808. if (this.fsw.closed) return;
  40809. if (dir.has(item)) {
  40810. if (this.fsw._symlinkPaths.get(full) !== linkPath) {
  40811. this.fsw._symlinkPaths.set(full, linkPath);
  40812. this.fsw._emit(EV_CHANGE$2, path, entry.stats);
  40813. }
  40814. } else {
  40815. dir.add(item);
  40816. this.fsw._symlinkPaths.set(full, linkPath);
  40817. this.fsw._emit(EV_ADD$2, path, entry.stats);
  40818. }
  40819. this.fsw._emitReady();
  40820. return true;
  40821. }
  40822. // don't follow the same symlink more than once
  40823. if (this.fsw._symlinkPaths.has(full)) {
  40824. return true;
  40825. }
  40826. this.fsw._symlinkPaths.set(full, true);
  40827. }
  40828. _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
  40829. // Normalize the directory name on Windows
  40830. directory = sysPath$2.join(directory, EMPTY_STR$1);
  40831. if (!wh.hasGlob) {
  40832. throttler = this.fsw._throttle('readdir', directory, 1000);
  40833. if (!throttler) return;
  40834. }
  40835. const previous = this.fsw._getWatchedDir(wh.path);
  40836. const current = new Set();
  40837. let stream = this.fsw._readdirp(directory, {
  40838. fileFilter: entry => wh.filterPath(entry),
  40839. directoryFilter: entry => wh.filterDir(entry),
  40840. depth: 0
  40841. }).on(STR_DATA$1, async (entry) => {
  40842. if (this.fsw.closed) {
  40843. stream = undefined;
  40844. return;
  40845. }
  40846. const item = entry.path;
  40847. let path = sysPath$2.join(directory, item);
  40848. current.add(item);
  40849. if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
  40850. return;
  40851. }
  40852. if (this.fsw.closed) {
  40853. stream = undefined;
  40854. return;
  40855. }
  40856. // Files that present in current directory snapshot
  40857. // but absent in previous are added to watch list and
  40858. // emit `add` event.
  40859. if (item === target || !target && !previous.has(item)) {
  40860. this.fsw._incrReadyCount();
  40861. // ensure relativeness of path is preserved in case of watcher reuse
  40862. path = sysPath$2.join(dir, sysPath$2.relative(dir, path));
  40863. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  40864. }
  40865. }).on(EV_ERROR$2, this._boundHandleError);
  40866. return new Promise(resolve =>
  40867. stream.once(STR_END$2, () => {
  40868. if (this.fsw.closed) {
  40869. stream = undefined;
  40870. return;
  40871. }
  40872. const wasThrottled = throttler ? throttler.clear() : false;
  40873. resolve();
  40874. // Files that absent in current directory snapshot
  40875. // but present in previous emit `remove` event
  40876. // and are removed from @watched[directory].
  40877. previous.getChildren().filter((item) => {
  40878. return item !== directory &&
  40879. !current.has(item) &&
  40880. // in case of intersecting globs;
  40881. // a path may have been filtered out of this readdir, but
  40882. // shouldn't be removed because it matches a different glob
  40883. (!wh.hasGlob || wh.filterPath({
  40884. fullPath: sysPath$2.resolve(directory, item)
  40885. }));
  40886. }).forEach((item) => {
  40887. this.fsw._remove(directory, item);
  40888. });
  40889. stream = undefined;
  40890. // one more time for any missed in case changes came in extremely quickly
  40891. if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
  40892. })
  40893. );
  40894. }
  40895. /**
  40896. * Read directory to add / remove files from `@watched` list and re-read it on change.
  40897. * @param {String} dir fs path
  40898. * @param {fs.Stats} stats
  40899. * @param {Boolean} initialAdd
  40900. * @param {Number} depth relative to user-supplied path
  40901. * @param {String} target child path targeted for watch
  40902. * @param {Object} wh Common watch helpers for this path
  40903. * @param {String} realpath
  40904. * @returns {Promise<Function>} closer for the watcher instance.
  40905. */
  40906. async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
  40907. const parentDir = this.fsw._getWatchedDir(sysPath$2.dirname(dir));
  40908. const tracked = parentDir.has(sysPath$2.basename(dir));
  40909. if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
  40910. if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR$2, dir, stats);
  40911. }
  40912. // ensure dir is tracked (harmless if redundant)
  40913. parentDir.add(sysPath$2.basename(dir));
  40914. this.fsw._getWatchedDir(dir);
  40915. let throttler;
  40916. let closer;
  40917. const oDepth = this.fsw.options.depth;
  40918. if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
  40919. if (!target) {
  40920. await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
  40921. if (this.fsw.closed) return;
  40922. }
  40923. closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
  40924. // if current directory is removed, do nothing
  40925. if (stats && stats.mtimeMs === 0) return;
  40926. this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
  40927. });
  40928. }
  40929. return closer;
  40930. }
  40931. /**
  40932. * Handle added file, directory, or glob pattern.
  40933. * Delegates call to _handleFile / _handleDir after checks.
  40934. * @param {String} path to file or ir
  40935. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40936. * @param {Object} priorWh depth relative to user-supplied path
  40937. * @param {Number} depth Child path actually targeted for watch
  40938. * @param {String=} target Child path actually targeted for watch
  40939. * @returns {Promise}
  40940. */
  40941. async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
  40942. const ready = this.fsw._emitReady;
  40943. if (this.fsw._isIgnored(path) || this.fsw.closed) {
  40944. ready();
  40945. return false;
  40946. }
  40947. const wh = this.fsw._getWatchHelpers(path, depth);
  40948. if (!wh.hasGlob && priorWh) {
  40949. wh.hasGlob = priorWh.hasGlob;
  40950. wh.globFilter = priorWh.globFilter;
  40951. wh.filterPath = entry => priorWh.filterPath(entry);
  40952. wh.filterDir = entry => priorWh.filterDir(entry);
  40953. }
  40954. // evaluate what is at the path we're being asked to watch
  40955. try {
  40956. const stats = await statMethods$1[wh.statMethod](wh.watchPath);
  40957. if (this.fsw.closed) return;
  40958. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  40959. ready();
  40960. return false;
  40961. }
  40962. const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START$1);
  40963. let closer;
  40964. if (stats.isDirectory()) {
  40965. const absPath = sysPath$2.resolve(path);
  40966. const targetPath = follow ? await fsrealpath(path) : path;
  40967. if (this.fsw.closed) return;
  40968. closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
  40969. if (this.fsw.closed) return;
  40970. // preserve this symlink's target path
  40971. if (absPath !== targetPath && targetPath !== undefined) {
  40972. this.fsw._symlinkPaths.set(absPath, targetPath);
  40973. }
  40974. } else if (stats.isSymbolicLink()) {
  40975. const targetPath = follow ? await fsrealpath(path) : path;
  40976. if (this.fsw.closed) return;
  40977. const parent = sysPath$2.dirname(wh.watchPath);
  40978. this.fsw._getWatchedDir(parent).add(wh.watchPath);
  40979. this.fsw._emit(EV_ADD$2, wh.watchPath, stats);
  40980. closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
  40981. if (this.fsw.closed) return;
  40982. // preserve this symlink's target path
  40983. if (targetPath !== undefined) {
  40984. this.fsw._symlinkPaths.set(sysPath$2.resolve(path), targetPath);
  40985. }
  40986. } else {
  40987. closer = this._handleFile(wh.watchPath, stats, initialAdd);
  40988. }
  40989. ready();
  40990. this.fsw._addPathCloser(path, closer);
  40991. return false;
  40992. } catch (error) {
  40993. if (this.fsw._handleError(error)) {
  40994. ready();
  40995. return path;
  40996. }
  40997. }
  40998. }
  40999. };
  41000. var nodefsHandler = NodeFsHandler$1;
  41001. var fseventsHandler = {exports: {}};
  41002. const fs$6 = require$$0__default;
  41003. const sysPath$1 = require$$0$4;
  41004. const { promisify: promisify$1 } = require$$0$6;
  41005. let fsevents;
  41006. try {
  41007. fsevents = __require('fsevents');
  41008. } catch (error) {
  41009. if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error);
  41010. }
  41011. if (fsevents) {
  41012. // TODO: real check
  41013. const mtch = process.version.match(/v(\d+)\.(\d+)/);
  41014. if (mtch && mtch[1] && mtch[2]) {
  41015. const maj = Number.parseInt(mtch[1], 10);
  41016. const min = Number.parseInt(mtch[2], 10);
  41017. if (maj === 8 && min < 16) {
  41018. fsevents = undefined;
  41019. }
  41020. }
  41021. }
  41022. const {
  41023. EV_ADD: EV_ADD$1,
  41024. EV_CHANGE: EV_CHANGE$1,
  41025. EV_ADD_DIR: EV_ADD_DIR$1,
  41026. EV_UNLINK: EV_UNLINK$1,
  41027. EV_ERROR: EV_ERROR$1,
  41028. STR_DATA,
  41029. STR_END: STR_END$1,
  41030. FSEVENT_CREATED,
  41031. FSEVENT_MODIFIED,
  41032. FSEVENT_DELETED,
  41033. FSEVENT_MOVED,
  41034. // FSEVENT_CLONED,
  41035. FSEVENT_UNKNOWN,
  41036. FSEVENT_FLAG_MUST_SCAN_SUBDIRS,
  41037. FSEVENT_TYPE_FILE,
  41038. FSEVENT_TYPE_DIRECTORY,
  41039. FSEVENT_TYPE_SYMLINK,
  41040. ROOT_GLOBSTAR,
  41041. DIR_SUFFIX,
  41042. DOT_SLASH,
  41043. FUNCTION_TYPE: FUNCTION_TYPE$1,
  41044. EMPTY_FN: EMPTY_FN$1,
  41045. IDENTITY_FN
  41046. } = constants$1;
  41047. const Depth = (value) => isNaN(value) ? {} : {depth: value};
  41048. const stat$1 = promisify$1(fs$6.stat);
  41049. const lstat = promisify$1(fs$6.lstat);
  41050. const realpath = promisify$1(fs$6.realpath);
  41051. const statMethods = { stat: stat$1, lstat };
  41052. /**
  41053. * @typedef {String} Path
  41054. */
  41055. /**
  41056. * @typedef {Object} FsEventsWatchContainer
  41057. * @property {Set<Function>} listeners
  41058. * @property {Function} rawEmitter
  41059. * @property {{stop: Function}} watcher
  41060. */
  41061. // fsevents instance helper functions
  41062. /**
  41063. * Object to hold per-process fsevents instances (may be shared across chokidar FSWatcher instances)
  41064. * @type {Map<Path,FsEventsWatchContainer>}
  41065. */
  41066. const FSEventsWatchers = new Map();
  41067. // Threshold of duplicate path prefixes at which to start
  41068. // consolidating going forward
  41069. const consolidateThreshhold = 10;
  41070. const wrongEventFlags = new Set([
  41071. 69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912
  41072. ]);
  41073. /**
  41074. * Instantiates the fsevents interface
  41075. * @param {Path} path path to be watched
  41076. * @param {Function} callback called when fsevents is bound and ready
  41077. * @returns {{stop: Function}} new fsevents instance
  41078. */
  41079. const createFSEventsInstance = (path, callback) => {
  41080. const stop = fsevents.watch(path, callback);
  41081. return {stop};
  41082. };
  41083. /**
  41084. * Instantiates the fsevents interface or binds listeners to an existing one covering
  41085. * the same file tree.
  41086. * @param {Path} path - to be watched
  41087. * @param {Path} realPath - real path for symlinks
  41088. * @param {Function} listener - called when fsevents emits events
  41089. * @param {Function} rawEmitter - passes data to listeners of the 'raw' event
  41090. * @returns {Function} closer
  41091. */
  41092. function setFSEventsListener(path, realPath, listener, rawEmitter) {
  41093. let watchPath = sysPath$1.extname(realPath) ? sysPath$1.dirname(realPath) : realPath;
  41094. const parentPath = sysPath$1.dirname(watchPath);
  41095. let cont = FSEventsWatchers.get(watchPath);
  41096. // If we've accumulated a substantial number of paths that
  41097. // could have been consolidated by watching one directory
  41098. // above the current one, create a watcher on the parent
  41099. // path instead, so that we do consolidate going forward.
  41100. if (couldConsolidate(parentPath)) {
  41101. watchPath = parentPath;
  41102. }
  41103. const resolvedPath = sysPath$1.resolve(path);
  41104. const hasSymlink = resolvedPath !== realPath;
  41105. const filteredListener = (fullPath, flags, info) => {
  41106. if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
  41107. if (
  41108. fullPath === resolvedPath ||
  41109. !fullPath.indexOf(resolvedPath + sysPath$1.sep)
  41110. ) listener(fullPath, flags, info);
  41111. };
  41112. // check if there is already a watcher on a parent path
  41113. // modifies `watchPath` to the parent path when it finds a match
  41114. let watchedParent = false;
  41115. for (const watchedPath of FSEventsWatchers.keys()) {
  41116. if (realPath.indexOf(sysPath$1.resolve(watchedPath) + sysPath$1.sep) === 0) {
  41117. watchPath = watchedPath;
  41118. cont = FSEventsWatchers.get(watchPath);
  41119. watchedParent = true;
  41120. break;
  41121. }
  41122. }
  41123. if (cont || watchedParent) {
  41124. cont.listeners.add(filteredListener);
  41125. } else {
  41126. cont = {
  41127. listeners: new Set([filteredListener]),
  41128. rawEmitter,
  41129. watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
  41130. if (!cont.listeners.size) return;
  41131. if (flags & FSEVENT_FLAG_MUST_SCAN_SUBDIRS) return;
  41132. const info = fsevents.getInfo(fullPath, flags);
  41133. cont.listeners.forEach(list => {
  41134. list(fullPath, flags, info);
  41135. });
  41136. cont.rawEmitter(info.event, fullPath, info);
  41137. })
  41138. };
  41139. FSEventsWatchers.set(watchPath, cont);
  41140. }
  41141. // removes this instance's listeners and closes the underlying fsevents
  41142. // instance if there are no more listeners left
  41143. return () => {
  41144. const lst = cont.listeners;
  41145. lst.delete(filteredListener);
  41146. if (!lst.size) {
  41147. FSEventsWatchers.delete(watchPath);
  41148. if (cont.watcher) return cont.watcher.stop().then(() => {
  41149. cont.rawEmitter = cont.watcher = undefined;
  41150. Object.freeze(cont);
  41151. });
  41152. }
  41153. };
  41154. }
  41155. // Decide whether or not we should start a new higher-level
  41156. // parent watcher
  41157. const couldConsolidate = (path) => {
  41158. let count = 0;
  41159. for (const watchPath of FSEventsWatchers.keys()) {
  41160. if (watchPath.indexOf(path) === 0) {
  41161. count++;
  41162. if (count >= consolidateThreshhold) {
  41163. return true;
  41164. }
  41165. }
  41166. }
  41167. return false;
  41168. };
  41169. // returns boolean indicating whether fsevents can be used
  41170. const canUse = () => fsevents && FSEventsWatchers.size < 128;
  41171. // determines subdirectory traversal levels from root to path
  41172. const calcDepth = (path, root) => {
  41173. let i = 0;
  41174. while (!path.indexOf(root) && (path = sysPath$1.dirname(path)) !== root) i++;
  41175. return i;
  41176. };
  41177. // returns boolean indicating whether the fsevents' event info has the same type
  41178. // as the one returned by fs.stat
  41179. const sameTypes = (info, stats) => (
  41180. info.type === FSEVENT_TYPE_DIRECTORY && stats.isDirectory() ||
  41181. info.type === FSEVENT_TYPE_SYMLINK && stats.isSymbolicLink() ||
  41182. info.type === FSEVENT_TYPE_FILE && stats.isFile()
  41183. );
  41184. /**
  41185. * @mixin
  41186. */
  41187. let FsEventsHandler$1 = class FsEventsHandler {
  41188. /**
  41189. * @param {import('../index').FSWatcher} fsw
  41190. */
  41191. constructor(fsw) {
  41192. this.fsw = fsw;
  41193. }
  41194. checkIgnored(path, stats) {
  41195. const ipaths = this.fsw._ignoredPaths;
  41196. if (this.fsw._isIgnored(path, stats)) {
  41197. ipaths.add(path);
  41198. if (stats && stats.isDirectory()) {
  41199. ipaths.add(path + ROOT_GLOBSTAR);
  41200. }
  41201. return true;
  41202. }
  41203. ipaths.delete(path);
  41204. ipaths.delete(path + ROOT_GLOBSTAR);
  41205. }
  41206. addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  41207. const event = watchedDir.has(item) ? EV_CHANGE$1 : EV_ADD$1;
  41208. this.handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41209. }
  41210. async checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  41211. try {
  41212. const stats = await stat$1(path);
  41213. if (this.fsw.closed) return;
  41214. if (sameTypes(info, stats)) {
  41215. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41216. } else {
  41217. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41218. }
  41219. } catch (error) {
  41220. if (error.code === 'EACCES') {
  41221. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41222. } else {
  41223. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41224. }
  41225. }
  41226. }
  41227. handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  41228. if (this.fsw.closed || this.checkIgnored(path)) return;
  41229. if (event === EV_UNLINK$1) {
  41230. const isDirectory = info.type === FSEVENT_TYPE_DIRECTORY;
  41231. // suppress unlink events on never before seen files
  41232. if (isDirectory || watchedDir.has(item)) {
  41233. this.fsw._remove(parent, item, isDirectory);
  41234. }
  41235. } else {
  41236. if (event === EV_ADD$1) {
  41237. // track new directories
  41238. if (info.type === FSEVENT_TYPE_DIRECTORY) this.fsw._getWatchedDir(path);
  41239. if (info.type === FSEVENT_TYPE_SYMLINK && opts.followSymlinks) {
  41240. // push symlinks back to the top of the stack to get handled
  41241. const curDepth = opts.depth === undefined ?
  41242. undefined : calcDepth(fullPath, realPath) + 1;
  41243. return this._addToFsEvents(path, false, true, curDepth);
  41244. }
  41245. // track new paths
  41246. // (other than symlinks being followed, which will be tracked soon)
  41247. this.fsw._getWatchedDir(parent).add(item);
  41248. }
  41249. /**
  41250. * @type {'add'|'addDir'|'unlink'|'unlinkDir'}
  41251. */
  41252. const eventName = info.type === FSEVENT_TYPE_DIRECTORY ? event + DIR_SUFFIX : event;
  41253. this.fsw._emit(eventName, path);
  41254. if (eventName === EV_ADD_DIR$1) this._addToFsEvents(path, false, true);
  41255. }
  41256. }
  41257. /**
  41258. * Handle symlinks encountered during directory scan
  41259. * @param {String} watchPath - file/dir path to be watched with fsevents
  41260. * @param {String} realPath - real path (in case of symlinks)
  41261. * @param {Function} transform - path transformer
  41262. * @param {Function} globFilter - path filter in case a glob pattern was provided
  41263. * @returns {Function} closer for the watcher instance
  41264. */
  41265. _watchWithFsEvents(watchPath, realPath, transform, globFilter) {
  41266. if (this.fsw.closed || this.fsw._isIgnored(watchPath)) return;
  41267. const opts = this.fsw.options;
  41268. const watchCallback = async (fullPath, flags, info) => {
  41269. // PATCH: bypass the callback for better perf when fullPath hit the ignored file list
  41270. if (this.fsw.closed || this.fsw._isIgnored(fullPath)) return;
  41271. if (
  41272. opts.depth !== undefined &&
  41273. calcDepth(fullPath, realPath) > opts.depth
  41274. ) return;
  41275. const path = transform(sysPath$1.join(
  41276. watchPath, sysPath$1.relative(watchPath, fullPath)
  41277. ));
  41278. if (globFilter && !globFilter(path)) return;
  41279. // ensure directories are tracked
  41280. const parent = sysPath$1.dirname(path);
  41281. const item = sysPath$1.basename(path);
  41282. const watchedDir = this.fsw._getWatchedDir(
  41283. info.type === FSEVENT_TYPE_DIRECTORY ? path : parent
  41284. );
  41285. // correct for wrong events emitted
  41286. if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) {
  41287. if (typeof opts.ignored === FUNCTION_TYPE$1) {
  41288. let stats;
  41289. try {
  41290. stats = await stat$1(path);
  41291. } catch (error) {}
  41292. if (this.fsw.closed) return;
  41293. if (this.checkIgnored(path, stats)) return;
  41294. if (sameTypes(info, stats)) {
  41295. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41296. } else {
  41297. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41298. }
  41299. } else {
  41300. this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41301. }
  41302. } else {
  41303. switch (info.event) {
  41304. case FSEVENT_CREATED:
  41305. case FSEVENT_MODIFIED:
  41306. return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41307. case FSEVENT_DELETED:
  41308. case FSEVENT_MOVED:
  41309. return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  41310. }
  41311. }
  41312. };
  41313. const closer = setFSEventsListener(
  41314. watchPath,
  41315. realPath,
  41316. watchCallback,
  41317. this.fsw._emitRaw
  41318. );
  41319. this.fsw._emitReady();
  41320. return closer;
  41321. }
  41322. /**
  41323. * Handle symlinks encountered during directory scan
  41324. * @param {String} linkPath path to symlink
  41325. * @param {String} fullPath absolute path to the symlink
  41326. * @param {Function} transform pre-existing path transformer
  41327. * @param {Number} curDepth level of subdirectories traversed to where symlink is
  41328. * @returns {Promise<void>}
  41329. */
  41330. async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) {
  41331. // don't follow the same symlink more than once
  41332. if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return;
  41333. this.fsw._symlinkPaths.set(fullPath, true);
  41334. this.fsw._incrReadyCount();
  41335. try {
  41336. const linkTarget = await realpath(linkPath);
  41337. if (this.fsw.closed) return;
  41338. if (this.fsw._isIgnored(linkTarget)) {
  41339. return this.fsw._emitReady();
  41340. }
  41341. this.fsw._incrReadyCount();
  41342. // add the linkTarget for watching with a wrapper for transform
  41343. // that causes emitted paths to incorporate the link's path
  41344. this._addToFsEvents(linkTarget || linkPath, (path) => {
  41345. let aliasedPath = linkPath;
  41346. if (linkTarget && linkTarget !== DOT_SLASH) {
  41347. aliasedPath = path.replace(linkTarget, linkPath);
  41348. } else if (path !== DOT_SLASH) {
  41349. aliasedPath = sysPath$1.join(linkPath, path);
  41350. }
  41351. return transform(aliasedPath);
  41352. }, false, curDepth);
  41353. } catch(error) {
  41354. if (this.fsw._handleError(error)) {
  41355. return this.fsw._emitReady();
  41356. }
  41357. }
  41358. }
  41359. /**
  41360. *
  41361. * @param {Path} newPath
  41362. * @param {fs.Stats} stats
  41363. */
  41364. emitAdd(newPath, stats, processPath, opts, forceAdd) {
  41365. const pp = processPath(newPath);
  41366. const isDir = stats.isDirectory();
  41367. const dirObj = this.fsw._getWatchedDir(sysPath$1.dirname(pp));
  41368. const base = sysPath$1.basename(pp);
  41369. // ensure empty dirs get tracked
  41370. if (isDir) this.fsw._getWatchedDir(pp);
  41371. if (dirObj.has(base)) return;
  41372. dirObj.add(base);
  41373. if (!opts.ignoreInitial || forceAdd === true) {
  41374. this.fsw._emit(isDir ? EV_ADD_DIR$1 : EV_ADD$1, pp, stats);
  41375. }
  41376. }
  41377. initWatch(realPath, path, wh, processPath) {
  41378. if (this.fsw.closed) return;
  41379. const closer = this._watchWithFsEvents(
  41380. wh.watchPath,
  41381. sysPath$1.resolve(realPath || wh.watchPath),
  41382. processPath,
  41383. wh.globFilter
  41384. );
  41385. this.fsw._addPathCloser(path, closer);
  41386. }
  41387. /**
  41388. * Handle added path with fsevents
  41389. * @param {String} path file/dir path or glob pattern
  41390. * @param {Function|Boolean=} transform converts working path to what the user expects
  41391. * @param {Boolean=} forceAdd ensure add is emitted
  41392. * @param {Number=} priorDepth Level of subdirectories already traversed.
  41393. * @returns {Promise<void>}
  41394. */
  41395. async _addToFsEvents(path, transform, forceAdd, priorDepth) {
  41396. if (this.fsw.closed) {
  41397. return;
  41398. }
  41399. const opts = this.fsw.options;
  41400. const processPath = typeof transform === FUNCTION_TYPE$1 ? transform : IDENTITY_FN;
  41401. const wh = this.fsw._getWatchHelpers(path);
  41402. // evaluate what is at the path we're being asked to watch
  41403. try {
  41404. const stats = await statMethods[wh.statMethod](wh.watchPath);
  41405. if (this.fsw.closed) return;
  41406. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  41407. throw null;
  41408. }
  41409. if (stats.isDirectory()) {
  41410. // emit addDir unless this is a glob parent
  41411. if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd);
  41412. // don't recurse further if it would exceed depth setting
  41413. if (priorDepth && priorDepth > opts.depth) return;
  41414. // scan the contents of the dir
  41415. this.fsw._readdirp(wh.watchPath, {
  41416. fileFilter: entry => wh.filterPath(entry),
  41417. directoryFilter: entry => wh.filterDir(entry),
  41418. ...Depth(opts.depth - (priorDepth || 0))
  41419. }).on(STR_DATA, (entry) => {
  41420. // need to check filterPath on dirs b/c filterDir is less restrictive
  41421. if (this.fsw.closed) {
  41422. return;
  41423. }
  41424. if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
  41425. const joinedPath = sysPath$1.join(wh.watchPath, entry.path);
  41426. const {fullPath} = entry;
  41427. if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
  41428. // preserve the current depth here since it can't be derived from
  41429. // real paths past the symlink
  41430. const curDepth = opts.depth === undefined ?
  41431. undefined : calcDepth(joinedPath, sysPath$1.resolve(wh.watchPath)) + 1;
  41432. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  41433. } else {
  41434. this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
  41435. }
  41436. }).on(EV_ERROR$1, EMPTY_FN$1).on(STR_END$1, () => {
  41437. this.fsw._emitReady();
  41438. });
  41439. } else {
  41440. this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd);
  41441. this.fsw._emitReady();
  41442. }
  41443. } catch (error) {
  41444. if (!error || this.fsw._handleError(error)) {
  41445. // TODO: Strange thing: "should not choke on an ignored watch path" will be failed without 2 ready calls -__-
  41446. this.fsw._emitReady();
  41447. this.fsw._emitReady();
  41448. }
  41449. }
  41450. if (opts.persistent && forceAdd !== true) {
  41451. if (typeof transform === FUNCTION_TYPE$1) {
  41452. // realpath has already been resolved
  41453. this.initWatch(undefined, path, wh, processPath);
  41454. } else {
  41455. let realPath;
  41456. try {
  41457. realPath = await realpath(wh.watchPath);
  41458. } catch (e) {}
  41459. this.initWatch(realPath, path, wh, processPath);
  41460. }
  41461. }
  41462. }
  41463. };
  41464. fseventsHandler.exports = FsEventsHandler$1;
  41465. fseventsHandler.exports.canUse = canUse;
  41466. var fseventsHandlerExports = fseventsHandler.exports;
  41467. const { EventEmitter: EventEmitter$2 } = require$$0$5;
  41468. const fs$5 = require$$0__default;
  41469. const sysPath = require$$0$4;
  41470. const { promisify } = require$$0$6;
  41471. const readdirp = readdirp_1;
  41472. const anymatch = anymatchExports.default;
  41473. const globParent = globParent$2;
  41474. const isGlob = isGlob$2;
  41475. const braces = braces_1;
  41476. const normalizePath = normalizePath$2;
  41477. const NodeFsHandler = nodefsHandler;
  41478. const FsEventsHandler = fseventsHandlerExports;
  41479. const {
  41480. EV_ALL,
  41481. EV_READY,
  41482. EV_ADD,
  41483. EV_CHANGE,
  41484. EV_UNLINK,
  41485. EV_ADD_DIR,
  41486. EV_UNLINK_DIR,
  41487. EV_RAW,
  41488. EV_ERROR,
  41489. STR_CLOSE,
  41490. STR_END,
  41491. BACK_SLASH_RE,
  41492. DOUBLE_SLASH_RE,
  41493. SLASH_OR_BACK_SLASH_RE,
  41494. DOT_RE,
  41495. REPLACER_RE,
  41496. SLASH,
  41497. SLASH_SLASH,
  41498. BRACE_START,
  41499. BANG,
  41500. ONE_DOT,
  41501. TWO_DOTS,
  41502. GLOBSTAR,
  41503. SLASH_GLOBSTAR,
  41504. ANYMATCH_OPTS,
  41505. STRING_TYPE,
  41506. FUNCTION_TYPE,
  41507. EMPTY_STR,
  41508. EMPTY_FN,
  41509. isWindows: isWindows$1,
  41510. isMacos,
  41511. isIBMi
  41512. } = constants$1;
  41513. const stat = promisify(fs$5.stat);
  41514. const readdir = promisify(fs$5.readdir);
  41515. /**
  41516. * @typedef {String} Path
  41517. * @typedef {'all'|'add'|'addDir'|'change'|'unlink'|'unlinkDir'|'raw'|'error'|'ready'} EventName
  41518. * @typedef {'readdir'|'watch'|'add'|'remove'|'change'} ThrottleType
  41519. */
  41520. /**
  41521. *
  41522. * @typedef {Object} WatchHelpers
  41523. * @property {Boolean} followSymlinks
  41524. * @property {'stat'|'lstat'} statMethod
  41525. * @property {Path} path
  41526. * @property {Path} watchPath
  41527. * @property {Function} entryPath
  41528. * @property {Boolean} hasGlob
  41529. * @property {Object} globFilter
  41530. * @property {Function} filterPath
  41531. * @property {Function} filterDir
  41532. */
  41533. const arrify = (value = []) => Array.isArray(value) ? value : [value];
  41534. const flatten = (list, result = []) => {
  41535. list.forEach(item => {
  41536. if (Array.isArray(item)) {
  41537. flatten(item, result);
  41538. } else {
  41539. result.push(item);
  41540. }
  41541. });
  41542. return result;
  41543. };
  41544. const unifyPaths = (paths_) => {
  41545. /**
  41546. * @type {Array<String>}
  41547. */
  41548. const paths = flatten(arrify(paths_));
  41549. if (!paths.every(p => typeof p === STRING_TYPE)) {
  41550. throw new TypeError(`Non-string provided as watch path: ${paths}`);
  41551. }
  41552. return paths.map(normalizePathToUnix);
  41553. };
  41554. // If SLASH_SLASH occurs at the beginning of path, it is not replaced
  41555. // because "//StoragePC/DrivePool/Movies" is a valid network path
  41556. const toUnix = (string) => {
  41557. let str = string.replace(BACK_SLASH_RE, SLASH);
  41558. let prepend = false;
  41559. if (str.startsWith(SLASH_SLASH)) {
  41560. prepend = true;
  41561. }
  41562. while (str.match(DOUBLE_SLASH_RE)) {
  41563. str = str.replace(DOUBLE_SLASH_RE, SLASH);
  41564. }
  41565. if (prepend) {
  41566. str = SLASH + str;
  41567. }
  41568. return str;
  41569. };
  41570. // Our version of upath.normalize
  41571. // TODO: this is not equal to path-normalize module - investigate why
  41572. const normalizePathToUnix = (path) => toUnix(sysPath.normalize(toUnix(path)));
  41573. const normalizeIgnored = (cwd = EMPTY_STR) => (path) => {
  41574. if (typeof path !== STRING_TYPE) return path;
  41575. return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
  41576. };
  41577. const getAbsolutePath = (path, cwd) => {
  41578. if (sysPath.isAbsolute(path)) {
  41579. return path;
  41580. }
  41581. if (path.startsWith(BANG)) {
  41582. return BANG + sysPath.join(cwd, path.slice(1));
  41583. }
  41584. return sysPath.join(cwd, path);
  41585. };
  41586. const undef = (opts, key) => opts[key] === undefined;
  41587. /**
  41588. * Directory entry.
  41589. * @property {Path} path
  41590. * @property {Set<Path>} items
  41591. */
  41592. class DirEntry {
  41593. /**
  41594. * @param {Path} dir
  41595. * @param {Function} removeWatcher
  41596. */
  41597. constructor(dir, removeWatcher) {
  41598. this.path = dir;
  41599. this._removeWatcher = removeWatcher;
  41600. /** @type {Set<Path>} */
  41601. this.items = new Set();
  41602. }
  41603. add(item) {
  41604. const {items} = this;
  41605. if (!items) return;
  41606. if (item !== ONE_DOT && item !== TWO_DOTS) items.add(item);
  41607. }
  41608. async remove(item) {
  41609. const {items} = this;
  41610. if (!items) return;
  41611. items.delete(item);
  41612. if (items.size > 0) return;
  41613. const dir = this.path;
  41614. try {
  41615. await readdir(dir);
  41616. } catch (err) {
  41617. if (this._removeWatcher) {
  41618. this._removeWatcher(sysPath.dirname(dir), sysPath.basename(dir));
  41619. }
  41620. }
  41621. }
  41622. has(item) {
  41623. const {items} = this;
  41624. if (!items) return;
  41625. return items.has(item);
  41626. }
  41627. /**
  41628. * @returns {Array<String>}
  41629. */
  41630. getChildren() {
  41631. const {items} = this;
  41632. if (!items) return;
  41633. return [...items.values()];
  41634. }
  41635. dispose() {
  41636. this.items.clear();
  41637. delete this.path;
  41638. delete this._removeWatcher;
  41639. delete this.items;
  41640. Object.freeze(this);
  41641. }
  41642. }
  41643. const STAT_METHOD_F = 'stat';
  41644. const STAT_METHOD_L = 'lstat';
  41645. class WatchHelper {
  41646. constructor(path, watchPath, follow, fsw) {
  41647. this.fsw = fsw;
  41648. this.path = path = path.replace(REPLACER_RE, EMPTY_STR);
  41649. this.watchPath = watchPath;
  41650. this.fullWatchPath = sysPath.resolve(watchPath);
  41651. this.hasGlob = watchPath !== path;
  41652. /** @type {object|boolean} */
  41653. if (path === EMPTY_STR) this.hasGlob = false;
  41654. this.globSymlink = this.hasGlob && follow ? undefined : false;
  41655. this.globFilter = this.hasGlob ? anymatch(path, undefined, ANYMATCH_OPTS) : false;
  41656. this.dirParts = this.getDirParts(path);
  41657. this.dirParts.forEach((parts) => {
  41658. if (parts.length > 1) parts.pop();
  41659. });
  41660. this.followSymlinks = follow;
  41661. this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
  41662. }
  41663. checkGlobSymlink(entry) {
  41664. // only need to resolve once
  41665. // first entry should always have entry.parentDir === EMPTY_STR
  41666. if (this.globSymlink === undefined) {
  41667. this.globSymlink = entry.fullParentDir === this.fullWatchPath ?
  41668. false : {realPath: entry.fullParentDir, linkPath: this.fullWatchPath};
  41669. }
  41670. if (this.globSymlink) {
  41671. return entry.fullPath.replace(this.globSymlink.realPath, this.globSymlink.linkPath);
  41672. }
  41673. return entry.fullPath;
  41674. }
  41675. entryPath(entry) {
  41676. return sysPath.join(this.watchPath,
  41677. sysPath.relative(this.watchPath, this.checkGlobSymlink(entry))
  41678. );
  41679. }
  41680. filterPath(entry) {
  41681. const {stats} = entry;
  41682. if (stats && stats.isSymbolicLink()) return this.filterDir(entry);
  41683. const resolvedPath = this.entryPath(entry);
  41684. const matchesGlob = this.hasGlob && typeof this.globFilter === FUNCTION_TYPE ?
  41685. this.globFilter(resolvedPath) : true;
  41686. return matchesGlob &&
  41687. this.fsw._isntIgnored(resolvedPath, stats) &&
  41688. this.fsw._hasReadPermissions(stats);
  41689. }
  41690. getDirParts(path) {
  41691. if (!this.hasGlob) return [];
  41692. const parts = [];
  41693. const expandedPath = path.includes(BRACE_START) ? braces.expand(path) : [path];
  41694. expandedPath.forEach((path) => {
  41695. parts.push(sysPath.relative(this.watchPath, path).split(SLASH_OR_BACK_SLASH_RE));
  41696. });
  41697. return parts;
  41698. }
  41699. filterDir(entry) {
  41700. if (this.hasGlob) {
  41701. const entryParts = this.getDirParts(this.checkGlobSymlink(entry));
  41702. let globstar = false;
  41703. this.unmatchedGlob = !this.dirParts.some((parts) => {
  41704. return parts.every((part, i) => {
  41705. if (part === GLOBSTAR) globstar = true;
  41706. return globstar || !entryParts[0][i] || anymatch(part, entryParts[0][i], ANYMATCH_OPTS);
  41707. });
  41708. });
  41709. }
  41710. return !this.unmatchedGlob && this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
  41711. }
  41712. }
  41713. /**
  41714. * Watches files & directories for changes. Emitted events:
  41715. * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
  41716. *
  41717. * new FSWatcher()
  41718. * .add(directories)
  41719. * .on('add', path => log('File', path, 'was added'))
  41720. */
  41721. class FSWatcher extends EventEmitter$2 {
  41722. // Not indenting methods for history sake; for now.
  41723. constructor(_opts) {
  41724. super();
  41725. const opts = {};
  41726. if (_opts) Object.assign(opts, _opts); // for frozen objects
  41727. /** @type {Map<String, DirEntry>} */
  41728. this._watched = new Map();
  41729. /** @type {Map<String, Array>} */
  41730. this._closers = new Map();
  41731. /** @type {Set<String>} */
  41732. this._ignoredPaths = new Set();
  41733. /** @type {Map<ThrottleType, Map>} */
  41734. this._throttled = new Map();
  41735. /** @type {Map<Path, String|Boolean>} */
  41736. this._symlinkPaths = new Map();
  41737. this._streams = new Set();
  41738. this.closed = false;
  41739. // Set up default options.
  41740. if (undef(opts, 'persistent')) opts.persistent = true;
  41741. if (undef(opts, 'ignoreInitial')) opts.ignoreInitial = false;
  41742. if (undef(opts, 'ignorePermissionErrors')) opts.ignorePermissionErrors = false;
  41743. if (undef(opts, 'interval')) opts.interval = 100;
  41744. if (undef(opts, 'binaryInterval')) opts.binaryInterval = 300;
  41745. if (undef(opts, 'disableGlobbing')) opts.disableGlobbing = false;
  41746. opts.enableBinaryInterval = opts.binaryInterval !== opts.interval;
  41747. // Enable fsevents on OS X when polling isn't explicitly enabled.
  41748. if (undef(opts, 'useFsEvents')) opts.useFsEvents = !opts.usePolling;
  41749. // If we can't use fsevents, ensure the options reflect it's disabled.
  41750. const canUseFsEvents = FsEventsHandler.canUse();
  41751. if (!canUseFsEvents) opts.useFsEvents = false;
  41752. // Use polling on Mac if not using fsevents.
  41753. // Other platforms use non-polling fs_watch.
  41754. if (undef(opts, 'usePolling') && !opts.useFsEvents) {
  41755. opts.usePolling = isMacos;
  41756. }
  41757. // Always default to polling on IBM i because fs.watch() is not available on IBM i.
  41758. if(isIBMi) {
  41759. opts.usePolling = true;
  41760. }
  41761. // Global override (useful for end-developers that need to force polling for all
  41762. // instances of chokidar, regardless of usage/dependency depth)
  41763. const envPoll = process.env.CHOKIDAR_USEPOLLING;
  41764. if (envPoll !== undefined) {
  41765. const envLower = envPoll.toLowerCase();
  41766. if (envLower === 'false' || envLower === '0') {
  41767. opts.usePolling = false;
  41768. } else if (envLower === 'true' || envLower === '1') {
  41769. opts.usePolling = true;
  41770. } else {
  41771. opts.usePolling = !!envLower;
  41772. }
  41773. }
  41774. const envInterval = process.env.CHOKIDAR_INTERVAL;
  41775. if (envInterval) {
  41776. opts.interval = Number.parseInt(envInterval, 10);
  41777. }
  41778. // Editor atomic write normalization enabled by default with fs.watch
  41779. if (undef(opts, 'atomic')) opts.atomic = !opts.usePolling && !opts.useFsEvents;
  41780. if (opts.atomic) this._pendingUnlinks = new Map();
  41781. if (undef(opts, 'followSymlinks')) opts.followSymlinks = true;
  41782. if (undef(opts, 'awaitWriteFinish')) opts.awaitWriteFinish = false;
  41783. if (opts.awaitWriteFinish === true) opts.awaitWriteFinish = {};
  41784. const awf = opts.awaitWriteFinish;
  41785. if (awf) {
  41786. if (!awf.stabilityThreshold) awf.stabilityThreshold = 2000;
  41787. if (!awf.pollInterval) awf.pollInterval = 100;
  41788. this._pendingWrites = new Map();
  41789. }
  41790. if (opts.ignored) opts.ignored = arrify(opts.ignored);
  41791. let readyCalls = 0;
  41792. this._emitReady = () => {
  41793. readyCalls++;
  41794. if (readyCalls >= this._readyCount) {
  41795. this._emitReady = EMPTY_FN;
  41796. this._readyEmitted = true;
  41797. // use process.nextTick to allow time for listener to be bound
  41798. process.nextTick(() => this.emit(EV_READY));
  41799. }
  41800. };
  41801. this._emitRaw = (...args) => this.emit(EV_RAW, ...args);
  41802. this._readyEmitted = false;
  41803. this.options = opts;
  41804. // Initialize with proper watcher.
  41805. if (opts.useFsEvents) {
  41806. this._fsEventsHandler = new FsEventsHandler(this);
  41807. } else {
  41808. this._nodeFsHandler = new NodeFsHandler(this);
  41809. }
  41810. // You’re frozen when your heart’s not open.
  41811. Object.freeze(opts);
  41812. }
  41813. // Public methods
  41814. /**
  41815. * Adds paths to be watched on an existing FSWatcher instance
  41816. * @param {Path|Array<Path>} paths_
  41817. * @param {String=} _origAdd private; for handling non-existent paths to be watched
  41818. * @param {Boolean=} _internal private; indicates a non-user add
  41819. * @returns {FSWatcher} for chaining
  41820. */
  41821. add(paths_, _origAdd, _internal) {
  41822. const {cwd, disableGlobbing} = this.options;
  41823. this.closed = false;
  41824. let paths = unifyPaths(paths_);
  41825. if (cwd) {
  41826. paths = paths.map((path) => {
  41827. const absPath = getAbsolutePath(path, cwd);
  41828. // Check `path` instead of `absPath` because the cwd portion can't be a glob
  41829. if (disableGlobbing || !isGlob(path)) {
  41830. return absPath;
  41831. }
  41832. return normalizePath(absPath);
  41833. });
  41834. }
  41835. // set aside negated glob strings
  41836. paths = paths.filter((path) => {
  41837. if (path.startsWith(BANG)) {
  41838. this._ignoredPaths.add(path.slice(1));
  41839. return false;
  41840. }
  41841. // if a path is being added that was previously ignored, stop ignoring it
  41842. this._ignoredPaths.delete(path);
  41843. this._ignoredPaths.delete(path + SLASH_GLOBSTAR);
  41844. // reset the cached userIgnored anymatch fn
  41845. // to make ignoredPaths changes effective
  41846. this._userIgnored = undefined;
  41847. return true;
  41848. });
  41849. if (this.options.useFsEvents && this._fsEventsHandler) {
  41850. if (!this._readyCount) this._readyCount = paths.length;
  41851. if (this.options.persistent) this._readyCount += paths.length;
  41852. paths.forEach((path) => this._fsEventsHandler._addToFsEvents(path));
  41853. } else {
  41854. if (!this._readyCount) this._readyCount = 0;
  41855. this._readyCount += paths.length;
  41856. Promise.all(
  41857. paths.map(async path => {
  41858. const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, 0, 0, _origAdd);
  41859. if (res) this._emitReady();
  41860. return res;
  41861. })
  41862. ).then(results => {
  41863. if (this.closed) return;
  41864. results.filter(item => item).forEach(item => {
  41865. this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
  41866. });
  41867. });
  41868. }
  41869. return this;
  41870. }
  41871. /**
  41872. * Close watchers or start ignoring events from specified paths.
  41873. * @param {Path|Array<Path>} paths_ - string or array of strings, file/directory paths and/or globs
  41874. * @returns {FSWatcher} for chaining
  41875. */
  41876. unwatch(paths_) {
  41877. if (this.closed) return this;
  41878. const paths = unifyPaths(paths_);
  41879. const {cwd} = this.options;
  41880. paths.forEach((path) => {
  41881. // convert to absolute path unless relative path already matches
  41882. if (!sysPath.isAbsolute(path) && !this._closers.has(path)) {
  41883. if (cwd) path = sysPath.join(cwd, path);
  41884. path = sysPath.resolve(path);
  41885. }
  41886. this._closePath(path);
  41887. this._ignoredPaths.add(path);
  41888. if (this._watched.has(path)) {
  41889. this._ignoredPaths.add(path + SLASH_GLOBSTAR);
  41890. }
  41891. // reset the cached userIgnored anymatch fn
  41892. // to make ignoredPaths changes effective
  41893. this._userIgnored = undefined;
  41894. });
  41895. return this;
  41896. }
  41897. /**
  41898. * Close watchers and remove all listeners from watched paths.
  41899. * @returns {Promise<void>}.
  41900. */
  41901. close() {
  41902. if (this.closed) return this._closePromise;
  41903. this.closed = true;
  41904. // Memory management.
  41905. this.removeAllListeners();
  41906. const closers = [];
  41907. this._closers.forEach(closerList => closerList.forEach(closer => {
  41908. const promise = closer();
  41909. if (promise instanceof Promise) closers.push(promise);
  41910. }));
  41911. this._streams.forEach(stream => stream.destroy());
  41912. this._userIgnored = undefined;
  41913. this._readyCount = 0;
  41914. this._readyEmitted = false;
  41915. this._watched.forEach(dirent => dirent.dispose());
  41916. ['closers', 'watched', 'streams', 'symlinkPaths', 'throttled'].forEach(key => {
  41917. this[`_${key}`].clear();
  41918. });
  41919. this._closePromise = closers.length ? Promise.all(closers).then(() => undefined) : Promise.resolve();
  41920. return this._closePromise;
  41921. }
  41922. /**
  41923. * Expose list of watched paths
  41924. * @returns {Object} for chaining
  41925. */
  41926. getWatched() {
  41927. const watchList = {};
  41928. this._watched.forEach((entry, dir) => {
  41929. const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
  41930. watchList[key || ONE_DOT] = entry.getChildren().sort();
  41931. });
  41932. return watchList;
  41933. }
  41934. emitWithAll(event, args) {
  41935. this.emit(...args);
  41936. if (event !== EV_ERROR) this.emit(EV_ALL, ...args);
  41937. }
  41938. // Common helpers
  41939. // --------------
  41940. /**
  41941. * Normalize and emit events.
  41942. * Calling _emit DOES NOT MEAN emit() would be called!
  41943. * @param {EventName} event Type of event
  41944. * @param {Path} path File or directory path
  41945. * @param {*=} val1 arguments to be passed with event
  41946. * @param {*=} val2
  41947. * @param {*=} val3
  41948. * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  41949. */
  41950. async _emit(event, path, val1, val2, val3) {
  41951. if (this.closed) return;
  41952. const opts = this.options;
  41953. if (isWindows$1) path = sysPath.normalize(path);
  41954. if (opts.cwd) path = sysPath.relative(opts.cwd, path);
  41955. /** @type Array<any> */
  41956. const args = [event, path];
  41957. if (val3 !== undefined) args.push(val1, val2, val3);
  41958. else if (val2 !== undefined) args.push(val1, val2);
  41959. else if (val1 !== undefined) args.push(val1);
  41960. const awf = opts.awaitWriteFinish;
  41961. let pw;
  41962. if (awf && (pw = this._pendingWrites.get(path))) {
  41963. pw.lastChange = new Date();
  41964. return this;
  41965. }
  41966. if (opts.atomic) {
  41967. if (event === EV_UNLINK) {
  41968. this._pendingUnlinks.set(path, args);
  41969. setTimeout(() => {
  41970. this._pendingUnlinks.forEach((entry, path) => {
  41971. this.emit(...entry);
  41972. this.emit(EV_ALL, ...entry);
  41973. this._pendingUnlinks.delete(path);
  41974. });
  41975. }, typeof opts.atomic === 'number' ? opts.atomic : 100);
  41976. return this;
  41977. }
  41978. if (event === EV_ADD && this._pendingUnlinks.has(path)) {
  41979. event = args[0] = EV_CHANGE;
  41980. this._pendingUnlinks.delete(path);
  41981. }
  41982. }
  41983. if (awf && (event === EV_ADD || event === EV_CHANGE) && this._readyEmitted) {
  41984. const awfEmit = (err, stats) => {
  41985. if (err) {
  41986. event = args[0] = EV_ERROR;
  41987. args[1] = err;
  41988. this.emitWithAll(event, args);
  41989. } else if (stats) {
  41990. // if stats doesn't exist the file must have been deleted
  41991. if (args.length > 2) {
  41992. args[2] = stats;
  41993. } else {
  41994. args.push(stats);
  41995. }
  41996. this.emitWithAll(event, args);
  41997. }
  41998. };
  41999. this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
  42000. return this;
  42001. }
  42002. if (event === EV_CHANGE) {
  42003. const isThrottled = !this._throttle(EV_CHANGE, path, 50);
  42004. if (isThrottled) return this;
  42005. }
  42006. if (opts.alwaysStat && val1 === undefined &&
  42007. (event === EV_ADD || event === EV_ADD_DIR || event === EV_CHANGE)
  42008. ) {
  42009. const fullPath = opts.cwd ? sysPath.join(opts.cwd, path) : path;
  42010. let stats;
  42011. try {
  42012. stats = await stat(fullPath);
  42013. } catch (err) {}
  42014. // Suppress event when fs_stat fails, to avoid sending undefined 'stat'
  42015. if (!stats || this.closed) return;
  42016. args.push(stats);
  42017. }
  42018. this.emitWithAll(event, args);
  42019. return this;
  42020. }
  42021. /**
  42022. * Common handler for errors
  42023. * @param {Error} error
  42024. * @returns {Error|Boolean} The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  42025. */
  42026. _handleError(error) {
  42027. const code = error && error.code;
  42028. if (error && code !== 'ENOENT' && code !== 'ENOTDIR' &&
  42029. (!this.options.ignorePermissionErrors || (code !== 'EPERM' && code !== 'EACCES'))
  42030. ) {
  42031. this.emit(EV_ERROR, error);
  42032. }
  42033. return error || this.closed;
  42034. }
  42035. /**
  42036. * Helper utility for throttling
  42037. * @param {ThrottleType} actionType type being throttled
  42038. * @param {Path} path being acted upon
  42039. * @param {Number} timeout duration of time to suppress duplicate actions
  42040. * @returns {Object|false} tracking object or false if action should be suppressed
  42041. */
  42042. _throttle(actionType, path, timeout) {
  42043. if (!this._throttled.has(actionType)) {
  42044. this._throttled.set(actionType, new Map());
  42045. }
  42046. /** @type {Map<Path, Object>} */
  42047. const action = this._throttled.get(actionType);
  42048. /** @type {Object} */
  42049. const actionPath = action.get(path);
  42050. if (actionPath) {
  42051. actionPath.count++;
  42052. return false;
  42053. }
  42054. let timeoutObject;
  42055. const clear = () => {
  42056. const item = action.get(path);
  42057. const count = item ? item.count : 0;
  42058. action.delete(path);
  42059. clearTimeout(timeoutObject);
  42060. if (item) clearTimeout(item.timeoutObject);
  42061. return count;
  42062. };
  42063. timeoutObject = setTimeout(clear, timeout);
  42064. const thr = {timeoutObject, clear, count: 0};
  42065. action.set(path, thr);
  42066. return thr;
  42067. }
  42068. _incrReadyCount() {
  42069. return this._readyCount++;
  42070. }
  42071. /**
  42072. * Awaits write operation to finish.
  42073. * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
  42074. * @param {Path} path being acted upon
  42075. * @param {Number} threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
  42076. * @param {EventName} event
  42077. * @param {Function} awfEmit Callback to be called when ready for event to be emitted.
  42078. */
  42079. _awaitWriteFinish(path, threshold, event, awfEmit) {
  42080. let timeoutHandler;
  42081. let fullPath = path;
  42082. if (this.options.cwd && !sysPath.isAbsolute(path)) {
  42083. fullPath = sysPath.join(this.options.cwd, path);
  42084. }
  42085. const now = new Date();
  42086. const awaitWriteFinish = (prevStat) => {
  42087. fs$5.stat(fullPath, (err, curStat) => {
  42088. if (err || !this._pendingWrites.has(path)) {
  42089. if (err && err.code !== 'ENOENT') awfEmit(err);
  42090. return;
  42091. }
  42092. const now = Number(new Date());
  42093. if (prevStat && curStat.size !== prevStat.size) {
  42094. this._pendingWrites.get(path).lastChange = now;
  42095. }
  42096. const pw = this._pendingWrites.get(path);
  42097. const df = now - pw.lastChange;
  42098. if (df >= threshold) {
  42099. this._pendingWrites.delete(path);
  42100. awfEmit(undefined, curStat);
  42101. } else {
  42102. timeoutHandler = setTimeout(
  42103. awaitWriteFinish,
  42104. this.options.awaitWriteFinish.pollInterval,
  42105. curStat
  42106. );
  42107. }
  42108. });
  42109. };
  42110. if (!this._pendingWrites.has(path)) {
  42111. this._pendingWrites.set(path, {
  42112. lastChange: now,
  42113. cancelWait: () => {
  42114. this._pendingWrites.delete(path);
  42115. clearTimeout(timeoutHandler);
  42116. return event;
  42117. }
  42118. });
  42119. timeoutHandler = setTimeout(
  42120. awaitWriteFinish,
  42121. this.options.awaitWriteFinish.pollInterval
  42122. );
  42123. }
  42124. }
  42125. _getGlobIgnored() {
  42126. return [...this._ignoredPaths.values()];
  42127. }
  42128. /**
  42129. * Determines whether user has asked to ignore this path.
  42130. * @param {Path} path filepath or dir
  42131. * @param {fs.Stats=} stats result of fs.stat
  42132. * @returns {Boolean}
  42133. */
  42134. _isIgnored(path, stats) {
  42135. if (this.options.atomic && DOT_RE.test(path)) return true;
  42136. if (!this._userIgnored) {
  42137. const {cwd} = this.options;
  42138. const ign = this.options.ignored;
  42139. const ignored = ign && ign.map(normalizeIgnored(cwd));
  42140. const paths = arrify(ignored)
  42141. .filter((path) => typeof path === STRING_TYPE && !isGlob(path))
  42142. .map((path) => path + SLASH_GLOBSTAR);
  42143. const list = this._getGlobIgnored().map(normalizeIgnored(cwd)).concat(ignored, paths);
  42144. this._userIgnored = anymatch(list, undefined, ANYMATCH_OPTS);
  42145. }
  42146. return this._userIgnored([path, stats]);
  42147. }
  42148. _isntIgnored(path, stat) {
  42149. return !this._isIgnored(path, stat);
  42150. }
  42151. /**
  42152. * Provides a set of common helpers and properties relating to symlink and glob handling.
  42153. * @param {Path} path file, directory, or glob pattern being watched
  42154. * @param {Number=} depth at any depth > 0, this isn't a glob
  42155. * @returns {WatchHelper} object containing helpers for this path
  42156. */
  42157. _getWatchHelpers(path, depth) {
  42158. const watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
  42159. const follow = this.options.followSymlinks;
  42160. return new WatchHelper(path, watchPath, follow, this);
  42161. }
  42162. // Directory helpers
  42163. // -----------------
  42164. /**
  42165. * Provides directory tracking objects
  42166. * @param {String} directory path of the directory
  42167. * @returns {DirEntry} the directory's tracking object
  42168. */
  42169. _getWatchedDir(directory) {
  42170. if (!this._boundRemove) this._boundRemove = this._remove.bind(this);
  42171. const dir = sysPath.resolve(directory);
  42172. if (!this._watched.has(dir)) this._watched.set(dir, new DirEntry(dir, this._boundRemove));
  42173. return this._watched.get(dir);
  42174. }
  42175. // File helpers
  42176. // ------------
  42177. /**
  42178. * Check for read permissions.
  42179. * Based on this answer on SO: https://stackoverflow.com/a/11781404/1358405
  42180. * @param {fs.Stats} stats - object, result of fs_stat
  42181. * @returns {Boolean} indicates whether the file can be read
  42182. */
  42183. _hasReadPermissions(stats) {
  42184. if (this.options.ignorePermissionErrors) return true;
  42185. // stats.mode may be bigint
  42186. const md = stats && Number.parseInt(stats.mode, 10);
  42187. const st = md & 0o777;
  42188. const it = Number.parseInt(st.toString(8)[0], 10);
  42189. return Boolean(4 & it);
  42190. }
  42191. /**
  42192. * Handles emitting unlink events for
  42193. * files and directories, and via recursion, for
  42194. * files and directories within directories that are unlinked
  42195. * @param {String} directory within which the following item is located
  42196. * @param {String} item base path of item/directory
  42197. * @returns {void}
  42198. */
  42199. _remove(directory, item, isDirectory) {
  42200. // if what is being deleted is a directory, get that directory's paths
  42201. // for recursive deleting and cleaning of watched object
  42202. // if it is not a directory, nestedDirectoryChildren will be empty array
  42203. const path = sysPath.join(directory, item);
  42204. const fullPath = sysPath.resolve(path);
  42205. isDirectory = isDirectory != null
  42206. ? isDirectory
  42207. : this._watched.has(path) || this._watched.has(fullPath);
  42208. // prevent duplicate handling in case of arriving here nearly simultaneously
  42209. // via multiple paths (such as _handleFile and _handleDir)
  42210. if (!this._throttle('remove', path, 100)) return;
  42211. // if the only watched file is removed, watch for its return
  42212. if (!isDirectory && !this.options.useFsEvents && this._watched.size === 1) {
  42213. this.add(directory, item, true);
  42214. }
  42215. // This will create a new entry in the watched object in either case
  42216. // so we got to do the directory check beforehand
  42217. const wp = this._getWatchedDir(path);
  42218. const nestedDirectoryChildren = wp.getChildren();
  42219. // Recursively remove children directories / files.
  42220. nestedDirectoryChildren.forEach(nested => this._remove(path, nested));
  42221. // Check if item was on the watched list and remove it
  42222. const parent = this._getWatchedDir(directory);
  42223. const wasTracked = parent.has(item);
  42224. parent.remove(item);
  42225. // Fixes issue #1042 -> Relative paths were detected and added as symlinks
  42226. // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
  42227. // but never removed from the map in case the path was deleted.
  42228. // This leads to an incorrect state if the path was recreated:
  42229. // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
  42230. if (this._symlinkPaths.has(fullPath)) {
  42231. this._symlinkPaths.delete(fullPath);
  42232. }
  42233. // If we wait for this file to be fully written, cancel the wait.
  42234. let relPath = path;
  42235. if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path);
  42236. if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
  42237. const event = this._pendingWrites.get(relPath).cancelWait();
  42238. if (event === EV_ADD) return;
  42239. }
  42240. // The Entry will either be a directory that just got removed
  42241. // or a bogus entry to a file, in either case we have to remove it
  42242. this._watched.delete(path);
  42243. this._watched.delete(fullPath);
  42244. const eventName = isDirectory ? EV_UNLINK_DIR : EV_UNLINK;
  42245. if (wasTracked && !this._isIgnored(path)) this._emit(eventName, path);
  42246. // Avoid conflicts if we later create another file with the same name
  42247. if (!this.options.useFsEvents) {
  42248. this._closePath(path);
  42249. }
  42250. }
  42251. /**
  42252. * Closes all watchers for a path
  42253. * @param {Path} path
  42254. */
  42255. _closePath(path) {
  42256. this._closeFile(path);
  42257. const dir = sysPath.dirname(path);
  42258. this._getWatchedDir(dir).remove(sysPath.basename(path));
  42259. }
  42260. /**
  42261. * Closes only file-specific watchers
  42262. * @param {Path} path
  42263. */
  42264. _closeFile(path) {
  42265. const closers = this._closers.get(path);
  42266. if (!closers) return;
  42267. closers.forEach(closer => closer());
  42268. this._closers.delete(path);
  42269. }
  42270. /**
  42271. *
  42272. * @param {Path} path
  42273. * @param {Function} closer
  42274. */
  42275. _addPathCloser(path, closer) {
  42276. if (!closer) return;
  42277. let list = this._closers.get(path);
  42278. if (!list) {
  42279. list = [];
  42280. this._closers.set(path, list);
  42281. }
  42282. list.push(closer);
  42283. }
  42284. _readdirp(root, opts) {
  42285. if (this.closed) return;
  42286. const options = {type: EV_ALL, alwaysStat: true, lstat: true, ...opts};
  42287. let stream = readdirp(root, options);
  42288. this._streams.add(stream);
  42289. stream.once(STR_CLOSE, () => {
  42290. stream = undefined;
  42291. });
  42292. stream.once(STR_END, () => {
  42293. if (stream) {
  42294. this._streams.delete(stream);
  42295. stream = undefined;
  42296. }
  42297. });
  42298. return stream;
  42299. }
  42300. }
  42301. // Export FSWatcher class
  42302. chokidar.FSWatcher = FSWatcher;
  42303. /**
  42304. * Instantiates watcher with paths to be tracked.
  42305. * @param {String|Array<String>} paths file/directory paths and/or globs
  42306. * @param {Object=} options chokidar opts
  42307. * @returns an instance of FSWatcher for chaining.
  42308. */
  42309. const watch = (paths, options) => {
  42310. const watcher = new FSWatcher(options);
  42311. watcher.add(paths);
  42312. return watcher;
  42313. };
  42314. chokidar.watch = watch;
  42315. var shellQuote$1 = {};
  42316. var quote = function quote(xs) {
  42317. return xs.map(function (s) {
  42318. if (s && typeof s === 'object') {
  42319. return s.op.replace(/(.)/g, '\\$1');
  42320. }
  42321. if ((/["\s]/).test(s) && !(/'/).test(s)) {
  42322. return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
  42323. }
  42324. if ((/["'\s]/).test(s)) {
  42325. return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
  42326. }
  42327. return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, '$1\\$2');
  42328. }).join(' ');
  42329. };
  42330. // '<(' is process substitution operator and
  42331. // can be parsed the same as control operator
  42332. var CONTROL = '(?:' + [
  42333. '\\|\\|',
  42334. '\\&\\&',
  42335. ';;',
  42336. '\\|\\&',
  42337. '\\<\\(',
  42338. '\\<\\<\\<',
  42339. '>>',
  42340. '>\\&',
  42341. '<\\&',
  42342. '[&;()|<>]'
  42343. ].join('|') + ')';
  42344. var controlRE = new RegExp('^' + CONTROL + '$');
  42345. var META = '|&;()<> \\t';
  42346. var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
  42347. var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
  42348. var hash = /^#$/;
  42349. var SQ = "'";
  42350. var DQ = '"';
  42351. var DS = '$';
  42352. var TOKEN = '';
  42353. var mult = 0x100000000; // Math.pow(16, 8);
  42354. for (var i = 0; i < 4; i++) {
  42355. TOKEN += (mult * Math.random()).toString(16);
  42356. }
  42357. var startsWithToken = new RegExp('^' + TOKEN);
  42358. function matchAll(s, r) {
  42359. var origIndex = r.lastIndex;
  42360. var matches = [];
  42361. var matchObj;
  42362. while ((matchObj = r.exec(s))) {
  42363. matches.push(matchObj);
  42364. if (r.lastIndex === matchObj.index) {
  42365. r.lastIndex += 1;
  42366. }
  42367. }
  42368. r.lastIndex = origIndex;
  42369. return matches;
  42370. }
  42371. function getVar(env, pre, key) {
  42372. var r = typeof env === 'function' ? env(key) : env[key];
  42373. if (typeof r === 'undefined' && key != '') {
  42374. r = '';
  42375. } else if (typeof r === 'undefined') {
  42376. r = '$';
  42377. }
  42378. if (typeof r === 'object') {
  42379. return pre + TOKEN + JSON.stringify(r) + TOKEN;
  42380. }
  42381. return pre + r;
  42382. }
  42383. function parseInternal(string, env, opts) {
  42384. if (!opts) {
  42385. opts = {};
  42386. }
  42387. var BS = opts.escape || '\\';
  42388. var BAREWORD = '(\\' + BS + '[\'"' + META + ']|[^\\s\'"' + META + '])+';
  42389. var chunker = new RegExp([
  42390. '(' + CONTROL + ')', // control chars
  42391. '(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
  42392. ].join('|'), 'g');
  42393. var matches = matchAll(string, chunker);
  42394. if (matches.length === 0) {
  42395. return [];
  42396. }
  42397. if (!env) {
  42398. env = {};
  42399. }
  42400. var commented = false;
  42401. return matches.map(function (match) {
  42402. var s = match[0];
  42403. if (!s || commented) {
  42404. return void undefined;
  42405. }
  42406. if (controlRE.test(s)) {
  42407. return { op: s };
  42408. }
  42409. // Hand-written scanner/parser for Bash quoting rules:
  42410. //
  42411. // 1. inside single quotes, all characters are printed literally.
  42412. // 2. inside double quotes, all characters are printed literally
  42413. // except variables prefixed by '$' and backslashes followed by
  42414. // either a double quote or another backslash.
  42415. // 3. outside of any quotes, backslashes are treated as escape
  42416. // characters and not printed (unless they are themselves escaped)
  42417. // 4. quote context can switch mid-token if there is no whitespace
  42418. // between the two quote contexts (e.g. all'one'"token" parses as
  42419. // "allonetoken")
  42420. var quote = false;
  42421. var esc = false;
  42422. var out = '';
  42423. var isGlob = false;
  42424. var i;
  42425. function parseEnvVar() {
  42426. i += 1;
  42427. var varend;
  42428. var varname;
  42429. var char = s.charAt(i);
  42430. if (char === '{') {
  42431. i += 1;
  42432. if (s.charAt(i) === '}') {
  42433. throw new Error('Bad substitution: ' + s.slice(i - 2, i + 1));
  42434. }
  42435. varend = s.indexOf('}', i);
  42436. if (varend < 0) {
  42437. throw new Error('Bad substitution: ' + s.slice(i));
  42438. }
  42439. varname = s.slice(i, varend);
  42440. i = varend;
  42441. } else if ((/[*@#?$!_-]/).test(char)) {
  42442. varname = char;
  42443. i += 1;
  42444. } else {
  42445. var slicedFromI = s.slice(i);
  42446. varend = slicedFromI.match(/[^\w\d_]/);
  42447. if (!varend) {
  42448. varname = slicedFromI;
  42449. i = s.length;
  42450. } else {
  42451. varname = slicedFromI.slice(0, varend.index);
  42452. i += varend.index - 1;
  42453. }
  42454. }
  42455. return getVar(env, '', varname);
  42456. }
  42457. for (i = 0; i < s.length; i++) {
  42458. var c = s.charAt(i);
  42459. isGlob = isGlob || (!quote && (c === '*' || c === '?'));
  42460. if (esc) {
  42461. out += c;
  42462. esc = false;
  42463. } else if (quote) {
  42464. if (c === quote) {
  42465. quote = false;
  42466. } else if (quote == SQ) {
  42467. out += c;
  42468. } else { // Double quote
  42469. if (c === BS) {
  42470. i += 1;
  42471. c = s.charAt(i);
  42472. if (c === DQ || c === BS || c === DS) {
  42473. out += c;
  42474. } else {
  42475. out += BS + c;
  42476. }
  42477. } else if (c === DS) {
  42478. out += parseEnvVar();
  42479. } else {
  42480. out += c;
  42481. }
  42482. }
  42483. } else if (c === DQ || c === SQ) {
  42484. quote = c;
  42485. } else if (controlRE.test(c)) {
  42486. return { op: s };
  42487. } else if (hash.test(c)) {
  42488. commented = true;
  42489. var commentObj = { comment: string.slice(match.index + i + 1) };
  42490. if (out.length) {
  42491. return [out, commentObj];
  42492. }
  42493. return [commentObj];
  42494. } else if (c === BS) {
  42495. esc = true;
  42496. } else if (c === DS) {
  42497. out += parseEnvVar();
  42498. } else {
  42499. out += c;
  42500. }
  42501. }
  42502. if (isGlob) {
  42503. return { op: 'glob', pattern: out };
  42504. }
  42505. return out;
  42506. }).reduce(function (prev, arg) { // finalize parsed arguments
  42507. // TODO: replace this whole reduce with a concat
  42508. return typeof arg === 'undefined' ? prev : prev.concat(arg);
  42509. }, []);
  42510. }
  42511. var parse$6 = function parse(s, env, opts) {
  42512. var mapped = parseInternal(s, env, opts);
  42513. if (typeof env !== 'function') {
  42514. return mapped;
  42515. }
  42516. return mapped.reduce(function (acc, s) {
  42517. if (typeof s === 'object') {
  42518. return acc.concat(s);
  42519. }
  42520. var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
  42521. if (xs.length === 1) {
  42522. return acc.concat(xs[0]);
  42523. }
  42524. return acc.concat(xs.filter(Boolean).map(function (x) {
  42525. if (startsWithToken.test(x)) {
  42526. return JSON.parse(x.split(TOKEN)[1]);
  42527. }
  42528. return x;
  42529. }));
  42530. }, []);
  42531. };
  42532. shellQuote$1.quote = quote;
  42533. shellQuote$1.parse = parse$6;
  42534. var osx = {
  42535. '/Applications/Atom.app/Contents/MacOS/Atom': 'atom',
  42536. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta':
  42537. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta',
  42538. '/Applications/Brackets.app/Contents/MacOS/Brackets': 'brackets',
  42539. '/Applications/Sublime Text.app/Contents/MacOS/Sublime Text':
  42540. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42541. '/Applications/Sublime Text.app/Contents/MacOS/sublime_text':
  42542. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42543. '/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2':
  42544. '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl',
  42545. '/Applications/Sublime Text Dev.app/Contents/MacOS/Sublime Text':
  42546. '/Applications/Sublime Text Dev.app/Contents/SharedSupport/bin/subl',
  42547. '/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code',
  42548. '/Applications/Visual Studio Code - Insiders.app/Contents/MacOS/Electron':
  42549. 'code-insiders',
  42550. '/Applications/VSCodium.app/Contents/MacOS/Electron': 'codium',
  42551. '/Applications/AppCode.app/Contents/MacOS/appcode':
  42552. '/Applications/AppCode.app/Contents/MacOS/appcode',
  42553. '/Applications/CLion.app/Contents/MacOS/clion':
  42554. '/Applications/CLion.app/Contents/MacOS/clion',
  42555. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea':
  42556. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea',
  42557. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm':
  42558. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm',
  42559. '/Applications/PyCharm.app/Contents/MacOS/pycharm':
  42560. '/Applications/PyCharm.app/Contents/MacOS/pycharm',
  42561. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm':
  42562. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm',
  42563. '/Applications/RubyMine.app/Contents/MacOS/rubymine':
  42564. '/Applications/RubyMine.app/Contents/MacOS/rubymine',
  42565. '/Applications/WebStorm.app/Contents/MacOS/webstorm':
  42566. '/Applications/WebStorm.app/Contents/MacOS/webstorm',
  42567. '/Applications/MacVim.app/Contents/MacOS/MacVim': 'mvim',
  42568. '/Applications/GoLand.app/Contents/MacOS/goland':
  42569. '/Applications/GoLand.app/Contents/MacOS/goland',
  42570. '/Applications/Rider.app/Contents/MacOS/rider':
  42571. '/Applications/Rider.app/Contents/MacOS/rider'
  42572. };
  42573. var linux = {
  42574. atom: 'atom',
  42575. Brackets: 'brackets',
  42576. 'code-insiders': 'code-insiders',
  42577. code: 'code',
  42578. vscodium: 'vscodium',
  42579. codium: 'codium',
  42580. emacs: 'emacs',
  42581. gvim: 'gvim',
  42582. 'idea.sh': 'idea',
  42583. 'phpstorm.sh': 'phpstorm',
  42584. 'pycharm.sh': 'pycharm',
  42585. 'rubymine.sh': 'rubymine',
  42586. sublime_text: 'subl',
  42587. vim: 'vim',
  42588. 'webstorm.sh': 'webstorm',
  42589. 'goland.sh': 'goland',
  42590. 'rider.sh': 'rider'
  42591. };
  42592. var windows$1 = [
  42593. 'Brackets.exe',
  42594. 'Code.exe',
  42595. 'Code - Insiders.exe',
  42596. 'VSCodium.exe',
  42597. 'atom.exe',
  42598. 'sublime_text.exe',
  42599. 'notepad++.exe',
  42600. 'clion.exe',
  42601. 'clion64.exe',
  42602. 'idea.exe',
  42603. 'idea64.exe',
  42604. 'phpstorm.exe',
  42605. 'phpstorm64.exe',
  42606. 'pycharm.exe',
  42607. 'pycharm64.exe',
  42608. 'rubymine.exe',
  42609. 'rubymine64.exe',
  42610. 'webstorm.exe',
  42611. 'webstorm64.exe',
  42612. 'goland.exe',
  42613. 'goland64.exe',
  42614. 'rider.exe',
  42615. 'rider64.exe'
  42616. ];
  42617. const path$7 = require$$0$4;
  42618. const shellQuote = shellQuote$1;
  42619. const childProcess$2 = require$$2$1;
  42620. // Map from full process name to binary that starts the process
  42621. // We can't just re-use full process name, because it will spawn a new instance
  42622. // of the app every time
  42623. const COMMON_EDITORS_OSX = osx;
  42624. const COMMON_EDITORS_LINUX = linux;
  42625. const COMMON_EDITORS_WIN = windows$1;
  42626. var guess = function guessEditor (specifiedEditor) {
  42627. if (specifiedEditor) {
  42628. return shellQuote.parse(specifiedEditor)
  42629. }
  42630. if (process.env.LAUNCH_EDITOR) {
  42631. return [process.env.LAUNCH_EDITOR]
  42632. }
  42633. if (process.versions.webcontainer) {
  42634. return [process.env.EDITOR || 'code']
  42635. }
  42636. // We can find out which editor is currently running by:
  42637. // `ps x` on macOS and Linux
  42638. // `Get-Process` on Windows
  42639. try {
  42640. if (process.platform === 'darwin') {
  42641. const output = childProcess$2
  42642. .execSync('ps x -o comm=', {
  42643. stdio: ['pipe', 'pipe', 'ignore']
  42644. })
  42645. .toString();
  42646. const processNames = Object.keys(COMMON_EDITORS_OSX);
  42647. const processList = output.split('\n');
  42648. for (let i = 0; i < processNames.length; i++) {
  42649. const processName = processNames[i];
  42650. // Find editor by exact match.
  42651. if (output.indexOf(processName) !== -1) {
  42652. return [COMMON_EDITORS_OSX[processName]]
  42653. }
  42654. const processNameWithoutApplications = processName.replace('/Applications', '');
  42655. // Find editor installation not in /Applications.
  42656. if (output.indexOf(processNameWithoutApplications) !== -1) {
  42657. // Use the CLI command if one is specified
  42658. if (processName !== COMMON_EDITORS_OSX[processName]) {
  42659. return [COMMON_EDITORS_OSX[processName]]
  42660. }
  42661. // Use a partial match to find the running process path. If one is found, use the
  42662. // existing path since it can be running from anywhere.
  42663. const runningProcess = processList.find((procName) => procName.endsWith(processNameWithoutApplications));
  42664. if (runningProcess !== undefined) {
  42665. return [runningProcess]
  42666. }
  42667. }
  42668. }
  42669. } else if (process.platform === 'win32') {
  42670. const output = childProcess$2
  42671. .execSync(
  42672. 'powershell -NoProfile -Command "Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"',
  42673. {
  42674. stdio: ['pipe', 'pipe', 'ignore']
  42675. }
  42676. )
  42677. .toString();
  42678. const runningProcesses = output.split('\r\n');
  42679. for (let i = 0; i < runningProcesses.length; i++) {
  42680. const fullProcessPath = runningProcesses[i].trim();
  42681. const shortProcessName = path$7.basename(fullProcessPath);
  42682. if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
  42683. return [fullProcessPath]
  42684. }
  42685. }
  42686. } else if (process.platform === 'linux') {
  42687. // --no-heading No header line
  42688. // x List all processes owned by you
  42689. // -o comm Need only names column
  42690. const output = childProcess$2
  42691. .execSync('ps x --no-heading -o comm --sort=comm', {
  42692. stdio: ['pipe', 'pipe', 'ignore']
  42693. })
  42694. .toString();
  42695. const processNames = Object.keys(COMMON_EDITORS_LINUX);
  42696. for (let i = 0; i < processNames.length; i++) {
  42697. const processName = processNames[i];
  42698. if (output.indexOf(processName) !== -1) {
  42699. return [COMMON_EDITORS_LINUX[processName]]
  42700. }
  42701. }
  42702. }
  42703. } catch (error) {
  42704. // Ignore...
  42705. }
  42706. // Last resort, use old skool env vars
  42707. if (process.env.VISUAL) {
  42708. return [process.env.VISUAL]
  42709. } else if (process.env.EDITOR) {
  42710. return [process.env.EDITOR]
  42711. }
  42712. return [null]
  42713. };
  42714. const path$6 = require$$0$4;
  42715. // normalize file/line numbers into command line args for specific editors
  42716. var getArgs = function getArgumentsForPosition (
  42717. editor,
  42718. fileName,
  42719. lineNumber,
  42720. columnNumber = 1
  42721. ) {
  42722. const editorBasename = path$6.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
  42723. switch (editorBasename) {
  42724. case 'atom':
  42725. case 'Atom':
  42726. case 'Atom Beta':
  42727. case 'subl':
  42728. case 'sublime':
  42729. case 'sublime_text':
  42730. case 'wstorm':
  42731. case 'charm':
  42732. return [`${fileName}:${lineNumber}:${columnNumber}`]
  42733. case 'notepad++':
  42734. return ['-n' + lineNumber, '-c' + columnNumber, fileName]
  42735. case 'vim':
  42736. case 'mvim':
  42737. return [`+call cursor(${lineNumber}, ${columnNumber})`, fileName]
  42738. case 'joe':
  42739. case 'gvim':
  42740. return ['+' + `${lineNumber}`, fileName]
  42741. case 'emacs':
  42742. case 'emacsclient':
  42743. return [`+${lineNumber}:${columnNumber}`, fileName]
  42744. case 'rmate':
  42745. case 'mate':
  42746. case 'mine':
  42747. return ['--line', lineNumber, fileName]
  42748. case 'code':
  42749. case 'Code':
  42750. case 'code-insiders':
  42751. case 'Code - Insiders':
  42752. case 'codium':
  42753. case 'vscodium':
  42754. case 'VSCodium':
  42755. return ['-r', '-g', `${fileName}:${lineNumber}:${columnNumber}`]
  42756. case 'appcode':
  42757. case 'clion':
  42758. case 'clion64':
  42759. case 'idea':
  42760. case 'idea64':
  42761. case 'phpstorm':
  42762. case 'phpstorm64':
  42763. case 'pycharm':
  42764. case 'pycharm64':
  42765. case 'rubymine':
  42766. case 'rubymine64':
  42767. case 'webstorm':
  42768. case 'webstorm64':
  42769. case 'goland':
  42770. case 'goland64':
  42771. case 'rider':
  42772. case 'rider64':
  42773. return ['--line', lineNumber, '--column', columnNumber, fileName]
  42774. }
  42775. if (process.env.LAUNCH_EDITOR) {
  42776. return [fileName, lineNumber, columnNumber]
  42777. }
  42778. // For all others, drop the lineNumber until we have
  42779. // a mapping above, since providing the lineNumber incorrectly
  42780. // can result in errors or confusing behavior.
  42781. return [fileName]
  42782. };
  42783. /**
  42784. * Copyright (c) 2015-present, Facebook, Inc.
  42785. *
  42786. * This source code is licensed under the MIT license found in the
  42787. * LICENSE file at
  42788. * https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
  42789. *
  42790. * Modified by Yuxi Evan You
  42791. */
  42792. const fs$4 = require$$0__default;
  42793. const os$1 = require$$2;
  42794. const path$5 = require$$0$4;
  42795. const colors = picocolorsExports;
  42796. const childProcess$1 = require$$2$1;
  42797. const guessEditor = guess;
  42798. const getArgumentsForPosition = getArgs;
  42799. function wrapErrorCallback (cb) {
  42800. return (fileName, errorMessage) => {
  42801. console.log();
  42802. console.log(
  42803. colors.red('Could not open ' + path$5.basename(fileName) + ' in the editor.')
  42804. );
  42805. if (errorMessage) {
  42806. if (errorMessage[errorMessage.length - 1] !== '.') {
  42807. errorMessage += '.';
  42808. }
  42809. console.log(
  42810. colors.red('The editor process exited with an error: ' + errorMessage)
  42811. );
  42812. }
  42813. console.log();
  42814. if (cb) cb(fileName, errorMessage);
  42815. }
  42816. }
  42817. function isTerminalEditor (editor) {
  42818. switch (editor) {
  42819. case 'vim':
  42820. case 'emacs':
  42821. case 'nano':
  42822. return true
  42823. }
  42824. return false
  42825. }
  42826. const positionRE = /:(\d+)(:(\d+))?$/;
  42827. function parseFile (file) {
  42828. const fileName = file.replace(positionRE, '');
  42829. const match = file.match(positionRE);
  42830. const lineNumber = match && match[1];
  42831. const columnNumber = match && match[3];
  42832. return {
  42833. fileName,
  42834. lineNumber,
  42835. columnNumber
  42836. }
  42837. }
  42838. let _childProcess = null;
  42839. function launchEditor (file, specifiedEditor, onErrorCallback) {
  42840. const parsed = parseFile(file);
  42841. let { fileName } = parsed;
  42842. const { lineNumber, columnNumber } = parsed;
  42843. if (!fs$4.existsSync(fileName)) {
  42844. return
  42845. }
  42846. if (typeof specifiedEditor === 'function') {
  42847. onErrorCallback = specifiedEditor;
  42848. specifiedEditor = undefined;
  42849. }
  42850. onErrorCallback = wrapErrorCallback(onErrorCallback);
  42851. const [editor, ...args] = guessEditor(specifiedEditor);
  42852. if (!editor) {
  42853. onErrorCallback(fileName, null);
  42854. return
  42855. }
  42856. if (
  42857. process.platform === 'linux' &&
  42858. fileName.startsWith('/mnt/') &&
  42859. /Microsoft/i.test(os$1.release())
  42860. ) {
  42861. // Assume WSL / "Bash on Ubuntu on Windows" is being used, and
  42862. // that the file exists on the Windows file system.
  42863. // `os.release()` is "4.4.0-43-Microsoft" in the current release
  42864. // build of WSL, see: https://github.com/Microsoft/BashOnWindows/issues/423#issuecomment-221627364
  42865. // When a Windows editor is specified, interop functionality can
  42866. // handle the path translation, but only if a relative path is used.
  42867. fileName = path$5.relative('', fileName);
  42868. }
  42869. if (lineNumber) {
  42870. const extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber);
  42871. args.push.apply(args, extraArgs);
  42872. } else {
  42873. args.push(fileName);
  42874. }
  42875. if (_childProcess && isTerminalEditor(editor)) {
  42876. // There's an existing editor process already and it's attached
  42877. // to the terminal, so go kill it. Otherwise two separate editor
  42878. // instances attach to the stdin/stdout which gets confusing.
  42879. _childProcess.kill('SIGKILL');
  42880. }
  42881. if (process.platform === 'win32') {
  42882. // On Windows, launch the editor in a shell because spawn can only
  42883. // launch .exe files.
  42884. _childProcess = childProcess$1.spawn(
  42885. 'cmd.exe',
  42886. ['/C', editor].concat(args),
  42887. { stdio: 'inherit' }
  42888. );
  42889. } else {
  42890. _childProcess = childProcess$1.spawn(editor, args, { stdio: 'inherit' });
  42891. }
  42892. _childProcess.on('exit', function (errorCode) {
  42893. _childProcess = null;
  42894. if (errorCode) {
  42895. onErrorCallback(fileName, '(code ' + errorCode + ')');
  42896. }
  42897. });
  42898. _childProcess.on('error', function (error) {
  42899. onErrorCallback(fileName, error.message);
  42900. });
  42901. }
  42902. var launchEditor_1 = launchEditor;
  42903. const url$2 = require$$0$9;
  42904. const path$4 = require$$0$4;
  42905. const launch = launchEditor_1;
  42906. var launchEditorMiddleware = (specifiedEditor, srcRoot, onErrorCallback) => {
  42907. if (typeof specifiedEditor === 'function') {
  42908. onErrorCallback = specifiedEditor;
  42909. specifiedEditor = undefined;
  42910. }
  42911. if (typeof srcRoot === 'function') {
  42912. onErrorCallback = srcRoot;
  42913. srcRoot = undefined;
  42914. }
  42915. srcRoot = srcRoot || process.cwd();
  42916. return function launchEditorMiddleware (req, res, next) {
  42917. const { file } = url$2.parse(req.url, true).query || {};
  42918. if (!file) {
  42919. res.statusCode = 500;
  42920. res.end(`launch-editor-middleware: required query param "file" is missing.`);
  42921. } else {
  42922. launch(path$4.resolve(srcRoot, file), specifiedEditor, onErrorCallback);
  42923. res.end();
  42924. }
  42925. }
  42926. };
  42927. var launchEditorMiddleware$1 = /*@__PURE__*/getDefaultExportFromCjs(launchEditorMiddleware);
  42928. async function resolveHttpServer({ proxy }, app, httpsOptions) {
  42929. if (!httpsOptions) {
  42930. const { createServer } = await import('node:http');
  42931. return createServer(app);
  42932. }
  42933. // #484 fallback to http1 when proxy is needed.
  42934. if (proxy) {
  42935. const { createServer } = await import('node:https');
  42936. return createServer(httpsOptions, app);
  42937. }
  42938. else {
  42939. const { createSecureServer } = await import('node:http2');
  42940. return createSecureServer({
  42941. // Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM
  42942. // errors on large numbers of requests
  42943. maxSessionMemory: 1000,
  42944. ...httpsOptions,
  42945. allowHTTP1: true,
  42946. },
  42947. // @ts-expect-error TODO: is this correct?
  42948. app);
  42949. }
  42950. }
  42951. async function resolveHttpsConfig(https) {
  42952. if (!https)
  42953. return undefined;
  42954. const [ca, cert, key, pfx] = await Promise.all([
  42955. readFileIfExists(https.ca),
  42956. readFileIfExists(https.cert),
  42957. readFileIfExists(https.key),
  42958. readFileIfExists(https.pfx),
  42959. ]);
  42960. return { ...https, ca, cert, key, pfx };
  42961. }
  42962. async function readFileIfExists(value) {
  42963. if (typeof value === 'string') {
  42964. return fsp.readFile(path$o.resolve(value)).catch(() => value);
  42965. }
  42966. return value;
  42967. }
  42968. async function httpServerStart(httpServer, serverOptions) {
  42969. let { port, strictPort, host, logger } = serverOptions;
  42970. return new Promise((resolve, reject) => {
  42971. const onError = (e) => {
  42972. if (e.code === 'EADDRINUSE') {
  42973. if (strictPort) {
  42974. httpServer.removeListener('error', onError);
  42975. reject(new Error(`Port ${port} is already in use`));
  42976. }
  42977. else {
  42978. logger.info(`Port ${port} is in use, trying another one...`);
  42979. httpServer.listen(++port, host);
  42980. }
  42981. }
  42982. else {
  42983. httpServer.removeListener('error', onError);
  42984. reject(e);
  42985. }
  42986. };
  42987. httpServer.on('error', onError);
  42988. httpServer.listen(port, host, () => {
  42989. httpServer.removeListener('error', onError);
  42990. resolve(port);
  42991. });
  42992. });
  42993. }
  42994. function setClientErrorHandler(server, logger) {
  42995. server.on('clientError', (err, socket) => {
  42996. let msg = '400 Bad Request';
  42997. if (err.code === 'HPE_HEADER_OVERFLOW') {
  42998. msg = '431 Request Header Fields Too Large';
  42999. logger.warn(colors$1.yellow('Server responded with status code 431. ' +
  43000. 'See https://vitejs.dev/guide/troubleshooting.html#_431-request-header-fields-too-large.'));
  43001. }
  43002. if (err.code === 'ECONNRESET' || !socket.writable) {
  43003. return;
  43004. }
  43005. socket.end(`HTTP/1.1 ${msg}\r\n\r\n`);
  43006. });
  43007. }
  43008. // An implementation of fsUtils without caching
  43009. const commonFsUtils = {
  43010. existsSync: fs$l.existsSync,
  43011. isDirectory,
  43012. tryResolveRealFile,
  43013. tryResolveRealFileWithExtensions,
  43014. tryResolveRealFileOrType,
  43015. };
  43016. const cachedFsUtilsMap = new WeakMap();
  43017. function getFsUtils(config) {
  43018. let fsUtils = cachedFsUtilsMap.get(config);
  43019. if (!fsUtils) {
  43020. if (config.command !== 'serve' ||
  43021. config.server.fs.cachedChecks === false ||
  43022. config.server.watch?.ignored ||
  43023. process.versions.pnp) {
  43024. // cached fsUtils is only used in the dev server for now
  43025. // it is enabled by default only when there aren't custom watcher ignored patterns configured
  43026. // and if yarn pnp isn't used
  43027. fsUtils = commonFsUtils;
  43028. }
  43029. else if (!config.resolve.preserveSymlinks &&
  43030. config.root !== getRealPath(config.root)) {
  43031. fsUtils = commonFsUtils;
  43032. }
  43033. else {
  43034. fsUtils = createCachedFsUtils(config);
  43035. }
  43036. cachedFsUtilsMap.set(config, fsUtils);
  43037. }
  43038. return fsUtils;
  43039. }
  43040. function readDirCacheSync(file) {
  43041. let dirents;
  43042. try {
  43043. dirents = fs$l.readdirSync(file, { withFileTypes: true });
  43044. }
  43045. catch {
  43046. return;
  43047. }
  43048. return direntsToDirentMap(dirents);
  43049. }
  43050. function direntsToDirentMap(fsDirents) {
  43051. const dirents = new Map();
  43052. for (const dirent of fsDirents) {
  43053. // We ignore non directory, file, and symlink entries
  43054. const type = dirent.isDirectory()
  43055. ? 'directory'
  43056. : dirent.isSymbolicLink()
  43057. ? 'symlink'
  43058. : dirent.isFile()
  43059. ? 'file'
  43060. : undefined;
  43061. if (type) {
  43062. dirents.set(dirent.name, { type });
  43063. }
  43064. }
  43065. return dirents;
  43066. }
  43067. function ensureFileMaybeSymlinkIsResolved(direntCache, filePath) {
  43068. if (direntCache.type !== 'file_maybe_symlink')
  43069. return;
  43070. const isSymlink = fs$l
  43071. .lstatSync(filePath, { throwIfNoEntry: false })
  43072. ?.isSymbolicLink();
  43073. direntCache.type =
  43074. isSymlink === undefined ? 'error' : isSymlink ? 'symlink' : 'file';
  43075. }
  43076. function pathUntilPart(root, parts, i) {
  43077. let p = root;
  43078. for (let k = 0; k < i; k++)
  43079. p += '/' + parts[k];
  43080. return p;
  43081. }
  43082. function createCachedFsUtils(config) {
  43083. const root = config.root; // root is resolved and normalized, so it doesn't have a trailing slash
  43084. const rootDirPath = `${root}/`;
  43085. const rootCache = { type: 'directory' }; // dirents will be computed lazily
  43086. const getDirentCacheSync = (parts) => {
  43087. let direntCache = rootCache;
  43088. for (let i = 0; i < parts.length; i++) {
  43089. if (direntCache.type === 'directory') {
  43090. let dirPath;
  43091. if (!direntCache.dirents) {
  43092. dirPath = pathUntilPart(root, parts, i);
  43093. const dirents = readDirCacheSync(dirPath);
  43094. if (!dirents) {
  43095. direntCache.type = 'error';
  43096. return;
  43097. }
  43098. direntCache.dirents = dirents;
  43099. }
  43100. const nextDirentCache = direntCache.dirents.get(parts[i]);
  43101. if (!nextDirentCache) {
  43102. return;
  43103. }
  43104. if (nextDirentCache.type === 'directory_maybe_symlink') {
  43105. dirPath ??= pathUntilPart(root, parts, i + 1);
  43106. const isSymlink = fs$l
  43107. .lstatSync(dirPath, { throwIfNoEntry: false })
  43108. ?.isSymbolicLink();
  43109. nextDirentCache.type = isSymlink ? 'symlink' : 'directory';
  43110. }
  43111. direntCache = nextDirentCache;
  43112. }
  43113. else if (direntCache.type === 'symlink') {
  43114. // early return if we encounter a symlink
  43115. return direntCache;
  43116. }
  43117. else if (direntCache.type === 'error') {
  43118. return direntCache;
  43119. }
  43120. else {
  43121. if (i !== parts.length - 1) {
  43122. return;
  43123. }
  43124. if (direntCache.type === 'file_maybe_symlink') {
  43125. ensureFileMaybeSymlinkIsResolved(direntCache, pathUntilPart(root, parts, i));
  43126. return direntCache;
  43127. }
  43128. else if (direntCache.type === 'file') {
  43129. return direntCache;
  43130. }
  43131. else {
  43132. return;
  43133. }
  43134. }
  43135. }
  43136. return direntCache;
  43137. };
  43138. function getDirentCacheFromPath(normalizedFile) {
  43139. // path.posix.normalize may return a path either with / or without /
  43140. if (normalizedFile[normalizedFile.length - 1] === '/') {
  43141. normalizedFile = normalizedFile.slice(0, -1);
  43142. }
  43143. if (normalizedFile === root) {
  43144. return rootCache;
  43145. }
  43146. if (!normalizedFile.startsWith(rootDirPath)) {
  43147. return undefined;
  43148. }
  43149. const pathFromRoot = normalizedFile.slice(rootDirPath.length);
  43150. const parts = pathFromRoot.split('/');
  43151. const direntCache = getDirentCacheSync(parts);
  43152. if (!direntCache || direntCache.type === 'error') {
  43153. return false;
  43154. }
  43155. return direntCache;
  43156. }
  43157. function onPathAdd(file, type) {
  43158. const direntCache = getDirentCacheFromPath(normalizePath$3(path$o.dirname(file)));
  43159. if (direntCache &&
  43160. direntCache.type === 'directory' &&
  43161. direntCache.dirents) {
  43162. direntCache.dirents.set(path$o.basename(file), { type });
  43163. }
  43164. }
  43165. function onPathUnlink(file) {
  43166. const direntCache = getDirentCacheFromPath(normalizePath$3(path$o.dirname(file)));
  43167. if (direntCache &&
  43168. direntCache.type === 'directory' &&
  43169. direntCache.dirents) {
  43170. direntCache.dirents.delete(path$o.basename(file));
  43171. }
  43172. }
  43173. return {
  43174. existsSync(file) {
  43175. if (isInNodeModules$1(file)) {
  43176. return fs$l.existsSync(file);
  43177. }
  43178. const normalizedFile = normalizePath$3(file);
  43179. const direntCache = getDirentCacheFromPath(normalizedFile);
  43180. if (direntCache === undefined ||
  43181. (direntCache && direntCache.type === 'symlink')) {
  43182. // fallback to built-in fs for out-of-root and symlinked files
  43183. return fs$l.existsSync(file);
  43184. }
  43185. return !!direntCache;
  43186. },
  43187. tryResolveRealFile(file, preserveSymlinks) {
  43188. if (isInNodeModules$1(file)) {
  43189. return tryResolveRealFile(file, preserveSymlinks);
  43190. }
  43191. const normalizedFile = normalizePath$3(file);
  43192. const direntCache = getDirentCacheFromPath(normalizedFile);
  43193. if (direntCache === undefined ||
  43194. (direntCache && direntCache.type === 'symlink')) {
  43195. // fallback to built-in fs for out-of-root and symlinked files
  43196. return tryResolveRealFile(file, preserveSymlinks);
  43197. }
  43198. if (!direntCache || direntCache.type === 'directory') {
  43199. return;
  43200. }
  43201. // We can avoid getRealPath even if preserveSymlinks is false because we know it's
  43202. // a file without symlinks in its path
  43203. return normalizedFile;
  43204. },
  43205. tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks) {
  43206. if (isInNodeModules$1(file)) {
  43207. return tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks);
  43208. }
  43209. const normalizedFile = normalizePath$3(file);
  43210. const dirPath = path$o.posix.dirname(normalizedFile);
  43211. const direntCache = getDirentCacheFromPath(dirPath);
  43212. if (direntCache === undefined ||
  43213. (direntCache && direntCache.type === 'symlink')) {
  43214. // fallback to built-in fs for out-of-root and symlinked files
  43215. return tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks);
  43216. }
  43217. if (!direntCache || direntCache.type !== 'directory') {
  43218. return;
  43219. }
  43220. if (!direntCache.dirents) {
  43221. const dirents = readDirCacheSync(dirPath);
  43222. if (!dirents) {
  43223. direntCache.type = 'error';
  43224. return;
  43225. }
  43226. direntCache.dirents = dirents;
  43227. }
  43228. const base = path$o.posix.basename(normalizedFile);
  43229. for (const ext of extensions) {
  43230. const fileName = base + ext;
  43231. const fileDirentCache = direntCache.dirents.get(fileName);
  43232. if (fileDirentCache) {
  43233. const filePath = dirPath + '/' + fileName;
  43234. ensureFileMaybeSymlinkIsResolved(fileDirentCache, filePath);
  43235. if (fileDirentCache.type === 'symlink') {
  43236. // fallback to built-in fs for symlinked files
  43237. return tryResolveRealFile(filePath, preserveSymlinks);
  43238. }
  43239. if (fileDirentCache.type === 'file') {
  43240. return filePath;
  43241. }
  43242. }
  43243. }
  43244. },
  43245. tryResolveRealFileOrType(file, preserveSymlinks) {
  43246. if (isInNodeModules$1(file)) {
  43247. return tryResolveRealFileOrType(file, preserveSymlinks);
  43248. }
  43249. const normalizedFile = normalizePath$3(file);
  43250. const direntCache = getDirentCacheFromPath(normalizedFile);
  43251. if (direntCache === undefined ||
  43252. (direntCache && direntCache.type === 'symlink')) {
  43253. // fallback to built-in fs for out-of-root and symlinked files
  43254. return tryResolveRealFileOrType(file, preserveSymlinks);
  43255. }
  43256. if (!direntCache) {
  43257. return;
  43258. }
  43259. if (direntCache.type === 'directory') {
  43260. return { type: 'directory' };
  43261. }
  43262. // We can avoid getRealPath even if preserveSymlinks is false because we know it's
  43263. // a file without symlinks in its path
  43264. return { path: normalizedFile, type: 'file' };
  43265. },
  43266. isDirectory(dirPath) {
  43267. if (isInNodeModules$1(dirPath)) {
  43268. return isDirectory(dirPath);
  43269. }
  43270. const direntCache = getDirentCacheFromPath(normalizePath$3(dirPath));
  43271. if (direntCache === undefined ||
  43272. (direntCache && direntCache.type === 'symlink')) {
  43273. // fallback to built-in fs for out-of-root and symlinked files
  43274. return isDirectory(dirPath);
  43275. }
  43276. return direntCache && direntCache.type === 'directory';
  43277. },
  43278. initWatcher(watcher) {
  43279. watcher.on('add', (file) => {
  43280. onPathAdd(file, 'file_maybe_symlink');
  43281. });
  43282. watcher.on('addDir', (dir) => {
  43283. onPathAdd(dir, 'directory_maybe_symlink');
  43284. });
  43285. watcher.on('unlink', onPathUnlink);
  43286. watcher.on('unlinkDir', onPathUnlink);
  43287. },
  43288. };
  43289. }
  43290. function tryResolveRealFile(file, preserveSymlinks) {
  43291. const stat = tryStatSync(file);
  43292. if (stat?.isFile())
  43293. return getRealPath(file, preserveSymlinks);
  43294. }
  43295. function tryResolveRealFileWithExtensions(filePath, extensions, preserveSymlinks) {
  43296. for (const ext of extensions) {
  43297. const res = tryResolveRealFile(filePath + ext, preserveSymlinks);
  43298. if (res)
  43299. return res;
  43300. }
  43301. }
  43302. function tryResolveRealFileOrType(file, preserveSymlinks) {
  43303. const fileStat = tryStatSync(file);
  43304. if (fileStat?.isFile()) {
  43305. return { path: getRealPath(file, preserveSymlinks), type: 'file' };
  43306. }
  43307. if (fileStat?.isDirectory()) {
  43308. return { type: 'directory' };
  43309. }
  43310. return;
  43311. }
  43312. function getRealPath(resolved, preserveSymlinks) {
  43313. if (!preserveSymlinks) {
  43314. resolved = safeRealpathSync(resolved);
  43315. }
  43316. return normalizePath$3(resolved);
  43317. }
  43318. function isDirectory(path) {
  43319. const stat = tryStatSync(path);
  43320. return stat?.isDirectory() ?? false;
  43321. }
  43322. /*!
  43323. * etag
  43324. * Copyright(c) 2014-2016 Douglas Christopher Wilson
  43325. * MIT Licensed
  43326. */
  43327. /**
  43328. * Module exports.
  43329. * @public
  43330. */
  43331. var etag_1 = etag;
  43332. /**
  43333. * Module dependencies.
  43334. * @private
  43335. */
  43336. var crypto = require$$3$1;
  43337. var Stats = require$$0__default.Stats;
  43338. /**
  43339. * Module variables.
  43340. * @private
  43341. */
  43342. var toString = Object.prototype.toString;
  43343. /**
  43344. * Generate an entity tag.
  43345. *
  43346. * @param {Buffer|string} entity
  43347. * @return {string}
  43348. * @private
  43349. */
  43350. function entitytag (entity) {
  43351. if (entity.length === 0) {
  43352. // fast-path empty
  43353. return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"'
  43354. }
  43355. // compute hash of entity
  43356. var hash = crypto
  43357. .createHash('sha1')
  43358. .update(entity, 'utf8')
  43359. .digest('base64')
  43360. .substring(0, 27);
  43361. // compute length of entity
  43362. var len = typeof entity === 'string'
  43363. ? Buffer.byteLength(entity, 'utf8')
  43364. : entity.length;
  43365. return '"' + len.toString(16) + '-' + hash + '"'
  43366. }
  43367. /**
  43368. * Create a simple ETag.
  43369. *
  43370. * @param {string|Buffer|Stats} entity
  43371. * @param {object} [options]
  43372. * @param {boolean} [options.weak]
  43373. * @return {String}
  43374. * @public
  43375. */
  43376. function etag (entity, options) {
  43377. if (entity == null) {
  43378. throw new TypeError('argument entity is required')
  43379. }
  43380. // support fs.Stats object
  43381. var isStats = isstats(entity);
  43382. var weak = options && typeof options.weak === 'boolean'
  43383. ? options.weak
  43384. : isStats;
  43385. // validate argument
  43386. if (!isStats && typeof entity !== 'string' && !Buffer.isBuffer(entity)) {
  43387. throw new TypeError('argument entity must be string, Buffer, or fs.Stats')
  43388. }
  43389. // generate entity tag
  43390. var tag = isStats
  43391. ? stattag(entity)
  43392. : entitytag(entity);
  43393. return weak
  43394. ? 'W/' + tag
  43395. : tag
  43396. }
  43397. /**
  43398. * Determine if object is a Stats object.
  43399. *
  43400. * @param {object} obj
  43401. * @return {boolean}
  43402. * @api private
  43403. */
  43404. function isstats (obj) {
  43405. // genuine fs.Stats
  43406. if (typeof Stats === 'function' && obj instanceof Stats) {
  43407. return true
  43408. }
  43409. // quack quack
  43410. return obj && typeof obj === 'object' &&
  43411. 'ctime' in obj && toString.call(obj.ctime) === '[object Date]' &&
  43412. 'mtime' in obj && toString.call(obj.mtime) === '[object Date]' &&
  43413. 'ino' in obj && typeof obj.ino === 'number' &&
  43414. 'size' in obj && typeof obj.size === 'number'
  43415. }
  43416. /**
  43417. * Generate a tag for a stat.
  43418. *
  43419. * @param {object} stat
  43420. * @return {string}
  43421. * @private
  43422. */
  43423. function stattag (stat) {
  43424. var mtime = stat.mtime.getTime().toString(16);
  43425. var size = stat.size.toString(16);
  43426. return '"' + size + '-' + mtime + '"'
  43427. }
  43428. var getEtag = /*@__PURE__*/getDefaultExportFromCjs(etag_1);
  43429. function e(e,n,r){throw new Error(r?`No known conditions for "${n}" specifier in "${e}" package`:`Missing "${n}" specifier in "${e}" package`)}function n(n,i,o,f){let s,u,l=r(n,o),c=function(e){let n=new Set(["default",...e.conditions||[]]);return e.unsafe||n.add(e.require?"require":"import"),e.unsafe||n.add(e.browser?"browser":"node"),n}(f||{}),a=i[l];if(void 0===a){let e,n,r,t;for(t in i)n&&t.length<n.length||("/"===t[t.length-1]&&l.startsWith(t)?(u=l.substring(t.length),n=t):t.length>1&&(r=t.indexOf("*",1),~r&&(e=RegExp("^"+t.substring(0,r)+"(.*)"+t.substring(1+r)).exec(l),e&&e[1]&&(u=e[1],n=t))));a=i[n];}return a||e(n,l),s=t(a,c),s||e(n,l,1),u&&function(e,n){let r,t=0,i=e.length,o=/[*]/g,f=/[/]$/;for(;t<i;t++)e[t]=o.test(r=e[t])?r.replace(o,n):f.test(r)?r+n:r;}(s,u),s}function r(e,n,r){if(e===n||"."===n)return ".";let t=e+"/",i=t.length,o=n.slice(0,i)===t,f=o?n.slice(i):n;return "#"===f[0]?f:o||!r?"./"===f.slice(0,2)?f:"./"+f:f}function t(e,n,r){if(e){if("string"==typeof e)return r&&r.add(e),[e];let i,o;if(Array.isArray(e)){for(o=r||new Set,i=0;i<e.length;i++)t(e[i],n,o);if(!r&&o.size)return [...o]}else for(i in e)if(n.has(i))return t(e[i],n,r)}}function o(e,r,t){let i,o=e.exports;if(o){if("string"==typeof o)o={".":o};else for(i in o){"."!==i[0]&&(o={".":o});break}return n(e.name,o,r||".",t)}}function f(e,r,t){if(e.imports)return n(e.name,e.imports,r,t)}
  43430. const normalizedClientEntry$1 = normalizePath$3(CLIENT_ENTRY);
  43431. const normalizedEnvEntry$1 = normalizePath$3(ENV_ENTRY);
  43432. const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL';
  43433. // special id for paths marked with browser: false
  43434. // https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module
  43435. const browserExternalId = '__vite-browser-external';
  43436. // special id for packages that are optional peer deps
  43437. const optionalPeerDepId = '__vite-optional-peer-dep';
  43438. const subpathImportsPrefix = '#';
  43439. const startsWithWordCharRE = /^\w/;
  43440. const debug$c = createDebugger('vite:resolve-details', {
  43441. onlyWhenFocused: true,
  43442. });
  43443. function resolvePlugin(resolveOptions) {
  43444. const { root, isProduction, asSrc, ssrConfig, preferRelative = false, } = resolveOptions;
  43445. const { target: ssrTarget, noExternal: ssrNoExternal, external: ssrExternal, } = ssrConfig ?? {};
  43446. // In unix systems, absolute paths inside root first needs to be checked as an
  43447. // absolute URL (/root/root/path-to-file) resulting in failed checks before falling
  43448. // back to checking the path as absolute. If /root/root isn't a valid path, we can
  43449. // avoid these checks. Absolute paths inside root are common in user code as many
  43450. // paths are resolved by the user. For example for an alias.
  43451. const rootInRoot = tryStatSync(path$o.join(root, root))?.isDirectory() ?? false;
  43452. return {
  43453. name: 'vite:resolve',
  43454. async resolveId(id, importer, resolveOpts) {
  43455. if (id[0] === '\0' ||
  43456. id.startsWith('virtual:') ||
  43457. // When injected directly in html/client code
  43458. id.startsWith('/virtual:')) {
  43459. return;
  43460. }
  43461. const ssr = resolveOpts?.ssr === true;
  43462. // We need to delay depsOptimizer until here instead of passing it as an option
  43463. // the resolvePlugin because the optimizer is created on server listen during dev
  43464. const depsOptimizer = resolveOptions.getDepsOptimizer?.(ssr);
  43465. if (id.startsWith(browserExternalId)) {
  43466. return id;
  43467. }
  43468. const targetWeb = !ssr || ssrTarget === 'webworker';
  43469. // this is passed by @rollup/plugin-commonjs
  43470. const isRequire = resolveOpts?.custom?.['node-resolve']?.isRequire ?? false;
  43471. // end user can configure different conditions for ssr and client.
  43472. // falls back to client conditions if no ssr conditions supplied
  43473. const ssrConditions = resolveOptions.ssrConfig?.resolve?.conditions ||
  43474. resolveOptions.conditions;
  43475. const options = {
  43476. isRequire,
  43477. ...resolveOptions,
  43478. scan: resolveOpts?.scan ?? resolveOptions.scan,
  43479. conditions: ssr ? ssrConditions : resolveOptions.conditions,
  43480. };
  43481. const resolvedImports = resolveSubpathImports(id, importer, options, targetWeb);
  43482. if (resolvedImports) {
  43483. id = resolvedImports;
  43484. if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
  43485. return id;
  43486. }
  43487. }
  43488. if (importer) {
  43489. if (isTsRequest(importer) ||
  43490. resolveOpts.custom?.depScan?.loader?.startsWith('ts')) {
  43491. options.isFromTsImporter = true;
  43492. }
  43493. else {
  43494. const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang;
  43495. options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`);
  43496. }
  43497. }
  43498. let res;
  43499. // resolve pre-bundled deps requests, these could be resolved by
  43500. // tryFileResolve or /fs/ resolution but these files may not yet
  43501. // exists if we are in the middle of a deps re-processing
  43502. if (asSrc && depsOptimizer?.isOptimizedDepUrl(id)) {
  43503. const optimizedPath = id.startsWith(FS_PREFIX)
  43504. ? fsPathFromId(id)
  43505. : normalizePath$3(path$o.resolve(root, id.slice(1)));
  43506. return optimizedPath;
  43507. }
  43508. // explicit fs paths that starts with /@fs/*
  43509. if (asSrc && id.startsWith(FS_PREFIX)) {
  43510. res = fsPathFromId(id);
  43511. // We don't need to resolve these paths since they are already resolved
  43512. // always return here even if res doesn't exist since /@fs/ is explicit
  43513. // if the file doesn't exist it should be a 404.
  43514. debug$c?.(`[@fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43515. return ensureVersionQuery(res, id, options, depsOptimizer);
  43516. }
  43517. // URL
  43518. // /foo -> /fs-root/foo
  43519. if (asSrc &&
  43520. id[0] === '/' &&
  43521. (rootInRoot || !id.startsWith(withTrailingSlash(root)))) {
  43522. const fsPath = path$o.resolve(root, id.slice(1));
  43523. if ((res = tryFsResolve(fsPath, options))) {
  43524. debug$c?.(`[url] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43525. return ensureVersionQuery(res, id, options, depsOptimizer);
  43526. }
  43527. }
  43528. // relative
  43529. if (id[0] === '.' ||
  43530. ((preferRelative || importer?.endsWith('.html')) &&
  43531. startsWithWordCharRE.test(id))) {
  43532. const basedir = importer ? path$o.dirname(importer) : process.cwd();
  43533. const fsPath = path$o.resolve(basedir, id);
  43534. // handle browser field mapping for relative imports
  43535. const normalizedFsPath = normalizePath$3(fsPath);
  43536. if (depsOptimizer?.isOptimizedDepFile(normalizedFsPath)) {
  43537. // Optimized files could not yet exist in disk, resolve to the full path
  43538. // Inject the current browserHash version if the path doesn't have one
  43539. if (!resolveOptions.isBuild &&
  43540. !DEP_VERSION_RE.test(normalizedFsPath)) {
  43541. const browserHash = optimizedDepInfoFromFile(depsOptimizer.metadata, normalizedFsPath)?.browserHash;
  43542. if (browserHash) {
  43543. return injectQuery(normalizedFsPath, `v=${browserHash}`);
  43544. }
  43545. }
  43546. return normalizedFsPath;
  43547. }
  43548. if (targetWeb &&
  43549. options.mainFields.includes('browser') &&
  43550. (res = tryResolveBrowserMapping(fsPath, importer, options, true))) {
  43551. return res;
  43552. }
  43553. if ((res = tryFsResolve(fsPath, options))) {
  43554. res = ensureVersionQuery(res, id, options, depsOptimizer);
  43555. debug$c?.(`[relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43556. // If this isn't a script imported from a .html file, include side effects
  43557. // hints so the non-used code is properly tree-shaken during build time.
  43558. if (!options.idOnly &&
  43559. !options.scan &&
  43560. options.isBuild &&
  43561. !importer?.endsWith('.html')) {
  43562. const resPkg = findNearestPackageData(path$o.dirname(res), options.packageCache);
  43563. if (resPkg) {
  43564. return {
  43565. id: res,
  43566. moduleSideEffects: resPkg.hasSideEffects(res),
  43567. };
  43568. }
  43569. }
  43570. return res;
  43571. }
  43572. }
  43573. // drive relative fs paths (only windows)
  43574. if (isWindows$5 && id[0] === '/') {
  43575. const basedir = importer ? path$o.dirname(importer) : process.cwd();
  43576. const fsPath = path$o.resolve(basedir, id);
  43577. if ((res = tryFsResolve(fsPath, options))) {
  43578. debug$c?.(`[drive-relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43579. return ensureVersionQuery(res, id, options, depsOptimizer);
  43580. }
  43581. }
  43582. // absolute fs paths
  43583. if (isNonDriveRelativeAbsolutePath(id) &&
  43584. (res = tryFsResolve(id, options))) {
  43585. debug$c?.(`[fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43586. return ensureVersionQuery(res, id, options, depsOptimizer);
  43587. }
  43588. // external
  43589. if (isExternalUrl(id)) {
  43590. return options.idOnly ? id : { id, external: true };
  43591. }
  43592. // data uri: pass through (this only happens during build and will be
  43593. // handled by dedicated plugin)
  43594. if (isDataUrl(id)) {
  43595. return null;
  43596. }
  43597. // bare package imports, perform node resolve
  43598. if (bareImportRE.test(id)) {
  43599. const external = options.shouldExternalize?.(id, importer);
  43600. if (!external &&
  43601. asSrc &&
  43602. depsOptimizer &&
  43603. !options.scan &&
  43604. (res = await tryOptimizedResolve(depsOptimizer, id, importer, options.preserveSymlinks, options.packageCache))) {
  43605. return res;
  43606. }
  43607. if (targetWeb &&
  43608. options.mainFields.includes('browser') &&
  43609. (res = tryResolveBrowserMapping(id, importer, options, false, external))) {
  43610. return res;
  43611. }
  43612. if ((res = tryNodeResolve(id, importer, options, targetWeb, depsOptimizer, ssr, external))) {
  43613. return res;
  43614. }
  43615. // node built-ins.
  43616. // externalize if building for SSR, otherwise redirect to empty module
  43617. if (isBuiltin(id)) {
  43618. if (ssr) {
  43619. if (targetWeb &&
  43620. ssrNoExternal === true &&
  43621. // if both noExternal and external are true, noExternal will take the higher priority and bundle it.
  43622. // only if the id is explicitly listed in external, we will externalize it and skip this error.
  43623. (ssrExternal === true || !ssrExternal?.includes(id))) {
  43624. let message = `Cannot bundle Node.js built-in "${id}"`;
  43625. if (importer) {
  43626. message += ` imported from "${path$o.relative(process.cwd(), importer)}"`;
  43627. }
  43628. message += `. Consider disabling ssr.noExternal or remove the built-in dependency.`;
  43629. this.error(message);
  43630. }
  43631. return options.idOnly
  43632. ? id
  43633. : { id, external: true, moduleSideEffects: false };
  43634. }
  43635. else {
  43636. if (!asSrc) {
  43637. debug$c?.(`externalized node built-in "${id}" to empty module. ` +
  43638. `(imported by: ${colors$1.white(colors$1.dim(importer))})`);
  43639. }
  43640. else if (isProduction) {
  43641. this.warn(`Module "${id}" has been externalized for browser compatibility, imported by "${importer}". ` +
  43642. `See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.`);
  43643. }
  43644. return isProduction
  43645. ? browserExternalId
  43646. : `${browserExternalId}:${id}`;
  43647. }
  43648. }
  43649. }
  43650. debug$c?.(`[fallthrough] ${colors$1.dim(id)}`);
  43651. },
  43652. load(id) {
  43653. if (id.startsWith(browserExternalId)) {
  43654. if (isProduction) {
  43655. return `export default {}`;
  43656. }
  43657. else {
  43658. id = id.slice(browserExternalId.length + 1);
  43659. return `\
  43660. export default new Proxy({}, {
  43661. get(_, key) {
  43662. throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
  43663. }
  43664. })`;
  43665. }
  43666. }
  43667. if (id.startsWith(optionalPeerDepId)) {
  43668. if (isProduction) {
  43669. return `export default {}`;
  43670. }
  43671. else {
  43672. const [, peerDep, parentDep] = id.split(':');
  43673. return `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`;
  43674. }
  43675. }
  43676. },
  43677. };
  43678. }
  43679. function resolveSubpathImports(id, importer, options, targetWeb) {
  43680. if (!importer || !id.startsWith(subpathImportsPrefix))
  43681. return;
  43682. const basedir = path$o.dirname(importer);
  43683. const pkgData = findNearestPackageData(basedir, options.packageCache);
  43684. if (!pkgData)
  43685. return;
  43686. let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1));
  43687. idWithoutPostfix = '#' + idWithoutPostfix;
  43688. let importsPath = resolveExportsOrImports(pkgData.data, idWithoutPostfix, options, targetWeb, 'imports');
  43689. if (importsPath?.[0] === '.') {
  43690. importsPath = path$o.relative(basedir, path$o.join(pkgData.dir, importsPath));
  43691. if (importsPath[0] !== '.') {
  43692. importsPath = `./${importsPath}`;
  43693. }
  43694. }
  43695. return importsPath + postfix;
  43696. }
  43697. function ensureVersionQuery(resolved, id, options, depsOptimizer) {
  43698. if (!options.isBuild &&
  43699. !options.scan &&
  43700. depsOptimizer &&
  43701. !(resolved === normalizedClientEntry$1 || resolved === normalizedEnvEntry$1)) {
  43702. // Ensure that direct imports of node_modules have the same version query
  43703. // as if they would have been imported through a bare import
  43704. // Use the original id to do the check as the resolved id may be the real
  43705. // file path after symlinks resolution
  43706. const isNodeModule = isInNodeModules$1(id) || isInNodeModules$1(resolved);
  43707. if (isNodeModule && !DEP_VERSION_RE.test(resolved)) {
  43708. const versionHash = depsOptimizer.metadata.browserHash;
  43709. if (versionHash && isOptimizable(resolved, depsOptimizer.options)) {
  43710. resolved = injectQuery(resolved, `v=${versionHash}`);
  43711. }
  43712. }
  43713. }
  43714. return resolved;
  43715. }
  43716. function splitFileAndPostfix(path) {
  43717. const file = cleanUrl(path);
  43718. return { file, postfix: path.slice(file.length) };
  43719. }
  43720. function tryFsResolve(fsPath, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43721. // Dependencies like es5-ext use `#` in their paths. We don't support `#` in user
  43722. // source code so we only need to perform the check for dependencies.
  43723. // We don't support `?` in node_modules paths, so we only need to check in this branch.
  43724. const hashIndex = fsPath.indexOf('#');
  43725. if (hashIndex >= 0 && isInNodeModules$1(fsPath)) {
  43726. const queryIndex = fsPath.indexOf('?');
  43727. // We only need to check foo#bar?baz and foo#bar, ignore foo?bar#baz
  43728. if (queryIndex < 0 || queryIndex > hashIndex) {
  43729. const file = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath;
  43730. const res = tryCleanFsResolve(file, options, tryIndex, targetWeb, skipPackageJson);
  43731. if (res)
  43732. return res + fsPath.slice(file.length);
  43733. }
  43734. }
  43735. const { file, postfix } = splitFileAndPostfix(fsPath);
  43736. const res = tryCleanFsResolve(file, options, tryIndex, targetWeb, skipPackageJson);
  43737. if (res)
  43738. return res + postfix;
  43739. }
  43740. const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/;
  43741. const isPossibleTsOutput = (url) => knownTsOutputRE.test(url);
  43742. function tryCleanFsResolve(file, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43743. const { tryPrefix, extensions, preserveSymlinks } = options;
  43744. const fsUtils = options.fsUtils ?? commonFsUtils;
  43745. // Optimization to get the real type or file type (directory, file, other)
  43746. const fileResult = fsUtils.tryResolveRealFileOrType(file, options.preserveSymlinks);
  43747. if (fileResult?.path)
  43748. return fileResult.path;
  43749. let res;
  43750. // If path.dirname is a valid directory, try extensions and ts resolution logic
  43751. const possibleJsToTs = options.isFromTsImporter && isPossibleTsOutput(file);
  43752. if (possibleJsToTs || options.extensions.length || tryPrefix) {
  43753. const dirPath = path$o.dirname(file);
  43754. if (fsUtils.isDirectory(dirPath)) {
  43755. if (possibleJsToTs) {
  43756. // try resolve .js, .mjs, .cjs or .jsx import to typescript file
  43757. const fileExt = path$o.extname(file);
  43758. const fileName = file.slice(0, -fileExt.length);
  43759. if ((res = fsUtils.tryResolveRealFile(fileName + fileExt.replace('js', 'ts'), preserveSymlinks)))
  43760. return res;
  43761. // for .js, also try .tsx
  43762. if (fileExt === '.js' &&
  43763. (res = fsUtils.tryResolveRealFile(fileName + '.tsx', preserveSymlinks)))
  43764. return res;
  43765. }
  43766. if ((res = fsUtils.tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks)))
  43767. return res;
  43768. if (tryPrefix) {
  43769. const prefixed = `${dirPath}/${options.tryPrefix}${path$o.basename(file)}`;
  43770. if ((res = fsUtils.tryResolveRealFile(prefixed, preserveSymlinks)))
  43771. return res;
  43772. if ((res = fsUtils.tryResolveRealFileWithExtensions(prefixed, extensions, preserveSymlinks)))
  43773. return res;
  43774. }
  43775. }
  43776. }
  43777. if (tryIndex && fileResult?.type === 'directory') {
  43778. // Path points to a directory, check for package.json and entry and /index file
  43779. const dirPath = file;
  43780. if (!skipPackageJson) {
  43781. let pkgPath = `${dirPath}/package.json`;
  43782. try {
  43783. if (fsUtils.existsSync(pkgPath)) {
  43784. if (!options.preserveSymlinks) {
  43785. pkgPath = safeRealpathSync(pkgPath);
  43786. }
  43787. // path points to a node package
  43788. const pkg = loadPackageData(pkgPath);
  43789. return resolvePackageEntry(dirPath, pkg, targetWeb, options);
  43790. }
  43791. }
  43792. catch (e) {
  43793. // This check is best effort, so if an entry is not found, skip error for now
  43794. if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== 'ENOENT')
  43795. throw e;
  43796. }
  43797. }
  43798. if ((res = fsUtils.tryResolveRealFileWithExtensions(`${dirPath}/index`, extensions, preserveSymlinks)))
  43799. return res;
  43800. if (tryPrefix) {
  43801. if ((res = fsUtils.tryResolveRealFileWithExtensions(`${dirPath}/${options.tryPrefix}index`, extensions, preserveSymlinks)))
  43802. return res;
  43803. }
  43804. }
  43805. }
  43806. function tryNodeResolve(id, importer, options, targetWeb, depsOptimizer, ssr = false, externalize, allowLinkedExternal = true) {
  43807. const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options;
  43808. // check for deep import, e.g. "my-lib/foo"
  43809. const deepMatch = id.match(deepImportRE);
  43810. // package name doesn't include postfixes
  43811. // trim them to support importing package with queries (e.g. `import css from 'normalize.css?inline'`)
  43812. const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id);
  43813. let basedir;
  43814. if (dedupe?.includes(pkgId)) {
  43815. basedir = root;
  43816. }
  43817. else if (importer &&
  43818. path$o.isAbsolute(importer) &&
  43819. // css processing appends `*` for importer
  43820. (importer[importer.length - 1] === '*' || fs$l.existsSync(cleanUrl(importer)))) {
  43821. basedir = path$o.dirname(importer);
  43822. }
  43823. else {
  43824. basedir = root;
  43825. }
  43826. let selfPkg = null;
  43827. if (!isBuiltin(id) && !id.includes('\0') && bareImportRE.test(id)) {
  43828. // check if it's a self reference dep.
  43829. const selfPackageData = findNearestPackageData(basedir, packageCache);
  43830. selfPkg =
  43831. selfPackageData?.data.exports && selfPackageData?.data.name === pkgId
  43832. ? selfPackageData
  43833. : null;
  43834. }
  43835. const pkg = selfPkg ||
  43836. resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache);
  43837. if (!pkg) {
  43838. // if import can't be found, check if it's an optional peer dep.
  43839. // if so, we can resolve to a special id that errors only when imported.
  43840. if (basedir !== root && // root has no peer dep
  43841. !isBuiltin(id) &&
  43842. !id.includes('\0') &&
  43843. bareImportRE.test(id)) {
  43844. const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data;
  43845. if (mainPkg) {
  43846. const pkgName = getNpmPackageName(id);
  43847. if (pkgName != null &&
  43848. mainPkg.peerDependencies?.[pkgName] &&
  43849. mainPkg.peerDependenciesMeta?.[pkgName]?.optional) {
  43850. return {
  43851. id: `${optionalPeerDepId}:${id}:${mainPkg.name}`,
  43852. };
  43853. }
  43854. }
  43855. }
  43856. return;
  43857. }
  43858. const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry;
  43859. const unresolvedId = deepMatch ? '.' + id.slice(pkgId.length) : id;
  43860. let resolved;
  43861. try {
  43862. resolved = resolveId(unresolvedId, pkg, targetWeb, options);
  43863. }
  43864. catch (err) {
  43865. if (!options.tryEsmOnly) {
  43866. throw err;
  43867. }
  43868. }
  43869. if (!resolved && options.tryEsmOnly) {
  43870. resolved = resolveId(unresolvedId, pkg, targetWeb, {
  43871. ...options,
  43872. isRequire: false,
  43873. mainFields: DEFAULT_MAIN_FIELDS,
  43874. extensions: DEFAULT_EXTENSIONS,
  43875. });
  43876. }
  43877. if (!resolved) {
  43878. return;
  43879. }
  43880. const processResult = (resolved) => {
  43881. if (!externalize) {
  43882. return resolved;
  43883. }
  43884. // don't external symlink packages
  43885. if (!allowLinkedExternal && !isInNodeModules$1(resolved.id)) {
  43886. return resolved;
  43887. }
  43888. const resolvedExt = path$o.extname(resolved.id);
  43889. // don't external non-js imports
  43890. if (resolvedExt &&
  43891. resolvedExt !== '.js' &&
  43892. resolvedExt !== '.mjs' &&
  43893. resolvedExt !== '.cjs') {
  43894. return resolved;
  43895. }
  43896. let resolvedId = id;
  43897. if (deepMatch && !pkg?.data.exports && path$o.extname(id) !== resolvedExt) {
  43898. // id date-fns/locale
  43899. // resolve.id ...date-fns/esm/locale/index.js
  43900. const index = resolved.id.indexOf(id);
  43901. if (index > -1) {
  43902. resolvedId = resolved.id.slice(index);
  43903. debug$c?.(`[processResult] ${colors$1.cyan(id)} -> ${colors$1.dim(resolvedId)}`);
  43904. }
  43905. }
  43906. return { ...resolved, id: resolvedId, external: true };
  43907. };
  43908. if (!options.idOnly &&
  43909. ((!options.scan && isBuild && !depsOptimizer) || externalize)) {
  43910. // Resolve package side effects for build so that rollup can better
  43911. // perform tree-shaking
  43912. return processResult({
  43913. id: resolved,
  43914. moduleSideEffects: pkg.hasSideEffects(resolved),
  43915. });
  43916. }
  43917. if (!options.ssrOptimizeCheck &&
  43918. (!isInNodeModules$1(resolved) || // linked
  43919. !depsOptimizer || // resolving before listening to the server
  43920. options.scan) // initial esbuild scan phase
  43921. ) {
  43922. return { id: resolved };
  43923. }
  43924. // if we reach here, it's a valid dep import that hasn't been optimized.
  43925. const isJsType = depsOptimizer
  43926. ? isOptimizable(resolved, depsOptimizer.options)
  43927. : OPTIMIZABLE_ENTRY_RE.test(resolved);
  43928. let exclude = depsOptimizer?.options.exclude;
  43929. let include = depsOptimizer?.options.include;
  43930. if (options.ssrOptimizeCheck) {
  43931. // we don't have the depsOptimizer
  43932. exclude = options.ssrConfig?.optimizeDeps?.exclude;
  43933. include = options.ssrConfig?.optimizeDeps?.include;
  43934. }
  43935. const skipOptimization = (!options.ssrOptimizeCheck && depsOptimizer?.options.noDiscovery) ||
  43936. !isJsType ||
  43937. (importer && isInNodeModules$1(importer)) ||
  43938. exclude?.includes(pkgId) ||
  43939. exclude?.includes(id) ||
  43940. SPECIAL_QUERY_RE.test(resolved) ||
  43941. // During dev SSR, we don't have a way to reload the module graph if
  43942. // a non-optimized dep is found. So we need to skip optimization here.
  43943. // The only optimized deps are the ones explicitly listed in the config.
  43944. (!options.ssrOptimizeCheck && !isBuild && ssr) ||
  43945. // Only optimize non-external CJS deps during SSR by default
  43946. (ssr &&
  43947. isFilePathESM(resolved, options.packageCache) &&
  43948. !(include?.includes(pkgId) || include?.includes(id)));
  43949. if (options.ssrOptimizeCheck) {
  43950. return {
  43951. id: skipOptimization
  43952. ? injectQuery(resolved, `__vite_skip_optimization`)
  43953. : resolved,
  43954. };
  43955. }
  43956. if (skipOptimization) {
  43957. // excluded from optimization
  43958. // Inject a version query to npm deps so that the browser
  43959. // can cache it without re-validation, but only do so for known js types.
  43960. // otherwise we may introduce duplicated modules for externalized files
  43961. // from pre-bundled deps.
  43962. if (!isBuild) {
  43963. const versionHash = depsOptimizer.metadata.browserHash;
  43964. if (versionHash && isJsType) {
  43965. resolved = injectQuery(resolved, `v=${versionHash}`);
  43966. }
  43967. }
  43968. }
  43969. else {
  43970. // this is a missing import, queue optimize-deps re-run and
  43971. // get a resolved its optimized info
  43972. const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved);
  43973. resolved = depsOptimizer.getOptimizedDepId(optimizedInfo);
  43974. }
  43975. if (!options.idOnly && !options.scan && isBuild) {
  43976. // Resolve package side effects for build so that rollup can better
  43977. // perform tree-shaking
  43978. return {
  43979. id: resolved,
  43980. moduleSideEffects: pkg.hasSideEffects(resolved),
  43981. };
  43982. }
  43983. else {
  43984. return { id: resolved };
  43985. }
  43986. }
  43987. async function tryOptimizedResolve(depsOptimizer, id, importer, preserveSymlinks, packageCache) {
  43988. // TODO: we need to wait until scanning is done here as this function
  43989. // is used in the preAliasPlugin to decide if an aliased dep is optimized,
  43990. // and avoid replacing the bare import with the resolved path.
  43991. // We should be able to remove this in the future
  43992. await depsOptimizer.scanProcessing;
  43993. const metadata = depsOptimizer.metadata;
  43994. const depInfo = optimizedDepInfoFromId(metadata, id);
  43995. if (depInfo) {
  43996. return depsOptimizer.getOptimizedDepId(depInfo);
  43997. }
  43998. if (!importer)
  43999. return;
  44000. // further check if id is imported by nested dependency
  44001. let idPkgDir;
  44002. const nestedIdMatch = `> ${id}`;
  44003. for (const optimizedData of metadata.depInfoList) {
  44004. if (!optimizedData.src)
  44005. continue; // Ignore chunks
  44006. // check where "foo" is nested in "my-lib > foo"
  44007. if (!optimizedData.id.endsWith(nestedIdMatch))
  44008. continue;
  44009. // lazily initialize idPkgDir
  44010. if (idPkgDir == null) {
  44011. const pkgName = getNpmPackageName(id);
  44012. if (!pkgName)
  44013. break;
  44014. idPkgDir = resolvePackageData(pkgName, importer, preserveSymlinks, packageCache)?.dir;
  44015. // if still null, it likely means that this id isn't a dep for importer.
  44016. // break to bail early
  44017. if (idPkgDir == null)
  44018. break;
  44019. idPkgDir = normalizePath$3(idPkgDir);
  44020. }
  44021. // match by src to correctly identify if id belongs to nested dependency
  44022. if (optimizedData.src.startsWith(withTrailingSlash(idPkgDir))) {
  44023. return depsOptimizer.getOptimizedDepId(optimizedData);
  44024. }
  44025. }
  44026. }
  44027. function resolvePackageEntry(id, { dir, data, setResolvedCache, getResolvedCache }, targetWeb, options) {
  44028. const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id);
  44029. const cached = getResolvedCache('.', targetWeb);
  44030. if (cached) {
  44031. return cached + postfix;
  44032. }
  44033. try {
  44034. let entryPoint;
  44035. // resolve exports field with highest priority
  44036. // using https://github.com/lukeed/resolve.exports
  44037. if (data.exports) {
  44038. entryPoint = resolveExportsOrImports(data, '.', options, targetWeb, 'exports');
  44039. }
  44040. // fallback to mainFields if still not resolved
  44041. if (!entryPoint) {
  44042. for (const field of options.mainFields) {
  44043. if (field === 'browser') {
  44044. if (targetWeb) {
  44045. entryPoint = tryResolveBrowserEntry(dir, data, options);
  44046. if (entryPoint) {
  44047. break;
  44048. }
  44049. }
  44050. }
  44051. else if (typeof data[field] === 'string') {
  44052. entryPoint = data[field];
  44053. break;
  44054. }
  44055. }
  44056. }
  44057. entryPoint ||= data.main;
  44058. // try default entry when entry is not define
  44059. // https://nodejs.org/api/modules.html#all-together
  44060. const entryPoints = entryPoint
  44061. ? [entryPoint]
  44062. : ['index.js', 'index.json', 'index.node'];
  44063. for (let entry of entryPoints) {
  44064. // make sure we don't get scripts when looking for sass
  44065. let skipPackageJson = false;
  44066. if (options.mainFields[0] === 'sass' &&
  44067. !options.extensions.includes(path$o.extname(entry))) {
  44068. entry = '';
  44069. skipPackageJson = true;
  44070. }
  44071. else {
  44072. // resolve object browser field in package.json
  44073. const { browser: browserField } = data;
  44074. if (targetWeb &&
  44075. options.mainFields.includes('browser') &&
  44076. isObject$1(browserField)) {
  44077. entry = mapWithBrowserField(entry, browserField) || entry;
  44078. }
  44079. }
  44080. const entryPointPath = path$o.join(dir, entry);
  44081. const resolvedEntryPoint = tryFsResolve(entryPointPath, options, true, true, skipPackageJson);
  44082. if (resolvedEntryPoint) {
  44083. debug$c?.(`[package entry] ${colors$1.cyan(idWithoutPostfix)} -> ${colors$1.dim(resolvedEntryPoint)}${postfix !== '' ? ` (postfix: ${postfix})` : ''}`);
  44084. setResolvedCache('.', resolvedEntryPoint, targetWeb);
  44085. return resolvedEntryPoint + postfix;
  44086. }
  44087. }
  44088. }
  44089. catch (e) {
  44090. packageEntryFailure(id, e.message);
  44091. }
  44092. packageEntryFailure(id);
  44093. }
  44094. function packageEntryFailure(id, details) {
  44095. const err = new Error(`Failed to resolve entry for package "${id}". ` +
  44096. `The package may have incorrect main/module/exports specified in its package.json` +
  44097. (details ? ': ' + details : '.'));
  44098. err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL;
  44099. throw err;
  44100. }
  44101. function resolveExportsOrImports(pkg, key, options, targetWeb, type) {
  44102. const additionalConditions = new Set(options.overrideConditions || [
  44103. 'production',
  44104. 'development',
  44105. 'module',
  44106. ...options.conditions,
  44107. ]);
  44108. const conditions = [...additionalConditions].filter((condition) => {
  44109. switch (condition) {
  44110. case 'production':
  44111. return options.isProduction;
  44112. case 'development':
  44113. return !options.isProduction;
  44114. }
  44115. return true;
  44116. });
  44117. const fn = type === 'imports' ? f : o;
  44118. const result = fn(pkg, key, {
  44119. browser: targetWeb && !additionalConditions.has('node'),
  44120. require: options.isRequire && !additionalConditions.has('import'),
  44121. conditions,
  44122. });
  44123. return result ? result[0] : undefined;
  44124. }
  44125. function resolveDeepImport(id, { webResolvedImports, setResolvedCache, getResolvedCache, dir, data, }, targetWeb, options) {
  44126. const cache = getResolvedCache(id, targetWeb);
  44127. if (cache) {
  44128. return cache;
  44129. }
  44130. let relativeId = id;
  44131. const { exports: exportsField, browser: browserField } = data;
  44132. // map relative based on exports data
  44133. if (exportsField) {
  44134. if (isObject$1(exportsField) && !Array.isArray(exportsField)) {
  44135. // resolve without postfix (see #7098)
  44136. const { file, postfix } = splitFileAndPostfix(relativeId);
  44137. const exportsId = resolveExportsOrImports(data, file, options, targetWeb, 'exports');
  44138. if (exportsId !== undefined) {
  44139. relativeId = exportsId + postfix;
  44140. }
  44141. else {
  44142. relativeId = undefined;
  44143. }
  44144. }
  44145. else {
  44146. // not exposed
  44147. relativeId = undefined;
  44148. }
  44149. if (!relativeId) {
  44150. throw new Error(`Package subpath '${relativeId}' is not defined by "exports" in ` +
  44151. `${path$o.join(dir, 'package.json')}.`);
  44152. }
  44153. }
  44154. else if (targetWeb &&
  44155. options.mainFields.includes('browser') &&
  44156. isObject$1(browserField)) {
  44157. // resolve without postfix (see #7098)
  44158. const { file, postfix } = splitFileAndPostfix(relativeId);
  44159. const mapped = mapWithBrowserField(file, browserField);
  44160. if (mapped) {
  44161. relativeId = mapped + postfix;
  44162. }
  44163. else if (mapped === false) {
  44164. return (webResolvedImports[id] = browserExternalId);
  44165. }
  44166. }
  44167. if (relativeId) {
  44168. const resolved = tryFsResolve(path$o.join(dir, relativeId), options, !exportsField, // try index only if no exports field
  44169. targetWeb);
  44170. if (resolved) {
  44171. debug$c?.(`[node/deep-import] ${colors$1.cyan(id)} -> ${colors$1.dim(resolved)}`);
  44172. setResolvedCache(id, resolved, targetWeb);
  44173. return resolved;
  44174. }
  44175. }
  44176. }
  44177. function tryResolveBrowserMapping(id, importer, options, isFilePath, externalize) {
  44178. let res;
  44179. const pkg = importer &&
  44180. findNearestPackageData(path$o.dirname(importer), options.packageCache);
  44181. if (pkg && isObject$1(pkg.data.browser)) {
  44182. const mapId = isFilePath ? './' + slash$1(path$o.relative(pkg.dir, id)) : id;
  44183. const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser);
  44184. if (browserMappedPath) {
  44185. if ((res = bareImportRE.test(browserMappedPath)
  44186. ? tryNodeResolve(browserMappedPath, importer, options, true)?.id
  44187. : tryFsResolve(path$o.join(pkg.dir, browserMappedPath), options))) {
  44188. debug$c?.(`[browser mapped] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  44189. let result = { id: res };
  44190. if (options.idOnly) {
  44191. return result;
  44192. }
  44193. if (!options.scan && options.isBuild) {
  44194. const resPkg = findNearestPackageData(path$o.dirname(res), options.packageCache);
  44195. if (resPkg) {
  44196. result = {
  44197. id: res,
  44198. moduleSideEffects: resPkg.hasSideEffects(res),
  44199. };
  44200. }
  44201. }
  44202. return externalize ? { ...result, external: true } : result;
  44203. }
  44204. }
  44205. else if (browserMappedPath === false) {
  44206. return browserExternalId;
  44207. }
  44208. }
  44209. }
  44210. function tryResolveBrowserEntry(dir, data, options) {
  44211. // handle edge case with browser and module field semantics
  44212. // check browser field
  44213. // https://github.com/defunctzombie/package-browser-field-spec
  44214. const browserEntry = typeof data.browser === 'string'
  44215. ? data.browser
  44216. : isObject$1(data.browser) && data.browser['.'];
  44217. if (browserEntry) {
  44218. // check if the package also has a "module" field.
  44219. if (!options.isRequire &&
  44220. options.mainFields.includes('module') &&
  44221. typeof data.module === 'string' &&
  44222. data.module !== browserEntry) {
  44223. // if both are present, we may have a problem: some package points both
  44224. // to ESM, with "module" targeting Node.js, while some packages points
  44225. // "module" to browser ESM and "browser" to UMD/IIFE.
  44226. // the heuristics here is to actually read the browser entry when
  44227. // possible and check for hints of ESM. If it is not ESM, prefer "module"
  44228. // instead; Otherwise, assume it's ESM and use it.
  44229. const resolvedBrowserEntry = tryFsResolve(path$o.join(dir, browserEntry), options);
  44230. if (resolvedBrowserEntry) {
  44231. const content = fs$l.readFileSync(resolvedBrowserEntry, 'utf-8');
  44232. if (hasESMSyntax(content)) {
  44233. // likely ESM, prefer browser
  44234. return browserEntry;
  44235. }
  44236. else {
  44237. // non-ESM, UMD or IIFE or CJS(!!! e.g. firebase 7.x), prefer module
  44238. return data.module;
  44239. }
  44240. }
  44241. }
  44242. else {
  44243. return browserEntry;
  44244. }
  44245. }
  44246. }
  44247. /**
  44248. * given a relative path in pkg dir,
  44249. * return a relative path in pkg dir,
  44250. * mapped with the "map" object
  44251. *
  44252. * - Returning `undefined` means there is no browser mapping for this id
  44253. * - Returning `false` means this id is explicitly externalized for browser
  44254. */
  44255. function mapWithBrowserField(relativePathInPkgDir, map) {
  44256. const normalizedPath = path$o.posix.normalize(relativePathInPkgDir);
  44257. for (const key in map) {
  44258. const normalizedKey = path$o.posix.normalize(key);
  44259. if (normalizedPath === normalizedKey ||
  44260. equalWithoutSuffix(normalizedPath, normalizedKey, '.js') ||
  44261. equalWithoutSuffix(normalizedPath, normalizedKey, '/index.js')) {
  44262. return map[key];
  44263. }
  44264. }
  44265. }
  44266. function equalWithoutSuffix(path, key, suffix) {
  44267. return key.endsWith(suffix) && key.slice(0, -suffix.length) === path;
  44268. }
  44269. const externalWithConversionNamespace = 'vite:dep-pre-bundle:external-conversion';
  44270. const convertedExternalPrefix = 'vite-dep-pre-bundle-external:';
  44271. const cjsExternalFacadeNamespace = 'vite:cjs-external-facade';
  44272. const nonFacadePrefix = 'vite-cjs-external-facade:';
  44273. const externalTypes = [
  44274. 'css',
  44275. // supported pre-processor types
  44276. 'less',
  44277. 'sass',
  44278. 'scss',
  44279. 'styl',
  44280. 'stylus',
  44281. 'pcss',
  44282. 'postcss',
  44283. // wasm
  44284. 'wasm',
  44285. // known SFC types
  44286. 'vue',
  44287. 'svelte',
  44288. 'marko',
  44289. 'astro',
  44290. 'imba',
  44291. // JSX/TSX may be configured to be compiled differently from how esbuild
  44292. // handles it by default, so exclude them as well
  44293. 'jsx',
  44294. 'tsx',
  44295. ...KNOWN_ASSET_TYPES,
  44296. ];
  44297. function esbuildDepPlugin(qualified, external, config, ssr) {
  44298. const { extensions } = getDepOptimizationConfig(config, ssr);
  44299. // remove optimizable extensions from `externalTypes` list
  44300. const allExternalTypes = extensions
  44301. ? externalTypes.filter((type) => !extensions?.includes('.' + type))
  44302. : externalTypes;
  44303. // use separate package cache for optimizer as it caches paths around node_modules
  44304. // and it's unlikely for the core Vite process to traverse into node_modules again
  44305. const esmPackageCache = new Map();
  44306. const cjsPackageCache = new Map();
  44307. // default resolver which prefers ESM
  44308. const _resolve = config.createResolver({
  44309. asSrc: false,
  44310. scan: true,
  44311. packageCache: esmPackageCache,
  44312. });
  44313. // cjs resolver that prefers Node
  44314. const _resolveRequire = config.createResolver({
  44315. asSrc: false,
  44316. isRequire: true,
  44317. scan: true,
  44318. packageCache: cjsPackageCache,
  44319. });
  44320. const resolve = (id, importer, kind, resolveDir) => {
  44321. let _importer;
  44322. // explicit resolveDir - this is passed only during yarn pnp resolve for
  44323. // entries
  44324. if (resolveDir) {
  44325. _importer = normalizePath$3(path$o.join(resolveDir, '*'));
  44326. }
  44327. else {
  44328. // map importer ids to file paths for correct resolution
  44329. _importer = importer in qualified ? qualified[importer] : importer;
  44330. }
  44331. const resolver = kind.startsWith('require') ? _resolveRequire : _resolve;
  44332. return resolver(id, _importer, undefined, ssr);
  44333. };
  44334. const resolveResult = (id, resolved) => {
  44335. if (resolved.startsWith(browserExternalId)) {
  44336. return {
  44337. path: id,
  44338. namespace: 'browser-external',
  44339. };
  44340. }
  44341. if (resolved.startsWith(optionalPeerDepId)) {
  44342. return {
  44343. path: resolved,
  44344. namespace: 'optional-peer-dep',
  44345. };
  44346. }
  44347. if (ssr && isBuiltin(resolved)) {
  44348. return;
  44349. }
  44350. if (isExternalUrl(resolved)) {
  44351. return {
  44352. path: resolved,
  44353. external: true,
  44354. };
  44355. }
  44356. return {
  44357. path: path$o.resolve(resolved),
  44358. };
  44359. };
  44360. return {
  44361. name: 'vite:dep-pre-bundle',
  44362. setup(build) {
  44363. // clear package cache when esbuild is finished
  44364. build.onEnd(() => {
  44365. esmPackageCache.clear();
  44366. cjsPackageCache.clear();
  44367. });
  44368. // externalize assets and commonly known non-js file types
  44369. // See #8459 for more details about this require-import conversion
  44370. build.onResolve({
  44371. filter: new RegExp(`\\.(` + allExternalTypes.join('|') + `)(\\?.*)?$`),
  44372. }, async ({ path: id, importer, kind }) => {
  44373. // if the prefix exist, it is already converted to `import`, so set `external: true`
  44374. if (id.startsWith(convertedExternalPrefix)) {
  44375. return {
  44376. path: id.slice(convertedExternalPrefix.length),
  44377. external: true,
  44378. };
  44379. }
  44380. const resolved = await resolve(id, importer, kind);
  44381. if (resolved) {
  44382. if (kind === 'require-call') {
  44383. // here it is not set to `external: true` to convert `require` to `import`
  44384. return {
  44385. path: resolved,
  44386. namespace: externalWithConversionNamespace,
  44387. };
  44388. }
  44389. return {
  44390. path: resolved,
  44391. external: true,
  44392. };
  44393. }
  44394. });
  44395. build.onLoad({ filter: /./, namespace: externalWithConversionNamespace }, (args) => {
  44396. // import itself with prefix (this is the actual part of require-import conversion)
  44397. const modulePath = `"${convertedExternalPrefix}${args.path}"`;
  44398. return {
  44399. contents: isCSSRequest(args.path) && !isModuleCSSRequest(args.path)
  44400. ? `import ${modulePath};`
  44401. : `export { default } from ${modulePath};` +
  44402. `export * from ${modulePath};`,
  44403. loader: 'js',
  44404. };
  44405. });
  44406. function resolveEntry(id) {
  44407. const flatId = flattenId(id);
  44408. if (flatId in qualified) {
  44409. return {
  44410. path: qualified[flatId],
  44411. };
  44412. }
  44413. }
  44414. build.onResolve({ filter: /^[\w@][^:]/ }, async ({ path: id, importer, kind }) => {
  44415. if (moduleListContains(external, id)) {
  44416. return {
  44417. path: id,
  44418. external: true,
  44419. };
  44420. }
  44421. // ensure esbuild uses our resolved entries
  44422. let entry;
  44423. // if this is an entry, return entry namespace resolve result
  44424. if (!importer) {
  44425. if ((entry = resolveEntry(id)))
  44426. return entry;
  44427. // check if this is aliased to an entry - also return entry namespace
  44428. const aliased = await _resolve(id, undefined, true);
  44429. if (aliased && (entry = resolveEntry(aliased))) {
  44430. return entry;
  44431. }
  44432. }
  44433. // use vite's own resolver
  44434. const resolved = await resolve(id, importer, kind);
  44435. if (resolved) {
  44436. return resolveResult(id, resolved);
  44437. }
  44438. });
  44439. build.onLoad({ filter: /.*/, namespace: 'browser-external' }, ({ path }) => {
  44440. if (config.isProduction) {
  44441. return {
  44442. contents: 'module.exports = {}',
  44443. };
  44444. }
  44445. else {
  44446. return {
  44447. // Return in CJS to intercept named imports. Use `Object.create` to
  44448. // create the Proxy in the prototype to workaround esbuild issue. Why?
  44449. //
  44450. // In short, esbuild cjs->esm flow:
  44451. // 1. Create empty object using `Object.create(Object.getPrototypeOf(module.exports))`.
  44452. // 2. Assign props of `module.exports` to the object.
  44453. // 3. Return object for ESM use.
  44454. //
  44455. // If we do `module.exports = new Proxy({}, {})`, step 1 returns empty object,
  44456. // step 2 does nothing as there's no props for `module.exports`. The final object
  44457. // is just an empty object.
  44458. //
  44459. // Creating the Proxy in the prototype satisfies step 1 immediately, which means
  44460. // the returned object is a Proxy that we can intercept.
  44461. //
  44462. // Note: Skip keys that are accessed by esbuild and browser devtools.
  44463. contents: `\
  44464. module.exports = Object.create(new Proxy({}, {
  44465. get(_, key) {
  44466. if (
  44467. key !== '__esModule' &&
  44468. key !== '__proto__' &&
  44469. key !== 'constructor' &&
  44470. key !== 'splice'
  44471. ) {
  44472. console.warn(\`Module "${path}" has been externalized for browser compatibility. Cannot access "${path}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
  44473. }
  44474. }
  44475. }))`,
  44476. };
  44477. }
  44478. });
  44479. build.onLoad({ filter: /.*/, namespace: 'optional-peer-dep' }, ({ path }) => {
  44480. if (config.isProduction) {
  44481. return {
  44482. contents: 'module.exports = {}',
  44483. };
  44484. }
  44485. else {
  44486. const [, peerDep, parentDep] = path.split(':');
  44487. return {
  44488. contents: `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`,
  44489. };
  44490. }
  44491. });
  44492. },
  44493. };
  44494. }
  44495. const matchesEntireLine = (text) => `^${escapeRegex(text)}$`;
  44496. // esbuild doesn't transpile `require('foo')` into `import` statements if 'foo' is externalized
  44497. // https://github.com/evanw/esbuild/issues/566#issuecomment-735551834
  44498. function esbuildCjsExternalPlugin(externals, platform) {
  44499. return {
  44500. name: 'cjs-external',
  44501. setup(build) {
  44502. const filter = new RegExp(externals.map(matchesEntireLine).join('|'));
  44503. build.onResolve({ filter: new RegExp(`^${nonFacadePrefix}`) }, (args) => {
  44504. return {
  44505. path: args.path.slice(nonFacadePrefix.length),
  44506. external: true,
  44507. };
  44508. });
  44509. build.onResolve({ filter }, (args) => {
  44510. // preserve `require` for node because it's more accurate than converting it to import
  44511. if (args.kind === 'require-call' && platform !== 'node') {
  44512. return {
  44513. path: args.path,
  44514. namespace: cjsExternalFacadeNamespace,
  44515. };
  44516. }
  44517. return {
  44518. path: args.path,
  44519. external: true,
  44520. };
  44521. });
  44522. build.onLoad({ filter: /.*/, namespace: cjsExternalFacadeNamespace }, (args) => ({
  44523. contents: `import * as m from ${JSON.stringify(nonFacadePrefix + args.path)};` + `module.exports = m;`,
  44524. }));
  44525. },
  44526. };
  44527. }
  44528. const debug$b = createDebugger('vite:ssr-external');
  44529. const isSsrExternalCache = new WeakMap();
  44530. function shouldExternalizeForSSR(id, importer, config) {
  44531. let isSsrExternal = isSsrExternalCache.get(config);
  44532. if (!isSsrExternal) {
  44533. isSsrExternal = createIsSsrExternal(config);
  44534. isSsrExternalCache.set(config, isSsrExternal);
  44535. }
  44536. return isSsrExternal(id, importer);
  44537. }
  44538. function createIsConfiguredAsSsrExternal(config) {
  44539. const { ssr, root } = config;
  44540. const noExternal = ssr?.noExternal;
  44541. const noExternalFilter = noExternal !== 'undefined' &&
  44542. typeof noExternal !== 'boolean' &&
  44543. createFilter(undefined, noExternal, { resolve: false });
  44544. const targetConditions = config.ssr.resolve?.externalConditions || [];
  44545. const resolveOptions = {
  44546. ...config.resolve,
  44547. root,
  44548. isProduction: false,
  44549. isBuild: true,
  44550. conditions: targetConditions,
  44551. };
  44552. const isExternalizable = (id, importer, configuredAsExternal) => {
  44553. if (!bareImportRE.test(id) || id.includes('\0')) {
  44554. return false;
  44555. }
  44556. try {
  44557. return !!tryNodeResolve(id,
  44558. // Skip passing importer in build to avoid externalizing non-hoisted dependencies
  44559. // unresolvable from root (which would be unresolvable from output bundles also)
  44560. config.command === 'build' ? undefined : importer, resolveOptions, ssr?.target === 'webworker', undefined, true,
  44561. // try to externalize, will return undefined or an object without
  44562. // a external flag if it isn't externalizable
  44563. true,
  44564. // Allow linked packages to be externalized if they are explicitly
  44565. // configured as external
  44566. !!configuredAsExternal)?.external;
  44567. }
  44568. catch (e) {
  44569. debug$b?.(`Failed to node resolve "${id}". Skipping externalizing it by default.`);
  44570. // may be an invalid import that's resolved by a plugin
  44571. return false;
  44572. }
  44573. };
  44574. // Returns true if it is configured as external, false if it is filtered
  44575. // by noExternal and undefined if it isn't affected by the explicit config
  44576. return (id, importer) => {
  44577. if (
  44578. // If this id is defined as external, force it as external
  44579. // Note that individual package entries are allowed in ssr.external
  44580. ssr.external !== true &&
  44581. ssr.external?.includes(id)) {
  44582. return true;
  44583. }
  44584. const pkgName = getNpmPackageName(id);
  44585. if (!pkgName) {
  44586. return isExternalizable(id, importer);
  44587. }
  44588. if (
  44589. // A package name in ssr.external externalizes every
  44590. // externalizable package entry
  44591. ssr.external !== true &&
  44592. ssr.external?.includes(pkgName)) {
  44593. return isExternalizable(id, importer, true);
  44594. }
  44595. if (typeof noExternal === 'boolean') {
  44596. return !noExternal;
  44597. }
  44598. if (noExternalFilter && !noExternalFilter(pkgName)) {
  44599. return false;
  44600. }
  44601. // If `ssr.external: true`, all will be externalized by default, regardless if
  44602. // it's a linked package
  44603. return isExternalizable(id, importer, ssr.external === true);
  44604. };
  44605. }
  44606. function createIsSsrExternal(config) {
  44607. const processedIds = new Map();
  44608. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  44609. return (id, importer) => {
  44610. if (processedIds.has(id)) {
  44611. return processedIds.get(id);
  44612. }
  44613. let external = false;
  44614. if (id[0] !== '.' && !path$o.isAbsolute(id)) {
  44615. external = isBuiltin(id) || isConfiguredAsExternal(id, importer);
  44616. }
  44617. processedIds.set(id, external);
  44618. return external;
  44619. };
  44620. }
  44621. /**
  44622. * https://github.com/rollup/plugins/blob/master/packages/json/src/index.js
  44623. *
  44624. * This source code is licensed under the MIT license found in the
  44625. * LICENSE file at
  44626. * https://github.com/rollup/plugins/blob/master/LICENSE
  44627. */
  44628. // Custom json filter for vite
  44629. const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/;
  44630. const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`;
  44631. const jsonLangRE = new RegExp(jsonLangs);
  44632. const isJSONRequest = (request) => jsonLangRE.test(request);
  44633. function jsonPlugin(options = {}, isBuild) {
  44634. return {
  44635. name: 'vite:json',
  44636. transform(json, id) {
  44637. if (!jsonExtRE.test(id))
  44638. return null;
  44639. if (SPECIAL_QUERY_RE.test(id))
  44640. return null;
  44641. json = stripBomTag(json);
  44642. try {
  44643. if (options.stringify) {
  44644. if (isBuild) {
  44645. return {
  44646. // during build, parse then double-stringify to remove all
  44647. // unnecessary whitespaces to reduce bundle size.
  44648. code: `export default JSON.parse(${JSON.stringify(JSON.stringify(JSON.parse(json)))})`,
  44649. map: { mappings: '' },
  44650. };
  44651. }
  44652. else {
  44653. return `export default JSON.parse(${JSON.stringify(json)})`;
  44654. }
  44655. }
  44656. const parsed = JSON.parse(json);
  44657. return {
  44658. code: dataToEsm(parsed, {
  44659. preferConst: true,
  44660. namedExports: options.namedExports,
  44661. }),
  44662. map: { mappings: '' },
  44663. };
  44664. }
  44665. catch (e) {
  44666. const position = extractJsonErrorPosition(e.message, json.length);
  44667. const msg = position
  44668. ? `, invalid JSON syntax found at position ${position}`
  44669. : `.`;
  44670. this.error(`Failed to parse JSON file` + msg, position);
  44671. }
  44672. },
  44673. };
  44674. }
  44675. function extractJsonErrorPosition(errorMessage, inputLength) {
  44676. if (errorMessage.startsWith('Unexpected end of JSON input')) {
  44677. return inputLength - 1;
  44678. }
  44679. const errorMessageList = /at position (\d+)/.exec(errorMessage);
  44680. return errorMessageList
  44681. ? Math.max(parseInt(errorMessageList[1], 10) - 1, 0)
  44682. : undefined;
  44683. }
  44684. const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR = 'ERR_OPTIMIZE_DEPS_PROCESSING_ERROR';
  44685. const ERR_OUTDATED_OPTIMIZED_DEP = 'ERR_OUTDATED_OPTIMIZED_DEP';
  44686. const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR = 'ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR';
  44687. const debug$a = createDebugger('vite:optimize-deps');
  44688. function optimizedDepsPlugin(config) {
  44689. return {
  44690. name: 'vite:optimized-deps',
  44691. resolveId(id, source, { ssr }) {
  44692. if (getDepsOptimizer(config, ssr)?.isOptimizedDepFile(id)) {
  44693. return id;
  44694. }
  44695. },
  44696. // this.load({ id }) isn't implemented in PluginContainer
  44697. // The logic to register an id to wait until it is processed
  44698. // is in importAnalysis, see call to delayDepsOptimizerUntil
  44699. async load(id, options) {
  44700. const ssr = options?.ssr === true;
  44701. const depsOptimizer = getDepsOptimizer(config, ssr);
  44702. if (depsOptimizer?.isOptimizedDepFile(id)) {
  44703. const metadata = depsOptimizer.metadata;
  44704. const file = cleanUrl(id);
  44705. const versionMatch = id.match(DEP_VERSION_RE);
  44706. const browserHash = versionMatch
  44707. ? versionMatch[1].split('=')[1]
  44708. : undefined;
  44709. // Search in both the currently optimized and newly discovered deps
  44710. const info = optimizedDepInfoFromFile(metadata, file);
  44711. if (info) {
  44712. if (browserHash && info.browserHash !== browserHash) {
  44713. throwOutdatedRequest(id);
  44714. }
  44715. try {
  44716. // This is an entry point, it may still not be bundled
  44717. await info.processing;
  44718. }
  44719. catch {
  44720. // If the refresh has not happened after timeout, Vite considers
  44721. // something unexpected has happened. In this case, Vite
  44722. // returns an empty response that will error.
  44723. throwProcessingError(id);
  44724. }
  44725. const newMetadata = depsOptimizer.metadata;
  44726. if (metadata !== newMetadata) {
  44727. const currentInfo = optimizedDepInfoFromFile(newMetadata, file);
  44728. if (info.browserHash !== currentInfo?.browserHash) {
  44729. throwOutdatedRequest(id);
  44730. }
  44731. }
  44732. }
  44733. debug$a?.(`load ${colors$1.cyan(file)}`);
  44734. // Load the file from the cache instead of waiting for other plugin
  44735. // load hooks to avoid race conditions, once processing is resolved,
  44736. // we are sure that the file has been properly save to disk
  44737. try {
  44738. return await fsp.readFile(file, 'utf-8');
  44739. }
  44740. catch (e) {
  44741. const newMetadata = depsOptimizer.metadata;
  44742. if (optimizedDepInfoFromFile(newMetadata, file)) {
  44743. // Outdated non-entry points (CHUNK), loaded after a rerun
  44744. throwOutdatedRequest(id);
  44745. }
  44746. throwFileNotFoundInOptimizedDep(id);
  44747. }
  44748. }
  44749. },
  44750. };
  44751. }
  44752. function throwProcessingError(id) {
  44753. const err = new Error(`Something unexpected happened while optimizing "${id}". ` +
  44754. `The current page should have reloaded by now`);
  44755. err.code = ERR_OPTIMIZE_DEPS_PROCESSING_ERROR;
  44756. // This error will be caught by the transform middleware that will
  44757. // send a 504 status code request timeout
  44758. throw err;
  44759. }
  44760. function throwOutdatedRequest(id) {
  44761. const err = new Error(`There is a new version of the pre-bundle for "${id}", ` +
  44762. `a page reload is going to ask for it.`);
  44763. err.code = ERR_OUTDATED_OPTIMIZED_DEP;
  44764. // This error will be caught by the transform middleware that will
  44765. // send a 504 status code request timeout
  44766. throw err;
  44767. }
  44768. function throwFileNotFoundInOptimizedDep(id) {
  44769. const err = new Error(`The file does not exist at "${id}" which is in the optimize deps directory. ` +
  44770. `The dependency might be incompatible with the dep optimizer. ` +
  44771. `Try adding it to \`optimizeDeps.exclude\`.`);
  44772. err.code = ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR;
  44773. // This error will be caught by the transform middleware that will
  44774. // send a 404 status code not found
  44775. throw err;
  44776. }
  44777. const nonJsRe = /\.json(?:$|\?)/;
  44778. const isNonJsRequest = (request) => nonJsRe.test(request);
  44779. function definePlugin(config) {
  44780. const isBuild = config.command === 'build';
  44781. const isBuildLib = isBuild && config.build.lib;
  44782. // ignore replace process.env in lib build
  44783. const processEnv = {};
  44784. if (!isBuildLib) {
  44785. const nodeEnv = process.env.NODE_ENV || config.mode;
  44786. Object.assign(processEnv, {
  44787. 'process.env': `{}`,
  44788. 'global.process.env': `{}`,
  44789. 'globalThis.process.env': `{}`,
  44790. 'process.env.NODE_ENV': JSON.stringify(nodeEnv),
  44791. 'global.process.env.NODE_ENV': JSON.stringify(nodeEnv),
  44792. 'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv),
  44793. });
  44794. }
  44795. // during dev, import.meta properties are handled by importAnalysis plugin.
  44796. const importMetaKeys = {};
  44797. const importMetaEnvKeys = {};
  44798. const importMetaFallbackKeys = {};
  44799. if (isBuild) {
  44800. importMetaKeys['import.meta.hot'] = `undefined`;
  44801. for (const key in config.env) {
  44802. const val = JSON.stringify(config.env[key]);
  44803. importMetaKeys[`import.meta.env.${key}`] = val;
  44804. importMetaEnvKeys[key] = val;
  44805. }
  44806. // these will be set to a proper value in `generatePattern`
  44807. importMetaKeys['import.meta.env.SSR'] = `undefined`;
  44808. importMetaFallbackKeys['import.meta.env'] = `undefined`;
  44809. }
  44810. const userDefine = {};
  44811. const userDefineEnv = {};
  44812. for (const key in config.define) {
  44813. userDefine[key] = handleDefineValue(config.define[key]);
  44814. // make sure `import.meta.env` object has user define properties
  44815. if (isBuild && key.startsWith('import.meta.env.')) {
  44816. userDefineEnv[key.slice(16)] = config.define[key];
  44817. }
  44818. }
  44819. function generatePattern(ssr) {
  44820. const replaceProcessEnv = !ssr || config.ssr?.target === 'webworker';
  44821. const define = {
  44822. ...(replaceProcessEnv ? processEnv : {}),
  44823. ...importMetaKeys,
  44824. ...userDefine,
  44825. ...importMetaFallbackKeys,
  44826. };
  44827. // Additional define fixes based on `ssr` value
  44828. if ('import.meta.env.SSR' in define) {
  44829. define['import.meta.env.SSR'] = ssr + '';
  44830. }
  44831. if ('import.meta.env' in define) {
  44832. define['import.meta.env'] = serializeDefine({
  44833. ...importMetaEnvKeys,
  44834. SSR: ssr + '',
  44835. ...userDefineEnv,
  44836. });
  44837. }
  44838. // Create regex pattern as a fast check before running esbuild
  44839. const patternKeys = Object.keys(userDefine);
  44840. if (replaceProcessEnv && Object.keys(processEnv).length) {
  44841. patternKeys.push('process.env');
  44842. }
  44843. if (Object.keys(importMetaKeys).length) {
  44844. patternKeys.push('import.meta.env', 'import.meta.hot');
  44845. }
  44846. const pattern = patternKeys.length
  44847. ? new RegExp(patternKeys.map(escapeRegex).join('|'))
  44848. : null;
  44849. return [define, pattern];
  44850. }
  44851. const defaultPattern = generatePattern(false);
  44852. const ssrPattern = generatePattern(true);
  44853. return {
  44854. name: 'vite:define',
  44855. async transform(code, id, options) {
  44856. const ssr = options?.ssr === true;
  44857. if (!ssr && !isBuild) {
  44858. // for dev we inject actual global defines in the vite client to
  44859. // avoid the transform cost. see the `clientInjection` and
  44860. // `importAnalysis` plugin.
  44861. return;
  44862. }
  44863. if (
  44864. // exclude html, css and static assets for performance
  44865. isHTMLRequest(id) ||
  44866. isCSSRequest(id) ||
  44867. isNonJsRequest(id) ||
  44868. config.assetsInclude(id)) {
  44869. return;
  44870. }
  44871. const [define, pattern] = ssr ? ssrPattern : defaultPattern;
  44872. if (!pattern)
  44873. return;
  44874. // Check if our code needs any replacements before running esbuild
  44875. pattern.lastIndex = 0;
  44876. if (!pattern.test(code))
  44877. return;
  44878. return await replaceDefine(code, id, define, config);
  44879. },
  44880. };
  44881. }
  44882. async function replaceDefine(code, id, define, config) {
  44883. // Because esbuild only allows JSON-serializable values, and `import.meta.env`
  44884. // may contain values with raw identifiers, making it non-JSON-serializable,
  44885. // we replace it with a temporary marker and then replace it back after to
  44886. // workaround it. This means that esbuild is unable to optimize the `import.meta.env`
  44887. // access, but that's a tradeoff for now.
  44888. const replacementMarkers = {};
  44889. const env = define['import.meta.env'];
  44890. if (env && !canJsonParse(env)) {
  44891. const marker = `_${getHash(env, env.length - 2)}_`;
  44892. replacementMarkers[marker] = env;
  44893. define = { ...define, 'import.meta.env': marker };
  44894. }
  44895. const esbuildOptions = config.esbuild || {};
  44896. const result = await transform$1(code, {
  44897. loader: 'js',
  44898. charset: esbuildOptions.charset ?? 'utf8',
  44899. platform: 'neutral',
  44900. define,
  44901. sourcefile: id,
  44902. sourcemap: config.command === 'build' ? !!config.build.sourcemap : true,
  44903. });
  44904. // remove esbuild's <define:...> source entries
  44905. // since they would confuse source map remapping/collapsing which expects a single source
  44906. if (result.map.includes('<define:')) {
  44907. const originalMap = new TraceMap(result.map);
  44908. if (originalMap.sources.length >= 2) {
  44909. const sourceIndex = originalMap.sources.indexOf(id);
  44910. const decoded = decodedMap(originalMap);
  44911. decoded.sources = [id];
  44912. decoded.mappings = decoded.mappings.map((segments) => segments.filter((segment) => {
  44913. // modify and filter
  44914. const index = segment[1];
  44915. segment[1] = 0;
  44916. return index === sourceIndex;
  44917. }));
  44918. result.map = JSON.stringify(encodedMap(new TraceMap(decoded)));
  44919. }
  44920. }
  44921. for (const marker in replacementMarkers) {
  44922. result.code = result.code.replaceAll(marker, replacementMarkers[marker]);
  44923. }
  44924. return {
  44925. code: result.code,
  44926. map: result.map || null,
  44927. };
  44928. }
  44929. /**
  44930. * Like `JSON.stringify` but keeps raw string values as a literal
  44931. * in the generated code. For example: `"window"` would refer to
  44932. * the global `window` object directly.
  44933. */
  44934. function serializeDefine(define) {
  44935. let res = `{`;
  44936. const keys = Object.keys(define);
  44937. for (let i = 0; i < keys.length; i++) {
  44938. const key = keys[i];
  44939. const val = define[key];
  44940. res += `${JSON.stringify(key)}: ${handleDefineValue(val)}`;
  44941. if (i !== keys.length - 1) {
  44942. res += `, `;
  44943. }
  44944. }
  44945. return res + `}`;
  44946. }
  44947. function handleDefineValue(value) {
  44948. if (typeof value === 'undefined')
  44949. return 'undefined';
  44950. if (typeof value === 'string')
  44951. return value;
  44952. return JSON.stringify(value);
  44953. }
  44954. function canJsonParse(value) {
  44955. try {
  44956. JSON.parse(value);
  44957. return true;
  44958. }
  44959. catch {
  44960. return false;
  44961. }
  44962. }
  44963. // ids in transform are normalized to unix style
  44964. const normalizedClientEntry = normalizePath$3(CLIENT_ENTRY);
  44965. const normalizedEnvEntry = normalizePath$3(ENV_ENTRY);
  44966. /**
  44967. * some values used by the client needs to be dynamically injected by the server
  44968. * @server-only
  44969. */
  44970. function clientInjectionsPlugin(config) {
  44971. let injectConfigValues;
  44972. return {
  44973. name: 'vite:client-inject',
  44974. async buildStart() {
  44975. const resolvedServerHostname = (await resolveHostname(config.server.host))
  44976. .name;
  44977. const resolvedServerPort = config.server.port;
  44978. const devBase = config.base;
  44979. const serverHost = `${resolvedServerHostname}:${resolvedServerPort}${devBase}`;
  44980. let hmrConfig = config.server.hmr;
  44981. hmrConfig = isObject$1(hmrConfig) ? hmrConfig : undefined;
  44982. const host = hmrConfig?.host || null;
  44983. const protocol = hmrConfig?.protocol || null;
  44984. const timeout = hmrConfig?.timeout || 30000;
  44985. const overlay = hmrConfig?.overlay !== false;
  44986. const isHmrServerSpecified = !!hmrConfig?.server;
  44987. const hmrConfigName = path$o.basename(config.configFile || 'vite.config.js');
  44988. // hmr.clientPort -> hmr.port
  44989. // -> (24678 if middleware mode and HMR server is not specified) -> new URL(import.meta.url).port
  44990. let port = hmrConfig?.clientPort || hmrConfig?.port || null;
  44991. if (config.server.middlewareMode && !isHmrServerSpecified) {
  44992. port ||= 24678;
  44993. }
  44994. let directTarget = hmrConfig?.host || resolvedServerHostname;
  44995. directTarget += `:${hmrConfig?.port || resolvedServerPort}`;
  44996. directTarget += devBase;
  44997. let hmrBase = devBase;
  44998. if (hmrConfig?.path) {
  44999. hmrBase = path$o.posix.join(hmrBase, hmrConfig.path);
  45000. }
  45001. const userDefine = {};
  45002. for (const key in config.define) {
  45003. // import.meta.env.* is handled in `importAnalysis` plugin
  45004. if (!key.startsWith('import.meta.env.')) {
  45005. userDefine[key] = config.define[key];
  45006. }
  45007. }
  45008. const serializedDefines = serializeDefine(userDefine);
  45009. const modeReplacement = escapeReplacement(config.mode);
  45010. const baseReplacement = escapeReplacement(devBase);
  45011. const definesReplacement = () => serializedDefines;
  45012. const serverHostReplacement = escapeReplacement(serverHost);
  45013. const hmrProtocolReplacement = escapeReplacement(protocol);
  45014. const hmrHostnameReplacement = escapeReplacement(host);
  45015. const hmrPortReplacement = escapeReplacement(port);
  45016. const hmrDirectTargetReplacement = escapeReplacement(directTarget);
  45017. const hmrBaseReplacement = escapeReplacement(hmrBase);
  45018. const hmrTimeoutReplacement = escapeReplacement(timeout);
  45019. const hmrEnableOverlayReplacement = escapeReplacement(overlay);
  45020. const hmrConfigNameReplacement = escapeReplacement(hmrConfigName);
  45021. injectConfigValues = (code) => {
  45022. return code
  45023. .replace(`__MODE__`, modeReplacement)
  45024. .replace(/__BASE__/g, baseReplacement)
  45025. .replace(`__DEFINES__`, definesReplacement)
  45026. .replace(`__SERVER_HOST__`, serverHostReplacement)
  45027. .replace(`__HMR_PROTOCOL__`, hmrProtocolReplacement)
  45028. .replace(`__HMR_HOSTNAME__`, hmrHostnameReplacement)
  45029. .replace(`__HMR_PORT__`, hmrPortReplacement)
  45030. .replace(`__HMR_DIRECT_TARGET__`, hmrDirectTargetReplacement)
  45031. .replace(`__HMR_BASE__`, hmrBaseReplacement)
  45032. .replace(`__HMR_TIMEOUT__`, hmrTimeoutReplacement)
  45033. .replace(`__HMR_ENABLE_OVERLAY__`, hmrEnableOverlayReplacement)
  45034. .replace(`__HMR_CONFIG_NAME__`, hmrConfigNameReplacement);
  45035. };
  45036. },
  45037. async transform(code, id, options) {
  45038. if (id === normalizedClientEntry || id === normalizedEnvEntry) {
  45039. return injectConfigValues(code);
  45040. }
  45041. else if (!options?.ssr && code.includes('process.env.NODE_ENV')) {
  45042. // replace process.env.NODE_ENV instead of defining a global
  45043. // for it to avoid shimming a `process` object during dev,
  45044. // avoiding inconsistencies between dev and build
  45045. const nodeEnv = config.define?.['process.env.NODE_ENV'] ||
  45046. JSON.stringify(process.env.NODE_ENV || config.mode);
  45047. return await replaceDefine(code, id, {
  45048. 'process.env.NODE_ENV': nodeEnv,
  45049. 'global.process.env.NODE_ENV': nodeEnv,
  45050. 'globalThis.process.env.NODE_ENV': nodeEnv,
  45051. }, config);
  45052. }
  45053. },
  45054. };
  45055. }
  45056. function escapeReplacement(value) {
  45057. const jsonValue = JSON.stringify(value);
  45058. return () => jsonValue;
  45059. }
  45060. const wasmHelperId = '\0vite/wasm-helper.js';
  45061. const wasmHelper = async (opts = {}, url) => {
  45062. let result;
  45063. if (url.startsWith('data:')) {
  45064. const urlContent = url.replace(/^data:.*?base64,/, '');
  45065. let bytes;
  45066. if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
  45067. bytes = Buffer.from(urlContent, 'base64');
  45068. }
  45069. else if (typeof atob === 'function') {
  45070. const binaryString = atob(urlContent);
  45071. bytes = new Uint8Array(binaryString.length);
  45072. for (let i = 0; i < binaryString.length; i++) {
  45073. bytes[i] = binaryString.charCodeAt(i);
  45074. }
  45075. }
  45076. else {
  45077. throw new Error('Failed to decode base64-encoded data URL, Buffer and atob are not supported');
  45078. }
  45079. result = await WebAssembly.instantiate(bytes, opts);
  45080. }
  45081. else {
  45082. // https://github.com/mdn/webassembly-examples/issues/5
  45083. // WebAssembly.instantiateStreaming requires the server to provide the
  45084. // correct MIME type for .wasm files, which unfortunately doesn't work for
  45085. // a lot of static file servers, so we just work around it by getting the
  45086. // raw buffer.
  45087. const response = await fetch(url);
  45088. const contentType = response.headers.get('Content-Type') || '';
  45089. if ('instantiateStreaming' in WebAssembly &&
  45090. contentType.startsWith('application/wasm')) {
  45091. result = await WebAssembly.instantiateStreaming(response, opts);
  45092. }
  45093. else {
  45094. const buffer = await response.arrayBuffer();
  45095. result = await WebAssembly.instantiate(buffer, opts);
  45096. }
  45097. }
  45098. return result.instance;
  45099. };
  45100. const wasmHelperCode = wasmHelper.toString();
  45101. const wasmHelperPlugin = (config) => {
  45102. return {
  45103. name: 'vite:wasm-helper',
  45104. resolveId(id) {
  45105. if (id === wasmHelperId) {
  45106. return id;
  45107. }
  45108. },
  45109. async load(id) {
  45110. if (id === wasmHelperId) {
  45111. return `export default ${wasmHelperCode}`;
  45112. }
  45113. if (!id.endsWith('.wasm?init')) {
  45114. return;
  45115. }
  45116. const url = await fileToUrl$1(id, config, this);
  45117. return `
  45118. import initWasm from "${wasmHelperId}"
  45119. export default opts => initWasm(opts, ${JSON.stringify(url)})
  45120. `;
  45121. },
  45122. };
  45123. };
  45124. const wasmFallbackPlugin = () => {
  45125. return {
  45126. name: 'vite:wasm-fallback',
  45127. async load(id) {
  45128. if (!id.endsWith('.wasm')) {
  45129. return;
  45130. }
  45131. throw new Error('"ESM integration proposal for Wasm" is not supported currently. ' +
  45132. 'Use vite-plugin-wasm or other community plugins to handle this. ' +
  45133. 'Alternatively, you can use `.wasm?init` or `.wasm?url`. ' +
  45134. 'See https://vitejs.dev/guide/features.html#webassembly for more details.');
  45135. },
  45136. };
  45137. };
  45138. const workerOrSharedWorkerRE = /(?:\?|&)(worker|sharedworker)(?:&|$)/;
  45139. const workerFileRE = /(?:\?|&)worker_file&type=(\w+)(?:&|$)/;
  45140. const inlineRE = /[?&]inline\b/;
  45141. const WORKER_FILE_ID = 'worker_file';
  45142. const workerCache = new WeakMap();
  45143. function saveEmitWorkerAsset(config, asset) {
  45144. const workerMap = workerCache.get(config.mainConfig || config);
  45145. workerMap.assets.set(asset.fileName, asset);
  45146. }
  45147. async function bundleWorkerEntry(config, id) {
  45148. const input = cleanUrl(id);
  45149. const newBundleChain = [...config.bundleChain, input];
  45150. if (config.bundleChain.includes(input)) {
  45151. throw new Error('Circular worker imports detected. Vite does not support it. ' +
  45152. `Import chain: ${newBundleChain.map((id) => prettifyUrl(id, config.root)).join(' -> ')}`);
  45153. }
  45154. // bundle the file as entry to support imports
  45155. const { rollup } = await import('rollup');
  45156. const { plugins, rollupOptions, format } = config.worker;
  45157. const bundle = await rollup({
  45158. ...rollupOptions,
  45159. input,
  45160. plugins: await plugins(newBundleChain),
  45161. onwarn(warning, warn) {
  45162. onRollupWarning(warning, warn, config);
  45163. },
  45164. preserveEntrySignatures: false,
  45165. });
  45166. let chunk;
  45167. try {
  45168. const workerOutputConfig = config.worker.rollupOptions.output;
  45169. const workerConfig = workerOutputConfig
  45170. ? Array.isArray(workerOutputConfig)
  45171. ? workerOutputConfig[0] || {}
  45172. : workerOutputConfig
  45173. : {};
  45174. const { output: [outputChunk, ...outputChunks], } = await bundle.generate({
  45175. entryFileNames: path$o.posix.join(config.build.assetsDir, '[name]-[hash].js'),
  45176. chunkFileNames: path$o.posix.join(config.build.assetsDir, '[name]-[hash].js'),
  45177. assetFileNames: path$o.posix.join(config.build.assetsDir, '[name]-[hash].[ext]'),
  45178. ...workerConfig,
  45179. format,
  45180. sourcemap: config.build.sourcemap,
  45181. });
  45182. chunk = outputChunk;
  45183. outputChunks.forEach((outputChunk) => {
  45184. if (outputChunk.type === 'asset') {
  45185. saveEmitWorkerAsset(config, outputChunk);
  45186. }
  45187. else if (outputChunk.type === 'chunk') {
  45188. saveEmitWorkerAsset(config, {
  45189. fileName: outputChunk.fileName,
  45190. source: outputChunk.code,
  45191. });
  45192. }
  45193. });
  45194. }
  45195. finally {
  45196. await bundle.close();
  45197. }
  45198. return emitSourcemapForWorkerEntry(config, chunk);
  45199. }
  45200. function emitSourcemapForWorkerEntry(config, chunk) {
  45201. const { map: sourcemap } = chunk;
  45202. if (sourcemap) {
  45203. if (config.build.sourcemap === 'hidden' ||
  45204. config.build.sourcemap === true) {
  45205. const data = sourcemap.toString();
  45206. const mapFileName = chunk.fileName + '.map';
  45207. saveEmitWorkerAsset(config, {
  45208. fileName: mapFileName,
  45209. source: data,
  45210. });
  45211. }
  45212. }
  45213. return chunk;
  45214. }
  45215. const workerAssetUrlRE = /__VITE_WORKER_ASSET__([a-z\d]{8})__/g;
  45216. function encodeWorkerAssetFileName(fileName, workerCache) {
  45217. const { fileNameHash } = workerCache;
  45218. const hash = getHash(fileName);
  45219. if (!fileNameHash.get(hash)) {
  45220. fileNameHash.set(hash, fileName);
  45221. }
  45222. return `__VITE_WORKER_ASSET__${hash}__`;
  45223. }
  45224. async function workerFileToUrl(config, id) {
  45225. const workerMap = workerCache.get(config.mainConfig || config);
  45226. let fileName = workerMap.bundle.get(id);
  45227. if (!fileName) {
  45228. const outputChunk = await bundleWorkerEntry(config, id);
  45229. fileName = outputChunk.fileName;
  45230. saveEmitWorkerAsset(config, {
  45231. fileName,
  45232. source: outputChunk.code,
  45233. });
  45234. workerMap.bundle.set(id, fileName);
  45235. }
  45236. return encodeWorkerAssetFileName(fileName, workerMap);
  45237. }
  45238. function webWorkerPostPlugin() {
  45239. return {
  45240. name: 'vite:worker-post',
  45241. resolveImportMeta(property, { format }) {
  45242. // document is undefined in the worker, so we need to avoid it in iife
  45243. if (format === 'iife') {
  45244. // compiling import.meta
  45245. if (!property) {
  45246. // rollup only supports `url` property. we only support `url` property as well.
  45247. // https://github.com/rollup/rollup/blob/62b648e1cc6a1f00260bb85aa2050097bb4afd2b/src/ast/nodes/MetaProperty.ts#L164-L173
  45248. return `{
  45249. url: self.location.href
  45250. }`;
  45251. }
  45252. // compiling import.meta.url
  45253. if (property === 'url') {
  45254. return 'self.location.href';
  45255. }
  45256. }
  45257. return null;
  45258. },
  45259. };
  45260. }
  45261. function webWorkerPlugin(config) {
  45262. const isBuild = config.command === 'build';
  45263. let server;
  45264. const isWorker = config.isWorker;
  45265. return {
  45266. name: 'vite:worker',
  45267. configureServer(_server) {
  45268. server = _server;
  45269. },
  45270. buildStart() {
  45271. if (isWorker) {
  45272. return;
  45273. }
  45274. workerCache.set(config, {
  45275. assets: new Map(),
  45276. bundle: new Map(),
  45277. fileNameHash: new Map(),
  45278. });
  45279. },
  45280. load(id) {
  45281. if (isBuild && workerOrSharedWorkerRE.test(id)) {
  45282. return '';
  45283. }
  45284. },
  45285. shouldTransformCachedModule({ id }) {
  45286. if (isBuild && config.build.watch && workerOrSharedWorkerRE.test(id)) {
  45287. return true;
  45288. }
  45289. },
  45290. async transform(raw, id) {
  45291. const workerFileMatch = workerFileRE.exec(id);
  45292. if (workerFileMatch) {
  45293. // if import worker by worker constructor will have query.type
  45294. // other type will be import worker by esm
  45295. const workerType = workerFileMatch[1];
  45296. let injectEnv = '';
  45297. const scriptPath = JSON.stringify(path$o.posix.join(config.base, ENV_PUBLIC_PATH));
  45298. if (workerType === 'classic') {
  45299. injectEnv = `importScripts(${scriptPath})\n`;
  45300. }
  45301. else if (workerType === 'module') {
  45302. injectEnv = `import ${scriptPath}\n`;
  45303. }
  45304. else if (workerType === 'ignore') {
  45305. if (isBuild) {
  45306. injectEnv = '';
  45307. }
  45308. else if (server) {
  45309. // dynamic worker type we can't know how import the env
  45310. // so we copy /@vite/env code of server transform result into file header
  45311. const { moduleGraph } = server;
  45312. const module = moduleGraph.getModuleById(ENV_ENTRY);
  45313. injectEnv = module?.transformResult?.code || '';
  45314. }
  45315. }
  45316. if (injectEnv) {
  45317. const s = new MagicString(raw);
  45318. s.prepend(injectEnv);
  45319. return {
  45320. code: s.toString(),
  45321. map: s.generateMap({ hires: 'boundary' }),
  45322. };
  45323. }
  45324. return;
  45325. }
  45326. const workerMatch = workerOrSharedWorkerRE.exec(id);
  45327. if (!workerMatch)
  45328. return;
  45329. const { format } = config.worker;
  45330. const workerConstructor = workerMatch[1] === 'sharedworker' ? 'SharedWorker' : 'Worker';
  45331. const workerType = isBuild
  45332. ? format === 'es'
  45333. ? 'module'
  45334. : 'classic'
  45335. : 'module';
  45336. const workerTypeOption = `{
  45337. ${workerType === 'module' ? `type: "module",` : ''}
  45338. name: options?.name
  45339. }`;
  45340. let urlCode;
  45341. if (isBuild) {
  45342. if (isWorker && this.getModuleInfo(cleanUrl(id))?.isEntry) {
  45343. urlCode = 'self.location.href';
  45344. }
  45345. else if (inlineRE.test(id)) {
  45346. const chunk = await bundleWorkerEntry(config, id);
  45347. const encodedJs = `const encodedJs = "${Buffer.from(chunk.code).toString('base64')}";`;
  45348. const code =
  45349. // Using blob URL for SharedWorker results in multiple instances of a same worker
  45350. workerConstructor === 'Worker'
  45351. ? `${encodedJs}
  45352. const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
  45353. const blob = typeof window !== "undefined" && window.Blob && new Blob([${workerType === 'classic'
  45354. ? ''
  45355. : // `URL` is always available, in `Worker[type="module"]`
  45356. `'URL.revokeObjectURL(import.meta.url);',`}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
  45357. export default function WorkerWrapper(options) {
  45358. let objURL;
  45359. try {
  45360. objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
  45361. if (!objURL) throw ''
  45362. const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
  45363. worker.addEventListener("error", () => {
  45364. (window.URL || window.webkitURL).revokeObjectURL(objURL);
  45365. });
  45366. return worker;
  45367. } catch(e) {
  45368. return new ${workerConstructor}(
  45369. "data:text/javascript;base64," + encodedJs,
  45370. ${workerTypeOption}
  45371. );
  45372. }${
  45373. // For module workers, we should not revoke the URL until the worker runs,
  45374. // otherwise the worker fails to run
  45375. workerType === 'classic'
  45376. ? ` finally {
  45377. objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
  45378. }`
  45379. : ''}
  45380. }`
  45381. : `${encodedJs}
  45382. export default function WorkerWrapper(options) {
  45383. return new ${workerConstructor}(
  45384. "data:text/javascript;base64," + encodedJs,
  45385. ${workerTypeOption}
  45386. );
  45387. }
  45388. `;
  45389. return {
  45390. code,
  45391. // Empty sourcemap to suppress Rollup warning
  45392. map: { mappings: '' },
  45393. };
  45394. }
  45395. else {
  45396. urlCode = JSON.stringify(await workerFileToUrl(config, id));
  45397. }
  45398. }
  45399. else {
  45400. let url = await fileToUrl$1(cleanUrl(id), config, this);
  45401. url = injectQuery(url, `${WORKER_FILE_ID}&type=${workerType}`);
  45402. urlCode = JSON.stringify(url);
  45403. }
  45404. if (urlRE.test(id)) {
  45405. return {
  45406. code: `export default ${urlCode}`,
  45407. map: { mappings: '' }, // Empty sourcemap to suppress Rollup warning
  45408. };
  45409. }
  45410. return {
  45411. code: `export default function WorkerWrapper(options) {
  45412. return new ${workerConstructor}(
  45413. ${urlCode},
  45414. ${workerTypeOption}
  45415. );
  45416. }`,
  45417. map: { mappings: '' }, // Empty sourcemap to suppress Rollup warning
  45418. };
  45419. },
  45420. renderChunk(code, chunk, outputOptions) {
  45421. let s;
  45422. const result = () => {
  45423. return (s && {
  45424. code: s.toString(),
  45425. map: config.build.sourcemap
  45426. ? s.generateMap({ hires: 'boundary' })
  45427. : null,
  45428. });
  45429. };
  45430. workerAssetUrlRE.lastIndex = 0;
  45431. if (workerAssetUrlRE.test(code)) {
  45432. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(outputOptions.format, config.isWorker);
  45433. let match;
  45434. s = new MagicString(code);
  45435. workerAssetUrlRE.lastIndex = 0;
  45436. // Replace "__VITE_WORKER_ASSET__5aa0ddc0__" using relative paths
  45437. const workerMap = workerCache.get(config.mainConfig || config);
  45438. const { fileNameHash } = workerMap;
  45439. while ((match = workerAssetUrlRE.exec(code))) {
  45440. const [full, hash] = match;
  45441. const filename = fileNameHash.get(hash);
  45442. const replacement = toOutputFilePathInJS(filename, 'asset', chunk.fileName, 'js', config, toRelativeRuntime);
  45443. const replacementString = typeof replacement === 'string'
  45444. ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
  45445. : `"+${replacement.runtime}+"`;
  45446. s.update(match.index, match.index + full.length, replacementString);
  45447. }
  45448. }
  45449. return result();
  45450. },
  45451. generateBundle(opts, bundle) {
  45452. // @ts-expect-error asset emits are skipped in legacy bundle
  45453. if (opts.__vite_skip_asset_emit__ || isWorker) {
  45454. return;
  45455. }
  45456. const workerMap = workerCache.get(config);
  45457. workerMap.assets.forEach((asset) => {
  45458. const duplicateAsset = bundle[asset.fileName];
  45459. if (duplicateAsset) {
  45460. const content = duplicateAsset.type === 'asset'
  45461. ? duplicateAsset.source
  45462. : duplicateAsset.code;
  45463. // don't emit if the file name and the content is same
  45464. if (isSameContent(content, asset.source)) {
  45465. return;
  45466. }
  45467. }
  45468. this.emitFile({
  45469. type: 'asset',
  45470. fileName: asset.fileName,
  45471. source: asset.source,
  45472. });
  45473. });
  45474. workerMap.assets.clear();
  45475. },
  45476. };
  45477. }
  45478. function isSameContent(a, b) {
  45479. if (typeof a === 'string') {
  45480. if (typeof b === 'string') {
  45481. return a === b;
  45482. }
  45483. return Buffer.from(a).equals(b);
  45484. }
  45485. return Buffer.from(b).equals(a);
  45486. }
  45487. /**
  45488. * A plugin to avoid an aliased AND optimized dep from being aliased in src
  45489. */
  45490. function preAliasPlugin(config) {
  45491. const findPatterns = getAliasPatterns(config.resolve.alias);
  45492. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  45493. const isBuild = config.command === 'build';
  45494. const fsUtils = getFsUtils(config);
  45495. return {
  45496. name: 'vite:pre-alias',
  45497. async resolveId(id, importer, options) {
  45498. const ssr = options?.ssr === true;
  45499. const depsOptimizer = !isBuild && getDepsOptimizer(config, ssr);
  45500. if (importer &&
  45501. depsOptimizer &&
  45502. bareImportRE.test(id) &&
  45503. !options?.scan &&
  45504. id !== '@vite/client' &&
  45505. id !== '@vite/env') {
  45506. if (findPatterns.find((pattern) => matches(pattern, id))) {
  45507. const optimizedId = await tryOptimizedResolve(depsOptimizer, id, importer, config.resolve.preserveSymlinks, config.packageCache);
  45508. if (optimizedId) {
  45509. return optimizedId; // aliased dep already optimized
  45510. }
  45511. if (depsOptimizer.options.noDiscovery) {
  45512. return;
  45513. }
  45514. const resolved = await this.resolve(id, importer, {
  45515. ...options,
  45516. custom: { ...options.custom, 'vite:pre-alias': true },
  45517. });
  45518. if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
  45519. const optimizeDeps = depsOptimizer.options;
  45520. const resolvedId = cleanUrl(resolved.id);
  45521. const isVirtual = resolvedId === id || resolvedId.includes('\0');
  45522. if (!isVirtual &&
  45523. fsUtils.existsSync(resolvedId) &&
  45524. !moduleListContains(optimizeDeps.exclude, id) &&
  45525. path$o.isAbsolute(resolvedId) &&
  45526. (isInNodeModules$1(resolvedId) ||
  45527. optimizeDeps.include?.includes(id)) &&
  45528. isOptimizable(resolvedId, optimizeDeps) &&
  45529. !(isBuild && ssr && isConfiguredAsExternal(id, importer)) &&
  45530. (!ssr || optimizeAliasReplacementForSSR(resolvedId, optimizeDeps))) {
  45531. // aliased dep has not yet been optimized
  45532. const optimizedInfo = depsOptimizer.registerMissingImport(id, resolvedId);
  45533. return { id: depsOptimizer.getOptimizedDepId(optimizedInfo) };
  45534. }
  45535. }
  45536. return resolved;
  45537. }
  45538. }
  45539. },
  45540. };
  45541. }
  45542. function optimizeAliasReplacementForSSR(id, optimizeDeps) {
  45543. if (optimizeDeps.include?.includes(id)) {
  45544. return true;
  45545. }
  45546. // In the regular resolution, the default for non-external modules is to
  45547. // be optimized if they are CJS. Here, we don't have the package id but
  45548. // only the replacement file path. We could find the package.json from
  45549. // the id and respect the same default in the future.
  45550. // Default to not optimize an aliased replacement for now, forcing the
  45551. // user to explicitly add it to the ssr.optimizeDeps.include list.
  45552. return false;
  45553. }
  45554. // In sync with rollup plugin alias logic
  45555. function matches(pattern, importee) {
  45556. if (pattern instanceof RegExp) {
  45557. return pattern.test(importee);
  45558. }
  45559. if (importee.length < pattern.length) {
  45560. return false;
  45561. }
  45562. if (importee === pattern) {
  45563. return true;
  45564. }
  45565. return importee.startsWith(withTrailingSlash(pattern));
  45566. }
  45567. function getAliasPatterns(entries) {
  45568. if (!entries) {
  45569. return [];
  45570. }
  45571. if (Array.isArray(entries)) {
  45572. return entries.map((entry) => entry.find);
  45573. }
  45574. return Object.entries(entries).map(([find]) => find);
  45575. }
  45576. function getAliasPatternMatcher(entries) {
  45577. const patterns = getAliasPatterns(entries);
  45578. return (importee) => patterns.some((pattern) => matches(pattern, importee));
  45579. }
  45580. function err(e, pos) {
  45581. const error = new Error(e);
  45582. error.pos = pos;
  45583. return error;
  45584. }
  45585. function parseWorkerOptions(rawOpts, optsStartIndex) {
  45586. let opts = {};
  45587. try {
  45588. opts = evalValue(rawOpts);
  45589. }
  45590. catch {
  45591. throw err('Vite is unable to parse the worker options as the value is not static.' +
  45592. 'To ignore this error, please use /* @vite-ignore */ in the worker options.', optsStartIndex);
  45593. }
  45594. if (opts == null) {
  45595. return {};
  45596. }
  45597. if (typeof opts !== 'object') {
  45598. throw err(`Expected worker options to be an object, got ${typeof opts}`, optsStartIndex);
  45599. }
  45600. return opts;
  45601. }
  45602. function getWorkerType(raw, clean, i) {
  45603. const commaIndex = clean.indexOf(',', i);
  45604. if (commaIndex === -1) {
  45605. return 'classic';
  45606. }
  45607. const endIndex = clean.indexOf(')', i);
  45608. // case: ') ... ,' mean no worker options params
  45609. if (commaIndex > endIndex) {
  45610. return 'classic';
  45611. }
  45612. // need to find in comment code
  45613. const workerOptString = raw
  45614. .substring(commaIndex + 1, endIndex)
  45615. .replace(/\}[\s\S]*,/g, '}'); // strip trailing comma for parsing
  45616. const hasViteIgnore = hasViteIgnoreRE.test(workerOptString);
  45617. if (hasViteIgnore) {
  45618. return 'ignore';
  45619. }
  45620. // need to find in no comment code
  45621. const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim();
  45622. if (!cleanWorkerOptString.length) {
  45623. return 'classic';
  45624. }
  45625. const workerOpts = parseWorkerOptions(workerOptString, commaIndex + 1);
  45626. if (workerOpts.type && ['classic', 'module'].includes(workerOpts.type)) {
  45627. return workerOpts.type;
  45628. }
  45629. return 'classic';
  45630. }
  45631. function isIncludeWorkerImportMetaUrl(code) {
  45632. if ((code.includes('new Worker') || code.includes('new SharedWorker')) &&
  45633. code.includes('new URL') &&
  45634. code.includes(`import.meta.url`)) {
  45635. return true;
  45636. }
  45637. return false;
  45638. }
  45639. function workerImportMetaUrlPlugin(config) {
  45640. const isBuild = config.command === 'build';
  45641. let workerResolver;
  45642. const fsResolveOptions = {
  45643. ...config.resolve,
  45644. root: config.root,
  45645. isProduction: config.isProduction,
  45646. isBuild: config.command === 'build',
  45647. packageCache: config.packageCache,
  45648. ssrConfig: config.ssr,
  45649. asSrc: true,
  45650. };
  45651. return {
  45652. name: 'vite:worker-import-meta-url',
  45653. shouldTransformCachedModule({ code }) {
  45654. if (isBuild && config.build.watch && isIncludeWorkerImportMetaUrl(code)) {
  45655. return true;
  45656. }
  45657. },
  45658. async transform(code, id, options) {
  45659. if (!options?.ssr && isIncludeWorkerImportMetaUrl(code)) {
  45660. let s;
  45661. const cleanString = stripLiteral(code);
  45662. const workerImportMetaUrlRE = /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
  45663. let match;
  45664. while ((match = workerImportMetaUrlRE.exec(cleanString))) {
  45665. const [[, endIndex], [expStart, expEnd], [urlStart, urlEnd]] = match.indices;
  45666. const rawUrl = code.slice(urlStart, urlEnd);
  45667. // potential dynamic template string
  45668. if (rawUrl[0] === '`' && rawUrl.includes('${')) {
  45669. this.error(`\`new URL(url, import.meta.url)\` is not supported in dynamic template string.`, expStart);
  45670. }
  45671. s ||= new MagicString(code);
  45672. const workerType = getWorkerType(code, cleanString, endIndex);
  45673. const url = rawUrl.slice(1, -1);
  45674. let file;
  45675. if (url[0] === '.') {
  45676. file = path$o.resolve(path$o.dirname(id), url);
  45677. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45678. }
  45679. else {
  45680. workerResolver ??= config.createResolver({
  45681. extensions: [],
  45682. tryIndex: false,
  45683. preferRelative: true,
  45684. });
  45685. file = await workerResolver(url, id);
  45686. file ??=
  45687. url[0] === '/'
  45688. ? slash$1(path$o.join(config.publicDir, url))
  45689. : slash$1(path$o.resolve(path$o.dirname(id), url));
  45690. }
  45691. if (isBuild &&
  45692. config.isWorker &&
  45693. this.getModuleInfo(cleanUrl(file))?.isEntry) {
  45694. s.update(expStart, expEnd, 'self.location.href');
  45695. }
  45696. else {
  45697. let builtUrl;
  45698. if (isBuild) {
  45699. builtUrl = await workerFileToUrl(config, file);
  45700. }
  45701. else {
  45702. builtUrl = await fileToUrl$1(cleanUrl(file), config, this);
  45703. builtUrl = injectQuery(builtUrl, `${WORKER_FILE_ID}&type=${workerType}`);
  45704. }
  45705. s.update(expStart, expEnd,
  45706. // add `'' +` to skip vite:asset-import-meta-url plugin
  45707. `new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`);
  45708. }
  45709. }
  45710. if (s) {
  45711. return transformStableResult(s, id, config);
  45712. }
  45713. return null;
  45714. }
  45715. },
  45716. };
  45717. }
  45718. /**
  45719. * Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
  45720. *
  45721. * Supports template string with dynamic segments:
  45722. * ```
  45723. * new URL(`./dir/${name}.png`, import.meta.url)
  45724. * // transformed to
  45725. * import.meta.glob('./dir/**.png', { eager: true, import: 'default' })[`./dir/${name}.png`]
  45726. * ```
  45727. */
  45728. function assetImportMetaUrlPlugin(config) {
  45729. const { publicDir } = config;
  45730. let assetResolver;
  45731. const fsResolveOptions = {
  45732. ...config.resolve,
  45733. root: config.root,
  45734. isProduction: config.isProduction,
  45735. isBuild: config.command === 'build',
  45736. packageCache: config.packageCache,
  45737. ssrConfig: config.ssr,
  45738. asSrc: true,
  45739. };
  45740. return {
  45741. name: 'vite:asset-import-meta-url',
  45742. async transform(code, id, options) {
  45743. if (!options?.ssr &&
  45744. id !== preloadHelperId &&
  45745. id !== CLIENT_ENTRY &&
  45746. code.includes('new URL') &&
  45747. code.includes(`import.meta.url`)) {
  45748. let s;
  45749. const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg;
  45750. const cleanString = stripLiteral(code);
  45751. let match;
  45752. while ((match = assetImportMetaUrlRE.exec(cleanString))) {
  45753. const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices;
  45754. const rawUrl = code.slice(urlStart, urlEnd);
  45755. if (!s)
  45756. s = new MagicString(code);
  45757. // potential dynamic template string
  45758. if (rawUrl[0] === '`' && rawUrl.includes('${')) {
  45759. const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl);
  45760. const hasQueryDelimiter = queryDelimiterIndex !== -1;
  45761. const pureUrl = hasQueryDelimiter
  45762. ? rawUrl.slice(0, queryDelimiterIndex) + '`'
  45763. : rawUrl;
  45764. const queryString = hasQueryDelimiter
  45765. ? rawUrl.slice(queryDelimiterIndex, -1)
  45766. : '';
  45767. const ast = this.parse(pureUrl);
  45768. const templateLiteral = ast.body[0].expression;
  45769. if (templateLiteral.expressions.length) {
  45770. const pattern = buildGlobPattern(templateLiteral);
  45771. if (pattern.startsWith('**')) {
  45772. // don't transform for patterns like this
  45773. // because users won't intend to do that in most cases
  45774. continue;
  45775. }
  45776. const globOptions = {
  45777. eager: true,
  45778. import: 'default',
  45779. // A hack to allow 'as' & 'query' exist at the same time
  45780. query: injectQuery(queryString, 'url'),
  45781. };
  45782. s.update(startIndex, endIndex, `new URL((import.meta.glob(${JSON.stringify(pattern)}, ${JSON.stringify(globOptions)}))[${pureUrl}], import.meta.url)`);
  45783. continue;
  45784. }
  45785. }
  45786. const url = rawUrl.slice(1, -1);
  45787. let file;
  45788. if (url[0] === '.') {
  45789. file = slash$1(path$o.resolve(path$o.dirname(id), url));
  45790. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45791. }
  45792. else {
  45793. assetResolver ??= config.createResolver({
  45794. extensions: [],
  45795. mainFields: [],
  45796. tryIndex: false,
  45797. preferRelative: true,
  45798. });
  45799. file = await assetResolver(url, id);
  45800. file ??=
  45801. url[0] === '/'
  45802. ? slash$1(path$o.join(publicDir, url))
  45803. : slash$1(path$o.resolve(path$o.dirname(id), url));
  45804. }
  45805. // Get final asset URL. If the file does not exist,
  45806. // we fall back to the initial URL and let it resolve in runtime
  45807. let builtUrl;
  45808. if (file) {
  45809. try {
  45810. if (publicDir && isParentDirectory(publicDir, file)) {
  45811. const publicPath = '/' + path$o.posix.relative(publicDir, file);
  45812. builtUrl = await fileToUrl$1(publicPath, config, this);
  45813. }
  45814. else {
  45815. builtUrl = await fileToUrl$1(file, config, this);
  45816. }
  45817. }
  45818. catch {
  45819. // do nothing, we'll log a warning after this
  45820. }
  45821. }
  45822. if (!builtUrl) {
  45823. const rawExp = code.slice(startIndex, endIndex);
  45824. config.logger.warnOnce(`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime`);
  45825. builtUrl = url;
  45826. }
  45827. s.update(startIndex, endIndex, `new URL(${JSON.stringify(builtUrl)}, import.meta.url)`);
  45828. }
  45829. if (s) {
  45830. return transformStableResult(s, id, config);
  45831. }
  45832. }
  45833. return null;
  45834. },
  45835. };
  45836. }
  45837. function buildGlobPattern(ast) {
  45838. let pattern = '';
  45839. let lastElementIndex = -1;
  45840. for (const exp of ast.expressions) {
  45841. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45842. const el = ast.quasis[i];
  45843. if (el.end < exp.start) {
  45844. pattern += el.value.raw;
  45845. lastElementIndex = i;
  45846. }
  45847. }
  45848. pattern += '**';
  45849. }
  45850. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45851. pattern += ast.quasis[i].value.raw;
  45852. }
  45853. return pattern;
  45854. }
  45855. function getQueryDelimiterIndex(rawUrl) {
  45856. let bracketsStack = 0;
  45857. for (let i = 0; i < rawUrl.length; i++) {
  45858. if (rawUrl[i] === '{') {
  45859. bracketsStack++;
  45860. }
  45861. else if (rawUrl[i] === '}') {
  45862. bracketsStack--;
  45863. }
  45864. else if (rawUrl[i] === '?' && bracketsStack === 0) {
  45865. return i;
  45866. }
  45867. }
  45868. return -1;
  45869. }
  45870. /**
  45871. * Prepares the rendered chunks to contain additional metadata during build.
  45872. */
  45873. function metadataPlugin() {
  45874. return {
  45875. name: 'vite:build-metadata',
  45876. async renderChunk(_code, chunk) {
  45877. chunk.viteMetadata = {
  45878. importedAssets: new Set(),
  45879. importedCss: new Set(),
  45880. };
  45881. return null;
  45882. },
  45883. };
  45884. }
  45885. // Astring is a tiny and fast JavaScript code generator from an ESTree-compliant AST.
  45886. //
  45887. // Astring was written by David Bonnet and released under an MIT license.
  45888. //
  45889. // The Git repository for Astring is available at:
  45890. // https://github.com/davidbonnet/astring.git
  45891. //
  45892. // Please use the GitHub bug tracker to report issues:
  45893. // https://github.com/davidbonnet/astring/issues
  45894. /* c8 ignore if */
  45895. if (!String.prototype.repeat) {
  45896. /* c8 ignore next */
  45897. throw new Error(
  45898. 'String.prototype.repeat is undefined, see https://github.com/davidbonnet/astring#installation',
  45899. )
  45900. }
  45901. /* c8 ignore if */
  45902. if (!String.prototype.endsWith) {
  45903. /* c8 ignore next */
  45904. throw new Error(
  45905. 'String.prototype.endsWith is undefined, see https://github.com/davidbonnet/astring#installation',
  45906. )
  45907. }
  45908. class VariableDynamicImportError extends Error {}
  45909. /* eslint-disable-next-line no-template-curly-in-string */
  45910. const example = 'For example: import(`./foo/${bar}.js`).';
  45911. function sanitizeString(str) {
  45912. if (str === '') return str;
  45913. if (str.includes('*')) {
  45914. throw new VariableDynamicImportError('A dynamic import cannot contain * characters.');
  45915. }
  45916. return glob.escapePath(str);
  45917. }
  45918. function templateLiteralToGlob(node) {
  45919. let glob = '';
  45920. for (let i = 0; i < node.quasis.length; i += 1) {
  45921. glob += sanitizeString(node.quasis[i].value.raw);
  45922. if (node.expressions[i]) {
  45923. glob += expressionToGlob(node.expressions[i]);
  45924. }
  45925. }
  45926. return glob;
  45927. }
  45928. function callExpressionToGlob(node) {
  45929. const { callee } = node;
  45930. if (
  45931. callee.type === 'MemberExpression' &&
  45932. callee.property.type === 'Identifier' &&
  45933. callee.property.name === 'concat'
  45934. ) {
  45935. return `${expressionToGlob(callee.object)}${node.arguments.map(expressionToGlob).join('')}`;
  45936. }
  45937. return '*';
  45938. }
  45939. function binaryExpressionToGlob(node) {
  45940. if (node.operator !== '+') {
  45941. throw new VariableDynamicImportError(`${node.operator} operator is not supported.`);
  45942. }
  45943. return `${expressionToGlob(node.left)}${expressionToGlob(node.right)}`;
  45944. }
  45945. function expressionToGlob(node) {
  45946. switch (node.type) {
  45947. case 'TemplateLiteral':
  45948. return templateLiteralToGlob(node);
  45949. case 'CallExpression':
  45950. return callExpressionToGlob(node);
  45951. case 'BinaryExpression':
  45952. return binaryExpressionToGlob(node);
  45953. case 'Literal': {
  45954. return sanitizeString(node.value);
  45955. }
  45956. default:
  45957. return '*';
  45958. }
  45959. }
  45960. const defaultProtocol = 'file:';
  45961. const ignoredProtocols = ['data:', 'http:', 'https:'];
  45962. function shouldIgnore(glob) {
  45963. const containsAsterisk = glob.includes('*');
  45964. const globURL = new URL(glob, defaultProtocol);
  45965. const containsIgnoredProtocol = ignoredProtocols.some(
  45966. (ignoredProtocol) => ignoredProtocol === globURL.protocol
  45967. );
  45968. return !containsAsterisk || containsIgnoredProtocol;
  45969. }
  45970. function dynamicImportToGlob(node, sourceString) {
  45971. let glob = expressionToGlob(node);
  45972. if (shouldIgnore(glob)) {
  45973. return null;
  45974. }
  45975. glob = glob.replace(/\*\*/g, '*');
  45976. if (glob.startsWith('*')) {
  45977. throw new VariableDynamicImportError(
  45978. `invalid import "${sourceString}". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. ${example}`
  45979. );
  45980. }
  45981. if (glob.startsWith('/')) {
  45982. throw new VariableDynamicImportError(
  45983. `invalid import "${sourceString}". Variable absolute imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45984. );
  45985. }
  45986. if (!glob.startsWith('./') && !glob.startsWith('../')) {
  45987. throw new VariableDynamicImportError(
  45988. `invalid import "${sourceString}". Variable bare imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45989. );
  45990. }
  45991. // Disallow ./*.ext
  45992. const ownDirectoryStarExtension = /^\.\/\*\.[\w]+$/;
  45993. if (ownDirectoryStarExtension.test(glob)) {
  45994. throw new VariableDynamicImportError(
  45995. `${
  45996. `invalid import "${sourceString}". Variable imports cannot import their own directory, ` +
  45997. 'place imports in a separate directory or make the import filename more specific. '
  45998. }${example}`
  45999. );
  46000. }
  46001. if (require$$0$4.extname(glob) === '') {
  46002. throw new VariableDynamicImportError(
  46003. `invalid import "${sourceString}". A file extension must be included in the static part of the import. ${example}`
  46004. );
  46005. }
  46006. return glob;
  46007. }
  46008. const dynamicImportHelperId = '\0vite/dynamic-import-helper.js';
  46009. const relativePathRE = /^\.{1,2}\//;
  46010. // fast path to check if source contains a dynamic import. we check for a
  46011. // trailing slash too as a dynamic import statement can have comments between
  46012. // the `import` and the `(`.
  46013. const hasDynamicImportRE = /\bimport\s*[(/]/;
  46014. const dynamicImportHelper = (glob, path) => {
  46015. const v = glob[path];
  46016. if (v) {
  46017. return typeof v === 'function' ? v() : Promise.resolve(v);
  46018. }
  46019. return new Promise((_, reject) => {
  46020. (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(reject.bind(null, new Error('Unknown variable dynamic import: ' + path)));
  46021. });
  46022. };
  46023. function parseDynamicImportPattern(strings) {
  46024. const filename = strings.slice(1, -1);
  46025. const ast = parse$9(strings, {
  46026. ecmaVersion: 'latest',
  46027. sourceType: 'module',
  46028. }).body[0].expression;
  46029. const userPatternQuery = dynamicImportToGlob(ast, filename);
  46030. if (!userPatternQuery) {
  46031. return null;
  46032. }
  46033. const [userPattern] = userPatternQuery.split(
  46034. // ? is escaped on posix OS
  46035. requestQueryMaybeEscapedSplitRE, 2);
  46036. let [rawPattern, search] = filename.split(requestQuerySplitRE, 2);
  46037. let globParams = null;
  46038. if (search) {
  46039. search = '?' + search;
  46040. if (workerOrSharedWorkerRE.test(search) ||
  46041. urlRE.test(search) ||
  46042. rawRE.test(search)) {
  46043. globParams = {
  46044. query: search,
  46045. import: '*',
  46046. };
  46047. }
  46048. else {
  46049. globParams = {
  46050. query: search,
  46051. };
  46052. }
  46053. }
  46054. return {
  46055. globParams,
  46056. userPattern,
  46057. rawPattern,
  46058. };
  46059. }
  46060. async function transformDynamicImport(importSource, importer, resolve, root) {
  46061. if (importSource[1] !== '.' && importSource[1] !== '/') {
  46062. const resolvedFileName = await resolve(importSource.slice(1, -1), importer);
  46063. if (!resolvedFileName) {
  46064. return null;
  46065. }
  46066. const relativeFileName = posix$1.relative(posix$1.dirname(normalizePath$3(importer)), normalizePath$3(resolvedFileName));
  46067. importSource = normalizePath$3('`' + (relativeFileName[0] === '.' ? '' : './') + relativeFileName + '`');
  46068. }
  46069. const dynamicImportPattern = parseDynamicImportPattern(importSource);
  46070. if (!dynamicImportPattern) {
  46071. return null;
  46072. }
  46073. const { globParams, rawPattern, userPattern } = dynamicImportPattern;
  46074. const params = globParams ? `, ${JSON.stringify(globParams)}` : '';
  46075. let newRawPattern = posix$1.relative(posix$1.dirname(importer), await toAbsoluteGlob(rawPattern, root, importer, resolve));
  46076. if (!relativePathRE.test(newRawPattern)) {
  46077. newRawPattern = `./${newRawPattern}`;
  46078. }
  46079. const exp = `(import.meta.glob(${JSON.stringify(userPattern)}${params}))`;
  46080. return {
  46081. rawPattern: newRawPattern,
  46082. pattern: userPattern,
  46083. glob: exp,
  46084. };
  46085. }
  46086. function dynamicImportVarsPlugin(config) {
  46087. const resolve = config.createResolver({
  46088. preferRelative: true,
  46089. tryIndex: false,
  46090. extensions: [],
  46091. });
  46092. const { include, exclude, warnOnError } = config.build.dynamicImportVarsOptions;
  46093. const filter = createFilter(include, exclude);
  46094. return {
  46095. name: 'vite:dynamic-import-vars',
  46096. resolveId(id) {
  46097. if (id === dynamicImportHelperId) {
  46098. return id;
  46099. }
  46100. },
  46101. load(id) {
  46102. if (id === dynamicImportHelperId) {
  46103. return 'export default ' + dynamicImportHelper.toString();
  46104. }
  46105. },
  46106. async transform(source, importer) {
  46107. if (!filter(importer) ||
  46108. importer === CLIENT_ENTRY ||
  46109. !hasDynamicImportRE.test(source)) {
  46110. return;
  46111. }
  46112. await init;
  46113. let imports = [];
  46114. try {
  46115. imports = parse$e(source)[0];
  46116. }
  46117. catch (e) {
  46118. // ignore as it might not be a JS file, the subsequent plugins will catch the error
  46119. return null;
  46120. }
  46121. if (!imports.length) {
  46122. return null;
  46123. }
  46124. let s;
  46125. let needDynamicImportHelper = false;
  46126. for (let index = 0; index < imports.length; index++) {
  46127. const { s: start, e: end, ss: expStart, se: expEnd, d: dynamicIndex, } = imports[index];
  46128. if (dynamicIndex === -1 || source[start] !== '`') {
  46129. continue;
  46130. }
  46131. if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) {
  46132. continue;
  46133. }
  46134. s ||= new MagicString(source);
  46135. let result;
  46136. try {
  46137. result = await transformDynamicImport(source.slice(start, end), importer, resolve, config.root);
  46138. }
  46139. catch (error) {
  46140. if (warnOnError) {
  46141. this.warn(error);
  46142. }
  46143. else {
  46144. this.error(error);
  46145. }
  46146. }
  46147. if (!result) {
  46148. continue;
  46149. }
  46150. const { rawPattern, glob } = result;
  46151. needDynamicImportHelper = true;
  46152. s.overwrite(expStart, expEnd, `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`);
  46153. }
  46154. if (s) {
  46155. if (needDynamicImportHelper) {
  46156. s.prepend(`import __variableDynamicImportRuntimeHelper from "${dynamicImportHelperId}";`);
  46157. }
  46158. return transformStableResult(s, importer, config);
  46159. }
  46160. },
  46161. };
  46162. }
  46163. async function resolvePlugins(config, prePlugins, normalPlugins, postPlugins) {
  46164. const isBuild = config.command === 'build';
  46165. const isWorker = config.isWorker;
  46166. const buildPlugins = isBuild
  46167. ? await (await Promise.resolve().then(function () { return build$1; })).resolveBuildPlugins(config)
  46168. : { pre: [], post: [] };
  46169. const { modulePreload } = config.build;
  46170. const depsOptimizerEnabled = !isBuild &&
  46171. (isDepsOptimizerEnabled(config, false) ||
  46172. isDepsOptimizerEnabled(config, true));
  46173. return [
  46174. depsOptimizerEnabled ? optimizedDepsPlugin(config) : null,
  46175. isBuild ? metadataPlugin() : null,
  46176. !isWorker ? watchPackageDataPlugin(config.packageCache) : null,
  46177. preAliasPlugin(config),
  46178. alias$1({
  46179. entries: config.resolve.alias,
  46180. customResolver: viteAliasCustomResolver,
  46181. }),
  46182. ...prePlugins,
  46183. modulePreload !== false && modulePreload.polyfill
  46184. ? modulePreloadPolyfillPlugin(config)
  46185. : null,
  46186. resolvePlugin({
  46187. ...config.resolve,
  46188. root: config.root,
  46189. isProduction: config.isProduction,
  46190. isBuild,
  46191. packageCache: config.packageCache,
  46192. ssrConfig: config.ssr,
  46193. asSrc: true,
  46194. fsUtils: getFsUtils(config),
  46195. getDepsOptimizer: isBuild
  46196. ? undefined
  46197. : (ssr) => getDepsOptimizer(config, ssr),
  46198. shouldExternalize: isBuild && config.build.ssr
  46199. ? (id, importer) => shouldExternalizeForSSR(id, importer, config)
  46200. : undefined,
  46201. }),
  46202. htmlInlineProxyPlugin(config),
  46203. cssPlugin(config),
  46204. config.esbuild !== false ? esbuildPlugin(config) : null,
  46205. jsonPlugin({
  46206. namedExports: true,
  46207. ...config.json,
  46208. }, isBuild),
  46209. wasmHelperPlugin(config),
  46210. webWorkerPlugin(config),
  46211. assetPlugin(config),
  46212. ...normalPlugins,
  46213. wasmFallbackPlugin(),
  46214. definePlugin(config),
  46215. cssPostPlugin(config),
  46216. isBuild && buildHtmlPlugin(config),
  46217. workerImportMetaUrlPlugin(config),
  46218. assetImportMetaUrlPlugin(config),
  46219. ...buildPlugins.pre,
  46220. dynamicImportVarsPlugin(config),
  46221. importGlobPlugin(config),
  46222. ...postPlugins,
  46223. ...buildPlugins.post,
  46224. // internal server-only plugins are always applied after everything else
  46225. ...(isBuild
  46226. ? []
  46227. : [
  46228. clientInjectionsPlugin(config),
  46229. cssAnalysisPlugin(config),
  46230. importAnalysisPlugin(config),
  46231. ]),
  46232. ].filter(Boolean);
  46233. }
  46234. function createPluginHookUtils(plugins) {
  46235. // sort plugins per hook
  46236. const sortedPluginsCache = new Map();
  46237. function getSortedPlugins(hookName) {
  46238. if (sortedPluginsCache.has(hookName))
  46239. return sortedPluginsCache.get(hookName);
  46240. const sorted = getSortedPluginsByHook(hookName, plugins);
  46241. sortedPluginsCache.set(hookName, sorted);
  46242. return sorted;
  46243. }
  46244. function getSortedPluginHooks(hookName) {
  46245. const plugins = getSortedPlugins(hookName);
  46246. return plugins.map((p) => getHookHandler(p[hookName])).filter(Boolean);
  46247. }
  46248. return {
  46249. getSortedPlugins,
  46250. getSortedPluginHooks,
  46251. };
  46252. }
  46253. function getSortedPluginsByHook(hookName, plugins) {
  46254. const sortedPlugins = [];
  46255. // Use indexes to track and insert the ordered plugins directly in the
  46256. // resulting array to avoid creating 3 extra temporary arrays per hook
  46257. let pre = 0, normal = 0, post = 0;
  46258. for (const plugin of plugins) {
  46259. const hook = plugin[hookName];
  46260. if (hook) {
  46261. if (typeof hook === 'object') {
  46262. if (hook.order === 'pre') {
  46263. sortedPlugins.splice(pre++, 0, plugin);
  46264. continue;
  46265. }
  46266. if (hook.order === 'post') {
  46267. sortedPlugins.splice(pre + normal + post++, 0, plugin);
  46268. continue;
  46269. }
  46270. }
  46271. sortedPlugins.splice(pre + normal++, 0, plugin);
  46272. }
  46273. }
  46274. return sortedPlugins;
  46275. }
  46276. function getHookHandler(hook) {
  46277. return (typeof hook === 'object' ? hook.handler : hook);
  46278. }
  46279. // Same as `@rollup/plugin-alias` default resolver, but we attach additional meta
  46280. // if we can't resolve to something, which will error in `importAnalysis`
  46281. const viteAliasCustomResolver = async function (id, importer, options) {
  46282. const resolved = await this.resolve(id, importer, options);
  46283. return resolved || { id, meta: { 'vite:alias': { noResolved: true } } };
  46284. };
  46285. function ansiRegex({onlyFirst = false} = {}) {
  46286. const pattern = [
  46287. '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
  46288. '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
  46289. ].join('|');
  46290. return new RegExp(pattern, onlyFirst ? undefined : 'g');
  46291. }
  46292. const regex = ansiRegex();
  46293. function stripAnsi(string) {
  46294. if (typeof string !== 'string') {
  46295. throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
  46296. }
  46297. // Even though the regex is global, we don't need to reset the `.lastIndex`
  46298. // because unlike `.exec()` and `.test()`, `.replace()` does it automatically
  46299. // and doing it manually has a performance penalty.
  46300. return string.replace(regex, '');
  46301. }
  46302. function prepareError(err) {
  46303. // only copy the information we need and avoid serializing unnecessary
  46304. // properties, since some errors may attach full objects (e.g. PostCSS)
  46305. return {
  46306. message: stripAnsi(err.message),
  46307. stack: stripAnsi(cleanStack(err.stack || '')),
  46308. id: err.id,
  46309. frame: stripAnsi(err.frame || ''),
  46310. plugin: err.plugin,
  46311. pluginCode: err.pluginCode?.toString(),
  46312. loc: err.loc,
  46313. };
  46314. }
  46315. function buildErrorMessage(err, args = [], includeStack = true) {
  46316. if (err.plugin)
  46317. args.push(` Plugin: ${colors$1.magenta(err.plugin)}`);
  46318. const loc = err.loc ? `:${err.loc.line}:${err.loc.column}` : '';
  46319. if (err.id)
  46320. args.push(` File: ${colors$1.cyan(err.id)}${loc}`);
  46321. if (err.frame)
  46322. args.push(colors$1.yellow(pad$1(err.frame)));
  46323. if (includeStack && err.stack)
  46324. args.push(pad$1(cleanStack(err.stack)));
  46325. return args.join('\n');
  46326. }
  46327. function cleanStack(stack) {
  46328. return stack
  46329. .split(/\n/g)
  46330. .filter((l) => /^\s*at/.test(l))
  46331. .join('\n');
  46332. }
  46333. function logError(server, err) {
  46334. const msg = buildErrorMessage(err, [
  46335. colors$1.red(`Internal server error: ${err.message}`),
  46336. ]);
  46337. server.config.logger.error(msg, {
  46338. clear: true,
  46339. timestamp: true,
  46340. error: err,
  46341. });
  46342. server.hot.send({
  46343. type: 'error',
  46344. err: prepareError(err),
  46345. });
  46346. }
  46347. function errorMiddleware(server, allowNext = false) {
  46348. // note the 4 args must be kept for connect to treat this as error middleware
  46349. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  46350. return function viteErrorMiddleware(err, _req, res, next) {
  46351. logError(server, err);
  46352. if (allowNext) {
  46353. next();
  46354. }
  46355. else {
  46356. res.statusCode = 500;
  46357. res.end(`
  46358. <!DOCTYPE html>
  46359. <html lang="en">
  46360. <head>
  46361. <meta charset="UTF-8" />
  46362. <title>Error</title>
  46363. <script type="module">
  46364. import { ErrorOverlay } from '/@vite/client'
  46365. document.body.appendChild(new ErrorOverlay(${JSON.stringify(prepareError(err)).replace(/</g, '\\u003c')}))
  46366. </script>
  46367. </head>
  46368. <body>
  46369. </body>
  46370. </html>
  46371. `);
  46372. }
  46373. };
  46374. }
  46375. /**
  46376. * This file is refactored into TypeScript based on
  46377. * https://github.com/preactjs/wmr/blob/main/packages/wmr/src/lib/rollup-plugin-container.js
  46378. */
  46379. /**
  46380. https://github.com/preactjs/wmr/blob/master/LICENSE
  46381. MIT License
  46382. Copyright (c) 2020 The Preact Authors
  46383. Permission is hereby granted, free of charge, to any person obtaining a copy
  46384. of this software and associated documentation files (the "Software"), to deal
  46385. in the Software without restriction, including without limitation the rights
  46386. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  46387. copies of the Software, and to permit persons to whom the Software is
  46388. furnished to do so, subject to the following conditions:
  46389. The above copyright notice and this permission notice shall be included in all
  46390. copies or substantial portions of the Software.
  46391. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46392. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46393. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  46394. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  46395. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  46396. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  46397. SOFTWARE.
  46398. */
  46399. const noop$2 = () => { };
  46400. const ERR_CLOSED_SERVER = 'ERR_CLOSED_SERVER';
  46401. function throwClosedServerError() {
  46402. const err = new Error('The server is being restarted or closed. Request is outdated');
  46403. err.code = ERR_CLOSED_SERVER;
  46404. // This error will be caught by the transform middleware that will
  46405. // send a 504 status code request timeout
  46406. throw err;
  46407. }
  46408. async function createPluginContainer(config, moduleGraph, watcher) {
  46409. const { plugins, logger, root, build: { rollupOptions }, } = config;
  46410. const { getSortedPluginHooks, getSortedPlugins } = createPluginHookUtils(plugins);
  46411. const seenResolves = {};
  46412. const debugResolve = createDebugger('vite:resolve');
  46413. const debugPluginResolve = createDebugger('vite:plugin-resolve', {
  46414. onlyWhenFocused: 'vite:plugin',
  46415. });
  46416. const debugPluginTransform = createDebugger('vite:plugin-transform', {
  46417. onlyWhenFocused: 'vite:plugin',
  46418. });
  46419. const debugSourcemapCombineFilter = process.env.DEBUG_VITE_SOURCEMAP_COMBINE_FILTER;
  46420. const debugSourcemapCombine = createDebugger('vite:sourcemap-combine', {
  46421. onlyWhenFocused: true,
  46422. });
  46423. // ---------------------------------------------------------------------------
  46424. const watchFiles = new Set();
  46425. // _addedFiles from the `load()` hook gets saved here so it can be reused in the `transform()` hook
  46426. const moduleNodeToLoadAddedImports = new WeakMap();
  46427. const minimalContext = {
  46428. meta: {
  46429. rollupVersion,
  46430. watchMode: true,
  46431. },
  46432. debug: noop$2,
  46433. info: noop$2,
  46434. warn: noop$2,
  46435. // @ts-expect-error noop
  46436. error: noop$2,
  46437. };
  46438. function warnIncompatibleMethod(method, plugin) {
  46439. logger.warn(colors$1.cyan(`[plugin:${plugin}] `) +
  46440. colors$1.yellow(`context method ${colors$1.bold(`${method}()`)} is not supported in serve mode. This plugin is likely not vite-compatible.`));
  46441. }
  46442. // parallel, ignores returns
  46443. async function hookParallel(hookName, context, args) {
  46444. const parallelPromises = [];
  46445. for (const plugin of getSortedPlugins(hookName)) {
  46446. // Don't throw here if closed, so buildEnd and closeBundle hooks can finish running
  46447. const hook = plugin[hookName];
  46448. if (!hook)
  46449. continue;
  46450. const handler = getHookHandler(hook);
  46451. if (hook.sequential) {
  46452. await Promise.all(parallelPromises);
  46453. parallelPromises.length = 0;
  46454. await handler.apply(context(plugin), args(plugin));
  46455. }
  46456. else {
  46457. parallelPromises.push(handler.apply(context(plugin), args(plugin)));
  46458. }
  46459. }
  46460. await Promise.all(parallelPromises);
  46461. }
  46462. // throw when an unsupported ModuleInfo property is accessed,
  46463. // so that incompatible plugins fail in a non-cryptic way.
  46464. const ModuleInfoProxy = {
  46465. get(info, key) {
  46466. if (key in info) {
  46467. return info[key];
  46468. }
  46469. // Don't throw an error when returning from an async function
  46470. if (key === 'then') {
  46471. return undefined;
  46472. }
  46473. throw Error(`[vite] The "${key}" property of ModuleInfo is not supported.`);
  46474. },
  46475. };
  46476. // same default value of "moduleInfo.meta" as in Rollup
  46477. const EMPTY_OBJECT = Object.freeze({});
  46478. function getModuleInfo(id) {
  46479. const module = moduleGraph?.getModuleById(id);
  46480. if (!module) {
  46481. return null;
  46482. }
  46483. if (!module.info) {
  46484. module.info = new Proxy({ id, meta: module.meta || EMPTY_OBJECT }, ModuleInfoProxy);
  46485. }
  46486. return module.info;
  46487. }
  46488. function updateModuleInfo(id, { meta }) {
  46489. if (meta) {
  46490. const moduleInfo = getModuleInfo(id);
  46491. if (moduleInfo) {
  46492. moduleInfo.meta = { ...moduleInfo.meta, ...meta };
  46493. }
  46494. }
  46495. }
  46496. function updateModuleLoadAddedImports(id, ctx) {
  46497. const module = moduleGraph?.getModuleById(id);
  46498. if (module) {
  46499. moduleNodeToLoadAddedImports.set(module, ctx._addedImports);
  46500. }
  46501. }
  46502. // we should create a new context for each async hook pipeline so that the
  46503. // active plugin in that pipeline can be tracked in a concurrency-safe manner.
  46504. // using a class to make creating new contexts more efficient
  46505. class Context {
  46506. meta = minimalContext.meta;
  46507. ssr = false;
  46508. _scan = false;
  46509. _activePlugin;
  46510. _activeId = null;
  46511. _activeCode = null;
  46512. _resolveSkips;
  46513. _addedImports = null;
  46514. constructor(initialPlugin) {
  46515. this._activePlugin = initialPlugin || null;
  46516. }
  46517. parse(code, opts) {
  46518. return parseAst(code, opts);
  46519. }
  46520. async resolve(id, importer, options) {
  46521. let skip;
  46522. if (options?.skipSelf !== false && this._activePlugin) {
  46523. skip = new Set(this._resolveSkips);
  46524. skip.add(this._activePlugin);
  46525. }
  46526. let out = await container.resolveId(id, importer, {
  46527. attributes: options?.attributes,
  46528. custom: options?.custom,
  46529. isEntry: !!options?.isEntry,
  46530. skip,
  46531. ssr: this.ssr,
  46532. scan: this._scan,
  46533. });
  46534. if (typeof out === 'string')
  46535. out = { id: out };
  46536. return out;
  46537. }
  46538. async load(options) {
  46539. // We may not have added this to our module graph yet, so ensure it exists
  46540. await moduleGraph?.ensureEntryFromUrl(unwrapId$1(options.id), this.ssr);
  46541. // Not all options passed to this function make sense in the context of loading individual files,
  46542. // but we can at least update the module info properties we support
  46543. updateModuleInfo(options.id, options);
  46544. const loadResult = await container.load(options.id, { ssr: this.ssr });
  46545. const code = typeof loadResult === 'object' ? loadResult?.code : loadResult;
  46546. if (code != null) {
  46547. await container.transform(code, options.id, { ssr: this.ssr });
  46548. }
  46549. const moduleInfo = this.getModuleInfo(options.id);
  46550. // This shouldn't happen due to calling ensureEntryFromUrl, but 1) our types can't ensure that
  46551. // and 2) moduleGraph may not have been provided (though in the situations where that happens,
  46552. // we should never have plugins calling this.load)
  46553. if (!moduleInfo)
  46554. throw Error(`Failed to load module with id ${options.id}`);
  46555. return moduleInfo;
  46556. }
  46557. getModuleInfo(id) {
  46558. return getModuleInfo(id);
  46559. }
  46560. getModuleIds() {
  46561. return moduleGraph
  46562. ? moduleGraph.idToModuleMap.keys()
  46563. : Array.prototype[Symbol.iterator]();
  46564. }
  46565. addWatchFile(id) {
  46566. watchFiles.add(id);
  46567. (this._addedImports || (this._addedImports = new Set())).add(id);
  46568. if (watcher)
  46569. ensureWatchedFile(watcher, id, root);
  46570. }
  46571. getWatchFiles() {
  46572. return [...watchFiles];
  46573. }
  46574. emitFile(assetOrFile) {
  46575. warnIncompatibleMethod(`emitFile`, this._activePlugin.name);
  46576. return '';
  46577. }
  46578. setAssetSource() {
  46579. warnIncompatibleMethod(`setAssetSource`, this._activePlugin.name);
  46580. }
  46581. getFileName() {
  46582. warnIncompatibleMethod(`getFileName`, this._activePlugin.name);
  46583. return '';
  46584. }
  46585. warn(e, position) {
  46586. const err = formatError(typeof e === 'function' ? e() : e, position, this);
  46587. const msg = buildErrorMessage(err, [colors$1.yellow(`warning: ${err.message}`)], false);
  46588. logger.warn(msg, {
  46589. clear: true,
  46590. timestamp: true,
  46591. });
  46592. }
  46593. error(e, position) {
  46594. // error thrown here is caught by the transform middleware and passed on
  46595. // the the error middleware.
  46596. throw formatError(e, position, this);
  46597. }
  46598. debug = noop$2;
  46599. info = noop$2;
  46600. }
  46601. function formatError(e, position, ctx) {
  46602. const err = (typeof e === 'string' ? new Error(e) : e);
  46603. if (err.pluginCode) {
  46604. return err; // The plugin likely called `this.error`
  46605. }
  46606. if (ctx._activePlugin)
  46607. err.plugin = ctx._activePlugin.name;
  46608. if (ctx._activeId && !err.id)
  46609. err.id = ctx._activeId;
  46610. if (ctx._activeCode) {
  46611. err.pluginCode = ctx._activeCode;
  46612. // some rollup plugins, e.g. json, sets err.position instead of err.pos
  46613. const pos = position ?? err.pos ?? err.position;
  46614. if (pos != null) {
  46615. let errLocation;
  46616. try {
  46617. errLocation = numberToPos(ctx._activeCode, pos);
  46618. }
  46619. catch (err2) {
  46620. logger.error(colors$1.red(`Error in error handler:\n${err2.stack || err2.message}\n`),
  46621. // print extra newline to separate the two errors
  46622. { error: err2 });
  46623. throw err;
  46624. }
  46625. err.loc = err.loc || {
  46626. file: err.id,
  46627. ...errLocation,
  46628. };
  46629. err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos);
  46630. }
  46631. else if (err.loc) {
  46632. // css preprocessors may report errors in an included file
  46633. if (!err.frame) {
  46634. let code = ctx._activeCode;
  46635. if (err.loc.file) {
  46636. err.id = normalizePath$3(err.loc.file);
  46637. try {
  46638. code = fs$l.readFileSync(err.loc.file, 'utf-8');
  46639. }
  46640. catch { }
  46641. }
  46642. err.frame = generateCodeFrame(code, err.loc);
  46643. }
  46644. }
  46645. else if (err.line && err.column) {
  46646. err.loc = {
  46647. file: err.id,
  46648. line: err.line,
  46649. column: err.column,
  46650. };
  46651. err.frame = err.frame || generateCodeFrame(ctx._activeCode, err.loc);
  46652. }
  46653. if (ctx instanceof TransformContext &&
  46654. typeof err.loc?.line === 'number' &&
  46655. typeof err.loc?.column === 'number') {
  46656. const rawSourceMap = ctx._getCombinedSourcemap();
  46657. if (rawSourceMap && 'version' in rawSourceMap) {
  46658. const traced = new TraceMap(rawSourceMap);
  46659. const { source, line, column } = originalPositionFor$1(traced, {
  46660. line: Number(err.loc.line),
  46661. column: Number(err.loc.column),
  46662. });
  46663. if (source && line != null && column != null) {
  46664. err.loc = { file: source, line, column };
  46665. }
  46666. }
  46667. }
  46668. }
  46669. else if (err.loc) {
  46670. if (!err.frame) {
  46671. let code = err.pluginCode;
  46672. if (err.loc.file) {
  46673. err.id = normalizePath$3(err.loc.file);
  46674. if (!code) {
  46675. try {
  46676. code = fs$l.readFileSync(err.loc.file, 'utf-8');
  46677. }
  46678. catch { }
  46679. }
  46680. }
  46681. if (code) {
  46682. err.frame = generateCodeFrame(`${code}`, err.loc);
  46683. }
  46684. }
  46685. }
  46686. if (typeof err.loc?.column !== 'number' &&
  46687. typeof err.loc?.line !== 'number' &&
  46688. !err.loc?.file) {
  46689. delete err.loc;
  46690. }
  46691. return err;
  46692. }
  46693. class TransformContext extends Context {
  46694. filename;
  46695. originalCode;
  46696. originalSourcemap = null;
  46697. sourcemapChain = [];
  46698. combinedMap = null;
  46699. constructor(id, code, inMap) {
  46700. super();
  46701. this.filename = id;
  46702. this.originalCode = code;
  46703. if (inMap) {
  46704. if (debugSourcemapCombine) {
  46705. // @ts-expect-error inject name for debug purpose
  46706. inMap.name = '$inMap';
  46707. }
  46708. this.sourcemapChain.push(inMap);
  46709. }
  46710. // Inherit `_addedImports` from the `load()` hook
  46711. const node = moduleGraph?.getModuleById(id);
  46712. if (node) {
  46713. this._addedImports = moduleNodeToLoadAddedImports.get(node) ?? null;
  46714. }
  46715. }
  46716. _getCombinedSourcemap() {
  46717. if (debugSourcemapCombine &&
  46718. debugSourcemapCombineFilter &&
  46719. this.filename.includes(debugSourcemapCombineFilter)) {
  46720. debugSourcemapCombine('----------', this.filename);
  46721. debugSourcemapCombine(this.combinedMap);
  46722. debugSourcemapCombine(this.sourcemapChain);
  46723. debugSourcemapCombine('----------');
  46724. }
  46725. let combinedMap = this.combinedMap;
  46726. // { mappings: '' }
  46727. if (combinedMap &&
  46728. !('version' in combinedMap) &&
  46729. combinedMap.mappings === '') {
  46730. this.sourcemapChain.length = 0;
  46731. return combinedMap;
  46732. }
  46733. for (let m of this.sourcemapChain) {
  46734. if (typeof m === 'string')
  46735. m = JSON.parse(m);
  46736. if (!('version' in m)) {
  46737. // { mappings: '' }
  46738. if (m.mappings === '') {
  46739. combinedMap = { mappings: '' };
  46740. break;
  46741. }
  46742. // empty, nullified source map
  46743. combinedMap = null;
  46744. break;
  46745. }
  46746. if (!combinedMap) {
  46747. const sm = m;
  46748. // sourcemap should not include `sources: [null]` (because `sources` should be string) nor
  46749. // `sources: ['']` (because `''` means the path of sourcemap)
  46750. // but MagicString generates this when `filename` option is not set.
  46751. // Rollup supports these and therefore we support this as well
  46752. if (sm.sources.length === 1 && !sm.sources[0]) {
  46753. combinedMap = {
  46754. ...sm,
  46755. sources: [this.filename],
  46756. sourcesContent: [this.originalCode],
  46757. };
  46758. }
  46759. else {
  46760. combinedMap = sm;
  46761. }
  46762. }
  46763. else {
  46764. combinedMap = combineSourcemaps(cleanUrl(this.filename), [
  46765. m,
  46766. combinedMap,
  46767. ]);
  46768. }
  46769. }
  46770. if (combinedMap !== this.combinedMap) {
  46771. this.combinedMap = combinedMap;
  46772. this.sourcemapChain.length = 0;
  46773. }
  46774. return this.combinedMap;
  46775. }
  46776. getCombinedSourcemap() {
  46777. const map = this._getCombinedSourcemap();
  46778. if (!map || (!('version' in map) && map.mappings === '')) {
  46779. return new MagicString(this.originalCode).generateMap({
  46780. includeContent: true,
  46781. hires: 'boundary',
  46782. source: cleanUrl(this.filename),
  46783. });
  46784. }
  46785. return map;
  46786. }
  46787. }
  46788. let closed = false;
  46789. const processesing = new Set();
  46790. // keeps track of hook promises so that we can wait for them all to finish upon closing the server
  46791. function handleHookPromise(maybePromise) {
  46792. if (!maybePromise?.then) {
  46793. return maybePromise;
  46794. }
  46795. const promise = maybePromise;
  46796. processesing.add(promise);
  46797. return promise.finally(() => processesing.delete(promise));
  46798. }
  46799. const container = {
  46800. options: await (async () => {
  46801. let options = rollupOptions;
  46802. for (const optionsHook of getSortedPluginHooks('options')) {
  46803. if (closed)
  46804. throwClosedServerError();
  46805. options =
  46806. (await handleHookPromise(optionsHook.call(minimalContext, options))) || options;
  46807. }
  46808. return options;
  46809. })(),
  46810. getModuleInfo,
  46811. async buildStart() {
  46812. await handleHookPromise(hookParallel('buildStart', (plugin) => new Context(plugin), () => [container.options]));
  46813. },
  46814. async resolveId(rawId, importer = join$2(root, 'index.html'), options) {
  46815. const skip = options?.skip;
  46816. const ssr = options?.ssr;
  46817. const scan = !!options?.scan;
  46818. const ctx = new Context();
  46819. ctx.ssr = !!ssr;
  46820. ctx._scan = scan;
  46821. ctx._resolveSkips = skip;
  46822. const resolveStart = debugResolve ? performance.now() : 0;
  46823. let id = null;
  46824. const partial = {};
  46825. for (const plugin of getSortedPlugins('resolveId')) {
  46826. if (closed && !ssr)
  46827. throwClosedServerError();
  46828. if (!plugin.resolveId)
  46829. continue;
  46830. if (skip?.has(plugin))
  46831. continue;
  46832. ctx._activePlugin = plugin;
  46833. const pluginResolveStart = debugPluginResolve ? performance.now() : 0;
  46834. const handler = getHookHandler(plugin.resolveId);
  46835. const result = await handleHookPromise(handler.call(ctx, rawId, importer, {
  46836. attributes: options?.attributes ?? {},
  46837. custom: options?.custom,
  46838. isEntry: !!options?.isEntry,
  46839. ssr,
  46840. scan,
  46841. }));
  46842. if (!result)
  46843. continue;
  46844. if (typeof result === 'string') {
  46845. id = result;
  46846. }
  46847. else {
  46848. id = result.id;
  46849. Object.assign(partial, result);
  46850. }
  46851. debugPluginResolve?.(timeFrom(pluginResolveStart), plugin.name, prettifyUrl(id, root));
  46852. // resolveId() is hookFirst - first non-null result is returned.
  46853. break;
  46854. }
  46855. if (debugResolve && rawId !== id && !rawId.startsWith(FS_PREFIX)) {
  46856. const key = rawId + id;
  46857. // avoid spamming
  46858. if (!seenResolves[key]) {
  46859. seenResolves[key] = true;
  46860. debugResolve(`${timeFrom(resolveStart)} ${colors$1.cyan(rawId)} -> ${colors$1.dim(id)}`);
  46861. }
  46862. }
  46863. if (id) {
  46864. partial.id = isExternalUrl(id) ? id : normalizePath$3(id);
  46865. return partial;
  46866. }
  46867. else {
  46868. return null;
  46869. }
  46870. },
  46871. async load(id, options) {
  46872. const ssr = options?.ssr;
  46873. const ctx = new Context();
  46874. ctx.ssr = !!ssr;
  46875. for (const plugin of getSortedPlugins('load')) {
  46876. if (closed && !ssr)
  46877. throwClosedServerError();
  46878. if (!plugin.load)
  46879. continue;
  46880. ctx._activePlugin = plugin;
  46881. const handler = getHookHandler(plugin.load);
  46882. const result = await handleHookPromise(handler.call(ctx, id, { ssr }));
  46883. if (result != null) {
  46884. if (isObject$1(result)) {
  46885. updateModuleInfo(id, result);
  46886. }
  46887. updateModuleLoadAddedImports(id, ctx);
  46888. return result;
  46889. }
  46890. }
  46891. updateModuleLoadAddedImports(id, ctx);
  46892. return null;
  46893. },
  46894. async transform(code, id, options) {
  46895. const inMap = options?.inMap;
  46896. const ssr = options?.ssr;
  46897. const ctx = new TransformContext(id, code, inMap);
  46898. ctx.ssr = !!ssr;
  46899. for (const plugin of getSortedPlugins('transform')) {
  46900. if (closed && !ssr)
  46901. throwClosedServerError();
  46902. if (!plugin.transform)
  46903. continue;
  46904. ctx._activePlugin = plugin;
  46905. ctx._activeId = id;
  46906. ctx._activeCode = code;
  46907. const start = debugPluginTransform ? performance.now() : 0;
  46908. let result;
  46909. const handler = getHookHandler(plugin.transform);
  46910. try {
  46911. result = await handleHookPromise(handler.call(ctx, code, id, { ssr }));
  46912. }
  46913. catch (e) {
  46914. ctx.error(e);
  46915. }
  46916. if (!result)
  46917. continue;
  46918. debugPluginTransform?.(timeFrom(start), plugin.name, prettifyUrl(id, root));
  46919. if (isObject$1(result)) {
  46920. if (result.code !== undefined) {
  46921. code = result.code;
  46922. if (result.map) {
  46923. if (debugSourcemapCombine) {
  46924. // @ts-expect-error inject plugin name for debug purpose
  46925. result.map.name = plugin.name;
  46926. }
  46927. ctx.sourcemapChain.push(result.map);
  46928. }
  46929. }
  46930. updateModuleInfo(id, result);
  46931. }
  46932. else {
  46933. code = result;
  46934. }
  46935. }
  46936. return {
  46937. code,
  46938. map: ctx._getCombinedSourcemap(),
  46939. };
  46940. },
  46941. async watchChange(id, change) {
  46942. const ctx = new Context();
  46943. await hookParallel('watchChange', () => ctx, () => [id, change]);
  46944. },
  46945. async close() {
  46946. if (closed)
  46947. return;
  46948. closed = true;
  46949. await Promise.allSettled(Array.from(processesing));
  46950. const ctx = new Context();
  46951. await hookParallel('buildEnd', () => ctx, () => []);
  46952. await hookParallel('closeBundle', () => ctx, () => []);
  46953. },
  46954. };
  46955. return container;
  46956. }
  46957. const debug$9 = createDebugger('vite:deps');
  46958. const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/;
  46959. // A simple regex to detect import sources. This is only used on
  46960. // <script lang="ts"> blocks in vue (setup only) or svelte files, since
  46961. // seemingly unused imports are dropped by esbuild when transpiling TS which
  46962. // prevents it from crawling further.
  46963. // We can't use es-module-lexer because it can't handle TS, and don't want to
  46964. // use Acorn because it's slow. Luckily this doesn't have to be bullet proof
  46965. // since even missed imports can be caught at runtime, and false positives will
  46966. // simply be ignored.
  46967. const importsRE = /(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm;
  46968. function scanImports(config) {
  46969. // Only used to scan non-ssr code
  46970. const start = performance.now();
  46971. const deps = {};
  46972. const missing = {};
  46973. let entries;
  46974. const scanContext = { cancelled: false };
  46975. const esbuildContext = computeEntries(config).then((computedEntries) => {
  46976. entries = computedEntries;
  46977. if (!entries.length) {
  46978. if (!config.optimizeDeps.entries && !config.optimizeDeps.include) {
  46979. config.logger.warn(colors$1.yellow('(!) Could not auto-determine entry point from rollupOptions or html files ' +
  46980. 'and there are no explicit optimizeDeps.include patterns. ' +
  46981. 'Skipping dependency pre-bundling.'));
  46982. }
  46983. return;
  46984. }
  46985. if (scanContext.cancelled)
  46986. return;
  46987. debug$9?.(`Crawling dependencies using entries: ${entries
  46988. .map((entry) => `\n ${colors$1.dim(entry)}`)
  46989. .join('')}`);
  46990. return prepareEsbuildScanner(config, entries, deps, missing, scanContext);
  46991. });
  46992. const result = esbuildContext
  46993. .then((context) => {
  46994. function disposeContext() {
  46995. return context?.dispose().catch((e) => {
  46996. config.logger.error('Failed to dispose esbuild context', { error: e });
  46997. });
  46998. }
  46999. if (!context || scanContext?.cancelled) {
  47000. disposeContext();
  47001. return { deps: {}, missing: {} };
  47002. }
  47003. return context
  47004. .rebuild()
  47005. .then(() => {
  47006. return {
  47007. // Ensure a fixed order so hashes are stable and improve logs
  47008. deps: orderedDependencies(deps),
  47009. missing,
  47010. };
  47011. })
  47012. .finally(() => {
  47013. return disposeContext();
  47014. });
  47015. })
  47016. .catch(async (e) => {
  47017. if (e.errors && e.message.includes('The build was canceled')) {
  47018. // esbuild logs an error when cancelling, but this is expected so
  47019. // return an empty result instead
  47020. return { deps: {}, missing: {} };
  47021. }
  47022. const prependMessage = colors$1.red(`\
  47023. Failed to scan for dependencies from entries:
  47024. ${entries.join('\n')}
  47025. `);
  47026. if (e.errors) {
  47027. const msgs = await formatMessages(e.errors, {
  47028. kind: 'error',
  47029. color: true,
  47030. });
  47031. e.message = prependMessage + msgs.join('\n');
  47032. }
  47033. else {
  47034. e.message = prependMessage + e.message;
  47035. }
  47036. throw e;
  47037. })
  47038. .finally(() => {
  47039. if (debug$9) {
  47040. const duration = (performance.now() - start).toFixed(2);
  47041. const depsStr = Object.keys(orderedDependencies(deps))
  47042. .sort()
  47043. .map((id) => `\n ${colors$1.cyan(id)} -> ${colors$1.dim(deps[id])}`)
  47044. .join('') || colors$1.dim('no dependencies found');
  47045. debug$9(`Scan completed in ${duration}ms: ${depsStr}`);
  47046. }
  47047. });
  47048. return {
  47049. cancel: async () => {
  47050. scanContext.cancelled = true;
  47051. return esbuildContext.then((context) => context?.cancel());
  47052. },
  47053. result,
  47054. };
  47055. }
  47056. async function computeEntries(config) {
  47057. let entries = [];
  47058. const explicitEntryPatterns = config.optimizeDeps.entries;
  47059. const buildInput = config.build.rollupOptions?.input;
  47060. if (explicitEntryPatterns) {
  47061. entries = await globEntries(explicitEntryPatterns, config);
  47062. }
  47063. else if (buildInput) {
  47064. const resolvePath = (p) => path$o.resolve(config.root, p);
  47065. if (typeof buildInput === 'string') {
  47066. entries = [resolvePath(buildInput)];
  47067. }
  47068. else if (Array.isArray(buildInput)) {
  47069. entries = buildInput.map(resolvePath);
  47070. }
  47071. else if (isObject$1(buildInput)) {
  47072. entries = Object.values(buildInput).map(resolvePath);
  47073. }
  47074. else {
  47075. throw new Error('invalid rollupOptions.input value.');
  47076. }
  47077. }
  47078. else {
  47079. entries = await globEntries('**/*.html', config);
  47080. }
  47081. // Non-supported entry file types and virtual files should not be scanned for
  47082. // dependencies.
  47083. entries = entries.filter((entry) => isScannable(entry, config.optimizeDeps.extensions) &&
  47084. fs$l.existsSync(entry));
  47085. return entries;
  47086. }
  47087. async function prepareEsbuildScanner(config, entries, deps, missing, scanContext) {
  47088. const container = await createPluginContainer(config);
  47089. if (scanContext?.cancelled)
  47090. return;
  47091. const plugin = esbuildScanPlugin(config, container, deps, missing, entries);
  47092. const { plugins = [], ...esbuildOptions } = config.optimizeDeps?.esbuildOptions ?? {};
  47093. // The plugin pipeline automatically loads the closest tsconfig.json.
  47094. // But esbuild doesn't support reading tsconfig.json if the plugin has resolved the path (https://github.com/evanw/esbuild/issues/2265).
  47095. // Due to syntax incompatibilities between the experimental decorators in TypeScript and TC39 decorators,
  47096. // we cannot simply set `"experimentalDecorators": true` or `false`. (https://github.com/vitejs/vite/pull/15206#discussion_r1417414715)
  47097. // Therefore, we use the closest tsconfig.json from the root to make it work in most cases.
  47098. let tsconfigRaw = esbuildOptions.tsconfigRaw;
  47099. if (!tsconfigRaw && !esbuildOptions.tsconfig) {
  47100. const tsconfigResult = await loadTsconfigJsonForFile(path$o.join(config.root, '_dummy.js'));
  47101. if (tsconfigResult.compilerOptions?.experimentalDecorators) {
  47102. tsconfigRaw = { compilerOptions: { experimentalDecorators: true } };
  47103. }
  47104. }
  47105. return await esbuild.context({
  47106. absWorkingDir: process.cwd(),
  47107. write: false,
  47108. stdin: {
  47109. contents: entries.map((e) => `import ${JSON.stringify(e)}`).join('\n'),
  47110. loader: 'js',
  47111. },
  47112. bundle: true,
  47113. format: 'esm',
  47114. logLevel: 'silent',
  47115. plugins: [...plugins, plugin],
  47116. ...esbuildOptions,
  47117. tsconfigRaw,
  47118. });
  47119. }
  47120. function orderedDependencies(deps) {
  47121. const depsList = Object.entries(deps);
  47122. // Ensure the same browserHash for the same set of dependencies
  47123. depsList.sort((a, b) => a[0].localeCompare(b[0]));
  47124. return Object.fromEntries(depsList);
  47125. }
  47126. function globEntries(pattern, config) {
  47127. const resolvedPatterns = arraify(pattern);
  47128. if (resolvedPatterns.every((str) => !glob.isDynamicPattern(str))) {
  47129. return resolvedPatterns.map((p) => normalizePath$3(path$o.resolve(config.root, p)));
  47130. }
  47131. return glob(pattern, {
  47132. cwd: config.root,
  47133. ignore: [
  47134. '**/node_modules/**',
  47135. `**/${config.build.outDir}/**`,
  47136. // if there aren't explicit entries, also ignore other common folders
  47137. ...(config.optimizeDeps.entries
  47138. ? []
  47139. : [`**/__tests__/**`, `**/coverage/**`]),
  47140. ],
  47141. absolute: true,
  47142. suppressErrors: true, // suppress EACCES errors
  47143. });
  47144. }
  47145. const scriptRE = /(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis;
  47146. const commentRE = /<!--.*?-->/gs;
  47147. const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  47148. const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  47149. const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  47150. const contextRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  47151. function esbuildScanPlugin(config, container, depImports, missing, entries) {
  47152. const seen = new Map();
  47153. const resolve = async (id, importer, options) => {
  47154. const key = id + (importer && path$o.dirname(importer));
  47155. if (seen.has(key)) {
  47156. return seen.get(key);
  47157. }
  47158. const resolved = await container.resolveId(id, importer && normalizePath$3(importer), {
  47159. ...options,
  47160. scan: true,
  47161. });
  47162. const res = resolved?.id;
  47163. seen.set(key, res);
  47164. return res;
  47165. };
  47166. const include = config.optimizeDeps?.include;
  47167. const exclude = [
  47168. ...(config.optimizeDeps?.exclude || []),
  47169. '@vite/client',
  47170. '@vite/env',
  47171. ];
  47172. const isUnlessEntry = (path) => !entries.includes(path);
  47173. const externalUnlessEntry = ({ path }) => ({
  47174. path,
  47175. external: isUnlessEntry(path),
  47176. });
  47177. const doTransformGlobImport = async (contents, id, loader) => {
  47178. let transpiledContents;
  47179. // transpile because `transformGlobImport` only expects js
  47180. if (loader !== 'js') {
  47181. transpiledContents = (await transform$1(contents, { loader })).code;
  47182. }
  47183. else {
  47184. transpiledContents = contents;
  47185. }
  47186. const result = await transformGlobImport(transpiledContents, id, config.root, resolve);
  47187. return result?.s.toString() || transpiledContents;
  47188. };
  47189. return {
  47190. name: 'vite:dep-scan',
  47191. setup(build) {
  47192. const scripts = {};
  47193. // external urls
  47194. build.onResolve({ filter: externalRE }, ({ path }) => ({
  47195. path,
  47196. external: true,
  47197. }));
  47198. // data urls
  47199. build.onResolve({ filter: dataUrlRE }, ({ path }) => ({
  47200. path,
  47201. external: true,
  47202. }));
  47203. // local scripts (`<script>` in Svelte and `<script setup>` in Vue)
  47204. build.onResolve({ filter: virtualModuleRE }, ({ path }) => {
  47205. return {
  47206. // strip prefix to get valid filesystem path so esbuild can resolve imports in the file
  47207. path: path.replace(virtualModulePrefix, ''),
  47208. namespace: 'script',
  47209. };
  47210. });
  47211. build.onLoad({ filter: /.*/, namespace: 'script' }, ({ path }) => {
  47212. return scripts[path];
  47213. });
  47214. // html types: extract script contents -----------------------------------
  47215. build.onResolve({ filter: htmlTypesRE }, async ({ path, importer }) => {
  47216. const resolved = await resolve(path, importer);
  47217. if (!resolved)
  47218. return;
  47219. // It is possible for the scanner to scan html types in node_modules.
  47220. // If we can optimize this html type, skip it so it's handled by the
  47221. // bare import resolve, and recorded as optimization dep.
  47222. if (isInNodeModules$1(resolved) &&
  47223. isOptimizable(resolved, config.optimizeDeps))
  47224. return;
  47225. return {
  47226. path: resolved,
  47227. namespace: 'html',
  47228. };
  47229. });
  47230. const htmlTypeOnLoadCallback = async ({ path: p }) => {
  47231. let raw = await fsp.readFile(p, 'utf-8');
  47232. // Avoid matching the content of the comment
  47233. raw = raw.replace(commentRE, '<!---->');
  47234. const isHtml = p.endsWith('.html');
  47235. let js = '';
  47236. let scriptId = 0;
  47237. const matches = raw.matchAll(scriptRE);
  47238. for (const [, openTag, content] of matches) {
  47239. const typeMatch = openTag.match(typeRE);
  47240. const type = typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3]);
  47241. const langMatch = openTag.match(langRE);
  47242. const lang = langMatch && (langMatch[1] || langMatch[2] || langMatch[3]);
  47243. // skip non type module script
  47244. if (isHtml && type !== 'module') {
  47245. continue;
  47246. }
  47247. // skip type="application/ld+json" and other non-JS types
  47248. if (type &&
  47249. !(type.includes('javascript') ||
  47250. type.includes('ecmascript') ||
  47251. type === 'module')) {
  47252. continue;
  47253. }
  47254. let loader = 'js';
  47255. if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') {
  47256. loader = lang;
  47257. }
  47258. else if (p.endsWith('.astro')) {
  47259. loader = 'ts';
  47260. }
  47261. const srcMatch = openTag.match(srcRE);
  47262. if (srcMatch) {
  47263. const src = srcMatch[1] || srcMatch[2] || srcMatch[3];
  47264. js += `import ${JSON.stringify(src)}\n`;
  47265. }
  47266. else if (content.trim()) {
  47267. // The reason why virtual modules are needed:
  47268. // 1. There can be module scripts (`<script context="module">` in Svelte and `<script>` in Vue)
  47269. // or local scripts (`<script>` in Svelte and `<script setup>` in Vue)
  47270. // 2. There can be multiple module scripts in html
  47271. // We need to handle these separately in case variable names are reused between them
  47272. // append imports in TS to prevent esbuild from removing them
  47273. // since they may be used in the template
  47274. const contents = content +
  47275. (loader.startsWith('ts') ? extractImportPaths(content) : '');
  47276. const key = `${p}?id=${scriptId++}`;
  47277. if (contents.includes('import.meta.glob')) {
  47278. scripts[key] = {
  47279. loader: 'js',
  47280. contents: await doTransformGlobImport(contents, p, loader),
  47281. resolveDir: normalizePath$3(path$o.dirname(p)),
  47282. pluginData: {
  47283. htmlType: { loader },
  47284. },
  47285. };
  47286. }
  47287. else {
  47288. scripts[key] = {
  47289. loader,
  47290. contents,
  47291. resolveDir: normalizePath$3(path$o.dirname(p)),
  47292. pluginData: {
  47293. htmlType: { loader },
  47294. },
  47295. };
  47296. }
  47297. const virtualModulePath = JSON.stringify(virtualModulePrefix + key);
  47298. const contextMatch = openTag.match(contextRE);
  47299. const context = contextMatch &&
  47300. (contextMatch[1] || contextMatch[2] || contextMatch[3]);
  47301. // Especially for Svelte files, exports in <script context="module"> means module exports,
  47302. // exports in <script> means component props. To avoid having two same export name from the
  47303. // star exports, we need to ignore exports in <script>
  47304. if (p.endsWith('.svelte') && context !== 'module') {
  47305. js += `import ${virtualModulePath}\n`;
  47306. }
  47307. else {
  47308. js += `export * from ${virtualModulePath}\n`;
  47309. }
  47310. }
  47311. }
  47312. // This will trigger incorrectly if `export default` is contained
  47313. // anywhere in a string. Svelte and Astro files can't have
  47314. // `export default` as code so we know if it's encountered it's a
  47315. // false positive (e.g. contained in a string)
  47316. if (!p.endsWith('.vue') || !js.includes('export default')) {
  47317. js += '\nexport default {}';
  47318. }
  47319. return {
  47320. loader: 'js',
  47321. contents: js,
  47322. };
  47323. };
  47324. // extract scripts inside HTML-like files and treat it as a js module
  47325. build.onLoad({ filter: htmlTypesRE, namespace: 'html' }, htmlTypeOnLoadCallback);
  47326. // the onResolve above will use namespace=html but esbuild doesn't
  47327. // call onResolve for glob imports and those will use namespace=file
  47328. // https://github.com/evanw/esbuild/issues/3317
  47329. build.onLoad({ filter: htmlTypesRE, namespace: 'file' }, htmlTypeOnLoadCallback);
  47330. // bare imports: record and externalize ----------------------------------
  47331. build.onResolve({
  47332. // avoid matching windows volume
  47333. filter: /^[\w@][^:]/,
  47334. }, async ({ path: id, importer, pluginData }) => {
  47335. if (moduleListContains(exclude, id)) {
  47336. return externalUnlessEntry({ path: id });
  47337. }
  47338. if (depImports[id]) {
  47339. return externalUnlessEntry({ path: id });
  47340. }
  47341. const resolved = await resolve(id, importer, {
  47342. custom: {
  47343. depScan: { loader: pluginData?.htmlType?.loader },
  47344. },
  47345. });
  47346. if (resolved) {
  47347. if (shouldExternalizeDep(resolved, id)) {
  47348. return externalUnlessEntry({ path: id });
  47349. }
  47350. if (isInNodeModules$1(resolved) || include?.includes(id)) {
  47351. // dependency or forced included, externalize and stop crawling
  47352. if (isOptimizable(resolved, config.optimizeDeps)) {
  47353. depImports[id] = resolved;
  47354. }
  47355. return externalUnlessEntry({ path: id });
  47356. }
  47357. else if (isScannable(resolved, config.optimizeDeps.extensions)) {
  47358. const namespace = htmlTypesRE.test(resolved) ? 'html' : undefined;
  47359. // linked package, keep crawling
  47360. return {
  47361. path: path$o.resolve(resolved),
  47362. namespace,
  47363. };
  47364. }
  47365. else {
  47366. return externalUnlessEntry({ path: id });
  47367. }
  47368. }
  47369. else {
  47370. missing[id] = normalizePath$3(importer);
  47371. }
  47372. });
  47373. // Externalized file types -----------------------------------------------
  47374. // these are done on raw ids using esbuild's native regex filter so it
  47375. // should be faster than doing it in the catch-all via js
  47376. // they are done after the bare import resolve because a package name
  47377. // may end with these extensions
  47378. const setupExternalize = (filter, doExternalize) => {
  47379. build.onResolve({ filter }, ({ path }) => {
  47380. return {
  47381. path,
  47382. external: doExternalize(path),
  47383. };
  47384. });
  47385. };
  47386. // css
  47387. setupExternalize(CSS_LANGS_RE, isUnlessEntry);
  47388. // json & wasm
  47389. setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry);
  47390. // known asset types
  47391. setupExternalize(new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`), isUnlessEntry);
  47392. // known vite query types: ?worker, ?raw
  47393. setupExternalize(SPECIAL_QUERY_RE, () => true);
  47394. // catch all -------------------------------------------------------------
  47395. build.onResolve({
  47396. filter: /.*/,
  47397. }, async ({ path: id, importer, pluginData }) => {
  47398. // use vite resolver to support urls and omitted extensions
  47399. const resolved = await resolve(id, importer, {
  47400. custom: {
  47401. depScan: { loader: pluginData?.htmlType?.loader },
  47402. },
  47403. });
  47404. if (resolved) {
  47405. if (shouldExternalizeDep(resolved, id) ||
  47406. !isScannable(resolved, config.optimizeDeps.extensions)) {
  47407. return externalUnlessEntry({ path: id });
  47408. }
  47409. const namespace = htmlTypesRE.test(resolved) ? 'html' : undefined;
  47410. return {
  47411. path: path$o.resolve(cleanUrl(resolved)),
  47412. namespace,
  47413. };
  47414. }
  47415. else {
  47416. // resolve failed... probably unsupported type
  47417. return externalUnlessEntry({ path: id });
  47418. }
  47419. });
  47420. // for jsx/tsx, we need to access the content and check for
  47421. // presence of import.meta.glob, since it results in import relationships
  47422. // but isn't crawled by esbuild.
  47423. build.onLoad({ filter: JS_TYPES_RE }, async ({ path: id }) => {
  47424. let ext = path$o.extname(id).slice(1);
  47425. if (ext === 'mjs')
  47426. ext = 'js';
  47427. let contents = await fsp.readFile(id, 'utf-8');
  47428. if (ext.endsWith('x') && config.esbuild && config.esbuild.jsxInject) {
  47429. contents = config.esbuild.jsxInject + `\n` + contents;
  47430. }
  47431. const loader = config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] ||
  47432. ext;
  47433. if (contents.includes('import.meta.glob')) {
  47434. return {
  47435. loader: 'js',
  47436. contents: await doTransformGlobImport(contents, id, loader),
  47437. };
  47438. }
  47439. return {
  47440. loader,
  47441. contents,
  47442. };
  47443. });
  47444. // onResolve is not called for glob imports.
  47445. // we need to add that here as well until esbuild calls onResolve for glob imports.
  47446. // https://github.com/evanw/esbuild/issues/3317
  47447. build.onLoad({ filter: /.*/, namespace: 'file' }, () => {
  47448. return {
  47449. loader: 'js',
  47450. contents: 'export default {}',
  47451. };
  47452. });
  47453. },
  47454. };
  47455. }
  47456. /**
  47457. * when using TS + (Vue + `<script setup>`) or Svelte, imports may seem
  47458. * unused to esbuild and dropped in the build output, which prevents
  47459. * esbuild from crawling further.
  47460. * the solution is to add `import 'x'` for every source to force
  47461. * esbuild to keep crawling due to potential side effects.
  47462. */
  47463. function extractImportPaths(code) {
  47464. // empty singleline & multiline comments to avoid matching comments
  47465. code = code
  47466. .replace(multilineCommentsRE, '/* */')
  47467. .replace(singlelineCommentsRE, '');
  47468. let js = '';
  47469. let m;
  47470. importsRE.lastIndex = 0;
  47471. while ((m = importsRE.exec(code)) != null) {
  47472. js += `\nimport ${m[1]}`;
  47473. }
  47474. return js;
  47475. }
  47476. function shouldExternalizeDep(resolvedId, rawId) {
  47477. // not a valid file path
  47478. if (!path$o.isAbsolute(resolvedId)) {
  47479. return true;
  47480. }
  47481. // virtual id
  47482. if (resolvedId === rawId || resolvedId.includes('\0')) {
  47483. return true;
  47484. }
  47485. return false;
  47486. }
  47487. function isScannable(id, extensions) {
  47488. return (JS_TYPES_RE.test(id) ||
  47489. htmlTypesRE.test(id) ||
  47490. extensions?.includes(path$o.extname(id)) ||
  47491. false);
  47492. }
  47493. function createOptimizeDepsIncludeResolver(config, ssr) {
  47494. const resolve = config.createResolver({
  47495. asSrc: false,
  47496. scan: true,
  47497. ssrOptimizeCheck: ssr,
  47498. ssrConfig: config.ssr,
  47499. packageCache: new Map(),
  47500. });
  47501. return async (id) => {
  47502. const lastArrowIndex = id.lastIndexOf('>');
  47503. if (lastArrowIndex === -1) {
  47504. return await resolve(id, undefined, undefined, ssr);
  47505. }
  47506. // split nested selected id by last '>', for example:
  47507. // 'foo > bar > baz' => 'foo > bar' & 'baz'
  47508. const nestedRoot = id.substring(0, lastArrowIndex).trim();
  47509. const nestedPath = id.substring(lastArrowIndex + 1).trim();
  47510. const basedir = nestedResolveBasedir(nestedRoot, config.root, config.resolve.preserveSymlinks);
  47511. return await resolve(nestedPath, path$o.resolve(basedir, 'package.json'), undefined, ssr);
  47512. };
  47513. }
  47514. /**
  47515. * Expand the glob syntax in `optimizeDeps.include` to proper import paths
  47516. */
  47517. function expandGlobIds(id, config) {
  47518. const pkgName = getNpmPackageName(id);
  47519. if (!pkgName)
  47520. return [];
  47521. const pkgData = resolvePackageData(pkgName, config.root, config.resolve.preserveSymlinks, config.packageCache);
  47522. if (!pkgData)
  47523. return [];
  47524. const pattern = '.' + id.slice(pkgName.length);
  47525. const exports = pkgData.data.exports;
  47526. // if package has exports field, get all possible export paths and apply
  47527. // glob on them with micromatch
  47528. if (exports) {
  47529. if (typeof exports === 'string' || Array.isArray(exports)) {
  47530. return [pkgName];
  47531. }
  47532. const possibleExportPaths = [];
  47533. for (const key in exports) {
  47534. if (key[0] === '.') {
  47535. if (key.includes('*')) {
  47536. // "./glob/*": {
  47537. // "browser": "./dist/glob/*-browser/*.js", <-- get this one
  47538. // "default": "./dist/glob/*/*.js"
  47539. // }
  47540. // NOTE: theoretically the "default" condition could map to a different
  47541. // set of files, but that complicates the resolve logic, so we assume
  47542. // all conditions map to the same set of files, and get the first one.
  47543. const exportsValue = getFirstExportStringValue(exports[key]);
  47544. if (!exportsValue)
  47545. continue;
  47546. // "./dist/glob/*-browser/*.js" => "./dist/glob/**/*-browser/**/*.js"
  47547. // NOTE: in some cases, this could expand to consecutive /**/*/**/* etc
  47548. // but it's fine since fast-glob handles it the same.
  47549. const exportValuePattern = exportsValue.replace(/\*/g, '**/*');
  47550. // "./dist/glob/*-browser/*.js" => /dist\/glob\/(.*)-browser\/(.*)\.js/
  47551. const exportsValueGlobRe = new RegExp(exportsValue.split('*').map(escapeRegex).join('(.*)'));
  47552. possibleExportPaths.push(...glob
  47553. .sync(exportValuePattern, {
  47554. cwd: pkgData.dir,
  47555. ignore: ['node_modules'],
  47556. })
  47557. .map((filePath) => {
  47558. // ensure "./" prefix for inconsistent fast-glob result
  47559. // glob.sync("./some-dir/**/*") -> "./some-dir/some-file"
  47560. // glob.sync("./**/*") -> "some-dir/some-file"
  47561. if (exportsValue.startsWith('./') &&
  47562. !filePath.startsWith('./')) {
  47563. filePath = './' + filePath;
  47564. }
  47565. // "./glob/*": "./dist/glob/*-browser/*.js"
  47566. // `filePath`: "./dist/glob/foo-browser/foo.js"
  47567. // we need to revert the file path back to the export key by
  47568. // matching value regex and replacing the capture groups to the key
  47569. const matched = slash$1(filePath).match(exportsValueGlobRe);
  47570. // `matched`: [..., 'foo', 'foo']
  47571. if (matched) {
  47572. let allGlobSame = matched.length === 2;
  47573. // exports key can only have one *, so for >=2 matched groups,
  47574. // make sure they have the same value
  47575. if (!allGlobSame) {
  47576. // assume true, if one group is different, set false and break
  47577. allGlobSame = true;
  47578. for (let i = 2; i < matched.length; i++) {
  47579. if (matched[i] !== matched[i - 1]) {
  47580. allGlobSame = false;
  47581. break;
  47582. }
  47583. }
  47584. }
  47585. if (allGlobSame) {
  47586. return key.replace('*', matched[1]).slice(2);
  47587. }
  47588. }
  47589. return '';
  47590. })
  47591. .filter(Boolean));
  47592. }
  47593. else {
  47594. possibleExportPaths.push(key.slice(2));
  47595. }
  47596. }
  47597. }
  47598. const matched = micromatch$2(possibleExportPaths, pattern).map((match) => path$o.posix.join(pkgName, match));
  47599. matched.unshift(pkgName);
  47600. return matched;
  47601. }
  47602. else {
  47603. // for packages without exports, we can do a simple glob
  47604. const matched = glob
  47605. .sync(pattern, { cwd: pkgData.dir, ignore: ['node_modules'] })
  47606. .map((match) => path$o.posix.join(pkgName, slash$1(match)));
  47607. matched.unshift(pkgName);
  47608. return matched;
  47609. }
  47610. }
  47611. function getFirstExportStringValue(obj) {
  47612. if (typeof obj === 'string') {
  47613. return obj;
  47614. }
  47615. else if (Array.isArray(obj)) {
  47616. return obj[0];
  47617. }
  47618. else {
  47619. for (const key in obj) {
  47620. return getFirstExportStringValue(obj[key]);
  47621. }
  47622. }
  47623. }
  47624. /**
  47625. * Continuously resolve the basedir of packages separated by '>'
  47626. */
  47627. function nestedResolveBasedir(id, basedir, preserveSymlinks = false) {
  47628. const pkgs = id.split('>').map((pkg) => pkg.trim());
  47629. for (const pkg of pkgs) {
  47630. basedir = resolvePackageData(pkg, basedir, preserveSymlinks)?.dir || basedir;
  47631. }
  47632. return basedir;
  47633. }
  47634. const debug$8 = createDebugger('vite:deps');
  47635. /**
  47636. * The amount to wait for requests to register newly found dependencies before triggering
  47637. * a re-bundle + page reload
  47638. */
  47639. const debounceMs = 100;
  47640. const depsOptimizerMap = new WeakMap();
  47641. const devSsrDepsOptimizerMap = new WeakMap();
  47642. function getDepsOptimizer(config, ssr) {
  47643. return (ssr ? devSsrDepsOptimizerMap : depsOptimizerMap).get(config);
  47644. }
  47645. async function initDepsOptimizer(config, server) {
  47646. if (!getDepsOptimizer(config, false)) {
  47647. await createDepsOptimizer(config, server);
  47648. }
  47649. }
  47650. let creatingDevSsrOptimizer;
  47651. async function initDevSsrDepsOptimizer(config, server) {
  47652. if (getDepsOptimizer(config, true)) {
  47653. // ssr
  47654. return;
  47655. }
  47656. if (creatingDevSsrOptimizer) {
  47657. return creatingDevSsrOptimizer;
  47658. }
  47659. creatingDevSsrOptimizer = (async function () {
  47660. // Important: scanning needs to be done before starting the SSR dev optimizer
  47661. // If ssrLoadModule is called before server.listen(), the main deps optimizer
  47662. // will not be yet created
  47663. const ssr = false;
  47664. if (!getDepsOptimizer(config, ssr)) {
  47665. await initDepsOptimizer(config, server);
  47666. }
  47667. await getDepsOptimizer(config, ssr).scanProcessing;
  47668. await createDevSsrDepsOptimizer(config);
  47669. creatingDevSsrOptimizer = undefined;
  47670. })();
  47671. return await creatingDevSsrOptimizer;
  47672. }
  47673. async function createDepsOptimizer(config, server) {
  47674. const { logger } = config;
  47675. const ssr = false;
  47676. const sessionTimestamp = Date.now().toString();
  47677. const cachedMetadata = await loadCachedDepOptimizationMetadata(config, ssr);
  47678. let debounceProcessingHandle;
  47679. let closed = false;
  47680. let metadata = cachedMetadata || initDepsOptimizerMetadata(config, ssr, sessionTimestamp);
  47681. const options = getDepOptimizationConfig(config, ssr);
  47682. const { noDiscovery, holdUntilCrawlEnd } = options;
  47683. const depsOptimizer = {
  47684. metadata,
  47685. registerMissingImport,
  47686. run: () => debouncedProcessing(0),
  47687. isOptimizedDepFile: createIsOptimizedDepFile(config),
  47688. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  47689. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  47690. close,
  47691. options,
  47692. };
  47693. depsOptimizerMap.set(config, depsOptimizer);
  47694. let newDepsDiscovered = false;
  47695. let newDepsToLog = [];
  47696. let newDepsToLogHandle;
  47697. const logNewlyDiscoveredDeps = () => {
  47698. if (newDepsToLog.length) {
  47699. config.logger.info(colors$1.green(`✨ new dependencies optimized: ${depsLogString(newDepsToLog)}`), {
  47700. timestamp: true,
  47701. });
  47702. newDepsToLog = [];
  47703. }
  47704. };
  47705. let discoveredDepsWhileScanning = [];
  47706. const logDiscoveredDepsWhileScanning = () => {
  47707. if (discoveredDepsWhileScanning.length) {
  47708. config.logger.info(colors$1.green(`✨ discovered while scanning: ${depsLogString(discoveredDepsWhileScanning)}`), {
  47709. timestamp: true,
  47710. });
  47711. discoveredDepsWhileScanning = [];
  47712. }
  47713. };
  47714. let depOptimizationProcessing = promiseWithResolvers();
  47715. let depOptimizationProcessingQueue = [];
  47716. const resolveEnqueuedProcessingPromises = () => {
  47717. // Resolve all the processings (including the ones which were delayed)
  47718. for (const processing of depOptimizationProcessingQueue) {
  47719. processing.resolve();
  47720. }
  47721. depOptimizationProcessingQueue = [];
  47722. };
  47723. let enqueuedRerun;
  47724. let currentlyProcessing = false;
  47725. let firstRunCalled = !!cachedMetadata;
  47726. let warnAboutMissedDependencies = false;
  47727. // If this is a cold run, we wait for static imports discovered
  47728. // from the first request before resolving to minimize full page reloads.
  47729. // On warm start or after the first optimization is run, we use a simpler
  47730. // debounce strategy each time a new dep is discovered.
  47731. let waitingForCrawlEnd = false;
  47732. if (!cachedMetadata) {
  47733. server._onCrawlEnd(onCrawlEnd);
  47734. waitingForCrawlEnd = true;
  47735. }
  47736. let optimizationResult;
  47737. let discover;
  47738. async function close() {
  47739. closed = true;
  47740. await Promise.allSettled([
  47741. discover?.cancel(),
  47742. depsOptimizer.scanProcessing,
  47743. optimizationResult?.cancel(),
  47744. ]);
  47745. }
  47746. if (!cachedMetadata) {
  47747. // Enter processing state until crawl of static imports ends
  47748. currentlyProcessing = true;
  47749. // Initialize discovered deps with manually added optimizeDeps.include info
  47750. const manuallyIncludedDeps = {};
  47751. await addManuallyIncludedOptimizeDeps(manuallyIncludedDeps, config, ssr);
  47752. const manuallyIncludedDepsInfo = toDiscoveredDependencies(config, manuallyIncludedDeps, ssr, sessionTimestamp);
  47753. for (const depInfo of Object.values(manuallyIncludedDepsInfo)) {
  47754. addOptimizedDepInfo(metadata, 'discovered', {
  47755. ...depInfo,
  47756. processing: depOptimizationProcessing.promise,
  47757. });
  47758. newDepsDiscovered = true;
  47759. }
  47760. if (noDiscovery) {
  47761. // We don't need to scan for dependencies or wait for the static crawl to end
  47762. // Run the first optimization run immediately
  47763. runOptimizer();
  47764. }
  47765. else {
  47766. // Important, the scanner is dev only
  47767. depsOptimizer.scanProcessing = new Promise((resolve) => {
  47768. (async () => {
  47769. try {
  47770. debug$8?.(colors$1.green(`scanning for dependencies...`));
  47771. discover = discoverProjectDependencies(config);
  47772. const deps = await discover.result;
  47773. discover = undefined;
  47774. const manuallyIncluded = Object.keys(manuallyIncludedDepsInfo);
  47775. discoveredDepsWhileScanning.push(...Object.keys(metadata.discovered).filter((dep) => !deps[dep] && !manuallyIncluded.includes(dep)));
  47776. // Add these dependencies to the discovered list, as these are currently
  47777. // used by the preAliasPlugin to support aliased and optimized deps.
  47778. // This is also used by the CJS externalization heuristics in legacy mode
  47779. for (const id of Object.keys(deps)) {
  47780. if (!metadata.discovered[id]) {
  47781. addMissingDep(id, deps[id]);
  47782. }
  47783. }
  47784. const knownDeps = prepareKnownDeps();
  47785. startNextDiscoveredBatch();
  47786. // For dev, we run the scanner and the first optimization
  47787. // run on the background
  47788. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47789. // If the holdUntilCrawlEnd stratey is used, we wait until crawling has
  47790. // ended to decide if we send this result to the browser or we need to
  47791. // do another optimize step
  47792. if (!holdUntilCrawlEnd) {
  47793. // If not, we release the result to the browser as soon as the scanner
  47794. // is done. If the scanner missed any dependency, and a new dependency
  47795. // is discovered while crawling static imports, then there will be a
  47796. // full-page reload if new common chunks are generated between the old
  47797. // and new optimized deps.
  47798. optimizationResult.result.then((result) => {
  47799. // Check if the crawling of static imports has already finished. In that
  47800. // case, the result is handled by the onCrawlEnd callback
  47801. if (!waitingForCrawlEnd)
  47802. return;
  47803. optimizationResult = undefined; // signal that we'll be using the result
  47804. runOptimizer(result);
  47805. });
  47806. }
  47807. }
  47808. catch (e) {
  47809. logger.error(e.stack || e.message);
  47810. }
  47811. finally {
  47812. resolve();
  47813. depsOptimizer.scanProcessing = undefined;
  47814. }
  47815. })();
  47816. });
  47817. }
  47818. }
  47819. function startNextDiscoveredBatch() {
  47820. newDepsDiscovered = false;
  47821. // Add the current depOptimizationProcessing to the queue, these
  47822. // promises are going to be resolved once a rerun is committed
  47823. depOptimizationProcessingQueue.push(depOptimizationProcessing);
  47824. // Create a new promise for the next rerun, discovered missing
  47825. // dependencies will be assigned this promise from this point
  47826. depOptimizationProcessing = promiseWithResolvers();
  47827. }
  47828. function prepareKnownDeps() {
  47829. const knownDeps = {};
  47830. // Clone optimized info objects, fileHash, browserHash may be changed for them
  47831. for (const dep of Object.keys(metadata.optimized)) {
  47832. knownDeps[dep] = { ...metadata.optimized[dep] };
  47833. }
  47834. for (const dep of Object.keys(metadata.discovered)) {
  47835. // Clone the discovered info discarding its processing promise
  47836. const { processing, ...info } = metadata.discovered[dep];
  47837. knownDeps[dep] = info;
  47838. }
  47839. return knownDeps;
  47840. }
  47841. async function runOptimizer(preRunResult) {
  47842. // a successful completion of the optimizeDeps rerun will end up
  47843. // creating new bundled version of all current and discovered deps
  47844. // in the cache dir and a new metadata info object assigned
  47845. // to _metadata. A fullReload is only issued if the previous bundled
  47846. // dependencies have changed.
  47847. // if the rerun fails, _metadata remains untouched, current discovered
  47848. // deps are cleaned, and a fullReload is issued
  47849. // All deps, previous known and newly discovered are rebundled,
  47850. // respect insertion order to keep the metadata file stable
  47851. const isRerun = firstRunCalled;
  47852. firstRunCalled = true;
  47853. // Ensure that rerun is called sequentially
  47854. enqueuedRerun = undefined;
  47855. // Ensure that a rerun will not be issued for current discovered deps
  47856. if (debounceProcessingHandle)
  47857. clearTimeout(debounceProcessingHandle);
  47858. if (closed) {
  47859. currentlyProcessing = false;
  47860. return;
  47861. }
  47862. currentlyProcessing = true;
  47863. try {
  47864. let processingResult;
  47865. if (preRunResult) {
  47866. processingResult = preRunResult;
  47867. }
  47868. else {
  47869. const knownDeps = prepareKnownDeps();
  47870. startNextDiscoveredBatch();
  47871. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47872. processingResult = await optimizationResult.result;
  47873. optimizationResult = undefined;
  47874. }
  47875. if (closed) {
  47876. currentlyProcessing = false;
  47877. processingResult.cancel();
  47878. resolveEnqueuedProcessingPromises();
  47879. return;
  47880. }
  47881. const newData = processingResult.metadata;
  47882. const needsInteropMismatch = findInteropMismatches(metadata.discovered, newData.optimized);
  47883. // After a re-optimization, if the internal bundled chunks change a full page reload
  47884. // is required. If the files are stable, we can avoid the reload that is expensive
  47885. // for large applications. Comparing their fileHash we can find out if it is safe to
  47886. // keep the current browser state.
  47887. const needsReload = needsInteropMismatch.length > 0 ||
  47888. metadata.hash !== newData.hash ||
  47889. Object.keys(metadata.optimized).some((dep) => {
  47890. return (metadata.optimized[dep].fileHash !== newData.optimized[dep].fileHash);
  47891. });
  47892. const commitProcessing = async () => {
  47893. await processingResult.commit();
  47894. // While optimizeDeps is running, new missing deps may be discovered,
  47895. // in which case they will keep being added to metadata.discovered
  47896. for (const id in metadata.discovered) {
  47897. if (!newData.optimized[id]) {
  47898. addOptimizedDepInfo(newData, 'discovered', metadata.discovered[id]);
  47899. }
  47900. }
  47901. // If we don't reload the page, we need to keep browserHash stable
  47902. if (!needsReload) {
  47903. newData.browserHash = metadata.browserHash;
  47904. for (const dep in newData.chunks) {
  47905. newData.chunks[dep].browserHash = metadata.browserHash;
  47906. }
  47907. for (const dep in newData.optimized) {
  47908. newData.optimized[dep].browserHash = (metadata.optimized[dep] || metadata.discovered[dep]).browserHash;
  47909. }
  47910. }
  47911. // Commit hash and needsInterop changes to the discovered deps info
  47912. // object. Allow for code to await for the discovered processing promise
  47913. // and use the information in the same object
  47914. for (const o in newData.optimized) {
  47915. const discovered = metadata.discovered[o];
  47916. if (discovered) {
  47917. const optimized = newData.optimized[o];
  47918. discovered.browserHash = optimized.browserHash;
  47919. discovered.fileHash = optimized.fileHash;
  47920. discovered.needsInterop = optimized.needsInterop;
  47921. discovered.processing = undefined;
  47922. }
  47923. }
  47924. if (isRerun) {
  47925. newDepsToLog.push(...Object.keys(newData.optimized).filter((dep) => !metadata.optimized[dep]));
  47926. }
  47927. metadata = depsOptimizer.metadata = newData;
  47928. resolveEnqueuedProcessingPromises();
  47929. };
  47930. if (!needsReload) {
  47931. await commitProcessing();
  47932. if (!debug$8) {
  47933. if (newDepsToLogHandle)
  47934. clearTimeout(newDepsToLogHandle);
  47935. newDepsToLogHandle = setTimeout(() => {
  47936. newDepsToLogHandle = undefined;
  47937. logNewlyDiscoveredDeps();
  47938. if (warnAboutMissedDependencies) {
  47939. logDiscoveredDepsWhileScanning();
  47940. config.logger.info(colors$1.magenta(`❗ add these dependencies to optimizeDeps.include to speed up cold start`), { timestamp: true });
  47941. warnAboutMissedDependencies = false;
  47942. }
  47943. }, 2 * debounceMs);
  47944. }
  47945. else {
  47946. debug$8(colors$1.green(`✨ ${!isRerun
  47947. ? `dependencies optimized`
  47948. : `optimized dependencies unchanged`}`));
  47949. }
  47950. }
  47951. else {
  47952. if (newDepsDiscovered) {
  47953. // There are newly discovered deps, and another rerun is about to be
  47954. // executed. Avoid the current full reload discarding this rerun result
  47955. // We don't resolve the processing promise, as they will be resolved
  47956. // once a rerun is committed
  47957. processingResult.cancel();
  47958. debug$8?.(colors$1.green(`✨ delaying reload as new dependencies have been found...`));
  47959. }
  47960. else {
  47961. await commitProcessing();
  47962. if (!debug$8) {
  47963. if (newDepsToLogHandle)
  47964. clearTimeout(newDepsToLogHandle);
  47965. newDepsToLogHandle = undefined;
  47966. logNewlyDiscoveredDeps();
  47967. if (warnAboutMissedDependencies) {
  47968. logDiscoveredDepsWhileScanning();
  47969. config.logger.info(colors$1.magenta(`❗ add these dependencies to optimizeDeps.include to avoid a full page reload during cold start`), { timestamp: true });
  47970. warnAboutMissedDependencies = false;
  47971. }
  47972. }
  47973. logger.info(colors$1.green(`✨ optimized dependencies changed. reloading`), {
  47974. timestamp: true,
  47975. });
  47976. if (needsInteropMismatch.length > 0) {
  47977. config.logger.warn(`Mixed ESM and CJS detected in ${colors$1.yellow(needsInteropMismatch.join(', '))}, add ${needsInteropMismatch.length === 1 ? 'it' : 'them'} to optimizeDeps.needsInterop to speed up cold start`, {
  47978. timestamp: true,
  47979. });
  47980. }
  47981. fullReload();
  47982. }
  47983. }
  47984. }
  47985. catch (e) {
  47986. logger.error(colors$1.red(`error while updating dependencies:\n${e.stack}`), { timestamp: true, error: e });
  47987. resolveEnqueuedProcessingPromises();
  47988. // Reset missing deps, let the server rediscover the dependencies
  47989. metadata.discovered = {};
  47990. }
  47991. currentlyProcessing = false;
  47992. // @ts-expect-error `enqueuedRerun` could exist because `debouncedProcessing` may run while awaited
  47993. enqueuedRerun?.();
  47994. }
  47995. function fullReload() {
  47996. // Cached transform results have stale imports (resolved to
  47997. // old locations) so they need to be invalidated before the page is
  47998. // reloaded.
  47999. server.moduleGraph.invalidateAll();
  48000. server.hot.send({
  48001. type: 'full-reload',
  48002. path: '*',
  48003. });
  48004. }
  48005. async function rerun() {
  48006. // debounce time to wait for new missing deps finished, issue a new
  48007. // optimization of deps (both old and newly found) once the previous
  48008. // optimizeDeps processing is finished
  48009. const deps = Object.keys(metadata.discovered);
  48010. const depsString = depsLogString(deps);
  48011. debug$8?.(colors$1.green(`new dependencies found: ${depsString}`));
  48012. runOptimizer();
  48013. }
  48014. function getDiscoveredBrowserHash(hash, deps, missing) {
  48015. return getHash(hash + JSON.stringify(deps) + JSON.stringify(missing) + sessionTimestamp);
  48016. }
  48017. function registerMissingImport(id, resolved) {
  48018. const optimized = metadata.optimized[id];
  48019. if (optimized) {
  48020. return optimized;
  48021. }
  48022. const chunk = metadata.chunks[id];
  48023. if (chunk) {
  48024. return chunk;
  48025. }
  48026. let missing = metadata.discovered[id];
  48027. if (missing) {
  48028. // We are already discover this dependency
  48029. // It will be processed in the next rerun call
  48030. return missing;
  48031. }
  48032. missing = addMissingDep(id, resolved);
  48033. // Until the first optimize run is called, avoid triggering processing
  48034. // We'll wait until the user codebase is eagerly processed by Vite so
  48035. // we can get a list of every missing dependency before giving to the
  48036. // browser a dependency that may be outdated, thus avoiding full page reloads
  48037. if (!waitingForCrawlEnd) {
  48038. // Debounced rerun, let other missing dependencies be discovered before
  48039. // the running next optimizeDeps
  48040. debouncedProcessing();
  48041. }
  48042. // Return the path for the optimized bundle, this path is known before
  48043. // esbuild is run to generate the pre-bundle
  48044. return missing;
  48045. }
  48046. function addMissingDep(id, resolved) {
  48047. newDepsDiscovered = true;
  48048. return addOptimizedDepInfo(metadata, 'discovered', {
  48049. id,
  48050. file: getOptimizedDepPath(id, config, ssr),
  48051. src: resolved,
  48052. // Adding a browserHash to this missing dependency that is unique to
  48053. // the current state of known + missing deps. If its optimizeDeps run
  48054. // doesn't alter the bundled files of previous known dependencies,
  48055. // we don't need a full reload and this browserHash will be kept
  48056. browserHash: getDiscoveredBrowserHash(metadata.hash, depsFromOptimizedDepInfo(metadata.optimized), depsFromOptimizedDepInfo(metadata.discovered)),
  48057. // loading of this pre-bundled dep needs to await for its processing
  48058. // promise to be resolved
  48059. processing: depOptimizationProcessing.promise,
  48060. exportsData: extractExportsData(resolved, config, ssr),
  48061. });
  48062. }
  48063. function debouncedProcessing(timeout = debounceMs) {
  48064. // Debounced rerun, let other missing dependencies be discovered before
  48065. // the next optimizeDeps run
  48066. enqueuedRerun = undefined;
  48067. if (debounceProcessingHandle)
  48068. clearTimeout(debounceProcessingHandle);
  48069. if (newDepsToLogHandle)
  48070. clearTimeout(newDepsToLogHandle);
  48071. newDepsToLogHandle = undefined;
  48072. debounceProcessingHandle = setTimeout(() => {
  48073. debounceProcessingHandle = undefined;
  48074. enqueuedRerun = rerun;
  48075. if (!currentlyProcessing) {
  48076. enqueuedRerun();
  48077. }
  48078. }, timeout);
  48079. }
  48080. // onCrawlEnd is called once when the server starts and all static
  48081. // imports after the first request have been crawled (dynamic imports may also
  48082. // be crawled if the browser requests them right away).
  48083. async function onCrawlEnd() {
  48084. // switch after this point to a simple debounce strategy
  48085. waitingForCrawlEnd = false;
  48086. debug$8?.(colors$1.green(`✨ static imports crawl ended`));
  48087. if (closed) {
  48088. return;
  48089. }
  48090. // Await for the scan+optimize step running in the background
  48091. // It normally should be over by the time crawling of user code ended
  48092. await depsOptimizer.scanProcessing;
  48093. if (optimizationResult && !config.optimizeDeps.noDiscovery) {
  48094. // In the holdUntilCrawlEnd strategy, we don't release the result of the
  48095. // post-scanner optimize step to the browser until we reach this point
  48096. // If there are new dependencies, we do another optimize run, if not, we
  48097. // use the post-scanner optimize result
  48098. // If holdUntilCrawlEnd is false and we reach here, it means that the
  48099. // scan+optimize step finished after crawl end. We follow the same
  48100. // process as in the holdUntilCrawlEnd in this case.
  48101. const afterScanResult = optimizationResult.result;
  48102. optimizationResult = undefined; // signal that we'll be using the result
  48103. const result = await afterScanResult;
  48104. currentlyProcessing = false;
  48105. const crawlDeps = Object.keys(metadata.discovered);
  48106. const scanDeps = Object.keys(result.metadata.optimized);
  48107. if (scanDeps.length === 0 && crawlDeps.length === 0) {
  48108. debug$8?.(colors$1.green(`✨ no dependencies found by the scanner or crawling static imports`));
  48109. // We still commit the result so the scanner isn't run on the next cold start
  48110. // for projects without dependencies
  48111. startNextDiscoveredBatch();
  48112. runOptimizer(result);
  48113. return;
  48114. }
  48115. const needsInteropMismatch = findInteropMismatches(metadata.discovered, result.metadata.optimized);
  48116. const scannerMissedDeps = crawlDeps.some((dep) => !scanDeps.includes(dep));
  48117. const outdatedResult = needsInteropMismatch.length > 0 || scannerMissedDeps;
  48118. if (outdatedResult) {
  48119. // Drop this scan result, and perform a new optimization to avoid a full reload
  48120. result.cancel();
  48121. // Add deps found by the scanner to the discovered deps while crawling
  48122. for (const dep of scanDeps) {
  48123. if (!crawlDeps.includes(dep)) {
  48124. addMissingDep(dep, result.metadata.optimized[dep].src);
  48125. }
  48126. }
  48127. if (scannerMissedDeps) {
  48128. debug$8?.(colors$1.yellow(`✨ new dependencies were found while crawling that weren't detected by the scanner`));
  48129. }
  48130. debug$8?.(colors$1.green(`✨ re-running optimizer`));
  48131. debouncedProcessing(0);
  48132. }
  48133. else {
  48134. debug$8?.(colors$1.green(`✨ using post-scan optimizer result, the scanner found every used dependency`));
  48135. startNextDiscoveredBatch();
  48136. runOptimizer(result);
  48137. }
  48138. }
  48139. else if (!holdUntilCrawlEnd) {
  48140. // The post-scanner optimize result has been released to the browser
  48141. // If new deps have been discovered, issue a regular rerun of the
  48142. // optimizer. A full page reload may still be avoided if the new
  48143. // optimize result is compatible in this case
  48144. if (newDepsDiscovered) {
  48145. debug$8?.(colors$1.green(`✨ new dependencies were found while crawling static imports, re-running optimizer`));
  48146. warnAboutMissedDependencies = true;
  48147. debouncedProcessing(0);
  48148. }
  48149. }
  48150. else {
  48151. const crawlDeps = Object.keys(metadata.discovered);
  48152. currentlyProcessing = false;
  48153. if (crawlDeps.length === 0) {
  48154. debug$8?.(colors$1.green(`✨ no dependencies found while crawling the static imports`));
  48155. firstRunCalled = true;
  48156. }
  48157. // queue the first optimizer run, even without deps so the result is cached
  48158. debouncedProcessing(0);
  48159. }
  48160. }
  48161. }
  48162. async function createDevSsrDepsOptimizer(config) {
  48163. const metadata = await optimizeServerSsrDeps(config);
  48164. const depsOptimizer = {
  48165. metadata,
  48166. isOptimizedDepFile: createIsOptimizedDepFile(config),
  48167. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  48168. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  48169. registerMissingImport: () => {
  48170. throw new Error('Vite Internal Error: registerMissingImport is not supported in dev SSR');
  48171. },
  48172. // noop, there is no scanning during dev SSR
  48173. // the optimizer blocks the server start
  48174. run: () => { },
  48175. close: async () => { },
  48176. options: config.ssr.optimizeDeps,
  48177. };
  48178. devSsrDepsOptimizerMap.set(config, depsOptimizer);
  48179. }
  48180. function findInteropMismatches(discovered, optimized) {
  48181. const needsInteropMismatch = [];
  48182. for (const dep in discovered) {
  48183. const discoveredDepInfo = discovered[dep];
  48184. const depInfo = optimized[dep];
  48185. if (depInfo) {
  48186. if (discoveredDepInfo.needsInterop !== undefined &&
  48187. depInfo.needsInterop !== discoveredDepInfo.needsInterop) {
  48188. // This only happens when a discovered dependency has mixed ESM and CJS syntax
  48189. // and it hasn't been manually added to optimizeDeps.needsInterop
  48190. needsInteropMismatch.push(dep);
  48191. debug$8?.(colors$1.cyan(`✨ needsInterop mismatch detected for ${dep}`));
  48192. }
  48193. }
  48194. }
  48195. return needsInteropMismatch;
  48196. }
  48197. const debug$7 = createDebugger('vite:deps');
  48198. const jsExtensionRE = /\.js$/i;
  48199. const jsMapExtensionRE = /\.js\.map$/i;
  48200. /**
  48201. * Scan and optimize dependencies within a project.
  48202. * Used by Vite CLI when running `vite optimize`.
  48203. */
  48204. async function optimizeDeps(config, force = config.optimizeDeps.force, asCommand = false) {
  48205. const log = asCommand ? config.logger.info : debug$7;
  48206. const ssr = false;
  48207. const cachedMetadata = await loadCachedDepOptimizationMetadata(config, ssr, force, asCommand);
  48208. if (cachedMetadata) {
  48209. return cachedMetadata;
  48210. }
  48211. const deps = await discoverProjectDependencies(config).result;
  48212. const depsString = depsLogString(Object.keys(deps));
  48213. log?.(colors$1.green(`Optimizing dependencies:\n ${depsString}`));
  48214. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  48215. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  48216. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  48217. await result.commit();
  48218. return result.metadata;
  48219. }
  48220. async function optimizeServerSsrDeps(config) {
  48221. const ssr = true;
  48222. const cachedMetadata = await loadCachedDepOptimizationMetadata(config, ssr, config.optimizeDeps.force, false);
  48223. if (cachedMetadata) {
  48224. return cachedMetadata;
  48225. }
  48226. const deps = {};
  48227. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  48228. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  48229. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  48230. await result.commit();
  48231. return result.metadata;
  48232. }
  48233. function initDepsOptimizerMetadata(config, ssr, timestamp) {
  48234. const { lockfileHash, configHash, hash } = getDepHash(config, ssr);
  48235. return {
  48236. hash,
  48237. lockfileHash,
  48238. configHash,
  48239. browserHash: getOptimizedBrowserHash(hash, {}, timestamp),
  48240. optimized: {},
  48241. chunks: {},
  48242. discovered: {},
  48243. depInfoList: [],
  48244. };
  48245. }
  48246. function addOptimizedDepInfo(metadata, type, depInfo) {
  48247. metadata[type][depInfo.id] = depInfo;
  48248. metadata.depInfoList.push(depInfo);
  48249. return depInfo;
  48250. }
  48251. let firstLoadCachedDepOptimizationMetadata = true;
  48252. /**
  48253. * Creates the initial dep optimization metadata, loading it from the deps cache
  48254. * if it exists and pre-bundling isn't forced
  48255. */
  48256. async function loadCachedDepOptimizationMetadata(config, ssr, force = config.optimizeDeps.force, asCommand = false) {
  48257. const log = asCommand ? config.logger.info : debug$7;
  48258. if (firstLoadCachedDepOptimizationMetadata) {
  48259. firstLoadCachedDepOptimizationMetadata = false;
  48260. // Fire up a clean up of stale processing deps dirs if older process exited early
  48261. setTimeout(() => cleanupDepsCacheStaleDirs(config), 0);
  48262. }
  48263. const depsCacheDir = getDepsCacheDir(config, ssr);
  48264. if (!force) {
  48265. let cachedMetadata;
  48266. try {
  48267. const cachedMetadataPath = path$o.join(depsCacheDir, METADATA_FILENAME);
  48268. cachedMetadata = parseDepsOptimizerMetadata(await fsp.readFile(cachedMetadataPath, 'utf-8'), depsCacheDir);
  48269. }
  48270. catch (e) { }
  48271. // hash is consistent, no need to re-bundle
  48272. if (cachedMetadata) {
  48273. if (cachedMetadata.lockfileHash !== getLockfileHash(config)) {
  48274. config.logger.info('Re-optimizing dependencies because lockfile has changed');
  48275. }
  48276. else if (cachedMetadata.configHash !== getConfigHash(config, ssr)) {
  48277. config.logger.info('Re-optimizing dependencies because vite config has changed');
  48278. }
  48279. else {
  48280. log?.('Hash is consistent. Skipping. Use --force to override.');
  48281. // Nothing to commit or cancel as we are using the cache, we only
  48282. // need to resolve the processing promise so requests can move on
  48283. return cachedMetadata;
  48284. }
  48285. }
  48286. }
  48287. else {
  48288. config.logger.info('Forced re-optimization of dependencies');
  48289. }
  48290. // Start with a fresh cache
  48291. debug$7?.(colors$1.green(`removing old cache dir ${depsCacheDir}`));
  48292. await fsp.rm(depsCacheDir, { recursive: true, force: true });
  48293. }
  48294. /**
  48295. * Initial optimizeDeps at server start. Perform a fast scan using esbuild to
  48296. * find deps to pre-bundle and include user hard-coded dependencies
  48297. */
  48298. function discoverProjectDependencies(config) {
  48299. const { cancel, result } = scanImports(config);
  48300. return {
  48301. cancel,
  48302. result: result.then(({ deps, missing }) => {
  48303. const missingIds = Object.keys(missing);
  48304. if (missingIds.length) {
  48305. throw new Error(`The following dependencies are imported but could not be resolved:\n\n ${missingIds
  48306. .map((id) => `${colors$1.cyan(id)} ${colors$1.white(colors$1.dim(`(imported by ${missing[id]})`))}`)
  48307. .join(`\n `)}\n\nAre they installed?`);
  48308. }
  48309. return deps;
  48310. }),
  48311. };
  48312. }
  48313. function toDiscoveredDependencies(config, deps, ssr, timestamp) {
  48314. const browserHash = getOptimizedBrowserHash(getDepHash(config, ssr).hash, deps, timestamp);
  48315. const discovered = {};
  48316. for (const id in deps) {
  48317. const src = deps[id];
  48318. discovered[id] = {
  48319. id,
  48320. file: getOptimizedDepPath(id, config, ssr),
  48321. src,
  48322. browserHash: browserHash,
  48323. exportsData: extractExportsData(src, config, ssr),
  48324. };
  48325. }
  48326. return discovered;
  48327. }
  48328. function depsLogString(qualifiedIds) {
  48329. return colors$1.yellow(qualifiedIds.join(`, `));
  48330. }
  48331. /**
  48332. * Internally, Vite uses this function to prepare a optimizeDeps run. When Vite starts, we can get
  48333. * the metadata and start the server without waiting for the optimizeDeps processing to be completed
  48334. */
  48335. function runOptimizeDeps(resolvedConfig, depsInfo, ssr) {
  48336. const optimizerContext = { cancelled: false };
  48337. const config = {
  48338. ...resolvedConfig,
  48339. command: 'build',
  48340. };
  48341. const depsCacheDir = getDepsCacheDir(resolvedConfig, ssr);
  48342. const processingCacheDir = getProcessingDepsCacheDir(resolvedConfig, ssr);
  48343. // Create a temporary directory so we don't need to delete optimized deps
  48344. // until they have been processed. This also avoids leaving the deps cache
  48345. // directory in a corrupted state if there is an error
  48346. fs$l.mkdirSync(processingCacheDir, { recursive: true });
  48347. // a hint for Node.js
  48348. // all files in the cache directory should be recognized as ES modules
  48349. debug$7?.(colors$1.green(`creating package.json in ${processingCacheDir}`));
  48350. fs$l.writeFileSync(path$o.resolve(processingCacheDir, 'package.json'), `{\n "type": "module"\n}\n`);
  48351. const metadata = initDepsOptimizerMetadata(config, ssr);
  48352. metadata.browserHash = getOptimizedBrowserHash(metadata.hash, depsFromOptimizedDepInfo(depsInfo));
  48353. // We prebundle dependencies with esbuild and cache them, but there is no need
  48354. // to wait here. Code that needs to access the cached deps needs to await
  48355. // the optimizedDepInfo.processing promise for each dep
  48356. const qualifiedIds = Object.keys(depsInfo);
  48357. let cleaned = false;
  48358. let committed = false;
  48359. const cleanUp = () => {
  48360. // If commit was already called, ignore the clean up even if a cancel was requested
  48361. // This minimizes the chances of leaving the deps cache in a corrupted state
  48362. if (!cleaned && !committed) {
  48363. cleaned = true;
  48364. // No need to wait, we can clean up in the background because temp folders
  48365. // are unique per run
  48366. debug$7?.(colors$1.green(`removing cache dir ${processingCacheDir}`));
  48367. try {
  48368. // When exiting the process, `fsp.rm` may not take effect, so we use `fs.rmSync`
  48369. fs$l.rmSync(processingCacheDir, { recursive: true, force: true });
  48370. }
  48371. catch (error) {
  48372. // Ignore errors
  48373. }
  48374. }
  48375. };
  48376. const successfulResult = {
  48377. metadata,
  48378. cancel: cleanUp,
  48379. commit: async () => {
  48380. if (cleaned) {
  48381. throw new Error('Can not commit a Deps Optimization run as it was cancelled');
  48382. }
  48383. // Ignore clean up requests after this point so the temp folder isn't deleted before
  48384. // we finish commiting the new deps cache files to the deps folder
  48385. committed = true;
  48386. // Write metadata file, then commit the processing folder to the global deps cache
  48387. // Rewire the file paths from the temporary processing dir to the final deps cache dir
  48388. const dataPath = path$o.join(processingCacheDir, METADATA_FILENAME);
  48389. debug$7?.(colors$1.green(`creating ${METADATA_FILENAME} in ${processingCacheDir}`));
  48390. fs$l.writeFileSync(dataPath, stringifyDepsOptimizerMetadata(metadata, depsCacheDir));
  48391. // In order to minimize the time where the deps folder isn't in a consistent state,
  48392. // we first rename the old depsCacheDir to a temporary path, then we rename the
  48393. // new processing cache dir to the depsCacheDir. In systems where doing so in sync
  48394. // is safe, we do an atomic operation (at least for this thread). For Windows, we
  48395. // found there are cases where the rename operation may finish before it's done
  48396. // so we do a graceful rename checking that the folder has been properly renamed.
  48397. // We found that the rename-rename (then delete the old folder in the background)
  48398. // is safer than a delete-rename operation.
  48399. const temporaryPath = depsCacheDir + getTempSuffix();
  48400. const depsCacheDirPresent = fs$l.existsSync(depsCacheDir);
  48401. if (isWindows$5) {
  48402. if (depsCacheDirPresent) {
  48403. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  48404. await safeRename(depsCacheDir, temporaryPath);
  48405. }
  48406. debug$7?.(colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`));
  48407. await safeRename(processingCacheDir, depsCacheDir);
  48408. }
  48409. else {
  48410. if (depsCacheDirPresent) {
  48411. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  48412. fs$l.renameSync(depsCacheDir, temporaryPath);
  48413. }
  48414. debug$7?.(colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`));
  48415. fs$l.renameSync(processingCacheDir, depsCacheDir);
  48416. }
  48417. // Delete temporary path in the background
  48418. if (depsCacheDirPresent) {
  48419. debug$7?.(colors$1.green(`removing cache temp dir ${temporaryPath}`));
  48420. fsp.rm(temporaryPath, { recursive: true, force: true });
  48421. }
  48422. },
  48423. };
  48424. if (!qualifiedIds.length) {
  48425. // No deps to optimize, we still commit the processing cache dir to remove
  48426. // the previous optimized deps if they exist, and let the next server start
  48427. // skip the scanner step if the lockfile hasn't changed
  48428. return {
  48429. cancel: async () => cleanUp(),
  48430. result: Promise.resolve(successfulResult),
  48431. };
  48432. }
  48433. const cancelledResult = {
  48434. metadata,
  48435. commit: async () => cleanUp(),
  48436. cancel: cleanUp,
  48437. };
  48438. const start = performance.now();
  48439. const preparedRun = prepareEsbuildOptimizerRun(resolvedConfig, depsInfo, ssr, processingCacheDir, optimizerContext);
  48440. const runResult = preparedRun.then(({ context, idToExports }) => {
  48441. function disposeContext() {
  48442. return context?.dispose().catch((e) => {
  48443. config.logger.error('Failed to dispose esbuild context', { error: e });
  48444. });
  48445. }
  48446. if (!context || optimizerContext.cancelled) {
  48447. disposeContext();
  48448. return cancelledResult;
  48449. }
  48450. return context
  48451. .rebuild()
  48452. .then((result) => {
  48453. const meta = result.metafile;
  48454. // the paths in `meta.outputs` are relative to `process.cwd()`
  48455. const processingCacheDirOutputPath = path$o.relative(process.cwd(), processingCacheDir);
  48456. for (const id in depsInfo) {
  48457. const output = esbuildOutputFromId(meta.outputs, id, processingCacheDir);
  48458. const { exportsData, ...info } = depsInfo[id];
  48459. addOptimizedDepInfo(metadata, 'optimized', {
  48460. ...info,
  48461. // We only need to hash the output.imports in to check for stability, but adding the hash
  48462. // and file path gives us a unique hash that may be useful for other things in the future
  48463. fileHash: getHash(metadata.hash +
  48464. depsInfo[id].file +
  48465. JSON.stringify(output.imports)),
  48466. browserHash: metadata.browserHash,
  48467. // After bundling we have more information and can warn the user about legacy packages
  48468. // that require manual configuration
  48469. needsInterop: needsInterop(config, ssr, id, idToExports[id], output),
  48470. });
  48471. }
  48472. for (const o of Object.keys(meta.outputs)) {
  48473. if (!jsMapExtensionRE.test(o)) {
  48474. const id = path$o
  48475. .relative(processingCacheDirOutputPath, o)
  48476. .replace(jsExtensionRE, '');
  48477. const file = getOptimizedDepPath(id, resolvedConfig, ssr);
  48478. if (!findOptimizedDepInfoInRecord(metadata.optimized, (depInfo) => depInfo.file === file)) {
  48479. addOptimizedDepInfo(metadata, 'chunks', {
  48480. id,
  48481. file,
  48482. needsInterop: false,
  48483. browserHash: metadata.browserHash,
  48484. });
  48485. }
  48486. }
  48487. }
  48488. debug$7?.(`Dependencies bundled in ${(performance.now() - start).toFixed(2)}ms`);
  48489. return successfulResult;
  48490. })
  48491. .catch((e) => {
  48492. if (e.errors && e.message.includes('The build was canceled')) {
  48493. // esbuild logs an error when cancelling, but this is expected so
  48494. // return an empty result instead
  48495. return cancelledResult;
  48496. }
  48497. throw e;
  48498. })
  48499. .finally(() => {
  48500. return disposeContext();
  48501. });
  48502. });
  48503. runResult.catch(() => {
  48504. cleanUp();
  48505. });
  48506. return {
  48507. async cancel() {
  48508. optimizerContext.cancelled = true;
  48509. const { context } = await preparedRun;
  48510. await context?.cancel();
  48511. cleanUp();
  48512. },
  48513. result: runResult,
  48514. };
  48515. }
  48516. async function prepareEsbuildOptimizerRun(resolvedConfig, depsInfo, ssr, processingCacheDir, optimizerContext) {
  48517. const config = {
  48518. ...resolvedConfig,
  48519. command: 'build',
  48520. };
  48521. // esbuild generates nested directory output with lowest common ancestor base
  48522. // this is unpredictable and makes it difficult to analyze entry / output
  48523. // mapping. So what we do here is:
  48524. // 1. flatten all ids to eliminate slash
  48525. // 2. in the plugin, read the entry ourselves as virtual files to retain the
  48526. // path.
  48527. const flatIdDeps = {};
  48528. const idToExports = {};
  48529. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  48530. const { plugins: pluginsFromConfig = [], ...esbuildOptions } = optimizeDeps?.esbuildOptions ?? {};
  48531. await Promise.all(Object.keys(depsInfo).map(async (id) => {
  48532. const src = depsInfo[id].src;
  48533. const exportsData = await (depsInfo[id].exportsData ??
  48534. extractExportsData(src, config, ssr));
  48535. if (exportsData.jsxLoader && !esbuildOptions.loader?.['.js']) {
  48536. // Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
  48537. // This is useful for packages such as Gatsby.
  48538. esbuildOptions.loader = {
  48539. '.js': 'jsx',
  48540. ...esbuildOptions.loader,
  48541. };
  48542. }
  48543. const flatId = flattenId(id);
  48544. flatIdDeps[flatId] = src;
  48545. idToExports[id] = exportsData;
  48546. }));
  48547. if (optimizerContext.cancelled)
  48548. return { context: undefined, idToExports };
  48549. const define = {
  48550. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode),
  48551. };
  48552. const platform = ssr && config.ssr?.target !== 'webworker' ? 'node' : 'browser';
  48553. const external = [...(optimizeDeps?.exclude ?? [])];
  48554. const plugins = [...pluginsFromConfig];
  48555. if (external.length) {
  48556. plugins.push(esbuildCjsExternalPlugin(external, platform));
  48557. }
  48558. plugins.push(esbuildDepPlugin(flatIdDeps, external, config, ssr));
  48559. const context = await esbuild.context({
  48560. absWorkingDir: process.cwd(),
  48561. entryPoints: Object.keys(flatIdDeps),
  48562. bundle: true,
  48563. // We can't use platform 'neutral', as esbuild has custom handling
  48564. // when the platform is 'node' or 'browser' that can't be emulated
  48565. // by using mainFields and conditions
  48566. platform,
  48567. define,
  48568. format: 'esm',
  48569. // See https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
  48570. banner: platform === 'node'
  48571. ? {
  48572. js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);`,
  48573. }
  48574. : undefined,
  48575. target: ESBUILD_MODULES_TARGET,
  48576. external,
  48577. logLevel: 'error',
  48578. splitting: true,
  48579. sourcemap: true,
  48580. outdir: processingCacheDir,
  48581. ignoreAnnotations: true,
  48582. metafile: true,
  48583. plugins,
  48584. charset: 'utf8',
  48585. ...esbuildOptions,
  48586. supported: {
  48587. ...defaultEsbuildSupported,
  48588. ...esbuildOptions.supported,
  48589. },
  48590. });
  48591. return { context, idToExports };
  48592. }
  48593. async function addManuallyIncludedOptimizeDeps(deps, config, ssr) {
  48594. const { logger } = config;
  48595. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  48596. const optimizeDepsInclude = optimizeDeps?.include ?? [];
  48597. if (optimizeDepsInclude.length) {
  48598. const unableToOptimize = (id, msg) => {
  48599. if (optimizeDepsInclude.includes(id)) {
  48600. logger.warn(`${msg}: ${colors$1.cyan(id)}, present in '${ssr ? 'ssr.' : ''}optimizeDeps.include'`);
  48601. }
  48602. };
  48603. const includes = [...optimizeDepsInclude];
  48604. for (let i = 0; i < includes.length; i++) {
  48605. const id = includes[i];
  48606. if (glob.isDynamicPattern(id)) {
  48607. const globIds = expandGlobIds(id, config);
  48608. includes.splice(i, 1, ...globIds);
  48609. i += globIds.length - 1;
  48610. }
  48611. }
  48612. const resolve = createOptimizeDepsIncludeResolver(config, ssr);
  48613. for (const id of includes) {
  48614. // normalize 'foo >bar` as 'foo > bar' to prevent same id being added
  48615. // and for pretty printing
  48616. const normalizedId = normalizeId(id);
  48617. if (!deps[normalizedId]) {
  48618. const entry = await resolve(id);
  48619. if (entry) {
  48620. if (isOptimizable(entry, optimizeDeps)) {
  48621. if (!entry.endsWith('?__vite_skip_optimization')) {
  48622. deps[normalizedId] = entry;
  48623. }
  48624. }
  48625. else {
  48626. unableToOptimize(id, 'Cannot optimize dependency');
  48627. }
  48628. }
  48629. else {
  48630. unableToOptimize(id, 'Failed to resolve dependency');
  48631. }
  48632. }
  48633. }
  48634. }
  48635. }
  48636. // Convert to { id: src }
  48637. function depsFromOptimizedDepInfo(depsInfo) {
  48638. const obj = {};
  48639. for (const key in depsInfo) {
  48640. obj[key] = depsInfo[key].src;
  48641. }
  48642. return obj;
  48643. }
  48644. function getOptimizedDepPath(id, config, ssr) {
  48645. return normalizePath$3(path$o.resolve(getDepsCacheDir(config, ssr), flattenId(id) + '.js'));
  48646. }
  48647. function getDepsCacheSuffix(ssr) {
  48648. return ssr ? '_ssr' : '';
  48649. }
  48650. function getDepsCacheDir(config, ssr) {
  48651. return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr);
  48652. }
  48653. function getProcessingDepsCacheDir(config, ssr) {
  48654. return (getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr) + getTempSuffix());
  48655. }
  48656. function getTempSuffix() {
  48657. return ('_temp_' +
  48658. getHash(`${process.pid}:${Date.now().toString()}:${Math.random()
  48659. .toString(16)
  48660. .slice(2)}`));
  48661. }
  48662. function getDepsCacheDirPrefix(config) {
  48663. return normalizePath$3(path$o.resolve(config.cacheDir, 'deps'));
  48664. }
  48665. function createIsOptimizedDepFile(config) {
  48666. const depsCacheDirPrefix = getDepsCacheDirPrefix(config);
  48667. return (id) => id.startsWith(depsCacheDirPrefix);
  48668. }
  48669. function createIsOptimizedDepUrl(config) {
  48670. const { root } = config;
  48671. const depsCacheDir = getDepsCacheDirPrefix(config);
  48672. // determine the url prefix of files inside cache directory
  48673. const depsCacheDirRelative = normalizePath$3(path$o.relative(root, depsCacheDir));
  48674. const depsCacheDirPrefix = depsCacheDirRelative.startsWith('../')
  48675. ? // if the cache directory is outside root, the url prefix would be something
  48676. // like '/@fs/absolute/path/to/node_modules/.vite'
  48677. `/@fs/${removeLeadingSlash(normalizePath$3(depsCacheDir))}`
  48678. : // if the cache directory is inside root, the url prefix would be something
  48679. // like '/node_modules/.vite'
  48680. `/${depsCacheDirRelative}`;
  48681. return function isOptimizedDepUrl(url) {
  48682. return url.startsWith(depsCacheDirPrefix);
  48683. };
  48684. }
  48685. function parseDepsOptimizerMetadata(jsonMetadata, depsCacheDir) {
  48686. const { hash, lockfileHash, configHash, browserHash, optimized, chunks } = JSON.parse(jsonMetadata, (key, value) => {
  48687. // Paths can be absolute or relative to the deps cache dir where
  48688. // the _metadata.json is located
  48689. if (key === 'file' || key === 'src') {
  48690. return normalizePath$3(path$o.resolve(depsCacheDir, value));
  48691. }
  48692. return value;
  48693. });
  48694. if (!chunks ||
  48695. Object.values(optimized).some((depInfo) => !depInfo.fileHash)) {
  48696. // outdated _metadata.json version, ignore
  48697. return;
  48698. }
  48699. const metadata = {
  48700. hash,
  48701. lockfileHash,
  48702. configHash,
  48703. browserHash,
  48704. optimized: {},
  48705. discovered: {},
  48706. chunks: {},
  48707. depInfoList: [],
  48708. };
  48709. for (const id of Object.keys(optimized)) {
  48710. addOptimizedDepInfo(metadata, 'optimized', {
  48711. ...optimized[id],
  48712. id,
  48713. browserHash,
  48714. });
  48715. }
  48716. for (const id of Object.keys(chunks)) {
  48717. addOptimizedDepInfo(metadata, 'chunks', {
  48718. ...chunks[id],
  48719. id,
  48720. browserHash,
  48721. needsInterop: false,
  48722. });
  48723. }
  48724. return metadata;
  48725. }
  48726. /**
  48727. * Stringify metadata for deps cache. Remove processing promises
  48728. * and individual dep info browserHash. Once the cache is reload
  48729. * the next time the server start we need to use the global
  48730. * browserHash to allow long term caching
  48731. */
  48732. function stringifyDepsOptimizerMetadata(metadata, depsCacheDir) {
  48733. const { hash, configHash, lockfileHash, browserHash, optimized, chunks } = metadata;
  48734. return JSON.stringify({
  48735. hash,
  48736. configHash,
  48737. lockfileHash,
  48738. browserHash,
  48739. optimized: Object.fromEntries(Object.values(optimized).map(({ id, src, file, fileHash, needsInterop }) => [
  48740. id,
  48741. {
  48742. src,
  48743. file,
  48744. fileHash,
  48745. needsInterop,
  48746. },
  48747. ])),
  48748. chunks: Object.fromEntries(Object.values(chunks).map(({ id, file }) => [id, { file }])),
  48749. }, (key, value) => {
  48750. // Paths can be absolute or relative to the deps cache dir where
  48751. // the _metadata.json is located
  48752. if (key === 'file' || key === 'src') {
  48753. return normalizePath$3(path$o.relative(depsCacheDir, value));
  48754. }
  48755. return value;
  48756. }, 2);
  48757. }
  48758. function esbuildOutputFromId(outputs, id, cacheDirOutputPath) {
  48759. const cwd = process.cwd();
  48760. const flatId = flattenId(id) + '.js';
  48761. const normalizedOutputPath = normalizePath$3(path$o.relative(cwd, path$o.join(cacheDirOutputPath, flatId)));
  48762. const output = outputs[normalizedOutputPath];
  48763. if (output) {
  48764. return output;
  48765. }
  48766. // If the root dir was symlinked, esbuild could return output keys as `../cwd/`
  48767. // Normalize keys to support this case too
  48768. for (const [key, value] of Object.entries(outputs)) {
  48769. if (normalizePath$3(path$o.relative(cwd, key)) === normalizedOutputPath) {
  48770. return value;
  48771. }
  48772. }
  48773. }
  48774. async function extractExportsData(filePath, config, ssr) {
  48775. await init;
  48776. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  48777. const esbuildOptions = optimizeDeps?.esbuildOptions ?? {};
  48778. if (optimizeDeps.extensions?.some((ext) => filePath.endsWith(ext))) {
  48779. // For custom supported extensions, build the entry file to transform it into JS,
  48780. // and then parse with es-module-lexer. Note that the `bundle` option is not `true`,
  48781. // so only the entry file is being transformed.
  48782. const result = await build$3({
  48783. ...esbuildOptions,
  48784. entryPoints: [filePath],
  48785. write: false,
  48786. format: 'esm',
  48787. });
  48788. const [, exports, , hasModuleSyntax] = parse$e(result.outputFiles[0].text);
  48789. return {
  48790. hasModuleSyntax,
  48791. exports: exports.map((e) => e.n),
  48792. };
  48793. }
  48794. let parseResult;
  48795. let usedJsxLoader = false;
  48796. const entryContent = await fsp.readFile(filePath, 'utf-8');
  48797. try {
  48798. parseResult = parse$e(entryContent);
  48799. }
  48800. catch {
  48801. const loader = esbuildOptions.loader?.[path$o.extname(filePath)] || 'jsx';
  48802. debug$7?.(`Unable to parse: ${filePath}.\n Trying again with a ${loader} transform.`);
  48803. const transformed = await transformWithEsbuild(entryContent, filePath, {
  48804. loader,
  48805. });
  48806. parseResult = parse$e(transformed.code);
  48807. usedJsxLoader = true;
  48808. }
  48809. const [, exports, , hasModuleSyntax] = parseResult;
  48810. const exportsData = {
  48811. hasModuleSyntax,
  48812. exports: exports.map((e) => e.n),
  48813. jsxLoader: usedJsxLoader,
  48814. };
  48815. return exportsData;
  48816. }
  48817. function needsInterop(config, ssr, id, exportsData, output) {
  48818. if (getDepOptimizationConfig(config, ssr)?.needsInterop?.includes(id)) {
  48819. return true;
  48820. }
  48821. const { hasModuleSyntax, exports } = exportsData;
  48822. // entry has no ESM syntax - likely CJS or UMD
  48823. if (!hasModuleSyntax) {
  48824. return true;
  48825. }
  48826. if (output) {
  48827. // if a peer dependency used require() on an ESM dependency, esbuild turns the
  48828. // ESM dependency's entry chunk into a single default export... detect
  48829. // such cases by checking exports mismatch, and force interop.
  48830. const generatedExports = output.exports;
  48831. if (!generatedExports ||
  48832. (isSingleDefaultExport(generatedExports) &&
  48833. !isSingleDefaultExport(exports))) {
  48834. return true;
  48835. }
  48836. }
  48837. return false;
  48838. }
  48839. function isSingleDefaultExport(exports) {
  48840. return exports.length === 1 && exports[0] === 'default';
  48841. }
  48842. const lockfileFormats = [
  48843. { name: 'package-lock.json', checkPatches: true, manager: 'npm' },
  48844. { name: 'yarn.lock', checkPatches: true, manager: 'yarn' },
  48845. { name: 'pnpm-lock.yaml', checkPatches: false, manager: 'pnpm' },
  48846. { name: 'bun.lockb', checkPatches: true, manager: 'bun' },
  48847. ].sort((_, { manager }) => {
  48848. return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1;
  48849. });
  48850. const lockfileNames = lockfileFormats.map((l) => l.name);
  48851. function getConfigHash(config, ssr) {
  48852. // Take config into account
  48853. // only a subset of config options that can affect dep optimization
  48854. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  48855. const content = JSON.stringify({
  48856. mode: process.env.NODE_ENV || config.mode,
  48857. root: config.root,
  48858. resolve: config.resolve,
  48859. assetsInclude: config.assetsInclude,
  48860. plugins: config.plugins.map((p) => p.name),
  48861. optimizeDeps: {
  48862. include: optimizeDeps?.include
  48863. ? unique(optimizeDeps.include).sort()
  48864. : undefined,
  48865. exclude: optimizeDeps?.exclude
  48866. ? unique(optimizeDeps.exclude).sort()
  48867. : undefined,
  48868. esbuildOptions: {
  48869. ...optimizeDeps?.esbuildOptions,
  48870. plugins: optimizeDeps?.esbuildOptions?.plugins?.map((p) => p.name),
  48871. },
  48872. },
  48873. }, (_, value) => {
  48874. if (typeof value === 'function' || value instanceof RegExp) {
  48875. return value.toString();
  48876. }
  48877. return value;
  48878. });
  48879. return getHash(content);
  48880. }
  48881. function getLockfileHash(config, ssr) {
  48882. const lockfilePath = lookupFile(config.root, lockfileNames);
  48883. let content = lockfilePath ? fs$l.readFileSync(lockfilePath, 'utf-8') : '';
  48884. if (lockfilePath) {
  48885. const lockfileName = path$o.basename(lockfilePath);
  48886. const { checkPatches } = lockfileFormats.find((f) => f.name === lockfileName);
  48887. if (checkPatches) {
  48888. // Default of https://github.com/ds300/patch-package
  48889. const fullPath = path$o.join(path$o.dirname(lockfilePath), 'patches');
  48890. const stat = tryStatSync(fullPath);
  48891. if (stat?.isDirectory()) {
  48892. content += stat.mtimeMs.toString();
  48893. }
  48894. }
  48895. }
  48896. return getHash(content);
  48897. }
  48898. function getDepHash(config, ssr) {
  48899. const lockfileHash = getLockfileHash(config);
  48900. const configHash = getConfigHash(config, ssr);
  48901. const hash = getHash(lockfileHash + configHash);
  48902. return {
  48903. hash,
  48904. lockfileHash,
  48905. configHash,
  48906. };
  48907. }
  48908. function getOptimizedBrowserHash(hash, deps, timestamp = '') {
  48909. return getHash(hash + JSON.stringify(deps) + timestamp);
  48910. }
  48911. function optimizedDepInfoFromId(metadata, id) {
  48912. return (metadata.optimized[id] || metadata.discovered[id] || metadata.chunks[id]);
  48913. }
  48914. function optimizedDepInfoFromFile(metadata, file) {
  48915. return metadata.depInfoList.find((depInfo) => depInfo.file === file);
  48916. }
  48917. function findOptimizedDepInfoInRecord(dependenciesInfo, callbackFn) {
  48918. for (const o of Object.keys(dependenciesInfo)) {
  48919. const info = dependenciesInfo[o];
  48920. if (callbackFn(info, o)) {
  48921. return info;
  48922. }
  48923. }
  48924. }
  48925. async function optimizedDepNeedsInterop(metadata, file, config, ssr) {
  48926. const depInfo = optimizedDepInfoFromFile(metadata, file);
  48927. if (depInfo?.src && depInfo.needsInterop === undefined) {
  48928. depInfo.exportsData ??= extractExportsData(depInfo.src, config, ssr);
  48929. depInfo.needsInterop = needsInterop(config, ssr, depInfo.id, await depInfo.exportsData);
  48930. }
  48931. return depInfo?.needsInterop;
  48932. }
  48933. const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1000;
  48934. async function cleanupDepsCacheStaleDirs(config) {
  48935. try {
  48936. const cacheDir = path$o.resolve(config.cacheDir);
  48937. if (fs$l.existsSync(cacheDir)) {
  48938. const dirents = await fsp.readdir(cacheDir, { withFileTypes: true });
  48939. for (const dirent of dirents) {
  48940. if (dirent.isDirectory() && dirent.name.includes('_temp_')) {
  48941. const tempDirPath = path$o.resolve(config.cacheDir, dirent.name);
  48942. const stats = await fsp.stat(tempDirPath).catch((_) => null);
  48943. if (stats?.mtime &&
  48944. Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS) {
  48945. debug$7?.(`removing stale cache temp dir ${tempDirPath}`);
  48946. await fsp.rm(tempDirPath, { recursive: true, force: true });
  48947. }
  48948. }
  48949. }
  48950. }
  48951. }
  48952. catch (err) {
  48953. config.logger.error(err);
  48954. }
  48955. }
  48956. // We found issues with renaming folders in some systems. This is a custom
  48957. // implementation for the optimizer. It isn't intended to be a general utility
  48958. // Based on node-graceful-fs
  48959. // The ISC License
  48960. // Copyright (c) 2011-2022 Isaac Z. Schlueter, Ben Noordhuis, and Contributors
  48961. // https://github.com/isaacs/node-graceful-fs/blob/main/LICENSE
  48962. // On Windows, A/V software can lock the directory, causing this
  48963. // to fail with an EACCES or EPERM if the directory contains newly
  48964. // created files. The original tried for up to 60 seconds, we only
  48965. // wait for 5 seconds, as a longer time would be seen as an error
  48966. const GRACEFUL_RENAME_TIMEOUT = 5000;
  48967. const safeRename = promisify$4(function gracefulRename(from, to, cb) {
  48968. const start = Date.now();
  48969. let backoff = 0;
  48970. fs$l.rename(from, to, function CB(er) {
  48971. if (er &&
  48972. (er.code === 'EACCES' || er.code === 'EPERM') &&
  48973. Date.now() - start < GRACEFUL_RENAME_TIMEOUT) {
  48974. setTimeout(function () {
  48975. fs$l.stat(to, function (stater, st) {
  48976. if (stater && stater.code === 'ENOENT')
  48977. fs$l.rename(from, to, CB);
  48978. else
  48979. CB(er);
  48980. });
  48981. }, backoff);
  48982. if (backoff < 100)
  48983. backoff += 10;
  48984. return;
  48985. }
  48986. if (cb)
  48987. cb(er);
  48988. });
  48989. });
  48990. var index$1 = {
  48991. __proto__: null,
  48992. addManuallyIncludedOptimizeDeps: addManuallyIncludedOptimizeDeps,
  48993. addOptimizedDepInfo: addOptimizedDepInfo,
  48994. cleanupDepsCacheStaleDirs: cleanupDepsCacheStaleDirs,
  48995. createIsOptimizedDepFile: createIsOptimizedDepFile,
  48996. createIsOptimizedDepUrl: createIsOptimizedDepUrl,
  48997. depsFromOptimizedDepInfo: depsFromOptimizedDepInfo,
  48998. depsLogString: depsLogString,
  48999. discoverProjectDependencies: discoverProjectDependencies,
  49000. extractExportsData: extractExportsData,
  49001. getDepsCacheDir: getDepsCacheDir,
  49002. getDepsOptimizer: getDepsOptimizer,
  49003. getOptimizedDepPath: getOptimizedDepPath,
  49004. initDepsOptimizer: initDepsOptimizer,
  49005. initDepsOptimizerMetadata: initDepsOptimizerMetadata,
  49006. initDevSsrDepsOptimizer: initDevSsrDepsOptimizer,
  49007. loadCachedDepOptimizationMetadata: loadCachedDepOptimizationMetadata,
  49008. optimizeDeps: optimizeDeps,
  49009. optimizeServerSsrDeps: optimizeServerSsrDeps,
  49010. optimizedDepInfoFromFile: optimizedDepInfoFromFile,
  49011. optimizedDepInfoFromId: optimizedDepInfoFromId,
  49012. optimizedDepNeedsInterop: optimizedDepNeedsInterop,
  49013. runOptimizeDeps: runOptimizeDeps,
  49014. toDiscoveredDependencies: toDiscoveredDependencies
  49015. };
  49016. function totalist(dir, callback, pre='') {
  49017. dir = resolve$3('.', dir);
  49018. let arr = readdirSync(dir);
  49019. let i=0, abs, stats;
  49020. for (; i < arr.length; i++) {
  49021. abs = join$1(dir, arr[i]);
  49022. stats = statSync$1(abs);
  49023. stats.isDirectory()
  49024. ? totalist(abs, callback, join$1(pre, arr[i]))
  49025. : callback(join$1(pre, arr[i]), abs, stats);
  49026. }
  49027. }
  49028. /**
  49029. * @typedef ParsedURL
  49030. * @type {import('.').ParsedURL}
  49031. */
  49032. /**
  49033. * @typedef Request
  49034. * @property {string} url
  49035. * @property {ParsedURL} _parsedUrl
  49036. */
  49037. /**
  49038. * @param {Request} req
  49039. * @returns {ParsedURL|void}
  49040. */
  49041. function parse$5(req) {
  49042. let raw = req.url;
  49043. if (raw == null) return;
  49044. let prev = req._parsedUrl;
  49045. if (prev && prev.raw === raw) return prev;
  49046. let pathname=raw, search='', query;
  49047. if (raw.length > 1) {
  49048. let idx = raw.indexOf('?', 1);
  49049. if (idx !== -1) {
  49050. search = raw.substring(idx);
  49051. pathname = raw.substring(0, idx);
  49052. if (search.length > 1) {
  49053. query = qs.parse(search.substring(1));
  49054. }
  49055. }
  49056. }
  49057. return req._parsedUrl = { pathname, search, query, raw };
  49058. }
  49059. const noop$1 = () => {};
  49060. function isMatch(uri, arr) {
  49061. for (let i=0; i < arr.length; i++) {
  49062. if (arr[i].test(uri)) return true;
  49063. }
  49064. }
  49065. function toAssume(uri, extns) {
  49066. let i=0, x, len=uri.length - 1;
  49067. if (uri.charCodeAt(len) === 47) {
  49068. uri = uri.substring(0, len);
  49069. }
  49070. let arr=[], tmp=`${uri}/index`;
  49071. for (; i < extns.length; i++) {
  49072. x = extns[i] ? `.${extns[i]}` : '';
  49073. if (uri) arr.push(uri + x);
  49074. arr.push(tmp + x);
  49075. }
  49076. return arr;
  49077. }
  49078. function viaCache(cache, uri, extns) {
  49079. let i=0, data, arr=toAssume(uri, extns);
  49080. for (; i < arr.length; i++) {
  49081. if (data = cache[arr[i]]) return data;
  49082. }
  49083. }
  49084. function viaLocal(dir, isEtag, uri, extns, shouldServe) {
  49085. let i=0, arr=toAssume(uri, extns);
  49086. let abs, stats, name, headers;
  49087. for (; i < arr.length; i++) {
  49088. abs = normalize(join$1(dir, name=arr[i]));
  49089. if (abs.startsWith(dir) && require$$0$2.existsSync(abs)) {
  49090. stats = require$$0$2.statSync(abs);
  49091. if (stats.isDirectory()) continue;
  49092. if (shouldServe && !shouldServe(abs)) continue;
  49093. headers = toHeaders(name, stats, isEtag);
  49094. headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
  49095. return { abs, stats, headers };
  49096. }
  49097. }
  49098. }
  49099. function is404(req, res) {
  49100. return (res.statusCode=404,res.end());
  49101. }
  49102. function send$1(req, res, file, stats, headers) {
  49103. let code=200, tmp, opts={};
  49104. headers = { ...headers };
  49105. for (let key in headers) {
  49106. tmp = res.getHeader(key);
  49107. if (tmp) headers[key] = tmp;
  49108. }
  49109. if (tmp = res.getHeader('content-type')) {
  49110. headers['Content-Type'] = tmp;
  49111. }
  49112. if (req.headers.range) {
  49113. code = 206;
  49114. let [x, y] = req.headers.range.replace('bytes=', '').split('-');
  49115. let end = opts.end = parseInt(y, 10) || stats.size - 1;
  49116. let start = opts.start = parseInt(x, 10) || 0;
  49117. if (end >= stats.size) {
  49118. end = stats.size - 1;
  49119. }
  49120. if (start >= stats.size) {
  49121. res.setHeader('Content-Range', `bytes */${stats.size}`);
  49122. res.statusCode = 416;
  49123. return res.end();
  49124. }
  49125. headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
  49126. headers['Content-Length'] = (end - start + 1);
  49127. headers['Accept-Ranges'] = 'bytes';
  49128. }
  49129. res.writeHead(code, headers);
  49130. require$$0$2.createReadStream(file, opts).pipe(res);
  49131. }
  49132. const ENCODING = {
  49133. '.br': 'br',
  49134. '.gz': 'gzip',
  49135. };
  49136. function toHeaders(name, stats, isEtag) {
  49137. let enc = ENCODING[name.slice(-3)];
  49138. let ctype = lookup(name.slice(0, enc && -3)) || '';
  49139. if (ctype === 'text/html') ctype += ';charset=utf-8';
  49140. let headers = {
  49141. 'Content-Length': stats.size,
  49142. 'Content-Type': ctype,
  49143. 'Last-Modified': stats.mtime.toUTCString(),
  49144. };
  49145. if (enc) headers['Content-Encoding'] = enc;
  49146. if (isEtag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
  49147. return headers;
  49148. }
  49149. function sirv (dir, opts={}) {
  49150. dir = resolve$3(dir || '.');
  49151. let isNotFound = opts.onNoMatch || is404;
  49152. let setHeaders = opts.setHeaders || noop$1;
  49153. let extensions = opts.extensions || ['html', 'htm'];
  49154. let gzips = opts.gzip && extensions.map(x => `${x}.gz`).concat('gz');
  49155. let brots = opts.brotli && extensions.map(x => `${x}.br`).concat('br');
  49156. const FILES = {};
  49157. let fallback = '/';
  49158. let isEtag = !!opts.etag;
  49159. let isSPA = !!opts.single;
  49160. if (typeof opts.single === 'string') {
  49161. let idx = opts.single.lastIndexOf('.');
  49162. fallback += !!~idx ? opts.single.substring(0, idx) : opts.single;
  49163. }
  49164. let ignores = [];
  49165. if (opts.ignores !== false) {
  49166. ignores.push(/[/]([A-Za-z\s\d~$._-]+\.\w+){1,}$/); // any extn
  49167. if (opts.dotfiles) ignores.push(/\/\.\w/);
  49168. else ignores.push(/\/\.well-known/);
  49169. [].concat(opts.ignores || []).forEach(x => {
  49170. ignores.push(new RegExp(x, 'i'));
  49171. });
  49172. }
  49173. let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
  49174. if (cc && opts.immutable) cc += ',immutable';
  49175. else if (cc && opts.maxAge === 0) cc += ',must-revalidate';
  49176. if (!opts.dev) {
  49177. totalist(dir, (name, abs, stats) => {
  49178. if (/\.well-known[\\+\/]/.test(name)) ; // keep
  49179. else if (!opts.dotfiles && /(^\.|[\\+|\/+]\.)/.test(name)) return;
  49180. let headers = toHeaders(name, stats, isEtag);
  49181. if (cc) headers['Cache-Control'] = cc;
  49182. FILES['/' + name.normalize().replace(/\\+/g, '/')] = { abs, stats, headers };
  49183. });
  49184. }
  49185. let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);
  49186. return function (req, res, next) {
  49187. let extns = [''];
  49188. let pathname = parse$5(req).pathname;
  49189. let val = req.headers['accept-encoding'] || '';
  49190. if (gzips && val.includes('gzip')) extns.unshift(...gzips);
  49191. if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots);
  49192. extns.push(...extensions); // [...br, ...gz, orig, ...exts]
  49193. if (pathname.indexOf('%') !== -1) {
  49194. try { pathname = decodeURI(pathname); }
  49195. catch (err) { /* malform uri */ }
  49196. }
  49197. let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe);
  49198. if (!data) return next ? next() : isNotFound(req, res);
  49199. if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
  49200. res.writeHead(304);
  49201. return res.end();
  49202. }
  49203. if (gzips || brots) {
  49204. res.setHeader('Vary', 'Accept-Encoding');
  49205. }
  49206. setHeaders(res, pathname, data.stats);
  49207. send$1(req, res, data.abs, data.stats, data.headers);
  49208. };
  49209. }
  49210. const knownJavascriptExtensionRE = /\.[tj]sx?$/;
  49211. const sirvOptions = ({ getHeaders, }) => {
  49212. return {
  49213. dev: true,
  49214. etag: true,
  49215. extensions: [],
  49216. setHeaders(res, pathname) {
  49217. // Matches js, jsx, ts, tsx.
  49218. // The reason this is done, is that the .ts file extension is reserved
  49219. // for the MIME type video/mp2t. In almost all cases, we can expect
  49220. // these files to be TypeScript files, and for Vite to serve them with
  49221. // this Content-Type.
  49222. if (knownJavascriptExtensionRE.test(pathname)) {
  49223. res.setHeader('Content-Type', 'text/javascript');
  49224. }
  49225. const headers = getHeaders();
  49226. if (headers) {
  49227. for (const name in headers) {
  49228. res.setHeader(name, headers[name]);
  49229. }
  49230. }
  49231. },
  49232. };
  49233. };
  49234. function servePublicMiddleware(server, publicFiles) {
  49235. const dir = server.config.publicDir;
  49236. const serve = sirv(dir, sirvOptions({
  49237. getHeaders: () => server.config.server.headers,
  49238. }));
  49239. const toFilePath = (url) => {
  49240. let filePath = cleanUrl(url);
  49241. if (filePath.indexOf('%') !== -1) {
  49242. try {
  49243. filePath = decodeURI(filePath);
  49244. }
  49245. catch (err) {
  49246. /* malform uri */
  49247. }
  49248. }
  49249. return normalizePath$3(filePath);
  49250. };
  49251. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  49252. return function viteServePublicMiddleware(req, res, next) {
  49253. // To avoid the performance impact of `existsSync` on every request, we check against an
  49254. // in-memory set of known public files. This set is updated on restarts.
  49255. // also skip import request and internal requests `/@fs/ /@vite-client` etc...
  49256. if ((publicFiles && !publicFiles.has(toFilePath(req.url))) ||
  49257. isImportRequest(req.url) ||
  49258. isInternalRequest(req.url)) {
  49259. return next();
  49260. }
  49261. serve(req, res, next);
  49262. };
  49263. }
  49264. function serveStaticMiddleware(server) {
  49265. const dir = server.config.root;
  49266. const serve = sirv(dir, sirvOptions({
  49267. getHeaders: () => server.config.server.headers,
  49268. }));
  49269. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  49270. return function viteServeStaticMiddleware(req, res, next) {
  49271. // only serve the file if it's not an html request or ends with `/`
  49272. // so that html requests can fallthrough to our html middleware for
  49273. // special processing
  49274. // also skip internal requests `/@fs/ /@vite-client` etc...
  49275. const cleanedUrl = cleanUrl(req.url);
  49276. if (cleanedUrl[cleanedUrl.length - 1] === '/' ||
  49277. path$o.extname(cleanedUrl) === '.html' ||
  49278. isInternalRequest(req.url)) {
  49279. return next();
  49280. }
  49281. const url = new URL(req.url.replace(/^\/{2,}/, '/'), 'http://example.com');
  49282. const pathname = decodeURI(url.pathname);
  49283. // apply aliases to static requests as well
  49284. let redirectedPathname;
  49285. for (const { find, replacement } of server.config.resolve.alias) {
  49286. const matches = typeof find === 'string'
  49287. ? pathname.startsWith(find)
  49288. : find.test(pathname);
  49289. if (matches) {
  49290. redirectedPathname = pathname.replace(find, replacement);
  49291. break;
  49292. }
  49293. }
  49294. if (redirectedPathname) {
  49295. // dir is pre-normalized to posix style
  49296. if (redirectedPathname.startsWith(withTrailingSlash(dir))) {
  49297. redirectedPathname = redirectedPathname.slice(dir.length);
  49298. }
  49299. }
  49300. const resolvedPathname = redirectedPathname || pathname;
  49301. let fileUrl = path$o.resolve(dir, removeLeadingSlash(resolvedPathname));
  49302. if (resolvedPathname[resolvedPathname.length - 1] === '/' &&
  49303. fileUrl[fileUrl.length - 1] !== '/') {
  49304. fileUrl = withTrailingSlash(fileUrl);
  49305. }
  49306. if (!ensureServingAccess(fileUrl, server, res, next)) {
  49307. return;
  49308. }
  49309. if (redirectedPathname) {
  49310. url.pathname = encodeURI(redirectedPathname);
  49311. req.url = url.href.slice(url.origin.length);
  49312. }
  49313. serve(req, res, next);
  49314. };
  49315. }
  49316. function serveRawFsMiddleware(server) {
  49317. const serveFromRoot = sirv('/', sirvOptions({ getHeaders: () => server.config.server.headers }));
  49318. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  49319. return function viteServeRawFsMiddleware(req, res, next) {
  49320. const url = new URL(req.url.replace(/^\/{2,}/, '/'), 'http://example.com');
  49321. // In some cases (e.g. linked monorepos) files outside of root will
  49322. // reference assets that are also out of served root. In such cases
  49323. // the paths are rewritten to `/@fs/` prefixed paths and must be served by
  49324. // searching based from fs root.
  49325. if (url.pathname.startsWith(FS_PREFIX)) {
  49326. const pathname = decodeURI(url.pathname);
  49327. // restrict files outside of `fs.allow`
  49328. if (!ensureServingAccess(slash$1(path$o.resolve(fsPathFromId(pathname))), server, res, next)) {
  49329. return;
  49330. }
  49331. let newPathname = pathname.slice(FS_PREFIX.length);
  49332. if (isWindows$5)
  49333. newPathname = newPathname.replace(/^[A-Z]:/i, '');
  49334. url.pathname = encodeURI(newPathname);
  49335. req.url = url.href.slice(url.origin.length);
  49336. serveFromRoot(req, res, next);
  49337. }
  49338. else {
  49339. next();
  49340. }
  49341. };
  49342. }
  49343. /**
  49344. * Check if the url is allowed to be served, via the `server.fs` config.
  49345. */
  49346. function isFileServingAllowed(url, server) {
  49347. if (!server.config.server.fs.strict)
  49348. return true;
  49349. const file = fsPathFromUrl(url);
  49350. if (server._fsDenyGlob(file))
  49351. return false;
  49352. if (server.moduleGraph.safeModulesPath.has(file))
  49353. return true;
  49354. if (server.config.server.fs.allow.some((uri) => isSameFileUri(uri, file) || isParentDirectory(uri, file)))
  49355. return true;
  49356. return false;
  49357. }
  49358. function ensureServingAccess(url, server, res, next) {
  49359. if (isFileServingAllowed(url, server)) {
  49360. return true;
  49361. }
  49362. if (isFileReadable(cleanUrl(url))) {
  49363. const urlMessage = `The request url "${url}" is outside of Vite serving allow list.`;
  49364. const hintMessage = `
  49365. ${server.config.server.fs.allow.map((i) => `- ${i}`).join('\n')}
  49366. Refer to docs https://vitejs.dev/config/server-options.html#server-fs-allow for configurations and more details.`;
  49367. server.config.logger.error(urlMessage);
  49368. server.config.logger.warnOnce(hintMessage + '\n');
  49369. res.statusCode = 403;
  49370. res.write(renderRestrictedErrorHTML(urlMessage + '\n' + hintMessage));
  49371. res.end();
  49372. }
  49373. else {
  49374. // if the file doesn't exist, we shouldn't restrict this path as it can
  49375. // be an API call. Middlewares would issue a 404 if the file isn't handled
  49376. next();
  49377. }
  49378. return false;
  49379. }
  49380. function renderRestrictedErrorHTML(msg) {
  49381. // to have syntax highlighting and autocompletion in IDE
  49382. const html = String.raw;
  49383. return html `
  49384. <body>
  49385. <h1>403 Restricted</h1>
  49386. <p>${escapeHtml$2(msg).replace(/\n/g, '<br/>')}</p>
  49387. <style>
  49388. body {
  49389. padding: 1em 2em;
  49390. }
  49391. </style>
  49392. </body>
  49393. `;
  49394. }
  49395. const ERR_LOAD_URL = 'ERR_LOAD_URL';
  49396. const ERR_LOAD_PUBLIC_URL = 'ERR_LOAD_PUBLIC_URL';
  49397. const debugLoad = createDebugger('vite:load');
  49398. const debugTransform = createDebugger('vite:transform');
  49399. const debugCache$1 = createDebugger('vite:cache');
  49400. function transformRequest(url, server, options = {}) {
  49401. if (server._restartPromise && !options.ssr)
  49402. throwClosedServerError();
  49403. const cacheKey = (options.ssr ? 'ssr:' : options.html ? 'html:' : '') + url;
  49404. // This module may get invalidated while we are processing it. For example
  49405. // when a full page reload is needed after the re-processing of pre-bundled
  49406. // dependencies when a missing dep is discovered. We save the current time
  49407. // to compare it to the last invalidation performed to know if we should
  49408. // cache the result of the transformation or we should discard it as stale.
  49409. //
  49410. // A module can be invalidated due to:
  49411. // 1. A full reload because of pre-bundling newly discovered deps
  49412. // 2. A full reload after a config change
  49413. // 3. The file that generated the module changed
  49414. // 4. Invalidation for a virtual module
  49415. //
  49416. // For 1 and 2, a new request for this module will be issued after
  49417. // the invalidation as part of the browser reloading the page. For 3 and 4
  49418. // there may not be a new request right away because of HMR handling.
  49419. // In all cases, the next time this module is requested, it should be
  49420. // re-processed.
  49421. //
  49422. // We save the timestamp when we start processing and compare it with the
  49423. // last time this module is invalidated
  49424. const timestamp = Date.now();
  49425. const pending = server._pendingRequests.get(cacheKey);
  49426. if (pending) {
  49427. return server.moduleGraph
  49428. .getModuleByUrl(removeTimestampQuery(url), options.ssr)
  49429. .then((module) => {
  49430. if (!module || pending.timestamp > module.lastInvalidationTimestamp) {
  49431. // The pending request is still valid, we can safely reuse its result
  49432. return pending.request;
  49433. }
  49434. else {
  49435. // Request 1 for module A (pending.timestamp)
  49436. // Invalidate module A (module.lastInvalidationTimestamp)
  49437. // Request 2 for module A (timestamp)
  49438. // First request has been invalidated, abort it to clear the cache,
  49439. // then perform a new doTransform.
  49440. pending.abort();
  49441. return transformRequest(url, server, options);
  49442. }
  49443. });
  49444. }
  49445. const request = doTransform(url, server, options, timestamp);
  49446. // Avoid clearing the cache of future requests if aborted
  49447. let cleared = false;
  49448. const clearCache = () => {
  49449. if (!cleared) {
  49450. server._pendingRequests.delete(cacheKey);
  49451. cleared = true;
  49452. }
  49453. };
  49454. // Cache the request and clear it once processing is done
  49455. server._pendingRequests.set(cacheKey, {
  49456. request,
  49457. timestamp,
  49458. abort: clearCache,
  49459. });
  49460. return request.finally(clearCache);
  49461. }
  49462. async function doTransform(url, server, options, timestamp) {
  49463. url = removeTimestampQuery(url);
  49464. const { config, pluginContainer } = server;
  49465. const ssr = !!options.ssr;
  49466. if (ssr && isDepsOptimizerEnabled(config, true)) {
  49467. await initDevSsrDepsOptimizer(config, server);
  49468. }
  49469. let module = await server.moduleGraph.getModuleByUrl(url, ssr);
  49470. if (module) {
  49471. // try use cache from url
  49472. const cached = await getCachedTransformResult(url, module, server, ssr, timestamp);
  49473. if (cached)
  49474. return cached;
  49475. }
  49476. const resolved = module
  49477. ? undefined
  49478. : (await pluginContainer.resolveId(url, undefined, { ssr })) ?? undefined;
  49479. // resolve
  49480. const id = module?.id ?? resolved?.id ?? url;
  49481. module ??= server.moduleGraph.getModuleById(id);
  49482. if (module) {
  49483. // if a different url maps to an existing loaded id, make sure we relate this url to the id
  49484. await server.moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved);
  49485. // try use cache from id
  49486. const cached = await getCachedTransformResult(url, module, server, ssr, timestamp);
  49487. if (cached)
  49488. return cached;
  49489. }
  49490. const result = loadAndTransform(id, url, server, options, timestamp, module, resolved);
  49491. if (!ssr) {
  49492. // Only register client requests, server.waitForRequestsIdle should
  49493. // have been called server.waitForClientRequestsIdle. We can rename
  49494. // it as part of the environment API work
  49495. const depsOptimizer = getDepsOptimizer(config, ssr);
  49496. if (!depsOptimizer?.isOptimizedDepFile(id)) {
  49497. server._registerRequestProcessing(id, () => result);
  49498. }
  49499. }
  49500. return result;
  49501. }
  49502. async function getCachedTransformResult(url, module, server, ssr, timestamp) {
  49503. const prettyUrl = debugCache$1 ? prettifyUrl(url, server.config.root) : '';
  49504. // tries to handle soft invalidation of the module if available,
  49505. // returns a boolean true is successful, or false if no handling is needed
  49506. const softInvalidatedTransformResult = module &&
  49507. (await handleModuleSoftInvalidation(module, ssr, timestamp, server));
  49508. if (softInvalidatedTransformResult) {
  49509. debugCache$1?.(`[memory-hmr] ${prettyUrl}`);
  49510. return softInvalidatedTransformResult;
  49511. }
  49512. // check if we have a fresh cache
  49513. const cached = module && (ssr ? module.ssrTransformResult : module.transformResult);
  49514. if (cached) {
  49515. debugCache$1?.(`[memory] ${prettyUrl}`);
  49516. return cached;
  49517. }
  49518. }
  49519. async function loadAndTransform(id, url, server, options, timestamp, mod, resolved) {
  49520. const { config, pluginContainer, moduleGraph } = server;
  49521. const { logger } = config;
  49522. const prettyUrl = debugLoad || debugTransform ? prettifyUrl(url, config.root) : '';
  49523. const ssr = !!options.ssr;
  49524. const file = cleanUrl(id);
  49525. let code = null;
  49526. let map = null;
  49527. // load
  49528. const loadStart = debugLoad ? performance.now() : 0;
  49529. const loadResult = await pluginContainer.load(id, { ssr });
  49530. if (loadResult == null) {
  49531. // if this is an html request and there is no load result, skip ahead to
  49532. // SPA fallback.
  49533. if (options.html && !id.endsWith('.html')) {
  49534. return null;
  49535. }
  49536. // try fallback loading it from fs as string
  49537. // if the file is a binary, there should be a plugin that already loaded it
  49538. // as string
  49539. // only try the fallback if access is allowed, skip for out of root url
  49540. // like /service-worker.js or /api/users
  49541. if (options.ssr || isFileServingAllowed(file, server)) {
  49542. try {
  49543. code = await fsp.readFile(file, 'utf-8');
  49544. debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`);
  49545. }
  49546. catch (e) {
  49547. if (e.code !== 'ENOENT') {
  49548. if (e.code === 'EISDIR') {
  49549. e.message = `${e.message} ${file}`;
  49550. }
  49551. throw e;
  49552. }
  49553. }
  49554. if (code != null) {
  49555. ensureWatchedFile(server.watcher, file, config.root);
  49556. }
  49557. }
  49558. if (code) {
  49559. try {
  49560. const extracted = await extractSourcemapFromFile(code, file);
  49561. if (extracted) {
  49562. code = extracted.code;
  49563. map = extracted.map;
  49564. }
  49565. }
  49566. catch (e) {
  49567. logger.warn(`Failed to load source map for ${file}.\n${e}`, {
  49568. timestamp: true,
  49569. });
  49570. }
  49571. }
  49572. }
  49573. else {
  49574. debugLoad?.(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`);
  49575. if (isObject$1(loadResult)) {
  49576. code = loadResult.code;
  49577. map = loadResult.map;
  49578. }
  49579. else {
  49580. code = loadResult;
  49581. }
  49582. }
  49583. if (code == null) {
  49584. const isPublicFile = checkPublicFile(url, config);
  49585. let publicDirName = path$o.relative(config.root, config.publicDir);
  49586. if (publicDirName[0] !== '.')
  49587. publicDirName = '/' + publicDirName;
  49588. const msg = isPublicFile
  49589. ? `This file is in ${publicDirName} and will be copied as-is during ` +
  49590. `build without going through the plugin transforms, and therefore ` +
  49591. `should not be imported from source code. It can only be referenced ` +
  49592. `via HTML tags.`
  49593. : `Does the file exist?`;
  49594. const importerMod = server.moduleGraph.idToModuleMap
  49595. .get(id)
  49596. ?.importers.values()
  49597. .next().value;
  49598. const importer = importerMod?.file || importerMod?.url;
  49599. const err = new Error(`Failed to load url ${url} (resolved id: ${id})${importer ? ` in ${importer}` : ''}. ${msg}`);
  49600. err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL;
  49601. throw err;
  49602. }
  49603. if (server._restartPromise && !ssr)
  49604. throwClosedServerError();
  49605. // ensure module in graph after successful load
  49606. mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved);
  49607. // transform
  49608. const transformStart = debugTransform ? performance.now() : 0;
  49609. const transformResult = await pluginContainer.transform(code, id, {
  49610. inMap: map,
  49611. ssr,
  49612. });
  49613. const originalCode = code;
  49614. if (transformResult == null ||
  49615. (isObject$1(transformResult) && transformResult.code == null)) {
  49616. // no transform applied, keep code as-is
  49617. debugTransform?.(timeFrom(transformStart) + colors$1.dim(` [skipped] ${prettyUrl}`));
  49618. }
  49619. else {
  49620. debugTransform?.(`${timeFrom(transformStart)} ${prettyUrl}`);
  49621. code = transformResult.code;
  49622. map = transformResult.map;
  49623. }
  49624. let normalizedMap;
  49625. if (typeof map === 'string') {
  49626. normalizedMap = JSON.parse(map);
  49627. }
  49628. else if (map) {
  49629. normalizedMap = map;
  49630. }
  49631. else {
  49632. normalizedMap = null;
  49633. }
  49634. if (normalizedMap && 'version' in normalizedMap && mod.file) {
  49635. if (normalizedMap.mappings) {
  49636. await injectSourcesContent(normalizedMap, mod.file, logger);
  49637. }
  49638. const sourcemapPath = `${mod.file}.map`;
  49639. applySourcemapIgnoreList(normalizedMap, sourcemapPath, config.server.sourcemapIgnoreList, logger);
  49640. if (path$o.isAbsolute(mod.file)) {
  49641. let modDirname;
  49642. for (let sourcesIndex = 0; sourcesIndex < normalizedMap.sources.length; ++sourcesIndex) {
  49643. const sourcePath = normalizedMap.sources[sourcesIndex];
  49644. if (sourcePath) {
  49645. // Rewrite sources to relative paths to give debuggers the chance
  49646. // to resolve and display them in a meaningful way (rather than
  49647. // with absolute paths).
  49648. if (path$o.isAbsolute(sourcePath)) {
  49649. modDirname ??= path$o.dirname(mod.file);
  49650. normalizedMap.sources[sourcesIndex] = path$o.relative(modDirname, sourcePath);
  49651. }
  49652. }
  49653. }
  49654. }
  49655. }
  49656. if (server._restartPromise && !ssr)
  49657. throwClosedServerError();
  49658. const result = ssr && !server.config.experimental.skipSsrTransform
  49659. ? await server.ssrTransform(code, normalizedMap, url, originalCode)
  49660. : ({
  49661. code,
  49662. map: normalizedMap,
  49663. etag: getEtag(code, { weak: true }),
  49664. });
  49665. // Only cache the result if the module wasn't invalidated while it was
  49666. // being processed, so it is re-processed next time if it is stale
  49667. if (timestamp > mod.lastInvalidationTimestamp)
  49668. moduleGraph.updateModuleTransformResult(mod, result, ssr);
  49669. return result;
  49670. }
  49671. /**
  49672. * When a module is soft-invalidated, we can preserve its previous `transformResult` and
  49673. * return similar code to before:
  49674. *
  49675. * - Client: We need to transform the import specifiers with new timestamps
  49676. * - SSR: We don't need to change anything as `ssrLoadModule` controls it
  49677. */
  49678. async function handleModuleSoftInvalidation(mod, ssr, timestamp, server) {
  49679. const transformResult = ssr ? mod.ssrInvalidationState : mod.invalidationState;
  49680. // Reset invalidation state
  49681. if (ssr)
  49682. mod.ssrInvalidationState = undefined;
  49683. else
  49684. mod.invalidationState = undefined;
  49685. // Skip if not soft-invalidated
  49686. if (!transformResult || transformResult === 'HARD_INVALIDATED')
  49687. return;
  49688. if (ssr ? mod.ssrTransformResult : mod.transformResult) {
  49689. throw new Error(`Internal server error: Soft-invalidated module "${mod.url}" should not have existing transform result`);
  49690. }
  49691. let result;
  49692. // For SSR soft-invalidation, no transformation is needed
  49693. if (ssr) {
  49694. result = transformResult;
  49695. }
  49696. // For client soft-invalidation, we need to transform each imports with new timestamps if available
  49697. else {
  49698. await init;
  49699. const source = transformResult.code;
  49700. const s = new MagicString(source);
  49701. const [imports] = parse$e(source, mod.id || undefined);
  49702. for (const imp of imports) {
  49703. let rawUrl = source.slice(imp.s, imp.e);
  49704. if (rawUrl === 'import.meta')
  49705. continue;
  49706. const hasQuotes = rawUrl[0] === '"' || rawUrl[0] === "'";
  49707. if (hasQuotes) {
  49708. rawUrl = rawUrl.slice(1, -1);
  49709. }
  49710. const urlWithoutTimestamp = removeTimestampQuery(rawUrl);
  49711. // hmrUrl must be derived the same way as importAnalysis
  49712. const hmrUrl = unwrapId$1(stripBase(removeImportQuery(urlWithoutTimestamp), server.config.base));
  49713. for (const importedMod of mod.clientImportedModules) {
  49714. if (importedMod.url !== hmrUrl)
  49715. continue;
  49716. if (importedMod.lastHMRTimestamp > 0) {
  49717. const replacedUrl = injectQuery(urlWithoutTimestamp, `t=${importedMod.lastHMRTimestamp}`);
  49718. const start = hasQuotes ? imp.s + 1 : imp.s;
  49719. const end = hasQuotes ? imp.e - 1 : imp.e;
  49720. s.overwrite(start, end, replacedUrl);
  49721. }
  49722. if (imp.d === -1 && server.config.server.preTransformRequests) {
  49723. // pre-transform known direct imports
  49724. server.warmupRequest(hmrUrl, { ssr });
  49725. }
  49726. break;
  49727. }
  49728. }
  49729. // Update `transformResult` with new code. We don't have to update the sourcemap
  49730. // as the timestamp changes doesn't affect the code lines (stable).
  49731. const code = s.toString();
  49732. result = {
  49733. ...transformResult,
  49734. code,
  49735. etag: getEtag(code, { weak: true }),
  49736. };
  49737. }
  49738. // Only cache the result if the module wasn't invalidated while it was
  49739. // being processed, so it is re-processed next time if it is stale
  49740. if (timestamp > mod.lastInvalidationTimestamp)
  49741. server.moduleGraph.updateModuleTransformResult(mod, result, ssr);
  49742. return result;
  49743. }
  49744. /**
  49745. * Vite converts `import { } from 'foo'` to `const _ = __vite_ssr_import__('foo')`.
  49746. * Top-level imports and dynamic imports work slightly differently in Node.js.
  49747. * This function normalizes the differences so it matches prod behaviour.
  49748. */
  49749. function analyzeImportedModDifference(mod, rawId, moduleType, metadata) {
  49750. // No normalization needed if the user already dynamic imports this module
  49751. if (metadata?.isDynamicImport)
  49752. return;
  49753. // If file path is ESM, everything should be fine
  49754. if (moduleType === 'module')
  49755. return;
  49756. // For non-ESM, named imports is done via static analysis with cjs-module-lexer in Node.js.
  49757. // If the user named imports a specifier that can't be analyzed, error.
  49758. if (metadata?.importedNames?.length) {
  49759. const missingBindings = metadata.importedNames.filter((s) => !(s in mod));
  49760. if (missingBindings.length) {
  49761. const lastBinding = missingBindings[missingBindings.length - 1];
  49762. // Copied from Node.js
  49763. throw new SyntaxError(`\
  49764. [vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
  49765. CommonJS modules can always be imported via the default export, for example using:
  49766. import pkg from '${rawId}';
  49767. const {${missingBindings.join(', ')}} = pkg;
  49768. `);
  49769. }
  49770. }
  49771. }
  49772. /**
  49773. * Guard invalid named exports only, similar to how Node.js errors for top-level imports.
  49774. * But since we transform as dynamic imports, we need to emulate the error manually.
  49775. */
  49776. function proxyGuardOnlyEsm(mod, rawId, metadata) {
  49777. // If the module doesn't import anything explicitly, e.g. `import 'foo'` or
  49778. // `import * as foo from 'foo'`, we can skip the proxy guard.
  49779. if (!metadata?.importedNames?.length)
  49780. return mod;
  49781. return new Proxy(mod, {
  49782. get(mod, prop) {
  49783. if (prop !== 'then' && !(prop in mod)) {
  49784. throw new SyntaxError(`[vite] The requested module '${rawId}' does not provide an export named '${prop.toString()}'`);
  49785. }
  49786. return mod[prop];
  49787. },
  49788. });
  49789. }
  49790. /**
  49791. * @param {import('estree').Node} param
  49792. * @returns {string[]}
  49793. */
  49794. function extract_names(param) {
  49795. return extract_identifiers(param).map((node) => node.name);
  49796. }
  49797. /**
  49798. * @param {import('estree').Node} param
  49799. * @param {import('estree').Identifier[]} nodes
  49800. * @returns {import('estree').Identifier[]}
  49801. */
  49802. function extract_identifiers(param, nodes = []) {
  49803. switch (param.type) {
  49804. case 'Identifier':
  49805. nodes.push(param);
  49806. break;
  49807. case 'MemberExpression':
  49808. let object = param;
  49809. while (object.type === 'MemberExpression') {
  49810. object = /** @type {any} */ (object.object);
  49811. }
  49812. nodes.push(/** @type {any} */ (object));
  49813. break;
  49814. case 'ObjectPattern':
  49815. for (const prop of param.properties) {
  49816. if (prop.type === 'RestElement') {
  49817. extract_identifiers(prop.argument, nodes);
  49818. } else {
  49819. extract_identifiers(prop.value, nodes);
  49820. }
  49821. }
  49822. break;
  49823. case 'ArrayPattern':
  49824. for (const element of param.elements) {
  49825. if (element) extract_identifiers(element, nodes);
  49826. }
  49827. break;
  49828. case 'RestElement':
  49829. extract_identifiers(param.argument, nodes);
  49830. break;
  49831. case 'AssignmentPattern':
  49832. extract_identifiers(param.left, nodes);
  49833. break;
  49834. }
  49835. return nodes;
  49836. }
  49837. /**
  49838. * @typedef { import('estree').Node} Node
  49839. * @typedef {{
  49840. * skip: () => void;
  49841. * remove: () => void;
  49842. * replace: (node: Node) => void;
  49843. * }} WalkerContext
  49844. */
  49845. class WalkerBase {
  49846. constructor() {
  49847. /** @type {boolean} */
  49848. this.should_skip = false;
  49849. /** @type {boolean} */
  49850. this.should_remove = false;
  49851. /** @type {Node | null} */
  49852. this.replacement = null;
  49853. /** @type {WalkerContext} */
  49854. this.context = {
  49855. skip: () => (this.should_skip = true),
  49856. remove: () => (this.should_remove = true),
  49857. replace: (node) => (this.replacement = node)
  49858. };
  49859. }
  49860. /**
  49861. * @template {Node} Parent
  49862. * @param {Parent | null | undefined} parent
  49863. * @param {keyof Parent | null | undefined} prop
  49864. * @param {number | null | undefined} index
  49865. * @param {Node} node
  49866. */
  49867. replace(parent, prop, index, node) {
  49868. if (parent && prop) {
  49869. if (index != null) {
  49870. /** @type {Array<Node>} */ (parent[prop])[index] = node;
  49871. } else {
  49872. /** @type {Node} */ (parent[prop]) = node;
  49873. }
  49874. }
  49875. }
  49876. /**
  49877. * @template {Node} Parent
  49878. * @param {Parent | null | undefined} parent
  49879. * @param {keyof Parent | null | undefined} prop
  49880. * @param {number | null | undefined} index
  49881. */
  49882. remove(parent, prop, index) {
  49883. if (parent && prop) {
  49884. if (index !== null && index !== undefined) {
  49885. /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
  49886. } else {
  49887. delete parent[prop];
  49888. }
  49889. }
  49890. }
  49891. }
  49892. /**
  49893. * @typedef { import('estree').Node} Node
  49894. * @typedef { import('./walker.js').WalkerContext} WalkerContext
  49895. * @typedef {(
  49896. * this: WalkerContext,
  49897. * node: Node,
  49898. * parent: Node | null,
  49899. * key: string | number | symbol | null | undefined,
  49900. * index: number | null | undefined
  49901. * ) => void} SyncHandler
  49902. */
  49903. class SyncWalker extends WalkerBase {
  49904. /**
  49905. *
  49906. * @param {SyncHandler} [enter]
  49907. * @param {SyncHandler} [leave]
  49908. */
  49909. constructor(enter, leave) {
  49910. super();
  49911. /** @type {boolean} */
  49912. this.should_skip = false;
  49913. /** @type {boolean} */
  49914. this.should_remove = false;
  49915. /** @type {Node | null} */
  49916. this.replacement = null;
  49917. /** @type {WalkerContext} */
  49918. this.context = {
  49919. skip: () => (this.should_skip = true),
  49920. remove: () => (this.should_remove = true),
  49921. replace: (node) => (this.replacement = node)
  49922. };
  49923. /** @type {SyncHandler | undefined} */
  49924. this.enter = enter;
  49925. /** @type {SyncHandler | undefined} */
  49926. this.leave = leave;
  49927. }
  49928. /**
  49929. * @template {Node} Parent
  49930. * @param {Node} node
  49931. * @param {Parent | null} parent
  49932. * @param {keyof Parent} [prop]
  49933. * @param {number | null} [index]
  49934. * @returns {Node | null}
  49935. */
  49936. visit(node, parent, prop, index) {
  49937. if (node) {
  49938. if (this.enter) {
  49939. const _should_skip = this.should_skip;
  49940. const _should_remove = this.should_remove;
  49941. const _replacement = this.replacement;
  49942. this.should_skip = false;
  49943. this.should_remove = false;
  49944. this.replacement = null;
  49945. this.enter.call(this.context, node, parent, prop, index);
  49946. if (this.replacement) {
  49947. node = this.replacement;
  49948. this.replace(parent, prop, index, node);
  49949. }
  49950. if (this.should_remove) {
  49951. this.remove(parent, prop, index);
  49952. }
  49953. const skipped = this.should_skip;
  49954. const removed = this.should_remove;
  49955. this.should_skip = _should_skip;
  49956. this.should_remove = _should_remove;
  49957. this.replacement = _replacement;
  49958. if (skipped) return node;
  49959. if (removed) return null;
  49960. }
  49961. /** @type {keyof Node} */
  49962. let key;
  49963. for (key in node) {
  49964. /** @type {unknown} */
  49965. const value = node[key];
  49966. if (value && typeof value === 'object') {
  49967. if (Array.isArray(value)) {
  49968. const nodes = /** @type {Array<unknown>} */ (value);
  49969. for (let i = 0; i < nodes.length; i += 1) {
  49970. const item = nodes[i];
  49971. if (isNode(item)) {
  49972. if (!this.visit(item, node, key, i)) {
  49973. // removed
  49974. i--;
  49975. }
  49976. }
  49977. }
  49978. } else if (isNode(value)) {
  49979. this.visit(value, node, key, null);
  49980. }
  49981. }
  49982. }
  49983. if (this.leave) {
  49984. const _replacement = this.replacement;
  49985. const _should_remove = this.should_remove;
  49986. this.replacement = null;
  49987. this.should_remove = false;
  49988. this.leave.call(this.context, node, parent, prop, index);
  49989. if (this.replacement) {
  49990. node = this.replacement;
  49991. this.replace(parent, prop, index, node);
  49992. }
  49993. if (this.should_remove) {
  49994. this.remove(parent, prop, index);
  49995. }
  49996. const removed = this.should_remove;
  49997. this.replacement = _replacement;
  49998. this.should_remove = _should_remove;
  49999. if (removed) return null;
  50000. }
  50001. }
  50002. return node;
  50003. }
  50004. }
  50005. /**
  50006. * Ducktype a node.
  50007. *
  50008. * @param {unknown} value
  50009. * @returns {value is Node}
  50010. */
  50011. function isNode(value) {
  50012. return (
  50013. value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
  50014. );
  50015. }
  50016. /**
  50017. * @typedef {import('estree').Node} Node
  50018. * @typedef {import('./sync.js').SyncHandler} SyncHandler
  50019. * @typedef {import('./async.js').AsyncHandler} AsyncHandler
  50020. */
  50021. /**
  50022. * @param {Node} ast
  50023. * @param {{
  50024. * enter?: SyncHandler
  50025. * leave?: SyncHandler
  50026. * }} walker
  50027. * @returns {Node | null}
  50028. */
  50029. function walk$1(ast, { enter, leave }) {
  50030. const instance = new SyncWalker(enter, leave);
  50031. return instance.visit(ast, null);
  50032. }
  50033. const ssrModuleExportsKey = `__vite_ssr_exports__`;
  50034. const ssrImportKey = `__vite_ssr_import__`;
  50035. const ssrDynamicImportKey = `__vite_ssr_dynamic_import__`;
  50036. const ssrExportAllKey = `__vite_ssr_exportAll__`;
  50037. const ssrImportMetaKey = `__vite_ssr_import_meta__`;
  50038. const hashbangRE = /^#!.*\n/;
  50039. async function ssrTransform(code, inMap, url, originalCode, options) {
  50040. if (options?.json?.stringify && isJSONRequest(url)) {
  50041. return ssrTransformJSON(code, inMap);
  50042. }
  50043. return ssrTransformScript(code, inMap, url, originalCode);
  50044. }
  50045. async function ssrTransformJSON(code, inMap) {
  50046. return {
  50047. code: code.replace('export default', `${ssrModuleExportsKey}.default =`),
  50048. map: inMap,
  50049. deps: [],
  50050. dynamicDeps: [],
  50051. };
  50052. }
  50053. async function ssrTransformScript(code, inMap, url, originalCode) {
  50054. const s = new MagicString(code);
  50055. let ast;
  50056. try {
  50057. ast = await parseAstAsync(code);
  50058. }
  50059. catch (err) {
  50060. if (!err.loc || !err.loc.line)
  50061. throw err;
  50062. const line = err.loc.line;
  50063. throw new Error(`Parse failure: ${err.message}\nAt file: ${url}\nContents of line ${line}: ${code.split('\n')[line - 1]}`);
  50064. }
  50065. let uid = 0;
  50066. const deps = new Set();
  50067. const dynamicDeps = new Set();
  50068. const idToImportMap = new Map();
  50069. const declaredConst = new Set();
  50070. // hoist at the start of the file, after the hashbang
  50071. const hoistIndex = code.match(hashbangRE)?.[0].length ?? 0;
  50072. function defineImport(index, source, metadata) {
  50073. deps.add(source);
  50074. const importId = `__vite_ssr_import_${uid++}__`;
  50075. // Reduce metadata to undefined if it's all default values
  50076. if (metadata &&
  50077. (metadata.importedNames == null || metadata.importedNames.length === 0)) {
  50078. metadata = undefined;
  50079. }
  50080. const metadataStr = metadata ? `, ${JSON.stringify(metadata)}` : '';
  50081. // There will be an error if the module is called before it is imported,
  50082. // so the module import statement is hoisted to the top
  50083. s.appendLeft(index, `const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)}${metadataStr});\n`);
  50084. return importId;
  50085. }
  50086. function defineExport(position, name, local = name) {
  50087. s.appendLeft(position, `\nObject.defineProperty(${ssrModuleExportsKey}, "${name}", ` +
  50088. `{ enumerable: true, configurable: true, get(){ return ${local} }});`);
  50089. }
  50090. // 1. check all import statements and record id -> importName map
  50091. for (const node of ast.body) {
  50092. // import foo from 'foo' --> foo -> __import_foo__.default
  50093. // import { baz } from 'foo' --> baz -> __import_foo__.baz
  50094. // import * as ok from 'foo' --> ok -> __import_foo__
  50095. if (node.type === 'ImportDeclaration') {
  50096. const importId = defineImport(hoistIndex, node.source.value, {
  50097. importedNames: node.specifiers
  50098. .map((s) => {
  50099. if (s.type === 'ImportSpecifier')
  50100. return s.imported.name;
  50101. else if (s.type === 'ImportDefaultSpecifier')
  50102. return 'default';
  50103. })
  50104. .filter(isDefined),
  50105. });
  50106. s.remove(node.start, node.end);
  50107. for (const spec of node.specifiers) {
  50108. if (spec.type === 'ImportSpecifier') {
  50109. idToImportMap.set(spec.local.name, `${importId}.${spec.imported.name}`);
  50110. }
  50111. else if (spec.type === 'ImportDefaultSpecifier') {
  50112. idToImportMap.set(spec.local.name, `${importId}.default`);
  50113. }
  50114. else {
  50115. // namespace specifier
  50116. idToImportMap.set(spec.local.name, importId);
  50117. }
  50118. }
  50119. }
  50120. }
  50121. // 2. check all export statements and define exports
  50122. for (const node of ast.body) {
  50123. // named exports
  50124. if (node.type === 'ExportNamedDeclaration') {
  50125. if (node.declaration) {
  50126. if (node.declaration.type === 'FunctionDeclaration' ||
  50127. node.declaration.type === 'ClassDeclaration') {
  50128. // export function foo() {}
  50129. defineExport(node.end, node.declaration.id.name);
  50130. }
  50131. else {
  50132. // export const foo = 1, bar = 2
  50133. for (const declaration of node.declaration.declarations) {
  50134. const names = extract_names(declaration.id);
  50135. for (const name of names) {
  50136. defineExport(node.end, name);
  50137. }
  50138. }
  50139. }
  50140. s.remove(node.start, node.declaration.start);
  50141. }
  50142. else {
  50143. s.remove(node.start, node.end);
  50144. if (node.source) {
  50145. // export { foo, bar } from './foo'
  50146. const importId = defineImport(node.start, node.source.value, {
  50147. importedNames: node.specifiers.map((s) => s.local.name),
  50148. });
  50149. for (const spec of node.specifiers) {
  50150. defineExport(node.start, spec.exported.name, `${importId}.${spec.local.name}`);
  50151. }
  50152. }
  50153. else {
  50154. // export { foo, bar }
  50155. for (const spec of node.specifiers) {
  50156. const local = spec.local.name;
  50157. const binding = idToImportMap.get(local);
  50158. defineExport(node.end, spec.exported.name, binding || local);
  50159. }
  50160. }
  50161. }
  50162. }
  50163. // default export
  50164. if (node.type === 'ExportDefaultDeclaration') {
  50165. const expressionTypes = ['FunctionExpression', 'ClassExpression'];
  50166. if ('id' in node.declaration &&
  50167. node.declaration.id &&
  50168. !expressionTypes.includes(node.declaration.type)) {
  50169. // named hoistable/class exports
  50170. // export default function foo() {}
  50171. // export default class A {}
  50172. const { name } = node.declaration.id;
  50173. s.remove(node.start, node.start + 15 /* 'export default '.length */);
  50174. s.append(`\nObject.defineProperty(${ssrModuleExportsKey}, "default", ` +
  50175. `{ enumerable: true, configurable: true, value: ${name} });`);
  50176. }
  50177. else {
  50178. // anonymous default exports
  50179. s.update(node.start, node.start + 14 /* 'export default'.length */, `${ssrModuleExportsKey}.default =`);
  50180. }
  50181. }
  50182. // export * from './foo'
  50183. if (node.type === 'ExportAllDeclaration') {
  50184. s.remove(node.start, node.end);
  50185. const importId = defineImport(node.start, node.source.value);
  50186. if (node.exported) {
  50187. defineExport(node.start, node.exported.name, `${importId}`);
  50188. }
  50189. else {
  50190. s.appendLeft(node.start, `${ssrExportAllKey}(${importId});\n`);
  50191. }
  50192. }
  50193. }
  50194. // 3. convert references to import bindings & import.meta references
  50195. walk(ast, {
  50196. onIdentifier(id, parent, parentStack) {
  50197. const grandparent = parentStack[1];
  50198. const binding = idToImportMap.get(id.name);
  50199. if (!binding) {
  50200. return;
  50201. }
  50202. if (isStaticProperty(parent) && parent.shorthand) {
  50203. // let binding used in a property shorthand
  50204. // { foo } -> { foo: __import_x__.foo }
  50205. // skip for destructuring patterns
  50206. if (!isNodeInPattern(parent) ||
  50207. isInDestructuringAssignment(parent, parentStack)) {
  50208. s.appendLeft(id.end, `: ${binding}`);
  50209. }
  50210. }
  50211. else if ((parent.type === 'PropertyDefinition' &&
  50212. grandparent?.type === 'ClassBody') ||
  50213. (parent.type === 'ClassDeclaration' && id === parent.superClass)) {
  50214. if (!declaredConst.has(id.name)) {
  50215. declaredConst.add(id.name);
  50216. // locate the top-most node containing the class declaration
  50217. const topNode = parentStack[parentStack.length - 2];
  50218. s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`);
  50219. }
  50220. }
  50221. else if (
  50222. // don't transform class name identifier
  50223. !(parent.type === 'ClassExpression' && id === parent.id)) {
  50224. s.update(id.start, id.end, binding);
  50225. }
  50226. },
  50227. onImportMeta(node) {
  50228. s.update(node.start, node.end, ssrImportMetaKey);
  50229. },
  50230. onDynamicImport(node) {
  50231. s.update(node.start, node.start + 6, ssrDynamicImportKey);
  50232. if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
  50233. dynamicDeps.add(node.source.value);
  50234. }
  50235. },
  50236. });
  50237. let map = s.generateMap({ hires: 'boundary' });
  50238. if (inMap &&
  50239. inMap.mappings &&
  50240. 'sources' in inMap &&
  50241. inMap.sources.length > 0) {
  50242. map = combineSourcemaps(url, [
  50243. {
  50244. ...map,
  50245. sources: inMap.sources,
  50246. sourcesContent: inMap.sourcesContent,
  50247. },
  50248. inMap,
  50249. ]);
  50250. }
  50251. else {
  50252. map.sources = [path$o.basename(url)];
  50253. // needs to use originalCode instead of code
  50254. // because code might be already transformed even if map is null
  50255. map.sourcesContent = [originalCode];
  50256. }
  50257. return {
  50258. code: s.toString(),
  50259. map,
  50260. deps: [...deps],
  50261. dynamicDeps: [...dynamicDeps],
  50262. };
  50263. }
  50264. const isNodeInPatternWeakSet = new WeakSet();
  50265. const setIsNodeInPattern = (node) => isNodeInPatternWeakSet.add(node);
  50266. const isNodeInPattern = (node) => isNodeInPatternWeakSet.has(node);
  50267. /**
  50268. * Same logic from \@vue/compiler-core & \@vue/compiler-sfc
  50269. * Except this is using acorn AST
  50270. */
  50271. function walk(root, { onIdentifier, onImportMeta, onDynamicImport }) {
  50272. const parentStack = [];
  50273. const varKindStack = [];
  50274. const scopeMap = new WeakMap();
  50275. const identifiers = [];
  50276. const setScope = (node, name) => {
  50277. let scopeIds = scopeMap.get(node);
  50278. if (scopeIds && scopeIds.has(name)) {
  50279. return;
  50280. }
  50281. if (!scopeIds) {
  50282. scopeIds = new Set();
  50283. scopeMap.set(node, scopeIds);
  50284. }
  50285. scopeIds.add(name);
  50286. };
  50287. function isInScope(name, parents) {
  50288. return parents.some((node) => node && scopeMap.get(node)?.has(name));
  50289. }
  50290. function handlePattern(p, parentScope) {
  50291. if (p.type === 'Identifier') {
  50292. setScope(parentScope, p.name);
  50293. }
  50294. else if (p.type === 'RestElement') {
  50295. handlePattern(p.argument, parentScope);
  50296. }
  50297. else if (p.type === 'ObjectPattern') {
  50298. p.properties.forEach((property) => {
  50299. if (property.type === 'RestElement') {
  50300. setScope(parentScope, property.argument.name);
  50301. }
  50302. else {
  50303. handlePattern(property.value, parentScope);
  50304. }
  50305. });
  50306. }
  50307. else if (p.type === 'ArrayPattern') {
  50308. p.elements.forEach((element) => {
  50309. if (element) {
  50310. handlePattern(element, parentScope);
  50311. }
  50312. });
  50313. }
  50314. else if (p.type === 'AssignmentPattern') {
  50315. handlePattern(p.left, parentScope);
  50316. }
  50317. else {
  50318. setScope(parentScope, p.name);
  50319. }
  50320. }
  50321. walk$1(root, {
  50322. enter(node, parent) {
  50323. if (node.type === 'ImportDeclaration') {
  50324. return this.skip();
  50325. }
  50326. // track parent stack, skip for "else-if"/"else" branches as acorn nests
  50327. // the ast within "if" nodes instead of flattening them
  50328. if (parent &&
  50329. !(parent.type === 'IfStatement' && node === parent.alternate)) {
  50330. parentStack.unshift(parent);
  50331. }
  50332. // track variable declaration kind stack used by VariableDeclarator
  50333. if (node.type === 'VariableDeclaration') {
  50334. varKindStack.unshift(node.kind);
  50335. }
  50336. if (node.type === 'MetaProperty' && node.meta.name === 'import') {
  50337. onImportMeta(node);
  50338. }
  50339. else if (node.type === 'ImportExpression') {
  50340. onDynamicImport(node);
  50341. }
  50342. if (node.type === 'Identifier') {
  50343. if (!isInScope(node.name, parentStack) &&
  50344. isRefIdentifier(node, parent, parentStack)) {
  50345. // record the identifier, for DFS -> BFS
  50346. identifiers.push([node, parentStack.slice(0)]);
  50347. }
  50348. }
  50349. else if (isFunction$1(node)) {
  50350. // If it is a function declaration, it could be shadowing an import
  50351. // Add its name to the scope so it won't get replaced
  50352. if (node.type === 'FunctionDeclaration') {
  50353. const parentScope = findParentScope(parentStack);
  50354. if (parentScope) {
  50355. setScope(parentScope, node.id.name);
  50356. }
  50357. }
  50358. // walk function expressions and add its arguments to known identifiers
  50359. // so that we don't prefix them
  50360. node.params.forEach((p) => {
  50361. if (p.type === 'ObjectPattern' || p.type === 'ArrayPattern') {
  50362. handlePattern(p, node);
  50363. return;
  50364. }
  50365. walk$1(p.type === 'AssignmentPattern' ? p.left : p, {
  50366. enter(child, parent) {
  50367. // skip params default value of destructure
  50368. if (parent?.type === 'AssignmentPattern' &&
  50369. parent?.right === child) {
  50370. return this.skip();
  50371. }
  50372. if (child.type !== 'Identifier')
  50373. return;
  50374. // do not record as scope variable if is a destructuring keyword
  50375. if (isStaticPropertyKey(child, parent))
  50376. return;
  50377. // do not record if this is a default value
  50378. // assignment of a destructuring variable
  50379. if ((parent?.type === 'TemplateLiteral' &&
  50380. parent?.expressions.includes(child)) ||
  50381. (parent?.type === 'CallExpression' && parent?.callee === child)) {
  50382. return;
  50383. }
  50384. setScope(node, child.name);
  50385. },
  50386. });
  50387. });
  50388. }
  50389. else if (node.type === 'Property' && parent.type === 'ObjectPattern') {
  50390. // mark property in destructuring pattern
  50391. setIsNodeInPattern(node);
  50392. }
  50393. else if (node.type === 'VariableDeclarator') {
  50394. const parentFunction = findParentScope(parentStack, varKindStack[0] === 'var');
  50395. if (parentFunction) {
  50396. handlePattern(node.id, parentFunction);
  50397. }
  50398. }
  50399. else if (node.type === 'CatchClause' && node.param) {
  50400. handlePattern(node.param, node);
  50401. }
  50402. },
  50403. leave(node, parent) {
  50404. // untrack parent stack from above
  50405. if (parent &&
  50406. !(parent.type === 'IfStatement' && node === parent.alternate)) {
  50407. parentStack.shift();
  50408. }
  50409. if (node.type === 'VariableDeclaration') {
  50410. varKindStack.shift();
  50411. }
  50412. },
  50413. });
  50414. // emit the identifier events in BFS so the hoisted declarations
  50415. // can be captured correctly
  50416. identifiers.forEach(([node, stack]) => {
  50417. if (!isInScope(node.name, stack))
  50418. onIdentifier(node, stack[0], stack);
  50419. });
  50420. }
  50421. function isRefIdentifier(id, parent, parentStack) {
  50422. // declaration id
  50423. if (parent.type === 'CatchClause' ||
  50424. ((parent.type === 'VariableDeclarator' ||
  50425. parent.type === 'ClassDeclaration') &&
  50426. parent.id === id)) {
  50427. return false;
  50428. }
  50429. if (isFunction$1(parent)) {
  50430. // function declaration/expression id
  50431. if (parent.id === id) {
  50432. return false;
  50433. }
  50434. // params list
  50435. if (parent.params.includes(id)) {
  50436. return false;
  50437. }
  50438. }
  50439. // class method name
  50440. if (parent.type === 'MethodDefinition' && !parent.computed) {
  50441. return false;
  50442. }
  50443. // property key
  50444. if (isStaticPropertyKey(id, parent)) {
  50445. return false;
  50446. }
  50447. // object destructuring pattern
  50448. if (isNodeInPattern(parent) && parent.value === id) {
  50449. return false;
  50450. }
  50451. // non-assignment array destructuring pattern
  50452. if (parent.type === 'ArrayPattern' &&
  50453. !isInDestructuringAssignment(parent, parentStack)) {
  50454. return false;
  50455. }
  50456. // member expression property
  50457. if (parent.type === 'MemberExpression' &&
  50458. parent.property === id &&
  50459. !parent.computed) {
  50460. return false;
  50461. }
  50462. if (parent.type === 'ExportSpecifier') {
  50463. return false;
  50464. }
  50465. // is a special keyword but parsed as identifier
  50466. if (id.name === 'arguments') {
  50467. return false;
  50468. }
  50469. return true;
  50470. }
  50471. const isStaticProperty = (node) => node && node.type === 'Property' && !node.computed;
  50472. const isStaticPropertyKey = (node, parent) => isStaticProperty(parent) && parent.key === node;
  50473. const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/;
  50474. function isFunction$1(node) {
  50475. return functionNodeTypeRE.test(node.type);
  50476. }
  50477. const blockNodeTypeRE = /^BlockStatement$|^For(?:In|Of)?Statement$/;
  50478. function isBlock(node) {
  50479. return blockNodeTypeRE.test(node.type);
  50480. }
  50481. function findParentScope(parentStack, isVar = false) {
  50482. return parentStack.find(isVar ? isFunction$1 : isBlock);
  50483. }
  50484. function isInDestructuringAssignment(parent, parentStack) {
  50485. if (parent &&
  50486. (parent.type === 'Property' || parent.type === 'ArrayPattern')) {
  50487. return parentStack.some((i) => i.type === 'AssignmentExpression');
  50488. }
  50489. return false;
  50490. }
  50491. let offset;
  50492. function calculateOffsetOnce() {
  50493. if (offset !== undefined) {
  50494. return;
  50495. }
  50496. try {
  50497. new Function('throw new Error(1)')();
  50498. }
  50499. catch (e) {
  50500. // in Node 12, stack traces account for the function wrapper.
  50501. // in Node 13 and later, the function wrapper adds two lines,
  50502. // which must be subtracted to generate a valid mapping
  50503. const match = /:(\d+):\d+\)$/.exec(e.stack.split('\n')[1]);
  50504. offset = match ? +match[1] - 1 : 0;
  50505. }
  50506. }
  50507. function ssrRewriteStacktrace(stack, moduleGraph) {
  50508. calculateOffsetOnce();
  50509. return stack
  50510. .split('\n')
  50511. .map((line) => {
  50512. return line.replace(/^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/, (input, varName, id, line, column) => {
  50513. if (!id)
  50514. return input;
  50515. const mod = moduleGraph.idToModuleMap.get(id);
  50516. const rawSourceMap = mod?.ssrTransformResult?.map;
  50517. if (!rawSourceMap) {
  50518. return input;
  50519. }
  50520. const traced = new TraceMap(rawSourceMap);
  50521. const pos = originalPositionFor$1(traced, {
  50522. line: Number(line) - offset,
  50523. // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
  50524. column: Number(column) - 1,
  50525. });
  50526. if (!pos.source || pos.line == null || pos.column == null) {
  50527. return input;
  50528. }
  50529. const trimmedVarName = varName.trim();
  50530. const sourceFile = path$o.resolve(path$o.dirname(id), pos.source);
  50531. // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
  50532. const source = `${sourceFile}:${pos.line}:${pos.column + 1}`;
  50533. if (!trimmedVarName || trimmedVarName === 'eval') {
  50534. return ` at ${source}`;
  50535. }
  50536. else {
  50537. return ` at ${trimmedVarName} (${source})`;
  50538. }
  50539. });
  50540. })
  50541. .join('\n');
  50542. }
  50543. function rebindErrorStacktrace(e, stacktrace) {
  50544. const { configurable, writable } = Object.getOwnPropertyDescriptor(e, 'stack');
  50545. if (configurable) {
  50546. Object.defineProperty(e, 'stack', {
  50547. value: stacktrace,
  50548. enumerable: true,
  50549. configurable: true,
  50550. writable: true,
  50551. });
  50552. }
  50553. else if (writable) {
  50554. e.stack = stacktrace;
  50555. }
  50556. }
  50557. const rewroteStacktraces = new WeakSet();
  50558. function ssrFixStacktrace(e, moduleGraph) {
  50559. if (!e.stack)
  50560. return;
  50561. // stacktrace shouldn't be rewritten more than once
  50562. if (rewroteStacktraces.has(e))
  50563. return;
  50564. const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph);
  50565. rebindErrorStacktrace(e, stacktrace);
  50566. rewroteStacktraces.add(e);
  50567. }
  50568. const pendingModules = new Map();
  50569. const pendingImports = new Map();
  50570. const importErrors = new WeakMap();
  50571. async function ssrLoadModule(url, server, context = { global }, urlStack = [], fixStacktrace) {
  50572. url = unwrapId$1(url);
  50573. // when we instantiate multiple dependency modules in parallel, they may
  50574. // point to shared modules. We need to avoid duplicate instantiation attempts
  50575. // by register every module as pending synchronously so that all subsequent
  50576. // request to that module are simply waiting on the same promise.
  50577. const pending = pendingModules.get(url);
  50578. if (pending) {
  50579. return pending;
  50580. }
  50581. const modulePromise = instantiateModule(url, server, context, urlStack, fixStacktrace);
  50582. pendingModules.set(url, modulePromise);
  50583. modulePromise
  50584. .catch(() => {
  50585. pendingImports.delete(url);
  50586. })
  50587. .finally(() => {
  50588. pendingModules.delete(url);
  50589. });
  50590. return modulePromise;
  50591. }
  50592. async function instantiateModule(url, server, context = { global }, urlStack = [], fixStacktrace) {
  50593. const { moduleGraph } = server;
  50594. const mod = await moduleGraph.ensureEntryFromUrl(url, true);
  50595. if (mod.ssrError) {
  50596. throw mod.ssrError;
  50597. }
  50598. if (mod.ssrModule) {
  50599. return mod.ssrModule;
  50600. }
  50601. const result = mod.ssrTransformResult ||
  50602. (await transformRequest(url, server, { ssr: true }));
  50603. if (!result) {
  50604. // TODO more info? is this even necessary?
  50605. throw new Error(`failed to load module for ssr: ${url}`);
  50606. }
  50607. const ssrModule = {
  50608. [Symbol.toStringTag]: 'Module',
  50609. };
  50610. Object.defineProperty(ssrModule, '__esModule', { value: true });
  50611. // Tolerate circular imports by ensuring the module can be
  50612. // referenced before it's been instantiated.
  50613. mod.ssrModule = ssrModule;
  50614. // replace '/' with '\\' on Windows to match Node.js
  50615. const osNormalizedFilename = isWindows$5 ? path$o.resolve(mod.file) : mod.file;
  50616. const ssrImportMeta = {
  50617. dirname: path$o.dirname(osNormalizedFilename),
  50618. filename: osNormalizedFilename,
  50619. // The filesystem URL, matching native Node.js modules
  50620. url: pathToFileURL(mod.file).toString(),
  50621. };
  50622. urlStack = urlStack.concat(url);
  50623. const isCircular = (url) => urlStack.includes(url);
  50624. const { isProduction, resolve: { dedupe, preserveSymlinks }, root, ssr, } = server.config;
  50625. const overrideConditions = ssr.resolve?.externalConditions || [];
  50626. const resolveOptions = {
  50627. mainFields: ['main'],
  50628. conditions: [],
  50629. overrideConditions: [...overrideConditions, 'production', 'development'],
  50630. extensions: ['.js', '.cjs', '.json'],
  50631. dedupe,
  50632. preserveSymlinks,
  50633. isBuild: false,
  50634. isProduction,
  50635. root,
  50636. ssrConfig: ssr,
  50637. legacyProxySsrExternalModules: server.config.legacy?.proxySsrExternalModules,
  50638. packageCache: server.config.packageCache,
  50639. };
  50640. // Since dynamic imports can happen in parallel, we need to
  50641. // account for multiple pending deps and duplicate imports.
  50642. const pendingDeps = [];
  50643. const ssrImport = async (dep, metadata) => {
  50644. try {
  50645. if (dep[0] !== '.' && dep[0] !== '/') {
  50646. return await nodeImport(dep, mod.file, resolveOptions, metadata);
  50647. }
  50648. // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
  50649. dep = unwrapId$1(dep);
  50650. if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
  50651. pendingDeps.push(dep);
  50652. if (pendingDeps.length === 1) {
  50653. pendingImports.set(url, pendingDeps);
  50654. }
  50655. const mod = await ssrLoadModule(dep, server, context, urlStack, fixStacktrace);
  50656. if (pendingDeps.length === 1) {
  50657. pendingImports.delete(url);
  50658. }
  50659. else {
  50660. pendingDeps.splice(pendingDeps.indexOf(dep), 1);
  50661. }
  50662. // return local module to avoid race condition #5470
  50663. return mod;
  50664. }
  50665. return moduleGraph.urlToModuleMap.get(dep)?.ssrModule;
  50666. }
  50667. catch (err) {
  50668. // tell external error handler which mod was imported with error
  50669. importErrors.set(err, { importee: dep });
  50670. throw err;
  50671. }
  50672. };
  50673. const ssrDynamicImport = (dep) => {
  50674. // #3087 dynamic import vars is ignored at rewrite import path,
  50675. // so here need process relative path
  50676. if (dep[0] === '.') {
  50677. dep = path$o.posix.resolve(path$o.dirname(url), dep);
  50678. }
  50679. return ssrImport(dep, { isDynamicImport: true });
  50680. };
  50681. function ssrExportAll(sourceModule) {
  50682. for (const key in sourceModule) {
  50683. if (key !== 'default' && key !== '__esModule') {
  50684. Object.defineProperty(ssrModule, key, {
  50685. enumerable: true,
  50686. configurable: true,
  50687. get() {
  50688. return sourceModule[key];
  50689. },
  50690. });
  50691. }
  50692. }
  50693. }
  50694. let sourceMapSuffix = '';
  50695. if (result.map && 'version' in result.map) {
  50696. const moduleSourceMap = Object.assign({}, result.map, {
  50697. mappings: ';'.repeat(asyncFunctionDeclarationPaddingLineCount) +
  50698. result.map.mappings,
  50699. });
  50700. sourceMapSuffix = `\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(moduleSourceMap)}`;
  50701. }
  50702. try {
  50703. const initModule = new AsyncFunction(`global`, ssrModuleExportsKey, ssrImportMetaKey, ssrImportKey, ssrDynamicImportKey, ssrExportAllKey, '"use strict";' +
  50704. result.code +
  50705. `\n//# sourceURL=${mod.id}${sourceMapSuffix}`);
  50706. await initModule(context.global, ssrModule, ssrImportMeta, ssrImport, ssrDynamicImport, ssrExportAll);
  50707. }
  50708. catch (e) {
  50709. mod.ssrError = e;
  50710. const errorData = importErrors.get(e);
  50711. if (e.stack && fixStacktrace) {
  50712. ssrFixStacktrace(e, moduleGraph);
  50713. }
  50714. server.config.logger.error(colors$1.red(`Error when evaluating SSR module ${url}:` +
  50715. (errorData?.importee
  50716. ? ` failed to import "${errorData.importee}"`
  50717. : '') +
  50718. `\n|- ${e.stack}\n`), {
  50719. timestamp: true,
  50720. clear: server.config.clearScreen,
  50721. error: e,
  50722. });
  50723. throw e;
  50724. }
  50725. return Object.freeze(ssrModule);
  50726. }
  50727. // In node@12+ we can use dynamic import to load CJS and ESM
  50728. async function nodeImport(id, importer, resolveOptions, metadata) {
  50729. let url;
  50730. let filePath;
  50731. if (id.startsWith('data:') || isExternalUrl(id) || isBuiltin(id)) {
  50732. url = id;
  50733. }
  50734. else {
  50735. const resolved = tryNodeResolve(id, importer, { ...resolveOptions, tryEsmOnly: true }, false, undefined, true);
  50736. if (!resolved) {
  50737. const err = new Error(`Cannot find module '${id}' imported from '${importer}'`);
  50738. err.code = 'ERR_MODULE_NOT_FOUND';
  50739. throw err;
  50740. }
  50741. filePath = resolved.id;
  50742. url = pathToFileURL(resolved.id).toString();
  50743. }
  50744. const mod = await import(url);
  50745. if (resolveOptions.legacyProxySsrExternalModules) {
  50746. return proxyESM(mod);
  50747. }
  50748. else if (filePath) {
  50749. analyzeImportedModDifference(mod, id, isFilePathESM(filePath, resolveOptions.packageCache)
  50750. ? 'module'
  50751. : undefined, metadata);
  50752. return proxyGuardOnlyEsm(mod, id);
  50753. }
  50754. else {
  50755. return mod;
  50756. }
  50757. }
  50758. // rollup-style default import interop for cjs
  50759. function proxyESM(mod) {
  50760. // This is the only sensible option when the exports object is a primitive
  50761. if (isPrimitive(mod))
  50762. return { default: mod };
  50763. let defaultExport = 'default' in mod ? mod.default : mod;
  50764. if (!isPrimitive(defaultExport) && '__esModule' in defaultExport) {
  50765. mod = defaultExport;
  50766. if ('default' in defaultExport) {
  50767. defaultExport = defaultExport.default;
  50768. }
  50769. }
  50770. return new Proxy(mod, {
  50771. get(mod, prop) {
  50772. if (prop === 'default')
  50773. return defaultExport;
  50774. return mod[prop] ?? defaultExport?.[prop];
  50775. },
  50776. });
  50777. }
  50778. function isPrimitive(value) {
  50779. return !value || (typeof value !== 'object' && typeof value !== 'function');
  50780. }
  50781. var isWsl$2 = {exports: {}};
  50782. const fs$3 = require$$0__default;
  50783. let isDocker$2;
  50784. function hasDockerEnv() {
  50785. try {
  50786. fs$3.statSync('/.dockerenv');
  50787. return true;
  50788. } catch (_) {
  50789. return false;
  50790. }
  50791. }
  50792. function hasDockerCGroup() {
  50793. try {
  50794. return fs$3.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
  50795. } catch (_) {
  50796. return false;
  50797. }
  50798. }
  50799. var isDocker_1 = () => {
  50800. if (isDocker$2 === undefined) {
  50801. isDocker$2 = hasDockerEnv() || hasDockerCGroup();
  50802. }
  50803. return isDocker$2;
  50804. };
  50805. const os = require$$2;
  50806. const fs$2 = require$$0__default;
  50807. const isDocker$1 = isDocker_1;
  50808. const isWsl$1 = () => {
  50809. if (process.platform !== 'linux') {
  50810. return false;
  50811. }
  50812. if (os.release().toLowerCase().includes('microsoft')) {
  50813. if (isDocker$1()) {
  50814. return false;
  50815. }
  50816. return true;
  50817. }
  50818. try {
  50819. return fs$2.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft') ?
  50820. !isDocker$1() : false;
  50821. } catch (_) {
  50822. return false;
  50823. }
  50824. };
  50825. if (process.env.__IS_WSL_TEST__) {
  50826. isWsl$2.exports = isWsl$1;
  50827. } else {
  50828. isWsl$2.exports = isWsl$1();
  50829. }
  50830. var isWslExports = isWsl$2.exports;
  50831. var defineLazyProp = (object, propertyName, fn) => {
  50832. const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true});
  50833. Object.defineProperty(object, propertyName, {
  50834. configurable: true,
  50835. enumerable: true,
  50836. get() {
  50837. const result = fn();
  50838. define(result);
  50839. return result;
  50840. },
  50841. set(value) {
  50842. define(value);
  50843. }
  50844. });
  50845. return object;
  50846. };
  50847. const path$3 = require$$0$4;
  50848. const childProcess = require$$2$1;
  50849. const {promises: fs$1, constants: fsConstants} = require$$0__default;
  50850. const isWsl = isWslExports;
  50851. const isDocker = isDocker_1;
  50852. const defineLazyProperty = defineLazyProp;
  50853. // Path to included `xdg-open`.
  50854. const localXdgOpenPath = path$3.join(__dirname, 'xdg-open');
  50855. const {platform, arch} = process;
  50856. // Podman detection
  50857. const hasContainerEnv = () => {
  50858. try {
  50859. fs$1.statSync('/run/.containerenv');
  50860. return true;
  50861. } catch {
  50862. return false;
  50863. }
  50864. };
  50865. let cachedResult;
  50866. function isInsideContainer() {
  50867. if (cachedResult === undefined) {
  50868. cachedResult = hasContainerEnv() || isDocker();
  50869. }
  50870. return cachedResult;
  50871. }
  50872. /**
  50873. Get the mount point for fixed drives in WSL.
  50874. @inner
  50875. @returns {string} The mount point.
  50876. */
  50877. const getWslDrivesMountPoint = (() => {
  50878. // Default value for "root" param
  50879. // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
  50880. const defaultMountPoint = '/mnt/';
  50881. let mountPoint;
  50882. return async function () {
  50883. if (mountPoint) {
  50884. // Return memoized mount point value
  50885. return mountPoint;
  50886. }
  50887. const configFilePath = '/etc/wsl.conf';
  50888. let isConfigFileExists = false;
  50889. try {
  50890. await fs$1.access(configFilePath, fsConstants.F_OK);
  50891. isConfigFileExists = true;
  50892. } catch {}
  50893. if (!isConfigFileExists) {
  50894. return defaultMountPoint;
  50895. }
  50896. const configContent = await fs$1.readFile(configFilePath, {encoding: 'utf8'});
  50897. const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
  50898. if (!configMountPoint) {
  50899. return defaultMountPoint;
  50900. }
  50901. mountPoint = configMountPoint.groups.mountPoint.trim();
  50902. mountPoint = mountPoint.endsWith('/') ? mountPoint : `${mountPoint}/`;
  50903. return mountPoint;
  50904. };
  50905. })();
  50906. const pTryEach = async (array, mapper) => {
  50907. let latestError;
  50908. for (const item of array) {
  50909. try {
  50910. return await mapper(item); // eslint-disable-line no-await-in-loop
  50911. } catch (error) {
  50912. latestError = error;
  50913. }
  50914. }
  50915. throw latestError;
  50916. };
  50917. const baseOpen = async options => {
  50918. options = {
  50919. wait: false,
  50920. background: false,
  50921. newInstance: false,
  50922. allowNonzeroExitCode: false,
  50923. ...options
  50924. };
  50925. if (Array.isArray(options.app)) {
  50926. return pTryEach(options.app, singleApp => baseOpen({
  50927. ...options,
  50928. app: singleApp
  50929. }));
  50930. }
  50931. let {name: app, arguments: appArguments = []} = options.app || {};
  50932. appArguments = [...appArguments];
  50933. if (Array.isArray(app)) {
  50934. return pTryEach(app, appName => baseOpen({
  50935. ...options,
  50936. app: {
  50937. name: appName,
  50938. arguments: appArguments
  50939. }
  50940. }));
  50941. }
  50942. let command;
  50943. const cliArguments = [];
  50944. const childProcessOptions = {};
  50945. if (platform === 'darwin') {
  50946. command = 'open';
  50947. if (options.wait) {
  50948. cliArguments.push('--wait-apps');
  50949. }
  50950. if (options.background) {
  50951. cliArguments.push('--background');
  50952. }
  50953. if (options.newInstance) {
  50954. cliArguments.push('--new');
  50955. }
  50956. if (app) {
  50957. cliArguments.push('-a', app);
  50958. }
  50959. } else if (platform === 'win32' || (isWsl && !isInsideContainer() && !app)) {
  50960. const mountPoint = await getWslDrivesMountPoint();
  50961. command = isWsl ?
  50962. `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
  50963. `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
  50964. cliArguments.push(
  50965. '-NoProfile',
  50966. '-NonInteractive',
  50967. '–ExecutionPolicy',
  50968. 'Bypass',
  50969. '-EncodedCommand'
  50970. );
  50971. if (!isWsl) {
  50972. childProcessOptions.windowsVerbatimArguments = true;
  50973. }
  50974. const encodedArguments = ['Start'];
  50975. if (options.wait) {
  50976. encodedArguments.push('-Wait');
  50977. }
  50978. if (app) {
  50979. // Double quote with double quotes to ensure the inner quotes are passed through.
  50980. // Inner quotes are delimited for PowerShell interpretation with backticks.
  50981. encodedArguments.push(`"\`"${app}\`""`, '-ArgumentList');
  50982. if (options.target) {
  50983. appArguments.unshift(options.target);
  50984. }
  50985. } else if (options.target) {
  50986. encodedArguments.push(`"${options.target}"`);
  50987. }
  50988. if (appArguments.length > 0) {
  50989. appArguments = appArguments.map(arg => `"\`"${arg}\`""`);
  50990. encodedArguments.push(appArguments.join(','));
  50991. }
  50992. // Using Base64-encoded command, accepted by PowerShell, to allow special characters.
  50993. options.target = Buffer.from(encodedArguments.join(' '), 'utf16le').toString('base64');
  50994. } else {
  50995. if (app) {
  50996. command = app;
  50997. } else {
  50998. // When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
  50999. const isBundled = !__dirname || __dirname === '/';
  51000. // Check if local `xdg-open` exists and is executable.
  51001. let exeLocalXdgOpen = false;
  51002. try {
  51003. await fs$1.access(localXdgOpenPath, fsConstants.X_OK);
  51004. exeLocalXdgOpen = true;
  51005. } catch {}
  51006. const useSystemXdgOpen = process.versions.electron ||
  51007. platform === 'android' || isBundled || !exeLocalXdgOpen;
  51008. command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
  51009. }
  51010. if (appArguments.length > 0) {
  51011. cliArguments.push(...appArguments);
  51012. }
  51013. if (!options.wait) {
  51014. // `xdg-open` will block the process unless stdio is ignored
  51015. // and it's detached from the parent even if it's unref'd.
  51016. childProcessOptions.stdio = 'ignore';
  51017. childProcessOptions.detached = true;
  51018. }
  51019. }
  51020. if (options.target) {
  51021. cliArguments.push(options.target);
  51022. }
  51023. if (platform === 'darwin' && appArguments.length > 0) {
  51024. cliArguments.push('--args', ...appArguments);
  51025. }
  51026. const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
  51027. if (options.wait) {
  51028. return new Promise((resolve, reject) => {
  51029. subprocess.once('error', reject);
  51030. subprocess.once('close', exitCode => {
  51031. if (!options.allowNonzeroExitCode && exitCode > 0) {
  51032. reject(new Error(`Exited with code ${exitCode}`));
  51033. return;
  51034. }
  51035. resolve(subprocess);
  51036. });
  51037. });
  51038. }
  51039. subprocess.unref();
  51040. return subprocess;
  51041. };
  51042. const open = (target, options) => {
  51043. if (typeof target !== 'string') {
  51044. throw new TypeError('Expected a `target`');
  51045. }
  51046. return baseOpen({
  51047. ...options,
  51048. target
  51049. });
  51050. };
  51051. const openApp = (name, options) => {
  51052. if (typeof name !== 'string') {
  51053. throw new TypeError('Expected a `name`');
  51054. }
  51055. const {arguments: appArguments = []} = options || {};
  51056. if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {
  51057. throw new TypeError('Expected `appArguments` as Array type');
  51058. }
  51059. return baseOpen({
  51060. ...options,
  51061. app: {
  51062. name,
  51063. arguments: appArguments
  51064. }
  51065. });
  51066. };
  51067. function detectArchBinary(binary) {
  51068. if (typeof binary === 'string' || Array.isArray(binary)) {
  51069. return binary;
  51070. }
  51071. const {[arch]: archBinary} = binary;
  51072. if (!archBinary) {
  51073. throw new Error(`${arch} is not supported`);
  51074. }
  51075. return archBinary;
  51076. }
  51077. function detectPlatformBinary({[platform]: platformBinary}, {wsl}) {
  51078. if (wsl && isWsl) {
  51079. return detectArchBinary(wsl);
  51080. }
  51081. if (!platformBinary) {
  51082. throw new Error(`${platform} is not supported`);
  51083. }
  51084. return detectArchBinary(platformBinary);
  51085. }
  51086. const apps = {};
  51087. defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({
  51088. darwin: 'google chrome',
  51089. win32: 'chrome',
  51090. linux: ['google-chrome', 'google-chrome-stable', 'chromium']
  51091. }, {
  51092. wsl: {
  51093. ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
  51094. x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe']
  51095. }
  51096. }));
  51097. defineLazyProperty(apps, 'firefox', () => detectPlatformBinary({
  51098. darwin: 'firefox',
  51099. win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
  51100. linux: 'firefox'
  51101. }, {
  51102. wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
  51103. }));
  51104. defineLazyProperty(apps, 'edge', () => detectPlatformBinary({
  51105. darwin: 'microsoft edge',
  51106. win32: 'msedge',
  51107. linux: ['microsoft-edge', 'microsoft-edge-dev']
  51108. }, {
  51109. wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
  51110. }));
  51111. open.apps = apps;
  51112. open.openApp = openApp;
  51113. var open_1 = open;
  51114. var open$1 = /*@__PURE__*/getDefaultExportFromCjs(open_1);
  51115. var crossSpawn = {exports: {}};
  51116. var windows;
  51117. var hasRequiredWindows;
  51118. function requireWindows () {
  51119. if (hasRequiredWindows) return windows;
  51120. hasRequiredWindows = 1;
  51121. windows = isexe;
  51122. isexe.sync = sync;
  51123. var fs = require$$0__default;
  51124. function checkPathExt (path, options) {
  51125. var pathext = options.pathExt !== undefined ?
  51126. options.pathExt : process.env.PATHEXT;
  51127. if (!pathext) {
  51128. return true
  51129. }
  51130. pathext = pathext.split(';');
  51131. if (pathext.indexOf('') !== -1) {
  51132. return true
  51133. }
  51134. for (var i = 0; i < pathext.length; i++) {
  51135. var p = pathext[i].toLowerCase();
  51136. if (p && path.substr(-p.length).toLowerCase() === p) {
  51137. return true
  51138. }
  51139. }
  51140. return false
  51141. }
  51142. function checkStat (stat, path, options) {
  51143. if (!stat.isSymbolicLink() && !stat.isFile()) {
  51144. return false
  51145. }
  51146. return checkPathExt(path, options)
  51147. }
  51148. function isexe (path, options, cb) {
  51149. fs.stat(path, function (er, stat) {
  51150. cb(er, er ? false : checkStat(stat, path, options));
  51151. });
  51152. }
  51153. function sync (path, options) {
  51154. return checkStat(fs.statSync(path), path, options)
  51155. }
  51156. return windows;
  51157. }
  51158. var mode;
  51159. var hasRequiredMode;
  51160. function requireMode () {
  51161. if (hasRequiredMode) return mode;
  51162. hasRequiredMode = 1;
  51163. mode = isexe;
  51164. isexe.sync = sync;
  51165. var fs = require$$0__default;
  51166. function isexe (path, options, cb) {
  51167. fs.stat(path, function (er, stat) {
  51168. cb(er, er ? false : checkStat(stat, options));
  51169. });
  51170. }
  51171. function sync (path, options) {
  51172. return checkStat(fs.statSync(path), options)
  51173. }
  51174. function checkStat (stat, options) {
  51175. return stat.isFile() && checkMode(stat, options)
  51176. }
  51177. function checkMode (stat, options) {
  51178. var mod = stat.mode;
  51179. var uid = stat.uid;
  51180. var gid = stat.gid;
  51181. var myUid = options.uid !== undefined ?
  51182. options.uid : process.getuid && process.getuid();
  51183. var myGid = options.gid !== undefined ?
  51184. options.gid : process.getgid && process.getgid();
  51185. var u = parseInt('100', 8);
  51186. var g = parseInt('010', 8);
  51187. var o = parseInt('001', 8);
  51188. var ug = u | g;
  51189. var ret = (mod & o) ||
  51190. (mod & g) && gid === myGid ||
  51191. (mod & u) && uid === myUid ||
  51192. (mod & ug) && myUid === 0;
  51193. return ret
  51194. }
  51195. return mode;
  51196. }
  51197. var core;
  51198. if (process.platform === 'win32' || commonjsGlobal.TESTING_WINDOWS) {
  51199. core = requireWindows();
  51200. } else {
  51201. core = requireMode();
  51202. }
  51203. var isexe_1 = isexe$1;
  51204. isexe$1.sync = sync;
  51205. function isexe$1 (path, options, cb) {
  51206. if (typeof options === 'function') {
  51207. cb = options;
  51208. options = {};
  51209. }
  51210. if (!cb) {
  51211. if (typeof Promise !== 'function') {
  51212. throw new TypeError('callback not provided')
  51213. }
  51214. return new Promise(function (resolve, reject) {
  51215. isexe$1(path, options || {}, function (er, is) {
  51216. if (er) {
  51217. reject(er);
  51218. } else {
  51219. resolve(is);
  51220. }
  51221. });
  51222. })
  51223. }
  51224. core(path, options || {}, function (er, is) {
  51225. // ignore EACCES because that just means we aren't allowed to run it
  51226. if (er) {
  51227. if (er.code === 'EACCES' || options && options.ignoreErrors) {
  51228. er = null;
  51229. is = false;
  51230. }
  51231. }
  51232. cb(er, is);
  51233. });
  51234. }
  51235. function sync (path, options) {
  51236. // my kingdom for a filtered catch
  51237. try {
  51238. return core.sync(path, options || {})
  51239. } catch (er) {
  51240. if (options && options.ignoreErrors || er.code === 'EACCES') {
  51241. return false
  51242. } else {
  51243. throw er
  51244. }
  51245. }
  51246. }
  51247. const isWindows = process.platform === 'win32' ||
  51248. process.env.OSTYPE === 'cygwin' ||
  51249. process.env.OSTYPE === 'msys';
  51250. const path$2 = require$$0$4;
  51251. const COLON = isWindows ? ';' : ':';
  51252. const isexe = isexe_1;
  51253. const getNotFoundError = (cmd) =>
  51254. Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' });
  51255. const getPathInfo = (cmd, opt) => {
  51256. const colon = opt.colon || COLON;
  51257. // If it has a slash, then we don't bother searching the pathenv.
  51258. // just check the file itself, and that's it.
  51259. const pathEnv = cmd.match(/\//) || isWindows && cmd.match(/\\/) ? ['']
  51260. : (
  51261. [
  51262. // windows always checks the cwd first
  51263. ...(isWindows ? [process.cwd()] : []),
  51264. ...(opt.path || process.env.PATH ||
  51265. /* istanbul ignore next: very unusual */ '').split(colon),
  51266. ]
  51267. );
  51268. const pathExtExe = isWindows
  51269. ? opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM'
  51270. : '';
  51271. const pathExt = isWindows ? pathExtExe.split(colon) : [''];
  51272. if (isWindows) {
  51273. if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
  51274. pathExt.unshift('');
  51275. }
  51276. return {
  51277. pathEnv,
  51278. pathExt,
  51279. pathExtExe,
  51280. }
  51281. };
  51282. const which$1 = (cmd, opt, cb) => {
  51283. if (typeof opt === 'function') {
  51284. cb = opt;
  51285. opt = {};
  51286. }
  51287. if (!opt)
  51288. opt = {};
  51289. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  51290. const found = [];
  51291. const step = i => new Promise((resolve, reject) => {
  51292. if (i === pathEnv.length)
  51293. return opt.all && found.length ? resolve(found)
  51294. : reject(getNotFoundError(cmd))
  51295. const ppRaw = pathEnv[i];
  51296. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  51297. const pCmd = path$2.join(pathPart, cmd);
  51298. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  51299. : pCmd;
  51300. resolve(subStep(p, i, 0));
  51301. });
  51302. const subStep = (p, i, ii) => new Promise((resolve, reject) => {
  51303. if (ii === pathExt.length)
  51304. return resolve(step(i + 1))
  51305. const ext = pathExt[ii];
  51306. isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
  51307. if (!er && is) {
  51308. if (opt.all)
  51309. found.push(p + ext);
  51310. else
  51311. return resolve(p + ext)
  51312. }
  51313. return resolve(subStep(p, i, ii + 1))
  51314. });
  51315. });
  51316. return cb ? step(0).then(res => cb(null, res), cb) : step(0)
  51317. };
  51318. const whichSync = (cmd, opt) => {
  51319. opt = opt || {};
  51320. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  51321. const found = [];
  51322. for (let i = 0; i < pathEnv.length; i ++) {
  51323. const ppRaw = pathEnv[i];
  51324. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  51325. const pCmd = path$2.join(pathPart, cmd);
  51326. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  51327. : pCmd;
  51328. for (let j = 0; j < pathExt.length; j ++) {
  51329. const cur = p + pathExt[j];
  51330. try {
  51331. const is = isexe.sync(cur, { pathExt: pathExtExe });
  51332. if (is) {
  51333. if (opt.all)
  51334. found.push(cur);
  51335. else
  51336. return cur
  51337. }
  51338. } catch (ex) {}
  51339. }
  51340. }
  51341. if (opt.all && found.length)
  51342. return found
  51343. if (opt.nothrow)
  51344. return null
  51345. throw getNotFoundError(cmd)
  51346. };
  51347. var which_1 = which$1;
  51348. which$1.sync = whichSync;
  51349. var pathKey$1 = {exports: {}};
  51350. const pathKey = (options = {}) => {
  51351. const environment = options.env || process.env;
  51352. const platform = options.platform || process.platform;
  51353. if (platform !== 'win32') {
  51354. return 'PATH';
  51355. }
  51356. return Object.keys(environment).reverse().find(key => key.toUpperCase() === 'PATH') || 'Path';
  51357. };
  51358. pathKey$1.exports = pathKey;
  51359. // TODO: Remove this for the next major release
  51360. pathKey$1.exports.default = pathKey;
  51361. var pathKeyExports = pathKey$1.exports;
  51362. const path$1 = require$$0$4;
  51363. const which = which_1;
  51364. const getPathKey = pathKeyExports;
  51365. function resolveCommandAttempt(parsed, withoutPathExt) {
  51366. const env = parsed.options.env || process.env;
  51367. const cwd = process.cwd();
  51368. const hasCustomCwd = parsed.options.cwd != null;
  51369. // Worker threads do not have process.chdir()
  51370. const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;
  51371. // If a custom `cwd` was specified, we need to change the process cwd
  51372. // because `which` will do stat calls but does not support a custom cwd
  51373. if (shouldSwitchCwd) {
  51374. try {
  51375. process.chdir(parsed.options.cwd);
  51376. } catch (err) {
  51377. /* Empty */
  51378. }
  51379. }
  51380. let resolved;
  51381. try {
  51382. resolved = which.sync(parsed.command, {
  51383. path: env[getPathKey({ env })],
  51384. pathExt: withoutPathExt ? path$1.delimiter : undefined,
  51385. });
  51386. } catch (e) {
  51387. /* Empty */
  51388. } finally {
  51389. if (shouldSwitchCwd) {
  51390. process.chdir(cwd);
  51391. }
  51392. }
  51393. // If we successfully resolved, ensure that an absolute path is returned
  51394. // Note that when a custom `cwd` was used, we need to resolve to an absolute path based on it
  51395. if (resolved) {
  51396. resolved = path$1.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
  51397. }
  51398. return resolved;
  51399. }
  51400. function resolveCommand$1(parsed) {
  51401. return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
  51402. }
  51403. var resolveCommand_1 = resolveCommand$1;
  51404. var _escape = {};
  51405. // See http://www.robvanderwoude.com/escapechars.php
  51406. const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
  51407. function escapeCommand(arg) {
  51408. // Escape meta chars
  51409. arg = arg.replace(metaCharsRegExp, '^$1');
  51410. return arg;
  51411. }
  51412. function escapeArgument(arg, doubleEscapeMetaChars) {
  51413. // Convert to string
  51414. arg = `${arg}`;
  51415. // Algorithm below is based on https://qntm.org/cmd
  51416. // Sequence of backslashes followed by a double quote:
  51417. // double up all the backslashes and escape the double quote
  51418. arg = arg.replace(/(\\*)"/g, '$1$1\\"');
  51419. // Sequence of backslashes followed by the end of the string
  51420. // (which will become a double quote later):
  51421. // double up all the backslashes
  51422. arg = arg.replace(/(\\*)$/, '$1$1');
  51423. // All other backslashes occur literally
  51424. // Quote the whole thing:
  51425. arg = `"${arg}"`;
  51426. // Escape meta chars
  51427. arg = arg.replace(metaCharsRegExp, '^$1');
  51428. // Double escape meta chars if necessary
  51429. if (doubleEscapeMetaChars) {
  51430. arg = arg.replace(metaCharsRegExp, '^$1');
  51431. }
  51432. return arg;
  51433. }
  51434. _escape.command = escapeCommand;
  51435. _escape.argument = escapeArgument;
  51436. var shebangRegex$1 = /^#!(.*)/;
  51437. const shebangRegex = shebangRegex$1;
  51438. var shebangCommand$1 = (string = '') => {
  51439. const match = string.match(shebangRegex);
  51440. if (!match) {
  51441. return null;
  51442. }
  51443. const [path, argument] = match[0].replace(/#! ?/, '').split(' ');
  51444. const binary = path.split('/').pop();
  51445. if (binary === 'env') {
  51446. return argument;
  51447. }
  51448. return argument ? `${binary} ${argument}` : binary;
  51449. };
  51450. const fs = require$$0__default;
  51451. const shebangCommand = shebangCommand$1;
  51452. function readShebang$1(command) {
  51453. // Read the first 150 bytes from the file
  51454. const size = 150;
  51455. const buffer = Buffer.alloc(size);
  51456. let fd;
  51457. try {
  51458. fd = fs.openSync(command, 'r');
  51459. fs.readSync(fd, buffer, 0, size, 0);
  51460. fs.closeSync(fd);
  51461. } catch (e) { /* Empty */ }
  51462. // Attempt to extract shebang (null is returned if not a shebang)
  51463. return shebangCommand(buffer.toString());
  51464. }
  51465. var readShebang_1 = readShebang$1;
  51466. const path = require$$0$4;
  51467. const resolveCommand = resolveCommand_1;
  51468. const escape$1 = _escape;
  51469. const readShebang = readShebang_1;
  51470. const isWin$1 = process.platform === 'win32';
  51471. const isExecutableRegExp = /\.(?:com|exe)$/i;
  51472. const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
  51473. function detectShebang(parsed) {
  51474. parsed.file = resolveCommand(parsed);
  51475. const shebang = parsed.file && readShebang(parsed.file);
  51476. if (shebang) {
  51477. parsed.args.unshift(parsed.file);
  51478. parsed.command = shebang;
  51479. return resolveCommand(parsed);
  51480. }
  51481. return parsed.file;
  51482. }
  51483. function parseNonShell(parsed) {
  51484. if (!isWin$1) {
  51485. return parsed;
  51486. }
  51487. // Detect & add support for shebangs
  51488. const commandFile = detectShebang(parsed);
  51489. // We don't need a shell if the command filename is an executable
  51490. const needsShell = !isExecutableRegExp.test(commandFile);
  51491. // If a shell is required, use cmd.exe and take care of escaping everything correctly
  51492. // Note that `forceShell` is an hidden option used only in tests
  51493. if (parsed.options.forceShell || needsShell) {
  51494. // Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
  51495. // The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
  51496. // Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
  51497. // we need to double escape them
  51498. const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
  51499. // Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
  51500. // This is necessary otherwise it will always fail with ENOENT in those cases
  51501. parsed.command = path.normalize(parsed.command);
  51502. // Escape command & arguments
  51503. parsed.command = escape$1.command(parsed.command);
  51504. parsed.args = parsed.args.map((arg) => escape$1.argument(arg, needsDoubleEscapeMetaChars));
  51505. const shellCommand = [parsed.command].concat(parsed.args).join(' ');
  51506. parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
  51507. parsed.command = process.env.comspec || 'cmd.exe';
  51508. parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
  51509. }
  51510. return parsed;
  51511. }
  51512. function parse$4(command, args, options) {
  51513. // Normalize arguments, similar to nodejs
  51514. if (args && !Array.isArray(args)) {
  51515. options = args;
  51516. args = null;
  51517. }
  51518. args = args ? args.slice(0) : []; // Clone array to avoid changing the original
  51519. options = Object.assign({}, options); // Clone object to avoid changing the original
  51520. // Build our parsed object
  51521. const parsed = {
  51522. command,
  51523. args,
  51524. options,
  51525. file: undefined,
  51526. original: {
  51527. command,
  51528. args,
  51529. },
  51530. };
  51531. // Delegate further parsing to shell or non-shell
  51532. return options.shell ? parsed : parseNonShell(parsed);
  51533. }
  51534. var parse_1 = parse$4;
  51535. const isWin = process.platform === 'win32';
  51536. function notFoundError(original, syscall) {
  51537. return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
  51538. code: 'ENOENT',
  51539. errno: 'ENOENT',
  51540. syscall: `${syscall} ${original.command}`,
  51541. path: original.command,
  51542. spawnargs: original.args,
  51543. });
  51544. }
  51545. function hookChildProcess(cp, parsed) {
  51546. if (!isWin) {
  51547. return;
  51548. }
  51549. const originalEmit = cp.emit;
  51550. cp.emit = function (name, arg1) {
  51551. // If emitting "exit" event and exit code is 1, we need to check if
  51552. // the command exists and emit an "error" instead
  51553. // See https://github.com/IndigoUnited/node-cross-spawn/issues/16
  51554. if (name === 'exit') {
  51555. const err = verifyENOENT(arg1, parsed);
  51556. if (err) {
  51557. return originalEmit.call(cp, 'error', err);
  51558. }
  51559. }
  51560. return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
  51561. };
  51562. }
  51563. function verifyENOENT(status, parsed) {
  51564. if (isWin && status === 1 && !parsed.file) {
  51565. return notFoundError(parsed.original, 'spawn');
  51566. }
  51567. return null;
  51568. }
  51569. function verifyENOENTSync(status, parsed) {
  51570. if (isWin && status === 1 && !parsed.file) {
  51571. return notFoundError(parsed.original, 'spawnSync');
  51572. }
  51573. return null;
  51574. }
  51575. var enoent$1 = {
  51576. hookChildProcess,
  51577. verifyENOENT,
  51578. verifyENOENTSync,
  51579. notFoundError,
  51580. };
  51581. const cp = require$$2$1;
  51582. const parse$3 = parse_1;
  51583. const enoent = enoent$1;
  51584. function spawn(command, args, options) {
  51585. // Parse the arguments
  51586. const parsed = parse$3(command, args, options);
  51587. // Spawn the child process
  51588. const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
  51589. // Hook into child process "exit" event to emit an error if the command
  51590. // does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  51591. enoent.hookChildProcess(spawned, parsed);
  51592. return spawned;
  51593. }
  51594. function spawnSync(command, args, options) {
  51595. // Parse the arguments
  51596. const parsed = parse$3(command, args, options);
  51597. // Spawn the child process
  51598. const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
  51599. // Analyze if the command does not exist, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  51600. result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
  51601. return result;
  51602. }
  51603. crossSpawn.exports = spawn;
  51604. crossSpawn.exports.spawn = spawn;
  51605. crossSpawn.exports.sync = spawnSync;
  51606. crossSpawn.exports._parse = parse$3;
  51607. crossSpawn.exports._enoent = enoent;
  51608. var crossSpawnExports = crossSpawn.exports;
  51609. var spawn$1 = /*@__PURE__*/getDefaultExportFromCjs(crossSpawnExports);
  51610. /**
  51611. * The following is modified based on source found in
  51612. * https://github.com/facebook/create-react-app
  51613. *
  51614. * MIT Licensed
  51615. * Copyright (c) 2015-present, Facebook, Inc.
  51616. * https://github.com/facebook/create-react-app/blob/master/LICENSE
  51617. *
  51618. */
  51619. /**
  51620. * Reads the BROWSER environment variable and decides what to do with it.
  51621. */
  51622. function openBrowser(url, opt, logger) {
  51623. // The browser executable to open.
  51624. // See https://github.com/sindresorhus/open#app for documentation.
  51625. const browser = typeof opt === 'string' ? opt : process.env.BROWSER || '';
  51626. if (browser.toLowerCase().endsWith('.js')) {
  51627. executeNodeScript(browser, url, logger);
  51628. }
  51629. else if (browser.toLowerCase() !== 'none') {
  51630. const browserArgs = process.env.BROWSER_ARGS
  51631. ? process.env.BROWSER_ARGS.split(' ')
  51632. : [];
  51633. startBrowserProcess(browser, browserArgs, url);
  51634. }
  51635. }
  51636. function executeNodeScript(scriptPath, url, logger) {
  51637. const extraArgs = process.argv.slice(2);
  51638. const child = spawn$1(process.execPath, [scriptPath, ...extraArgs, url], {
  51639. stdio: 'inherit',
  51640. });
  51641. child.on('close', (code) => {
  51642. if (code !== 0) {
  51643. logger.error(colors$1.red(`\nThe script specified as BROWSER environment variable failed.\n\n${colors$1.cyan(scriptPath)} exited with code ${code}.`), { error: null });
  51644. }
  51645. });
  51646. }
  51647. const supportedChromiumBrowsers = [
  51648. 'Google Chrome Canary',
  51649. 'Google Chrome Dev',
  51650. 'Google Chrome Beta',
  51651. 'Google Chrome',
  51652. 'Microsoft Edge',
  51653. 'Brave Browser',
  51654. 'Vivaldi',
  51655. 'Chromium',
  51656. ];
  51657. async function startBrowserProcess(browser, browserArgs, url) {
  51658. // If we're on OS X, the user hasn't specifically
  51659. // requested a different browser, we can try opening
  51660. // a Chromium browser with AppleScript. This lets us reuse an
  51661. // existing tab when possible instead of creating a new one.
  51662. const preferredOSXBrowser = browser === 'google chrome' ? 'Google Chrome' : browser;
  51663. const shouldTryOpenChromeWithAppleScript = process.platform === 'darwin' &&
  51664. (!preferredOSXBrowser ||
  51665. supportedChromiumBrowsers.includes(preferredOSXBrowser));
  51666. if (shouldTryOpenChromeWithAppleScript) {
  51667. try {
  51668. const ps = await execAsync('ps cax');
  51669. const openedBrowser = preferredOSXBrowser && ps.includes(preferredOSXBrowser)
  51670. ? preferredOSXBrowser
  51671. : supportedChromiumBrowsers.find((b) => ps.includes(b));
  51672. if (openedBrowser) {
  51673. // Try our best to reuse existing tab with AppleScript
  51674. await execAsync(`osascript openChrome.applescript "${encodeURI(url)}" "${openedBrowser}"`, {
  51675. cwd: join$2(VITE_PACKAGE_DIR, 'bin'),
  51676. });
  51677. return true;
  51678. }
  51679. }
  51680. catch (err) {
  51681. // Ignore errors
  51682. }
  51683. }
  51684. // Another special case: on OS X, check if BROWSER has been set to "open".
  51685. // In this case, instead of passing the string `open` to `open` function (which won't work),
  51686. // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
  51687. // https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
  51688. if (process.platform === 'darwin' && browser === 'open') {
  51689. browser = undefined;
  51690. }
  51691. // Fallback to open
  51692. // (It will always open new tab)
  51693. try {
  51694. const options = browser
  51695. ? { app: { name: browser, arguments: browserArgs } }
  51696. : {};
  51697. open$1(url, options).catch(() => { }); // Prevent `unhandledRejection` error.
  51698. return true;
  51699. }
  51700. catch (err) {
  51701. return false;
  51702. }
  51703. }
  51704. function execAsync(command, options) {
  51705. return new Promise((resolve, reject) => {
  51706. exec(command, options, (error, stdout) => {
  51707. if (error) {
  51708. reject(error);
  51709. }
  51710. else {
  51711. resolve(stdout.toString());
  51712. }
  51713. });
  51714. });
  51715. }
  51716. function bindCLIShortcuts(server, opts) {
  51717. if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
  51718. return;
  51719. }
  51720. const isDev = isDevServer(server);
  51721. if (isDev) {
  51722. server._shortcutsOptions = opts;
  51723. }
  51724. if (opts?.print) {
  51725. server.config.logger.info(colors$1.dim(colors$1.green(' ➜')) +
  51726. colors$1.dim(' press ') +
  51727. colors$1.bold('h + enter') +
  51728. colors$1.dim(' to show help'));
  51729. }
  51730. const shortcuts = (opts?.customShortcuts ?? []).concat((isDev
  51731. ? BASE_DEV_SHORTCUTS
  51732. : BASE_PREVIEW_SHORTCUTS));
  51733. let actionRunning = false;
  51734. const onInput = async (input) => {
  51735. if (actionRunning)
  51736. return;
  51737. if (input === 'h') {
  51738. const loggedKeys = new Set();
  51739. server.config.logger.info('\n Shortcuts');
  51740. for (const shortcut of shortcuts) {
  51741. if (loggedKeys.has(shortcut.key))
  51742. continue;
  51743. loggedKeys.add(shortcut.key);
  51744. if (shortcut.action == null)
  51745. continue;
  51746. server.config.logger.info(colors$1.dim(' press ') +
  51747. colors$1.bold(`${shortcut.key} + enter`) +
  51748. colors$1.dim(` to ${shortcut.description}`));
  51749. }
  51750. return;
  51751. }
  51752. const shortcut = shortcuts.find((shortcut) => shortcut.key === input);
  51753. if (!shortcut || shortcut.action == null)
  51754. return;
  51755. actionRunning = true;
  51756. await shortcut.action(server);
  51757. actionRunning = false;
  51758. };
  51759. const rl = readline.createInterface({ input: process.stdin });
  51760. rl.on('line', onInput);
  51761. server.httpServer.on('close', () => rl.close());
  51762. }
  51763. const BASE_DEV_SHORTCUTS = [
  51764. {
  51765. key: 'r',
  51766. description: 'restart the server',
  51767. async action(server) {
  51768. await restartServerWithUrls(server);
  51769. },
  51770. },
  51771. {
  51772. key: 'u',
  51773. description: 'show server url',
  51774. action(server) {
  51775. server.config.logger.info('');
  51776. server.printUrls();
  51777. },
  51778. },
  51779. {
  51780. key: 'o',
  51781. description: 'open in browser',
  51782. action(server) {
  51783. server.openBrowser();
  51784. },
  51785. },
  51786. {
  51787. key: 'c',
  51788. description: 'clear console',
  51789. action(server) {
  51790. server.config.logger.clearScreen('error');
  51791. },
  51792. },
  51793. {
  51794. key: 'q',
  51795. description: 'quit',
  51796. async action(server) {
  51797. await server.close().finally(() => process.exit());
  51798. },
  51799. },
  51800. ];
  51801. const BASE_PREVIEW_SHORTCUTS = [
  51802. {
  51803. key: 'o',
  51804. description: 'open in browser',
  51805. action(server) {
  51806. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  51807. if (url) {
  51808. openBrowser(url, true, server.config.logger);
  51809. }
  51810. else {
  51811. server.config.logger.warn('No URL available to open in browser');
  51812. }
  51813. },
  51814. },
  51815. {
  51816. key: 'q',
  51817. description: 'quit',
  51818. action(server) {
  51819. try {
  51820. server.httpServer.close();
  51821. }
  51822. finally {
  51823. process.exit();
  51824. }
  51825. },
  51826. },
  51827. ];
  51828. function getResolvedOutDirs(root, outDir, outputOptions) {
  51829. const resolvedOutDir = path$o.resolve(root, outDir);
  51830. if (!outputOptions)
  51831. return new Set([resolvedOutDir]);
  51832. return new Set(arraify(outputOptions).map(({ dir }) => dir ? path$o.resolve(root, dir) : resolvedOutDir));
  51833. }
  51834. function resolveEmptyOutDir(emptyOutDir, root, outDirs, logger) {
  51835. if (emptyOutDir != null)
  51836. return emptyOutDir;
  51837. for (const outDir of outDirs) {
  51838. if (!normalizePath$3(outDir).startsWith(withTrailingSlash(root))) {
  51839. // warn if outDir is outside of root
  51840. logger?.warn(picocolorsExports.yellow(`\n${picocolorsExports.bold(`(!)`)} outDir ${picocolorsExports.white(picocolorsExports.dim(outDir))} is not inside project root and will not be emptied.\n` +
  51841. `Use --emptyOutDir to override.\n`));
  51842. return false;
  51843. }
  51844. }
  51845. return true;
  51846. }
  51847. function resolveChokidarOptions(config, options, resolvedOutDirs, emptyOutDir) {
  51848. const { ignored: ignoredList, ...otherOptions } = options ?? {};
  51849. const ignored = [
  51850. '**/.git/**',
  51851. '**/node_modules/**',
  51852. '**/test-results/**',
  51853. glob.escapePath(config.cacheDir) + '/**',
  51854. ...arraify(ignoredList || []),
  51855. ];
  51856. if (emptyOutDir) {
  51857. ignored.push(...[...resolvedOutDirs].map((outDir) => glob.escapePath(outDir) + '/**'));
  51858. }
  51859. const resolvedWatchOptions = {
  51860. ignored,
  51861. ignoreInitial: true,
  51862. ignorePermissionErrors: true,
  51863. ...otherOptions,
  51864. };
  51865. return resolvedWatchOptions;
  51866. }
  51867. class NoopWatcher extends EventEmitter$4 {
  51868. options;
  51869. constructor(options) {
  51870. super();
  51871. this.options = options;
  51872. }
  51873. add() {
  51874. return this;
  51875. }
  51876. unwatch() {
  51877. return this;
  51878. }
  51879. getWatched() {
  51880. return {};
  51881. }
  51882. ref() {
  51883. return this;
  51884. }
  51885. unref() {
  51886. return this;
  51887. }
  51888. async close() {
  51889. // noop
  51890. }
  51891. }
  51892. function createNoopWatcher(options) {
  51893. return new NoopWatcher(options);
  51894. }
  51895. /**
  51896. * Fetch module information for Vite runtime.
  51897. * @experimental
  51898. */
  51899. async function fetchModule(server, url, importer, options = {}) {
  51900. // builtins should always be externalized
  51901. if (url.startsWith('data:') || isBuiltin(url)) {
  51902. return { externalize: url, type: 'builtin' };
  51903. }
  51904. if (isExternalUrl(url)) {
  51905. return { externalize: url, type: 'network' };
  51906. }
  51907. if (url[0] !== '.' && url[0] !== '/') {
  51908. const { isProduction, resolve: { dedupe, preserveSymlinks }, root, ssr, } = server.config;
  51909. const overrideConditions = ssr.resolve?.externalConditions || [];
  51910. const resolveOptions = {
  51911. mainFields: ['main'],
  51912. conditions: [],
  51913. overrideConditions: [...overrideConditions, 'production', 'development'],
  51914. extensions: ['.js', '.cjs', '.json'],
  51915. dedupe,
  51916. preserveSymlinks,
  51917. isBuild: false,
  51918. isProduction,
  51919. root,
  51920. ssrConfig: ssr,
  51921. packageCache: server.config.packageCache,
  51922. };
  51923. const resolved = tryNodeResolve(url, importer, { ...resolveOptions, tryEsmOnly: true }, false, undefined, true);
  51924. if (!resolved) {
  51925. const err = new Error(`Cannot find module '${url}' imported from '${importer}'`);
  51926. err.code = 'ERR_MODULE_NOT_FOUND';
  51927. throw err;
  51928. }
  51929. const file = pathToFileURL(resolved.id).toString();
  51930. const type = isFilePathESM(resolved.id, server.config.packageCache)
  51931. ? 'module'
  51932. : 'commonjs';
  51933. return { externalize: file, type };
  51934. }
  51935. url = unwrapId$1(url);
  51936. let result = await server.transformRequest(url, { ssr: true });
  51937. if (!result) {
  51938. throw new Error(`[vite] transform failed for module '${url}'${importer ? ` imported from '${importer}'` : ''}.`);
  51939. }
  51940. // module entry should be created by transformRequest
  51941. const mod = await server.moduleGraph.getModuleByUrl(url, true);
  51942. if (!mod) {
  51943. throw new Error(`[vite] cannot find module '${url}' ${importer ? ` imported from '${importer}'` : ''}.`);
  51944. }
  51945. if (options.inlineSourceMap !== false) {
  51946. result = inlineSourceMap(mod, result, options.processSourceMap);
  51947. }
  51948. // remove shebang
  51949. if (result.code[0] === '#')
  51950. result.code = result.code.replace(/^#!.*/, (s) => ' '.repeat(s.length));
  51951. return { code: result.code, file: mod.file };
  51952. }
  51953. const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`, 'gm');
  51954. function inlineSourceMap(mod, result, processSourceMap) {
  51955. const map = result.map;
  51956. let code = result.code;
  51957. if (!map ||
  51958. !('version' in map) ||
  51959. code.includes(VITE_RUNTIME_SOURCEMAPPING_SOURCE))
  51960. return result;
  51961. // to reduce the payload size, we only inline vite node source map, because it's also the only one we use
  51962. OTHER_SOURCE_MAP_REGEXP.lastIndex = 0;
  51963. if (OTHER_SOURCE_MAP_REGEXP.test(code))
  51964. code = code.replace(OTHER_SOURCE_MAP_REGEXP, '');
  51965. const sourceMap = processSourceMap?.(map) || map;
  51966. result.code = `${code.trimEnd()}\n//# sourceURL=${mod.id}\n${VITE_RUNTIME_SOURCEMAPPING_SOURCE}\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}\n`;
  51967. return result;
  51968. }
  51969. function ssrFetchModule(server, id, importer) {
  51970. return fetchModule(server, id, importer, {
  51971. processSourceMap(map) {
  51972. // this assumes that "new AsyncFunction" is used to create the module
  51973. return Object.assign({}, map, {
  51974. mappings: ';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings,
  51975. });
  51976. },
  51977. });
  51978. }
  51979. var bufferUtil$1 = {exports: {}};
  51980. var constants = {
  51981. BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
  51982. EMPTY_BUFFER: Buffer.alloc(0),
  51983. GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
  51984. kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
  51985. kListener: Symbol('kListener'),
  51986. kStatusCode: Symbol('status-code'),
  51987. kWebSocket: Symbol('websocket'),
  51988. NOOP: () => {}
  51989. };
  51990. const { EMPTY_BUFFER: EMPTY_BUFFER$3 } = constants;
  51991. const FastBuffer$2 = Buffer[Symbol.species];
  51992. /**
  51993. * Merges an array of buffers into a new buffer.
  51994. *
  51995. * @param {Buffer[]} list The array of buffers to concat
  51996. * @param {Number} totalLength The total length of buffers in the list
  51997. * @return {Buffer} The resulting buffer
  51998. * @public
  51999. */
  52000. function concat$1(list, totalLength) {
  52001. if (list.length === 0) return EMPTY_BUFFER$3;
  52002. if (list.length === 1) return list[0];
  52003. const target = Buffer.allocUnsafe(totalLength);
  52004. let offset = 0;
  52005. for (let i = 0; i < list.length; i++) {
  52006. const buf = list[i];
  52007. target.set(buf, offset);
  52008. offset += buf.length;
  52009. }
  52010. if (offset < totalLength) {
  52011. return new FastBuffer$2(target.buffer, target.byteOffset, offset);
  52012. }
  52013. return target;
  52014. }
  52015. /**
  52016. * Masks a buffer using the given mask.
  52017. *
  52018. * @param {Buffer} source The buffer to mask
  52019. * @param {Buffer} mask The mask to use
  52020. * @param {Buffer} output The buffer where to store the result
  52021. * @param {Number} offset The offset at which to start writing
  52022. * @param {Number} length The number of bytes to mask.
  52023. * @public
  52024. */
  52025. function _mask(source, mask, output, offset, length) {
  52026. for (let i = 0; i < length; i++) {
  52027. output[offset + i] = source[i] ^ mask[i & 3];
  52028. }
  52029. }
  52030. /**
  52031. * Unmasks a buffer using the given mask.
  52032. *
  52033. * @param {Buffer} buffer The buffer to unmask
  52034. * @param {Buffer} mask The mask to use
  52035. * @public
  52036. */
  52037. function _unmask(buffer, mask) {
  52038. for (let i = 0; i < buffer.length; i++) {
  52039. buffer[i] ^= mask[i & 3];
  52040. }
  52041. }
  52042. /**
  52043. * Converts a buffer to an `ArrayBuffer`.
  52044. *
  52045. * @param {Buffer} buf The buffer to convert
  52046. * @return {ArrayBuffer} Converted buffer
  52047. * @public
  52048. */
  52049. function toArrayBuffer$1(buf) {
  52050. if (buf.length === buf.buffer.byteLength) {
  52051. return buf.buffer;
  52052. }
  52053. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
  52054. }
  52055. /**
  52056. * Converts `data` to a `Buffer`.
  52057. *
  52058. * @param {*} data The data to convert
  52059. * @return {Buffer} The buffer
  52060. * @throws {TypeError}
  52061. * @public
  52062. */
  52063. function toBuffer$2(data) {
  52064. toBuffer$2.readOnly = true;
  52065. if (Buffer.isBuffer(data)) return data;
  52066. let buf;
  52067. if (data instanceof ArrayBuffer) {
  52068. buf = new FastBuffer$2(data);
  52069. } else if (ArrayBuffer.isView(data)) {
  52070. buf = new FastBuffer$2(data.buffer, data.byteOffset, data.byteLength);
  52071. } else {
  52072. buf = Buffer.from(data);
  52073. toBuffer$2.readOnly = false;
  52074. }
  52075. return buf;
  52076. }
  52077. bufferUtil$1.exports = {
  52078. concat: concat$1,
  52079. mask: _mask,
  52080. toArrayBuffer: toArrayBuffer$1,
  52081. toBuffer: toBuffer$2,
  52082. unmask: _unmask
  52083. };
  52084. /* istanbul ignore else */
  52085. if (!process.env.WS_NO_BUFFER_UTIL) {
  52086. try {
  52087. const bufferUtil = require('bufferutil');
  52088. bufferUtil$1.exports.mask = function (source, mask, output, offset, length) {
  52089. if (length < 48) _mask(source, mask, output, offset, length);
  52090. else bufferUtil.mask(source, mask, output, offset, length);
  52091. };
  52092. bufferUtil$1.exports.unmask = function (buffer, mask) {
  52093. if (buffer.length < 32) _unmask(buffer, mask);
  52094. else bufferUtil.unmask(buffer, mask);
  52095. };
  52096. } catch (e) {
  52097. // Continue regardless of the error.
  52098. }
  52099. }
  52100. var bufferUtilExports = bufferUtil$1.exports;
  52101. const kDone = Symbol('kDone');
  52102. const kRun = Symbol('kRun');
  52103. /**
  52104. * A very simple job queue with adjustable concurrency. Adapted from
  52105. * https://github.com/STRML/async-limiter
  52106. */
  52107. let Limiter$1 = class Limiter {
  52108. /**
  52109. * Creates a new `Limiter`.
  52110. *
  52111. * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
  52112. * to run concurrently
  52113. */
  52114. constructor(concurrency) {
  52115. this[kDone] = () => {
  52116. this.pending--;
  52117. this[kRun]();
  52118. };
  52119. this.concurrency = concurrency || Infinity;
  52120. this.jobs = [];
  52121. this.pending = 0;
  52122. }
  52123. /**
  52124. * Adds a job to the queue.
  52125. *
  52126. * @param {Function} job The job to run
  52127. * @public
  52128. */
  52129. add(job) {
  52130. this.jobs.push(job);
  52131. this[kRun]();
  52132. }
  52133. /**
  52134. * Removes a job from the queue and runs it if possible.
  52135. *
  52136. * @private
  52137. */
  52138. [kRun]() {
  52139. if (this.pending === this.concurrency) return;
  52140. if (this.jobs.length) {
  52141. const job = this.jobs.shift();
  52142. this.pending++;
  52143. job(this[kDone]);
  52144. }
  52145. }
  52146. };
  52147. var limiter = Limiter$1;
  52148. const zlib = zlib$1;
  52149. const bufferUtil = bufferUtilExports;
  52150. const Limiter = limiter;
  52151. const { kStatusCode: kStatusCode$2 } = constants;
  52152. const FastBuffer$1 = Buffer[Symbol.species];
  52153. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  52154. const kPerMessageDeflate = Symbol('permessage-deflate');
  52155. const kTotalLength = Symbol('total-length');
  52156. const kCallback = Symbol('callback');
  52157. const kBuffers = Symbol('buffers');
  52158. const kError$1 = Symbol('error');
  52159. //
  52160. // We limit zlib concurrency, which prevents severe memory fragmentation
  52161. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  52162. // and https://github.com/websockets/ws/issues/1202
  52163. //
  52164. // Intentionally global; it's the global thread pool that's an issue.
  52165. //
  52166. let zlibLimiter;
  52167. /**
  52168. * permessage-deflate implementation.
  52169. */
  52170. let PerMessageDeflate$4 = class PerMessageDeflate {
  52171. /**
  52172. * Creates a PerMessageDeflate instance.
  52173. *
  52174. * @param {Object} [options] Configuration options
  52175. * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
  52176. * for, or request, a custom client window size
  52177. * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
  52178. * acknowledge disabling of client context takeover
  52179. * @param {Number} [options.concurrencyLimit=10] The number of concurrent
  52180. * calls to zlib
  52181. * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
  52182. * use of a custom server window size
  52183. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  52184. * disabling of server context takeover
  52185. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  52186. * messages should not be compressed if context takeover is disabled
  52187. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  52188. * deflate
  52189. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  52190. * inflate
  52191. * @param {Boolean} [isServer=false] Create the instance in either server or
  52192. * client mode
  52193. * @param {Number} [maxPayload=0] The maximum allowed message length
  52194. */
  52195. constructor(options, isServer, maxPayload) {
  52196. this._maxPayload = maxPayload | 0;
  52197. this._options = options || {};
  52198. this._threshold =
  52199. this._options.threshold !== undefined ? this._options.threshold : 1024;
  52200. this._isServer = !!isServer;
  52201. this._deflate = null;
  52202. this._inflate = null;
  52203. this.params = null;
  52204. if (!zlibLimiter) {
  52205. const concurrency =
  52206. this._options.concurrencyLimit !== undefined
  52207. ? this._options.concurrencyLimit
  52208. : 10;
  52209. zlibLimiter = new Limiter(concurrency);
  52210. }
  52211. }
  52212. /**
  52213. * @type {String}
  52214. */
  52215. static get extensionName() {
  52216. return 'permessage-deflate';
  52217. }
  52218. /**
  52219. * Create an extension negotiation offer.
  52220. *
  52221. * @return {Object} Extension parameters
  52222. * @public
  52223. */
  52224. offer() {
  52225. const params = {};
  52226. if (this._options.serverNoContextTakeover) {
  52227. params.server_no_context_takeover = true;
  52228. }
  52229. if (this._options.clientNoContextTakeover) {
  52230. params.client_no_context_takeover = true;
  52231. }
  52232. if (this._options.serverMaxWindowBits) {
  52233. params.server_max_window_bits = this._options.serverMaxWindowBits;
  52234. }
  52235. if (this._options.clientMaxWindowBits) {
  52236. params.client_max_window_bits = this._options.clientMaxWindowBits;
  52237. } else if (this._options.clientMaxWindowBits == null) {
  52238. params.client_max_window_bits = true;
  52239. }
  52240. return params;
  52241. }
  52242. /**
  52243. * Accept an extension negotiation offer/response.
  52244. *
  52245. * @param {Array} configurations The extension negotiation offers/reponse
  52246. * @return {Object} Accepted configuration
  52247. * @public
  52248. */
  52249. accept(configurations) {
  52250. configurations = this.normalizeParams(configurations);
  52251. this.params = this._isServer
  52252. ? this.acceptAsServer(configurations)
  52253. : this.acceptAsClient(configurations);
  52254. return this.params;
  52255. }
  52256. /**
  52257. * Releases all resources used by the extension.
  52258. *
  52259. * @public
  52260. */
  52261. cleanup() {
  52262. if (this._inflate) {
  52263. this._inflate.close();
  52264. this._inflate = null;
  52265. }
  52266. if (this._deflate) {
  52267. const callback = this._deflate[kCallback];
  52268. this._deflate.close();
  52269. this._deflate = null;
  52270. if (callback) {
  52271. callback(
  52272. new Error(
  52273. 'The deflate stream was closed while data was being processed'
  52274. )
  52275. );
  52276. }
  52277. }
  52278. }
  52279. /**
  52280. * Accept an extension negotiation offer.
  52281. *
  52282. * @param {Array} offers The extension negotiation offers
  52283. * @return {Object} Accepted configuration
  52284. * @private
  52285. */
  52286. acceptAsServer(offers) {
  52287. const opts = this._options;
  52288. const accepted = offers.find((params) => {
  52289. if (
  52290. (opts.serverNoContextTakeover === false &&
  52291. params.server_no_context_takeover) ||
  52292. (params.server_max_window_bits &&
  52293. (opts.serverMaxWindowBits === false ||
  52294. (typeof opts.serverMaxWindowBits === 'number' &&
  52295. opts.serverMaxWindowBits > params.server_max_window_bits))) ||
  52296. (typeof opts.clientMaxWindowBits === 'number' &&
  52297. !params.client_max_window_bits)
  52298. ) {
  52299. return false;
  52300. }
  52301. return true;
  52302. });
  52303. if (!accepted) {
  52304. throw new Error('None of the extension offers can be accepted');
  52305. }
  52306. if (opts.serverNoContextTakeover) {
  52307. accepted.server_no_context_takeover = true;
  52308. }
  52309. if (opts.clientNoContextTakeover) {
  52310. accepted.client_no_context_takeover = true;
  52311. }
  52312. if (typeof opts.serverMaxWindowBits === 'number') {
  52313. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  52314. }
  52315. if (typeof opts.clientMaxWindowBits === 'number') {
  52316. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  52317. } else if (
  52318. accepted.client_max_window_bits === true ||
  52319. opts.clientMaxWindowBits === false
  52320. ) {
  52321. delete accepted.client_max_window_bits;
  52322. }
  52323. return accepted;
  52324. }
  52325. /**
  52326. * Accept the extension negotiation response.
  52327. *
  52328. * @param {Array} response The extension negotiation response
  52329. * @return {Object} Accepted configuration
  52330. * @private
  52331. */
  52332. acceptAsClient(response) {
  52333. const params = response[0];
  52334. if (
  52335. this._options.clientNoContextTakeover === false &&
  52336. params.client_no_context_takeover
  52337. ) {
  52338. throw new Error('Unexpected parameter "client_no_context_takeover"');
  52339. }
  52340. if (!params.client_max_window_bits) {
  52341. if (typeof this._options.clientMaxWindowBits === 'number') {
  52342. params.client_max_window_bits = this._options.clientMaxWindowBits;
  52343. }
  52344. } else if (
  52345. this._options.clientMaxWindowBits === false ||
  52346. (typeof this._options.clientMaxWindowBits === 'number' &&
  52347. params.client_max_window_bits > this._options.clientMaxWindowBits)
  52348. ) {
  52349. throw new Error(
  52350. 'Unexpected or invalid parameter "client_max_window_bits"'
  52351. );
  52352. }
  52353. return params;
  52354. }
  52355. /**
  52356. * Normalize parameters.
  52357. *
  52358. * @param {Array} configurations The extension negotiation offers/reponse
  52359. * @return {Array} The offers/response with normalized parameters
  52360. * @private
  52361. */
  52362. normalizeParams(configurations) {
  52363. configurations.forEach((params) => {
  52364. Object.keys(params).forEach((key) => {
  52365. let value = params[key];
  52366. if (value.length > 1) {
  52367. throw new Error(`Parameter "${key}" must have only a single value`);
  52368. }
  52369. value = value[0];
  52370. if (key === 'client_max_window_bits') {
  52371. if (value !== true) {
  52372. const num = +value;
  52373. if (!Number.isInteger(num) || num < 8 || num > 15) {
  52374. throw new TypeError(
  52375. `Invalid value for parameter "${key}": ${value}`
  52376. );
  52377. }
  52378. value = num;
  52379. } else if (!this._isServer) {
  52380. throw new TypeError(
  52381. `Invalid value for parameter "${key}": ${value}`
  52382. );
  52383. }
  52384. } else if (key === 'server_max_window_bits') {
  52385. const num = +value;
  52386. if (!Number.isInteger(num) || num < 8 || num > 15) {
  52387. throw new TypeError(
  52388. `Invalid value for parameter "${key}": ${value}`
  52389. );
  52390. }
  52391. value = num;
  52392. } else if (
  52393. key === 'client_no_context_takeover' ||
  52394. key === 'server_no_context_takeover'
  52395. ) {
  52396. if (value !== true) {
  52397. throw new TypeError(
  52398. `Invalid value for parameter "${key}": ${value}`
  52399. );
  52400. }
  52401. } else {
  52402. throw new Error(`Unknown parameter "${key}"`);
  52403. }
  52404. params[key] = value;
  52405. });
  52406. });
  52407. return configurations;
  52408. }
  52409. /**
  52410. * Decompress data. Concurrency limited.
  52411. *
  52412. * @param {Buffer} data Compressed data
  52413. * @param {Boolean} fin Specifies whether or not this is the last fragment
  52414. * @param {Function} callback Callback
  52415. * @public
  52416. */
  52417. decompress(data, fin, callback) {
  52418. zlibLimiter.add((done) => {
  52419. this._decompress(data, fin, (err, result) => {
  52420. done();
  52421. callback(err, result);
  52422. });
  52423. });
  52424. }
  52425. /**
  52426. * Compress data. Concurrency limited.
  52427. *
  52428. * @param {(Buffer|String)} data Data to compress
  52429. * @param {Boolean} fin Specifies whether or not this is the last fragment
  52430. * @param {Function} callback Callback
  52431. * @public
  52432. */
  52433. compress(data, fin, callback) {
  52434. zlibLimiter.add((done) => {
  52435. this._compress(data, fin, (err, result) => {
  52436. done();
  52437. callback(err, result);
  52438. });
  52439. });
  52440. }
  52441. /**
  52442. * Decompress data.
  52443. *
  52444. * @param {Buffer} data Compressed data
  52445. * @param {Boolean} fin Specifies whether or not this is the last fragment
  52446. * @param {Function} callback Callback
  52447. * @private
  52448. */
  52449. _decompress(data, fin, callback) {
  52450. const endpoint = this._isServer ? 'client' : 'server';
  52451. if (!this._inflate) {
  52452. const key = `${endpoint}_max_window_bits`;
  52453. const windowBits =
  52454. typeof this.params[key] !== 'number'
  52455. ? zlib.Z_DEFAULT_WINDOWBITS
  52456. : this.params[key];
  52457. this._inflate = zlib.createInflateRaw({
  52458. ...this._options.zlibInflateOptions,
  52459. windowBits
  52460. });
  52461. this._inflate[kPerMessageDeflate] = this;
  52462. this._inflate[kTotalLength] = 0;
  52463. this._inflate[kBuffers] = [];
  52464. this._inflate.on('error', inflateOnError);
  52465. this._inflate.on('data', inflateOnData);
  52466. }
  52467. this._inflate[kCallback] = callback;
  52468. this._inflate.write(data);
  52469. if (fin) this._inflate.write(TRAILER);
  52470. this._inflate.flush(() => {
  52471. const err = this._inflate[kError$1];
  52472. if (err) {
  52473. this._inflate.close();
  52474. this._inflate = null;
  52475. callback(err);
  52476. return;
  52477. }
  52478. const data = bufferUtil.concat(
  52479. this._inflate[kBuffers],
  52480. this._inflate[kTotalLength]
  52481. );
  52482. if (this._inflate._readableState.endEmitted) {
  52483. this._inflate.close();
  52484. this._inflate = null;
  52485. } else {
  52486. this._inflate[kTotalLength] = 0;
  52487. this._inflate[kBuffers] = [];
  52488. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  52489. this._inflate.reset();
  52490. }
  52491. }
  52492. callback(null, data);
  52493. });
  52494. }
  52495. /**
  52496. * Compress data.
  52497. *
  52498. * @param {(Buffer|String)} data Data to compress
  52499. * @param {Boolean} fin Specifies whether or not this is the last fragment
  52500. * @param {Function} callback Callback
  52501. * @private
  52502. */
  52503. _compress(data, fin, callback) {
  52504. const endpoint = this._isServer ? 'server' : 'client';
  52505. if (!this._deflate) {
  52506. const key = `${endpoint}_max_window_bits`;
  52507. const windowBits =
  52508. typeof this.params[key] !== 'number'
  52509. ? zlib.Z_DEFAULT_WINDOWBITS
  52510. : this.params[key];
  52511. this._deflate = zlib.createDeflateRaw({
  52512. ...this._options.zlibDeflateOptions,
  52513. windowBits
  52514. });
  52515. this._deflate[kTotalLength] = 0;
  52516. this._deflate[kBuffers] = [];
  52517. this._deflate.on('data', deflateOnData);
  52518. }
  52519. this._deflate[kCallback] = callback;
  52520. this._deflate.write(data);
  52521. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  52522. if (!this._deflate) {
  52523. //
  52524. // The deflate stream was closed while data was being processed.
  52525. //
  52526. return;
  52527. }
  52528. let data = bufferUtil.concat(
  52529. this._deflate[kBuffers],
  52530. this._deflate[kTotalLength]
  52531. );
  52532. if (fin) {
  52533. data = new FastBuffer$1(data.buffer, data.byteOffset, data.length - 4);
  52534. }
  52535. //
  52536. // Ensure that the callback will not be called again in
  52537. // `PerMessageDeflate#cleanup()`.
  52538. //
  52539. this._deflate[kCallback] = null;
  52540. this._deflate[kTotalLength] = 0;
  52541. this._deflate[kBuffers] = [];
  52542. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  52543. this._deflate.reset();
  52544. }
  52545. callback(null, data);
  52546. });
  52547. }
  52548. };
  52549. var permessageDeflate = PerMessageDeflate$4;
  52550. /**
  52551. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  52552. *
  52553. * @param {Buffer} chunk A chunk of data
  52554. * @private
  52555. */
  52556. function deflateOnData(chunk) {
  52557. this[kBuffers].push(chunk);
  52558. this[kTotalLength] += chunk.length;
  52559. }
  52560. /**
  52561. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  52562. *
  52563. * @param {Buffer} chunk A chunk of data
  52564. * @private
  52565. */
  52566. function inflateOnData(chunk) {
  52567. this[kTotalLength] += chunk.length;
  52568. if (
  52569. this[kPerMessageDeflate]._maxPayload < 1 ||
  52570. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  52571. ) {
  52572. this[kBuffers].push(chunk);
  52573. return;
  52574. }
  52575. this[kError$1] = new RangeError('Max payload size exceeded');
  52576. this[kError$1].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  52577. this[kError$1][kStatusCode$2] = 1009;
  52578. this.removeListener('data', inflateOnData);
  52579. this.reset();
  52580. }
  52581. /**
  52582. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  52583. *
  52584. * @param {Error} err The emitted error
  52585. * @private
  52586. */
  52587. function inflateOnError(err) {
  52588. //
  52589. // There is no need to call `Zlib#close()` as the handle is automatically
  52590. // closed when an error is emitted.
  52591. //
  52592. this[kPerMessageDeflate]._inflate = null;
  52593. err[kStatusCode$2] = 1007;
  52594. this[kCallback](err);
  52595. }
  52596. var validation = {exports: {}};
  52597. const { isUtf8 } = require$$0$a;
  52598. //
  52599. // Allowed token characters:
  52600. //
  52601. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  52602. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  52603. //
  52604. // tokenChars[32] === 0 // ' '
  52605. // tokenChars[33] === 1 // '!'
  52606. // tokenChars[34] === 0 // '"'
  52607. // ...
  52608. //
  52609. // prettier-ignore
  52610. const tokenChars$2 = [
  52611. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  52612. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  52613. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  52614. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  52615. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  52616. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  52617. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  52618. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  52619. ];
  52620. /**
  52621. * Checks if a status code is allowed in a close frame.
  52622. *
  52623. * @param {Number} code The status code
  52624. * @return {Boolean} `true` if the status code is valid, else `false`
  52625. * @public
  52626. */
  52627. function isValidStatusCode$2(code) {
  52628. return (
  52629. (code >= 1000 &&
  52630. code <= 1014 &&
  52631. code !== 1004 &&
  52632. code !== 1005 &&
  52633. code !== 1006) ||
  52634. (code >= 3000 && code <= 4999)
  52635. );
  52636. }
  52637. /**
  52638. * Checks if a given buffer contains only correct UTF-8.
  52639. * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
  52640. * Markus Kuhn.
  52641. *
  52642. * @param {Buffer} buf The buffer to check
  52643. * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
  52644. * @public
  52645. */
  52646. function _isValidUTF8(buf) {
  52647. const len = buf.length;
  52648. let i = 0;
  52649. while (i < len) {
  52650. if ((buf[i] & 0x80) === 0) {
  52651. // 0xxxxxxx
  52652. i++;
  52653. } else if ((buf[i] & 0xe0) === 0xc0) {
  52654. // 110xxxxx 10xxxxxx
  52655. if (
  52656. i + 1 === len ||
  52657. (buf[i + 1] & 0xc0) !== 0x80 ||
  52658. (buf[i] & 0xfe) === 0xc0 // Overlong
  52659. ) {
  52660. return false;
  52661. }
  52662. i += 2;
  52663. } else if ((buf[i] & 0xf0) === 0xe0) {
  52664. // 1110xxxx 10xxxxxx 10xxxxxx
  52665. if (
  52666. i + 2 >= len ||
  52667. (buf[i + 1] & 0xc0) !== 0x80 ||
  52668. (buf[i + 2] & 0xc0) !== 0x80 ||
  52669. (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
  52670. (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
  52671. ) {
  52672. return false;
  52673. }
  52674. i += 3;
  52675. } else if ((buf[i] & 0xf8) === 0xf0) {
  52676. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  52677. if (
  52678. i + 3 >= len ||
  52679. (buf[i + 1] & 0xc0) !== 0x80 ||
  52680. (buf[i + 2] & 0xc0) !== 0x80 ||
  52681. (buf[i + 3] & 0xc0) !== 0x80 ||
  52682. (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
  52683. (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
  52684. buf[i] > 0xf4 // > U+10FFFF
  52685. ) {
  52686. return false;
  52687. }
  52688. i += 4;
  52689. } else {
  52690. return false;
  52691. }
  52692. }
  52693. return true;
  52694. }
  52695. validation.exports = {
  52696. isValidStatusCode: isValidStatusCode$2,
  52697. isValidUTF8: _isValidUTF8,
  52698. tokenChars: tokenChars$2
  52699. };
  52700. if (isUtf8) {
  52701. validation.exports.isValidUTF8 = function (buf) {
  52702. return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
  52703. };
  52704. } /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {
  52705. try {
  52706. const isValidUTF8 = require('utf-8-validate');
  52707. validation.exports.isValidUTF8 = function (buf) {
  52708. return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
  52709. };
  52710. } catch (e) {
  52711. // Continue regardless of the error.
  52712. }
  52713. }
  52714. var validationExports = validation.exports;
  52715. const { Writable: Writable$1 } = require$$0$7;
  52716. const PerMessageDeflate$3 = permessageDeflate;
  52717. const {
  52718. BINARY_TYPES: BINARY_TYPES$1,
  52719. EMPTY_BUFFER: EMPTY_BUFFER$2,
  52720. kStatusCode: kStatusCode$1,
  52721. kWebSocket: kWebSocket$2
  52722. } = constants;
  52723. const { concat, toArrayBuffer, unmask } = bufferUtilExports;
  52724. const { isValidStatusCode: isValidStatusCode$1, isValidUTF8 } = validationExports;
  52725. const FastBuffer = Buffer[Symbol.species];
  52726. const promise = Promise.resolve();
  52727. //
  52728. // `queueMicrotask()` is not available in Node.js < 11.
  52729. //
  52730. const queueTask =
  52731. typeof queueMicrotask === 'function' ? queueMicrotask : queueMicrotaskShim;
  52732. const GET_INFO = 0;
  52733. const GET_PAYLOAD_LENGTH_16 = 1;
  52734. const GET_PAYLOAD_LENGTH_64 = 2;
  52735. const GET_MASK = 3;
  52736. const GET_DATA = 4;
  52737. const INFLATING = 5;
  52738. const DEFER_EVENT = 6;
  52739. /**
  52740. * HyBi Receiver implementation.
  52741. *
  52742. * @extends Writable
  52743. */
  52744. let Receiver$1 = class Receiver extends Writable$1 {
  52745. /**
  52746. * Creates a Receiver instance.
  52747. *
  52748. * @param {Object} [options] Options object
  52749. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  52750. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  52751. * multiple times in the same tick
  52752. * @param {String} [options.binaryType=nodebuffer] The type for binary data
  52753. * @param {Object} [options.extensions] An object containing the negotiated
  52754. * extensions
  52755. * @param {Boolean} [options.isServer=false] Specifies whether to operate in
  52756. * client or server mode
  52757. * @param {Number} [options.maxPayload=0] The maximum allowed message length
  52758. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  52759. * not to skip UTF-8 validation for text and close messages
  52760. */
  52761. constructor(options = {}) {
  52762. super();
  52763. this._allowSynchronousEvents = !!options.allowSynchronousEvents;
  52764. this._binaryType = options.binaryType || BINARY_TYPES$1[0];
  52765. this._extensions = options.extensions || {};
  52766. this._isServer = !!options.isServer;
  52767. this._maxPayload = options.maxPayload | 0;
  52768. this._skipUTF8Validation = !!options.skipUTF8Validation;
  52769. this[kWebSocket$2] = undefined;
  52770. this._bufferedBytes = 0;
  52771. this._buffers = [];
  52772. this._compressed = false;
  52773. this._payloadLength = 0;
  52774. this._mask = undefined;
  52775. this._fragmented = 0;
  52776. this._masked = false;
  52777. this._fin = false;
  52778. this._opcode = 0;
  52779. this._totalPayloadLength = 0;
  52780. this._messageLength = 0;
  52781. this._fragments = [];
  52782. this._errored = false;
  52783. this._loop = false;
  52784. this._state = GET_INFO;
  52785. }
  52786. /**
  52787. * Implements `Writable.prototype._write()`.
  52788. *
  52789. * @param {Buffer} chunk The chunk of data to write
  52790. * @param {String} encoding The character encoding of `chunk`
  52791. * @param {Function} cb Callback
  52792. * @private
  52793. */
  52794. _write(chunk, encoding, cb) {
  52795. if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
  52796. this._bufferedBytes += chunk.length;
  52797. this._buffers.push(chunk);
  52798. this.startLoop(cb);
  52799. }
  52800. /**
  52801. * Consumes `n` bytes from the buffered data.
  52802. *
  52803. * @param {Number} n The number of bytes to consume
  52804. * @return {Buffer} The consumed bytes
  52805. * @private
  52806. */
  52807. consume(n) {
  52808. this._bufferedBytes -= n;
  52809. if (n === this._buffers[0].length) return this._buffers.shift();
  52810. if (n < this._buffers[0].length) {
  52811. const buf = this._buffers[0];
  52812. this._buffers[0] = new FastBuffer(
  52813. buf.buffer,
  52814. buf.byteOffset + n,
  52815. buf.length - n
  52816. );
  52817. return new FastBuffer(buf.buffer, buf.byteOffset, n);
  52818. }
  52819. const dst = Buffer.allocUnsafe(n);
  52820. do {
  52821. const buf = this._buffers[0];
  52822. const offset = dst.length - n;
  52823. if (n >= buf.length) {
  52824. dst.set(this._buffers.shift(), offset);
  52825. } else {
  52826. dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
  52827. this._buffers[0] = new FastBuffer(
  52828. buf.buffer,
  52829. buf.byteOffset + n,
  52830. buf.length - n
  52831. );
  52832. }
  52833. n -= buf.length;
  52834. } while (n > 0);
  52835. return dst;
  52836. }
  52837. /**
  52838. * Starts the parsing loop.
  52839. *
  52840. * @param {Function} cb Callback
  52841. * @private
  52842. */
  52843. startLoop(cb) {
  52844. this._loop = true;
  52845. do {
  52846. switch (this._state) {
  52847. case GET_INFO:
  52848. this.getInfo(cb);
  52849. break;
  52850. case GET_PAYLOAD_LENGTH_16:
  52851. this.getPayloadLength16(cb);
  52852. break;
  52853. case GET_PAYLOAD_LENGTH_64:
  52854. this.getPayloadLength64(cb);
  52855. break;
  52856. case GET_MASK:
  52857. this.getMask();
  52858. break;
  52859. case GET_DATA:
  52860. this.getData(cb);
  52861. break;
  52862. case INFLATING:
  52863. case DEFER_EVENT:
  52864. this._loop = false;
  52865. return;
  52866. }
  52867. } while (this._loop);
  52868. if (!this._errored) cb();
  52869. }
  52870. /**
  52871. * Reads the first two bytes of a frame.
  52872. *
  52873. * @param {Function} cb Callback
  52874. * @private
  52875. */
  52876. getInfo(cb) {
  52877. if (this._bufferedBytes < 2) {
  52878. this._loop = false;
  52879. return;
  52880. }
  52881. const buf = this.consume(2);
  52882. if ((buf[0] & 0x30) !== 0x00) {
  52883. const error = this.createError(
  52884. RangeError,
  52885. 'RSV2 and RSV3 must be clear',
  52886. true,
  52887. 1002,
  52888. 'WS_ERR_UNEXPECTED_RSV_2_3'
  52889. );
  52890. cb(error);
  52891. return;
  52892. }
  52893. const compressed = (buf[0] & 0x40) === 0x40;
  52894. if (compressed && !this._extensions[PerMessageDeflate$3.extensionName]) {
  52895. const error = this.createError(
  52896. RangeError,
  52897. 'RSV1 must be clear',
  52898. true,
  52899. 1002,
  52900. 'WS_ERR_UNEXPECTED_RSV_1'
  52901. );
  52902. cb(error);
  52903. return;
  52904. }
  52905. this._fin = (buf[0] & 0x80) === 0x80;
  52906. this._opcode = buf[0] & 0x0f;
  52907. this._payloadLength = buf[1] & 0x7f;
  52908. if (this._opcode === 0x00) {
  52909. if (compressed) {
  52910. const error = this.createError(
  52911. RangeError,
  52912. 'RSV1 must be clear',
  52913. true,
  52914. 1002,
  52915. 'WS_ERR_UNEXPECTED_RSV_1'
  52916. );
  52917. cb(error);
  52918. return;
  52919. }
  52920. if (!this._fragmented) {
  52921. const error = this.createError(
  52922. RangeError,
  52923. 'invalid opcode 0',
  52924. true,
  52925. 1002,
  52926. 'WS_ERR_INVALID_OPCODE'
  52927. );
  52928. cb(error);
  52929. return;
  52930. }
  52931. this._opcode = this._fragmented;
  52932. } else if (this._opcode === 0x01 || this._opcode === 0x02) {
  52933. if (this._fragmented) {
  52934. const error = this.createError(
  52935. RangeError,
  52936. `invalid opcode ${this._opcode}`,
  52937. true,
  52938. 1002,
  52939. 'WS_ERR_INVALID_OPCODE'
  52940. );
  52941. cb(error);
  52942. return;
  52943. }
  52944. this._compressed = compressed;
  52945. } else if (this._opcode > 0x07 && this._opcode < 0x0b) {
  52946. if (!this._fin) {
  52947. const error = this.createError(
  52948. RangeError,
  52949. 'FIN must be set',
  52950. true,
  52951. 1002,
  52952. 'WS_ERR_EXPECTED_FIN'
  52953. );
  52954. cb(error);
  52955. return;
  52956. }
  52957. if (compressed) {
  52958. const error = this.createError(
  52959. RangeError,
  52960. 'RSV1 must be clear',
  52961. true,
  52962. 1002,
  52963. 'WS_ERR_UNEXPECTED_RSV_1'
  52964. );
  52965. cb(error);
  52966. return;
  52967. }
  52968. if (
  52969. this._payloadLength > 0x7d ||
  52970. (this._opcode === 0x08 && this._payloadLength === 1)
  52971. ) {
  52972. const error = this.createError(
  52973. RangeError,
  52974. `invalid payload length ${this._payloadLength}`,
  52975. true,
  52976. 1002,
  52977. 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
  52978. );
  52979. cb(error);
  52980. return;
  52981. }
  52982. } else {
  52983. const error = this.createError(
  52984. RangeError,
  52985. `invalid opcode ${this._opcode}`,
  52986. true,
  52987. 1002,
  52988. 'WS_ERR_INVALID_OPCODE'
  52989. );
  52990. cb(error);
  52991. return;
  52992. }
  52993. if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
  52994. this._masked = (buf[1] & 0x80) === 0x80;
  52995. if (this._isServer) {
  52996. if (!this._masked) {
  52997. const error = this.createError(
  52998. RangeError,
  52999. 'MASK must be set',
  53000. true,
  53001. 1002,
  53002. 'WS_ERR_EXPECTED_MASK'
  53003. );
  53004. cb(error);
  53005. return;
  53006. }
  53007. } else if (this._masked) {
  53008. const error = this.createError(
  53009. RangeError,
  53010. 'MASK must be clear',
  53011. true,
  53012. 1002,
  53013. 'WS_ERR_UNEXPECTED_MASK'
  53014. );
  53015. cb(error);
  53016. return;
  53017. }
  53018. if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
  53019. else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
  53020. else this.haveLength(cb);
  53021. }
  53022. /**
  53023. * Gets extended payload length (7+16).
  53024. *
  53025. * @param {Function} cb Callback
  53026. * @private
  53027. */
  53028. getPayloadLength16(cb) {
  53029. if (this._bufferedBytes < 2) {
  53030. this._loop = false;
  53031. return;
  53032. }
  53033. this._payloadLength = this.consume(2).readUInt16BE(0);
  53034. this.haveLength(cb);
  53035. }
  53036. /**
  53037. * Gets extended payload length (7+64).
  53038. *
  53039. * @param {Function} cb Callback
  53040. * @private
  53041. */
  53042. getPayloadLength64(cb) {
  53043. if (this._bufferedBytes < 8) {
  53044. this._loop = false;
  53045. return;
  53046. }
  53047. const buf = this.consume(8);
  53048. const num = buf.readUInt32BE(0);
  53049. //
  53050. // The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
  53051. // if payload length is greater than this number.
  53052. //
  53053. if (num > Math.pow(2, 53 - 32) - 1) {
  53054. const error = this.createError(
  53055. RangeError,
  53056. 'Unsupported WebSocket frame: payload length > 2^53 - 1',
  53057. false,
  53058. 1009,
  53059. 'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
  53060. );
  53061. cb(error);
  53062. return;
  53063. }
  53064. this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
  53065. this.haveLength(cb);
  53066. }
  53067. /**
  53068. * Payload length has been read.
  53069. *
  53070. * @param {Function} cb Callback
  53071. * @private
  53072. */
  53073. haveLength(cb) {
  53074. if (this._payloadLength && this._opcode < 0x08) {
  53075. this._totalPayloadLength += this._payloadLength;
  53076. if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
  53077. const error = this.createError(
  53078. RangeError,
  53079. 'Max payload size exceeded',
  53080. false,
  53081. 1009,
  53082. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  53083. );
  53084. cb(error);
  53085. return;
  53086. }
  53087. }
  53088. if (this._masked) this._state = GET_MASK;
  53089. else this._state = GET_DATA;
  53090. }
  53091. /**
  53092. * Reads mask bytes.
  53093. *
  53094. * @private
  53095. */
  53096. getMask() {
  53097. if (this._bufferedBytes < 4) {
  53098. this._loop = false;
  53099. return;
  53100. }
  53101. this._mask = this.consume(4);
  53102. this._state = GET_DATA;
  53103. }
  53104. /**
  53105. * Reads data bytes.
  53106. *
  53107. * @param {Function} cb Callback
  53108. * @private
  53109. */
  53110. getData(cb) {
  53111. let data = EMPTY_BUFFER$2;
  53112. if (this._payloadLength) {
  53113. if (this._bufferedBytes < this._payloadLength) {
  53114. this._loop = false;
  53115. return;
  53116. }
  53117. data = this.consume(this._payloadLength);
  53118. if (
  53119. this._masked &&
  53120. (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0
  53121. ) {
  53122. unmask(data, this._mask);
  53123. }
  53124. }
  53125. if (this._opcode > 0x07) {
  53126. this.controlMessage(data, cb);
  53127. return;
  53128. }
  53129. if (this._compressed) {
  53130. this._state = INFLATING;
  53131. this.decompress(data, cb);
  53132. return;
  53133. }
  53134. if (data.length) {
  53135. //
  53136. // This message is not compressed so its length is the sum of the payload
  53137. // length of all fragments.
  53138. //
  53139. this._messageLength = this._totalPayloadLength;
  53140. this._fragments.push(data);
  53141. }
  53142. this.dataMessage(cb);
  53143. }
  53144. /**
  53145. * Decompresses data.
  53146. *
  53147. * @param {Buffer} data Compressed data
  53148. * @param {Function} cb Callback
  53149. * @private
  53150. */
  53151. decompress(data, cb) {
  53152. const perMessageDeflate = this._extensions[PerMessageDeflate$3.extensionName];
  53153. perMessageDeflate.decompress(data, this._fin, (err, buf) => {
  53154. if (err) return cb(err);
  53155. if (buf.length) {
  53156. this._messageLength += buf.length;
  53157. if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
  53158. const error = this.createError(
  53159. RangeError,
  53160. 'Max payload size exceeded',
  53161. false,
  53162. 1009,
  53163. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  53164. );
  53165. cb(error);
  53166. return;
  53167. }
  53168. this._fragments.push(buf);
  53169. }
  53170. this.dataMessage(cb);
  53171. if (this._state === GET_INFO) this.startLoop(cb);
  53172. });
  53173. }
  53174. /**
  53175. * Handles a data message.
  53176. *
  53177. * @param {Function} cb Callback
  53178. * @private
  53179. */
  53180. dataMessage(cb) {
  53181. if (!this._fin) {
  53182. this._state = GET_INFO;
  53183. return;
  53184. }
  53185. const messageLength = this._messageLength;
  53186. const fragments = this._fragments;
  53187. this._totalPayloadLength = 0;
  53188. this._messageLength = 0;
  53189. this._fragmented = 0;
  53190. this._fragments = [];
  53191. if (this._opcode === 2) {
  53192. let data;
  53193. if (this._binaryType === 'nodebuffer') {
  53194. data = concat(fragments, messageLength);
  53195. } else if (this._binaryType === 'arraybuffer') {
  53196. data = toArrayBuffer(concat(fragments, messageLength));
  53197. } else {
  53198. data = fragments;
  53199. }
  53200. //
  53201. // If the state is `INFLATING`, it means that the frame data was
  53202. // decompressed asynchronously, so there is no need to defer the event
  53203. // as it will be emitted asynchronously anyway.
  53204. //
  53205. if (this._state === INFLATING || this._allowSynchronousEvents) {
  53206. this.emit('message', data, true);
  53207. this._state = GET_INFO;
  53208. } else {
  53209. this._state = DEFER_EVENT;
  53210. queueTask(() => {
  53211. this.emit('message', data, true);
  53212. this._state = GET_INFO;
  53213. this.startLoop(cb);
  53214. });
  53215. }
  53216. } else {
  53217. const buf = concat(fragments, messageLength);
  53218. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  53219. const error = this.createError(
  53220. Error,
  53221. 'invalid UTF-8 sequence',
  53222. true,
  53223. 1007,
  53224. 'WS_ERR_INVALID_UTF8'
  53225. );
  53226. cb(error);
  53227. return;
  53228. }
  53229. if (this._state === INFLATING || this._allowSynchronousEvents) {
  53230. this.emit('message', buf, false);
  53231. this._state = GET_INFO;
  53232. } else {
  53233. this._state = DEFER_EVENT;
  53234. queueTask(() => {
  53235. this.emit('message', buf, false);
  53236. this._state = GET_INFO;
  53237. this.startLoop(cb);
  53238. });
  53239. }
  53240. }
  53241. }
  53242. /**
  53243. * Handles a control message.
  53244. *
  53245. * @param {Buffer} data Data to handle
  53246. * @return {(Error|RangeError|undefined)} A possible error
  53247. * @private
  53248. */
  53249. controlMessage(data, cb) {
  53250. if (this._opcode === 0x08) {
  53251. if (data.length === 0) {
  53252. this._loop = false;
  53253. this.emit('conclude', 1005, EMPTY_BUFFER$2);
  53254. this.end();
  53255. } else {
  53256. const code = data.readUInt16BE(0);
  53257. if (!isValidStatusCode$1(code)) {
  53258. const error = this.createError(
  53259. RangeError,
  53260. `invalid status code ${code}`,
  53261. true,
  53262. 1002,
  53263. 'WS_ERR_INVALID_CLOSE_CODE'
  53264. );
  53265. cb(error);
  53266. return;
  53267. }
  53268. const buf = new FastBuffer(
  53269. data.buffer,
  53270. data.byteOffset + 2,
  53271. data.length - 2
  53272. );
  53273. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  53274. const error = this.createError(
  53275. Error,
  53276. 'invalid UTF-8 sequence',
  53277. true,
  53278. 1007,
  53279. 'WS_ERR_INVALID_UTF8'
  53280. );
  53281. cb(error);
  53282. return;
  53283. }
  53284. this._loop = false;
  53285. this.emit('conclude', code, buf);
  53286. this.end();
  53287. }
  53288. this._state = GET_INFO;
  53289. return;
  53290. }
  53291. if (this._allowSynchronousEvents) {
  53292. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  53293. this._state = GET_INFO;
  53294. } else {
  53295. this._state = DEFER_EVENT;
  53296. queueTask(() => {
  53297. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  53298. this._state = GET_INFO;
  53299. this.startLoop(cb);
  53300. });
  53301. }
  53302. }
  53303. /**
  53304. * Builds an error object.
  53305. *
  53306. * @param {function(new:Error|RangeError)} ErrorCtor The error constructor
  53307. * @param {String} message The error message
  53308. * @param {Boolean} prefix Specifies whether or not to add a default prefix to
  53309. * `message`
  53310. * @param {Number} statusCode The status code
  53311. * @param {String} errorCode The exposed error code
  53312. * @return {(Error|RangeError)} The error
  53313. * @private
  53314. */
  53315. createError(ErrorCtor, message, prefix, statusCode, errorCode) {
  53316. this._loop = false;
  53317. this._errored = true;
  53318. const err = new ErrorCtor(
  53319. prefix ? `Invalid WebSocket frame: ${message}` : message
  53320. );
  53321. Error.captureStackTrace(err, this.createError);
  53322. err.code = errorCode;
  53323. err[kStatusCode$1] = statusCode;
  53324. return err;
  53325. }
  53326. };
  53327. var receiver = Receiver$1;
  53328. /**
  53329. * A shim for `queueMicrotask()`.
  53330. *
  53331. * @param {Function} cb Callback
  53332. */
  53333. function queueMicrotaskShim(cb) {
  53334. promise.then(cb).catch(throwErrorNextTick);
  53335. }
  53336. /**
  53337. * Throws an error.
  53338. *
  53339. * @param {Error} err The error to throw
  53340. * @private
  53341. */
  53342. function throwError(err) {
  53343. throw err;
  53344. }
  53345. /**
  53346. * Throws an error in the next tick.
  53347. *
  53348. * @param {Error} err The error to throw
  53349. * @private
  53350. */
  53351. function throwErrorNextTick(err) {
  53352. process.nextTick(throwError, err);
  53353. }
  53354. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
  53355. const { randomFillSync } = require$$3$1;
  53356. const PerMessageDeflate$2 = permessageDeflate;
  53357. const { EMPTY_BUFFER: EMPTY_BUFFER$1 } = constants;
  53358. const { isValidStatusCode } = validationExports;
  53359. const { mask: applyMask, toBuffer: toBuffer$1 } = bufferUtilExports;
  53360. const kByteLength = Symbol('kByteLength');
  53361. const maskBuffer = Buffer.alloc(4);
  53362. /**
  53363. * HyBi Sender implementation.
  53364. */
  53365. let Sender$1 = class Sender {
  53366. /**
  53367. * Creates a Sender instance.
  53368. *
  53369. * @param {Duplex} socket The connection socket
  53370. * @param {Object} [extensions] An object containing the negotiated extensions
  53371. * @param {Function} [generateMask] The function used to generate the masking
  53372. * key
  53373. */
  53374. constructor(socket, extensions, generateMask) {
  53375. this._extensions = extensions || {};
  53376. if (generateMask) {
  53377. this._generateMask = generateMask;
  53378. this._maskBuffer = Buffer.alloc(4);
  53379. }
  53380. this._socket = socket;
  53381. this._firstFragment = true;
  53382. this._compress = false;
  53383. this._bufferedBytes = 0;
  53384. this._deflating = false;
  53385. this._queue = [];
  53386. }
  53387. /**
  53388. * Frames a piece of data according to the HyBi WebSocket protocol.
  53389. *
  53390. * @param {(Buffer|String)} data The data to frame
  53391. * @param {Object} options Options object
  53392. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  53393. * FIN bit
  53394. * @param {Function} [options.generateMask] The function used to generate the
  53395. * masking key
  53396. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  53397. * `data`
  53398. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  53399. * key
  53400. * @param {Number} options.opcode The opcode
  53401. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  53402. * modified
  53403. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  53404. * RSV1 bit
  53405. * @return {(Buffer|String)[]} The framed data
  53406. * @public
  53407. */
  53408. static frame(data, options) {
  53409. let mask;
  53410. let merge = false;
  53411. let offset = 2;
  53412. let skipMasking = false;
  53413. if (options.mask) {
  53414. mask = options.maskBuffer || maskBuffer;
  53415. if (options.generateMask) {
  53416. options.generateMask(mask);
  53417. } else {
  53418. randomFillSync(mask, 0, 4);
  53419. }
  53420. skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
  53421. offset = 6;
  53422. }
  53423. let dataLength;
  53424. if (typeof data === 'string') {
  53425. if (
  53426. (!options.mask || skipMasking) &&
  53427. options[kByteLength] !== undefined
  53428. ) {
  53429. dataLength = options[kByteLength];
  53430. } else {
  53431. data = Buffer.from(data);
  53432. dataLength = data.length;
  53433. }
  53434. } else {
  53435. dataLength = data.length;
  53436. merge = options.mask && options.readOnly && !skipMasking;
  53437. }
  53438. let payloadLength = dataLength;
  53439. if (dataLength >= 65536) {
  53440. offset += 8;
  53441. payloadLength = 127;
  53442. } else if (dataLength > 125) {
  53443. offset += 2;
  53444. payloadLength = 126;
  53445. }
  53446. const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
  53447. target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
  53448. if (options.rsv1) target[0] |= 0x40;
  53449. target[1] = payloadLength;
  53450. if (payloadLength === 126) {
  53451. target.writeUInt16BE(dataLength, 2);
  53452. } else if (payloadLength === 127) {
  53453. target[2] = target[3] = 0;
  53454. target.writeUIntBE(dataLength, 4, 6);
  53455. }
  53456. if (!options.mask) return [target, data];
  53457. target[1] |= 0x80;
  53458. target[offset - 4] = mask[0];
  53459. target[offset - 3] = mask[1];
  53460. target[offset - 2] = mask[2];
  53461. target[offset - 1] = mask[3];
  53462. if (skipMasking) return [target, data];
  53463. if (merge) {
  53464. applyMask(data, mask, target, offset, dataLength);
  53465. return [target];
  53466. }
  53467. applyMask(data, mask, data, 0, dataLength);
  53468. return [target, data];
  53469. }
  53470. /**
  53471. * Sends a close message to the other peer.
  53472. *
  53473. * @param {Number} [code] The status code component of the body
  53474. * @param {(String|Buffer)} [data] The message component of the body
  53475. * @param {Boolean} [mask=false] Specifies whether or not to mask the message
  53476. * @param {Function} [cb] Callback
  53477. * @public
  53478. */
  53479. close(code, data, mask, cb) {
  53480. let buf;
  53481. if (code === undefined) {
  53482. buf = EMPTY_BUFFER$1;
  53483. } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
  53484. throw new TypeError('First argument must be a valid error code number');
  53485. } else if (data === undefined || !data.length) {
  53486. buf = Buffer.allocUnsafe(2);
  53487. buf.writeUInt16BE(code, 0);
  53488. } else {
  53489. const length = Buffer.byteLength(data);
  53490. if (length > 123) {
  53491. throw new RangeError('The message must not be greater than 123 bytes');
  53492. }
  53493. buf = Buffer.allocUnsafe(2 + length);
  53494. buf.writeUInt16BE(code, 0);
  53495. if (typeof data === 'string') {
  53496. buf.write(data, 2);
  53497. } else {
  53498. buf.set(data, 2);
  53499. }
  53500. }
  53501. const options = {
  53502. [kByteLength]: buf.length,
  53503. fin: true,
  53504. generateMask: this._generateMask,
  53505. mask,
  53506. maskBuffer: this._maskBuffer,
  53507. opcode: 0x08,
  53508. readOnly: false,
  53509. rsv1: false
  53510. };
  53511. if (this._deflating) {
  53512. this.enqueue([this.dispatch, buf, false, options, cb]);
  53513. } else {
  53514. this.sendFrame(Sender.frame(buf, options), cb);
  53515. }
  53516. }
  53517. /**
  53518. * Sends a ping message to the other peer.
  53519. *
  53520. * @param {*} data The message to send
  53521. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  53522. * @param {Function} [cb] Callback
  53523. * @public
  53524. */
  53525. ping(data, mask, cb) {
  53526. let byteLength;
  53527. let readOnly;
  53528. if (typeof data === 'string') {
  53529. byteLength = Buffer.byteLength(data);
  53530. readOnly = false;
  53531. } else {
  53532. data = toBuffer$1(data);
  53533. byteLength = data.length;
  53534. readOnly = toBuffer$1.readOnly;
  53535. }
  53536. if (byteLength > 125) {
  53537. throw new RangeError('The data size must not be greater than 125 bytes');
  53538. }
  53539. const options = {
  53540. [kByteLength]: byteLength,
  53541. fin: true,
  53542. generateMask: this._generateMask,
  53543. mask,
  53544. maskBuffer: this._maskBuffer,
  53545. opcode: 0x09,
  53546. readOnly,
  53547. rsv1: false
  53548. };
  53549. if (this._deflating) {
  53550. this.enqueue([this.dispatch, data, false, options, cb]);
  53551. } else {
  53552. this.sendFrame(Sender.frame(data, options), cb);
  53553. }
  53554. }
  53555. /**
  53556. * Sends a pong message to the other peer.
  53557. *
  53558. * @param {*} data The message to send
  53559. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  53560. * @param {Function} [cb] Callback
  53561. * @public
  53562. */
  53563. pong(data, mask, cb) {
  53564. let byteLength;
  53565. let readOnly;
  53566. if (typeof data === 'string') {
  53567. byteLength = Buffer.byteLength(data);
  53568. readOnly = false;
  53569. } else {
  53570. data = toBuffer$1(data);
  53571. byteLength = data.length;
  53572. readOnly = toBuffer$1.readOnly;
  53573. }
  53574. if (byteLength > 125) {
  53575. throw new RangeError('The data size must not be greater than 125 bytes');
  53576. }
  53577. const options = {
  53578. [kByteLength]: byteLength,
  53579. fin: true,
  53580. generateMask: this._generateMask,
  53581. mask,
  53582. maskBuffer: this._maskBuffer,
  53583. opcode: 0x0a,
  53584. readOnly,
  53585. rsv1: false
  53586. };
  53587. if (this._deflating) {
  53588. this.enqueue([this.dispatch, data, false, options, cb]);
  53589. } else {
  53590. this.sendFrame(Sender.frame(data, options), cb);
  53591. }
  53592. }
  53593. /**
  53594. * Sends a data message to the other peer.
  53595. *
  53596. * @param {*} data The message to send
  53597. * @param {Object} options Options object
  53598. * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
  53599. * or text
  53600. * @param {Boolean} [options.compress=false] Specifies whether or not to
  53601. * compress `data`
  53602. * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
  53603. * last one
  53604. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  53605. * `data`
  53606. * @param {Function} [cb] Callback
  53607. * @public
  53608. */
  53609. send(data, options, cb) {
  53610. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  53611. let opcode = options.binary ? 2 : 1;
  53612. let rsv1 = options.compress;
  53613. let byteLength;
  53614. let readOnly;
  53615. if (typeof data === 'string') {
  53616. byteLength = Buffer.byteLength(data);
  53617. readOnly = false;
  53618. } else {
  53619. data = toBuffer$1(data);
  53620. byteLength = data.length;
  53621. readOnly = toBuffer$1.readOnly;
  53622. }
  53623. if (this._firstFragment) {
  53624. this._firstFragment = false;
  53625. if (
  53626. rsv1 &&
  53627. perMessageDeflate &&
  53628. perMessageDeflate.params[
  53629. perMessageDeflate._isServer
  53630. ? 'server_no_context_takeover'
  53631. : 'client_no_context_takeover'
  53632. ]
  53633. ) {
  53634. rsv1 = byteLength >= perMessageDeflate._threshold;
  53635. }
  53636. this._compress = rsv1;
  53637. } else {
  53638. rsv1 = false;
  53639. opcode = 0;
  53640. }
  53641. if (options.fin) this._firstFragment = true;
  53642. if (perMessageDeflate) {
  53643. const opts = {
  53644. [kByteLength]: byteLength,
  53645. fin: options.fin,
  53646. generateMask: this._generateMask,
  53647. mask: options.mask,
  53648. maskBuffer: this._maskBuffer,
  53649. opcode,
  53650. readOnly,
  53651. rsv1
  53652. };
  53653. if (this._deflating) {
  53654. this.enqueue([this.dispatch, data, this._compress, opts, cb]);
  53655. } else {
  53656. this.dispatch(data, this._compress, opts, cb);
  53657. }
  53658. } else {
  53659. this.sendFrame(
  53660. Sender.frame(data, {
  53661. [kByteLength]: byteLength,
  53662. fin: options.fin,
  53663. generateMask: this._generateMask,
  53664. mask: options.mask,
  53665. maskBuffer: this._maskBuffer,
  53666. opcode,
  53667. readOnly,
  53668. rsv1: false
  53669. }),
  53670. cb
  53671. );
  53672. }
  53673. }
  53674. /**
  53675. * Dispatches a message.
  53676. *
  53677. * @param {(Buffer|String)} data The message to send
  53678. * @param {Boolean} [compress=false] Specifies whether or not to compress
  53679. * `data`
  53680. * @param {Object} options Options object
  53681. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  53682. * FIN bit
  53683. * @param {Function} [options.generateMask] The function used to generate the
  53684. * masking key
  53685. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  53686. * `data`
  53687. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  53688. * key
  53689. * @param {Number} options.opcode The opcode
  53690. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  53691. * modified
  53692. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  53693. * RSV1 bit
  53694. * @param {Function} [cb] Callback
  53695. * @private
  53696. */
  53697. dispatch(data, compress, options, cb) {
  53698. if (!compress) {
  53699. this.sendFrame(Sender.frame(data, options), cb);
  53700. return;
  53701. }
  53702. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  53703. this._bufferedBytes += options[kByteLength];
  53704. this._deflating = true;
  53705. perMessageDeflate.compress(data, options.fin, (_, buf) => {
  53706. if (this._socket.destroyed) {
  53707. const err = new Error(
  53708. 'The socket was closed while data was being compressed'
  53709. );
  53710. if (typeof cb === 'function') cb(err);
  53711. for (let i = 0; i < this._queue.length; i++) {
  53712. const params = this._queue[i];
  53713. const callback = params[params.length - 1];
  53714. if (typeof callback === 'function') callback(err);
  53715. }
  53716. return;
  53717. }
  53718. this._bufferedBytes -= options[kByteLength];
  53719. this._deflating = false;
  53720. options.readOnly = false;
  53721. this.sendFrame(Sender.frame(buf, options), cb);
  53722. this.dequeue();
  53723. });
  53724. }
  53725. /**
  53726. * Executes queued send operations.
  53727. *
  53728. * @private
  53729. */
  53730. dequeue() {
  53731. while (!this._deflating && this._queue.length) {
  53732. const params = this._queue.shift();
  53733. this._bufferedBytes -= params[3][kByteLength];
  53734. Reflect.apply(params[0], this, params.slice(1));
  53735. }
  53736. }
  53737. /**
  53738. * Enqueues a send operation.
  53739. *
  53740. * @param {Array} params Send operation parameters.
  53741. * @private
  53742. */
  53743. enqueue(params) {
  53744. this._bufferedBytes += params[3][kByteLength];
  53745. this._queue.push(params);
  53746. }
  53747. /**
  53748. * Sends a frame.
  53749. *
  53750. * @param {Buffer[]} list The frame to send
  53751. * @param {Function} [cb] Callback
  53752. * @private
  53753. */
  53754. sendFrame(list, cb) {
  53755. if (list.length === 2) {
  53756. this._socket.cork();
  53757. this._socket.write(list[0]);
  53758. this._socket.write(list[1], cb);
  53759. this._socket.uncork();
  53760. } else {
  53761. this._socket.write(list[0], cb);
  53762. }
  53763. }
  53764. };
  53765. var sender = Sender$1;
  53766. const { kForOnEventAttribute: kForOnEventAttribute$1, kListener: kListener$1 } = constants;
  53767. const kCode = Symbol('kCode');
  53768. const kData = Symbol('kData');
  53769. const kError = Symbol('kError');
  53770. const kMessage = Symbol('kMessage');
  53771. const kReason = Symbol('kReason');
  53772. const kTarget = Symbol('kTarget');
  53773. const kType = Symbol('kType');
  53774. const kWasClean = Symbol('kWasClean');
  53775. /**
  53776. * Class representing an event.
  53777. */
  53778. let Event$1 = class Event {
  53779. /**
  53780. * Create a new `Event`.
  53781. *
  53782. * @param {String} type The name of the event
  53783. * @throws {TypeError} If the `type` argument is not specified
  53784. */
  53785. constructor(type) {
  53786. this[kTarget] = null;
  53787. this[kType] = type;
  53788. }
  53789. /**
  53790. * @type {*}
  53791. */
  53792. get target() {
  53793. return this[kTarget];
  53794. }
  53795. /**
  53796. * @type {String}
  53797. */
  53798. get type() {
  53799. return this[kType];
  53800. }
  53801. };
  53802. Object.defineProperty(Event$1.prototype, 'target', { enumerable: true });
  53803. Object.defineProperty(Event$1.prototype, 'type', { enumerable: true });
  53804. /**
  53805. * Class representing a close event.
  53806. *
  53807. * @extends Event
  53808. */
  53809. class CloseEvent extends Event$1 {
  53810. /**
  53811. * Create a new `CloseEvent`.
  53812. *
  53813. * @param {String} type The name of the event
  53814. * @param {Object} [options] A dictionary object that allows for setting
  53815. * attributes via object members of the same name
  53816. * @param {Number} [options.code=0] The status code explaining why the
  53817. * connection was closed
  53818. * @param {String} [options.reason=''] A human-readable string explaining why
  53819. * the connection was closed
  53820. * @param {Boolean} [options.wasClean=false] Indicates whether or not the
  53821. * connection was cleanly closed
  53822. */
  53823. constructor(type, options = {}) {
  53824. super(type);
  53825. this[kCode] = options.code === undefined ? 0 : options.code;
  53826. this[kReason] = options.reason === undefined ? '' : options.reason;
  53827. this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
  53828. }
  53829. /**
  53830. * @type {Number}
  53831. */
  53832. get code() {
  53833. return this[kCode];
  53834. }
  53835. /**
  53836. * @type {String}
  53837. */
  53838. get reason() {
  53839. return this[kReason];
  53840. }
  53841. /**
  53842. * @type {Boolean}
  53843. */
  53844. get wasClean() {
  53845. return this[kWasClean];
  53846. }
  53847. }
  53848. Object.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });
  53849. Object.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });
  53850. Object.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });
  53851. /**
  53852. * Class representing an error event.
  53853. *
  53854. * @extends Event
  53855. */
  53856. class ErrorEvent extends Event$1 {
  53857. /**
  53858. * Create a new `ErrorEvent`.
  53859. *
  53860. * @param {String} type The name of the event
  53861. * @param {Object} [options] A dictionary object that allows for setting
  53862. * attributes via object members of the same name
  53863. * @param {*} [options.error=null] The error that generated this event
  53864. * @param {String} [options.message=''] The error message
  53865. */
  53866. constructor(type, options = {}) {
  53867. super(type);
  53868. this[kError] = options.error === undefined ? null : options.error;
  53869. this[kMessage] = options.message === undefined ? '' : options.message;
  53870. }
  53871. /**
  53872. * @type {*}
  53873. */
  53874. get error() {
  53875. return this[kError];
  53876. }
  53877. /**
  53878. * @type {String}
  53879. */
  53880. get message() {
  53881. return this[kMessage];
  53882. }
  53883. }
  53884. Object.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });
  53885. Object.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });
  53886. /**
  53887. * Class representing a message event.
  53888. *
  53889. * @extends Event
  53890. */
  53891. class MessageEvent extends Event$1 {
  53892. /**
  53893. * Create a new `MessageEvent`.
  53894. *
  53895. * @param {String} type The name of the event
  53896. * @param {Object} [options] A dictionary object that allows for setting
  53897. * attributes via object members of the same name
  53898. * @param {*} [options.data=null] The message content
  53899. */
  53900. constructor(type, options = {}) {
  53901. super(type);
  53902. this[kData] = options.data === undefined ? null : options.data;
  53903. }
  53904. /**
  53905. * @type {*}
  53906. */
  53907. get data() {
  53908. return this[kData];
  53909. }
  53910. }
  53911. Object.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });
  53912. /**
  53913. * This provides methods for emulating the `EventTarget` interface. It's not
  53914. * meant to be used directly.
  53915. *
  53916. * @mixin
  53917. */
  53918. const EventTarget = {
  53919. /**
  53920. * Register an event listener.
  53921. *
  53922. * @param {String} type A string representing the event type to listen for
  53923. * @param {(Function|Object)} handler The listener to add
  53924. * @param {Object} [options] An options object specifies characteristics about
  53925. * the event listener
  53926. * @param {Boolean} [options.once=false] A `Boolean` indicating that the
  53927. * listener should be invoked at most once after being added. If `true`,
  53928. * the listener would be automatically removed when invoked.
  53929. * @public
  53930. */
  53931. addEventListener(type, handler, options = {}) {
  53932. for (const listener of this.listeners(type)) {
  53933. if (
  53934. !options[kForOnEventAttribute$1] &&
  53935. listener[kListener$1] === handler &&
  53936. !listener[kForOnEventAttribute$1]
  53937. ) {
  53938. return;
  53939. }
  53940. }
  53941. let wrapper;
  53942. if (type === 'message') {
  53943. wrapper = function onMessage(data, isBinary) {
  53944. const event = new MessageEvent('message', {
  53945. data: isBinary ? data : data.toString()
  53946. });
  53947. event[kTarget] = this;
  53948. callListener(handler, this, event);
  53949. };
  53950. } else if (type === 'close') {
  53951. wrapper = function onClose(code, message) {
  53952. const event = new CloseEvent('close', {
  53953. code,
  53954. reason: message.toString(),
  53955. wasClean: this._closeFrameReceived && this._closeFrameSent
  53956. });
  53957. event[kTarget] = this;
  53958. callListener(handler, this, event);
  53959. };
  53960. } else if (type === 'error') {
  53961. wrapper = function onError(error) {
  53962. const event = new ErrorEvent('error', {
  53963. error,
  53964. message: error.message
  53965. });
  53966. event[kTarget] = this;
  53967. callListener(handler, this, event);
  53968. };
  53969. } else if (type === 'open') {
  53970. wrapper = function onOpen() {
  53971. const event = new Event$1('open');
  53972. event[kTarget] = this;
  53973. callListener(handler, this, event);
  53974. };
  53975. } else {
  53976. return;
  53977. }
  53978. wrapper[kForOnEventAttribute$1] = !!options[kForOnEventAttribute$1];
  53979. wrapper[kListener$1] = handler;
  53980. if (options.once) {
  53981. this.once(type, wrapper);
  53982. } else {
  53983. this.on(type, wrapper);
  53984. }
  53985. },
  53986. /**
  53987. * Remove an event listener.
  53988. *
  53989. * @param {String} type A string representing the event type to remove
  53990. * @param {(Function|Object)} handler The listener to remove
  53991. * @public
  53992. */
  53993. removeEventListener(type, handler) {
  53994. for (const listener of this.listeners(type)) {
  53995. if (listener[kListener$1] === handler && !listener[kForOnEventAttribute$1]) {
  53996. this.removeListener(type, listener);
  53997. break;
  53998. }
  53999. }
  54000. }
  54001. };
  54002. var eventTarget = {
  54003. CloseEvent,
  54004. ErrorEvent,
  54005. Event: Event$1,
  54006. EventTarget,
  54007. MessageEvent
  54008. };
  54009. /**
  54010. * Call an event listener
  54011. *
  54012. * @param {(Function|Object)} listener The listener to call
  54013. * @param {*} thisArg The value to use as `this`` when calling the listener
  54014. * @param {Event} event The event to pass to the listener
  54015. * @private
  54016. */
  54017. function callListener(listener, thisArg, event) {
  54018. if (typeof listener === 'object' && listener.handleEvent) {
  54019. listener.handleEvent.call(listener, event);
  54020. } else {
  54021. listener.call(thisArg, event);
  54022. }
  54023. }
  54024. const { tokenChars: tokenChars$1 } = validationExports;
  54025. /**
  54026. * Adds an offer to the map of extension offers or a parameter to the map of
  54027. * parameters.
  54028. *
  54029. * @param {Object} dest The map of extension offers or parameters
  54030. * @param {String} name The extension or parameter name
  54031. * @param {(Object|Boolean|String)} elem The extension parameters or the
  54032. * parameter value
  54033. * @private
  54034. */
  54035. function push(dest, name, elem) {
  54036. if (dest[name] === undefined) dest[name] = [elem];
  54037. else dest[name].push(elem);
  54038. }
  54039. /**
  54040. * Parses the `Sec-WebSocket-Extensions` header into an object.
  54041. *
  54042. * @param {String} header The field value of the header
  54043. * @return {Object} The parsed object
  54044. * @public
  54045. */
  54046. function parse$2(header) {
  54047. const offers = Object.create(null);
  54048. let params = Object.create(null);
  54049. let mustUnescape = false;
  54050. let isEscaping = false;
  54051. let inQuotes = false;
  54052. let extensionName;
  54053. let paramName;
  54054. let start = -1;
  54055. let code = -1;
  54056. let end = -1;
  54057. let i = 0;
  54058. for (; i < header.length; i++) {
  54059. code = header.charCodeAt(i);
  54060. if (extensionName === undefined) {
  54061. if (end === -1 && tokenChars$1[code] === 1) {
  54062. if (start === -1) start = i;
  54063. } else if (
  54064. i !== 0 &&
  54065. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  54066. ) {
  54067. if (end === -1 && start !== -1) end = i;
  54068. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  54069. if (start === -1) {
  54070. throw new SyntaxError(`Unexpected character at index ${i}`);
  54071. }
  54072. if (end === -1) end = i;
  54073. const name = header.slice(start, end);
  54074. if (code === 0x2c) {
  54075. push(offers, name, params);
  54076. params = Object.create(null);
  54077. } else {
  54078. extensionName = name;
  54079. }
  54080. start = end = -1;
  54081. } else {
  54082. throw new SyntaxError(`Unexpected character at index ${i}`);
  54083. }
  54084. } else if (paramName === undefined) {
  54085. if (end === -1 && tokenChars$1[code] === 1) {
  54086. if (start === -1) start = i;
  54087. } else if (code === 0x20 || code === 0x09) {
  54088. if (end === -1 && start !== -1) end = i;
  54089. } else if (code === 0x3b || code === 0x2c) {
  54090. if (start === -1) {
  54091. throw new SyntaxError(`Unexpected character at index ${i}`);
  54092. }
  54093. if (end === -1) end = i;
  54094. push(params, header.slice(start, end), true);
  54095. if (code === 0x2c) {
  54096. push(offers, extensionName, params);
  54097. params = Object.create(null);
  54098. extensionName = undefined;
  54099. }
  54100. start = end = -1;
  54101. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  54102. paramName = header.slice(start, i);
  54103. start = end = -1;
  54104. } else {
  54105. throw new SyntaxError(`Unexpected character at index ${i}`);
  54106. }
  54107. } else {
  54108. //
  54109. // The value of a quoted-string after unescaping must conform to the
  54110. // token ABNF, so only token characters are valid.
  54111. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  54112. //
  54113. if (isEscaping) {
  54114. if (tokenChars$1[code] !== 1) {
  54115. throw new SyntaxError(`Unexpected character at index ${i}`);
  54116. }
  54117. if (start === -1) start = i;
  54118. else if (!mustUnescape) mustUnescape = true;
  54119. isEscaping = false;
  54120. } else if (inQuotes) {
  54121. if (tokenChars$1[code] === 1) {
  54122. if (start === -1) start = i;
  54123. } else if (code === 0x22 /* '"' */ && start !== -1) {
  54124. inQuotes = false;
  54125. end = i;
  54126. } else if (code === 0x5c /* '\' */) {
  54127. isEscaping = true;
  54128. } else {
  54129. throw new SyntaxError(`Unexpected character at index ${i}`);
  54130. }
  54131. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  54132. inQuotes = true;
  54133. } else if (end === -1 && tokenChars$1[code] === 1) {
  54134. if (start === -1) start = i;
  54135. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  54136. if (end === -1) end = i;
  54137. } else if (code === 0x3b || code === 0x2c) {
  54138. if (start === -1) {
  54139. throw new SyntaxError(`Unexpected character at index ${i}`);
  54140. }
  54141. if (end === -1) end = i;
  54142. let value = header.slice(start, end);
  54143. if (mustUnescape) {
  54144. value = value.replace(/\\/g, '');
  54145. mustUnescape = false;
  54146. }
  54147. push(params, paramName, value);
  54148. if (code === 0x2c) {
  54149. push(offers, extensionName, params);
  54150. params = Object.create(null);
  54151. extensionName = undefined;
  54152. }
  54153. paramName = undefined;
  54154. start = end = -1;
  54155. } else {
  54156. throw new SyntaxError(`Unexpected character at index ${i}`);
  54157. }
  54158. }
  54159. }
  54160. if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {
  54161. throw new SyntaxError('Unexpected end of input');
  54162. }
  54163. if (end === -1) end = i;
  54164. const token = header.slice(start, end);
  54165. if (extensionName === undefined) {
  54166. push(offers, token, params);
  54167. } else {
  54168. if (paramName === undefined) {
  54169. push(params, token, true);
  54170. } else if (mustUnescape) {
  54171. push(params, paramName, token.replace(/\\/g, ''));
  54172. } else {
  54173. push(params, paramName, token);
  54174. }
  54175. push(offers, extensionName, params);
  54176. }
  54177. return offers;
  54178. }
  54179. /**
  54180. * Builds the `Sec-WebSocket-Extensions` header field value.
  54181. *
  54182. * @param {Object} extensions The map of extensions and parameters to format
  54183. * @return {String} A string representing the given object
  54184. * @public
  54185. */
  54186. function format$1(extensions) {
  54187. return Object.keys(extensions)
  54188. .map((extension) => {
  54189. let configurations = extensions[extension];
  54190. if (!Array.isArray(configurations)) configurations = [configurations];
  54191. return configurations
  54192. .map((params) => {
  54193. return [extension]
  54194. .concat(
  54195. Object.keys(params).map((k) => {
  54196. let values = params[k];
  54197. if (!Array.isArray(values)) values = [values];
  54198. return values
  54199. .map((v) => (v === true ? k : `${k}=${v}`))
  54200. .join('; ');
  54201. })
  54202. )
  54203. .join('; ');
  54204. })
  54205. .join(', ');
  54206. })
  54207. .join(', ');
  54208. }
  54209. var extension$1 = { format: format$1, parse: parse$2 };
  54210. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$" }] */
  54211. const EventEmitter$1 = require$$0$5;
  54212. const https$2 = require$$1$1;
  54213. const http$3 = require$$1;
  54214. const net = require$$4$1;
  54215. const tls = require$$4$2;
  54216. const { randomBytes, createHash: createHash$1 } = require$$3$1;
  54217. const { URL: URL$2 } = require$$0$9;
  54218. const PerMessageDeflate$1 = permessageDeflate;
  54219. const Receiver = receiver;
  54220. const Sender = sender;
  54221. const {
  54222. BINARY_TYPES,
  54223. EMPTY_BUFFER,
  54224. GUID: GUID$1,
  54225. kForOnEventAttribute,
  54226. kListener,
  54227. kStatusCode,
  54228. kWebSocket: kWebSocket$1,
  54229. NOOP: NOOP$1
  54230. } = constants;
  54231. const {
  54232. EventTarget: { addEventListener, removeEventListener }
  54233. } = eventTarget;
  54234. const { format, parse: parse$1 } = extension$1;
  54235. const { toBuffer } = bufferUtilExports;
  54236. const closeTimeout = 30 * 1000;
  54237. const kAborted = Symbol('kAborted');
  54238. const protocolVersions = [8, 13];
  54239. const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
  54240. const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
  54241. /**
  54242. * Class representing a WebSocket.
  54243. *
  54244. * @extends EventEmitter
  54245. */
  54246. let WebSocket$1 = class WebSocket extends EventEmitter$1 {
  54247. /**
  54248. * Create a new `WebSocket`.
  54249. *
  54250. * @param {(String|URL)} address The URL to which to connect
  54251. * @param {(String|String[])} [protocols] The subprotocols
  54252. * @param {Object} [options] Connection options
  54253. */
  54254. constructor(address, protocols, options) {
  54255. super();
  54256. this._binaryType = BINARY_TYPES[0];
  54257. this._closeCode = 1006;
  54258. this._closeFrameReceived = false;
  54259. this._closeFrameSent = false;
  54260. this._closeMessage = EMPTY_BUFFER;
  54261. this._closeTimer = null;
  54262. this._extensions = {};
  54263. this._paused = false;
  54264. this._protocol = '';
  54265. this._readyState = WebSocket.CONNECTING;
  54266. this._receiver = null;
  54267. this._sender = null;
  54268. this._socket = null;
  54269. if (address !== null) {
  54270. this._bufferedAmount = 0;
  54271. this._isServer = false;
  54272. this._redirects = 0;
  54273. if (protocols === undefined) {
  54274. protocols = [];
  54275. } else if (!Array.isArray(protocols)) {
  54276. if (typeof protocols === 'object' && protocols !== null) {
  54277. options = protocols;
  54278. protocols = [];
  54279. } else {
  54280. protocols = [protocols];
  54281. }
  54282. }
  54283. initAsClient(this, address, protocols, options);
  54284. } else {
  54285. this._autoPong = options.autoPong;
  54286. this._isServer = true;
  54287. }
  54288. }
  54289. /**
  54290. * This deviates from the WHATWG interface since ws doesn't support the
  54291. * required default "blob" type (instead we define a custom "nodebuffer"
  54292. * type).
  54293. *
  54294. * @type {String}
  54295. */
  54296. get binaryType() {
  54297. return this._binaryType;
  54298. }
  54299. set binaryType(type) {
  54300. if (!BINARY_TYPES.includes(type)) return;
  54301. this._binaryType = type;
  54302. //
  54303. // Allow to change `binaryType` on the fly.
  54304. //
  54305. if (this._receiver) this._receiver._binaryType = type;
  54306. }
  54307. /**
  54308. * @type {Number}
  54309. */
  54310. get bufferedAmount() {
  54311. if (!this._socket) return this._bufferedAmount;
  54312. return this._socket._writableState.length + this._sender._bufferedBytes;
  54313. }
  54314. /**
  54315. * @type {String}
  54316. */
  54317. get extensions() {
  54318. return Object.keys(this._extensions).join();
  54319. }
  54320. /**
  54321. * @type {Boolean}
  54322. */
  54323. get isPaused() {
  54324. return this._paused;
  54325. }
  54326. /**
  54327. * @type {Function}
  54328. */
  54329. /* istanbul ignore next */
  54330. get onclose() {
  54331. return null;
  54332. }
  54333. /**
  54334. * @type {Function}
  54335. */
  54336. /* istanbul ignore next */
  54337. get onerror() {
  54338. return null;
  54339. }
  54340. /**
  54341. * @type {Function}
  54342. */
  54343. /* istanbul ignore next */
  54344. get onopen() {
  54345. return null;
  54346. }
  54347. /**
  54348. * @type {Function}
  54349. */
  54350. /* istanbul ignore next */
  54351. get onmessage() {
  54352. return null;
  54353. }
  54354. /**
  54355. * @type {String}
  54356. */
  54357. get protocol() {
  54358. return this._protocol;
  54359. }
  54360. /**
  54361. * @type {Number}
  54362. */
  54363. get readyState() {
  54364. return this._readyState;
  54365. }
  54366. /**
  54367. * @type {String}
  54368. */
  54369. get url() {
  54370. return this._url;
  54371. }
  54372. /**
  54373. * Set up the socket and the internal resources.
  54374. *
  54375. * @param {Duplex} socket The network socket between the server and client
  54376. * @param {Buffer} head The first packet of the upgraded stream
  54377. * @param {Object} options Options object
  54378. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  54379. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  54380. * multiple times in the same tick
  54381. * @param {Function} [options.generateMask] The function used to generate the
  54382. * masking key
  54383. * @param {Number} [options.maxPayload=0] The maximum allowed message size
  54384. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54385. * not to skip UTF-8 validation for text and close messages
  54386. * @private
  54387. */
  54388. setSocket(socket, head, options) {
  54389. const receiver = new Receiver({
  54390. allowSynchronousEvents: options.allowSynchronousEvents,
  54391. binaryType: this.binaryType,
  54392. extensions: this._extensions,
  54393. isServer: this._isServer,
  54394. maxPayload: options.maxPayload,
  54395. skipUTF8Validation: options.skipUTF8Validation
  54396. });
  54397. this._sender = new Sender(socket, this._extensions, options.generateMask);
  54398. this._receiver = receiver;
  54399. this._socket = socket;
  54400. receiver[kWebSocket$1] = this;
  54401. socket[kWebSocket$1] = this;
  54402. receiver.on('conclude', receiverOnConclude);
  54403. receiver.on('drain', receiverOnDrain);
  54404. receiver.on('error', receiverOnError);
  54405. receiver.on('message', receiverOnMessage);
  54406. receiver.on('ping', receiverOnPing);
  54407. receiver.on('pong', receiverOnPong);
  54408. //
  54409. // These methods may not be available if `socket` is just a `Duplex`.
  54410. //
  54411. if (socket.setTimeout) socket.setTimeout(0);
  54412. if (socket.setNoDelay) socket.setNoDelay();
  54413. if (head.length > 0) socket.unshift(head);
  54414. socket.on('close', socketOnClose);
  54415. socket.on('data', socketOnData);
  54416. socket.on('end', socketOnEnd);
  54417. socket.on('error', socketOnError$1);
  54418. this._readyState = WebSocket.OPEN;
  54419. this.emit('open');
  54420. }
  54421. /**
  54422. * Emit the `'close'` event.
  54423. *
  54424. * @private
  54425. */
  54426. emitClose() {
  54427. if (!this._socket) {
  54428. this._readyState = WebSocket.CLOSED;
  54429. this.emit('close', this._closeCode, this._closeMessage);
  54430. return;
  54431. }
  54432. if (this._extensions[PerMessageDeflate$1.extensionName]) {
  54433. this._extensions[PerMessageDeflate$1.extensionName].cleanup();
  54434. }
  54435. this._receiver.removeAllListeners();
  54436. this._readyState = WebSocket.CLOSED;
  54437. this.emit('close', this._closeCode, this._closeMessage);
  54438. }
  54439. /**
  54440. * Start a closing handshake.
  54441. *
  54442. * +----------+ +-----------+ +----------+
  54443. * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
  54444. * | +----------+ +-----------+ +----------+ |
  54445. * +----------+ +-----------+ |
  54446. * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
  54447. * +----------+ +-----------+ |
  54448. * | | | +---+ |
  54449. * +------------------------+-->|fin| - - - -
  54450. * | +---+ | +---+
  54451. * - - - - -|fin|<---------------------+
  54452. * +---+
  54453. *
  54454. * @param {Number} [code] Status code explaining why the connection is closing
  54455. * @param {(String|Buffer)} [data] The reason why the connection is
  54456. * closing
  54457. * @public
  54458. */
  54459. close(code, data) {
  54460. if (this.readyState === WebSocket.CLOSED) return;
  54461. if (this.readyState === WebSocket.CONNECTING) {
  54462. const msg = 'WebSocket was closed before the connection was established';
  54463. abortHandshake$1(this, this._req, msg);
  54464. return;
  54465. }
  54466. if (this.readyState === WebSocket.CLOSING) {
  54467. if (
  54468. this._closeFrameSent &&
  54469. (this._closeFrameReceived || this._receiver._writableState.errorEmitted)
  54470. ) {
  54471. this._socket.end();
  54472. }
  54473. return;
  54474. }
  54475. this._readyState = WebSocket.CLOSING;
  54476. this._sender.close(code, data, !this._isServer, (err) => {
  54477. //
  54478. // This error is handled by the `'error'` listener on the socket. We only
  54479. // want to know if the close frame has been sent here.
  54480. //
  54481. if (err) return;
  54482. this._closeFrameSent = true;
  54483. if (
  54484. this._closeFrameReceived ||
  54485. this._receiver._writableState.errorEmitted
  54486. ) {
  54487. this._socket.end();
  54488. }
  54489. });
  54490. //
  54491. // Specify a timeout for the closing handshake to complete.
  54492. //
  54493. this._closeTimer = setTimeout(
  54494. this._socket.destroy.bind(this._socket),
  54495. closeTimeout
  54496. );
  54497. }
  54498. /**
  54499. * Pause the socket.
  54500. *
  54501. * @public
  54502. */
  54503. pause() {
  54504. if (
  54505. this.readyState === WebSocket.CONNECTING ||
  54506. this.readyState === WebSocket.CLOSED
  54507. ) {
  54508. return;
  54509. }
  54510. this._paused = true;
  54511. this._socket.pause();
  54512. }
  54513. /**
  54514. * Send a ping.
  54515. *
  54516. * @param {*} [data] The data to send
  54517. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  54518. * @param {Function} [cb] Callback which is executed when the ping is sent
  54519. * @public
  54520. */
  54521. ping(data, mask, cb) {
  54522. if (this.readyState === WebSocket.CONNECTING) {
  54523. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  54524. }
  54525. if (typeof data === 'function') {
  54526. cb = data;
  54527. data = mask = undefined;
  54528. } else if (typeof mask === 'function') {
  54529. cb = mask;
  54530. mask = undefined;
  54531. }
  54532. if (typeof data === 'number') data = data.toString();
  54533. if (this.readyState !== WebSocket.OPEN) {
  54534. sendAfterClose(this, data, cb);
  54535. return;
  54536. }
  54537. if (mask === undefined) mask = !this._isServer;
  54538. this._sender.ping(data || EMPTY_BUFFER, mask, cb);
  54539. }
  54540. /**
  54541. * Send a pong.
  54542. *
  54543. * @param {*} [data] The data to send
  54544. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  54545. * @param {Function} [cb] Callback which is executed when the pong is sent
  54546. * @public
  54547. */
  54548. pong(data, mask, cb) {
  54549. if (this.readyState === WebSocket.CONNECTING) {
  54550. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  54551. }
  54552. if (typeof data === 'function') {
  54553. cb = data;
  54554. data = mask = undefined;
  54555. } else if (typeof mask === 'function') {
  54556. cb = mask;
  54557. mask = undefined;
  54558. }
  54559. if (typeof data === 'number') data = data.toString();
  54560. if (this.readyState !== WebSocket.OPEN) {
  54561. sendAfterClose(this, data, cb);
  54562. return;
  54563. }
  54564. if (mask === undefined) mask = !this._isServer;
  54565. this._sender.pong(data || EMPTY_BUFFER, mask, cb);
  54566. }
  54567. /**
  54568. * Resume the socket.
  54569. *
  54570. * @public
  54571. */
  54572. resume() {
  54573. if (
  54574. this.readyState === WebSocket.CONNECTING ||
  54575. this.readyState === WebSocket.CLOSED
  54576. ) {
  54577. return;
  54578. }
  54579. this._paused = false;
  54580. if (!this._receiver._writableState.needDrain) this._socket.resume();
  54581. }
  54582. /**
  54583. * Send a data message.
  54584. *
  54585. * @param {*} data The message to send
  54586. * @param {Object} [options] Options object
  54587. * @param {Boolean} [options.binary] Specifies whether `data` is binary or
  54588. * text
  54589. * @param {Boolean} [options.compress] Specifies whether or not to compress
  54590. * `data`
  54591. * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
  54592. * last one
  54593. * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
  54594. * @param {Function} [cb] Callback which is executed when data is written out
  54595. * @public
  54596. */
  54597. send(data, options, cb) {
  54598. if (this.readyState === WebSocket.CONNECTING) {
  54599. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  54600. }
  54601. if (typeof options === 'function') {
  54602. cb = options;
  54603. options = {};
  54604. }
  54605. if (typeof data === 'number') data = data.toString();
  54606. if (this.readyState !== WebSocket.OPEN) {
  54607. sendAfterClose(this, data, cb);
  54608. return;
  54609. }
  54610. const opts = {
  54611. binary: typeof data !== 'string',
  54612. mask: !this._isServer,
  54613. compress: true,
  54614. fin: true,
  54615. ...options
  54616. };
  54617. if (!this._extensions[PerMessageDeflate$1.extensionName]) {
  54618. opts.compress = false;
  54619. }
  54620. this._sender.send(data || EMPTY_BUFFER, opts, cb);
  54621. }
  54622. /**
  54623. * Forcibly close the connection.
  54624. *
  54625. * @public
  54626. */
  54627. terminate() {
  54628. if (this.readyState === WebSocket.CLOSED) return;
  54629. if (this.readyState === WebSocket.CONNECTING) {
  54630. const msg = 'WebSocket was closed before the connection was established';
  54631. abortHandshake$1(this, this._req, msg);
  54632. return;
  54633. }
  54634. if (this._socket) {
  54635. this._readyState = WebSocket.CLOSING;
  54636. this._socket.destroy();
  54637. }
  54638. }
  54639. };
  54640. /**
  54641. * @constant {Number} CONNECTING
  54642. * @memberof WebSocket
  54643. */
  54644. Object.defineProperty(WebSocket$1, 'CONNECTING', {
  54645. enumerable: true,
  54646. value: readyStates.indexOf('CONNECTING')
  54647. });
  54648. /**
  54649. * @constant {Number} CONNECTING
  54650. * @memberof WebSocket.prototype
  54651. */
  54652. Object.defineProperty(WebSocket$1.prototype, 'CONNECTING', {
  54653. enumerable: true,
  54654. value: readyStates.indexOf('CONNECTING')
  54655. });
  54656. /**
  54657. * @constant {Number} OPEN
  54658. * @memberof WebSocket
  54659. */
  54660. Object.defineProperty(WebSocket$1, 'OPEN', {
  54661. enumerable: true,
  54662. value: readyStates.indexOf('OPEN')
  54663. });
  54664. /**
  54665. * @constant {Number} OPEN
  54666. * @memberof WebSocket.prototype
  54667. */
  54668. Object.defineProperty(WebSocket$1.prototype, 'OPEN', {
  54669. enumerable: true,
  54670. value: readyStates.indexOf('OPEN')
  54671. });
  54672. /**
  54673. * @constant {Number} CLOSING
  54674. * @memberof WebSocket
  54675. */
  54676. Object.defineProperty(WebSocket$1, 'CLOSING', {
  54677. enumerable: true,
  54678. value: readyStates.indexOf('CLOSING')
  54679. });
  54680. /**
  54681. * @constant {Number} CLOSING
  54682. * @memberof WebSocket.prototype
  54683. */
  54684. Object.defineProperty(WebSocket$1.prototype, 'CLOSING', {
  54685. enumerable: true,
  54686. value: readyStates.indexOf('CLOSING')
  54687. });
  54688. /**
  54689. * @constant {Number} CLOSED
  54690. * @memberof WebSocket
  54691. */
  54692. Object.defineProperty(WebSocket$1, 'CLOSED', {
  54693. enumerable: true,
  54694. value: readyStates.indexOf('CLOSED')
  54695. });
  54696. /**
  54697. * @constant {Number} CLOSED
  54698. * @memberof WebSocket.prototype
  54699. */
  54700. Object.defineProperty(WebSocket$1.prototype, 'CLOSED', {
  54701. enumerable: true,
  54702. value: readyStates.indexOf('CLOSED')
  54703. });
  54704. [
  54705. 'binaryType',
  54706. 'bufferedAmount',
  54707. 'extensions',
  54708. 'isPaused',
  54709. 'protocol',
  54710. 'readyState',
  54711. 'url'
  54712. ].forEach((property) => {
  54713. Object.defineProperty(WebSocket$1.prototype, property, { enumerable: true });
  54714. });
  54715. //
  54716. // Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
  54717. // See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
  54718. //
  54719. ['open', 'error', 'close', 'message'].forEach((method) => {
  54720. Object.defineProperty(WebSocket$1.prototype, `on${method}`, {
  54721. enumerable: true,
  54722. get() {
  54723. for (const listener of this.listeners(method)) {
  54724. if (listener[kForOnEventAttribute]) return listener[kListener];
  54725. }
  54726. return null;
  54727. },
  54728. set(handler) {
  54729. for (const listener of this.listeners(method)) {
  54730. if (listener[kForOnEventAttribute]) {
  54731. this.removeListener(method, listener);
  54732. break;
  54733. }
  54734. }
  54735. if (typeof handler !== 'function') return;
  54736. this.addEventListener(method, handler, {
  54737. [kForOnEventAttribute]: true
  54738. });
  54739. }
  54740. });
  54741. });
  54742. WebSocket$1.prototype.addEventListener = addEventListener;
  54743. WebSocket$1.prototype.removeEventListener = removeEventListener;
  54744. var websocket = WebSocket$1;
  54745. /**
  54746. * Initialize a WebSocket client.
  54747. *
  54748. * @param {WebSocket} websocket The client to initialize
  54749. * @param {(String|URL)} address The URL to which to connect
  54750. * @param {Array} protocols The subprotocols
  54751. * @param {Object} [options] Connection options
  54752. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether any
  54753. * of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
  54754. * times in the same tick
  54755. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  54756. * automatically send a pong in response to a ping
  54757. * @param {Function} [options.finishRequest] A function which can be used to
  54758. * customize the headers of each http request before it is sent
  54759. * @param {Boolean} [options.followRedirects=false] Whether or not to follow
  54760. * redirects
  54761. * @param {Function} [options.generateMask] The function used to generate the
  54762. * masking key
  54763. * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
  54764. * handshake request
  54765. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  54766. * size
  54767. * @param {Number} [options.maxRedirects=10] The maximum number of redirects
  54768. * allowed
  54769. * @param {String} [options.origin] Value of the `Origin` or
  54770. * `Sec-WebSocket-Origin` header
  54771. * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
  54772. * permessage-deflate
  54773. * @param {Number} [options.protocolVersion=13] Value of the
  54774. * `Sec-WebSocket-Version` header
  54775. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54776. * not to skip UTF-8 validation for text and close messages
  54777. * @private
  54778. */
  54779. function initAsClient(websocket, address, protocols, options) {
  54780. const opts = {
  54781. allowSynchronousEvents: false,
  54782. autoPong: true,
  54783. protocolVersion: protocolVersions[1],
  54784. maxPayload: 100 * 1024 * 1024,
  54785. skipUTF8Validation: false,
  54786. perMessageDeflate: true,
  54787. followRedirects: false,
  54788. maxRedirects: 10,
  54789. ...options,
  54790. createConnection: undefined,
  54791. socketPath: undefined,
  54792. hostname: undefined,
  54793. protocol: undefined,
  54794. timeout: undefined,
  54795. method: 'GET',
  54796. host: undefined,
  54797. path: undefined,
  54798. port: undefined
  54799. };
  54800. websocket._autoPong = opts.autoPong;
  54801. if (!protocolVersions.includes(opts.protocolVersion)) {
  54802. throw new RangeError(
  54803. `Unsupported protocol version: ${opts.protocolVersion} ` +
  54804. `(supported versions: ${protocolVersions.join(', ')})`
  54805. );
  54806. }
  54807. let parsedUrl;
  54808. if (address instanceof URL$2) {
  54809. parsedUrl = address;
  54810. } else {
  54811. try {
  54812. parsedUrl = new URL$2(address);
  54813. } catch (e) {
  54814. throw new SyntaxError(`Invalid URL: ${address}`);
  54815. }
  54816. }
  54817. if (parsedUrl.protocol === 'http:') {
  54818. parsedUrl.protocol = 'ws:';
  54819. } else if (parsedUrl.protocol === 'https:') {
  54820. parsedUrl.protocol = 'wss:';
  54821. }
  54822. websocket._url = parsedUrl.href;
  54823. const isSecure = parsedUrl.protocol === 'wss:';
  54824. const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
  54825. let invalidUrlMessage;
  54826. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {
  54827. invalidUrlMessage =
  54828. 'The URL\'s protocol must be one of "ws:", "wss:", ' +
  54829. '"http:", "https", or "ws+unix:"';
  54830. } else if (isIpcUrl && !parsedUrl.pathname) {
  54831. invalidUrlMessage = "The URL's pathname is empty";
  54832. } else if (parsedUrl.hash) {
  54833. invalidUrlMessage = 'The URL contains a fragment identifier';
  54834. }
  54835. if (invalidUrlMessage) {
  54836. const err = new SyntaxError(invalidUrlMessage);
  54837. if (websocket._redirects === 0) {
  54838. throw err;
  54839. } else {
  54840. emitErrorAndClose(websocket, err);
  54841. return;
  54842. }
  54843. }
  54844. const defaultPort = isSecure ? 443 : 80;
  54845. const key = randomBytes(16).toString('base64');
  54846. const request = isSecure ? https$2.request : http$3.request;
  54847. const protocolSet = new Set();
  54848. let perMessageDeflate;
  54849. opts.createConnection = isSecure ? tlsConnect : netConnect;
  54850. opts.defaultPort = opts.defaultPort || defaultPort;
  54851. opts.port = parsedUrl.port || defaultPort;
  54852. opts.host = parsedUrl.hostname.startsWith('[')
  54853. ? parsedUrl.hostname.slice(1, -1)
  54854. : parsedUrl.hostname;
  54855. opts.headers = {
  54856. ...opts.headers,
  54857. 'Sec-WebSocket-Version': opts.protocolVersion,
  54858. 'Sec-WebSocket-Key': key,
  54859. Connection: 'Upgrade',
  54860. Upgrade: 'websocket'
  54861. };
  54862. opts.path = parsedUrl.pathname + parsedUrl.search;
  54863. opts.timeout = opts.handshakeTimeout;
  54864. if (opts.perMessageDeflate) {
  54865. perMessageDeflate = new PerMessageDeflate$1(
  54866. opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
  54867. false,
  54868. opts.maxPayload
  54869. );
  54870. opts.headers['Sec-WebSocket-Extensions'] = format({
  54871. [PerMessageDeflate$1.extensionName]: perMessageDeflate.offer()
  54872. });
  54873. }
  54874. if (protocols.length) {
  54875. for (const protocol of protocols) {
  54876. if (
  54877. typeof protocol !== 'string' ||
  54878. !subprotocolRegex.test(protocol) ||
  54879. protocolSet.has(protocol)
  54880. ) {
  54881. throw new SyntaxError(
  54882. 'An invalid or duplicated subprotocol was specified'
  54883. );
  54884. }
  54885. protocolSet.add(protocol);
  54886. }
  54887. opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
  54888. }
  54889. if (opts.origin) {
  54890. if (opts.protocolVersion < 13) {
  54891. opts.headers['Sec-WebSocket-Origin'] = opts.origin;
  54892. } else {
  54893. opts.headers.Origin = opts.origin;
  54894. }
  54895. }
  54896. if (parsedUrl.username || parsedUrl.password) {
  54897. opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
  54898. }
  54899. if (isIpcUrl) {
  54900. const parts = opts.path.split(':');
  54901. opts.socketPath = parts[0];
  54902. opts.path = parts[1];
  54903. }
  54904. let req;
  54905. if (opts.followRedirects) {
  54906. if (websocket._redirects === 0) {
  54907. websocket._originalIpc = isIpcUrl;
  54908. websocket._originalSecure = isSecure;
  54909. websocket._originalHostOrSocketPath = isIpcUrl
  54910. ? opts.socketPath
  54911. : parsedUrl.host;
  54912. const headers = options && options.headers;
  54913. //
  54914. // Shallow copy the user provided options so that headers can be changed
  54915. // without mutating the original object.
  54916. //
  54917. options = { ...options, headers: {} };
  54918. if (headers) {
  54919. for (const [key, value] of Object.entries(headers)) {
  54920. options.headers[key.toLowerCase()] = value;
  54921. }
  54922. }
  54923. } else if (websocket.listenerCount('redirect') === 0) {
  54924. const isSameHost = isIpcUrl
  54925. ? websocket._originalIpc
  54926. ? opts.socketPath === websocket._originalHostOrSocketPath
  54927. : false
  54928. : websocket._originalIpc
  54929. ? false
  54930. : parsedUrl.host === websocket._originalHostOrSocketPath;
  54931. if (!isSameHost || (websocket._originalSecure && !isSecure)) {
  54932. //
  54933. // Match curl 7.77.0 behavior and drop the following headers. These
  54934. // headers are also dropped when following a redirect to a subdomain.
  54935. //
  54936. delete opts.headers.authorization;
  54937. delete opts.headers.cookie;
  54938. if (!isSameHost) delete opts.headers.host;
  54939. opts.auth = undefined;
  54940. }
  54941. }
  54942. //
  54943. // Match curl 7.77.0 behavior and make the first `Authorization` header win.
  54944. // If the `Authorization` header is set, then there is nothing to do as it
  54945. // will take precedence.
  54946. //
  54947. if (opts.auth && !options.headers.authorization) {
  54948. options.headers.authorization =
  54949. 'Basic ' + Buffer.from(opts.auth).toString('base64');
  54950. }
  54951. req = websocket._req = request(opts);
  54952. if (websocket._redirects) {
  54953. //
  54954. // Unlike what is done for the `'upgrade'` event, no early exit is
  54955. // triggered here if the user calls `websocket.close()` or
  54956. // `websocket.terminate()` from a listener of the `'redirect'` event. This
  54957. // is because the user can also call `request.destroy()` with an error
  54958. // before calling `websocket.close()` or `websocket.terminate()` and this
  54959. // would result in an error being emitted on the `request` object with no
  54960. // `'error'` event listeners attached.
  54961. //
  54962. websocket.emit('redirect', websocket.url, req);
  54963. }
  54964. } else {
  54965. req = websocket._req = request(opts);
  54966. }
  54967. if (opts.timeout) {
  54968. req.on('timeout', () => {
  54969. abortHandshake$1(websocket, req, 'Opening handshake has timed out');
  54970. });
  54971. }
  54972. req.on('error', (err) => {
  54973. if (req === null || req[kAborted]) return;
  54974. req = websocket._req = null;
  54975. emitErrorAndClose(websocket, err);
  54976. });
  54977. req.on('response', (res) => {
  54978. const location = res.headers.location;
  54979. const statusCode = res.statusCode;
  54980. if (
  54981. location &&
  54982. opts.followRedirects &&
  54983. statusCode >= 300 &&
  54984. statusCode < 400
  54985. ) {
  54986. if (++websocket._redirects > opts.maxRedirects) {
  54987. abortHandshake$1(websocket, req, 'Maximum redirects exceeded');
  54988. return;
  54989. }
  54990. req.abort();
  54991. let addr;
  54992. try {
  54993. addr = new URL$2(location, address);
  54994. } catch (e) {
  54995. const err = new SyntaxError(`Invalid URL: ${location}`);
  54996. emitErrorAndClose(websocket, err);
  54997. return;
  54998. }
  54999. initAsClient(websocket, addr, protocols, options);
  55000. } else if (!websocket.emit('unexpected-response', req, res)) {
  55001. abortHandshake$1(
  55002. websocket,
  55003. req,
  55004. `Unexpected server response: ${res.statusCode}`
  55005. );
  55006. }
  55007. });
  55008. req.on('upgrade', (res, socket, head) => {
  55009. websocket.emit('upgrade', res);
  55010. //
  55011. // The user may have closed the connection from a listener of the
  55012. // `'upgrade'` event.
  55013. //
  55014. if (websocket.readyState !== WebSocket$1.CONNECTING) return;
  55015. req = websocket._req = null;
  55016. if (res.headers.upgrade.toLowerCase() !== 'websocket') {
  55017. abortHandshake$1(websocket, socket, 'Invalid Upgrade header');
  55018. return;
  55019. }
  55020. const digest = createHash$1('sha1')
  55021. .update(key + GUID$1)
  55022. .digest('base64');
  55023. if (res.headers['sec-websocket-accept'] !== digest) {
  55024. abortHandshake$1(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  55025. return;
  55026. }
  55027. const serverProt = res.headers['sec-websocket-protocol'];
  55028. let protError;
  55029. if (serverProt !== undefined) {
  55030. if (!protocolSet.size) {
  55031. protError = 'Server sent a subprotocol but none was requested';
  55032. } else if (!protocolSet.has(serverProt)) {
  55033. protError = 'Server sent an invalid subprotocol';
  55034. }
  55035. } else if (protocolSet.size) {
  55036. protError = 'Server sent no subprotocol';
  55037. }
  55038. if (protError) {
  55039. abortHandshake$1(websocket, socket, protError);
  55040. return;
  55041. }
  55042. if (serverProt) websocket._protocol = serverProt;
  55043. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  55044. if (secWebSocketExtensions !== undefined) {
  55045. if (!perMessageDeflate) {
  55046. const message =
  55047. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  55048. 'was requested';
  55049. abortHandshake$1(websocket, socket, message);
  55050. return;
  55051. }
  55052. let extensions;
  55053. try {
  55054. extensions = parse$1(secWebSocketExtensions);
  55055. } catch (err) {
  55056. const message = 'Invalid Sec-WebSocket-Extensions header';
  55057. abortHandshake$1(websocket, socket, message);
  55058. return;
  55059. }
  55060. const extensionNames = Object.keys(extensions);
  55061. if (
  55062. extensionNames.length !== 1 ||
  55063. extensionNames[0] !== PerMessageDeflate$1.extensionName
  55064. ) {
  55065. const message = 'Server indicated an extension that was not requested';
  55066. abortHandshake$1(websocket, socket, message);
  55067. return;
  55068. }
  55069. try {
  55070. perMessageDeflate.accept(extensions[PerMessageDeflate$1.extensionName]);
  55071. } catch (err) {
  55072. const message = 'Invalid Sec-WebSocket-Extensions header';
  55073. abortHandshake$1(websocket, socket, message);
  55074. return;
  55075. }
  55076. websocket._extensions[PerMessageDeflate$1.extensionName] =
  55077. perMessageDeflate;
  55078. }
  55079. websocket.setSocket(socket, head, {
  55080. allowSynchronousEvents: opts.allowSynchronousEvents,
  55081. generateMask: opts.generateMask,
  55082. maxPayload: opts.maxPayload,
  55083. skipUTF8Validation: opts.skipUTF8Validation
  55084. });
  55085. });
  55086. if (opts.finishRequest) {
  55087. opts.finishRequest(req, websocket);
  55088. } else {
  55089. req.end();
  55090. }
  55091. }
  55092. /**
  55093. * Emit the `'error'` and `'close'` events.
  55094. *
  55095. * @param {WebSocket} websocket The WebSocket instance
  55096. * @param {Error} The error to emit
  55097. * @private
  55098. */
  55099. function emitErrorAndClose(websocket, err) {
  55100. websocket._readyState = WebSocket$1.CLOSING;
  55101. websocket.emit('error', err);
  55102. websocket.emitClose();
  55103. }
  55104. /**
  55105. * Create a `net.Socket` and initiate a connection.
  55106. *
  55107. * @param {Object} options Connection options
  55108. * @return {net.Socket} The newly created socket used to start the connection
  55109. * @private
  55110. */
  55111. function netConnect(options) {
  55112. options.path = options.socketPath;
  55113. return net.connect(options);
  55114. }
  55115. /**
  55116. * Create a `tls.TLSSocket` and initiate a connection.
  55117. *
  55118. * @param {Object} options Connection options
  55119. * @return {tls.TLSSocket} The newly created socket used to start the connection
  55120. * @private
  55121. */
  55122. function tlsConnect(options) {
  55123. options.path = undefined;
  55124. if (!options.servername && options.servername !== '') {
  55125. options.servername = net.isIP(options.host) ? '' : options.host;
  55126. }
  55127. return tls.connect(options);
  55128. }
  55129. /**
  55130. * Abort the handshake and emit an error.
  55131. *
  55132. * @param {WebSocket} websocket The WebSocket instance
  55133. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  55134. * abort or the socket to destroy
  55135. * @param {String} message The error message
  55136. * @private
  55137. */
  55138. function abortHandshake$1(websocket, stream, message) {
  55139. websocket._readyState = WebSocket$1.CLOSING;
  55140. const err = new Error(message);
  55141. Error.captureStackTrace(err, abortHandshake$1);
  55142. if (stream.setHeader) {
  55143. stream[kAborted] = true;
  55144. stream.abort();
  55145. if (stream.socket && !stream.socket.destroyed) {
  55146. //
  55147. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  55148. // called after the request completed. See
  55149. // https://github.com/websockets/ws/issues/1869.
  55150. //
  55151. stream.socket.destroy();
  55152. }
  55153. process.nextTick(emitErrorAndClose, websocket, err);
  55154. } else {
  55155. stream.destroy(err);
  55156. stream.once('error', websocket.emit.bind(websocket, 'error'));
  55157. stream.once('close', websocket.emitClose.bind(websocket));
  55158. }
  55159. }
  55160. /**
  55161. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  55162. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  55163. *
  55164. * @param {WebSocket} websocket The WebSocket instance
  55165. * @param {*} [data] The data to send
  55166. * @param {Function} [cb] Callback
  55167. * @private
  55168. */
  55169. function sendAfterClose(websocket, data, cb) {
  55170. if (data) {
  55171. const length = toBuffer(data).length;
  55172. //
  55173. // The `_bufferedAmount` property is used only when the peer is a client and
  55174. // the opening handshake fails. Under these circumstances, in fact, the
  55175. // `setSocket()` method is not called, so the `_socket` and `_sender`
  55176. // properties are set to `null`.
  55177. //
  55178. if (websocket._socket) websocket._sender._bufferedBytes += length;
  55179. else websocket._bufferedAmount += length;
  55180. }
  55181. if (cb) {
  55182. const err = new Error(
  55183. `WebSocket is not open: readyState ${websocket.readyState} ` +
  55184. `(${readyStates[websocket.readyState]})`
  55185. );
  55186. process.nextTick(cb, err);
  55187. }
  55188. }
  55189. /**
  55190. * The listener of the `Receiver` `'conclude'` event.
  55191. *
  55192. * @param {Number} code The status code
  55193. * @param {Buffer} reason The reason for closing
  55194. * @private
  55195. */
  55196. function receiverOnConclude(code, reason) {
  55197. const websocket = this[kWebSocket$1];
  55198. websocket._closeFrameReceived = true;
  55199. websocket._closeMessage = reason;
  55200. websocket._closeCode = code;
  55201. if (websocket._socket[kWebSocket$1] === undefined) return;
  55202. websocket._socket.removeListener('data', socketOnData);
  55203. process.nextTick(resume, websocket._socket);
  55204. if (code === 1005) websocket.close();
  55205. else websocket.close(code, reason);
  55206. }
  55207. /**
  55208. * The listener of the `Receiver` `'drain'` event.
  55209. *
  55210. * @private
  55211. */
  55212. function receiverOnDrain() {
  55213. const websocket = this[kWebSocket$1];
  55214. if (!websocket.isPaused) websocket._socket.resume();
  55215. }
  55216. /**
  55217. * The listener of the `Receiver` `'error'` event.
  55218. *
  55219. * @param {(RangeError|Error)} err The emitted error
  55220. * @private
  55221. */
  55222. function receiverOnError(err) {
  55223. const websocket = this[kWebSocket$1];
  55224. if (websocket._socket[kWebSocket$1] !== undefined) {
  55225. websocket._socket.removeListener('data', socketOnData);
  55226. //
  55227. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  55228. // https://github.com/websockets/ws/issues/1940.
  55229. //
  55230. process.nextTick(resume, websocket._socket);
  55231. websocket.close(err[kStatusCode]);
  55232. }
  55233. websocket.emit('error', err);
  55234. }
  55235. /**
  55236. * The listener of the `Receiver` `'finish'` event.
  55237. *
  55238. * @private
  55239. */
  55240. function receiverOnFinish() {
  55241. this[kWebSocket$1].emitClose();
  55242. }
  55243. /**
  55244. * The listener of the `Receiver` `'message'` event.
  55245. *
  55246. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  55247. * @param {Boolean} isBinary Specifies whether the message is binary or not
  55248. * @private
  55249. */
  55250. function receiverOnMessage(data, isBinary) {
  55251. this[kWebSocket$1].emit('message', data, isBinary);
  55252. }
  55253. /**
  55254. * The listener of the `Receiver` `'ping'` event.
  55255. *
  55256. * @param {Buffer} data The data included in the ping frame
  55257. * @private
  55258. */
  55259. function receiverOnPing(data) {
  55260. const websocket = this[kWebSocket$1];
  55261. if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP$1);
  55262. websocket.emit('ping', data);
  55263. }
  55264. /**
  55265. * The listener of the `Receiver` `'pong'` event.
  55266. *
  55267. * @param {Buffer} data The data included in the pong frame
  55268. * @private
  55269. */
  55270. function receiverOnPong(data) {
  55271. this[kWebSocket$1].emit('pong', data);
  55272. }
  55273. /**
  55274. * Resume a readable stream
  55275. *
  55276. * @param {Readable} stream The readable stream
  55277. * @private
  55278. */
  55279. function resume(stream) {
  55280. stream.resume();
  55281. }
  55282. /**
  55283. * The listener of the socket `'close'` event.
  55284. *
  55285. * @private
  55286. */
  55287. function socketOnClose() {
  55288. const websocket = this[kWebSocket$1];
  55289. this.removeListener('close', socketOnClose);
  55290. this.removeListener('data', socketOnData);
  55291. this.removeListener('end', socketOnEnd);
  55292. websocket._readyState = WebSocket$1.CLOSING;
  55293. let chunk;
  55294. //
  55295. // The close frame might not have been received or the `'end'` event emitted,
  55296. // for example, if the socket was destroyed due to an error. Ensure that the
  55297. // `receiver` stream is closed after writing any remaining buffered data to
  55298. // it. If the readable side of the socket is in flowing mode then there is no
  55299. // buffered data as everything has been already written and `readable.read()`
  55300. // will return `null`. If instead, the socket is paused, any possible buffered
  55301. // data will be read as a single chunk.
  55302. //
  55303. if (
  55304. !this._readableState.endEmitted &&
  55305. !websocket._closeFrameReceived &&
  55306. !websocket._receiver._writableState.errorEmitted &&
  55307. (chunk = websocket._socket.read()) !== null
  55308. ) {
  55309. websocket._receiver.write(chunk);
  55310. }
  55311. websocket._receiver.end();
  55312. this[kWebSocket$1] = undefined;
  55313. clearTimeout(websocket._closeTimer);
  55314. if (
  55315. websocket._receiver._writableState.finished ||
  55316. websocket._receiver._writableState.errorEmitted
  55317. ) {
  55318. websocket.emitClose();
  55319. } else {
  55320. websocket._receiver.on('error', receiverOnFinish);
  55321. websocket._receiver.on('finish', receiverOnFinish);
  55322. }
  55323. }
  55324. /**
  55325. * The listener of the socket `'data'` event.
  55326. *
  55327. * @param {Buffer} chunk A chunk of data
  55328. * @private
  55329. */
  55330. function socketOnData(chunk) {
  55331. if (!this[kWebSocket$1]._receiver.write(chunk)) {
  55332. this.pause();
  55333. }
  55334. }
  55335. /**
  55336. * The listener of the socket `'end'` event.
  55337. *
  55338. * @private
  55339. */
  55340. function socketOnEnd() {
  55341. const websocket = this[kWebSocket$1];
  55342. websocket._readyState = WebSocket$1.CLOSING;
  55343. websocket._receiver.end();
  55344. this.end();
  55345. }
  55346. /**
  55347. * The listener of the socket `'error'` event.
  55348. *
  55349. * @private
  55350. */
  55351. function socketOnError$1() {
  55352. const websocket = this[kWebSocket$1];
  55353. this.removeListener('error', socketOnError$1);
  55354. this.on('error', NOOP$1);
  55355. if (websocket) {
  55356. websocket._readyState = WebSocket$1.CLOSING;
  55357. this.destroy();
  55358. }
  55359. }
  55360. const { tokenChars } = validationExports;
  55361. /**
  55362. * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
  55363. *
  55364. * @param {String} header The field value of the header
  55365. * @return {Set} The subprotocol names
  55366. * @public
  55367. */
  55368. function parse(header) {
  55369. const protocols = new Set();
  55370. let start = -1;
  55371. let end = -1;
  55372. let i = 0;
  55373. for (i; i < header.length; i++) {
  55374. const code = header.charCodeAt(i);
  55375. if (end === -1 && tokenChars[code] === 1) {
  55376. if (start === -1) start = i;
  55377. } else if (
  55378. i !== 0 &&
  55379. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  55380. ) {
  55381. if (end === -1 && start !== -1) end = i;
  55382. } else if (code === 0x2c /* ',' */) {
  55383. if (start === -1) {
  55384. throw new SyntaxError(`Unexpected character at index ${i}`);
  55385. }
  55386. if (end === -1) end = i;
  55387. const protocol = header.slice(start, end);
  55388. if (protocols.has(protocol)) {
  55389. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  55390. }
  55391. protocols.add(protocol);
  55392. start = end = -1;
  55393. } else {
  55394. throw new SyntaxError(`Unexpected character at index ${i}`);
  55395. }
  55396. }
  55397. if (start === -1 || end !== -1) {
  55398. throw new SyntaxError('Unexpected end of input');
  55399. }
  55400. const protocol = header.slice(start, i);
  55401. if (protocols.has(protocol)) {
  55402. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  55403. }
  55404. protocols.add(protocol);
  55405. return protocols;
  55406. }
  55407. var subprotocol$1 = { parse };
  55408. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$" }] */
  55409. const EventEmitter = require$$0$5;
  55410. const http$2 = require$$1;
  55411. const { createHash } = require$$3$1;
  55412. const extension = extension$1;
  55413. const PerMessageDeflate = permessageDeflate;
  55414. const subprotocol = subprotocol$1;
  55415. const WebSocket = websocket;
  55416. const { GUID, kWebSocket } = constants;
  55417. const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
  55418. const RUNNING = 0;
  55419. const CLOSING = 1;
  55420. const CLOSED = 2;
  55421. /**
  55422. * Class representing a WebSocket server.
  55423. *
  55424. * @extends EventEmitter
  55425. */
  55426. class WebSocketServer extends EventEmitter {
  55427. /**
  55428. * Create a `WebSocketServer` instance.
  55429. *
  55430. * @param {Object} options Configuration options
  55431. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  55432. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  55433. * multiple times in the same tick
  55434. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  55435. * automatically send a pong in response to a ping
  55436. * @param {Number} [options.backlog=511] The maximum length of the queue of
  55437. * pending connections
  55438. * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
  55439. * track clients
  55440. * @param {Function} [options.handleProtocols] A hook to handle protocols
  55441. * @param {String} [options.host] The hostname where to bind the server
  55442. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  55443. * size
  55444. * @param {Boolean} [options.noServer=false] Enable no server mode
  55445. * @param {String} [options.path] Accept only connections matching this path
  55446. * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
  55447. * permessage-deflate
  55448. * @param {Number} [options.port] The port where to bind the server
  55449. * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
  55450. * server to use
  55451. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  55452. * not to skip UTF-8 validation for text and close messages
  55453. * @param {Function} [options.verifyClient] A hook to reject connections
  55454. * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
  55455. * class to use. It must be the `WebSocket` class or class that extends it
  55456. * @param {Function} [callback] A listener for the `listening` event
  55457. */
  55458. constructor(options, callback) {
  55459. super();
  55460. options = {
  55461. allowSynchronousEvents: false,
  55462. autoPong: true,
  55463. maxPayload: 100 * 1024 * 1024,
  55464. skipUTF8Validation: false,
  55465. perMessageDeflate: false,
  55466. handleProtocols: null,
  55467. clientTracking: true,
  55468. verifyClient: null,
  55469. noServer: false,
  55470. backlog: null, // use default (511 as implemented in net.js)
  55471. server: null,
  55472. host: null,
  55473. path: null,
  55474. port: null,
  55475. WebSocket,
  55476. ...options
  55477. };
  55478. if (
  55479. (options.port == null && !options.server && !options.noServer) ||
  55480. (options.port != null && (options.server || options.noServer)) ||
  55481. (options.server && options.noServer)
  55482. ) {
  55483. throw new TypeError(
  55484. 'One and only one of the "port", "server", or "noServer" options ' +
  55485. 'must be specified'
  55486. );
  55487. }
  55488. if (options.port != null) {
  55489. this._server = http$2.createServer((req, res) => {
  55490. const body = http$2.STATUS_CODES[426];
  55491. res.writeHead(426, {
  55492. 'Content-Length': body.length,
  55493. 'Content-Type': 'text/plain'
  55494. });
  55495. res.end(body);
  55496. });
  55497. this._server.listen(
  55498. options.port,
  55499. options.host,
  55500. options.backlog,
  55501. callback
  55502. );
  55503. } else if (options.server) {
  55504. this._server = options.server;
  55505. }
  55506. if (this._server) {
  55507. const emitConnection = this.emit.bind(this, 'connection');
  55508. this._removeListeners = addListeners(this._server, {
  55509. listening: this.emit.bind(this, 'listening'),
  55510. error: this.emit.bind(this, 'error'),
  55511. upgrade: (req, socket, head) => {
  55512. this.handleUpgrade(req, socket, head, emitConnection);
  55513. }
  55514. });
  55515. }
  55516. if (options.perMessageDeflate === true) options.perMessageDeflate = {};
  55517. if (options.clientTracking) {
  55518. this.clients = new Set();
  55519. this._shouldEmitClose = false;
  55520. }
  55521. this.options = options;
  55522. this._state = RUNNING;
  55523. }
  55524. /**
  55525. * Returns the bound address, the address family name, and port of the server
  55526. * as reported by the operating system if listening on an IP socket.
  55527. * If the server is listening on a pipe or UNIX domain socket, the name is
  55528. * returned as a string.
  55529. *
  55530. * @return {(Object|String|null)} The address of the server
  55531. * @public
  55532. */
  55533. address() {
  55534. if (this.options.noServer) {
  55535. throw new Error('The server is operating in "noServer" mode');
  55536. }
  55537. if (!this._server) return null;
  55538. return this._server.address();
  55539. }
  55540. /**
  55541. * Stop the server from accepting new connections and emit the `'close'` event
  55542. * when all existing connections are closed.
  55543. *
  55544. * @param {Function} [cb] A one-time listener for the `'close'` event
  55545. * @public
  55546. */
  55547. close(cb) {
  55548. if (this._state === CLOSED) {
  55549. if (cb) {
  55550. this.once('close', () => {
  55551. cb(new Error('The server is not running'));
  55552. });
  55553. }
  55554. process.nextTick(emitClose, this);
  55555. return;
  55556. }
  55557. if (cb) this.once('close', cb);
  55558. if (this._state === CLOSING) return;
  55559. this._state = CLOSING;
  55560. if (this.options.noServer || this.options.server) {
  55561. if (this._server) {
  55562. this._removeListeners();
  55563. this._removeListeners = this._server = null;
  55564. }
  55565. if (this.clients) {
  55566. if (!this.clients.size) {
  55567. process.nextTick(emitClose, this);
  55568. } else {
  55569. this._shouldEmitClose = true;
  55570. }
  55571. } else {
  55572. process.nextTick(emitClose, this);
  55573. }
  55574. } else {
  55575. const server = this._server;
  55576. this._removeListeners();
  55577. this._removeListeners = this._server = null;
  55578. //
  55579. // The HTTP/S server was created internally. Close it, and rely on its
  55580. // `'close'` event.
  55581. //
  55582. server.close(() => {
  55583. emitClose(this);
  55584. });
  55585. }
  55586. }
  55587. /**
  55588. * See if a given request should be handled by this server instance.
  55589. *
  55590. * @param {http.IncomingMessage} req Request object to inspect
  55591. * @return {Boolean} `true` if the request is valid, else `false`
  55592. * @public
  55593. */
  55594. shouldHandle(req) {
  55595. if (this.options.path) {
  55596. const index = req.url.indexOf('?');
  55597. const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
  55598. if (pathname !== this.options.path) return false;
  55599. }
  55600. return true;
  55601. }
  55602. /**
  55603. * Handle a HTTP Upgrade request.
  55604. *
  55605. * @param {http.IncomingMessage} req The request object
  55606. * @param {Duplex} socket The network socket between the server and client
  55607. * @param {Buffer} head The first packet of the upgraded stream
  55608. * @param {Function} cb Callback
  55609. * @public
  55610. */
  55611. handleUpgrade(req, socket, head, cb) {
  55612. socket.on('error', socketOnError);
  55613. const key = req.headers['sec-websocket-key'];
  55614. const version = +req.headers['sec-websocket-version'];
  55615. if (req.method !== 'GET') {
  55616. const message = 'Invalid HTTP method';
  55617. abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
  55618. return;
  55619. }
  55620. if (req.headers.upgrade.toLowerCase() !== 'websocket') {
  55621. const message = 'Invalid Upgrade header';
  55622. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55623. return;
  55624. }
  55625. if (!key || !keyRegex.test(key)) {
  55626. const message = 'Missing or invalid Sec-WebSocket-Key header';
  55627. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55628. return;
  55629. }
  55630. if (version !== 8 && version !== 13) {
  55631. const message = 'Missing or invalid Sec-WebSocket-Version header';
  55632. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55633. return;
  55634. }
  55635. if (!this.shouldHandle(req)) {
  55636. abortHandshake(socket, 400);
  55637. return;
  55638. }
  55639. const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
  55640. let protocols = new Set();
  55641. if (secWebSocketProtocol !== undefined) {
  55642. try {
  55643. protocols = subprotocol.parse(secWebSocketProtocol);
  55644. } catch (err) {
  55645. const message = 'Invalid Sec-WebSocket-Protocol header';
  55646. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55647. return;
  55648. }
  55649. }
  55650. const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
  55651. const extensions = {};
  55652. if (
  55653. this.options.perMessageDeflate &&
  55654. secWebSocketExtensions !== undefined
  55655. ) {
  55656. const perMessageDeflate = new PerMessageDeflate(
  55657. this.options.perMessageDeflate,
  55658. true,
  55659. this.options.maxPayload
  55660. );
  55661. try {
  55662. const offers = extension.parse(secWebSocketExtensions);
  55663. if (offers[PerMessageDeflate.extensionName]) {
  55664. perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
  55665. extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
  55666. }
  55667. } catch (err) {
  55668. const message =
  55669. 'Invalid or unacceptable Sec-WebSocket-Extensions header';
  55670. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55671. return;
  55672. }
  55673. }
  55674. //
  55675. // Optionally call external client verification handler.
  55676. //
  55677. if (this.options.verifyClient) {
  55678. const info = {
  55679. origin:
  55680. req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
  55681. secure: !!(req.socket.authorized || req.socket.encrypted),
  55682. req
  55683. };
  55684. if (this.options.verifyClient.length === 2) {
  55685. this.options.verifyClient(info, (verified, code, message, headers) => {
  55686. if (!verified) {
  55687. return abortHandshake(socket, code || 401, message, headers);
  55688. }
  55689. this.completeUpgrade(
  55690. extensions,
  55691. key,
  55692. protocols,
  55693. req,
  55694. socket,
  55695. head,
  55696. cb
  55697. );
  55698. });
  55699. return;
  55700. }
  55701. if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
  55702. }
  55703. this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
  55704. }
  55705. /**
  55706. * Upgrade the connection to WebSocket.
  55707. *
  55708. * @param {Object} extensions The accepted extensions
  55709. * @param {String} key The value of the `Sec-WebSocket-Key` header
  55710. * @param {Set} protocols The subprotocols
  55711. * @param {http.IncomingMessage} req The request object
  55712. * @param {Duplex} socket The network socket between the server and client
  55713. * @param {Buffer} head The first packet of the upgraded stream
  55714. * @param {Function} cb Callback
  55715. * @throws {Error} If called more than once with the same socket
  55716. * @private
  55717. */
  55718. completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
  55719. //
  55720. // Destroy the socket if the client has already sent a FIN packet.
  55721. //
  55722. if (!socket.readable || !socket.writable) return socket.destroy();
  55723. if (socket[kWebSocket]) {
  55724. throw new Error(
  55725. 'server.handleUpgrade() was called more than once with the same ' +
  55726. 'socket, possibly due to a misconfiguration'
  55727. );
  55728. }
  55729. if (this._state > RUNNING) return abortHandshake(socket, 503);
  55730. const digest = createHash('sha1')
  55731. .update(key + GUID)
  55732. .digest('base64');
  55733. const headers = [
  55734. 'HTTP/1.1 101 Switching Protocols',
  55735. 'Upgrade: websocket',
  55736. 'Connection: Upgrade',
  55737. `Sec-WebSocket-Accept: ${digest}`
  55738. ];
  55739. const ws = new this.options.WebSocket(null, undefined, this.options);
  55740. if (protocols.size) {
  55741. //
  55742. // Optionally call external protocol selection handler.
  55743. //
  55744. const protocol = this.options.handleProtocols
  55745. ? this.options.handleProtocols(protocols, req)
  55746. : protocols.values().next().value;
  55747. if (protocol) {
  55748. headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
  55749. ws._protocol = protocol;
  55750. }
  55751. }
  55752. if (extensions[PerMessageDeflate.extensionName]) {
  55753. const params = extensions[PerMessageDeflate.extensionName].params;
  55754. const value = extension.format({
  55755. [PerMessageDeflate.extensionName]: [params]
  55756. });
  55757. headers.push(`Sec-WebSocket-Extensions: ${value}`);
  55758. ws._extensions = extensions;
  55759. }
  55760. //
  55761. // Allow external modification/inspection of handshake headers.
  55762. //
  55763. this.emit('headers', headers, req);
  55764. socket.write(headers.concat('\r\n').join('\r\n'));
  55765. socket.removeListener('error', socketOnError);
  55766. ws.setSocket(socket, head, {
  55767. allowSynchronousEvents: this.options.allowSynchronousEvents,
  55768. maxPayload: this.options.maxPayload,
  55769. skipUTF8Validation: this.options.skipUTF8Validation
  55770. });
  55771. if (this.clients) {
  55772. this.clients.add(ws);
  55773. ws.on('close', () => {
  55774. this.clients.delete(ws);
  55775. if (this._shouldEmitClose && !this.clients.size) {
  55776. process.nextTick(emitClose, this);
  55777. }
  55778. });
  55779. }
  55780. cb(ws, req);
  55781. }
  55782. }
  55783. var websocketServer = WebSocketServer;
  55784. /**
  55785. * Add event listeners on an `EventEmitter` using a map of <event, listener>
  55786. * pairs.
  55787. *
  55788. * @param {EventEmitter} server The event emitter
  55789. * @param {Object.<String, Function>} map The listeners to add
  55790. * @return {Function} A function that will remove the added listeners when
  55791. * called
  55792. * @private
  55793. */
  55794. function addListeners(server, map) {
  55795. for (const event of Object.keys(map)) server.on(event, map[event]);
  55796. return function removeListeners() {
  55797. for (const event of Object.keys(map)) {
  55798. server.removeListener(event, map[event]);
  55799. }
  55800. };
  55801. }
  55802. /**
  55803. * Emit a `'close'` event on an `EventEmitter`.
  55804. *
  55805. * @param {EventEmitter} server The event emitter
  55806. * @private
  55807. */
  55808. function emitClose(server) {
  55809. server._state = CLOSED;
  55810. server.emit('close');
  55811. }
  55812. /**
  55813. * Handle socket errors.
  55814. *
  55815. * @private
  55816. */
  55817. function socketOnError() {
  55818. this.destroy();
  55819. }
  55820. /**
  55821. * Close the connection when preconditions are not fulfilled.
  55822. *
  55823. * @param {Duplex} socket The socket of the upgrade request
  55824. * @param {Number} code The HTTP response status code
  55825. * @param {String} [message] The HTTP response body
  55826. * @param {Object} [headers] Additional HTTP response headers
  55827. * @private
  55828. */
  55829. function abortHandshake(socket, code, message, headers) {
  55830. //
  55831. // The socket is writable unless the user destroyed or ended it before calling
  55832. // `server.handleUpgrade()` or in the `verifyClient` function, which is a user
  55833. // error. Handling this does not make much sense as the worst that can happen
  55834. // is that some of the data written by the user might be discarded due to the
  55835. // call to `socket.end()` below, which triggers an `'error'` event that in
  55836. // turn causes the socket to be destroyed.
  55837. //
  55838. message = message || http$2.STATUS_CODES[code];
  55839. headers = {
  55840. Connection: 'close',
  55841. 'Content-Type': 'text/html',
  55842. 'Content-Length': Buffer.byteLength(message),
  55843. ...headers
  55844. };
  55845. socket.once('finish', socket.destroy);
  55846. socket.end(
  55847. `HTTP/1.1 ${code} ${http$2.STATUS_CODES[code]}\r\n` +
  55848. Object.keys(headers)
  55849. .map((h) => `${h}: ${headers[h]}`)
  55850. .join('\r\n') +
  55851. '\r\n\r\n' +
  55852. message
  55853. );
  55854. }
  55855. /**
  55856. * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
  55857. * one listener for it, otherwise call `abortHandshake()`.
  55858. *
  55859. * @param {WebSocketServer} server The WebSocket server
  55860. * @param {http.IncomingMessage} req The request object
  55861. * @param {Duplex} socket The socket of the upgrade request
  55862. * @param {Number} code The HTTP response status code
  55863. * @param {String} message The HTTP response body
  55864. * @private
  55865. */
  55866. function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
  55867. if (server.listenerCount('wsClientError')) {
  55868. const err = new Error(message);
  55869. Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
  55870. server.emit('wsClientError', err, socket, req);
  55871. } else {
  55872. abortHandshake(socket, code, message);
  55873. }
  55874. }
  55875. var WebSocketServerRaw_ = /*@__PURE__*/getDefaultExportFromCjs(websocketServer);
  55876. /* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version
  55877. * of `ws` will not work as Bun's req.socket does not allow reading/writing to the underlying socket.
  55878. */
  55879. const WebSocketServerRaw = process.versions.bun
  55880. ? // @ts-expect-error: Bun defines `import.meta.require`
  55881. import.meta.require('ws').WebSocketServer
  55882. : WebSocketServerRaw_;
  55883. const HMR_HEADER = 'vite-hmr';
  55884. const wsServerEvents = [
  55885. 'connection',
  55886. 'error',
  55887. 'headers',
  55888. 'listening',
  55889. 'message',
  55890. ];
  55891. function createWebSocketServer(server, config, httpsOptions) {
  55892. let wss;
  55893. let wsHttpServer = undefined;
  55894. const hmr = isObject$1(config.server.hmr) && config.server.hmr;
  55895. const hmrServer = hmr && hmr.server;
  55896. const hmrPort = hmr && hmr.port;
  55897. // TODO: the main server port may not have been chosen yet as it may use the next available
  55898. const portsAreCompatible = !hmrPort || hmrPort === config.server.port;
  55899. const wsServer = hmrServer || (portsAreCompatible && server);
  55900. let hmrServerWsListener;
  55901. const customListeners = new Map();
  55902. const clientsMap = new WeakMap();
  55903. const port = hmrPort || 24678;
  55904. const host = (hmr && hmr.host) || undefined;
  55905. if (wsServer) {
  55906. let hmrBase = config.base;
  55907. const hmrPath = hmr ? hmr.path : undefined;
  55908. if (hmrPath) {
  55909. hmrBase = path$o.posix.join(hmrBase, hmrPath);
  55910. }
  55911. wss = new WebSocketServerRaw({ noServer: true });
  55912. hmrServerWsListener = (req, socket, head) => {
  55913. if (req.headers['sec-websocket-protocol'] === HMR_HEADER &&
  55914. req.url === hmrBase) {
  55915. wss.handleUpgrade(req, socket, head, (ws) => {
  55916. wss.emit('connection', ws, req);
  55917. });
  55918. }
  55919. };
  55920. wsServer.on('upgrade', hmrServerWsListener);
  55921. }
  55922. else {
  55923. // http server request handler keeps the same with
  55924. // https://github.com/websockets/ws/blob/45e17acea791d865df6b255a55182e9c42e5877a/lib/websocket-server.js#L88-L96
  55925. const route = ((_, res) => {
  55926. const statusCode = 426;
  55927. const body = STATUS_CODES[statusCode];
  55928. if (!body)
  55929. throw new Error(`No body text found for the ${statusCode} status code`);
  55930. res.writeHead(statusCode, {
  55931. 'Content-Length': body.length,
  55932. 'Content-Type': 'text/plain',
  55933. });
  55934. res.end(body);
  55935. });
  55936. if (httpsOptions) {
  55937. wsHttpServer = createServer$2(httpsOptions, route);
  55938. }
  55939. else {
  55940. wsHttpServer = createServer$3(route);
  55941. }
  55942. // vite dev server in middleware mode
  55943. // need to call ws listen manually
  55944. wss = new WebSocketServerRaw({ server: wsHttpServer });
  55945. }
  55946. wss.on('connection', (socket) => {
  55947. socket.on('message', (raw) => {
  55948. if (!customListeners.size)
  55949. return;
  55950. let parsed;
  55951. try {
  55952. parsed = JSON.parse(String(raw));
  55953. }
  55954. catch { }
  55955. if (!parsed || parsed.type !== 'custom' || !parsed.event)
  55956. return;
  55957. const listeners = customListeners.get(parsed.event);
  55958. if (!listeners?.size)
  55959. return;
  55960. const client = getSocketClient(socket);
  55961. listeners.forEach((listener) => listener(parsed.data, client));
  55962. });
  55963. socket.on('error', (err) => {
  55964. config.logger.error(`${colors$1.red(`ws error:`)}\n${err.stack}`, {
  55965. timestamp: true,
  55966. error: err,
  55967. });
  55968. });
  55969. socket.send(JSON.stringify({ type: 'connected' }));
  55970. if (bufferedError) {
  55971. socket.send(JSON.stringify(bufferedError));
  55972. bufferedError = null;
  55973. }
  55974. });
  55975. wss.on('error', (e) => {
  55976. if (e.code === 'EADDRINUSE') {
  55977. config.logger.error(colors$1.red(`WebSocket server error: Port is already in use`), { error: e });
  55978. }
  55979. else {
  55980. config.logger.error(colors$1.red(`WebSocket server error:\n${e.stack || e.message}`), { error: e });
  55981. }
  55982. });
  55983. // Provide a wrapper to the ws client so we can send messages in JSON format
  55984. // To be consistent with server.ws.send
  55985. function getSocketClient(socket) {
  55986. if (!clientsMap.has(socket)) {
  55987. clientsMap.set(socket, {
  55988. send: (...args) => {
  55989. let payload;
  55990. if (typeof args[0] === 'string') {
  55991. payload = {
  55992. type: 'custom',
  55993. event: args[0],
  55994. data: args[1],
  55995. };
  55996. }
  55997. else {
  55998. payload = args[0];
  55999. }
  56000. socket.send(JSON.stringify(payload));
  56001. },
  56002. socket,
  56003. });
  56004. }
  56005. return clientsMap.get(socket);
  56006. }
  56007. // On page reloads, if a file fails to compile and returns 500, the server
  56008. // sends the error payload before the client connection is established.
  56009. // If we have no open clients, buffer the error and send it to the next
  56010. // connected client.
  56011. let bufferedError = null;
  56012. return {
  56013. name: 'ws',
  56014. listen: () => {
  56015. wsHttpServer?.listen(port, host);
  56016. },
  56017. on: ((event, fn) => {
  56018. if (wsServerEvents.includes(event))
  56019. wss.on(event, fn);
  56020. else {
  56021. if (!customListeners.has(event)) {
  56022. customListeners.set(event, new Set());
  56023. }
  56024. customListeners.get(event).add(fn);
  56025. }
  56026. }),
  56027. off: ((event, fn) => {
  56028. if (wsServerEvents.includes(event)) {
  56029. wss.off(event, fn);
  56030. }
  56031. else {
  56032. customListeners.get(event)?.delete(fn);
  56033. }
  56034. }),
  56035. get clients() {
  56036. return new Set(Array.from(wss.clients).map(getSocketClient));
  56037. },
  56038. send(...args) {
  56039. let payload;
  56040. if (typeof args[0] === 'string') {
  56041. payload = {
  56042. type: 'custom',
  56043. event: args[0],
  56044. data: args[1],
  56045. };
  56046. }
  56047. else {
  56048. payload = args[0];
  56049. }
  56050. if (payload.type === 'error' && !wss.clients.size) {
  56051. bufferedError = payload;
  56052. return;
  56053. }
  56054. const stringified = JSON.stringify(payload);
  56055. wss.clients.forEach((client) => {
  56056. // readyState 1 means the connection is open
  56057. if (client.readyState === 1) {
  56058. client.send(stringified);
  56059. }
  56060. });
  56061. },
  56062. close() {
  56063. // should remove listener if hmr.server is set
  56064. // otherwise the old listener swallows all WebSocket connections
  56065. if (hmrServerWsListener && wsServer) {
  56066. wsServer.off('upgrade', hmrServerWsListener);
  56067. }
  56068. return new Promise((resolve, reject) => {
  56069. wss.clients.forEach((client) => {
  56070. client.terminate();
  56071. });
  56072. wss.close((err) => {
  56073. if (err) {
  56074. reject(err);
  56075. }
  56076. else {
  56077. if (wsHttpServer) {
  56078. wsHttpServer.close((err) => {
  56079. if (err) {
  56080. reject(err);
  56081. }
  56082. else {
  56083. resolve();
  56084. }
  56085. });
  56086. }
  56087. else {
  56088. resolve();
  56089. }
  56090. }
  56091. });
  56092. });
  56093. },
  56094. };
  56095. }
  56096. // this middleware is only active when (base !== '/')
  56097. function baseMiddleware(rawBase, middlewareMode) {
  56098. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  56099. return function viteBaseMiddleware(req, res, next) {
  56100. const url = req.url;
  56101. const pathname = cleanUrl(url);
  56102. const base = rawBase;
  56103. if (pathname.startsWith(base)) {
  56104. // rewrite url to remove base. this ensures that other middleware does
  56105. // not need to consider base being prepended or not
  56106. req.url = stripBase(url, base);
  56107. return next();
  56108. }
  56109. // skip redirect and error fallback on middleware mode, #4057
  56110. if (middlewareMode) {
  56111. return next();
  56112. }
  56113. if (pathname === '/' || pathname === '/index.html') {
  56114. // redirect root visit to based url with search and hash
  56115. res.writeHead(302, {
  56116. Location: base + url.slice(pathname.length),
  56117. });
  56118. res.end();
  56119. return;
  56120. }
  56121. // non-based page visit
  56122. const redirectPath = withTrailingSlash(url) !== base ? joinUrlSegments(base, url) : base;
  56123. if (req.headers.accept?.includes('text/html')) {
  56124. res.writeHead(404, {
  56125. 'Content-Type': 'text/html',
  56126. });
  56127. res.end(`The server is configured with a public base URL of ${base} - ` +
  56128. `did you mean to visit <a href="${redirectPath}">${redirectPath}</a> instead?`);
  56129. return;
  56130. }
  56131. else {
  56132. // not found for resources
  56133. res.writeHead(404, {
  56134. 'Content-Type': 'text/plain',
  56135. });
  56136. res.end(`The server is configured with a public base URL of ${base} - ` +
  56137. `did you mean to visit ${redirectPath} instead?`);
  56138. return;
  56139. }
  56140. };
  56141. }
  56142. var httpProxy$3 = {exports: {}};
  56143. var eventemitter3 = {exports: {}};
  56144. (function (module) {
  56145. var has = Object.prototype.hasOwnProperty
  56146. , prefix = '~';
  56147. /**
  56148. * Constructor to create a storage for our `EE` objects.
  56149. * An `Events` instance is a plain object whose properties are event names.
  56150. *
  56151. * @constructor
  56152. * @private
  56153. */
  56154. function Events() {}
  56155. //
  56156. // We try to not inherit from `Object.prototype`. In some engines creating an
  56157. // instance in this way is faster than calling `Object.create(null)` directly.
  56158. // If `Object.create(null)` is not supported we prefix the event names with a
  56159. // character to make sure that the built-in object properties are not
  56160. // overridden or used as an attack vector.
  56161. //
  56162. if (Object.create) {
  56163. Events.prototype = Object.create(null);
  56164. //
  56165. // This hack is needed because the `__proto__` property is still inherited in
  56166. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  56167. //
  56168. if (!new Events().__proto__) prefix = false;
  56169. }
  56170. /**
  56171. * Representation of a single event listener.
  56172. *
  56173. * @param {Function} fn The listener function.
  56174. * @param {*} context The context to invoke the listener with.
  56175. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  56176. * @constructor
  56177. * @private
  56178. */
  56179. function EE(fn, context, once) {
  56180. this.fn = fn;
  56181. this.context = context;
  56182. this.once = once || false;
  56183. }
  56184. /**
  56185. * Add a listener for a given event.
  56186. *
  56187. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  56188. * @param {(String|Symbol)} event The event name.
  56189. * @param {Function} fn The listener function.
  56190. * @param {*} context The context to invoke the listener with.
  56191. * @param {Boolean} once Specify if the listener is a one-time listener.
  56192. * @returns {EventEmitter}
  56193. * @private
  56194. */
  56195. function addListener(emitter, event, fn, context, once) {
  56196. if (typeof fn !== 'function') {
  56197. throw new TypeError('The listener must be a function');
  56198. }
  56199. var listener = new EE(fn, context || emitter, once)
  56200. , evt = prefix ? prefix + event : event;
  56201. if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
  56202. else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
  56203. else emitter._events[evt] = [emitter._events[evt], listener];
  56204. return emitter;
  56205. }
  56206. /**
  56207. * Clear event by name.
  56208. *
  56209. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  56210. * @param {(String|Symbol)} evt The Event name.
  56211. * @private
  56212. */
  56213. function clearEvent(emitter, evt) {
  56214. if (--emitter._eventsCount === 0) emitter._events = new Events();
  56215. else delete emitter._events[evt];
  56216. }
  56217. /**
  56218. * Minimal `EventEmitter` interface that is molded against the Node.js
  56219. * `EventEmitter` interface.
  56220. *
  56221. * @constructor
  56222. * @public
  56223. */
  56224. function EventEmitter() {
  56225. this._events = new Events();
  56226. this._eventsCount = 0;
  56227. }
  56228. /**
  56229. * Return an array listing the events for which the emitter has registered
  56230. * listeners.
  56231. *
  56232. * @returns {Array}
  56233. * @public
  56234. */
  56235. EventEmitter.prototype.eventNames = function eventNames() {
  56236. var names = []
  56237. , events
  56238. , name;
  56239. if (this._eventsCount === 0) return names;
  56240. for (name in (events = this._events)) {
  56241. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  56242. }
  56243. if (Object.getOwnPropertySymbols) {
  56244. return names.concat(Object.getOwnPropertySymbols(events));
  56245. }
  56246. return names;
  56247. };
  56248. /**
  56249. * Return the listeners registered for a given event.
  56250. *
  56251. * @param {(String|Symbol)} event The event name.
  56252. * @returns {Array} The registered listeners.
  56253. * @public
  56254. */
  56255. EventEmitter.prototype.listeners = function listeners(event) {
  56256. var evt = prefix ? prefix + event : event
  56257. , handlers = this._events[evt];
  56258. if (!handlers) return [];
  56259. if (handlers.fn) return [handlers.fn];
  56260. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  56261. ee[i] = handlers[i].fn;
  56262. }
  56263. return ee;
  56264. };
  56265. /**
  56266. * Return the number of listeners listening to a given event.
  56267. *
  56268. * @param {(String|Symbol)} event The event name.
  56269. * @returns {Number} The number of listeners.
  56270. * @public
  56271. */
  56272. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  56273. var evt = prefix ? prefix + event : event
  56274. , listeners = this._events[evt];
  56275. if (!listeners) return 0;
  56276. if (listeners.fn) return 1;
  56277. return listeners.length;
  56278. };
  56279. /**
  56280. * Calls each of the listeners registered for a given event.
  56281. *
  56282. * @param {(String|Symbol)} event The event name.
  56283. * @returns {Boolean} `true` if the event had listeners, else `false`.
  56284. * @public
  56285. */
  56286. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  56287. var evt = prefix ? prefix + event : event;
  56288. if (!this._events[evt]) return false;
  56289. var listeners = this._events[evt]
  56290. , len = arguments.length
  56291. , args
  56292. , i;
  56293. if (listeners.fn) {
  56294. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  56295. switch (len) {
  56296. case 1: return listeners.fn.call(listeners.context), true;
  56297. case 2: return listeners.fn.call(listeners.context, a1), true;
  56298. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  56299. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  56300. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  56301. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  56302. }
  56303. for (i = 1, args = new Array(len -1); i < len; i++) {
  56304. args[i - 1] = arguments[i];
  56305. }
  56306. listeners.fn.apply(listeners.context, args);
  56307. } else {
  56308. var length = listeners.length
  56309. , j;
  56310. for (i = 0; i < length; i++) {
  56311. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  56312. switch (len) {
  56313. case 1: listeners[i].fn.call(listeners[i].context); break;
  56314. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  56315. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  56316. case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
  56317. default:
  56318. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  56319. args[j - 1] = arguments[j];
  56320. }
  56321. listeners[i].fn.apply(listeners[i].context, args);
  56322. }
  56323. }
  56324. }
  56325. return true;
  56326. };
  56327. /**
  56328. * Add a listener for a given event.
  56329. *
  56330. * @param {(String|Symbol)} event The event name.
  56331. * @param {Function} fn The listener function.
  56332. * @param {*} [context=this] The context to invoke the listener with.
  56333. * @returns {EventEmitter} `this`.
  56334. * @public
  56335. */
  56336. EventEmitter.prototype.on = function on(event, fn, context) {
  56337. return addListener(this, event, fn, context, false);
  56338. };
  56339. /**
  56340. * Add a one-time listener for a given event.
  56341. *
  56342. * @param {(String|Symbol)} event The event name.
  56343. * @param {Function} fn The listener function.
  56344. * @param {*} [context=this] The context to invoke the listener with.
  56345. * @returns {EventEmitter} `this`.
  56346. * @public
  56347. */
  56348. EventEmitter.prototype.once = function once(event, fn, context) {
  56349. return addListener(this, event, fn, context, true);
  56350. };
  56351. /**
  56352. * Remove the listeners of a given event.
  56353. *
  56354. * @param {(String|Symbol)} event The event name.
  56355. * @param {Function} fn Only remove the listeners that match this function.
  56356. * @param {*} context Only remove the listeners that have this context.
  56357. * @param {Boolean} once Only remove one-time listeners.
  56358. * @returns {EventEmitter} `this`.
  56359. * @public
  56360. */
  56361. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  56362. var evt = prefix ? prefix + event : event;
  56363. if (!this._events[evt]) return this;
  56364. if (!fn) {
  56365. clearEvent(this, evt);
  56366. return this;
  56367. }
  56368. var listeners = this._events[evt];
  56369. if (listeners.fn) {
  56370. if (
  56371. listeners.fn === fn &&
  56372. (!once || listeners.once) &&
  56373. (!context || listeners.context === context)
  56374. ) {
  56375. clearEvent(this, evt);
  56376. }
  56377. } else {
  56378. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  56379. if (
  56380. listeners[i].fn !== fn ||
  56381. (once && !listeners[i].once) ||
  56382. (context && listeners[i].context !== context)
  56383. ) {
  56384. events.push(listeners[i]);
  56385. }
  56386. }
  56387. //
  56388. // Reset the array, or remove it completely if we have no more listeners.
  56389. //
  56390. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
  56391. else clearEvent(this, evt);
  56392. }
  56393. return this;
  56394. };
  56395. /**
  56396. * Remove all listeners, or those of the specified event.
  56397. *
  56398. * @param {(String|Symbol)} [event] The event name.
  56399. * @returns {EventEmitter} `this`.
  56400. * @public
  56401. */
  56402. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  56403. var evt;
  56404. if (event) {
  56405. evt = prefix ? prefix + event : event;
  56406. if (this._events[evt]) clearEvent(this, evt);
  56407. } else {
  56408. this._events = new Events();
  56409. this._eventsCount = 0;
  56410. }
  56411. return this;
  56412. };
  56413. //
  56414. // Alias methods names because people roll like that.
  56415. //
  56416. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  56417. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  56418. //
  56419. // Expose the prefix.
  56420. //
  56421. EventEmitter.prefixed = prefix;
  56422. //
  56423. // Allow `EventEmitter` to be imported as module namespace.
  56424. //
  56425. EventEmitter.EventEmitter = EventEmitter;
  56426. //
  56427. // Expose the module.
  56428. //
  56429. {
  56430. module.exports = EventEmitter;
  56431. }
  56432. } (eventemitter3));
  56433. var eventemitter3Exports = eventemitter3.exports;
  56434. var common$3 = {};
  56435. /**
  56436. * Check if we're required to add a port number.
  56437. *
  56438. * @see https://url.spec.whatwg.org/#default-port
  56439. * @param {Number|String} port Port number we need to check
  56440. * @param {String} protocol Protocol we need to check against.
  56441. * @returns {Boolean} Is it a default port for the given protocol
  56442. * @api private
  56443. */
  56444. var requiresPort = function required(port, protocol) {
  56445. protocol = protocol.split(':')[0];
  56446. port = +port;
  56447. if (!port) return false;
  56448. switch (protocol) {
  56449. case 'http':
  56450. case 'ws':
  56451. return port !== 80;
  56452. case 'https':
  56453. case 'wss':
  56454. return port !== 443;
  56455. case 'ftp':
  56456. return port !== 21;
  56457. case 'gopher':
  56458. return port !== 70;
  56459. case 'file':
  56460. return false;
  56461. }
  56462. return port !== 0;
  56463. };
  56464. (function (exports) {
  56465. var common = exports,
  56466. url = require$$0$9,
  56467. extend = require$$0$6._extend,
  56468. required = requiresPort;
  56469. var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
  56470. isSSL = /^https|wss/;
  56471. /**
  56472. * Simple Regex for testing if protocol is https
  56473. */
  56474. common.isSSL = isSSL;
  56475. /**
  56476. * Copies the right headers from `options` and `req` to
  56477. * `outgoing` which is then used to fire the proxied
  56478. * request.
  56479. *
  56480. * Examples:
  56481. *
  56482. * common.setupOutgoing(outgoing, options, req)
  56483. * // => { host: ..., hostname: ...}
  56484. *
  56485. * @param {Object} Outgoing Base object to be filled with required properties
  56486. * @param {Object} Options Config object passed to the proxy
  56487. * @param {ClientRequest} Req Request Object
  56488. * @param {String} Forward String to select forward or target
  56489. * @return {Object} Outgoing Object with all required properties set
  56490. *
  56491. * @api private
  56492. */
  56493. common.setupOutgoing = function(outgoing, options, req, forward) {
  56494. outgoing.port = options[forward || 'target'].port ||
  56495. (isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
  56496. ['host', 'hostname', 'socketPath', 'pfx', 'key',
  56497. 'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
  56498. function(e) { outgoing[e] = options[forward || 'target'][e]; }
  56499. );
  56500. outgoing.method = options.method || req.method;
  56501. outgoing.headers = extend({}, req.headers);
  56502. if (options.headers){
  56503. extend(outgoing.headers, options.headers);
  56504. }
  56505. if (options.auth) {
  56506. outgoing.auth = options.auth;
  56507. }
  56508. if (options.ca) {
  56509. outgoing.ca = options.ca;
  56510. }
  56511. if (isSSL.test(options[forward || 'target'].protocol)) {
  56512. outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
  56513. }
  56514. outgoing.agent = options.agent || false;
  56515. outgoing.localAddress = options.localAddress;
  56516. //
  56517. // Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
  56518. // as node core doesn't handle this COMPLETELY properly yet.
  56519. //
  56520. if (!outgoing.agent) {
  56521. outgoing.headers = outgoing.headers || {};
  56522. if (typeof outgoing.headers.connection !== 'string'
  56523. || !upgradeHeader.test(outgoing.headers.connection)
  56524. ) { outgoing.headers.connection = 'close'; }
  56525. }
  56526. // the final path is target path + relative path requested by user:
  56527. var target = options[forward || 'target'];
  56528. var targetPath = target && options.prependPath !== false
  56529. ? (target.path || '')
  56530. : '';
  56531. //
  56532. // Remark: Can we somehow not use url.parse as a perf optimization?
  56533. //
  56534. var outgoingPath = !options.toProxy
  56535. ? (url.parse(req.url).path || '')
  56536. : req.url;
  56537. //
  56538. // Remark: ignorePath will just straight up ignore whatever the request's
  56539. // path is. This can be labeled as FOOT-GUN material if you do not know what
  56540. // you are doing and are using conflicting options.
  56541. //
  56542. outgoingPath = !options.ignorePath ? outgoingPath : '';
  56543. outgoing.path = common.urlJoin(targetPath, outgoingPath);
  56544. if (options.changeOrigin) {
  56545. outgoing.headers.host =
  56546. required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
  56547. ? outgoing.host + ':' + outgoing.port
  56548. : outgoing.host;
  56549. }
  56550. return outgoing;
  56551. };
  56552. /**
  56553. * Set the proper configuration for sockets,
  56554. * set no delay and set keep alive, also set
  56555. * the timeout to 0.
  56556. *
  56557. * Examples:
  56558. *
  56559. * common.setupSocket(socket)
  56560. * // => Socket
  56561. *
  56562. * @param {Socket} Socket instance to setup
  56563. * @return {Socket} Return the configured socket.
  56564. *
  56565. * @api private
  56566. */
  56567. common.setupSocket = function(socket) {
  56568. socket.setTimeout(0);
  56569. socket.setNoDelay(true);
  56570. socket.setKeepAlive(true, 0);
  56571. return socket;
  56572. };
  56573. /**
  56574. * Get the port number from the host. Or guess it based on the connection type.
  56575. *
  56576. * @param {Request} req Incoming HTTP request.
  56577. *
  56578. * @return {String} The port number.
  56579. *
  56580. * @api private
  56581. */
  56582. common.getPort = function(req) {
  56583. var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
  56584. return res ?
  56585. res[1] :
  56586. common.hasEncryptedConnection(req) ? '443' : '80';
  56587. };
  56588. /**
  56589. * Check if the request has an encrypted connection.
  56590. *
  56591. * @param {Request} req Incoming HTTP request.
  56592. *
  56593. * @return {Boolean} Whether the connection is encrypted or not.
  56594. *
  56595. * @api private
  56596. */
  56597. common.hasEncryptedConnection = function(req) {
  56598. return Boolean(req.connection.encrypted || req.connection.pair);
  56599. };
  56600. /**
  56601. * OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
  56602. *
  56603. * @return {String} The generated path.
  56604. *
  56605. * @api private
  56606. */
  56607. common.urlJoin = function() {
  56608. //
  56609. // We do not want to mess with the query string. All we want to touch is the path.
  56610. //
  56611. var args = Array.prototype.slice.call(arguments),
  56612. lastIndex = args.length - 1,
  56613. last = args[lastIndex],
  56614. lastSegs = last.split('?'),
  56615. retSegs;
  56616. args[lastIndex] = lastSegs.shift();
  56617. //
  56618. // Join all strings, but remove empty strings so we don't get extra slashes from
  56619. // joining e.g. ['', 'am']
  56620. //
  56621. retSegs = [
  56622. args.filter(Boolean).join('/')
  56623. .replace(/\/+/g, '/')
  56624. .replace('http:/', 'http://')
  56625. .replace('https:/', 'https://')
  56626. ];
  56627. // Only join the query string if it exists so we don't have trailing a '?'
  56628. // on every request
  56629. // Handle case where there could be multiple ? in the URL.
  56630. retSegs.push.apply(retSegs, lastSegs);
  56631. return retSegs.join('?')
  56632. };
  56633. /**
  56634. * Rewrites or removes the domain of a cookie header
  56635. *
  56636. * @param {String|Array} Header
  56637. * @param {Object} Config, mapping of domain to rewritten domain.
  56638. * '*' key to match any domain, null value to remove the domain.
  56639. *
  56640. * @api private
  56641. */
  56642. common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
  56643. if (Array.isArray(header)) {
  56644. return header.map(function (headerElement) {
  56645. return rewriteCookieProperty(headerElement, config, property);
  56646. });
  56647. }
  56648. return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
  56649. var newValue;
  56650. if (previousValue in config) {
  56651. newValue = config[previousValue];
  56652. } else if ('*' in config) {
  56653. newValue = config['*'];
  56654. } else {
  56655. //no match, return previous value
  56656. return match;
  56657. }
  56658. if (newValue) {
  56659. //replace value
  56660. return prefix + newValue;
  56661. } else {
  56662. //remove value
  56663. return '';
  56664. }
  56665. });
  56666. };
  56667. /**
  56668. * Check the host and see if it potentially has a port in it (keep it simple)
  56669. *
  56670. * @returns {Boolean} Whether we have one or not
  56671. *
  56672. * @api private
  56673. */
  56674. function hasPort(host) {
  56675. return !!~host.indexOf(':');
  56676. }} (common$3));
  56677. var url$1 = require$$0$9,
  56678. common$2 = common$3;
  56679. var redirectRegex = /^201|30(1|2|7|8)$/;
  56680. /*!
  56681. * Array of passes.
  56682. *
  56683. * A `pass` is just a function that is executed on `req, res, options`
  56684. * so that you can easily add new checks while still keeping the base
  56685. * flexible.
  56686. */
  56687. var webOutgoing = { // <--
  56688. /**
  56689. * If is a HTTP 1.0 request, remove chunk headers
  56690. *
  56691. * @param {ClientRequest} Req Request object
  56692. * @param {IncomingMessage} Res Response object
  56693. * @param {proxyResponse} Res Response object from the proxy request
  56694. *
  56695. * @api private
  56696. */
  56697. removeChunked: function removeChunked(req, res, proxyRes) {
  56698. if (req.httpVersion === '1.0') {
  56699. delete proxyRes.headers['transfer-encoding'];
  56700. }
  56701. },
  56702. /**
  56703. * If is a HTTP 1.0 request, set the correct connection header
  56704. * or if connection header not present, then use `keep-alive`
  56705. *
  56706. * @param {ClientRequest} Req Request object
  56707. * @param {IncomingMessage} Res Response object
  56708. * @param {proxyResponse} Res Response object from the proxy request
  56709. *
  56710. * @api private
  56711. */
  56712. setConnection: function setConnection(req, res, proxyRes) {
  56713. if (req.httpVersion === '1.0') {
  56714. proxyRes.headers.connection = req.headers.connection || 'close';
  56715. } else if (req.httpVersion !== '2.0' && !proxyRes.headers.connection) {
  56716. proxyRes.headers.connection = req.headers.connection || 'keep-alive';
  56717. }
  56718. },
  56719. setRedirectHostRewrite: function setRedirectHostRewrite(req, res, proxyRes, options) {
  56720. if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
  56721. && proxyRes.headers['location']
  56722. && redirectRegex.test(proxyRes.statusCode)) {
  56723. var target = url$1.parse(options.target);
  56724. var u = url$1.parse(proxyRes.headers['location']);
  56725. // make sure the redirected host matches the target host before rewriting
  56726. if (target.host != u.host) {
  56727. return;
  56728. }
  56729. if (options.hostRewrite) {
  56730. u.host = options.hostRewrite;
  56731. } else if (options.autoRewrite) {
  56732. u.host = req.headers['host'];
  56733. }
  56734. if (options.protocolRewrite) {
  56735. u.protocol = options.protocolRewrite;
  56736. }
  56737. proxyRes.headers['location'] = u.format();
  56738. }
  56739. },
  56740. /**
  56741. * Copy headers from proxyResponse to response
  56742. * set each header in response object.
  56743. *
  56744. * @param {ClientRequest} Req Request object
  56745. * @param {IncomingMessage} Res Response object
  56746. * @param {proxyResponse} Res Response object from the proxy request
  56747. * @param {Object} Options options.cookieDomainRewrite: Config to rewrite cookie domain
  56748. *
  56749. * @api private
  56750. */
  56751. writeHeaders: function writeHeaders(req, res, proxyRes, options) {
  56752. var rewriteCookieDomainConfig = options.cookieDomainRewrite,
  56753. rewriteCookiePathConfig = options.cookiePathRewrite,
  56754. preserveHeaderKeyCase = options.preserveHeaderKeyCase,
  56755. rawHeaderKeyMap,
  56756. setHeader = function(key, header) {
  56757. if (header == undefined) return;
  56758. if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
  56759. header = common$2.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain');
  56760. }
  56761. if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
  56762. header = common$2.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
  56763. }
  56764. res.setHeader(String(key).trim(), header);
  56765. };
  56766. if (typeof rewriteCookieDomainConfig === 'string') { //also test for ''
  56767. rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
  56768. }
  56769. if (typeof rewriteCookiePathConfig === 'string') { //also test for ''
  56770. rewriteCookiePathConfig = { '*': rewriteCookiePathConfig };
  56771. }
  56772. // message.rawHeaders is added in: v0.11.6
  56773. // https://nodejs.org/api/http.html#http_message_rawheaders
  56774. if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
  56775. rawHeaderKeyMap = {};
  56776. for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) {
  56777. var key = proxyRes.rawHeaders[i];
  56778. rawHeaderKeyMap[key.toLowerCase()] = key;
  56779. }
  56780. }
  56781. Object.keys(proxyRes.headers).forEach(function(key) {
  56782. var header = proxyRes.headers[key];
  56783. if (preserveHeaderKeyCase && rawHeaderKeyMap) {
  56784. key = rawHeaderKeyMap[key] || key;
  56785. }
  56786. setHeader(key, header);
  56787. });
  56788. },
  56789. /**
  56790. * Set the statusCode from the proxyResponse
  56791. *
  56792. * @param {ClientRequest} Req Request object
  56793. * @param {IncomingMessage} Res Response object
  56794. * @param {proxyResponse} Res Response object from the proxy request
  56795. *
  56796. * @api private
  56797. */
  56798. writeStatusCode: function writeStatusCode(req, res, proxyRes) {
  56799. // From Node.js docs: response.writeHead(statusCode[, statusMessage][, headers])
  56800. if(proxyRes.statusMessage) {
  56801. res.statusCode = proxyRes.statusCode;
  56802. res.statusMessage = proxyRes.statusMessage;
  56803. } else {
  56804. res.statusCode = proxyRes.statusCode;
  56805. }
  56806. }
  56807. };
  56808. var followRedirects$1 = {exports: {}};
  56809. var debug$6;
  56810. var debug_1 = function () {
  56811. if (!debug$6) {
  56812. try {
  56813. /* eslint global-require: off */
  56814. debug$6 = srcExports$1("follow-redirects");
  56815. }
  56816. catch (error) { /* */ }
  56817. if (typeof debug$6 !== "function") {
  56818. debug$6 = function () { /* */ };
  56819. }
  56820. }
  56821. debug$6.apply(null, arguments);
  56822. };
  56823. var url = require$$0$9;
  56824. var URL$1 = url.URL;
  56825. var http$1 = require$$1;
  56826. var https$1 = require$$1$1;
  56827. var Writable = require$$0$7.Writable;
  56828. var assert = require$$5;
  56829. var debug$5 = debug_1;
  56830. // Whether to use the native URL object or the legacy url module
  56831. var useNativeURL = false;
  56832. try {
  56833. assert(new URL$1());
  56834. }
  56835. catch (error) {
  56836. useNativeURL = error.code === "ERR_INVALID_URL";
  56837. }
  56838. // URL fields to preserve in copy operations
  56839. var preservedUrlFields = [
  56840. "auth",
  56841. "host",
  56842. "hostname",
  56843. "href",
  56844. "path",
  56845. "pathname",
  56846. "port",
  56847. "protocol",
  56848. "query",
  56849. "search",
  56850. "hash",
  56851. ];
  56852. // Create handlers that pass events from native requests
  56853. var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
  56854. var eventHandlers = Object.create(null);
  56855. events.forEach(function (event) {
  56856. eventHandlers[event] = function (arg1, arg2, arg3) {
  56857. this._redirectable.emit(event, arg1, arg2, arg3);
  56858. };
  56859. });
  56860. // Error types with codes
  56861. var InvalidUrlError = createErrorType(
  56862. "ERR_INVALID_URL",
  56863. "Invalid URL",
  56864. TypeError
  56865. );
  56866. var RedirectionError = createErrorType(
  56867. "ERR_FR_REDIRECTION_FAILURE",
  56868. "Redirected request failed"
  56869. );
  56870. var TooManyRedirectsError = createErrorType(
  56871. "ERR_FR_TOO_MANY_REDIRECTS",
  56872. "Maximum number of redirects exceeded",
  56873. RedirectionError
  56874. );
  56875. var MaxBodyLengthExceededError = createErrorType(
  56876. "ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
  56877. "Request body larger than maxBodyLength limit"
  56878. );
  56879. var WriteAfterEndError = createErrorType(
  56880. "ERR_STREAM_WRITE_AFTER_END",
  56881. "write after end"
  56882. );
  56883. // istanbul ignore next
  56884. var destroy = Writable.prototype.destroy || noop;
  56885. // An HTTP(S) request that can be redirected
  56886. function RedirectableRequest(options, responseCallback) {
  56887. // Initialize the request
  56888. Writable.call(this);
  56889. this._sanitizeOptions(options);
  56890. this._options = options;
  56891. this._ended = false;
  56892. this._ending = false;
  56893. this._redirectCount = 0;
  56894. this._redirects = [];
  56895. this._requestBodyLength = 0;
  56896. this._requestBodyBuffers = [];
  56897. // Attach a callback if passed
  56898. if (responseCallback) {
  56899. this.on("response", responseCallback);
  56900. }
  56901. // React to responses of native requests
  56902. var self = this;
  56903. this._onNativeResponse = function (response) {
  56904. try {
  56905. self._processResponse(response);
  56906. }
  56907. catch (cause) {
  56908. self.emit("error", cause instanceof RedirectionError ?
  56909. cause : new RedirectionError({ cause: cause }));
  56910. }
  56911. };
  56912. // Perform the first request
  56913. this._performRequest();
  56914. }
  56915. RedirectableRequest.prototype = Object.create(Writable.prototype);
  56916. RedirectableRequest.prototype.abort = function () {
  56917. destroyRequest(this._currentRequest);
  56918. this._currentRequest.abort();
  56919. this.emit("abort");
  56920. };
  56921. RedirectableRequest.prototype.destroy = function (error) {
  56922. destroyRequest(this._currentRequest, error);
  56923. destroy.call(this, error);
  56924. return this;
  56925. };
  56926. // Writes buffered data to the current native request
  56927. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  56928. // Writing is not allowed if end has been called
  56929. if (this._ending) {
  56930. throw new WriteAfterEndError();
  56931. }
  56932. // Validate input and shift parameters if necessary
  56933. if (!isString(data) && !isBuffer(data)) {
  56934. throw new TypeError("data should be a string, Buffer or Uint8Array");
  56935. }
  56936. if (isFunction(encoding)) {
  56937. callback = encoding;
  56938. encoding = null;
  56939. }
  56940. // Ignore empty buffers, since writing them doesn't invoke the callback
  56941. // https://github.com/nodejs/node/issues/22066
  56942. if (data.length === 0) {
  56943. if (callback) {
  56944. callback();
  56945. }
  56946. return;
  56947. }
  56948. // Only write when we don't exceed the maximum body length
  56949. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  56950. this._requestBodyLength += data.length;
  56951. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  56952. this._currentRequest.write(data, encoding, callback);
  56953. }
  56954. // Error when we exceed the maximum body length
  56955. else {
  56956. this.emit("error", new MaxBodyLengthExceededError());
  56957. this.abort();
  56958. }
  56959. };
  56960. // Ends the current native request
  56961. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  56962. // Shift parameters if necessary
  56963. if (isFunction(data)) {
  56964. callback = data;
  56965. data = encoding = null;
  56966. }
  56967. else if (isFunction(encoding)) {
  56968. callback = encoding;
  56969. encoding = null;
  56970. }
  56971. // Write data if needed and end
  56972. if (!data) {
  56973. this._ended = this._ending = true;
  56974. this._currentRequest.end(null, null, callback);
  56975. }
  56976. else {
  56977. var self = this;
  56978. var currentRequest = this._currentRequest;
  56979. this.write(data, encoding, function () {
  56980. self._ended = true;
  56981. currentRequest.end(null, null, callback);
  56982. });
  56983. this._ending = true;
  56984. }
  56985. };
  56986. // Sets a header value on the current native request
  56987. RedirectableRequest.prototype.setHeader = function (name, value) {
  56988. this._options.headers[name] = value;
  56989. this._currentRequest.setHeader(name, value);
  56990. };
  56991. // Clears a header value on the current native request
  56992. RedirectableRequest.prototype.removeHeader = function (name) {
  56993. delete this._options.headers[name];
  56994. this._currentRequest.removeHeader(name);
  56995. };
  56996. // Global timeout for all underlying requests
  56997. RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
  56998. var self = this;
  56999. // Destroys the socket on timeout
  57000. function destroyOnTimeout(socket) {
  57001. socket.setTimeout(msecs);
  57002. socket.removeListener("timeout", socket.destroy);
  57003. socket.addListener("timeout", socket.destroy);
  57004. }
  57005. // Sets up a timer to trigger a timeout event
  57006. function startTimer(socket) {
  57007. if (self._timeout) {
  57008. clearTimeout(self._timeout);
  57009. }
  57010. self._timeout = setTimeout(function () {
  57011. self.emit("timeout");
  57012. clearTimer();
  57013. }, msecs);
  57014. destroyOnTimeout(socket);
  57015. }
  57016. // Stops a timeout from triggering
  57017. function clearTimer() {
  57018. // Clear the timeout
  57019. if (self._timeout) {
  57020. clearTimeout(self._timeout);
  57021. self._timeout = null;
  57022. }
  57023. // Clean up all attached listeners
  57024. self.removeListener("abort", clearTimer);
  57025. self.removeListener("error", clearTimer);
  57026. self.removeListener("response", clearTimer);
  57027. self.removeListener("close", clearTimer);
  57028. if (callback) {
  57029. self.removeListener("timeout", callback);
  57030. }
  57031. if (!self.socket) {
  57032. self._currentRequest.removeListener("socket", startTimer);
  57033. }
  57034. }
  57035. // Attach callback if passed
  57036. if (callback) {
  57037. this.on("timeout", callback);
  57038. }
  57039. // Start the timer if or when the socket is opened
  57040. if (this.socket) {
  57041. startTimer(this.socket);
  57042. }
  57043. else {
  57044. this._currentRequest.once("socket", startTimer);
  57045. }
  57046. // Clean up on events
  57047. this.on("socket", destroyOnTimeout);
  57048. this.on("abort", clearTimer);
  57049. this.on("error", clearTimer);
  57050. this.on("response", clearTimer);
  57051. this.on("close", clearTimer);
  57052. return this;
  57053. };
  57054. // Proxy all other public ClientRequest methods
  57055. [
  57056. "flushHeaders", "getHeader",
  57057. "setNoDelay", "setSocketKeepAlive",
  57058. ].forEach(function (method) {
  57059. RedirectableRequest.prototype[method] = function (a, b) {
  57060. return this._currentRequest[method](a, b);
  57061. };
  57062. });
  57063. // Proxy all public ClientRequest properties
  57064. ["aborted", "connection", "socket"].forEach(function (property) {
  57065. Object.defineProperty(RedirectableRequest.prototype, property, {
  57066. get: function () { return this._currentRequest[property]; },
  57067. });
  57068. });
  57069. RedirectableRequest.prototype._sanitizeOptions = function (options) {
  57070. // Ensure headers are always present
  57071. if (!options.headers) {
  57072. options.headers = {};
  57073. }
  57074. // Since http.request treats host as an alias of hostname,
  57075. // but the url module interprets host as hostname plus port,
  57076. // eliminate the host property to avoid confusion.
  57077. if (options.host) {
  57078. // Use hostname if set, because it has precedence
  57079. if (!options.hostname) {
  57080. options.hostname = options.host;
  57081. }
  57082. delete options.host;
  57083. }
  57084. // Complete the URL object when necessary
  57085. if (!options.pathname && options.path) {
  57086. var searchPos = options.path.indexOf("?");
  57087. if (searchPos < 0) {
  57088. options.pathname = options.path;
  57089. }
  57090. else {
  57091. options.pathname = options.path.substring(0, searchPos);
  57092. options.search = options.path.substring(searchPos);
  57093. }
  57094. }
  57095. };
  57096. // Executes the next native request (initial or redirect)
  57097. RedirectableRequest.prototype._performRequest = function () {
  57098. // Load the native protocol
  57099. var protocol = this._options.protocol;
  57100. var nativeProtocol = this._options.nativeProtocols[protocol];
  57101. if (!nativeProtocol) {
  57102. throw new TypeError("Unsupported protocol " + protocol);
  57103. }
  57104. // If specified, use the agent corresponding to the protocol
  57105. // (HTTP and HTTPS use different types of agents)
  57106. if (this._options.agents) {
  57107. var scheme = protocol.slice(0, -1);
  57108. this._options.agent = this._options.agents[scheme];
  57109. }
  57110. // Create the native request and set up its event handlers
  57111. var request = this._currentRequest =
  57112. nativeProtocol.request(this._options, this._onNativeResponse);
  57113. request._redirectable = this;
  57114. for (var event of events) {
  57115. request.on(event, eventHandlers[event]);
  57116. }
  57117. // RFC7230§5.3.1: When making a request directly to an origin server, […]
  57118. // a client MUST send only the absolute path […] as the request-target.
  57119. this._currentUrl = /^\//.test(this._options.path) ?
  57120. url.format(this._options) :
  57121. // When making a request to a proxy, […]
  57122. // a client MUST send the target URI in absolute-form […].
  57123. this._options.path;
  57124. // End a redirected request
  57125. // (The first request must be ended explicitly with RedirectableRequest#end)
  57126. if (this._isRedirect) {
  57127. // Write the request entity and end
  57128. var i = 0;
  57129. var self = this;
  57130. var buffers = this._requestBodyBuffers;
  57131. (function writeNext(error) {
  57132. // Only write if this request has not been redirected yet
  57133. /* istanbul ignore else */
  57134. if (request === self._currentRequest) {
  57135. // Report any write errors
  57136. /* istanbul ignore if */
  57137. if (error) {
  57138. self.emit("error", error);
  57139. }
  57140. // Write the next buffer if there are still left
  57141. else if (i < buffers.length) {
  57142. var buffer = buffers[i++];
  57143. /* istanbul ignore else */
  57144. if (!request.finished) {
  57145. request.write(buffer.data, buffer.encoding, writeNext);
  57146. }
  57147. }
  57148. // End the request if `end` has been called on us
  57149. else if (self._ended) {
  57150. request.end();
  57151. }
  57152. }
  57153. }());
  57154. }
  57155. };
  57156. // Processes a response from the current native request
  57157. RedirectableRequest.prototype._processResponse = function (response) {
  57158. // Store the redirected response
  57159. var statusCode = response.statusCode;
  57160. if (this._options.trackRedirects) {
  57161. this._redirects.push({
  57162. url: this._currentUrl,
  57163. headers: response.headers,
  57164. statusCode: statusCode,
  57165. });
  57166. }
  57167. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  57168. // that further action needs to be taken by the user agent in order to
  57169. // fulfill the request. If a Location header field is provided,
  57170. // the user agent MAY automatically redirect its request to the URI
  57171. // referenced by the Location field value,
  57172. // even if the specific status code is not understood.
  57173. // If the response is not a redirect; return it as-is
  57174. var location = response.headers.location;
  57175. if (!location || this._options.followRedirects === false ||
  57176. statusCode < 300 || statusCode >= 400) {
  57177. response.responseUrl = this._currentUrl;
  57178. response.redirects = this._redirects;
  57179. this.emit("response", response);
  57180. // Clean up
  57181. this._requestBodyBuffers = [];
  57182. return;
  57183. }
  57184. // The response is a redirect, so abort the current request
  57185. destroyRequest(this._currentRequest);
  57186. // Discard the remainder of the response to avoid waiting for data
  57187. response.destroy();
  57188. // RFC7231§6.4: A client SHOULD detect and intervene
  57189. // in cyclical redirections (i.e., "infinite" redirection loops).
  57190. if (++this._redirectCount > this._options.maxRedirects) {
  57191. throw new TooManyRedirectsError();
  57192. }
  57193. // Store the request headers if applicable
  57194. var requestHeaders;
  57195. var beforeRedirect = this._options.beforeRedirect;
  57196. if (beforeRedirect) {
  57197. requestHeaders = Object.assign({
  57198. // The Host header was set by nativeProtocol.request
  57199. Host: response.req.getHeader("host"),
  57200. }, this._options.headers);
  57201. }
  57202. // RFC7231§6.4: Automatic redirection needs to done with
  57203. // care for methods not known to be safe, […]
  57204. // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
  57205. // the request method from POST to GET for the subsequent request.
  57206. var method = this._options.method;
  57207. if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
  57208. // RFC7231§6.4.4: The 303 (See Other) status code indicates that
  57209. // the server is redirecting the user agent to a different resource […]
  57210. // A user agent can perform a retrieval request targeting that URI
  57211. // (a GET or HEAD request if using HTTP) […]
  57212. (statusCode === 303) && !/^(?:GET|HEAD)$/.test(this._options.method)) {
  57213. this._options.method = "GET";
  57214. // Drop a possible entity and headers related to it
  57215. this._requestBodyBuffers = [];
  57216. removeMatchingHeaders(/^content-/i, this._options.headers);
  57217. }
  57218. // Drop the Host header, as the redirect might lead to a different host
  57219. var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
  57220. // If the redirect is relative, carry over the host of the last request
  57221. var currentUrlParts = parseUrl(this._currentUrl);
  57222. var currentHost = currentHostHeader || currentUrlParts.host;
  57223. var currentUrl = /^\w+:/.test(location) ? this._currentUrl :
  57224. url.format(Object.assign(currentUrlParts, { host: currentHost }));
  57225. // Create the redirected request
  57226. var redirectUrl = resolveUrl(location, currentUrl);
  57227. debug$5("redirecting to", redirectUrl.href);
  57228. this._isRedirect = true;
  57229. spreadUrlObject(redirectUrl, this._options);
  57230. // Drop confidential headers when redirecting to a less secure protocol
  57231. // or to a different domain that is not a superdomain
  57232. if (redirectUrl.protocol !== currentUrlParts.protocol &&
  57233. redirectUrl.protocol !== "https:" ||
  57234. redirectUrl.host !== currentHost &&
  57235. !isSubdomain(redirectUrl.host, currentHost)) {
  57236. removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
  57237. }
  57238. // Evaluate the beforeRedirect callback
  57239. if (isFunction(beforeRedirect)) {
  57240. var responseDetails = {
  57241. headers: response.headers,
  57242. statusCode: statusCode,
  57243. };
  57244. var requestDetails = {
  57245. url: currentUrl,
  57246. method: method,
  57247. headers: requestHeaders,
  57248. };
  57249. beforeRedirect(this._options, responseDetails, requestDetails);
  57250. this._sanitizeOptions(this._options);
  57251. }
  57252. // Perform the redirected request
  57253. this._performRequest();
  57254. };
  57255. // Wraps the key/value object of protocols with redirect functionality
  57256. function wrap(protocols) {
  57257. // Default settings
  57258. var exports = {
  57259. maxRedirects: 21,
  57260. maxBodyLength: 10 * 1024 * 1024,
  57261. };
  57262. // Wrap each protocol
  57263. var nativeProtocols = {};
  57264. Object.keys(protocols).forEach(function (scheme) {
  57265. var protocol = scheme + ":";
  57266. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  57267. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  57268. // Executes a request, following redirects
  57269. function request(input, options, callback) {
  57270. // Parse parameters, ensuring that input is an object
  57271. if (isURL(input)) {
  57272. input = spreadUrlObject(input);
  57273. }
  57274. else if (isString(input)) {
  57275. input = spreadUrlObject(parseUrl(input));
  57276. }
  57277. else {
  57278. callback = options;
  57279. options = validateUrl(input);
  57280. input = { protocol: protocol };
  57281. }
  57282. if (isFunction(options)) {
  57283. callback = options;
  57284. options = null;
  57285. }
  57286. // Set defaults
  57287. options = Object.assign({
  57288. maxRedirects: exports.maxRedirects,
  57289. maxBodyLength: exports.maxBodyLength,
  57290. }, input, options);
  57291. options.nativeProtocols = nativeProtocols;
  57292. if (!isString(options.host) && !isString(options.hostname)) {
  57293. options.hostname = "::1";
  57294. }
  57295. assert.equal(options.protocol, protocol, "protocol mismatch");
  57296. debug$5("options", options);
  57297. return new RedirectableRequest(options, callback);
  57298. }
  57299. // Executes a GET request, following redirects
  57300. function get(input, options, callback) {
  57301. var wrappedRequest = wrappedProtocol.request(input, options, callback);
  57302. wrappedRequest.end();
  57303. return wrappedRequest;
  57304. }
  57305. // Expose the properties on the wrapped protocol
  57306. Object.defineProperties(wrappedProtocol, {
  57307. request: { value: request, configurable: true, enumerable: true, writable: true },
  57308. get: { value: get, configurable: true, enumerable: true, writable: true },
  57309. });
  57310. });
  57311. return exports;
  57312. }
  57313. function noop() { /* empty */ }
  57314. function parseUrl(input) {
  57315. var parsed;
  57316. /* istanbul ignore else */
  57317. if (useNativeURL) {
  57318. parsed = new URL$1(input);
  57319. }
  57320. else {
  57321. // Ensure the URL is valid and absolute
  57322. parsed = validateUrl(url.parse(input));
  57323. if (!isString(parsed.protocol)) {
  57324. throw new InvalidUrlError({ input });
  57325. }
  57326. }
  57327. return parsed;
  57328. }
  57329. function resolveUrl(relative, base) {
  57330. /* istanbul ignore next */
  57331. return useNativeURL ? new URL$1(relative, base) : parseUrl(url.resolve(base, relative));
  57332. }
  57333. function validateUrl(input) {
  57334. if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
  57335. throw new InvalidUrlError({ input: input.href || input });
  57336. }
  57337. if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) {
  57338. throw new InvalidUrlError({ input: input.href || input });
  57339. }
  57340. return input;
  57341. }
  57342. function spreadUrlObject(urlObject, target) {
  57343. var spread = target || {};
  57344. for (var key of preservedUrlFields) {
  57345. spread[key] = urlObject[key];
  57346. }
  57347. // Fix IPv6 hostname
  57348. if (spread.hostname.startsWith("[")) {
  57349. spread.hostname = spread.hostname.slice(1, -1);
  57350. }
  57351. // Ensure port is a number
  57352. if (spread.port !== "") {
  57353. spread.port = Number(spread.port);
  57354. }
  57355. // Concatenate path
  57356. spread.path = spread.search ? spread.pathname + spread.search : spread.pathname;
  57357. return spread;
  57358. }
  57359. function removeMatchingHeaders(regex, headers) {
  57360. var lastValue;
  57361. for (var header in headers) {
  57362. if (regex.test(header)) {
  57363. lastValue = headers[header];
  57364. delete headers[header];
  57365. }
  57366. }
  57367. return (lastValue === null || typeof lastValue === "undefined") ?
  57368. undefined : String(lastValue).trim();
  57369. }
  57370. function createErrorType(code, message, baseClass) {
  57371. // Create constructor
  57372. function CustomError(properties) {
  57373. Error.captureStackTrace(this, this.constructor);
  57374. Object.assign(this, properties || {});
  57375. this.code = code;
  57376. this.message = this.cause ? message + ": " + this.cause.message : message;
  57377. }
  57378. // Attach constructor and set default properties
  57379. CustomError.prototype = new (baseClass || Error)();
  57380. Object.defineProperties(CustomError.prototype, {
  57381. constructor: {
  57382. value: CustomError,
  57383. enumerable: false,
  57384. },
  57385. name: {
  57386. value: "Error [" + code + "]",
  57387. enumerable: false,
  57388. },
  57389. });
  57390. return CustomError;
  57391. }
  57392. function destroyRequest(request, error) {
  57393. for (var event of events) {
  57394. request.removeListener(event, eventHandlers[event]);
  57395. }
  57396. request.on("error", noop);
  57397. request.destroy(error);
  57398. }
  57399. function isSubdomain(subdomain, domain) {
  57400. assert(isString(subdomain) && isString(domain));
  57401. var dot = subdomain.length - domain.length - 1;
  57402. return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
  57403. }
  57404. function isString(value) {
  57405. return typeof value === "string" || value instanceof String;
  57406. }
  57407. function isFunction(value) {
  57408. return typeof value === "function";
  57409. }
  57410. function isBuffer(value) {
  57411. return typeof value === "object" && ("length" in value);
  57412. }
  57413. function isURL(value) {
  57414. return URL$1 && value instanceof URL$1;
  57415. }
  57416. // Exports
  57417. followRedirects$1.exports = wrap({ http: http$1, https: https$1 });
  57418. followRedirects$1.exports.wrap = wrap;
  57419. var followRedirectsExports = followRedirects$1.exports;
  57420. var httpNative = require$$1,
  57421. httpsNative = require$$1$1,
  57422. web_o = webOutgoing,
  57423. common$1 = common$3,
  57424. followRedirects = followRedirectsExports;
  57425. web_o = Object.keys(web_o).map(function(pass) {
  57426. return web_o[pass];
  57427. });
  57428. var nativeAgents = { http: httpNative, https: httpsNative };
  57429. /*!
  57430. * Array of passes.
  57431. *
  57432. * A `pass` is just a function that is executed on `req, res, options`
  57433. * so that you can easily add new checks while still keeping the base
  57434. * flexible.
  57435. */
  57436. var webIncoming = {
  57437. /**
  57438. * Sets `content-length` to '0' if request is of DELETE type.
  57439. *
  57440. * @param {ClientRequest} Req Request object
  57441. * @param {IncomingMessage} Res Response object
  57442. * @param {Object} Options Config object passed to the proxy
  57443. *
  57444. * @api private
  57445. */
  57446. deleteLength: function deleteLength(req, res, options) {
  57447. if((req.method === 'DELETE' || req.method === 'OPTIONS')
  57448. && !req.headers['content-length']) {
  57449. req.headers['content-length'] = '0';
  57450. delete req.headers['transfer-encoding'];
  57451. }
  57452. },
  57453. /**
  57454. * Sets timeout in request socket if it was specified in options.
  57455. *
  57456. * @param {ClientRequest} Req Request object
  57457. * @param {IncomingMessage} Res Response object
  57458. * @param {Object} Options Config object passed to the proxy
  57459. *
  57460. * @api private
  57461. */
  57462. timeout: function timeout(req, res, options) {
  57463. if(options.timeout) {
  57464. req.socket.setTimeout(options.timeout);
  57465. }
  57466. },
  57467. /**
  57468. * Sets `x-forwarded-*` headers if specified in config.
  57469. *
  57470. * @param {ClientRequest} Req Request object
  57471. * @param {IncomingMessage} Res Response object
  57472. * @param {Object} Options Config object passed to the proxy
  57473. *
  57474. * @api private
  57475. */
  57476. XHeaders: function XHeaders(req, res, options) {
  57477. if(!options.xfwd) return;
  57478. var encrypted = req.isSpdy || common$1.hasEncryptedConnection(req);
  57479. var values = {
  57480. for : req.connection.remoteAddress || req.socket.remoteAddress,
  57481. port : common$1.getPort(req),
  57482. proto: encrypted ? 'https' : 'http'
  57483. };
  57484. ['for', 'port', 'proto'].forEach(function(header) {
  57485. req.headers['x-forwarded-' + header] =
  57486. (req.headers['x-forwarded-' + header] || '') +
  57487. (req.headers['x-forwarded-' + header] ? ',' : '') +
  57488. values[header];
  57489. });
  57490. req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
  57491. },
  57492. /**
  57493. * Does the actual proxying. If `forward` is enabled fires up
  57494. * a ForwardStream, same happens for ProxyStream. The request
  57495. * just dies otherwise.
  57496. *
  57497. * @param {ClientRequest} Req Request object
  57498. * @param {IncomingMessage} Res Response object
  57499. * @param {Object} Options Config object passed to the proxy
  57500. *
  57501. * @api private
  57502. */
  57503. stream: function stream(req, res, options, _, server, clb) {
  57504. // And we begin!
  57505. server.emit('start', req, res, options.target || options.forward);
  57506. var agents = options.followRedirects ? followRedirects : nativeAgents;
  57507. var http = agents.http;
  57508. var https = agents.https;
  57509. if(options.forward) {
  57510. // If forward enable, so just pipe the request
  57511. var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
  57512. common$1.setupOutgoing(options.ssl || {}, options, req, 'forward')
  57513. );
  57514. // error handler (e.g. ECONNRESET, ECONNREFUSED)
  57515. // Handle errors on incoming request as well as it makes sense to
  57516. var forwardError = createErrorHandler(forwardReq, options.forward);
  57517. req.on('error', forwardError);
  57518. forwardReq.on('error', forwardError);
  57519. (options.buffer || req).pipe(forwardReq);
  57520. if(!options.target) { return res.end(); }
  57521. }
  57522. // Request initalization
  57523. var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
  57524. common$1.setupOutgoing(options.ssl || {}, options, req)
  57525. );
  57526. // Enable developers to modify the proxyReq before headers are sent
  57527. proxyReq.on('socket', function(socket) {
  57528. if(server && !proxyReq.getHeader('expect')) {
  57529. server.emit('proxyReq', proxyReq, req, res, options);
  57530. }
  57531. });
  57532. // allow outgoing socket to timeout so that we could
  57533. // show an error page at the initial request
  57534. if(options.proxyTimeout) {
  57535. proxyReq.setTimeout(options.proxyTimeout, function() {
  57536. proxyReq.abort();
  57537. });
  57538. }
  57539. // Ensure we abort proxy if request is aborted
  57540. req.on('aborted', function () {
  57541. proxyReq.abort();
  57542. });
  57543. // handle errors in proxy and incoming request, just like for forward proxy
  57544. var proxyError = createErrorHandler(proxyReq, options.target);
  57545. req.on('error', proxyError);
  57546. proxyReq.on('error', proxyError);
  57547. function createErrorHandler(proxyReq, url) {
  57548. return function proxyError(err) {
  57549. if (req.socket.destroyed && err.code === 'ECONNRESET') {
  57550. server.emit('econnreset', err, req, res, url);
  57551. return proxyReq.abort();
  57552. }
  57553. if (clb) {
  57554. clb(err, req, res, url);
  57555. } else {
  57556. server.emit('error', err, req, res, url);
  57557. }
  57558. }
  57559. }
  57560. (options.buffer || req).pipe(proxyReq);
  57561. proxyReq.on('response', function(proxyRes) {
  57562. if(server) { server.emit('proxyRes', proxyRes, req, res); }
  57563. if(!res.headersSent && !options.selfHandleResponse) {
  57564. for(var i=0; i < web_o.length; i++) {
  57565. if(web_o[i](req, res, proxyRes, options)) { break; }
  57566. }
  57567. }
  57568. if (!res.finished) {
  57569. // Allow us to listen when the proxy has completed
  57570. proxyRes.on('end', function () {
  57571. if (server) server.emit('end', req, res, proxyRes);
  57572. });
  57573. // We pipe to the response unless its expected to be handled by the user
  57574. if (!options.selfHandleResponse) proxyRes.pipe(res);
  57575. } else {
  57576. if (server) server.emit('end', req, res, proxyRes);
  57577. }
  57578. });
  57579. }
  57580. };
  57581. var http = require$$1,
  57582. https = require$$1$1,
  57583. common = common$3;
  57584. /*!
  57585. * Array of passes.
  57586. *
  57587. * A `pass` is just a function that is executed on `req, socket, options`
  57588. * so that you can easily add new checks while still keeping the base
  57589. * flexible.
  57590. */
  57591. /*
  57592. * Websockets Passes
  57593. *
  57594. */
  57595. var wsIncoming = {
  57596. /**
  57597. * WebSocket requests must have the `GET` method and
  57598. * the `upgrade:websocket` header
  57599. *
  57600. * @param {ClientRequest} Req Request object
  57601. * @param {Socket} Websocket
  57602. *
  57603. * @api private
  57604. */
  57605. checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
  57606. if (req.method !== 'GET' || !req.headers.upgrade) {
  57607. socket.destroy();
  57608. return true;
  57609. }
  57610. if (req.headers.upgrade.toLowerCase() !== 'websocket') {
  57611. socket.destroy();
  57612. return true;
  57613. }
  57614. },
  57615. /**
  57616. * Sets `x-forwarded-*` headers if specified in config.
  57617. *
  57618. * @param {ClientRequest} Req Request object
  57619. * @param {Socket} Websocket
  57620. * @param {Object} Options Config object passed to the proxy
  57621. *
  57622. * @api private
  57623. */
  57624. XHeaders : function XHeaders(req, socket, options) {
  57625. if(!options.xfwd) return;
  57626. var values = {
  57627. for : req.connection.remoteAddress || req.socket.remoteAddress,
  57628. port : common.getPort(req),
  57629. proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
  57630. };
  57631. ['for', 'port', 'proto'].forEach(function(header) {
  57632. req.headers['x-forwarded-' + header] =
  57633. (req.headers['x-forwarded-' + header] || '') +
  57634. (req.headers['x-forwarded-' + header] ? ',' : '') +
  57635. values[header];
  57636. });
  57637. },
  57638. /**
  57639. * Does the actual proxying. Make the request and upgrade it
  57640. * send the Switching Protocols request and pipe the sockets.
  57641. *
  57642. * @param {ClientRequest} Req Request object
  57643. * @param {Socket} Websocket
  57644. * @param {Object} Options Config object passed to the proxy
  57645. *
  57646. * @api private
  57647. */
  57648. stream : function stream(req, socket, options, head, server, clb) {
  57649. var createHttpHeader = function(line, headers) {
  57650. return Object.keys(headers).reduce(function (head, key) {
  57651. var value = headers[key];
  57652. if (!Array.isArray(value)) {
  57653. head.push(key + ': ' + value);
  57654. return head;
  57655. }
  57656. for (var i = 0; i < value.length; i++) {
  57657. head.push(key + ': ' + value[i]);
  57658. }
  57659. return head;
  57660. }, [line])
  57661. .join('\r\n') + '\r\n\r\n';
  57662. };
  57663. common.setupSocket(socket);
  57664. if (head && head.length) socket.unshift(head);
  57665. var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
  57666. common.setupOutgoing(options.ssl || {}, options, req)
  57667. );
  57668. // Enable developers to modify the proxyReq before headers are sent
  57669. if (server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }
  57670. // Error Handler
  57671. proxyReq.on('error', onOutgoingError);
  57672. proxyReq.on('response', function (res) {
  57673. // if upgrade event isn't going to happen, close the socket
  57674. if (!res.upgrade) {
  57675. socket.write(createHttpHeader('HTTP/' + res.httpVersion + ' ' + res.statusCode + ' ' + res.statusMessage, res.headers));
  57676. res.pipe(socket);
  57677. }
  57678. });
  57679. proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
  57680. proxySocket.on('error', onOutgoingError);
  57681. // Allow us to listen when the websocket has completed
  57682. proxySocket.on('end', function () {
  57683. server.emit('close', proxyRes, proxySocket, proxyHead);
  57684. });
  57685. // The pipe below will end proxySocket if socket closes cleanly, but not
  57686. // if it errors (eg, vanishes from the net and starts returning
  57687. // EHOSTUNREACH). We need to do that explicitly.
  57688. socket.on('error', function () {
  57689. proxySocket.end();
  57690. });
  57691. common.setupSocket(proxySocket);
  57692. if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
  57693. //
  57694. // Remark: Handle writing the headers to the socket when switching protocols
  57695. // Also handles when a header is an array
  57696. //
  57697. socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
  57698. proxySocket.pipe(socket).pipe(proxySocket);
  57699. server.emit('open', proxySocket);
  57700. server.emit('proxySocket', proxySocket); //DEPRECATED.
  57701. });
  57702. return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
  57703. function onOutgoingError(err) {
  57704. if (clb) {
  57705. clb(err, req, socket);
  57706. } else {
  57707. server.emit('error', err, req, socket);
  57708. }
  57709. socket.end();
  57710. }
  57711. }
  57712. };
  57713. (function (module) {
  57714. var httpProxy = module.exports,
  57715. extend = require$$0$6._extend,
  57716. parse_url = require$$0$9.parse,
  57717. EE3 = eventemitter3Exports,
  57718. http = require$$1,
  57719. https = require$$1$1,
  57720. web = webIncoming,
  57721. ws = wsIncoming;
  57722. httpProxy.Server = ProxyServer;
  57723. /**
  57724. * Returns a function that creates the loader for
  57725. * either `ws` or `web`'s passes.
  57726. *
  57727. * Examples:
  57728. *
  57729. * httpProxy.createRightProxy('ws')
  57730. * // => [Function]
  57731. *
  57732. * @param {String} Type Either 'ws' or 'web'
  57733. * @return {Function} Loader Function that when called returns an iterator for the right passes
  57734. *
  57735. * @api private
  57736. */
  57737. function createRightProxy(type) {
  57738. return function(options) {
  57739. return function(req, res /*, [head], [opts] */) {
  57740. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57741. args = [].slice.call(arguments),
  57742. cntr = args.length - 1,
  57743. head, cbl;
  57744. /* optional args parse begin */
  57745. if(typeof args[cntr] === 'function') {
  57746. cbl = args[cntr];
  57747. cntr--;
  57748. }
  57749. var requestOptions = options;
  57750. if(
  57751. !(args[cntr] instanceof Buffer) &&
  57752. args[cntr] !== res
  57753. ) {
  57754. //Copy global options
  57755. requestOptions = extend({}, options);
  57756. //Overwrite with request options
  57757. extend(requestOptions, args[cntr]);
  57758. cntr--;
  57759. }
  57760. if(args[cntr] instanceof Buffer) {
  57761. head = args[cntr];
  57762. }
  57763. /* optional args parse end */
  57764. ['target', 'forward'].forEach(function(e) {
  57765. if (typeof requestOptions[e] === 'string')
  57766. requestOptions[e] = parse_url(requestOptions[e]);
  57767. });
  57768. if (!requestOptions.target && !requestOptions.forward) {
  57769. return this.emit('error', new Error('Must provide a proper URL as target'));
  57770. }
  57771. for(var i=0; i < passes.length; i++) {
  57772. /**
  57773. * Call of passes functions
  57774. * pass(req, res, options, head)
  57775. *
  57776. * In WebSockets case the `res` variable
  57777. * refer to the connection socket
  57778. * pass(req, socket, options, head)
  57779. */
  57780. if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
  57781. break;
  57782. }
  57783. }
  57784. };
  57785. };
  57786. }
  57787. httpProxy.createRightProxy = createRightProxy;
  57788. function ProxyServer(options) {
  57789. EE3.call(this);
  57790. options = options || {};
  57791. options.prependPath = options.prependPath === false ? false : true;
  57792. this.web = this.proxyRequest = createRightProxy('web')(options);
  57793. this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options);
  57794. this.options = options;
  57795. this.webPasses = Object.keys(web).map(function(pass) {
  57796. return web[pass];
  57797. });
  57798. this.wsPasses = Object.keys(ws).map(function(pass) {
  57799. return ws[pass];
  57800. });
  57801. this.on('error', this.onError, this);
  57802. }
  57803. require$$0$6.inherits(ProxyServer, EE3);
  57804. ProxyServer.prototype.onError = function (err) {
  57805. //
  57806. // Remark: Replicate node core behavior using EE3
  57807. // so we force people to handle their own errors
  57808. //
  57809. if(this.listeners('error').length === 1) {
  57810. throw err;
  57811. }
  57812. };
  57813. ProxyServer.prototype.listen = function(port, hostname) {
  57814. var self = this,
  57815. closure = function(req, res) { self.web(req, res); };
  57816. this._server = this.options.ssl ?
  57817. https.createServer(this.options.ssl, closure) :
  57818. http.createServer(closure);
  57819. if(this.options.ws) {
  57820. this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
  57821. }
  57822. this._server.listen(port, hostname);
  57823. return this;
  57824. };
  57825. ProxyServer.prototype.close = function(callback) {
  57826. var self = this;
  57827. if (this._server) {
  57828. this._server.close(done);
  57829. }
  57830. // Wrap callback to nullify server after all open connections are closed.
  57831. function done() {
  57832. self._server = null;
  57833. if (callback) {
  57834. callback.apply(null, arguments);
  57835. }
  57836. } };
  57837. ProxyServer.prototype.before = function(type, passName, callback) {
  57838. if (type !== 'ws' && type !== 'web') {
  57839. throw new Error('type must be `web` or `ws`');
  57840. }
  57841. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57842. i = false;
  57843. passes.forEach(function(v, idx) {
  57844. if(v.name === passName) i = idx;
  57845. });
  57846. if(i === false) throw new Error('No such pass');
  57847. passes.splice(i, 0, callback);
  57848. };
  57849. ProxyServer.prototype.after = function(type, passName, callback) {
  57850. if (type !== 'ws' && type !== 'web') {
  57851. throw new Error('type must be `web` or `ws`');
  57852. }
  57853. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57854. i = false;
  57855. passes.forEach(function(v, idx) {
  57856. if(v.name === passName) i = idx;
  57857. });
  57858. if(i === false) throw new Error('No such pass');
  57859. passes.splice(i++, 0, callback);
  57860. };
  57861. } (httpProxy$3));
  57862. var httpProxyExports = httpProxy$3.exports;
  57863. // Use explicit /index.js to help browserify negociation in require '/lib/http-proxy' (!)
  57864. var ProxyServer = httpProxyExports.Server;
  57865. /**
  57866. * Creates the proxy server.
  57867. *
  57868. * Examples:
  57869. *
  57870. * httpProxy.createProxyServer({ .. }, 8000)
  57871. * // => '{ web: [Function], ws: [Function] ... }'
  57872. *
  57873. * @param {Object} Options Config object passed to the proxy
  57874. *
  57875. * @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests
  57876. *
  57877. * @api public
  57878. */
  57879. function createProxyServer(options) {
  57880. /*
  57881. * `options` is needed and it must have the following layout:
  57882. *
  57883. * {
  57884. * target : <url string to be parsed with the url module>
  57885. * forward: <url string to be parsed with the url module>
  57886. * agent : <object to be passed to http(s).request>
  57887. * ssl : <object to be passed to https.createServer()>
  57888. * ws : <true/false, if you want to proxy websockets>
  57889. * xfwd : <true/false, adds x-forward headers>
  57890. * secure : <true/false, verify SSL certificate>
  57891. * toProxy: <true/false, explicitly specify if we are proxying to another proxy>
  57892. * prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
  57893. * ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
  57894. * localAddress : <Local interface string to bind for outgoing connections>
  57895. * changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
  57896. * preserveHeaderKeyCase: <true/false, Default: false - specify whether you want to keep letter case of response header key >
  57897. * auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
  57898. * hostRewrite: rewrites the location hostname on (201/301/302/307/308) redirects, Default: null.
  57899. * autoRewrite: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
  57900. * protocolRewrite: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.
  57901. * }
  57902. *
  57903. * NOTE: `options.ws` and `options.ssl` are optional.
  57904. * `options.target and `options.forward` cannot be
  57905. * both missing
  57906. * }
  57907. */
  57908. return new ProxyServer(options);
  57909. }
  57910. ProxyServer.createProxyServer = createProxyServer;
  57911. ProxyServer.createServer = createProxyServer;
  57912. ProxyServer.createProxy = createProxyServer;
  57913. /**
  57914. * Export the proxy "Server" as the main export.
  57915. */
  57916. var httpProxy$2 = ProxyServer;
  57917. /*!
  57918. * Caron dimonio, con occhi di bragia
  57919. * loro accennando, tutte le raccoglie;
  57920. * batte col remo qualunque s’adagia
  57921. *
  57922. * Charon the demon, with the eyes of glede,
  57923. * Beckoning to them, collects them all together,
  57924. * Beats with his oar whoever lags behind
  57925. *
  57926. * Dante - The Divine Comedy (Canto III)
  57927. */
  57928. var httpProxy = httpProxy$2;
  57929. var httpProxy$1 = /*@__PURE__*/getDefaultExportFromCjs(httpProxy);
  57930. const debug$4 = createDebugger('vite:proxy');
  57931. function proxyMiddleware(httpServer, options, config) {
  57932. // lazy require only when proxy is used
  57933. const proxies = {};
  57934. Object.keys(options).forEach((context) => {
  57935. let opts = options[context];
  57936. if (!opts) {
  57937. return;
  57938. }
  57939. if (typeof opts === 'string') {
  57940. opts = { target: opts, changeOrigin: true };
  57941. }
  57942. const proxy = httpProxy$1.createProxyServer(opts);
  57943. if (opts.configure) {
  57944. opts.configure(proxy, opts);
  57945. }
  57946. proxy.on('error', (err, req, originalRes) => {
  57947. // When it is ws proxy, res is net.Socket
  57948. // originalRes can be falsy if the proxy itself errored
  57949. const res = originalRes;
  57950. if (!res) {
  57951. config.logger.error(`${colors$1.red(`http proxy error: ${err.message}`)}\n${err.stack}`, {
  57952. timestamp: true,
  57953. error: err,
  57954. });
  57955. }
  57956. else if ('req' in res) {
  57957. config.logger.error(`${colors$1.red(`http proxy error: ${originalRes.req.url}`)}\n${err.stack}`, {
  57958. timestamp: true,
  57959. error: err,
  57960. });
  57961. if (!res.headersSent && !res.writableEnded) {
  57962. res
  57963. .writeHead(500, {
  57964. 'Content-Type': 'text/plain',
  57965. })
  57966. .end();
  57967. }
  57968. }
  57969. else {
  57970. config.logger.error(`${colors$1.red(`ws proxy error:`)}\n${err.stack}`, {
  57971. timestamp: true,
  57972. error: err,
  57973. });
  57974. res.end();
  57975. }
  57976. });
  57977. proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
  57978. socket.on('error', (err) => {
  57979. config.logger.error(`${colors$1.red(`ws proxy socket error:`)}\n${err.stack}`, {
  57980. timestamp: true,
  57981. error: err,
  57982. });
  57983. });
  57984. });
  57985. // https://github.com/http-party/node-http-proxy/issues/1520#issue-877626125
  57986. // https://github.com/chimurai/http-proxy-middleware/blob/cd58f962aec22c925b7df5140502978da8f87d5f/src/plugins/default/debug-proxy-errors-plugin.ts#L25-L37
  57987. proxy.on('proxyRes', (proxyRes, req, res) => {
  57988. res.on('close', () => {
  57989. if (!res.writableEnded) {
  57990. debug$4?.('destroying proxyRes in proxyRes close event');
  57991. proxyRes.destroy();
  57992. }
  57993. });
  57994. });
  57995. // clone before saving because http-proxy mutates the options
  57996. proxies[context] = [proxy, { ...opts }];
  57997. });
  57998. if (httpServer) {
  57999. httpServer.on('upgrade', (req, socket, head) => {
  58000. const url = req.url;
  58001. for (const context in proxies) {
  58002. if (doesProxyContextMatchUrl(context, url)) {
  58003. const [proxy, opts] = proxies[context];
  58004. if (opts.ws ||
  58005. opts.target?.toString().startsWith('ws:') ||
  58006. opts.target?.toString().startsWith('wss:')) {
  58007. if (opts.rewrite) {
  58008. req.url = opts.rewrite(url);
  58009. }
  58010. debug$4?.(`${req.url} -> ws ${opts.target}`);
  58011. proxy.ws(req, socket, head);
  58012. return;
  58013. }
  58014. }
  58015. }
  58016. });
  58017. }
  58018. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58019. return function viteProxyMiddleware(req, res, next) {
  58020. const url = req.url;
  58021. for (const context in proxies) {
  58022. if (doesProxyContextMatchUrl(context, url)) {
  58023. const [proxy, opts] = proxies[context];
  58024. const options = {};
  58025. if (opts.bypass) {
  58026. const bypassResult = opts.bypass(req, res, opts);
  58027. if (typeof bypassResult === 'string') {
  58028. req.url = bypassResult;
  58029. debug$4?.(`bypass: ${req.url} -> ${bypassResult}`);
  58030. return next();
  58031. }
  58032. else if (bypassResult === false) {
  58033. debug$4?.(`bypass: ${req.url} -> 404`);
  58034. res.statusCode = 404;
  58035. return res.end();
  58036. }
  58037. }
  58038. debug$4?.(`${req.url} -> ${opts.target || opts.forward}`);
  58039. if (opts.rewrite) {
  58040. req.url = opts.rewrite(req.url);
  58041. }
  58042. proxy.web(req, res, options);
  58043. return;
  58044. }
  58045. }
  58046. next();
  58047. };
  58048. }
  58049. function doesProxyContextMatchUrl(context, url) {
  58050. return ((context[0] === '^' && new RegExp(context).test(url)) ||
  58051. url.startsWith(context));
  58052. }
  58053. const debug$3 = createDebugger('vite:html-fallback');
  58054. function htmlFallbackMiddleware(root, spaFallback, fsUtils = commonFsUtils) {
  58055. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58056. return function viteHtmlFallbackMiddleware(req, res, next) {
  58057. if (
  58058. // Only accept GET or HEAD
  58059. (req.method !== 'GET' && req.method !== 'HEAD') ||
  58060. // Exclude default favicon requests
  58061. req.url === '/favicon.ico' ||
  58062. // Require Accept: text/html or */*
  58063. !(req.headers.accept === undefined || // equivalent to `Accept: */*`
  58064. req.headers.accept === '' || // equivalent to `Accept: */*`
  58065. req.headers.accept.includes('text/html') ||
  58066. req.headers.accept.includes('*/*'))) {
  58067. return next();
  58068. }
  58069. const url = cleanUrl(req.url);
  58070. const pathname = decodeURIComponent(url);
  58071. // .html files are not handled by serveStaticMiddleware
  58072. // so we need to check if the file exists
  58073. if (pathname.endsWith('.html')) {
  58074. const filePath = path$o.join(root, pathname);
  58075. if (fsUtils.existsSync(filePath)) {
  58076. debug$3?.(`Rewriting ${req.method} ${req.url} to ${url}`);
  58077. req.url = url;
  58078. return next();
  58079. }
  58080. }
  58081. // trailing slash should check for fallback index.html
  58082. else if (pathname[pathname.length - 1] === '/') {
  58083. const filePath = path$o.join(root, pathname, 'index.html');
  58084. if (fsUtils.existsSync(filePath)) {
  58085. const newUrl = url + 'index.html';
  58086. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  58087. req.url = newUrl;
  58088. return next();
  58089. }
  58090. }
  58091. // non-trailing slash should check for fallback .html
  58092. else {
  58093. const filePath = path$o.join(root, pathname + '.html');
  58094. if (fsUtils.existsSync(filePath)) {
  58095. const newUrl = url + '.html';
  58096. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  58097. req.url = newUrl;
  58098. return next();
  58099. }
  58100. }
  58101. if (spaFallback) {
  58102. debug$3?.(`Rewriting ${req.method} ${req.url} to /index.html`);
  58103. req.url = '/index.html';
  58104. }
  58105. next();
  58106. };
  58107. }
  58108. const debug$2 = createDebugger('vite:send', {
  58109. onlyWhenFocused: true,
  58110. });
  58111. const alias = {
  58112. js: 'text/javascript',
  58113. css: 'text/css',
  58114. html: 'text/html',
  58115. json: 'application/json',
  58116. };
  58117. function send(req, res, content, type, options) {
  58118. const { etag = getEtag(content, { weak: true }), cacheControl = 'no-cache', headers, map, } = options;
  58119. if (res.writableEnded) {
  58120. return;
  58121. }
  58122. if (req.headers['if-none-match'] === etag) {
  58123. res.statusCode = 304;
  58124. res.end();
  58125. return;
  58126. }
  58127. res.setHeader('Content-Type', alias[type] || type);
  58128. res.setHeader('Cache-Control', cacheControl);
  58129. res.setHeader('Etag', etag);
  58130. if (headers) {
  58131. for (const name in headers) {
  58132. res.setHeader(name, headers[name]);
  58133. }
  58134. }
  58135. // inject source map reference
  58136. if (map && 'version' in map && map.mappings) {
  58137. if (type === 'js' || type === 'css') {
  58138. content = getCodeWithSourcemap(type, content.toString(), map);
  58139. }
  58140. }
  58141. // inject fallback sourcemap for js for improved debugging
  58142. // https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
  58143. else if (type === 'js' && (!map || map.mappings !== '')) {
  58144. const code = content.toString();
  58145. // if the code has existing inline sourcemap, assume it's correct and skip
  58146. if (convertSourceMap.mapFileCommentRegex.test(code)) {
  58147. debug$2?.(`Skipped injecting fallback sourcemap for ${req.url}`);
  58148. }
  58149. else {
  58150. const urlWithoutTimestamp = removeTimestampQuery(req.url);
  58151. const ms = new MagicString(code);
  58152. content = getCodeWithSourcemap(type, code, ms.generateMap({
  58153. source: path$o.basename(urlWithoutTimestamp),
  58154. hires: 'boundary',
  58155. includeContent: true,
  58156. }));
  58157. }
  58158. }
  58159. res.statusCode = 200;
  58160. res.end(content);
  58161. return;
  58162. }
  58163. const debugCache = createDebugger('vite:cache');
  58164. const knownIgnoreList = new Set(['/', '/favicon.ico']);
  58165. /**
  58166. * A middleware that short-circuits the middleware chain to serve cached transformed modules
  58167. */
  58168. function cachedTransformMiddleware(server) {
  58169. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58170. return function viteCachedTransformMiddleware(req, res, next) {
  58171. // check if we can return 304 early
  58172. const ifNoneMatch = req.headers['if-none-match'];
  58173. if (ifNoneMatch) {
  58174. const moduleByEtag = server.moduleGraph.getModuleByEtag(ifNoneMatch);
  58175. if (moduleByEtag?.transformResult?.etag === ifNoneMatch) {
  58176. // For CSS requests, if the same CSS file is imported in a module,
  58177. // the browser sends the request for the direct CSS request with the etag
  58178. // from the imported CSS module. We ignore the etag in this case.
  58179. const maybeMixedEtag = isCSSRequest(req.url);
  58180. if (!maybeMixedEtag) {
  58181. debugCache?.(`[304] ${prettifyUrl(req.url, server.config.root)}`);
  58182. res.statusCode = 304;
  58183. return res.end();
  58184. }
  58185. }
  58186. }
  58187. next();
  58188. };
  58189. }
  58190. function transformMiddleware(server) {
  58191. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58192. // check if public dir is inside root dir
  58193. const { root, publicDir } = server.config;
  58194. const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root));
  58195. const publicPath = `${publicDir.slice(root.length)}/`;
  58196. return async function viteTransformMiddleware(req, res, next) {
  58197. if (req.method !== 'GET' || knownIgnoreList.has(req.url)) {
  58198. return next();
  58199. }
  58200. let url;
  58201. try {
  58202. url = decodeURI(removeTimestampQuery(req.url)).replace(NULL_BYTE_PLACEHOLDER, '\0');
  58203. }
  58204. catch (e) {
  58205. return next(e);
  58206. }
  58207. const withoutQuery = cleanUrl(url);
  58208. try {
  58209. const isSourceMap = withoutQuery.endsWith('.map');
  58210. // since we generate source map references, handle those requests here
  58211. if (isSourceMap) {
  58212. const depsOptimizer = getDepsOptimizer(server.config, false); // non-ssr
  58213. if (depsOptimizer?.isOptimizedDepUrl(url)) {
  58214. // If the browser is requesting a source map for an optimized dep, it
  58215. // means that the dependency has already been pre-bundled and loaded
  58216. const sourcemapPath = url.startsWith(FS_PREFIX)
  58217. ? fsPathFromId(url)
  58218. : normalizePath$3(path$o.resolve(server.config.root, url.slice(1)));
  58219. try {
  58220. const map = JSON.parse(await fsp.readFile(sourcemapPath, 'utf-8'));
  58221. applySourcemapIgnoreList(map, sourcemapPath, server.config.server.sourcemapIgnoreList, server.config.logger);
  58222. return send(req, res, JSON.stringify(map), 'json', {
  58223. headers: server.config.server.headers,
  58224. });
  58225. }
  58226. catch (e) {
  58227. // Outdated source map request for optimized deps, this isn't an error
  58228. // but part of the normal flow when re-optimizing after missing deps
  58229. // Send back an empty source map so the browser doesn't issue warnings
  58230. const dummySourceMap = {
  58231. version: 3,
  58232. file: sourcemapPath.replace(/\.map$/, ''),
  58233. sources: [],
  58234. sourcesContent: [],
  58235. names: [],
  58236. mappings: ';;;;;;;;;',
  58237. };
  58238. return send(req, res, JSON.stringify(dummySourceMap), 'json', {
  58239. cacheControl: 'no-cache',
  58240. headers: server.config.server.headers,
  58241. });
  58242. }
  58243. }
  58244. else {
  58245. const originalUrl = url.replace(/\.map($|\?)/, '$1');
  58246. const map = (await server.moduleGraph.getModuleByUrl(originalUrl, false))?.transformResult?.map;
  58247. if (map) {
  58248. return send(req, res, JSON.stringify(map), 'json', {
  58249. headers: server.config.server.headers,
  58250. });
  58251. }
  58252. else {
  58253. return next();
  58254. }
  58255. }
  58256. }
  58257. if (publicDirInRoot && url.startsWith(publicPath)) {
  58258. warnAboutExplicitPublicPathInUrl(url);
  58259. }
  58260. if (isJSRequest(url) ||
  58261. isImportRequest(url) ||
  58262. isCSSRequest(url) ||
  58263. isHTMLProxy(url)) {
  58264. // strip ?import
  58265. url = removeImportQuery(url);
  58266. // Strip valid id prefix. This is prepended to resolved Ids that are
  58267. // not valid browser import specifiers by the importAnalysis plugin.
  58268. url = unwrapId$1(url);
  58269. // for CSS, we differentiate between normal CSS requests and imports
  58270. if (isCSSRequest(url)) {
  58271. if (req.headers.accept?.includes('text/css') &&
  58272. !isDirectRequest(url)) {
  58273. url = injectQuery(url, 'direct');
  58274. }
  58275. // check if we can return 304 early for CSS requests. These aren't handled
  58276. // by the cachedTransformMiddleware due to the browser possibly mixing the
  58277. // etags of direct and imported CSS
  58278. const ifNoneMatch = req.headers['if-none-match'];
  58279. if (ifNoneMatch &&
  58280. (await server.moduleGraph.getModuleByUrl(url, false))
  58281. ?.transformResult?.etag === ifNoneMatch) {
  58282. debugCache?.(`[304] ${prettifyUrl(url, server.config.root)}`);
  58283. res.statusCode = 304;
  58284. return res.end();
  58285. }
  58286. }
  58287. // resolve, load and transform using the plugin container
  58288. const result = await transformRequest(url, server, {
  58289. html: req.headers.accept?.includes('text/html'),
  58290. });
  58291. if (result) {
  58292. const depsOptimizer = getDepsOptimizer(server.config, false); // non-ssr
  58293. const type = isDirectCSSRequest(url) ? 'css' : 'js';
  58294. const isDep = DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url);
  58295. return send(req, res, result.code, type, {
  58296. etag: result.etag,
  58297. // allow browser to cache npm deps!
  58298. cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
  58299. headers: server.config.server.headers,
  58300. map: result.map,
  58301. });
  58302. }
  58303. }
  58304. }
  58305. catch (e) {
  58306. if (e?.code === ERR_OPTIMIZE_DEPS_PROCESSING_ERROR) {
  58307. // Skip if response has already been sent
  58308. if (!res.writableEnded) {
  58309. res.statusCode = 504; // status code request timeout
  58310. res.statusMessage = 'Optimize Deps Processing Error';
  58311. res.end();
  58312. }
  58313. // This timeout is unexpected
  58314. server.config.logger.error(e.message);
  58315. return;
  58316. }
  58317. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) {
  58318. // Skip if response has already been sent
  58319. if (!res.writableEnded) {
  58320. res.statusCode = 504; // status code request timeout
  58321. res.statusMessage = 'Outdated Optimize Dep';
  58322. res.end();
  58323. }
  58324. // We don't need to log an error in this case, the request
  58325. // is outdated because new dependencies were discovered and
  58326. // the new pre-bundle dependencies have changed.
  58327. // A full-page reload has been issued, and these old requests
  58328. // can't be properly fulfilled. This isn't an unexpected
  58329. // error but a normal part of the missing deps discovery flow
  58330. return;
  58331. }
  58332. if (e?.code === ERR_CLOSED_SERVER) {
  58333. // Skip if response has already been sent
  58334. if (!res.writableEnded) {
  58335. res.statusCode = 504; // status code request timeout
  58336. res.statusMessage = 'Outdated Request';
  58337. res.end();
  58338. }
  58339. // We don't need to log an error in this case, the request
  58340. // is outdated because new dependencies were discovered and
  58341. // the new pre-bundle dependencies have changed.
  58342. // A full-page reload has been issued, and these old requests
  58343. // can't be properly fulfilled. This isn't an unexpected
  58344. // error but a normal part of the missing deps discovery flow
  58345. return;
  58346. }
  58347. if (e?.code === ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR) {
  58348. // Skip if response has already been sent
  58349. if (!res.writableEnded) {
  58350. res.statusCode = 404;
  58351. res.end();
  58352. }
  58353. server.config.logger.warn(colors$1.yellow(e.message));
  58354. return;
  58355. }
  58356. if (e?.code === ERR_LOAD_URL) {
  58357. // Let other middleware handle if we can't load the url via transformRequest
  58358. return next();
  58359. }
  58360. return next(e);
  58361. }
  58362. next();
  58363. };
  58364. function warnAboutExplicitPublicPathInUrl(url) {
  58365. let warning;
  58366. if (isImportRequest(url)) {
  58367. const rawUrl = removeImportQuery(url);
  58368. if (urlRE.test(url)) {
  58369. warning =
  58370. `Assets in the public directory are served at the root path.\n` +
  58371. `Instead of ${colors$1.cyan(rawUrl)}, use ${colors$1.cyan(rawUrl.replace(publicPath, '/'))}.`;
  58372. }
  58373. else {
  58374. warning =
  58375. 'Assets in public directory cannot be imported from JavaScript.\n' +
  58376. `If you intend to import that asset, put the file in the src directory, and use ${colors$1.cyan(rawUrl.replace(publicPath, '/src/'))} instead of ${colors$1.cyan(rawUrl)}.\n` +
  58377. `If you intend to use the URL of that asset, use ${colors$1.cyan(injectQuery(rawUrl.replace(publicPath, '/'), 'url'))}.`;
  58378. }
  58379. }
  58380. else {
  58381. warning =
  58382. `Files in the public directory are served at the root path.\n` +
  58383. `Instead of ${colors$1.cyan(url)}, use ${colors$1.cyan(url.replace(publicPath, '/'))}.`;
  58384. }
  58385. server.config.logger.warn(colors$1.yellow(warning));
  58386. }
  58387. }
  58388. function createDevHtmlTransformFn(config) {
  58389. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(config.plugins, config.logger);
  58390. const transformHooks = [
  58391. preImportMapHook(config),
  58392. injectCspNonceMetaTagHook(config),
  58393. ...preHooks,
  58394. htmlEnvHook(config),
  58395. devHtmlHook,
  58396. ...normalHooks,
  58397. ...postHooks,
  58398. injectNonceAttributeTagHook(config),
  58399. postImportMapHook(),
  58400. ];
  58401. return (server, url, html, originalUrl) => {
  58402. return applyHtmlTransforms(html, transformHooks, {
  58403. path: url,
  58404. filename: getHtmlFilename(url, server),
  58405. server,
  58406. originalUrl,
  58407. });
  58408. };
  58409. }
  58410. function getHtmlFilename(url, server) {
  58411. if (url.startsWith(FS_PREFIX)) {
  58412. return decodeURIComponent(fsPathFromId(url));
  58413. }
  58414. else {
  58415. return decodeURIComponent(normalizePath$3(path$o.join(server.config.root, url.slice(1))));
  58416. }
  58417. }
  58418. function shouldPreTransform(url, config) {
  58419. return (!checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url)));
  58420. }
  58421. const wordCharRE = /\w/;
  58422. function isBareRelative(url) {
  58423. return wordCharRE.test(url[0]) && !url.includes(':');
  58424. }
  58425. const isSrcSet = (attr) => attr.name === 'srcset' && attr.prefix === undefined;
  58426. const processNodeUrl = (url, useSrcSetReplacer, config, htmlPath, originalUrl, server, isClassicScriptLink) => {
  58427. // prefix with base (dev only, base is never relative)
  58428. const replacer = (url) => {
  58429. if (server?.moduleGraph) {
  58430. const mod = server.moduleGraph.urlToModuleMap.get(url);
  58431. if (mod && mod.lastHMRTimestamp > 0) {
  58432. url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
  58433. }
  58434. }
  58435. if ((url[0] === '/' && url[1] !== '/') ||
  58436. // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
  58437. // path will add `/a/` prefix, it will caused 404.
  58438. //
  58439. // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
  58440. //
  58441. // rewrite `./index.js` -> `localhost:5173/a/index.js`.
  58442. // rewrite `../index.js` -> `localhost:5173/index.js`.
  58443. // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
  58444. ((url[0] === '.' || isBareRelative(url)) &&
  58445. originalUrl &&
  58446. originalUrl !== '/' &&
  58447. htmlPath === '/index.html')) {
  58448. url = path$o.posix.join(config.base, url);
  58449. }
  58450. if (server && !isClassicScriptLink && shouldPreTransform(url, config)) {
  58451. let preTransformUrl;
  58452. if (url[0] === '/' && url[1] !== '/') {
  58453. preTransformUrl = url;
  58454. }
  58455. else if (url[0] === '.' || isBareRelative(url)) {
  58456. preTransformUrl = path$o.posix.join(config.base, path$o.posix.dirname(htmlPath), url);
  58457. }
  58458. if (preTransformUrl) {
  58459. preTransformRequest(server, preTransformUrl, config.base);
  58460. }
  58461. }
  58462. return url;
  58463. };
  58464. const processedUrl = useSrcSetReplacer
  58465. ? processSrcSetSync(url, ({ url }) => replacer(url))
  58466. : replacer(url);
  58467. return processedUrl;
  58468. };
  58469. const devHtmlHook = async (html, { path: htmlPath, filename, server, originalUrl }) => {
  58470. const { config, moduleGraph, watcher } = server;
  58471. const base = config.base || '/';
  58472. let proxyModulePath;
  58473. let proxyModuleUrl;
  58474. const trailingSlash = htmlPath.endsWith('/');
  58475. if (!trailingSlash && getFsUtils(config).existsSync(filename)) {
  58476. proxyModulePath = htmlPath;
  58477. proxyModuleUrl = proxyModulePath;
  58478. }
  58479. else {
  58480. // There are users of vite.transformIndexHtml calling it with url '/'
  58481. // for SSR integrations #7993, filename is root for this case
  58482. // A user may also use a valid name for a virtual html file
  58483. // Mark the path as virtual in both cases so sourcemaps aren't processed
  58484. // and ids are properly handled
  58485. const validPath = `${htmlPath}${trailingSlash ? 'index.html' : ''}`;
  58486. proxyModulePath = `\0${validPath}`;
  58487. proxyModuleUrl = wrapId$1(proxyModulePath);
  58488. }
  58489. proxyModuleUrl = joinUrlSegments(base, proxyModuleUrl);
  58490. const s = new MagicString(html);
  58491. let inlineModuleIndex = -1;
  58492. // The key to the proxyHtml cache is decoded, as it will be compared
  58493. // against decoded URLs by the HTML plugins.
  58494. const proxyCacheUrl = decodeURI(cleanUrl(proxyModulePath).replace(normalizePath$3(config.root), ''));
  58495. const styleUrl = [];
  58496. const inlineStyles = [];
  58497. const addInlineModule = (node, ext) => {
  58498. inlineModuleIndex++;
  58499. const contentNode = node.childNodes[0];
  58500. const code = contentNode.value;
  58501. let map;
  58502. if (proxyModulePath[0] !== '\0') {
  58503. map = new MagicString(html)
  58504. .snip(contentNode.sourceCodeLocation.startOffset, contentNode.sourceCodeLocation.endOffset)
  58505. .generateMap({ hires: 'boundary' });
  58506. map.sources = [filename];
  58507. map.file = filename;
  58508. }
  58509. // add HTML Proxy to Map
  58510. addToHTMLProxyCache(config, proxyCacheUrl, inlineModuleIndex, { code, map });
  58511. // inline js module. convert to src="proxy" (dev only, base is never relative)
  58512. const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`;
  58513. // invalidate the module so the newly cached contents will be served
  58514. const module = server?.moduleGraph.getModuleById(modulePath);
  58515. if (module) {
  58516. server?.moduleGraph.invalidateModule(module);
  58517. }
  58518. s.update(node.sourceCodeLocation.startOffset, node.sourceCodeLocation.endOffset, `<script type="module" src="${modulePath}"></script>`);
  58519. preTransformRequest(server, modulePath, base);
  58520. };
  58521. await traverseHtml(html, filename, (node) => {
  58522. if (!nodeIsElement(node)) {
  58523. return;
  58524. }
  58525. // script tags
  58526. if (node.nodeName === 'script') {
  58527. const { src, sourceCodeLocation, isModule } = getScriptInfo(node);
  58528. if (src) {
  58529. const processedUrl = processNodeUrl(src.value, isSrcSet(src), config, htmlPath, originalUrl, server, !isModule);
  58530. if (processedUrl !== src.value) {
  58531. overwriteAttrValue(s, sourceCodeLocation, processedUrl);
  58532. }
  58533. }
  58534. else if (isModule && node.childNodes.length) {
  58535. addInlineModule(node, 'js');
  58536. }
  58537. else if (node.childNodes.length) {
  58538. const scriptNode = node.childNodes[node.childNodes.length - 1];
  58539. for (const { url, start, end, } of extractImportExpressionFromClassicScript(scriptNode)) {
  58540. const processedUrl = processNodeUrl(url, false, config, htmlPath, originalUrl);
  58541. if (processedUrl !== url) {
  58542. s.update(start, end, processedUrl);
  58543. }
  58544. }
  58545. }
  58546. }
  58547. const inlineStyle = findNeedTransformStyleAttribute(node);
  58548. if (inlineStyle) {
  58549. inlineModuleIndex++;
  58550. inlineStyles.push({
  58551. index: inlineModuleIndex,
  58552. location: inlineStyle.location,
  58553. code: inlineStyle.attr.value,
  58554. });
  58555. }
  58556. if (node.nodeName === 'style' && node.childNodes.length) {
  58557. const children = node.childNodes[0];
  58558. styleUrl.push({
  58559. start: children.sourceCodeLocation.startOffset,
  58560. end: children.sourceCodeLocation.endOffset,
  58561. code: children.value,
  58562. });
  58563. }
  58564. // elements with [href/src] attrs
  58565. const assetAttrs = assetAttrsConfig[node.nodeName];
  58566. if (assetAttrs) {
  58567. for (const p of node.attrs) {
  58568. const attrKey = getAttrKey(p);
  58569. if (p.value && assetAttrs.includes(attrKey)) {
  58570. const processedUrl = processNodeUrl(p.value, isSrcSet(p), config, htmlPath, originalUrl);
  58571. if (processedUrl !== p.value) {
  58572. overwriteAttrValue(s, node.sourceCodeLocation.attrs[attrKey], processedUrl);
  58573. }
  58574. }
  58575. }
  58576. }
  58577. });
  58578. await Promise.all([
  58579. ...styleUrl.map(async ({ start, end, code }, index) => {
  58580. const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`;
  58581. // ensure module in graph after successful load
  58582. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  58583. ensureWatchedFile(watcher, mod.file, config.root);
  58584. const result = await server.pluginContainer.transform(code, mod.id);
  58585. let content = '';
  58586. if (result) {
  58587. if (result.map && 'version' in result.map) {
  58588. if (result.map.mappings) {
  58589. await injectSourcesContent(result.map, proxyModulePath, config.logger);
  58590. }
  58591. content = getCodeWithSourcemap('css', result.code, result.map);
  58592. }
  58593. else {
  58594. content = result.code;
  58595. }
  58596. }
  58597. s.overwrite(start, end, content);
  58598. }),
  58599. ...inlineStyles.map(async ({ index, location, code }) => {
  58600. // will transform with css plugin and cache result with css-post plugin
  58601. const url = `${proxyModulePath}?html-proxy&inline-css&style-attr&index=${index}.css`;
  58602. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  58603. ensureWatchedFile(watcher, mod.file, config.root);
  58604. await server?.pluginContainer.transform(code, mod.id);
  58605. const hash = getHash(cleanUrl(mod.id));
  58606. const result = htmlProxyResult.get(`${hash}_${index}`);
  58607. overwriteAttrValue(s, location, result ?? '');
  58608. }),
  58609. ]);
  58610. html = s.toString();
  58611. return {
  58612. html,
  58613. tags: [
  58614. {
  58615. tag: 'script',
  58616. attrs: {
  58617. type: 'module',
  58618. src: path$o.posix.join(base, CLIENT_PUBLIC_PATH),
  58619. },
  58620. injectTo: 'head-prepend',
  58621. },
  58622. ],
  58623. };
  58624. };
  58625. function indexHtmlMiddleware(root, server) {
  58626. const isDev = isDevServer(server);
  58627. const fsUtils = getFsUtils(server.config);
  58628. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58629. return async function viteIndexHtmlMiddleware(req, res, next) {
  58630. if (res.writableEnded) {
  58631. return next();
  58632. }
  58633. const url = req.url && cleanUrl(req.url);
  58634. // htmlFallbackMiddleware appends '.html' to URLs
  58635. if (url?.endsWith('.html') && req.headers['sec-fetch-dest'] !== 'script') {
  58636. let filePath;
  58637. if (isDev && url.startsWith(FS_PREFIX)) {
  58638. filePath = decodeURIComponent(fsPathFromId(url));
  58639. }
  58640. else {
  58641. filePath = path$o.join(root, decodeURIComponent(url));
  58642. }
  58643. if (fsUtils.existsSync(filePath)) {
  58644. const headers = isDev
  58645. ? server.config.server.headers
  58646. : server.config.preview.headers;
  58647. try {
  58648. let html = await fsp.readFile(filePath, 'utf-8');
  58649. if (isDev) {
  58650. html = await server.transformIndexHtml(url, html, req.originalUrl);
  58651. }
  58652. return send(req, res, html, 'html', { headers });
  58653. }
  58654. catch (e) {
  58655. return next(e);
  58656. }
  58657. }
  58658. }
  58659. next();
  58660. };
  58661. }
  58662. function preTransformRequest(server, url, base) {
  58663. if (!server.config.server.preTransformRequests)
  58664. return;
  58665. // transform all url as non-ssr as html includes client-side assets only
  58666. try {
  58667. url = unwrapId$1(stripBase(decodeURI(url), base));
  58668. }
  58669. catch {
  58670. // ignore
  58671. return;
  58672. }
  58673. server.warmupRequest(url);
  58674. }
  58675. const logTime = createDebugger('vite:time');
  58676. function timeMiddleware(root) {
  58677. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  58678. return function viteTimeMiddleware(req, res, next) {
  58679. const start = performance.now();
  58680. const end = res.end;
  58681. res.end = (...args) => {
  58682. logTime?.(`${timeFrom(start)} ${prettifyUrl(req.url, root)}`);
  58683. return end.call(res, ...args);
  58684. };
  58685. next();
  58686. };
  58687. }
  58688. class ModuleNode {
  58689. /**
  58690. * Public served url path, starts with /
  58691. */
  58692. url;
  58693. /**
  58694. * Resolved file system path + query
  58695. */
  58696. id = null;
  58697. file = null;
  58698. type;
  58699. info;
  58700. meta;
  58701. importers = new Set();
  58702. clientImportedModules = new Set();
  58703. ssrImportedModules = new Set();
  58704. acceptedHmrDeps = new Set();
  58705. acceptedHmrExports = null;
  58706. importedBindings = null;
  58707. isSelfAccepting;
  58708. transformResult = null;
  58709. ssrTransformResult = null;
  58710. ssrModule = null;
  58711. ssrError = null;
  58712. lastHMRTimestamp = 0;
  58713. /**
  58714. * `import.meta.hot.invalidate` is called by the client.
  58715. * If there's multiple clients, multiple `invalidate` request is received.
  58716. * This property is used to dedupe those request to avoid multiple updates happening.
  58717. * @internal
  58718. */
  58719. lastHMRInvalidationReceived = false;
  58720. lastInvalidationTimestamp = 0;
  58721. /**
  58722. * If the module only needs to update its imports timestamp (e.g. within an HMR chain),
  58723. * it is considered soft-invalidated. In this state, its `transformResult` should exist,
  58724. * and the next `transformRequest` for this module will replace the timestamps.
  58725. *
  58726. * By default the value is `undefined` if it's not soft/hard-invalidated. If it gets
  58727. * soft-invalidated, this will contain the previous `transformResult` value. If it gets
  58728. * hard-invalidated, this will be set to `'HARD_INVALIDATED'`.
  58729. * @internal
  58730. */
  58731. invalidationState;
  58732. /**
  58733. * @internal
  58734. */
  58735. ssrInvalidationState;
  58736. /**
  58737. * The module urls that are statically imported in the code. This information is separated
  58738. * out from `importedModules` as only importers that statically import the module can be
  58739. * soft invalidated. Other imports (e.g. watched files) needs the importer to be hard invalidated.
  58740. * @internal
  58741. */
  58742. staticImportedUrls;
  58743. /**
  58744. * @param setIsSelfAccepting - set `false` to set `isSelfAccepting` later. e.g. #7870
  58745. */
  58746. constructor(url, setIsSelfAccepting = true) {
  58747. this.url = url;
  58748. this.type = isDirectCSSRequest(url) ? 'css' : 'js';
  58749. if (setIsSelfAccepting) {
  58750. this.isSelfAccepting = false;
  58751. }
  58752. }
  58753. get importedModules() {
  58754. const importedModules = new Set(this.clientImportedModules);
  58755. for (const module of this.ssrImportedModules) {
  58756. importedModules.add(module);
  58757. }
  58758. return importedModules;
  58759. }
  58760. }
  58761. class ModuleGraph {
  58762. resolveId;
  58763. urlToModuleMap = new Map();
  58764. idToModuleMap = new Map();
  58765. etagToModuleMap = new Map();
  58766. // a single file may corresponds to multiple modules with different queries
  58767. fileToModulesMap = new Map();
  58768. safeModulesPath = new Set();
  58769. /**
  58770. * @internal
  58771. */
  58772. _unresolvedUrlToModuleMap = new Map();
  58773. /**
  58774. * @internal
  58775. */
  58776. _ssrUnresolvedUrlToModuleMap = new Map();
  58777. /** @internal */
  58778. _hasResolveFailedErrorModules = new Set();
  58779. constructor(resolveId) {
  58780. this.resolveId = resolveId;
  58781. }
  58782. async getModuleByUrl(rawUrl, ssr) {
  58783. // Quick path, if we already have a module for this rawUrl (even without extension)
  58784. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58785. const mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58786. if (mod) {
  58787. return mod;
  58788. }
  58789. const [url] = await this._resolveUrl(rawUrl, ssr);
  58790. return this.urlToModuleMap.get(url);
  58791. }
  58792. getModuleById(id) {
  58793. return this.idToModuleMap.get(removeTimestampQuery(id));
  58794. }
  58795. getModulesByFile(file) {
  58796. return this.fileToModulesMap.get(file);
  58797. }
  58798. onFileChange(file) {
  58799. const mods = this.getModulesByFile(file);
  58800. if (mods) {
  58801. const seen = new Set();
  58802. mods.forEach((mod) => {
  58803. this.invalidateModule(mod, seen);
  58804. });
  58805. }
  58806. }
  58807. onFileDelete(file) {
  58808. const mods = this.getModulesByFile(file);
  58809. if (mods) {
  58810. mods.forEach((mod) => {
  58811. mod.importedModules.forEach((importedMod) => {
  58812. importedMod.importers.delete(mod);
  58813. });
  58814. });
  58815. }
  58816. }
  58817. invalidateModule(mod, seen = new Set(), timestamp = Date.now(), isHmr = false,
  58818. /** @internal */
  58819. softInvalidate = false) {
  58820. const prevInvalidationState = mod.invalidationState;
  58821. const prevSsrInvalidationState = mod.ssrInvalidationState;
  58822. // Handle soft invalidation before the `seen` check, as consecutive soft/hard invalidations can
  58823. // cause the final soft invalidation state to be different.
  58824. // If soft invalidated, save the previous `transformResult` so that we can reuse and transform the
  58825. // import timestamps only in `transformRequest`. If there's no previous `transformResult`, hard invalidate it.
  58826. if (softInvalidate) {
  58827. mod.invalidationState ??= mod.transformResult ?? 'HARD_INVALIDATED';
  58828. mod.ssrInvalidationState ??= mod.ssrTransformResult ?? 'HARD_INVALIDATED';
  58829. }
  58830. // If hard invalidated, further soft invalidations have no effect until it's reset to `undefined`
  58831. else {
  58832. mod.invalidationState = 'HARD_INVALIDATED';
  58833. mod.ssrInvalidationState = 'HARD_INVALIDATED';
  58834. }
  58835. // Skip updating the module if it was already invalidated before and the invalidation state has not changed
  58836. if (seen.has(mod) &&
  58837. prevInvalidationState === mod.invalidationState &&
  58838. prevSsrInvalidationState === mod.ssrInvalidationState) {
  58839. return;
  58840. }
  58841. seen.add(mod);
  58842. if (isHmr) {
  58843. mod.lastHMRTimestamp = timestamp;
  58844. mod.lastHMRInvalidationReceived = false;
  58845. }
  58846. else {
  58847. // Save the timestamp for this invalidation, so we can avoid caching the result of possible already started
  58848. // processing being done for this module
  58849. mod.lastInvalidationTimestamp = timestamp;
  58850. }
  58851. // Don't invalidate mod.info and mod.meta, as they are part of the processing pipeline
  58852. // Invalidating the transform result is enough to ensure this module is re-processed next time it is requested
  58853. const etag = mod.transformResult?.etag;
  58854. if (etag)
  58855. this.etagToModuleMap.delete(etag);
  58856. mod.transformResult = null;
  58857. mod.ssrTransformResult = null;
  58858. mod.ssrModule = null;
  58859. mod.ssrError = null;
  58860. mod.importers.forEach((importer) => {
  58861. if (!importer.acceptedHmrDeps.has(mod)) {
  58862. // If the importer statically imports the current module, we can soft-invalidate the importer
  58863. // to only update the import timestamps. If it's not statically imported, e.g. watched/glob file,
  58864. // we can only soft invalidate if the current module was also soft-invalidated. A soft-invalidation
  58865. // doesn't need to trigger a re-load and re-transform of the importer.
  58866. const shouldSoftInvalidateImporter = importer.staticImportedUrls?.has(mod.url) || softInvalidate;
  58867. this.invalidateModule(importer, seen, timestamp, isHmr, shouldSoftInvalidateImporter);
  58868. }
  58869. });
  58870. this._hasResolveFailedErrorModules.delete(mod);
  58871. }
  58872. invalidateAll() {
  58873. const timestamp = Date.now();
  58874. const seen = new Set();
  58875. this.idToModuleMap.forEach((mod) => {
  58876. this.invalidateModule(mod, seen, timestamp);
  58877. });
  58878. }
  58879. /**
  58880. * Update the module graph based on a module's updated imports information
  58881. * If there are dependencies that no longer have any importers, they are
  58882. * returned as a Set.
  58883. *
  58884. * @param staticImportedUrls Subset of `importedModules` where they're statically imported in code.
  58885. * This is only used for soft invalidations so `undefined` is fine but may cause more runtime processing.
  58886. */
  58887. async updateModuleInfo(mod, importedModules, importedBindings, acceptedModules, acceptedExports, isSelfAccepting, ssr,
  58888. /** @internal */
  58889. staticImportedUrls) {
  58890. mod.isSelfAccepting = isSelfAccepting;
  58891. const prevImports = ssr ? mod.ssrImportedModules : mod.clientImportedModules;
  58892. let noLongerImported;
  58893. let resolvePromises = [];
  58894. let resolveResults = new Array(importedModules.size);
  58895. let index = 0;
  58896. // update import graph
  58897. for (const imported of importedModules) {
  58898. const nextIndex = index++;
  58899. if (typeof imported === 'string') {
  58900. resolvePromises.push(this.ensureEntryFromUrl(imported, ssr).then((dep) => {
  58901. dep.importers.add(mod);
  58902. resolveResults[nextIndex] = dep;
  58903. }));
  58904. }
  58905. else {
  58906. imported.importers.add(mod);
  58907. resolveResults[nextIndex] = imported;
  58908. }
  58909. }
  58910. if (resolvePromises.length) {
  58911. await Promise.all(resolvePromises);
  58912. }
  58913. const nextImports = new Set(resolveResults);
  58914. if (ssr) {
  58915. mod.ssrImportedModules = nextImports;
  58916. }
  58917. else {
  58918. mod.clientImportedModules = nextImports;
  58919. }
  58920. // remove the importer from deps that were imported but no longer are.
  58921. prevImports.forEach((dep) => {
  58922. if (!mod.clientImportedModules.has(dep) &&
  58923. !mod.ssrImportedModules.has(dep)) {
  58924. dep.importers.delete(mod);
  58925. if (!dep.importers.size) {
  58926. (noLongerImported || (noLongerImported = new Set())).add(dep);
  58927. }
  58928. }
  58929. });
  58930. // update accepted hmr deps
  58931. resolvePromises = [];
  58932. resolveResults = new Array(acceptedModules.size);
  58933. index = 0;
  58934. for (const accepted of acceptedModules) {
  58935. const nextIndex = index++;
  58936. if (typeof accepted === 'string') {
  58937. resolvePromises.push(this.ensureEntryFromUrl(accepted, ssr).then((dep) => {
  58938. resolveResults[nextIndex] = dep;
  58939. }));
  58940. }
  58941. else {
  58942. resolveResults[nextIndex] = accepted;
  58943. }
  58944. }
  58945. if (resolvePromises.length) {
  58946. await Promise.all(resolvePromises);
  58947. }
  58948. mod.acceptedHmrDeps = new Set(resolveResults);
  58949. mod.staticImportedUrls = staticImportedUrls;
  58950. // update accepted hmr exports
  58951. mod.acceptedHmrExports = acceptedExports;
  58952. mod.importedBindings = importedBindings;
  58953. return noLongerImported;
  58954. }
  58955. async ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true) {
  58956. return this._ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting);
  58957. }
  58958. /**
  58959. * @internal
  58960. */
  58961. async _ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true,
  58962. // Optimization, avoid resolving the same url twice if the caller already did it
  58963. resolved) {
  58964. // Quick path, if we already have a module for this rawUrl (even without extension)
  58965. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58966. let mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58967. if (mod) {
  58968. return mod;
  58969. }
  58970. const modPromise = (async () => {
  58971. const [url, resolvedId, meta] = await this._resolveUrl(rawUrl, ssr, resolved);
  58972. mod = this.idToModuleMap.get(resolvedId);
  58973. if (!mod) {
  58974. mod = new ModuleNode(url, setIsSelfAccepting);
  58975. if (meta)
  58976. mod.meta = meta;
  58977. this.urlToModuleMap.set(url, mod);
  58978. mod.id = resolvedId;
  58979. this.idToModuleMap.set(resolvedId, mod);
  58980. const file = (mod.file = cleanUrl(resolvedId));
  58981. let fileMappedModules = this.fileToModulesMap.get(file);
  58982. if (!fileMappedModules) {
  58983. fileMappedModules = new Set();
  58984. this.fileToModulesMap.set(file, fileMappedModules);
  58985. }
  58986. fileMappedModules.add(mod);
  58987. }
  58988. // multiple urls can map to the same module and id, make sure we register
  58989. // the url to the existing module in that case
  58990. else if (!this.urlToModuleMap.has(url)) {
  58991. this.urlToModuleMap.set(url, mod);
  58992. }
  58993. this._setUnresolvedUrlToModule(rawUrl, mod, ssr);
  58994. return mod;
  58995. })();
  58996. // Also register the clean url to the module, so that we can short-circuit
  58997. // resolving the same url twice
  58998. this._setUnresolvedUrlToModule(rawUrl, modPromise, ssr);
  58999. return modPromise;
  59000. }
  59001. // some deps, like a css file referenced via @import, don't have its own
  59002. // url because they are inlined into the main css import. But they still
  59003. // need to be represented in the module graph so that they can trigger
  59004. // hmr in the importing css file.
  59005. createFileOnlyEntry(file) {
  59006. file = normalizePath$3(file);
  59007. let fileMappedModules = this.fileToModulesMap.get(file);
  59008. if (!fileMappedModules) {
  59009. fileMappedModules = new Set();
  59010. this.fileToModulesMap.set(file, fileMappedModules);
  59011. }
  59012. const url = `${FS_PREFIX}${file}`;
  59013. for (const m of fileMappedModules) {
  59014. if (m.url === url || m.id === file) {
  59015. return m;
  59016. }
  59017. }
  59018. const mod = new ModuleNode(url);
  59019. mod.file = file;
  59020. fileMappedModules.add(mod);
  59021. return mod;
  59022. }
  59023. // for incoming urls, it is important to:
  59024. // 1. remove the HMR timestamp query (?t=xxxx) and the ?import query
  59025. // 2. resolve its extension so that urls with or without extension all map to
  59026. // the same module
  59027. async resolveUrl(url, ssr) {
  59028. url = removeImportQuery(removeTimestampQuery(url));
  59029. const mod = await this._getUnresolvedUrlToModule(url, ssr);
  59030. if (mod?.id) {
  59031. return [mod.url, mod.id, mod.meta];
  59032. }
  59033. return this._resolveUrl(url, ssr);
  59034. }
  59035. updateModuleTransformResult(mod, result, ssr) {
  59036. if (ssr) {
  59037. mod.ssrTransformResult = result;
  59038. }
  59039. else {
  59040. const prevEtag = mod.transformResult?.etag;
  59041. if (prevEtag)
  59042. this.etagToModuleMap.delete(prevEtag);
  59043. mod.transformResult = result;
  59044. if (result?.etag)
  59045. this.etagToModuleMap.set(result.etag, mod);
  59046. }
  59047. }
  59048. getModuleByEtag(etag) {
  59049. return this.etagToModuleMap.get(etag);
  59050. }
  59051. /**
  59052. * @internal
  59053. */
  59054. _getUnresolvedUrlToModule(url, ssr) {
  59055. return (ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap).get(url);
  59056. }
  59057. /**
  59058. * @internal
  59059. */
  59060. _setUnresolvedUrlToModule(url, mod, ssr) {
  59061. (ssr
  59062. ? this._ssrUnresolvedUrlToModuleMap
  59063. : this._unresolvedUrlToModuleMap).set(url, mod);
  59064. }
  59065. /**
  59066. * @internal
  59067. */
  59068. async _resolveUrl(url, ssr, alreadyResolved) {
  59069. const resolved = alreadyResolved ?? (await this.resolveId(url, !!ssr));
  59070. const resolvedId = resolved?.id || url;
  59071. if (url !== resolvedId &&
  59072. !url.includes('\0') &&
  59073. !url.startsWith(`virtual:`)) {
  59074. const ext = extname$1(cleanUrl(resolvedId));
  59075. if (ext) {
  59076. const pathname = cleanUrl(url);
  59077. if (!pathname.endsWith(ext)) {
  59078. url = pathname + ext + url.slice(pathname.length);
  59079. }
  59080. }
  59081. }
  59082. return [url, resolvedId, resolved?.meta];
  59083. }
  59084. }
  59085. function notFoundMiddleware() {
  59086. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  59087. return function vite404Middleware(_, res) {
  59088. res.statusCode = 404;
  59089. res.end();
  59090. };
  59091. }
  59092. // https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
  59093. const ROOT_FILES = [
  59094. // '.git',
  59095. // https://pnpm.io/workspaces/
  59096. 'pnpm-workspace.yaml',
  59097. // https://rushjs.io/pages/advanced/config_files/
  59098. // 'rush.json',
  59099. // https://nx.dev/latest/react/getting-started/nx-setup
  59100. // 'workspace.json',
  59101. // 'nx.json',
  59102. // https://github.com/lerna/lerna#lernajson
  59103. 'lerna.json',
  59104. ];
  59105. // npm: https://docs.npmjs.com/cli/v7/using-npm/workspaces#installing-workspaces
  59106. // yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
  59107. function hasWorkspacePackageJSON(root) {
  59108. const path = join$2(root, 'package.json');
  59109. if (!isFileReadable(path)) {
  59110. return false;
  59111. }
  59112. try {
  59113. const content = JSON.parse(fs$l.readFileSync(path, 'utf-8')) || {};
  59114. return !!content.workspaces;
  59115. }
  59116. catch {
  59117. return false;
  59118. }
  59119. }
  59120. function hasRootFile(root) {
  59121. return ROOT_FILES.some((file) => fs$l.existsSync(join$2(root, file)));
  59122. }
  59123. function hasPackageJSON(root) {
  59124. const path = join$2(root, 'package.json');
  59125. return fs$l.existsSync(path);
  59126. }
  59127. /**
  59128. * Search up for the nearest `package.json`
  59129. */
  59130. function searchForPackageRoot(current, root = current) {
  59131. if (hasPackageJSON(current))
  59132. return current;
  59133. const dir = dirname$2(current);
  59134. // reach the fs root
  59135. if (!dir || dir === current)
  59136. return root;
  59137. return searchForPackageRoot(dir, root);
  59138. }
  59139. /**
  59140. * Search up for the nearest workspace root
  59141. */
  59142. function searchForWorkspaceRoot(current, root = searchForPackageRoot(current)) {
  59143. if (hasRootFile(current))
  59144. return current;
  59145. if (hasWorkspacePackageJSON(current))
  59146. return current;
  59147. const dir = dirname$2(current);
  59148. // reach the fs root
  59149. if (!dir || dir === current)
  59150. return root;
  59151. return searchForWorkspaceRoot(dir, root);
  59152. }
  59153. function warmupFiles(server) {
  59154. const options = server.config.server.warmup;
  59155. const root = server.config.root;
  59156. if (options?.clientFiles?.length) {
  59157. mapFiles(options.clientFiles, root).then((files) => {
  59158. for (const file of files) {
  59159. warmupFile(server, file, false);
  59160. }
  59161. });
  59162. }
  59163. if (options?.ssrFiles?.length) {
  59164. mapFiles(options.ssrFiles, root).then((files) => {
  59165. for (const file of files) {
  59166. warmupFile(server, file, true);
  59167. }
  59168. });
  59169. }
  59170. }
  59171. async function warmupFile(server, file, ssr) {
  59172. // transform html with the `transformIndexHtml` hook as Vite internals would
  59173. // pre-transform the imported JS modules linked. this may cause `transformIndexHtml`
  59174. // plugins to be executed twice, but that's probably fine.
  59175. if (file.endsWith('.html')) {
  59176. const url = htmlFileToUrl(file, server.config.root);
  59177. if (url) {
  59178. try {
  59179. const html = await fsp.readFile(file, 'utf-8');
  59180. await server.transformIndexHtml(url, html);
  59181. }
  59182. catch (e) {
  59183. // Unexpected error, log the issue but avoid an unhandled exception
  59184. server.config.logger.error(`Pre-transform error (${colors$1.cyan(file)}): ${e.message}`, {
  59185. error: e,
  59186. timestamp: true,
  59187. });
  59188. }
  59189. }
  59190. }
  59191. // for other files, pass it through `transformRequest` with warmup
  59192. else {
  59193. const url = fileToUrl(file, server.config.root);
  59194. await server.warmupRequest(url, { ssr });
  59195. }
  59196. }
  59197. function htmlFileToUrl(file, root) {
  59198. const url = path$o.relative(root, file);
  59199. // out of root, ignore file
  59200. if (url[0] === '.')
  59201. return;
  59202. // file within root, create root-relative url
  59203. return '/' + normalizePath$3(url);
  59204. }
  59205. function fileToUrl(file, root) {
  59206. const url = path$o.relative(root, file);
  59207. // out of root, use /@fs/ prefix
  59208. if (url[0] === '.') {
  59209. return path$o.posix.join(FS_PREFIX, normalizePath$3(file));
  59210. }
  59211. // file within root, create root-relative url
  59212. return '/' + normalizePath$3(url);
  59213. }
  59214. function mapFiles(files, root) {
  59215. return glob(files, {
  59216. cwd: root,
  59217. absolute: true,
  59218. });
  59219. }
  59220. function createServer(inlineConfig = {}) {
  59221. return _createServer(inlineConfig, { hotListen: true });
  59222. }
  59223. async function _createServer(inlineConfig = {}, options) {
  59224. const config = await resolveConfig(inlineConfig, 'serve');
  59225. const initPublicFilesPromise = initPublicFiles(config);
  59226. const { root, server: serverConfig } = config;
  59227. const httpsOptions = await resolveHttpsConfig(config.server.https);
  59228. const { middlewareMode } = serverConfig;
  59229. const resolvedOutDirs = getResolvedOutDirs(config.root, config.build.outDir, config.build.rollupOptions?.output);
  59230. const emptyOutDir = resolveEmptyOutDir(config.build.emptyOutDir, config.root, resolvedOutDirs);
  59231. const resolvedWatchOptions = resolveChokidarOptions(config, {
  59232. disableGlobbing: true,
  59233. ...serverConfig.watch,
  59234. }, resolvedOutDirs, emptyOutDir);
  59235. const middlewares = connect$1();
  59236. const httpServer = middlewareMode
  59237. ? null
  59238. : await resolveHttpServer(serverConfig, middlewares, httpsOptions);
  59239. const ws = createWebSocketServer(httpServer, config, httpsOptions);
  59240. const hot = createHMRBroadcaster()
  59241. .addChannel(ws)
  59242. .addChannel(createServerHMRChannel());
  59243. if (typeof config.server.hmr === 'object' && config.server.hmr.channels) {
  59244. config.server.hmr.channels.forEach((channel) => hot.addChannel(channel));
  59245. }
  59246. if (httpServer) {
  59247. setClientErrorHandler(httpServer, config.logger);
  59248. }
  59249. // eslint-disable-next-line eqeqeq
  59250. const watchEnabled = serverConfig.watch !== null;
  59251. const watcher = watchEnabled
  59252. ? chokidar.watch(
  59253. // config file dependencies and env file might be outside of root
  59254. [
  59255. root,
  59256. ...config.configFileDependencies,
  59257. ...getEnvFilesForMode(config.mode, config.envDir),
  59258. ], resolvedWatchOptions)
  59259. : createNoopWatcher(resolvedWatchOptions);
  59260. const moduleGraph = new ModuleGraph((url, ssr) => container.resolveId(url, undefined, { ssr }));
  59261. const container = await createPluginContainer(config, moduleGraph, watcher);
  59262. const closeHttpServer = createServerCloseFn(httpServer);
  59263. let exitProcess;
  59264. const devHtmlTransformFn = createDevHtmlTransformFn(config);
  59265. const onCrawlEndCallbacks = [];
  59266. const crawlEndFinder = setupOnCrawlEnd(() => {
  59267. onCrawlEndCallbacks.forEach((cb) => cb());
  59268. });
  59269. function waitForRequestsIdle(ignoredId) {
  59270. return crawlEndFinder.waitForRequestsIdle(ignoredId);
  59271. }
  59272. function _registerRequestProcessing(id, done) {
  59273. crawlEndFinder.registerRequestProcessing(id, done);
  59274. }
  59275. function _onCrawlEnd(cb) {
  59276. onCrawlEndCallbacks.push(cb);
  59277. }
  59278. let server = {
  59279. config,
  59280. middlewares,
  59281. httpServer,
  59282. watcher,
  59283. pluginContainer: container,
  59284. ws,
  59285. hot,
  59286. moduleGraph,
  59287. resolvedUrls: null,
  59288. ssrTransform(code, inMap, url, originalCode = code) {
  59289. return ssrTransform(code, inMap, url, originalCode, server.config);
  59290. },
  59291. transformRequest(url, options) {
  59292. return transformRequest(url, server, options);
  59293. },
  59294. async warmupRequest(url, options) {
  59295. try {
  59296. await transformRequest(url, server, options);
  59297. }
  59298. catch (e) {
  59299. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP ||
  59300. e?.code === ERR_CLOSED_SERVER) {
  59301. // these are expected errors
  59302. return;
  59303. }
  59304. // Unexpected error, log the issue but avoid an unhandled exception
  59305. server.config.logger.error(`Pre-transform error: ${e.message}`, {
  59306. error: e,
  59307. timestamp: true,
  59308. });
  59309. }
  59310. },
  59311. transformIndexHtml(url, html, originalUrl) {
  59312. return devHtmlTransformFn(server, url, html, originalUrl);
  59313. },
  59314. async ssrLoadModule(url, opts) {
  59315. return ssrLoadModule(url, server, undefined, undefined, opts?.fixStacktrace);
  59316. },
  59317. async ssrFetchModule(url, importer) {
  59318. return ssrFetchModule(server, url, importer);
  59319. },
  59320. ssrFixStacktrace(e) {
  59321. ssrFixStacktrace(e, moduleGraph);
  59322. },
  59323. ssrRewriteStacktrace(stack) {
  59324. return ssrRewriteStacktrace(stack, moduleGraph);
  59325. },
  59326. async reloadModule(module) {
  59327. if (serverConfig.hmr !== false && module.file) {
  59328. updateModules(module.file, [module], Date.now(), server);
  59329. }
  59330. },
  59331. async listen(port, isRestart) {
  59332. await startServer(server, port);
  59333. if (httpServer) {
  59334. server.resolvedUrls = await resolveServerUrls(httpServer, config.server, config);
  59335. if (!isRestart && config.server.open)
  59336. server.openBrowser();
  59337. }
  59338. return server;
  59339. },
  59340. openBrowser() {
  59341. const options = server.config.server;
  59342. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  59343. if (url) {
  59344. const path = typeof options.open === 'string'
  59345. ? new URL(options.open, url).href
  59346. : url;
  59347. // We know the url that the browser would be opened to, so we can
  59348. // start the request while we are awaiting the browser. This will
  59349. // start the crawling of static imports ~500ms before.
  59350. // preTransformRequests needs to be enabled for this optimization.
  59351. if (server.config.server.preTransformRequests) {
  59352. setTimeout(() => {
  59353. const getMethod = path.startsWith('https:') ? get$1 : get$2;
  59354. getMethod(path, {
  59355. headers: {
  59356. // Allow the history middleware to redirect to /index.html
  59357. Accept: 'text/html',
  59358. },
  59359. }, (res) => {
  59360. res.on('end', () => {
  59361. // Ignore response, scripts discovered while processing the entry
  59362. // will be preprocessed (server.config.server.preTransformRequests)
  59363. });
  59364. })
  59365. .on('error', () => {
  59366. // Ignore errors
  59367. })
  59368. .end();
  59369. }, 0);
  59370. }
  59371. openBrowser(path, true, server.config.logger);
  59372. }
  59373. else {
  59374. server.config.logger.warn('No URL available to open in browser');
  59375. }
  59376. },
  59377. async close() {
  59378. if (!middlewareMode) {
  59379. process.off('SIGTERM', exitProcess);
  59380. if (process.env.CI !== 'true') {
  59381. process.stdin.off('end', exitProcess);
  59382. }
  59383. }
  59384. await Promise.allSettled([
  59385. watcher.close(),
  59386. hot.close(),
  59387. container.close(),
  59388. crawlEndFinder?.cancel(),
  59389. getDepsOptimizer(server.config)?.close(),
  59390. getDepsOptimizer(server.config, true)?.close(),
  59391. closeHttpServer(),
  59392. ]);
  59393. // Await pending requests. We throw early in transformRequest
  59394. // and in hooks if the server is closing for non-ssr requests,
  59395. // so the import analysis plugin stops pre-transforming static
  59396. // imports and this block is resolved sooner.
  59397. // During SSR, we let pending requests finish to avoid exposing
  59398. // the server closed error to the users.
  59399. while (server._pendingRequests.size > 0) {
  59400. await Promise.allSettled([...server._pendingRequests.values()].map((pending) => pending.request));
  59401. }
  59402. server.resolvedUrls = null;
  59403. },
  59404. printUrls() {
  59405. if (server.resolvedUrls) {
  59406. printServerUrls(server.resolvedUrls, serverConfig.host, config.logger.info);
  59407. }
  59408. else if (middlewareMode) {
  59409. throw new Error('cannot print server URLs in middleware mode.');
  59410. }
  59411. else {
  59412. throw new Error('cannot print server URLs before server.listen is called.');
  59413. }
  59414. },
  59415. bindCLIShortcuts(options) {
  59416. bindCLIShortcuts(server, options);
  59417. },
  59418. async restart(forceOptimize) {
  59419. if (!server._restartPromise) {
  59420. server._forceOptimizeOnRestart = !!forceOptimize;
  59421. server._restartPromise = restartServer(server).finally(() => {
  59422. server._restartPromise = null;
  59423. server._forceOptimizeOnRestart = false;
  59424. });
  59425. }
  59426. return server._restartPromise;
  59427. },
  59428. waitForRequestsIdle,
  59429. _registerRequestProcessing,
  59430. _onCrawlEnd,
  59431. _setInternalServer(_server) {
  59432. // Rebind internal the server variable so functions reference the user
  59433. // server instance after a restart
  59434. server = _server;
  59435. },
  59436. _restartPromise: null,
  59437. _importGlobMap: new Map(),
  59438. _forceOptimizeOnRestart: false,
  59439. _pendingRequests: new Map(),
  59440. _fsDenyGlob: picomatch$4(
  59441. // matchBase: true does not work as it's documented
  59442. // https://github.com/micromatch/picomatch/issues/89
  59443. // convert patterns without `/` on our side for now
  59444. config.server.fs.deny.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`), {
  59445. matchBase: false,
  59446. nocase: true,
  59447. dot: true,
  59448. }),
  59449. _shortcutsOptions: undefined,
  59450. };
  59451. // maintain consistency with the server instance after restarting.
  59452. const reflexServer = new Proxy(server, {
  59453. get: (_, property) => {
  59454. return server[property];
  59455. },
  59456. set: (_, property, value) => {
  59457. server[property] = value;
  59458. return true;
  59459. },
  59460. });
  59461. if (!middlewareMode) {
  59462. exitProcess = async () => {
  59463. try {
  59464. await server.close();
  59465. }
  59466. finally {
  59467. process.exit();
  59468. }
  59469. };
  59470. process.once('SIGTERM', exitProcess);
  59471. if (process.env.CI !== 'true') {
  59472. process.stdin.on('end', exitProcess);
  59473. }
  59474. }
  59475. const publicFiles = await initPublicFilesPromise;
  59476. const onHMRUpdate = async (type, file) => {
  59477. if (serverConfig.hmr !== false) {
  59478. try {
  59479. await handleHMRUpdate(type, file, server);
  59480. }
  59481. catch (err) {
  59482. hot.send({
  59483. type: 'error',
  59484. err: prepareError(err),
  59485. });
  59486. }
  59487. }
  59488. };
  59489. const { publicDir } = config;
  59490. const onFileAddUnlink = async (file, isUnlink) => {
  59491. file = normalizePath$3(file);
  59492. await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' });
  59493. if (publicDir && publicFiles) {
  59494. if (file.startsWith(publicDir)) {
  59495. const path = file.slice(publicDir.length);
  59496. publicFiles[isUnlink ? 'delete' : 'add'](path);
  59497. if (!isUnlink) {
  59498. const moduleWithSamePath = await moduleGraph.getModuleByUrl(path);
  59499. const etag = moduleWithSamePath?.transformResult?.etag;
  59500. if (etag) {
  59501. // The public file should win on the next request over a module with the
  59502. // same path. Prevent the transform etag fast path from serving the module
  59503. moduleGraph.etagToModuleMap.delete(etag);
  59504. }
  59505. }
  59506. }
  59507. }
  59508. if (isUnlink)
  59509. moduleGraph.onFileDelete(file);
  59510. await onHMRUpdate(isUnlink ? 'delete' : 'create', file);
  59511. };
  59512. watcher.on('change', async (file) => {
  59513. file = normalizePath$3(file);
  59514. await container.watchChange(file, { event: 'update' });
  59515. // invalidate module graph cache on file change
  59516. moduleGraph.onFileChange(file);
  59517. await onHMRUpdate('update', file);
  59518. });
  59519. getFsUtils(config).initWatcher?.(watcher);
  59520. watcher.on('add', (file) => {
  59521. onFileAddUnlink(file, false);
  59522. });
  59523. watcher.on('unlink', (file) => {
  59524. onFileAddUnlink(file, true);
  59525. });
  59526. hot.on('vite:invalidate', async ({ path, message }) => {
  59527. const mod = moduleGraph.urlToModuleMap.get(path);
  59528. if (mod &&
  59529. mod.isSelfAccepting &&
  59530. mod.lastHMRTimestamp > 0 &&
  59531. !mod.lastHMRInvalidationReceived) {
  59532. mod.lastHMRInvalidationReceived = true;
  59533. config.logger.info(colors$1.yellow(`hmr invalidate `) +
  59534. colors$1.dim(path) +
  59535. (message ? ` ${message}` : ''), { timestamp: true });
  59536. const file = getShortName(mod.file, config.root);
  59537. updateModules(file, [...mod.importers], mod.lastHMRTimestamp, server, true);
  59538. }
  59539. });
  59540. if (!middlewareMode && httpServer) {
  59541. httpServer.once('listening', () => {
  59542. // update actual port since this may be different from initial value
  59543. serverConfig.port = httpServer.address().port;
  59544. });
  59545. }
  59546. // apply server configuration hooks from plugins
  59547. const postHooks = [];
  59548. for (const hook of config.getSortedPluginHooks('configureServer')) {
  59549. postHooks.push(await hook(reflexServer));
  59550. }
  59551. // Internal middlewares ------------------------------------------------------
  59552. // request timer
  59553. if (process.env.DEBUG) {
  59554. middlewares.use(timeMiddleware(root));
  59555. }
  59556. // cors (enabled by default)
  59557. const { cors } = serverConfig;
  59558. if (cors !== false) {
  59559. middlewares.use(corsMiddleware(typeof cors === 'boolean' ? {} : cors));
  59560. }
  59561. middlewares.use(cachedTransformMiddleware(server));
  59562. // proxy
  59563. const { proxy } = serverConfig;
  59564. if (proxy) {
  59565. const middlewareServer = (isObject$1(middlewareMode) ? middlewareMode.server : null) || httpServer;
  59566. middlewares.use(proxyMiddleware(middlewareServer, proxy, config));
  59567. }
  59568. // base
  59569. if (config.base !== '/') {
  59570. middlewares.use(baseMiddleware(config.rawBase, !!middlewareMode));
  59571. }
  59572. // open in editor support
  59573. middlewares.use('/__open-in-editor', launchEditorMiddleware$1());
  59574. // ping request handler
  59575. // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  59576. middlewares.use(function viteHMRPingMiddleware(req, res, next) {
  59577. if (req.headers['accept'] === 'text/x-vite-ping') {
  59578. res.writeHead(204).end();
  59579. }
  59580. else {
  59581. next();
  59582. }
  59583. });
  59584. // serve static files under /public
  59585. // this applies before the transform middleware so that these files are served
  59586. // as-is without transforms.
  59587. if (publicDir) {
  59588. middlewares.use(servePublicMiddleware(server, publicFiles));
  59589. }
  59590. // main transform middleware
  59591. middlewares.use(transformMiddleware(server));
  59592. // serve static files
  59593. middlewares.use(serveRawFsMiddleware(server));
  59594. middlewares.use(serveStaticMiddleware(server));
  59595. // html fallback
  59596. if (config.appType === 'spa' || config.appType === 'mpa') {
  59597. middlewares.use(htmlFallbackMiddleware(root, config.appType === 'spa', getFsUtils(config)));
  59598. }
  59599. // run post config hooks
  59600. // This is applied before the html middleware so that user middleware can
  59601. // serve custom content instead of index.html.
  59602. postHooks.forEach((fn) => fn && fn());
  59603. if (config.appType === 'spa' || config.appType === 'mpa') {
  59604. // transform index.html
  59605. middlewares.use(indexHtmlMiddleware(root, server));
  59606. // handle 404s
  59607. middlewares.use(notFoundMiddleware());
  59608. }
  59609. // error handler
  59610. middlewares.use(errorMiddleware(server, !!middlewareMode));
  59611. // httpServer.listen can be called multiple times
  59612. // when port when using next port number
  59613. // this code is to avoid calling buildStart multiple times
  59614. let initingServer;
  59615. let serverInited = false;
  59616. const initServer = async () => {
  59617. if (serverInited)
  59618. return;
  59619. if (initingServer)
  59620. return initingServer;
  59621. initingServer = (async function () {
  59622. await container.buildStart({});
  59623. // start deps optimizer after all container plugins are ready
  59624. if (isDepsOptimizerEnabled(config, false)) {
  59625. await initDepsOptimizer(config, server);
  59626. }
  59627. warmupFiles(server);
  59628. initingServer = undefined;
  59629. serverInited = true;
  59630. })();
  59631. return initingServer;
  59632. };
  59633. if (!middlewareMode && httpServer) {
  59634. // overwrite listen to init optimizer before server start
  59635. const listen = httpServer.listen.bind(httpServer);
  59636. httpServer.listen = (async (port, ...args) => {
  59637. try {
  59638. // ensure ws server started
  59639. hot.listen();
  59640. await initServer();
  59641. }
  59642. catch (e) {
  59643. httpServer.emit('error', e);
  59644. return;
  59645. }
  59646. return listen(port, ...args);
  59647. });
  59648. }
  59649. else {
  59650. if (options.hotListen) {
  59651. hot.listen();
  59652. }
  59653. await initServer();
  59654. }
  59655. return server;
  59656. }
  59657. async function startServer(server, inlinePort) {
  59658. const httpServer = server.httpServer;
  59659. if (!httpServer) {
  59660. throw new Error('Cannot call server.listen in middleware mode.');
  59661. }
  59662. const options = server.config.server;
  59663. const hostname = await resolveHostname(options.host);
  59664. const configPort = inlinePort ?? options.port;
  59665. // When using non strict port for the dev server, the running port can be different from the config one.
  59666. // When restarting, the original port may be available but to avoid a switch of URL for the running
  59667. // browser tabs, we enforce the previously used port, expect if the config port changed.
  59668. const port = (!configPort || configPort === server._configServerPort
  59669. ? server._currentServerPort
  59670. : configPort) ?? DEFAULT_DEV_PORT;
  59671. server._configServerPort = configPort;
  59672. const serverPort = await httpServerStart(httpServer, {
  59673. port,
  59674. strictPort: options.strictPort,
  59675. host: hostname.host,
  59676. logger: server.config.logger,
  59677. });
  59678. server._currentServerPort = serverPort;
  59679. }
  59680. function createServerCloseFn(server) {
  59681. if (!server) {
  59682. return () => Promise.resolve();
  59683. }
  59684. let hasListened = false;
  59685. const openSockets = new Set();
  59686. server.on('connection', (socket) => {
  59687. openSockets.add(socket);
  59688. socket.on('close', () => {
  59689. openSockets.delete(socket);
  59690. });
  59691. });
  59692. server.once('listening', () => {
  59693. hasListened = true;
  59694. });
  59695. return () => new Promise((resolve, reject) => {
  59696. openSockets.forEach((s) => s.destroy());
  59697. if (hasListened) {
  59698. server.close((err) => {
  59699. if (err) {
  59700. reject(err);
  59701. }
  59702. else {
  59703. resolve();
  59704. }
  59705. });
  59706. }
  59707. else {
  59708. resolve();
  59709. }
  59710. });
  59711. }
  59712. function resolvedAllowDir(root, dir) {
  59713. return normalizePath$3(path$o.resolve(root, dir));
  59714. }
  59715. function resolveServerOptions(root, raw, logger) {
  59716. const server = {
  59717. preTransformRequests: true,
  59718. ...raw,
  59719. sourcemapIgnoreList: raw?.sourcemapIgnoreList === false
  59720. ? () => false
  59721. : raw?.sourcemapIgnoreList || isInNodeModules$1,
  59722. middlewareMode: raw?.middlewareMode || false,
  59723. };
  59724. let allowDirs = server.fs?.allow;
  59725. const deny = server.fs?.deny || ['.env', '.env.*', '*.{crt,pem}'];
  59726. if (!allowDirs) {
  59727. allowDirs = [searchForWorkspaceRoot(root)];
  59728. }
  59729. if (process.versions.pnp) {
  59730. try {
  59731. const enableGlobalCache = execSync('yarn config get enableGlobalCache', { cwd: root })
  59732. .toString()
  59733. .trim() === 'true';
  59734. const yarnCacheDir = execSync(`yarn config get ${enableGlobalCache ? 'globalFolder' : 'cacheFolder'}`, { cwd: root })
  59735. .toString()
  59736. .trim();
  59737. allowDirs.push(yarnCacheDir);
  59738. }
  59739. catch (e) {
  59740. logger.warn(`Get yarn cache dir error: ${e.message}`, {
  59741. timestamp: true,
  59742. });
  59743. }
  59744. }
  59745. allowDirs = allowDirs.map((i) => resolvedAllowDir(root, i));
  59746. // only push client dir when vite itself is outside-of-root
  59747. const resolvedClientDir = resolvedAllowDir(root, CLIENT_DIR);
  59748. if (!allowDirs.some((dir) => isParentDirectory(dir, resolvedClientDir))) {
  59749. allowDirs.push(resolvedClientDir);
  59750. }
  59751. server.fs = {
  59752. strict: server.fs?.strict ?? true,
  59753. allow: allowDirs,
  59754. deny,
  59755. cachedChecks: server.fs?.cachedChecks,
  59756. };
  59757. if (server.origin?.endsWith('/')) {
  59758. server.origin = server.origin.slice(0, -1);
  59759. logger.warn(colors$1.yellow(`${colors$1.bold('(!)')} server.origin should not end with "/". Using "${server.origin}" instead.`));
  59760. }
  59761. return server;
  59762. }
  59763. async function restartServer(server) {
  59764. global.__vite_start_time = performance.now();
  59765. const shortcutsOptions = server._shortcutsOptions;
  59766. let inlineConfig = server.config.inlineConfig;
  59767. if (server._forceOptimizeOnRestart) {
  59768. inlineConfig = mergeConfig(inlineConfig, {
  59769. optimizeDeps: {
  59770. force: true,
  59771. },
  59772. });
  59773. }
  59774. // Reinit the server by creating a new instance using the same inlineConfig
  59775. // This will triger a reload of the config file and re-create the plugins and
  59776. // middlewares. We then assign all properties of the new server to the existing
  59777. // server instance and set the user instance to be used in the new server.
  59778. // This allows us to keep the same server instance for the user.
  59779. {
  59780. let newServer = null;
  59781. try {
  59782. // delay ws server listen
  59783. newServer = await _createServer(inlineConfig, { hotListen: false });
  59784. }
  59785. catch (err) {
  59786. server.config.logger.error(err.message, {
  59787. timestamp: true,
  59788. });
  59789. server.config.logger.error('server restart failed', { timestamp: true });
  59790. return;
  59791. }
  59792. await server.close();
  59793. // Assign new server props to existing server instance
  59794. const middlewares = server.middlewares;
  59795. newServer._configServerPort = server._configServerPort;
  59796. newServer._currentServerPort = server._currentServerPort;
  59797. Object.assign(server, newServer);
  59798. // Keep the same connect instance so app.use(vite.middlewares) works
  59799. // after a restart in middlewareMode (.route is always '/')
  59800. middlewares.stack = newServer.middlewares.stack;
  59801. server.middlewares = middlewares;
  59802. // Rebind internal server variable so functions reference the user server
  59803. newServer._setInternalServer(server);
  59804. }
  59805. const { logger, server: { port, middlewareMode }, } = server.config;
  59806. if (!middlewareMode) {
  59807. await server.listen(port, true);
  59808. }
  59809. else {
  59810. server.hot.listen();
  59811. }
  59812. logger.info('server restarted.', { timestamp: true });
  59813. if (shortcutsOptions) {
  59814. shortcutsOptions.print = false;
  59815. bindCLIShortcuts(server, shortcutsOptions);
  59816. }
  59817. }
  59818. /**
  59819. * Internal function to restart the Vite server and print URLs if changed
  59820. */
  59821. async function restartServerWithUrls(server) {
  59822. if (server.config.server.middlewareMode) {
  59823. await server.restart();
  59824. return;
  59825. }
  59826. const { port: prevPort, host: prevHost } = server.config.server;
  59827. const prevUrls = server.resolvedUrls;
  59828. await server.restart();
  59829. const { logger, server: { port, host }, } = server.config;
  59830. if ((port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) ||
  59831. host !== prevHost ||
  59832. diffDnsOrderChange(prevUrls, server.resolvedUrls)) {
  59833. logger.info('');
  59834. server.printUrls();
  59835. }
  59836. }
  59837. const callCrawlEndIfIdleAfterMs = 50;
  59838. function setupOnCrawlEnd(onCrawlEnd) {
  59839. const registeredIds = new Set();
  59840. const seenIds = new Set();
  59841. const onCrawlEndPromiseWithResolvers = promiseWithResolvers();
  59842. let timeoutHandle;
  59843. let cancelled = false;
  59844. function cancel() {
  59845. cancelled = true;
  59846. }
  59847. let crawlEndCalled = false;
  59848. function callOnCrawlEnd() {
  59849. if (!cancelled && !crawlEndCalled) {
  59850. crawlEndCalled = true;
  59851. onCrawlEnd();
  59852. }
  59853. onCrawlEndPromiseWithResolvers.resolve();
  59854. }
  59855. function registerRequestProcessing(id, done) {
  59856. if (!seenIds.has(id)) {
  59857. seenIds.add(id);
  59858. registeredIds.add(id);
  59859. done()
  59860. .catch(() => { })
  59861. .finally(() => markIdAsDone(id));
  59862. }
  59863. }
  59864. function waitForRequestsIdle(ignoredId) {
  59865. if (ignoredId) {
  59866. seenIds.add(ignoredId);
  59867. markIdAsDone(ignoredId);
  59868. }
  59869. return onCrawlEndPromiseWithResolvers.promise;
  59870. }
  59871. function markIdAsDone(id) {
  59872. if (registeredIds.has(id)) {
  59873. registeredIds.delete(id);
  59874. checkIfCrawlEndAfterTimeout();
  59875. }
  59876. }
  59877. function checkIfCrawlEndAfterTimeout() {
  59878. if (cancelled || registeredIds.size > 0)
  59879. return;
  59880. if (timeoutHandle)
  59881. clearTimeout(timeoutHandle);
  59882. timeoutHandle = setTimeout(callOnCrawlEndWhenIdle, callCrawlEndIfIdleAfterMs);
  59883. }
  59884. async function callOnCrawlEndWhenIdle() {
  59885. if (cancelled || registeredIds.size > 0)
  59886. return;
  59887. callOnCrawlEnd();
  59888. }
  59889. return {
  59890. registerRequestProcessing,
  59891. waitForRequestsIdle,
  59892. cancel,
  59893. };
  59894. }
  59895. var index = {
  59896. __proto__: null,
  59897. _createServer: _createServer,
  59898. createServer: createServer,
  59899. createServerCloseFn: createServerCloseFn,
  59900. resolveServerOptions: resolveServerOptions,
  59901. restartServerWithUrls: restartServerWithUrls
  59902. };
  59903. const debugHmr = createDebugger('vite:hmr');
  59904. const whitespaceRE = /\s/;
  59905. const normalizedClientDir = normalizePath$3(CLIENT_DIR);
  59906. function getShortName(file, root) {
  59907. return file.startsWith(withTrailingSlash(root))
  59908. ? path$o.posix.relative(root, file)
  59909. : file;
  59910. }
  59911. async function handleHMRUpdate(type, file, server) {
  59912. const { hot, config, moduleGraph } = server;
  59913. const shortFile = getShortName(file, config.root);
  59914. const isConfig = file === config.configFile;
  59915. const isConfigDependency = config.configFileDependencies.some((name) => file === name);
  59916. const isEnv = config.inlineConfig.envFile !== false &&
  59917. getEnvFilesForMode(config.mode, config.envDir).includes(file);
  59918. if (isConfig || isConfigDependency || isEnv) {
  59919. // auto restart server
  59920. debugHmr?.(`[config change] ${colors$1.dim(shortFile)}`);
  59921. config.logger.info(colors$1.green(`${path$o.relative(process.cwd(), file)} changed, restarting server...`), { clear: true, timestamp: true });
  59922. try {
  59923. await restartServerWithUrls(server);
  59924. }
  59925. catch (e) {
  59926. config.logger.error(colors$1.red(e));
  59927. }
  59928. return;
  59929. }
  59930. debugHmr?.(`[file change] ${colors$1.dim(shortFile)}`);
  59931. // (dev only) the client itself cannot be hot updated.
  59932. if (file.startsWith(withTrailingSlash(normalizedClientDir))) {
  59933. hot.send({
  59934. type: 'full-reload',
  59935. path: '*',
  59936. triggeredBy: path$o.resolve(config.root, file),
  59937. });
  59938. return;
  59939. }
  59940. const mods = new Set(moduleGraph.getModulesByFile(file));
  59941. if (type === 'create') {
  59942. for (const mod of moduleGraph._hasResolveFailedErrorModules) {
  59943. mods.add(mod);
  59944. }
  59945. }
  59946. if (type === 'create' || type === 'delete') {
  59947. for (const mod of getAffectedGlobModules(file, server)) {
  59948. mods.add(mod);
  59949. }
  59950. }
  59951. // check if any plugin wants to perform custom HMR handling
  59952. const timestamp = Date.now();
  59953. const hmrContext = {
  59954. file,
  59955. timestamp,
  59956. modules: [...mods],
  59957. read: () => readModifiedFile(file),
  59958. server,
  59959. };
  59960. if (type === 'update') {
  59961. for (const hook of config.getSortedPluginHooks('handleHotUpdate')) {
  59962. const filteredModules = await hook(hmrContext);
  59963. if (filteredModules) {
  59964. hmrContext.modules = filteredModules;
  59965. }
  59966. }
  59967. }
  59968. if (!hmrContext.modules.length) {
  59969. // html file cannot be hot updated
  59970. if (file.endsWith('.html')) {
  59971. config.logger.info(colors$1.green(`page reload `) + colors$1.dim(shortFile), {
  59972. clear: true,
  59973. timestamp: true,
  59974. });
  59975. hot.send({
  59976. type: 'full-reload',
  59977. path: config.server.middlewareMode
  59978. ? '*'
  59979. : '/' + normalizePath$3(path$o.relative(config.root, file)),
  59980. });
  59981. }
  59982. else {
  59983. // loaded but not in the module graph, probably not js
  59984. debugHmr?.(`[no modules matched] ${colors$1.dim(shortFile)}`);
  59985. }
  59986. return;
  59987. }
  59988. updateModules(shortFile, hmrContext.modules, timestamp, server);
  59989. }
  59990. function updateModules(file, modules, timestamp, { config, hot, moduleGraph }, afterInvalidation) {
  59991. const updates = [];
  59992. const invalidatedModules = new Set();
  59993. const traversedModules = new Set();
  59994. let needFullReload = false;
  59995. for (const mod of modules) {
  59996. const boundaries = [];
  59997. const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries);
  59998. moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true);
  59999. if (needFullReload) {
  60000. continue;
  60001. }
  60002. if (hasDeadEnd) {
  60003. needFullReload = hasDeadEnd;
  60004. continue;
  60005. }
  60006. updates.push(...boundaries.map(({ boundary, acceptedVia, isWithinCircularImport }) => ({
  60007. type: `${boundary.type}-update`,
  60008. timestamp,
  60009. path: normalizeHmrUrl(boundary.url),
  60010. acceptedPath: normalizeHmrUrl(acceptedVia.url),
  60011. explicitImportRequired: boundary.type === 'js'
  60012. ? isExplicitImportRequired(acceptedVia.url)
  60013. : false,
  60014. isWithinCircularImport,
  60015. // browser modules are invalidated by changing ?t= query,
  60016. // but in ssr we control the module system, so we can directly remove them form cache
  60017. ssrInvalidates: getSSRInvalidatedImporters(acceptedVia),
  60018. })));
  60019. }
  60020. if (needFullReload) {
  60021. const reason = typeof needFullReload === 'string'
  60022. ? colors$1.dim(` (${needFullReload})`)
  60023. : '';
  60024. config.logger.info(colors$1.green(`page reload `) + colors$1.dim(file) + reason, { clear: !afterInvalidation, timestamp: true });
  60025. hot.send({
  60026. type: 'full-reload',
  60027. triggeredBy: path$o.resolve(config.root, file),
  60028. });
  60029. return;
  60030. }
  60031. if (updates.length === 0) {
  60032. debugHmr?.(colors$1.yellow(`no update happened `) + colors$1.dim(file));
  60033. return;
  60034. }
  60035. config.logger.info(colors$1.green(`hmr update `) +
  60036. colors$1.dim([...new Set(updates.map((u) => u.path))].join(', ')), { clear: !afterInvalidation, timestamp: true });
  60037. hot.send({
  60038. type: 'update',
  60039. updates,
  60040. });
  60041. }
  60042. function populateSSRImporters(module, timestamp, seen = new Set()) {
  60043. module.ssrImportedModules.forEach((importer) => {
  60044. if (seen.has(importer)) {
  60045. return;
  60046. }
  60047. if (importer.lastHMRTimestamp === timestamp ||
  60048. importer.lastInvalidationTimestamp === timestamp) {
  60049. seen.add(importer);
  60050. populateSSRImporters(importer, timestamp, seen);
  60051. }
  60052. });
  60053. return seen;
  60054. }
  60055. function getSSRInvalidatedImporters(module) {
  60056. return [...populateSSRImporters(module, module.lastHMRTimestamp)].map((m) => m.file);
  60057. }
  60058. function areAllImportsAccepted(importedBindings, acceptedExports) {
  60059. for (const binding of importedBindings) {
  60060. if (!acceptedExports.has(binding)) {
  60061. return false;
  60062. }
  60063. }
  60064. return true;
  60065. }
  60066. function propagateUpdate(node, traversedModules, boundaries, currentChain = [node]) {
  60067. if (traversedModules.has(node)) {
  60068. return false;
  60069. }
  60070. traversedModules.add(node);
  60071. // #7561
  60072. // if the imports of `node` have not been analyzed, then `node` has not
  60073. // been loaded in the browser and we should stop propagation.
  60074. if (node.id && node.isSelfAccepting === undefined) {
  60075. debugHmr?.(`[propagate update] stop propagation because not analyzed: ${colors$1.dim(node.id)}`);
  60076. return false;
  60077. }
  60078. if (node.isSelfAccepting) {
  60079. boundaries.push({
  60080. boundary: node,
  60081. acceptedVia: node,
  60082. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain),
  60083. });
  60084. // additionally check for CSS importers, since a PostCSS plugin like
  60085. // Tailwind JIT may register any file as a dependency to a CSS file.
  60086. for (const importer of node.importers) {
  60087. if (isCSSRequest(importer.url) && !currentChain.includes(importer)) {
  60088. propagateUpdate(importer, traversedModules, boundaries, currentChain.concat(importer));
  60089. }
  60090. }
  60091. return false;
  60092. }
  60093. // A partially accepted module with no importers is considered self accepting,
  60094. // because the deal is "there are parts of myself I can't self accept if they
  60095. // are used outside of me".
  60096. // Also, the imported module (this one) must be updated before the importers,
  60097. // so that they do get the fresh imported module when/if they are reloaded.
  60098. if (node.acceptedHmrExports) {
  60099. boundaries.push({
  60100. boundary: node,
  60101. acceptedVia: node,
  60102. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain),
  60103. });
  60104. }
  60105. else {
  60106. if (!node.importers.size) {
  60107. return true;
  60108. }
  60109. // #3716, #3913
  60110. // For a non-CSS file, if all of its importers are CSS files (registered via
  60111. // PostCSS plugins) it should be considered a dead end and force full reload.
  60112. if (!isCSSRequest(node.url) &&
  60113. [...node.importers].every((i) => isCSSRequest(i.url))) {
  60114. return true;
  60115. }
  60116. }
  60117. for (const importer of node.importers) {
  60118. const subChain = currentChain.concat(importer);
  60119. if (importer.acceptedHmrDeps.has(node)) {
  60120. boundaries.push({
  60121. boundary: importer,
  60122. acceptedVia: node,
  60123. isWithinCircularImport: isNodeWithinCircularImports(importer, subChain),
  60124. });
  60125. continue;
  60126. }
  60127. if (node.id && node.acceptedHmrExports && importer.importedBindings) {
  60128. const importedBindingsFromNode = importer.importedBindings.get(node.id);
  60129. if (importedBindingsFromNode &&
  60130. areAllImportsAccepted(importedBindingsFromNode, node.acceptedHmrExports)) {
  60131. continue;
  60132. }
  60133. }
  60134. if (!currentChain.includes(importer) &&
  60135. propagateUpdate(importer, traversedModules, boundaries, subChain)) {
  60136. return true;
  60137. }
  60138. }
  60139. return false;
  60140. }
  60141. /**
  60142. * Check importers recursively if it's an import loop. An accepted module within
  60143. * an import loop cannot recover its execution order and should be reloaded.
  60144. *
  60145. * @param node The node that accepts HMR and is a boundary
  60146. * @param nodeChain The chain of nodes/imports that lead to the node.
  60147. * (The last node in the chain imports the `node` parameter)
  60148. * @param currentChain The current chain tracked from the `node` parameter
  60149. * @param traversedModules The set of modules that have traversed
  60150. */
  60151. function isNodeWithinCircularImports(node, nodeChain, currentChain = [node], traversedModules = new Set()) {
  60152. // To help visualize how each parameters work, imagine this import graph:
  60153. //
  60154. // A -> B -> C -> ACCEPTED -> D -> E -> NODE
  60155. // ^--------------------------|
  60156. //
  60157. // ACCEPTED: the node that accepts HMR. the `node` parameter.
  60158. // NODE : the initial node that triggered this HMR.
  60159. //
  60160. // This function will return true in the above graph, which:
  60161. // `node` : ACCEPTED
  60162. // `nodeChain` : [NODE, E, D, ACCEPTED]
  60163. // `currentChain` : [ACCEPTED, C, B]
  60164. //
  60165. // It works by checking if any `node` importers are within `nodeChain`, which
  60166. // means there's an import loop with a HMR-accepted module in it.
  60167. if (traversedModules.has(node)) {
  60168. return false;
  60169. }
  60170. traversedModules.add(node);
  60171. for (const importer of node.importers) {
  60172. // Node may import itself which is safe
  60173. if (importer === node)
  60174. continue;
  60175. // a PostCSS plugin like Tailwind JIT may register
  60176. // any file as a dependency to a CSS file.
  60177. // But in that case, the actual dependency chain is separate.
  60178. if (isCSSRequest(importer.url))
  60179. continue;
  60180. // Check circular imports
  60181. const importerIndex = nodeChain.indexOf(importer);
  60182. if (importerIndex > -1) {
  60183. // Log extra debug information so users can fix and remove the circular imports
  60184. if (debugHmr) {
  60185. // Following explanation above:
  60186. // `importer` : E
  60187. // `currentChain` reversed : [B, C, ACCEPTED]
  60188. // `nodeChain` sliced & reversed : [D, E]
  60189. // Combined : [E, B, C, ACCEPTED, D, E]
  60190. const importChain = [
  60191. importer,
  60192. ...[...currentChain].reverse(),
  60193. ...nodeChain.slice(importerIndex, -1).reverse(),
  60194. ];
  60195. debugHmr(colors$1.yellow(`circular imports detected: `) +
  60196. importChain.map((m) => colors$1.dim(m.url)).join(' -> '));
  60197. }
  60198. return true;
  60199. }
  60200. // Continue recursively
  60201. if (!currentChain.includes(importer)) {
  60202. const result = isNodeWithinCircularImports(importer, nodeChain, currentChain.concat(importer), traversedModules);
  60203. if (result)
  60204. return result;
  60205. }
  60206. }
  60207. return false;
  60208. }
  60209. function handlePrunedModules(mods, { hot }) {
  60210. // update the disposed modules' hmr timestamp
  60211. // since if it's re-imported, it should re-apply side effects
  60212. // and without the timestamp the browser will not re-import it!
  60213. const t = Date.now();
  60214. mods.forEach((mod) => {
  60215. mod.lastHMRTimestamp = t;
  60216. mod.lastHMRInvalidationReceived = false;
  60217. debugHmr?.(`[dispose] ${colors$1.dim(mod.file)}`);
  60218. });
  60219. hot.send({
  60220. type: 'prune',
  60221. paths: [...mods].map((m) => m.url),
  60222. });
  60223. }
  60224. /**
  60225. * Lex import.meta.hot.accept() for accepted deps.
  60226. * Since hot.accept() can only accept string literals or array of string
  60227. * literals, we don't really need a heavy @babel/parse call on the entire source.
  60228. *
  60229. * @returns selfAccepts
  60230. */
  60231. function lexAcceptedHmrDeps(code, start, urls) {
  60232. let state = 0 /* LexerState.inCall */;
  60233. // the state can only be 2 levels deep so no need for a stack
  60234. let prevState = 0 /* LexerState.inCall */;
  60235. let currentDep = '';
  60236. function addDep(index) {
  60237. urls.add({
  60238. url: currentDep,
  60239. start: index - currentDep.length - 1,
  60240. end: index + 1,
  60241. });
  60242. currentDep = '';
  60243. }
  60244. for (let i = start; i < code.length; i++) {
  60245. const char = code.charAt(i);
  60246. switch (state) {
  60247. case 0 /* LexerState.inCall */:
  60248. case 4 /* LexerState.inArray */:
  60249. if (char === `'`) {
  60250. prevState = state;
  60251. state = 1 /* LexerState.inSingleQuoteString */;
  60252. }
  60253. else if (char === `"`) {
  60254. prevState = state;
  60255. state = 2 /* LexerState.inDoubleQuoteString */;
  60256. }
  60257. else if (char === '`') {
  60258. prevState = state;
  60259. state = 3 /* LexerState.inTemplateString */;
  60260. }
  60261. else if (whitespaceRE.test(char)) {
  60262. continue;
  60263. }
  60264. else {
  60265. if (state === 0 /* LexerState.inCall */) {
  60266. if (char === `[`) {
  60267. state = 4 /* LexerState.inArray */;
  60268. }
  60269. else {
  60270. // reaching here means the first arg is neither a string literal
  60271. // nor an Array literal (direct callback) or there is no arg
  60272. // in both case this indicates a self-accepting module
  60273. return true; // done
  60274. }
  60275. }
  60276. else if (state === 4 /* LexerState.inArray */) {
  60277. if (char === `]`) {
  60278. return false; // done
  60279. }
  60280. else if (char === ',') {
  60281. continue;
  60282. }
  60283. else {
  60284. error(i);
  60285. }
  60286. }
  60287. }
  60288. break;
  60289. case 1 /* LexerState.inSingleQuoteString */:
  60290. if (char === `'`) {
  60291. addDep(i);
  60292. if (prevState === 0 /* LexerState.inCall */) {
  60293. // accept('foo', ...)
  60294. return false;
  60295. }
  60296. else {
  60297. state = prevState;
  60298. }
  60299. }
  60300. else {
  60301. currentDep += char;
  60302. }
  60303. break;
  60304. case 2 /* LexerState.inDoubleQuoteString */:
  60305. if (char === `"`) {
  60306. addDep(i);
  60307. if (prevState === 0 /* LexerState.inCall */) {
  60308. // accept('foo', ...)
  60309. return false;
  60310. }
  60311. else {
  60312. state = prevState;
  60313. }
  60314. }
  60315. else {
  60316. currentDep += char;
  60317. }
  60318. break;
  60319. case 3 /* LexerState.inTemplateString */:
  60320. if (char === '`') {
  60321. addDep(i);
  60322. if (prevState === 0 /* LexerState.inCall */) {
  60323. // accept('foo', ...)
  60324. return false;
  60325. }
  60326. else {
  60327. state = prevState;
  60328. }
  60329. }
  60330. else if (char === '$' && code.charAt(i + 1) === '{') {
  60331. error(i);
  60332. }
  60333. else {
  60334. currentDep += char;
  60335. }
  60336. break;
  60337. default:
  60338. throw new Error('unknown import.meta.hot lexer state');
  60339. }
  60340. }
  60341. return false;
  60342. }
  60343. function lexAcceptedHmrExports(code, start, exportNames) {
  60344. const urls = new Set();
  60345. lexAcceptedHmrDeps(code, start, urls);
  60346. for (const { url } of urls) {
  60347. exportNames.add(url);
  60348. }
  60349. return urls.size > 0;
  60350. }
  60351. function normalizeHmrUrl(url) {
  60352. if (url[0] !== '.' && url[0] !== '/') {
  60353. url = wrapId$1(url);
  60354. }
  60355. return url;
  60356. }
  60357. function error(pos) {
  60358. const err = new Error(`import.meta.hot.accept() can only accept string literals or an ` +
  60359. `Array of string literals.`);
  60360. err.pos = pos;
  60361. throw err;
  60362. }
  60363. // vitejs/vite#610 when hot-reloading Vue files, we read immediately on file
  60364. // change event and sometimes this can be too early and get an empty buffer.
  60365. // Poll until the file's modified time has changed before reading again.
  60366. async function readModifiedFile(file) {
  60367. const content = await fsp.readFile(file, 'utf-8');
  60368. if (!content) {
  60369. const mtime = (await fsp.stat(file)).mtimeMs;
  60370. for (let n = 0; n < 10; n++) {
  60371. await new Promise((r) => setTimeout(r, 10));
  60372. const newMtime = (await fsp.stat(file)).mtimeMs;
  60373. if (newMtime !== mtime) {
  60374. break;
  60375. }
  60376. }
  60377. return await fsp.readFile(file, 'utf-8');
  60378. }
  60379. else {
  60380. return content;
  60381. }
  60382. }
  60383. function createHMRBroadcaster() {
  60384. const channels = [];
  60385. const readyChannels = new WeakSet();
  60386. const broadcaster = {
  60387. get channels() {
  60388. return [...channels];
  60389. },
  60390. addChannel(channel) {
  60391. if (channels.some((c) => c.name === channel.name)) {
  60392. throw new Error(`HMR channel "${channel.name}" is already defined.`);
  60393. }
  60394. channels.push(channel);
  60395. return broadcaster;
  60396. },
  60397. on(event, listener) {
  60398. // emit connection event only when all channels are ready
  60399. if (event === 'connection') {
  60400. // make a copy so we don't wait for channels that might be added after this is triggered
  60401. const channels = this.channels;
  60402. channels.forEach((channel) => channel.on('connection', () => {
  60403. readyChannels.add(channel);
  60404. if (channels.every((c) => readyChannels.has(c))) {
  60405. listener();
  60406. }
  60407. }));
  60408. return;
  60409. }
  60410. channels.forEach((channel) => channel.on(event, listener));
  60411. return;
  60412. },
  60413. off(event, listener) {
  60414. channels.forEach((channel) => channel.off(event, listener));
  60415. return;
  60416. },
  60417. send(...args) {
  60418. channels.forEach((channel) => channel.send(...args));
  60419. },
  60420. listen() {
  60421. channels.forEach((channel) => channel.listen());
  60422. },
  60423. close() {
  60424. return Promise.all(channels.map((channel) => channel.close()));
  60425. },
  60426. };
  60427. return broadcaster;
  60428. }
  60429. function createServerHMRChannel() {
  60430. const innerEmitter = new EventEmitter$4();
  60431. const outsideEmitter = new EventEmitter$4();
  60432. return {
  60433. name: 'ssr',
  60434. send(...args) {
  60435. let payload;
  60436. if (typeof args[0] === 'string') {
  60437. payload = {
  60438. type: 'custom',
  60439. event: args[0],
  60440. data: args[1],
  60441. };
  60442. }
  60443. else {
  60444. payload = args[0];
  60445. }
  60446. outsideEmitter.emit('send', payload);
  60447. },
  60448. off(event, listener) {
  60449. innerEmitter.off(event, listener);
  60450. },
  60451. on: ((event, listener) => {
  60452. innerEmitter.on(event, listener);
  60453. }),
  60454. close() {
  60455. innerEmitter.removeAllListeners();
  60456. outsideEmitter.removeAllListeners();
  60457. },
  60458. listen() {
  60459. innerEmitter.emit('connection');
  60460. },
  60461. api: {
  60462. innerEmitter,
  60463. outsideEmitter,
  60464. },
  60465. };
  60466. }
  60467. const debug$1 = createDebugger('vite:import-analysis');
  60468. const clientDir = normalizePath$3(CLIENT_DIR);
  60469. const skipRE = /\.(?:map|json)(?:$|\?)/;
  60470. const canSkipImportAnalysis = (id) => skipRE.test(id) || isDirectCSSRequest(id);
  60471. const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/;
  60472. const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/;
  60473. const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//;
  60474. const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/;
  60475. const templateLiteralRE = /^\s*`(.*)`\s*$/;
  60476. function isExplicitImportRequired(url) {
  60477. return !isJSRequest(url) && !isCSSRequest(url);
  60478. }
  60479. function extractImportedBindings(id, source, importSpec, importedBindings) {
  60480. let bindings = importedBindings.get(id);
  60481. if (!bindings) {
  60482. bindings = new Set();
  60483. importedBindings.set(id, bindings);
  60484. }
  60485. const isDynamic = importSpec.d > -1;
  60486. const isMeta = importSpec.d === -2;
  60487. if (isDynamic || isMeta) {
  60488. // this basically means the module will be impacted by any change in its dep
  60489. bindings.add('*');
  60490. return;
  60491. }
  60492. const exp = source.slice(importSpec.ss, importSpec.se);
  60493. const [match0] = findStaticImports(exp);
  60494. if (!match0) {
  60495. return;
  60496. }
  60497. const parsed = parseStaticImport(match0);
  60498. if (!parsed) {
  60499. return;
  60500. }
  60501. if (parsed.namespacedImport) {
  60502. bindings.add('*');
  60503. }
  60504. if (parsed.defaultImport) {
  60505. bindings.add('default');
  60506. }
  60507. if (parsed.namedImports) {
  60508. for (const name of Object.keys(parsed.namedImports)) {
  60509. bindings.add(name);
  60510. }
  60511. }
  60512. }
  60513. /**
  60514. * Server-only plugin that lexes, resolves, rewrites and analyzes url imports.
  60515. *
  60516. * - Imports are resolved to ensure they exist on disk
  60517. *
  60518. * - Lexes HMR accept calls and updates import relationships in the module graph
  60519. *
  60520. * - Bare module imports are resolved (by @rollup-plugin/node-resolve) to
  60521. * absolute file paths, e.g.
  60522. *
  60523. * ```js
  60524. * import 'foo'
  60525. * ```
  60526. * is rewritten to
  60527. * ```js
  60528. * import '/@fs//project/node_modules/foo/dist/foo.js'
  60529. * ```
  60530. *
  60531. * - CSS imports are appended with `.js` since both the js module and the actual
  60532. * css (referenced via `<link>`) may go through the transform pipeline:
  60533. *
  60534. * ```js
  60535. * import './style.css'
  60536. * ```
  60537. * is rewritten to
  60538. * ```js
  60539. * import './style.css.js'
  60540. * ```
  60541. */
  60542. function importAnalysisPlugin(config) {
  60543. const { root, base } = config;
  60544. const fsUtils = getFsUtils(config);
  60545. const clientPublicPath = path$o.posix.join(base, CLIENT_PUBLIC_PATH);
  60546. const enablePartialAccept = config.experimental?.hmrPartialAccept;
  60547. const matchAlias = getAliasPatternMatcher(config.resolve.alias);
  60548. let server;
  60549. let _env;
  60550. let _ssrEnv;
  60551. function getEnv(ssr) {
  60552. if (!_ssrEnv || !_env) {
  60553. const importMetaEnvKeys = {};
  60554. const userDefineEnv = {};
  60555. for (const key in config.env) {
  60556. importMetaEnvKeys[key] = JSON.stringify(config.env[key]);
  60557. }
  60558. for (const key in config.define) {
  60559. // non-import.meta.env.* is handled in `clientInjection` plugin
  60560. if (key.startsWith('import.meta.env.')) {
  60561. userDefineEnv[key.slice(16)] = config.define[key];
  60562. }
  60563. }
  60564. const env = `import.meta.env = ${serializeDefine({
  60565. ...importMetaEnvKeys,
  60566. SSR: '__vite_ssr__',
  60567. ...userDefineEnv,
  60568. })};`;
  60569. _ssrEnv = env.replace('__vite_ssr__', 'true');
  60570. _env = env.replace('__vite_ssr__', 'false');
  60571. }
  60572. return ssr ? _ssrEnv : _env;
  60573. }
  60574. return {
  60575. name: 'vite:import-analysis',
  60576. configureServer(_server) {
  60577. server = _server;
  60578. },
  60579. async transform(source, importer, options) {
  60580. // In a real app `server` is always defined, but it is undefined when
  60581. // running src/node/server/__tests__/pluginContainer.spec.ts
  60582. if (!server) {
  60583. return null;
  60584. }
  60585. const ssr = options?.ssr === true;
  60586. if (canSkipImportAnalysis(importer)) {
  60587. debug$1?.(colors$1.dim(`[skipped] ${prettifyUrl(importer, root)}`));
  60588. return null;
  60589. }
  60590. const msAtStart = debug$1 ? performance.now() : 0;
  60591. await init;
  60592. let imports;
  60593. let exports;
  60594. source = stripBomTag(source);
  60595. try {
  60596. [imports, exports] = parse$e(source);
  60597. }
  60598. catch (_e) {
  60599. const e = _e;
  60600. const { message, showCodeFrame } = createParseErrorInfo(importer, source);
  60601. this.error(message, showCodeFrame ? e.idx : undefined);
  60602. }
  60603. const depsOptimizer = getDepsOptimizer(config, ssr);
  60604. const { moduleGraph } = server;
  60605. // since we are already in the transform phase of the importer, it must
  60606. // have been loaded so its entry is guaranteed in the module graph.
  60607. const importerModule = moduleGraph.getModuleById(importer);
  60608. if (!importerModule) {
  60609. // This request is no longer valid. It could happen for optimized deps
  60610. // requests. A full reload is going to request this id again.
  60611. // Throwing an outdated error so we properly finish the request with a
  60612. // 504 sent to the browser.
  60613. throwOutdatedRequest(importer);
  60614. }
  60615. if (!imports.length && !this._addedImports) {
  60616. importerModule.isSelfAccepting = false;
  60617. debug$1?.(`${timeFrom(msAtStart)} ${colors$1.dim(`[no imports] ${prettifyUrl(importer, root)}`)}`);
  60618. return source;
  60619. }
  60620. let hasHMR = false;
  60621. let isSelfAccepting = false;
  60622. let hasEnv = false;
  60623. let needQueryInjectHelper = false;
  60624. let s;
  60625. const str = () => s || (s = new MagicString(source));
  60626. let isPartiallySelfAccepting = false;
  60627. const importedBindings = enablePartialAccept
  60628. ? new Map()
  60629. : null;
  60630. const toAbsoluteUrl = (url) => path$o.posix.resolve(path$o.posix.dirname(importerModule.url), url);
  60631. const normalizeUrl = async (url, pos, forceSkipImportAnalysis = false) => {
  60632. url = stripBase(url, base);
  60633. let importerFile = importer;
  60634. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  60635. if (moduleListContains(optimizeDeps?.exclude, url)) {
  60636. if (depsOptimizer) {
  60637. await depsOptimizer.scanProcessing;
  60638. // if the dependency encountered in the optimized file was excluded from the optimization
  60639. // the dependency needs to be resolved starting from the original source location of the optimized file
  60640. // because starting from node_modules/.vite will not find the dependency if it was not hoisted
  60641. // (that is, if it is under node_modules directory in the package source of the optimized file)
  60642. for (const optimizedModule of depsOptimizer.metadata.depInfoList) {
  60643. if (!optimizedModule.src)
  60644. continue; // Ignore chunks
  60645. if (optimizedModule.file === importerModule.file) {
  60646. importerFile = optimizedModule.src;
  60647. }
  60648. }
  60649. }
  60650. }
  60651. const resolved = await this.resolve(url, importerFile);
  60652. if (!resolved || resolved.meta?.['vite:alias']?.noResolved) {
  60653. // in ssr, we should let node handle the missing modules
  60654. if (ssr) {
  60655. return [url, url];
  60656. }
  60657. // fix#9534, prevent the importerModuleNode being stopped from propagating updates
  60658. importerModule.isSelfAccepting = false;
  60659. moduleGraph._hasResolveFailedErrorModules.add(importerModule);
  60660. return this.error(`Failed to resolve import "${url}" from "${normalizePath$3(path$o.relative(process.cwd(), importerFile))}". Does the file exist?`, pos);
  60661. }
  60662. if (isExternalUrl(resolved.id)) {
  60663. return [resolved.id, resolved.id];
  60664. }
  60665. const isRelative = url[0] === '.';
  60666. const isSelfImport = !isRelative && cleanUrl(url) === cleanUrl(importer);
  60667. // normalize all imports into resolved URLs
  60668. // e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js'`
  60669. if (resolved.id.startsWith(withTrailingSlash(root))) {
  60670. // in root: infer short absolute path from root
  60671. url = resolved.id.slice(root.length);
  60672. }
  60673. else if (depsOptimizer?.isOptimizedDepFile(resolved.id) ||
  60674. // vite-plugin-react isn't following the leading \0 virtual module convention.
  60675. // This is a temporary hack to avoid expensive fs checks for React apps.
  60676. // We'll remove this as soon we're able to fix the react plugins.
  60677. (resolved.id !== '/@react-refresh' &&
  60678. path$o.isAbsolute(resolved.id) &&
  60679. fsUtils.existsSync(cleanUrl(resolved.id)))) {
  60680. // an optimized deps may not yet exists in the filesystem, or
  60681. // a regular file exists but is out of root: rewrite to absolute /@fs/ paths
  60682. url = path$o.posix.join(FS_PREFIX, resolved.id);
  60683. }
  60684. else {
  60685. url = resolved.id;
  60686. }
  60687. // if the resolved id is not a valid browser import specifier,
  60688. // prefix it to make it valid. We will strip this before feeding it
  60689. // back into the transform pipeline
  60690. if (url[0] !== '.' && url[0] !== '/') {
  60691. url = wrapId$1(resolved.id);
  60692. }
  60693. // make the URL browser-valid if not SSR
  60694. if (!ssr) {
  60695. // mark non-js/css imports with `?import`
  60696. if (isExplicitImportRequired(url)) {
  60697. url = injectQuery(url, 'import');
  60698. }
  60699. else if ((isRelative || isSelfImport) &&
  60700. !DEP_VERSION_RE.test(url)) {
  60701. // If the url isn't a request for a pre-bundled common chunk,
  60702. // for relative js/css imports, or self-module virtual imports
  60703. // (e.g. vue blocks), inherit importer's version query
  60704. // do not do this for unknown type imports, otherwise the appended
  60705. // query can break 3rd party plugin's extension checks.
  60706. const versionMatch = importer.match(DEP_VERSION_RE);
  60707. if (versionMatch) {
  60708. url = injectQuery(url, versionMatch[1]);
  60709. }
  60710. }
  60711. // check if the dep has been hmr updated. If yes, we need to attach
  60712. // its last updated timestamp to force the browser to fetch the most
  60713. // up-to-date version of this module.
  60714. try {
  60715. // delay setting `isSelfAccepting` until the file is actually used (#7870)
  60716. // We use an internal function to avoid resolving the url again
  60717. const depModule = await moduleGraph._ensureEntryFromUrl(unwrapId$1(url), ssr, canSkipImportAnalysis(url) || forceSkipImportAnalysis, resolved);
  60718. if (depModule.lastHMRTimestamp > 0) {
  60719. url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`);
  60720. }
  60721. }
  60722. catch (e) {
  60723. // it's possible that the dep fails to resolve (non-existent import)
  60724. // attach location to the missing import
  60725. e.pos = pos;
  60726. throw e;
  60727. }
  60728. // prepend base
  60729. url = joinUrlSegments(base, url);
  60730. }
  60731. return [url, resolved.id];
  60732. };
  60733. const orderedImportedUrls = new Array(imports.length);
  60734. const orderedAcceptedUrls = new Array(imports.length);
  60735. const orderedAcceptedExports = new Array(imports.length);
  60736. await Promise.all(imports.map(async (importSpecifier, index) => {
  60737. const { s: start, e: end, ss: expStart, se: expEnd, d: dynamicIndex, a: attributeIndex, } = importSpecifier;
  60738. // #2083 User may use escape path,
  60739. // so use imports[index].n to get the unescaped string
  60740. let specifier = importSpecifier.n;
  60741. const rawUrl = source.slice(start, end);
  60742. // check import.meta usage
  60743. if (rawUrl === 'import.meta') {
  60744. const prop = source.slice(end, end + 4);
  60745. if (prop === '.hot') {
  60746. hasHMR = true;
  60747. const endHot = end + 4 + (source[end + 4] === '?' ? 1 : 0);
  60748. if (source.slice(endHot, endHot + 7) === '.accept') {
  60749. // further analyze accepted modules
  60750. if (source.slice(endHot, endHot + 14) === '.acceptExports') {
  60751. const importAcceptedExports = (orderedAcceptedExports[index] =
  60752. new Set());
  60753. lexAcceptedHmrExports(source, source.indexOf('(', endHot + 14) + 1, importAcceptedExports);
  60754. isPartiallySelfAccepting = true;
  60755. }
  60756. else {
  60757. const importAcceptedUrls = (orderedAcceptedUrls[index] =
  60758. new Set());
  60759. if (lexAcceptedHmrDeps(source, source.indexOf('(', endHot + 7) + 1, importAcceptedUrls)) {
  60760. isSelfAccepting = true;
  60761. }
  60762. }
  60763. }
  60764. }
  60765. else if (prop === '.env') {
  60766. hasEnv = true;
  60767. }
  60768. return;
  60769. }
  60770. else if (templateLiteralRE.test(rawUrl)) {
  60771. // If the import has backticks but isn't transformed as a glob import
  60772. // (as there's nothing to glob), check if it's simply a plain string.
  60773. // If so, we can replace the specifier as a plain string to prevent
  60774. // an incorrect "cannot be analyzed" warning.
  60775. if (!(rawUrl.includes('${') && rawUrl.includes('}'))) {
  60776. specifier = rawUrl.replace(templateLiteralRE, '$1');
  60777. }
  60778. }
  60779. const isDynamicImport = dynamicIndex > -1;
  60780. // strip import attributes as we can process them ourselves
  60781. if (!isDynamicImport && attributeIndex > -1) {
  60782. str().remove(end + 1, expEnd);
  60783. }
  60784. // static import or valid string in dynamic import
  60785. // If resolvable, let's resolve it
  60786. if (specifier !== undefined) {
  60787. // skip external / data uri
  60788. if (isExternalUrl(specifier) || isDataUrl(specifier)) {
  60789. return;
  60790. }
  60791. // skip ssr external
  60792. if (ssr && !matchAlias(specifier)) {
  60793. if (shouldExternalizeForSSR(specifier, importer, config)) {
  60794. return;
  60795. }
  60796. if (isBuiltin(specifier)) {
  60797. return;
  60798. }
  60799. }
  60800. // skip client
  60801. if (specifier === clientPublicPath) {
  60802. return;
  60803. }
  60804. // warn imports to non-asset /public files
  60805. if (specifier[0] === '/' &&
  60806. !(config.assetsInclude(cleanUrl(specifier)) ||
  60807. urlRE.test(specifier)) &&
  60808. checkPublicFile(specifier, config)) {
  60809. throw new Error(`Cannot import non-asset file ${specifier} which is inside /public. ` +
  60810. `JS/CSS files inside /public are copied as-is on build and ` +
  60811. `can only be referenced via <script src> or <link href> in html. ` +
  60812. `If you want to get the URL of that file, use ${injectQuery(specifier, 'url')} instead.`);
  60813. }
  60814. // normalize
  60815. const [url, resolvedId] = await normalizeUrl(specifier, start);
  60816. // record as safe modules
  60817. // safeModulesPath should not include the base prefix.
  60818. // See https://github.com/vitejs/vite/issues/9438#issuecomment-1465270409
  60819. server?.moduleGraph.safeModulesPath.add(fsPathFromUrl(stripBase(url, base)));
  60820. if (url !== specifier) {
  60821. let rewriteDone = false;
  60822. if (depsOptimizer?.isOptimizedDepFile(resolvedId) &&
  60823. !optimizedDepChunkRE.test(resolvedId)) {
  60824. // for optimized cjs deps, support named imports by rewriting named imports to const assignments.
  60825. // internal optimized chunks don't need es interop and are excluded
  60826. // The browserHash in resolvedId could be stale in which case there will be a full
  60827. // page reload. We could return a 404 in that case but it is safe to return the request
  60828. const file = cleanUrl(resolvedId); // Remove ?v={hash}
  60829. const needsInterop = await optimizedDepNeedsInterop(depsOptimizer.metadata, file, config, ssr);
  60830. if (needsInterop === undefined) {
  60831. // Non-entry dynamic imports from dependencies will reach here as there isn't
  60832. // optimize info for them, but they don't need es interop. If the request isn't
  60833. // a dynamic import, then it is an internal Vite error
  60834. if (!optimizedDepDynamicRE.test(file)) {
  60835. config.logger.error(colors$1.red(`Vite Error, ${url} optimized info should be defined`));
  60836. }
  60837. }
  60838. else if (needsInterop) {
  60839. debug$1?.(`${url} needs interop`);
  60840. interopNamedImports(str(), importSpecifier, url, index, importer, config);
  60841. rewriteDone = true;
  60842. }
  60843. }
  60844. // If source code imports builtin modules via named imports, the stub proxy export
  60845. // would fail as it's `export default` only. Apply interop for builtin modules to
  60846. // correctly throw the error message.
  60847. else if (url.includes(browserExternalId) &&
  60848. source.slice(expStart, start).includes('{')) {
  60849. interopNamedImports(str(), importSpecifier, url, index, importer, config);
  60850. rewriteDone = true;
  60851. }
  60852. if (!rewriteDone) {
  60853. const rewrittenUrl = JSON.stringify(url);
  60854. const s = isDynamicImport ? start : start - 1;
  60855. const e = isDynamicImport ? end : end + 1;
  60856. str().overwrite(s, e, rewrittenUrl, {
  60857. contentOnly: true,
  60858. });
  60859. }
  60860. }
  60861. // record for HMR import chain analysis
  60862. // make sure to unwrap and normalize away base
  60863. const hmrUrl = unwrapId$1(stripBase(url, base));
  60864. const isLocalImport = !isExternalUrl(hmrUrl) && !isDataUrl(hmrUrl);
  60865. if (isLocalImport) {
  60866. orderedImportedUrls[index] = hmrUrl;
  60867. }
  60868. if (enablePartialAccept && importedBindings) {
  60869. extractImportedBindings(resolvedId, source, importSpecifier, importedBindings);
  60870. }
  60871. if (!isDynamicImport &&
  60872. isLocalImport &&
  60873. config.server.preTransformRequests) {
  60874. // pre-transform known direct imports
  60875. // These requests will also be registered in transformRequest to be awaited
  60876. // by the deps optimizer
  60877. const url = removeImportQuery(hmrUrl);
  60878. server.warmupRequest(url, { ssr });
  60879. }
  60880. }
  60881. else if (!importer.startsWith(withTrailingSlash(clientDir))) {
  60882. if (!isInNodeModules$1(importer)) {
  60883. // check @vite-ignore which suppresses dynamic import warning
  60884. const hasViteIgnore = hasViteIgnoreRE.test(
  60885. // complete expression inside parens
  60886. source.slice(dynamicIndex + 1, end));
  60887. if (!hasViteIgnore) {
  60888. this.warn(`\n` +
  60889. colors$1.cyan(importerModule.file) +
  60890. `\n` +
  60891. colors$1.reset(generateCodeFrame(source, start, end)) +
  60892. colors$1.yellow(`\nThe above dynamic import cannot be analyzed by Vite.\n` +
  60893. `See ${colors$1.blue(`https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`)} ` +
  60894. `for supported dynamic import formats. ` +
  60895. `If this is intended to be left as-is, you can use the ` +
  60896. `/* @vite-ignore */ comment inside the import() call to suppress this warning.\n`));
  60897. }
  60898. }
  60899. if (!ssr) {
  60900. if (!urlIsStringRE.test(rawUrl) ||
  60901. isExplicitImportRequired(rawUrl.slice(1, -1))) {
  60902. needQueryInjectHelper = true;
  60903. str().overwrite(start, end, `__vite__injectQuery(${rawUrl}, 'import')`, { contentOnly: true });
  60904. }
  60905. }
  60906. }
  60907. }));
  60908. const _orderedImportedUrls = orderedImportedUrls.filter(isDefined);
  60909. const importedUrls = new Set(_orderedImportedUrls);
  60910. // `importedUrls` will be mixed with watched files for the module graph,
  60911. // `staticImportedUrls` will only contain the static top-level imports and
  60912. // dynamic imports
  60913. const staticImportedUrls = new Set(_orderedImportedUrls.map((url) => removeTimestampQuery(url)));
  60914. const acceptedUrls = mergeAcceptedUrls(orderedAcceptedUrls);
  60915. const acceptedExports = mergeAcceptedUrls(orderedAcceptedExports);
  60916. // While we always expect to work with ESM, a classic worker is the only
  60917. // case where it's not ESM and we need to avoid injecting ESM-specific code
  60918. const isClassicWorker = importer.includes(WORKER_FILE_ID) && importer.includes('type=classic');
  60919. if (hasEnv && !isClassicWorker) {
  60920. // inject import.meta.env
  60921. str().prepend(getEnv(ssr));
  60922. }
  60923. if (hasHMR && !ssr && !isClassicWorker) {
  60924. debugHmr?.(`${isSelfAccepting
  60925. ? `[self-accepts]`
  60926. : isPartiallySelfAccepting
  60927. ? `[accepts-exports]`
  60928. : acceptedUrls.size
  60929. ? `[accepts-deps]`
  60930. : `[detected api usage]`} ${prettifyUrl(importer, root)}`);
  60931. // inject hot context
  60932. str().prepend(`import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";` +
  60933. `import.meta.hot = __vite__createHotContext(${JSON.stringify(normalizeHmrUrl(importerModule.url))});`);
  60934. }
  60935. if (needQueryInjectHelper) {
  60936. if (isClassicWorker) {
  60937. str().append('\n' + __vite__injectQuery.toString());
  60938. }
  60939. else {
  60940. str().prepend(`import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`);
  60941. }
  60942. }
  60943. // normalize and rewrite accepted urls
  60944. const normalizedAcceptedUrls = new Set();
  60945. for (const { url, start, end } of acceptedUrls) {
  60946. const [normalized] = await moduleGraph.resolveUrl(toAbsoluteUrl(url), ssr);
  60947. normalizedAcceptedUrls.add(normalized);
  60948. str().overwrite(start, end, JSON.stringify(normalized), {
  60949. contentOnly: true,
  60950. });
  60951. }
  60952. // update the module graph for HMR analysis.
  60953. // node CSS imports does its own graph update in the css-analysis plugin so we
  60954. // only handle js graph updates here.
  60955. if (!isCSSRequest(importer)) {
  60956. // attached by pluginContainer.addWatchFile
  60957. const pluginImports = this._addedImports;
  60958. if (pluginImports) {
  60959. (await Promise.all([...pluginImports].map((id) => normalizeUrl(id, 0, true)))).forEach(([url]) => importedUrls.add(url));
  60960. }
  60961. // HMR transforms are no-ops in SSR, so an `accept` call will
  60962. // never be injected. Avoid updating the `isSelfAccepting`
  60963. // property for our module node in that case.
  60964. if (ssr && importerModule.isSelfAccepting) {
  60965. isSelfAccepting = true;
  60966. }
  60967. // a partially accepted module that accepts all its exports
  60968. // behaves like a self-accepted module in practice
  60969. if (!isSelfAccepting &&
  60970. isPartiallySelfAccepting &&
  60971. acceptedExports.size >= exports.length &&
  60972. exports.every((e) => acceptedExports.has(e.n))) {
  60973. isSelfAccepting = true;
  60974. }
  60975. const prunedImports = await moduleGraph.updateModuleInfo(importerModule, importedUrls, importedBindings, normalizedAcceptedUrls, isPartiallySelfAccepting ? acceptedExports : null, isSelfAccepting, ssr, staticImportedUrls);
  60976. if (hasHMR && prunedImports) {
  60977. handlePrunedModules(prunedImports, server);
  60978. }
  60979. }
  60980. debug$1?.(`${timeFrom(msAtStart)} ${colors$1.dim(`[${importedUrls.size} imports rewritten] ${prettifyUrl(importer, root)}`)}`);
  60981. if (s) {
  60982. return transformStableResult(s, importer, config);
  60983. }
  60984. else {
  60985. return source;
  60986. }
  60987. },
  60988. };
  60989. }
  60990. function mergeAcceptedUrls(orderedUrls) {
  60991. const acceptedUrls = new Set();
  60992. for (const urls of orderedUrls) {
  60993. if (!urls)
  60994. continue;
  60995. for (const url of urls)
  60996. acceptedUrls.add(url);
  60997. }
  60998. return acceptedUrls;
  60999. }
  61000. function createParseErrorInfo(importer, source) {
  61001. const isVue = importer.endsWith('.vue');
  61002. const isJsx = importer.endsWith('.jsx') || importer.endsWith('.tsx');
  61003. const maybeJSX = !isVue && isJSRequest(importer);
  61004. const probablyBinary = source.includes('\ufffd' /* unicode replacement character */);
  61005. const msg = isVue
  61006. ? `Install @vitejs/plugin-vue to handle .vue files.`
  61007. : maybeJSX
  61008. ? isJsx
  61009. ? `If you use tsconfig.json, make sure to not set jsx to preserve.`
  61010. : `If you are using JSX, make sure to name the file with the .jsx or .tsx extension.`
  61011. : `You may need to install appropriate plugins to handle the ${path$o.extname(importer)} file format, or if it's an asset, add "**/*${path$o.extname(importer)}" to \`assetsInclude\` in your configuration.`;
  61012. return {
  61013. message: `Failed to parse source for import analysis because the content ` +
  61014. `contains invalid JS syntax. ` +
  61015. msg,
  61016. showCodeFrame: !probablyBinary,
  61017. };
  61018. }
  61019. // prettier-ignore
  61020. const interopHelper = (m) => m?.__esModule ? m : { ...(typeof m === 'object' && !Array.isArray(m) || typeof m === 'function' ? m : {}), default: m };
  61021. function interopNamedImports(str, importSpecifier, rewrittenUrl, importIndex, importer, config) {
  61022. const source = str.original;
  61023. const { s: start, e: end, ss: expStart, se: expEnd, d: dynamicIndex, } = importSpecifier;
  61024. const exp = source.slice(expStart, expEnd);
  61025. if (dynamicIndex > -1) {
  61026. // rewrite `import('package')` to expose the default directly
  61027. str.overwrite(expStart, expEnd, `import('${rewrittenUrl}').then(m => (${interopHelper.toString()})(m.default))` +
  61028. getLineBreaks(exp), { contentOnly: true });
  61029. }
  61030. else {
  61031. const rawUrl = source.slice(start, end);
  61032. const rewritten = transformCjsImport(exp, rewrittenUrl, rawUrl, importIndex, importer, config);
  61033. if (rewritten) {
  61034. str.overwrite(expStart, expEnd, rewritten + getLineBreaks(exp), {
  61035. contentOnly: true,
  61036. });
  61037. }
  61038. else {
  61039. // #1439 export * from '...'
  61040. str.overwrite(start, end, rewrittenUrl + getLineBreaks(source.slice(start, end)), {
  61041. contentOnly: true,
  61042. });
  61043. }
  61044. }
  61045. }
  61046. // get line breaks to preserve line count for not breaking source maps
  61047. function getLineBreaks(str) {
  61048. return str.includes('\n') ? '\n'.repeat(str.split('\n').length - 1) : '';
  61049. }
  61050. /**
  61051. * Detect import statements to a known optimized CJS dependency and provide
  61052. * ES named imports interop. We do this by rewriting named imports to a variable
  61053. * assignment to the corresponding property on the `module.exports` of the cjs
  61054. * module. Note this doesn't support dynamic re-assignments from within the cjs
  61055. * module.
  61056. *
  61057. * Note that es-module-lexer treats `export * from '...'` as an import as well,
  61058. * so, we may encounter ExportAllDeclaration here, in which case `undefined`
  61059. * will be returned.
  61060. *
  61061. * Credits \@csr632 via #837
  61062. */
  61063. function transformCjsImport(importExp, url, rawUrl, importIndex, importer, config) {
  61064. const node = parse$9(importExp, {
  61065. ecmaVersion: 'latest',
  61066. sourceType: 'module',
  61067. }).body[0];
  61068. // `export * from '...'` may cause unexpected problem, so give it a warning
  61069. if (config.command === 'serve' &&
  61070. node.type === 'ExportAllDeclaration' &&
  61071. !node.exported) {
  61072. config.logger.warn(colors$1.yellow(`\nUnable to interop \`${importExp}\` in ${importer}, this may lose module exports. Please export "${rawUrl}" as ESM or use named exports instead, e.g. \`export { A, B } from "${rawUrl}"\``));
  61073. }
  61074. else if (node.type === 'ImportDeclaration' ||
  61075. node.type === 'ExportNamedDeclaration') {
  61076. if (!node.specifiers.length) {
  61077. return `import "${url}"`;
  61078. }
  61079. const importNames = [];
  61080. const exportNames = [];
  61081. let defaultExports = '';
  61082. for (const spec of node.specifiers) {
  61083. if (spec.type === 'ImportSpecifier' &&
  61084. spec.imported.type === 'Identifier') {
  61085. const importedName = spec.imported.name;
  61086. const localName = spec.local.name;
  61087. importNames.push({ importedName, localName });
  61088. }
  61089. else if (spec.type === 'ImportDefaultSpecifier') {
  61090. importNames.push({
  61091. importedName: 'default',
  61092. localName: spec.local.name,
  61093. });
  61094. }
  61095. else if (spec.type === 'ImportNamespaceSpecifier') {
  61096. importNames.push({ importedName: '*', localName: spec.local.name });
  61097. }
  61098. else if (spec.type === 'ExportSpecifier' &&
  61099. spec.exported.type === 'Identifier') {
  61100. // for ExportSpecifier, local name is same as imported name
  61101. // prefix the variable name to avoid clashing with other local variables
  61102. const importedName = spec.local.name;
  61103. // we want to specify exported name as variable and re-export it
  61104. const exportedName = spec.exported.name;
  61105. if (exportedName === 'default') {
  61106. defaultExports = makeLegalIdentifier(`__vite__cjsExportDefault_${importIndex}`);
  61107. importNames.push({ importedName, localName: defaultExports });
  61108. }
  61109. else {
  61110. const localName = makeLegalIdentifier(`__vite__cjsExport_${exportedName}`);
  61111. importNames.push({ importedName, localName });
  61112. exportNames.push(`${localName} as ${exportedName}`);
  61113. }
  61114. }
  61115. }
  61116. // If there is multiple import for same id in one file,
  61117. // importIndex will prevent the cjsModuleName to be duplicate
  61118. const cjsModuleName = makeLegalIdentifier(`__vite__cjsImport${importIndex}_${rawUrl}`);
  61119. const lines = [`import ${cjsModuleName} from "${url}"`];
  61120. importNames.forEach(({ importedName, localName }) => {
  61121. if (importedName === '*') {
  61122. lines.push(`const ${localName} = (${interopHelper.toString()})(${cjsModuleName})`);
  61123. }
  61124. else if (importedName === 'default') {
  61125. lines.push(`const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`);
  61126. }
  61127. else {
  61128. lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"]`);
  61129. }
  61130. });
  61131. if (defaultExports) {
  61132. lines.push(`export default ${defaultExports}`);
  61133. }
  61134. if (exportNames.length) {
  61135. lines.push(`export { ${exportNames.join(', ')} }`);
  61136. }
  61137. return lines.join('; ');
  61138. }
  61139. }
  61140. // Copied from `client/client.ts`. Only needed so we can inline inject this function for classic workers.
  61141. function __vite__injectQuery(url, queryToInject) {
  61142. // skip urls that won't be handled by vite
  61143. if (url[0] !== '.' && url[0] !== '/') {
  61144. return url;
  61145. }
  61146. // can't use pathname from URL since it may be relative like ../
  61147. const pathname = url.replace(/[?#].*$/, '');
  61148. const { search, hash } = new URL(url, 'http://vitejs.dev');
  61149. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${hash || ''}`;
  61150. }
  61151. /**
  61152. * A flag for injected helpers. This flag will be set to `false` if the output
  61153. * target is not native es - so that injected helper logic can be conditionally
  61154. * dropped.
  61155. */
  61156. const isModernFlag = `__VITE_IS_MODERN__`;
  61157. const preloadMethod = `__vitePreload`;
  61158. const preloadMarker = `__VITE_PRELOAD__`;
  61159. const preloadHelperId = '\0vite/preload-helper.js';
  61160. const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`, 'g');
  61161. const dynamicImportPrefixRE = /import\s*\(/;
  61162. function toRelativePath(filename, importer) {
  61163. const relPath = path$o.posix.relative(path$o.posix.dirname(importer), filename);
  61164. return relPath[0] === '.' ? relPath : `./${relPath}`;
  61165. }
  61166. function indexOfMatchInSlice(str, reg, pos = 0) {
  61167. reg.lastIndex = pos;
  61168. const result = reg.exec(str);
  61169. return result?.index ?? -1;
  61170. }
  61171. /**
  61172. * Helper for preloading CSS and direct imports of async chunks in parallel to
  61173. * the async chunk itself.
  61174. */
  61175. function detectScriptRel() {
  61176. const relList = typeof document !== 'undefined' && document.createElement('link').relList;
  61177. return relList && relList.supports && relList.supports('modulepreload')
  61178. ? 'modulepreload'
  61179. : 'preload';
  61180. }
  61181. function preload(baseModule, deps, importerUrl) {
  61182. let promise = Promise.resolve();
  61183. // @ts-expect-error __VITE_IS_MODERN__ will be replaced with boolean later
  61184. if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
  61185. const links = document.getElementsByTagName('link');
  61186. const cspNonceMeta = document.querySelector('meta[property=csp-nonce]');
  61187. // `.nonce` should be used to get along with nonce hiding (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#accessing_nonces_and_nonce_hiding)
  61188. // Firefox 67-74 uses modern chunks and supports CSP nonce, but does not support `.nonce`
  61189. // in that case fallback to getAttribute
  61190. const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute('nonce');
  61191. promise = Promise.all(deps.map((dep) => {
  61192. // @ts-expect-error assetsURL is declared before preload.toString()
  61193. dep = assetsURL(dep, importerUrl);
  61194. if (dep in seen)
  61195. return;
  61196. seen[dep] = true;
  61197. const isCss = dep.endsWith('.css');
  61198. const cssSelector = isCss ? '[rel="stylesheet"]' : '';
  61199. const isBaseRelative = !!importerUrl;
  61200. // check if the file is already preloaded by SSR markup
  61201. if (isBaseRelative) {
  61202. // When isBaseRelative is true then we have `importerUrl` and `dep` is
  61203. // already converted to an absolute URL by the `assetsURL` function
  61204. for (let i = links.length - 1; i >= 0; i--) {
  61205. const link = links[i];
  61206. // The `links[i].href` is an absolute URL thanks to browser doing the work
  61207. // for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
  61208. if (link.href === dep && (!isCss || link.rel === 'stylesheet')) {
  61209. return;
  61210. }
  61211. }
  61212. }
  61213. else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
  61214. return;
  61215. }
  61216. const link = document.createElement('link');
  61217. link.rel = isCss ? 'stylesheet' : scriptRel;
  61218. if (!isCss) {
  61219. link.as = 'script';
  61220. link.crossOrigin = '';
  61221. }
  61222. link.href = dep;
  61223. if (cspNonce) {
  61224. link.setAttribute('nonce', cspNonce);
  61225. }
  61226. document.head.appendChild(link);
  61227. if (isCss) {
  61228. return new Promise((res, rej) => {
  61229. link.addEventListener('load', res);
  61230. link.addEventListener('error', () => rej(new Error(`Unable to preload CSS for ${dep}`)));
  61231. });
  61232. }
  61233. }));
  61234. }
  61235. return promise
  61236. .then(() => baseModule())
  61237. .catch((err) => {
  61238. const e = new Event('vite:preloadError', { cancelable: true });
  61239. // @ts-expect-error custom payload
  61240. e.payload = err;
  61241. window.dispatchEvent(e);
  61242. if (!e.defaultPrevented) {
  61243. throw err;
  61244. }
  61245. });
  61246. }
  61247. /**
  61248. * Build only. During serve this is performed as part of ./importAnalysis.
  61249. */
  61250. function buildImportAnalysisPlugin(config) {
  61251. const ssr = !!config.build.ssr;
  61252. const isWorker = config.isWorker;
  61253. const insertPreload = !(ssr || !!config.build.lib || isWorker);
  61254. const resolveModulePreloadDependencies = config.build.modulePreload && config.build.modulePreload.resolveDependencies;
  61255. const renderBuiltUrl = config.experimental.renderBuiltUrl;
  61256. const customModulePreloadPaths = !!(resolveModulePreloadDependencies || renderBuiltUrl);
  61257. const isRelativeBase = config.base === './' || config.base === '';
  61258. const optimizeModulePreloadRelativePaths = isRelativeBase && !customModulePreloadPaths;
  61259. const { modulePreload } = config.build;
  61260. const scriptRel = modulePreload && modulePreload.polyfill
  61261. ? `'modulepreload'`
  61262. : `(${detectScriptRel.toString()})()`;
  61263. // There are three different cases for the preload list format in __vitePreload
  61264. //
  61265. // __vitePreload(() => import(asyncChunk), [ ...deps... ])
  61266. //
  61267. // This is maintained to keep backwards compatibility as some users developed plugins
  61268. // using regex over this list to workaround the fact that module preload wasn't
  61269. // configurable.
  61270. const assetsURL = customModulePreloadPaths
  61271. ? // If `experimental.renderBuiltUrl` or `build.modulePreload.resolveDependencies` are used
  61272. // the dependencies are already resolved. To avoid the need for `new URL(dep, import.meta.url)`
  61273. // a helper `__vitePreloadRelativeDep` is used to resolve from relative paths which can be minimized.
  61274. `function(dep, importerUrl) { return dep[0] === '.' ? new URL(dep, importerUrl).href : dep }`
  61275. : optimizeModulePreloadRelativePaths
  61276. ? // If there isn't custom resolvers affecting the deps list, deps in the list are relative
  61277. // to the current chunk and are resolved to absolute URL by the __vitePreload helper itself.
  61278. // The importerUrl is passed as third parameter to __vitePreload in this case
  61279. `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
  61280. : // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
  61281. // is appended inside __vitePreload too.
  61282. `function(dep) { return ${JSON.stringify(config.base)}+dep }`;
  61283. const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`;
  61284. return {
  61285. name: 'vite:build-import-analysis',
  61286. resolveId(id) {
  61287. if (id === preloadHelperId) {
  61288. return id;
  61289. }
  61290. },
  61291. load(id) {
  61292. if (id === preloadHelperId) {
  61293. return preloadCode;
  61294. }
  61295. },
  61296. async transform(source, importer) {
  61297. if (isInNodeModules$1(importer) && !dynamicImportPrefixRE.test(source)) {
  61298. return;
  61299. }
  61300. await init;
  61301. let imports = [];
  61302. try {
  61303. imports = parse$e(source)[0];
  61304. }
  61305. catch (_e) {
  61306. const e = _e;
  61307. const { message, showCodeFrame } = createParseErrorInfo(importer, source);
  61308. this.error(message, showCodeFrame ? e.idx : undefined);
  61309. }
  61310. if (!imports.length) {
  61311. return null;
  61312. }
  61313. let s;
  61314. const str = () => s || (s = new MagicString(source));
  61315. let needPreloadHelper = false;
  61316. for (let index = 0; index < imports.length; index++) {
  61317. const { e: end, ss: expStart, se: expEnd, d: dynamicIndex, a: attributeIndex, } = imports[index];
  61318. const isDynamicImport = dynamicIndex > -1;
  61319. // strip import attributes as we can process them ourselves
  61320. if (!isDynamicImport && attributeIndex > -1) {
  61321. str().remove(end + 1, expEnd);
  61322. }
  61323. if (isDynamicImport && insertPreload) {
  61324. needPreloadHelper = true;
  61325. str().prependLeft(expStart, `${preloadMethod}(() => `);
  61326. str().appendRight(expEnd, `,${isModernFlag}?"${preloadMarker}":void 0${optimizeModulePreloadRelativePaths || customModulePreloadPaths
  61327. ? ',import.meta.url'
  61328. : ''})`);
  61329. }
  61330. }
  61331. if (needPreloadHelper &&
  61332. insertPreload &&
  61333. !source.includes(`const ${preloadMethod} =`)) {
  61334. str().prepend(`import { ${preloadMethod} } from "${preloadHelperId}";`);
  61335. }
  61336. if (s) {
  61337. return {
  61338. code: s.toString(),
  61339. map: config.build.sourcemap
  61340. ? s.generateMap({ hires: 'boundary' })
  61341. : null,
  61342. };
  61343. }
  61344. },
  61345. renderChunk(code, _, { format }) {
  61346. // make sure we only perform the preload logic in modern builds.
  61347. if (code.indexOf(isModernFlag) > -1) {
  61348. const re = new RegExp(isModernFlag, 'g');
  61349. const isModern = String(format === 'es');
  61350. if (config.build.sourcemap) {
  61351. const s = new MagicString(code);
  61352. let match;
  61353. while ((match = re.exec(code))) {
  61354. s.update(match.index, match.index + isModernFlag.length, isModern);
  61355. }
  61356. return {
  61357. code: s.toString(),
  61358. map: s.generateMap({ hires: 'boundary' }),
  61359. };
  61360. }
  61361. else {
  61362. return code.replace(re, isModern);
  61363. }
  61364. }
  61365. return null;
  61366. },
  61367. generateBundle({ format }, bundle) {
  61368. if (format !== 'es' || ssr || isWorker) {
  61369. return;
  61370. }
  61371. for (const file in bundle) {
  61372. const chunk = bundle[file];
  61373. // can't use chunk.dynamicImports.length here since some modules e.g.
  61374. // dynamic import to constant json may get inlined.
  61375. if (chunk.type === 'chunk' && chunk.code.indexOf(preloadMarker) > -1) {
  61376. const code = chunk.code;
  61377. let imports;
  61378. try {
  61379. imports = parse$e(code)[0].filter((i) => i.d > -1);
  61380. }
  61381. catch (e) {
  61382. const loc = numberToPos(code, e.idx);
  61383. this.error({
  61384. name: e.name,
  61385. message: e.message,
  61386. stack: e.stack,
  61387. cause: e.cause,
  61388. pos: e.idx,
  61389. loc: { ...loc, file: chunk.fileName },
  61390. frame: generateCodeFrame(code, loc),
  61391. });
  61392. }
  61393. const s = new MagicString(code);
  61394. const rewroteMarkerStartPos = new Set(); // position of the leading double quote
  61395. const fileDeps = [];
  61396. const addFileDep = (url, runtime = false) => {
  61397. const index = fileDeps.findIndex((dep) => dep.url === url);
  61398. if (index === -1) {
  61399. return fileDeps.push({ url, runtime }) - 1;
  61400. }
  61401. else {
  61402. return index;
  61403. }
  61404. };
  61405. if (imports.length) {
  61406. for (let index = 0; index < imports.length; index++) {
  61407. // To handle escape sequences in specifier strings, the .n field will be provided where possible.
  61408. const { n: name, s: start, e: end, ss: expStart, se: expEnd, } = imports[index];
  61409. // check the chunk being imported
  61410. let url = name;
  61411. if (!url) {
  61412. const rawUrl = code.slice(start, end);
  61413. if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
  61414. url = rawUrl.slice(1, -1);
  61415. }
  61416. const deps = new Set();
  61417. let hasRemovedPureCssChunk = false;
  61418. let normalizedFile = undefined;
  61419. if (url) {
  61420. normalizedFile = path$o.posix.join(path$o.posix.dirname(chunk.fileName), url);
  61421. const ownerFilename = chunk.fileName;
  61422. // literal import - trace direct imports and add to deps
  61423. const analyzed = new Set();
  61424. const addDeps = (filename) => {
  61425. if (filename === ownerFilename)
  61426. return;
  61427. if (analyzed.has(filename))
  61428. return;
  61429. analyzed.add(filename);
  61430. const chunk = bundle[filename];
  61431. if (chunk) {
  61432. deps.add(chunk.fileName);
  61433. if (chunk.type === 'chunk') {
  61434. chunk.imports.forEach(addDeps);
  61435. // Ensure that the css imported by current chunk is loaded after the dependencies.
  61436. // So the style of current chunk won't be overwritten unexpectedly.
  61437. chunk.viteMetadata.importedCss.forEach((file) => {
  61438. deps.add(file);
  61439. });
  61440. }
  61441. }
  61442. else {
  61443. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  61444. const chunk = removedPureCssFiles.get(filename);
  61445. if (chunk) {
  61446. if (chunk.viteMetadata.importedCss.size) {
  61447. chunk.viteMetadata.importedCss.forEach((file) => {
  61448. deps.add(file);
  61449. });
  61450. hasRemovedPureCssChunk = true;
  61451. }
  61452. s.update(expStart, expEnd, 'Promise.resolve({})');
  61453. }
  61454. }
  61455. };
  61456. addDeps(normalizedFile);
  61457. }
  61458. let markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote, end);
  61459. // fix issue #3051
  61460. if (markerStartPos === -1 && imports.length === 1) {
  61461. markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote);
  61462. }
  61463. if (markerStartPos > 0) {
  61464. // the dep list includes the main chunk, so only need to reload when there are actual other deps.
  61465. const depsArray = deps.size > 1 ||
  61466. // main chunk is removed
  61467. (hasRemovedPureCssChunk && deps.size > 0)
  61468. ? modulePreload === false
  61469. ? // CSS deps use the same mechanism as module preloads, so even if disabled,
  61470. // we still need to pass these deps to the preload helper in dynamic imports.
  61471. [...deps].filter((d) => d.endsWith('.css'))
  61472. : [...deps]
  61473. : [];
  61474. let renderedDeps;
  61475. if (normalizedFile && customModulePreloadPaths) {
  61476. const { modulePreload } = config.build;
  61477. const resolveDependencies = modulePreload
  61478. ? modulePreload.resolveDependencies
  61479. : undefined;
  61480. let resolvedDeps;
  61481. if (resolveDependencies) {
  61482. // We can't let the user remove css deps as these aren't really preloads, they are just using
  61483. // the same mechanism as module preloads for this chunk
  61484. const cssDeps = [];
  61485. const otherDeps = [];
  61486. for (const dep of depsArray) {
  61487. (dep.endsWith('.css') ? cssDeps : otherDeps).push(dep);
  61488. }
  61489. resolvedDeps = [
  61490. ...resolveDependencies(normalizedFile, otherDeps, {
  61491. hostId: file,
  61492. hostType: 'js',
  61493. }),
  61494. ...cssDeps,
  61495. ];
  61496. }
  61497. else {
  61498. resolvedDeps = depsArray;
  61499. }
  61500. renderedDeps = resolvedDeps.map((dep) => {
  61501. const replacement = toOutputFilePathInJS(dep, 'asset', chunk.fileName, 'js', config, toRelativePath);
  61502. if (typeof replacement === 'string') {
  61503. return addFileDep(replacement);
  61504. }
  61505. return addFileDep(replacement.runtime, true);
  61506. });
  61507. }
  61508. else {
  61509. renderedDeps = depsArray.map((d) =>
  61510. // Don't include the assets dir if the default asset file names
  61511. // are used, the path will be reconstructed by the import preload helper
  61512. optimizeModulePreloadRelativePaths
  61513. ? addFileDep(toRelativePath(d, file))
  61514. : addFileDep(d));
  61515. }
  61516. s.update(markerStartPos, markerStartPos + preloadMarker.length + 2, renderedDeps.length > 0
  61517. ? `__vite__mapDeps([${renderedDeps.join(',')}])`
  61518. : `[]`);
  61519. rewroteMarkerStartPos.add(markerStartPos);
  61520. }
  61521. }
  61522. }
  61523. if (fileDeps.length > 0) {
  61524. const fileDepsCode = `[${fileDeps
  61525. .map((fileDep) => fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url))
  61526. .join(',')}]`;
  61527. const mapDepsCode = `const __vite__fileDeps=${fileDepsCode},__vite__mapDeps=i=>i.map(i=>__vite__fileDeps[i]);\n`;
  61528. // inject extra code at the top or next line of hashbang
  61529. if (code.startsWith('#!')) {
  61530. s.prependLeft(code.indexOf('\n') + 1, mapDepsCode);
  61531. }
  61532. else {
  61533. s.prepend(mapDepsCode);
  61534. }
  61535. }
  61536. // there may still be markers due to inlined dynamic imports, remove
  61537. // all the markers regardless
  61538. let markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote);
  61539. while (markerStartPos >= 0) {
  61540. if (!rewroteMarkerStartPos.has(markerStartPos)) {
  61541. s.update(markerStartPos, markerStartPos + preloadMarker.length + 2, 'void 0');
  61542. }
  61543. markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote, markerStartPos + preloadMarker.length + 2);
  61544. }
  61545. if (s.hasChanged()) {
  61546. chunk.code = s.toString();
  61547. if (config.build.sourcemap && chunk.map) {
  61548. const nextMap = s.generateMap({
  61549. source: chunk.fileName,
  61550. hires: 'boundary',
  61551. });
  61552. const map = combineSourcemaps(chunk.fileName, [
  61553. nextMap,
  61554. chunk.map,
  61555. ]);
  61556. map.toUrl = () => genSourceMapUrl(map);
  61557. chunk.map = map;
  61558. if (config.build.sourcemap === 'inline') {
  61559. chunk.code = chunk.code.replace(convertSourceMap.mapFileCommentRegex, '');
  61560. chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`;
  61561. }
  61562. else if (config.build.sourcemap) {
  61563. const mapAsset = bundle[chunk.fileName + '.map'];
  61564. if (mapAsset && mapAsset.type === 'asset') {
  61565. mapAsset.source = map.toString();
  61566. }
  61567. }
  61568. }
  61569. }
  61570. }
  61571. }
  61572. },
  61573. };
  61574. }
  61575. function ssrManifestPlugin(config) {
  61576. // module id => preload assets mapping
  61577. const ssrManifest = {};
  61578. const base = config.base; // TODO:base
  61579. return {
  61580. name: 'vite:ssr-manifest',
  61581. generateBundle(_options, bundle) {
  61582. for (const file in bundle) {
  61583. const chunk = bundle[file];
  61584. if (chunk.type === 'chunk') {
  61585. for (const id in chunk.modules) {
  61586. const normalizedId = normalizePath$3(relative$2(config.root, id));
  61587. const mappedChunks = ssrManifest[normalizedId] ?? (ssrManifest[normalizedId] = []);
  61588. if (!chunk.isEntry) {
  61589. mappedChunks.push(joinUrlSegments(base, chunk.fileName));
  61590. // <link> tags for entry chunks are already generated in static HTML,
  61591. // so we only need to record info for non-entry chunks.
  61592. chunk.viteMetadata.importedCss.forEach((file) => {
  61593. mappedChunks.push(joinUrlSegments(base, file));
  61594. });
  61595. }
  61596. chunk.viteMetadata.importedAssets.forEach((file) => {
  61597. mappedChunks.push(joinUrlSegments(base, file));
  61598. });
  61599. }
  61600. if (chunk.code.includes(preloadMethod)) {
  61601. // generate css deps map
  61602. const code = chunk.code;
  61603. let imports = [];
  61604. try {
  61605. imports = parse$e(code)[0].filter((i) => i.n && i.d > -1);
  61606. }
  61607. catch (_e) {
  61608. const e = _e;
  61609. const loc = numberToPos(code, e.idx);
  61610. this.error({
  61611. name: e.name,
  61612. message: e.message,
  61613. stack: e.stack,
  61614. cause: e.cause,
  61615. pos: e.idx,
  61616. loc: { ...loc, file: chunk.fileName },
  61617. frame: generateCodeFrame(code, loc),
  61618. });
  61619. }
  61620. if (imports.length) {
  61621. for (let index = 0; index < imports.length; index++) {
  61622. const { s: start, e: end, n: name } = imports[index];
  61623. // check the chunk being imported
  61624. const url = code.slice(start, end);
  61625. const deps = [];
  61626. const ownerFilename = chunk.fileName;
  61627. // literal import - trace direct imports and add to deps
  61628. const analyzed = new Set();
  61629. const addDeps = (filename) => {
  61630. if (filename === ownerFilename)
  61631. return;
  61632. if (analyzed.has(filename))
  61633. return;
  61634. analyzed.add(filename);
  61635. const chunk = bundle[filename];
  61636. if (chunk) {
  61637. chunk.viteMetadata.importedCss.forEach((file) => {
  61638. deps.push(joinUrlSegments(base, file)); // TODO:base
  61639. });
  61640. chunk.imports.forEach(addDeps);
  61641. }
  61642. };
  61643. const normalizedFile = normalizePath$3(join$2(dirname$2(chunk.fileName), url.slice(1, -1)));
  61644. addDeps(normalizedFile);
  61645. ssrManifest[basename$2(name)] = deps;
  61646. }
  61647. }
  61648. }
  61649. }
  61650. }
  61651. this.emitFile({
  61652. fileName: typeof config.build.ssrManifest === 'string'
  61653. ? config.build.ssrManifest
  61654. : '.vite/ssr-manifest.json',
  61655. type: 'asset',
  61656. source: JSON.stringify(sortObjectKeys(ssrManifest), undefined, 2),
  61657. });
  61658. },
  61659. };
  61660. }
  61661. /**
  61662. * A plugin to provide build load fallback for arbitrary request with queries.
  61663. */
  61664. function loadFallbackPlugin() {
  61665. return {
  61666. name: 'vite:load-fallback',
  61667. async load(id) {
  61668. try {
  61669. const cleanedId = cleanUrl(id);
  61670. const content = await fsp.readFile(cleanedId, 'utf-8');
  61671. this.addWatchFile(cleanedId);
  61672. return content;
  61673. }
  61674. catch (e) {
  61675. const content = await fsp.readFile(id, 'utf-8');
  61676. this.addWatchFile(id);
  61677. return content;
  61678. }
  61679. },
  61680. };
  61681. }
  61682. /**
  61683. * make sure systemjs register wrap to had complete parameters in system format
  61684. */
  61685. function completeSystemWrapPlugin() {
  61686. const SystemJSWrapRE = /System.register\(.*(\(exports\)|\(\))/g;
  61687. return {
  61688. name: 'vite:force-systemjs-wrap-complete',
  61689. renderChunk(code, chunk, opts) {
  61690. if (opts.format === 'system') {
  61691. return {
  61692. code: code.replace(SystemJSWrapRE, (s, s1) => s.replace(s1, '(exports, module)')),
  61693. map: null,
  61694. };
  61695. }
  61696. },
  61697. };
  61698. }
  61699. function resolveBuildOptions(raw, logger, root) {
  61700. const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload;
  61701. if (raw) {
  61702. const { polyfillModulePreload, ...rest } = raw;
  61703. raw = rest;
  61704. if (deprecatedPolyfillModulePreload !== undefined) {
  61705. logger.warn('polyfillModulePreload is deprecated. Use modulePreload.polyfill instead.');
  61706. }
  61707. if (deprecatedPolyfillModulePreload === false &&
  61708. raw.modulePreload === undefined) {
  61709. raw.modulePreload = { polyfill: false };
  61710. }
  61711. }
  61712. const modulePreload = raw?.modulePreload;
  61713. const defaultModulePreload = {
  61714. polyfill: true,
  61715. };
  61716. const defaultBuildOptions = {
  61717. outDir: 'dist',
  61718. assetsDir: 'assets',
  61719. assetsInlineLimit: DEFAULT_ASSETS_INLINE_LIMIT,
  61720. cssCodeSplit: !raw?.lib,
  61721. sourcemap: false,
  61722. rollupOptions: {},
  61723. minify: raw?.ssr ? false : 'esbuild',
  61724. terserOptions: {},
  61725. write: true,
  61726. emptyOutDir: null,
  61727. copyPublicDir: true,
  61728. manifest: false,
  61729. lib: false,
  61730. ssr: false,
  61731. ssrManifest: false,
  61732. ssrEmitAssets: false,
  61733. reportCompressedSize: true,
  61734. chunkSizeWarningLimit: 500,
  61735. watch: null,
  61736. };
  61737. const userBuildOptions = raw
  61738. ? mergeConfig(defaultBuildOptions, raw)
  61739. : defaultBuildOptions;
  61740. // @ts-expect-error Fallback options instead of merging
  61741. const resolved = {
  61742. target: 'modules',
  61743. cssTarget: false,
  61744. ...userBuildOptions,
  61745. commonjsOptions: {
  61746. include: [/node_modules/],
  61747. extensions: ['.js', '.cjs'],
  61748. ...userBuildOptions.commonjsOptions,
  61749. },
  61750. dynamicImportVarsOptions: {
  61751. warnOnError: true,
  61752. exclude: [/node_modules/],
  61753. ...userBuildOptions.dynamicImportVarsOptions,
  61754. },
  61755. // Resolve to false | object
  61756. modulePreload: modulePreload === false
  61757. ? false
  61758. : typeof modulePreload === 'object'
  61759. ? {
  61760. ...defaultModulePreload,
  61761. ...modulePreload,
  61762. }
  61763. : defaultModulePreload,
  61764. };
  61765. // handle special build targets
  61766. if (resolved.target === 'modules') {
  61767. resolved.target = ESBUILD_MODULES_TARGET;
  61768. }
  61769. else if (resolved.target === 'esnext' && resolved.minify === 'terser') {
  61770. try {
  61771. const terserPackageJsonPath = requireResolveFromRootWithFallback(root, 'terser/package.json');
  61772. const terserPackageJson = JSON.parse(fs$l.readFileSync(terserPackageJsonPath, 'utf-8'));
  61773. const v = terserPackageJson.version.split('.');
  61774. if (v[0] === '5' && v[1] < 16) {
  61775. // esnext + terser 5.16<: limit to es2021 so it can be minified by terser
  61776. resolved.target = 'es2021';
  61777. }
  61778. }
  61779. catch { }
  61780. }
  61781. if (!resolved.cssTarget) {
  61782. resolved.cssTarget = resolved.target;
  61783. }
  61784. // normalize false string into actual false
  61785. if (resolved.minify === 'false') {
  61786. resolved.minify = false;
  61787. }
  61788. else if (resolved.minify === true) {
  61789. resolved.minify = 'esbuild';
  61790. }
  61791. if (resolved.cssMinify == null) {
  61792. resolved.cssMinify = !!resolved.minify;
  61793. }
  61794. return resolved;
  61795. }
  61796. async function resolveBuildPlugins(config) {
  61797. const options = config.build;
  61798. const { commonjsOptions } = options;
  61799. const usePluginCommonjs = !Array.isArray(commonjsOptions?.include) ||
  61800. commonjsOptions?.include.length !== 0;
  61801. const rollupOptionsPlugins = options.rollupOptions.plugins;
  61802. return {
  61803. pre: [
  61804. completeSystemWrapPlugin(),
  61805. ...(usePluginCommonjs ? [commonjs(options.commonjsOptions)] : []),
  61806. dataURIPlugin(),
  61807. ...(await asyncFlatten(arraify(rollupOptionsPlugins))).filter(Boolean),
  61808. ...(config.isWorker ? [webWorkerPostPlugin()] : []),
  61809. ],
  61810. post: [
  61811. buildImportAnalysisPlugin(config),
  61812. ...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []),
  61813. ...(options.minify ? [terserPlugin(config)] : []),
  61814. ...(!config.isWorker
  61815. ? [
  61816. ...(options.manifest ? [manifestPlugin(config)] : []),
  61817. ...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
  61818. buildReporterPlugin(config),
  61819. ]
  61820. : []),
  61821. loadFallbackPlugin(),
  61822. ],
  61823. };
  61824. }
  61825. /**
  61826. * Bundles the app for production.
  61827. * Returns a Promise containing the build result.
  61828. */
  61829. async function build(inlineConfig = {}) {
  61830. const config = await resolveConfig(inlineConfig, 'build', 'production', 'production');
  61831. const options = config.build;
  61832. const ssr = !!options.ssr;
  61833. const libOptions = options.lib;
  61834. config.logger.info(colors$1.cyan(`vite v${VERSION} ${colors$1.green(`building ${ssr ? `SSR bundle ` : ``}for ${config.mode}...`)}`));
  61835. const resolve = (p) => path$o.resolve(config.root, p);
  61836. const input = libOptions
  61837. ? options.rollupOptions?.input ||
  61838. (typeof libOptions.entry === 'string'
  61839. ? resolve(libOptions.entry)
  61840. : Array.isArray(libOptions.entry)
  61841. ? libOptions.entry.map(resolve)
  61842. : Object.fromEntries(Object.entries(libOptions.entry).map(([alias, file]) => [
  61843. alias,
  61844. resolve(file),
  61845. ])))
  61846. : typeof options.ssr === 'string'
  61847. ? resolve(options.ssr)
  61848. : options.rollupOptions?.input || resolve('index.html');
  61849. if (ssr && typeof input === 'string' && input.endsWith('.html')) {
  61850. throw new Error(`rollupOptions.input should not be an html file when building for SSR. ` +
  61851. `Please specify a dedicated SSR entry.`);
  61852. }
  61853. if (config.build.cssCodeSplit === false) {
  61854. const inputs = typeof input === 'string'
  61855. ? [input]
  61856. : Array.isArray(input)
  61857. ? input
  61858. : Object.values(input);
  61859. if (inputs.some((input) => input.endsWith('.css'))) {
  61860. throw new Error(`When "build.cssCodeSplit: false" is set, "rollupOptions.input" should not include CSS files.`);
  61861. }
  61862. }
  61863. const outDir = resolve(options.outDir);
  61864. // inject ssr arg to plugin load/transform hooks
  61865. const plugins = (ssr ? config.plugins.map((p) => injectSsrFlagToHooks(p)) : config.plugins);
  61866. const rollupOptions = {
  61867. preserveEntrySignatures: ssr
  61868. ? 'allow-extension'
  61869. : libOptions
  61870. ? 'strict'
  61871. : false,
  61872. cache: config.build.watch ? undefined : false,
  61873. ...options.rollupOptions,
  61874. input,
  61875. plugins,
  61876. external: options.rollupOptions?.external,
  61877. onwarn(warning, warn) {
  61878. onRollupWarning(warning, warn, config);
  61879. },
  61880. };
  61881. const mergeRollupError = (e) => {
  61882. let msg = colors$1.red((e.plugin ? `[${e.plugin}] ` : '') + e.message);
  61883. if (e.id) {
  61884. msg += `\nfile: ${colors$1.cyan(e.id + (e.loc ? `:${e.loc.line}:${e.loc.column}` : ''))}`;
  61885. }
  61886. if (e.frame) {
  61887. msg += `\n` + colors$1.yellow(e.frame);
  61888. }
  61889. return msg;
  61890. };
  61891. const outputBuildError = (e) => {
  61892. const msg = mergeRollupError(e);
  61893. clearLine();
  61894. config.logger.error(msg, { error: e });
  61895. };
  61896. let bundle;
  61897. let startTime;
  61898. try {
  61899. const buildOutputOptions = (output = {}) => {
  61900. // @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
  61901. if (output.output) {
  61902. config.logger.warn(`You've set "rollupOptions.output.output" in your config. ` +
  61903. `This is deprecated and will override all Vite.js default output options. ` +
  61904. `Please use "rollupOptions.output" instead.`);
  61905. }
  61906. if (output.file) {
  61907. throw new Error(`Vite does not support "rollupOptions.output.file". ` +
  61908. `Please use "rollupOptions.output.dir" and "rollupOptions.output.entryFileNames" instead.`);
  61909. }
  61910. if (output.sourcemap) {
  61911. config.logger.warnOnce(colors$1.yellow(`Vite does not support "rollupOptions.output.sourcemap". ` +
  61912. `Please use "build.sourcemap" instead.`));
  61913. }
  61914. const ssrNodeBuild = ssr && config.ssr.target === 'node';
  61915. const ssrWorkerBuild = ssr && config.ssr.target === 'webworker';
  61916. const format = output.format || 'es';
  61917. const jsExt = ssrNodeBuild || libOptions
  61918. ? resolveOutputJsExtension(format, findNearestPackageData(config.root, config.packageCache)?.data
  61919. .type)
  61920. : 'js';
  61921. return {
  61922. dir: outDir,
  61923. // Default format is 'es' for regular and for SSR builds
  61924. format,
  61925. exports: 'auto',
  61926. sourcemap: options.sourcemap,
  61927. name: libOptions ? libOptions.name : undefined,
  61928. hoistTransitiveImports: libOptions ? false : undefined,
  61929. // es2015 enables `generatedCode.symbols`
  61930. // - #764 add `Symbol.toStringTag` when build es module into cjs chunk
  61931. // - #1048 add `Symbol.toStringTag` for module default export
  61932. generatedCode: 'es2015',
  61933. entryFileNames: ssr
  61934. ? `[name].${jsExt}`
  61935. : libOptions
  61936. ? ({ name }) => resolveLibFilename(libOptions, format, name, config.root, jsExt, config.packageCache)
  61937. : path$o.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61938. chunkFileNames: libOptions
  61939. ? `[name]-[hash].${jsExt}`
  61940. : path$o.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61941. assetFileNames: libOptions
  61942. ? `[name].[ext]`
  61943. : path$o.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
  61944. inlineDynamicImports: output.format === 'umd' ||
  61945. output.format === 'iife' ||
  61946. (ssrWorkerBuild &&
  61947. (typeof input === 'string' || Object.keys(input).length === 1)),
  61948. ...output,
  61949. };
  61950. };
  61951. // resolve lib mode outputs
  61952. const outputs = resolveBuildOutputs(options.rollupOptions?.output, libOptions, config.logger);
  61953. const normalizedOutputs = [];
  61954. if (Array.isArray(outputs)) {
  61955. for (const resolvedOutput of outputs) {
  61956. normalizedOutputs.push(buildOutputOptions(resolvedOutput));
  61957. }
  61958. }
  61959. else {
  61960. normalizedOutputs.push(buildOutputOptions(outputs));
  61961. }
  61962. const resolvedOutDirs = getResolvedOutDirs(config.root, options.outDir, options.rollupOptions?.output);
  61963. const emptyOutDir = resolveEmptyOutDir(options.emptyOutDir, config.root, resolvedOutDirs, config.logger);
  61964. // watch file changes with rollup
  61965. if (config.build.watch) {
  61966. config.logger.info(colors$1.cyan(`\nwatching for file changes...`));
  61967. const resolvedChokidarOptions = resolveChokidarOptions(config, config.build.watch.chokidar, resolvedOutDirs, emptyOutDir);
  61968. const { watch } = await import('rollup');
  61969. const watcher = watch({
  61970. ...rollupOptions,
  61971. output: normalizedOutputs,
  61972. watch: {
  61973. ...config.build.watch,
  61974. chokidar: resolvedChokidarOptions,
  61975. },
  61976. });
  61977. watcher.on('event', (event) => {
  61978. if (event.code === 'BUNDLE_START') {
  61979. config.logger.info(colors$1.cyan(`\nbuild started...`));
  61980. if (options.write) {
  61981. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  61982. }
  61983. }
  61984. else if (event.code === 'BUNDLE_END') {
  61985. event.result.close();
  61986. config.logger.info(colors$1.cyan(`built in ${event.duration}ms.`));
  61987. }
  61988. else if (event.code === 'ERROR') {
  61989. outputBuildError(event.error);
  61990. }
  61991. });
  61992. return watcher;
  61993. }
  61994. // write or generate files with rollup
  61995. const { rollup } = await import('rollup');
  61996. startTime = Date.now();
  61997. bundle = await rollup(rollupOptions);
  61998. if (options.write) {
  61999. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  62000. }
  62001. const res = [];
  62002. for (const output of normalizedOutputs) {
  62003. res.push(await bundle[options.write ? 'write' : 'generate'](output));
  62004. }
  62005. config.logger.info(`${colors$1.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`);
  62006. return Array.isArray(outputs) ? res : res[0];
  62007. }
  62008. catch (e) {
  62009. e.message = mergeRollupError(e);
  62010. clearLine();
  62011. if (startTime) {
  62012. config.logger.error(`${colors$1.red('x')} Build failed in ${displayTime(Date.now() - startTime)}`);
  62013. startTime = undefined;
  62014. }
  62015. throw e;
  62016. }
  62017. finally {
  62018. if (bundle)
  62019. await bundle.close();
  62020. }
  62021. }
  62022. function prepareOutDir(outDirs, emptyOutDir, config) {
  62023. const outDirsArray = [...outDirs];
  62024. for (const outDir of outDirs) {
  62025. if (emptyOutDir !== false && fs$l.existsSync(outDir)) {
  62026. // skip those other outDirs which are nested in current outDir
  62027. const skipDirs = outDirsArray
  62028. .map((dir) => {
  62029. const relative = path$o.relative(outDir, dir);
  62030. if (relative &&
  62031. !relative.startsWith('..') &&
  62032. !path$o.isAbsolute(relative)) {
  62033. return relative;
  62034. }
  62035. return '';
  62036. })
  62037. .filter(Boolean);
  62038. emptyDir(outDir, [...skipDirs, '.git']);
  62039. }
  62040. if (config.build.copyPublicDir &&
  62041. config.publicDir &&
  62042. fs$l.existsSync(config.publicDir)) {
  62043. if (!areSeparateFolders(outDir, config.publicDir)) {
  62044. config.logger.warn(colors$1.yellow(`\n${colors$1.bold(`(!)`)} The public directory feature may not work correctly. outDir ${colors$1.white(colors$1.dim(outDir))} and publicDir ${colors$1.white(colors$1.dim(config.publicDir))} are not separate folders.\n`));
  62045. }
  62046. copyDir(config.publicDir, outDir);
  62047. }
  62048. }
  62049. }
  62050. function getPkgName(name) {
  62051. return name?.[0] === '@' ? name.split('/')[1] : name;
  62052. }
  62053. function resolveOutputJsExtension(format, type = 'commonjs') {
  62054. if (type === 'module') {
  62055. return format === 'cjs' || format === 'umd' ? 'cjs' : 'js';
  62056. }
  62057. else {
  62058. return format === 'es' ? 'mjs' : 'js';
  62059. }
  62060. }
  62061. function resolveLibFilename(libOptions, format, entryName, root, extension, packageCache) {
  62062. if (typeof libOptions.fileName === 'function') {
  62063. return libOptions.fileName(format, entryName);
  62064. }
  62065. const packageJson = findNearestPackageData(root, packageCache)?.data;
  62066. const name = libOptions.fileName ||
  62067. (packageJson && typeof libOptions.entry === 'string'
  62068. ? getPkgName(packageJson.name)
  62069. : entryName);
  62070. if (!name)
  62071. throw new Error('Name in package.json is required if option "build.lib.fileName" is not provided.');
  62072. extension ??= resolveOutputJsExtension(format, packageJson?.type);
  62073. if (format === 'cjs' || format === 'es') {
  62074. return `${name}.${extension}`;
  62075. }
  62076. return `${name}.${format}.${extension}`;
  62077. }
  62078. function resolveBuildOutputs(outputs, libOptions, logger) {
  62079. if (libOptions) {
  62080. const libHasMultipleEntries = typeof libOptions.entry !== 'string' &&
  62081. Object.values(libOptions.entry).length > 1;
  62082. const libFormats = libOptions.formats ||
  62083. (libHasMultipleEntries ? ['es', 'cjs'] : ['es', 'umd']);
  62084. if (!Array.isArray(outputs)) {
  62085. if (libFormats.includes('umd') || libFormats.includes('iife')) {
  62086. if (libHasMultipleEntries) {
  62087. throw new Error('Multiple entry points are not supported when output formats include "umd" or "iife".');
  62088. }
  62089. if (!libOptions.name) {
  62090. throw new Error('Option "build.lib.name" is required when output formats include "umd" or "iife".');
  62091. }
  62092. }
  62093. return libFormats.map((format) => ({ ...outputs, format }));
  62094. }
  62095. // By this point, we know "outputs" is an Array.
  62096. if (libOptions.formats) {
  62097. logger.warn(colors$1.yellow('"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.'));
  62098. }
  62099. outputs.forEach((output) => {
  62100. if (['umd', 'iife'].includes(output.format) && !output.name) {
  62101. throw new Error('Entries in "build.rollupOptions.output" must specify "name" when the format is "umd" or "iife".');
  62102. }
  62103. });
  62104. }
  62105. return outputs;
  62106. }
  62107. const warningIgnoreList = [`CIRCULAR_DEPENDENCY`, `THIS_IS_UNDEFINED`];
  62108. const dynamicImportWarningIgnoreList = [
  62109. `Unsupported expression`,
  62110. `statically analyzed`,
  62111. ];
  62112. function clearLine() {
  62113. const tty = process.stdout.isTTY && !process.env.CI;
  62114. if (tty) {
  62115. process.stdout.clearLine(0);
  62116. process.stdout.cursorTo(0);
  62117. }
  62118. }
  62119. function onRollupWarning(warning, warn, config) {
  62120. const viteWarn = (warnLog) => {
  62121. let warning;
  62122. if (typeof warnLog === 'function') {
  62123. warning = warnLog();
  62124. }
  62125. else {
  62126. warning = warnLog;
  62127. }
  62128. if (typeof warning === 'object') {
  62129. if (warning.code === 'UNRESOLVED_IMPORT') {
  62130. const id = warning.id;
  62131. const exporter = warning.exporter;
  62132. // throw unless it's commonjs external...
  62133. if (!id || !id.endsWith('?commonjs-external')) {
  62134. throw new Error(`[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
  62135. `This is most likely unintended because it can break your application at runtime.\n` +
  62136. `If you do want to externalize this module explicitly add it to\n` +
  62137. `\`build.rollupOptions.external\``);
  62138. }
  62139. }
  62140. if (warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
  62141. dynamicImportWarningIgnoreList.some((msg) =>
  62142. // @ts-expect-error warning is RollupLog
  62143. warning.message.includes(msg))) {
  62144. return;
  62145. }
  62146. if (warningIgnoreList.includes(warning.code)) {
  62147. return;
  62148. }
  62149. if (warning.code === 'PLUGIN_WARNING') {
  62150. config.logger.warn(`${colors$1.bold(colors$1.yellow(`[plugin:${warning.plugin}]`))} ${colors$1.yellow(warning.message)}`);
  62151. return;
  62152. }
  62153. }
  62154. warn(warnLog);
  62155. };
  62156. clearLine();
  62157. const userOnWarn = config.build.rollupOptions?.onwarn;
  62158. if (userOnWarn) {
  62159. userOnWarn(warning, viteWarn);
  62160. }
  62161. else {
  62162. viteWarn(warning);
  62163. }
  62164. }
  62165. function resolveUserExternal(user, id, parentId, isResolved) {
  62166. if (typeof user === 'function') {
  62167. return user(id, parentId, isResolved);
  62168. }
  62169. else if (Array.isArray(user)) {
  62170. return user.some((test) => isExternal(id, test));
  62171. }
  62172. else {
  62173. return isExternal(id, user);
  62174. }
  62175. }
  62176. function isExternal(id, test) {
  62177. if (typeof test === 'string') {
  62178. return id === test;
  62179. }
  62180. else {
  62181. return test.test(id);
  62182. }
  62183. }
  62184. function injectSsrFlagToHooks(plugin) {
  62185. const { resolveId, load, transform } = plugin;
  62186. return {
  62187. ...plugin,
  62188. resolveId: wrapSsrResolveId(resolveId),
  62189. load: wrapSsrLoad(load),
  62190. transform: wrapSsrTransform(transform),
  62191. };
  62192. }
  62193. function wrapSsrResolveId(hook) {
  62194. if (!hook)
  62195. return;
  62196. const fn = getHookHandler(hook);
  62197. const handler = function (id, importer, options) {
  62198. return fn.call(this, id, importer, injectSsrFlag(options));
  62199. };
  62200. if ('handler' in hook) {
  62201. return {
  62202. ...hook,
  62203. handler,
  62204. };
  62205. }
  62206. else {
  62207. return handler;
  62208. }
  62209. }
  62210. function wrapSsrLoad(hook) {
  62211. if (!hook)
  62212. return;
  62213. const fn = getHookHandler(hook);
  62214. const handler = function (id, ...args) {
  62215. // @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
  62216. return fn.call(this, id, injectSsrFlag(args[0]));
  62217. };
  62218. if ('handler' in hook) {
  62219. return {
  62220. ...hook,
  62221. handler,
  62222. };
  62223. }
  62224. else {
  62225. return handler;
  62226. }
  62227. }
  62228. function wrapSsrTransform(hook) {
  62229. if (!hook)
  62230. return;
  62231. const fn = getHookHandler(hook);
  62232. const handler = function (code, importer, ...args) {
  62233. // @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
  62234. return fn.call(this, code, importer, injectSsrFlag(args[0]));
  62235. };
  62236. if ('handler' in hook) {
  62237. return {
  62238. ...hook,
  62239. handler,
  62240. };
  62241. }
  62242. else {
  62243. return handler;
  62244. }
  62245. }
  62246. function injectSsrFlag(options) {
  62247. return { ...(options ?? {}), ssr: true };
  62248. }
  62249. /*
  62250. The following functions are copied from rollup
  62251. https://github.com/rollup/rollup/blob/ce6cb93098850a46fa242e37b74a919e99a5de28/src/ast/nodes/MetaProperty.ts#L155-L203
  62252. https://github.com/rollup/rollup
  62253. The MIT License (MIT)
  62254. Copyright (c) 2017 [these people](https://github.com/rollup/rollup/graphs/contributors)
  62255. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  62256. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  62257. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  62258. */
  62259. const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/;
  62260. const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g;
  62261. const backSlashRegEx = /\\/g;
  62262. function escapeId(id) {
  62263. if (!needsEscapeRegEx.test(id))
  62264. return id;
  62265. return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1');
  62266. }
  62267. const getResolveUrl = (path, URL = 'URL') => `new ${URL}(${path}).href`;
  62268. const getRelativeUrlFromDocument = (relativePath, umd = false) => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', ${umd ? `typeof document === 'undefined' ? location.href : ` : ''}document.currentScript && document.currentScript.src || document.baseURI`);
  62269. const getFileUrlFromFullPath = (path) => `require('u' + 'rl').pathToFileURL(${path}).href`;
  62270. const getFileUrlFromRelativePath = (path) => getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`);
  62271. const relativeUrlMechanisms = {
  62272. amd: (relativePath) => {
  62273. if (relativePath[0] !== '.')
  62274. relativePath = './' + relativePath;
  62275. return getResolveUrl(`require.toUrl('${escapeId(relativePath)}'), document.baseURI`);
  62276. },
  62277. cjs: (relativePath) => `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(relativePath)} : ${getRelativeUrlFromDocument(relativePath)})`,
  62278. es: (relativePath) => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`),
  62279. iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
  62280. // NOTE: make sure rollup generate `module` params
  62281. system: (relativePath) => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`),
  62282. umd: (relativePath) => `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(relativePath)} : ${getRelativeUrlFromDocument(relativePath, true)})`,
  62283. };
  62284. /* end of copy */
  62285. const customRelativeUrlMechanisms = {
  62286. ...relativeUrlMechanisms,
  62287. 'worker-iife': (relativePath) => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`),
  62288. };
  62289. function toOutputFilePathInJS(filename, type, hostId, hostType, config, toRelative) {
  62290. const { renderBuiltUrl } = config.experimental;
  62291. let relative = config.base === '' || config.base === './';
  62292. if (renderBuiltUrl) {
  62293. const result = renderBuiltUrl(filename, {
  62294. hostId,
  62295. hostType,
  62296. type,
  62297. ssr: !!config.build.ssr,
  62298. });
  62299. if (typeof result === 'object') {
  62300. if (result.runtime) {
  62301. return { runtime: result.runtime };
  62302. }
  62303. if (typeof result.relative === 'boolean') {
  62304. relative = result.relative;
  62305. }
  62306. }
  62307. else if (result) {
  62308. return result;
  62309. }
  62310. }
  62311. if (relative && !config.build.ssr) {
  62312. return toRelative(filename, hostId);
  62313. }
  62314. return joinUrlSegments(config.base, filename);
  62315. }
  62316. function createToImportMetaURLBasedRelativeRuntime(format, isWorker) {
  62317. const formatLong = isWorker && format === 'iife' ? 'worker-iife' : format;
  62318. const toRelativePath = customRelativeUrlMechanisms[formatLong];
  62319. return (filename, importer) => ({
  62320. runtime: toRelativePath(path$o.posix.relative(path$o.dirname(importer), filename)),
  62321. });
  62322. }
  62323. function toOutputFilePathWithoutRuntime(filename, type, hostId, hostType, config, toRelative) {
  62324. const { renderBuiltUrl } = config.experimental;
  62325. let relative = config.base === '' || config.base === './';
  62326. if (renderBuiltUrl) {
  62327. const result = renderBuiltUrl(filename, {
  62328. hostId,
  62329. hostType,
  62330. type,
  62331. ssr: !!config.build.ssr,
  62332. });
  62333. if (typeof result === 'object') {
  62334. if (result.runtime) {
  62335. throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
  62336. }
  62337. if (typeof result.relative === 'boolean') {
  62338. relative = result.relative;
  62339. }
  62340. }
  62341. else if (result) {
  62342. return result;
  62343. }
  62344. }
  62345. if (relative && !config.build.ssr) {
  62346. return toRelative(filename, hostId);
  62347. }
  62348. else {
  62349. return joinUrlSegments(config.base, filename);
  62350. }
  62351. }
  62352. const toOutputFilePathInCss = toOutputFilePathWithoutRuntime;
  62353. const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime;
  62354. function areSeparateFolders(a, b) {
  62355. const na = normalizePath$3(a);
  62356. const nb = normalizePath$3(b);
  62357. return (na !== nb &&
  62358. !na.startsWith(withTrailingSlash(nb)) &&
  62359. !nb.startsWith(withTrailingSlash(na)));
  62360. }
  62361. var build$1 = {
  62362. __proto__: null,
  62363. build: build,
  62364. createToImportMetaURLBasedRelativeRuntime: createToImportMetaURLBasedRelativeRuntime,
  62365. onRollupWarning: onRollupWarning,
  62366. resolveBuildOptions: resolveBuildOptions,
  62367. resolveBuildOutputs: resolveBuildOutputs,
  62368. resolveBuildPlugins: resolveBuildPlugins,
  62369. resolveLibFilename: resolveLibFilename,
  62370. resolveUserExternal: resolveUserExternal,
  62371. toOutputFilePathInCss: toOutputFilePathInCss,
  62372. toOutputFilePathInHtml: toOutputFilePathInHtml,
  62373. toOutputFilePathInJS: toOutputFilePathInJS,
  62374. toOutputFilePathWithoutRuntime: toOutputFilePathWithoutRuntime
  62375. };
  62376. // NOTE: supports Node 6.x
  62377. const NOOP = () => {};
  62378. const MIMES = /text|javascript|\/json|xml/i;
  62379. /**
  62380. * @param {any} chunk
  62381. * @param {BufferEncoding} enc
  62382. * @returns {number}
  62383. */
  62384. function getChunkSize(chunk, enc) {
  62385. return chunk ? Buffer.byteLength(chunk, enc) : 0;
  62386. }
  62387. /**
  62388. * @param {import('./index.d.mts').Options} [options]
  62389. * @returns {import('./index.d.mts').Middleware}
  62390. */
  62391. function compression ({ threshold = 1024, level = -1, brotli = false, gzip = true, mimes = MIMES } = {}) {
  62392. const brotliOpts = (typeof brotli === 'object' && brotli) || {};
  62393. const gzipOpts = (typeof gzip === 'object' && gzip) || {};
  62394. // disable Brotli on Node<12.7 where it is unsupported:
  62395. if (!zlib$1.createBrotliCompress) brotli = false;
  62396. return (req, res, next = NOOP) => {
  62397. const accept = req.headers['accept-encoding'] + '';
  62398. const encoding = ((brotli && accept.match(/\bbr\b/)) || (gzip && accept.match(/\bgzip\b/)) || [])[0];
  62399. // skip if no response body or no supported encoding:
  62400. if (req.method === 'HEAD' || !encoding) return next();
  62401. /** @type {zlib.Gzip | zlib.BrotliCompress} */
  62402. let compress;
  62403. /** @type {Array<[string, function]>?} */
  62404. let pendingListeners = [];
  62405. let pendingStatus = 0;
  62406. let started = false;
  62407. let size = 0;
  62408. function start() {
  62409. started = true;
  62410. // @ts-ignore
  62411. size = res.getHeader('Content-Length') | 0 || size;
  62412. const compressible = mimes.test(
  62413. String(res.getHeader('Content-Type') || 'text/plain')
  62414. );
  62415. const cleartext = !res.getHeader('Content-Encoding');
  62416. const listeners = pendingListeners || [];
  62417. if (compressible && cleartext && size >= threshold) {
  62418. res.setHeader('Content-Encoding', encoding);
  62419. res.removeHeader('Content-Length');
  62420. if (encoding === 'br') {
  62421. compress = zlib$1.createBrotliCompress({
  62422. params: Object.assign({
  62423. [zlib$1.constants.BROTLI_PARAM_QUALITY]: level,
  62424. [zlib$1.constants.BROTLI_PARAM_SIZE_HINT]: size,
  62425. }, brotliOpts)
  62426. });
  62427. } else {
  62428. compress = zlib$1.createGzip(
  62429. Object.assign({ level }, gzipOpts)
  62430. );
  62431. }
  62432. // backpressure
  62433. compress.on('data', chunk => write.call(res, chunk) || compress.pause());
  62434. on.call(res, 'drain', () => compress.resume());
  62435. compress.on('end', () => end.call(res));
  62436. listeners.forEach(p => compress.on.apply(compress, p));
  62437. } else {
  62438. pendingListeners = null;
  62439. listeners.forEach(p => on.apply(res, p));
  62440. }
  62441. writeHead.call(res, pendingStatus || res.statusCode);
  62442. }
  62443. const { end, write, on, writeHead } = res;
  62444. res.writeHead = function (status, reason, headers) {
  62445. if (typeof reason !== 'string') [headers, reason] = [reason, headers];
  62446. if (headers) for (let k in headers) res.setHeader(k, headers[k]);
  62447. pendingStatus = status;
  62448. return this;
  62449. };
  62450. res.write = function (chunk, enc) {
  62451. size += getChunkSize(chunk, enc);
  62452. if (!started) start();
  62453. if (!compress) return write.apply(this, arguments);
  62454. return compress.write.apply(compress, arguments);
  62455. };
  62456. res.end = function (chunk, enc) {
  62457. if (arguments.length > 0 && typeof chunk !== 'function') {
  62458. size += getChunkSize(chunk, enc);
  62459. }
  62460. if (!started) start();
  62461. if (!compress) return end.apply(this, arguments);
  62462. return compress.end.apply(compress, arguments);
  62463. };
  62464. res.on = function (type, listener) {
  62465. if (!pendingListeners) on.call(this, type, listener);
  62466. else if (compress) compress.on(type, listener);
  62467. else pendingListeners.push([type, listener]);
  62468. return this;
  62469. };
  62470. next();
  62471. };
  62472. }
  62473. function resolvePreviewOptions(preview, server) {
  62474. // The preview server inherits every CommonServerOption from the `server` config
  62475. // except for the port to enable having both the dev and preview servers running
  62476. // at the same time without extra configuration
  62477. return {
  62478. port: preview?.port,
  62479. strictPort: preview?.strictPort ?? server.strictPort,
  62480. host: preview?.host ?? server.host,
  62481. https: preview?.https ?? server.https,
  62482. open: preview?.open ?? server.open,
  62483. proxy: preview?.proxy ?? server.proxy,
  62484. cors: preview?.cors ?? server.cors,
  62485. headers: preview?.headers ?? server.headers,
  62486. };
  62487. }
  62488. /**
  62489. * Starts the Vite server in preview mode, to simulate a production deployment
  62490. */
  62491. async function preview(inlineConfig = {}) {
  62492. const config = await resolveConfig(inlineConfig, 'serve', 'production', 'production', true);
  62493. const distDir = path$o.resolve(config.root, config.build.outDir);
  62494. if (!fs$l.existsSync(distDir) &&
  62495. // error if no plugins implement `configurePreviewServer`
  62496. config.plugins.every((plugin) => !plugin.configurePreviewServer) &&
  62497. // error if called in CLI only. programmatic usage could access `httpServer`
  62498. // and affect file serving
  62499. process.argv[1]?.endsWith(path$o.normalize('bin/vite.js')) &&
  62500. process.argv[2] === 'preview') {
  62501. throw new Error(`The directory "${config.build.outDir}" does not exist. Did you build your project?`);
  62502. }
  62503. const app = connect$1();
  62504. const httpServer = await resolveHttpServer(config.preview, app, await resolveHttpsConfig(config.preview?.https));
  62505. setClientErrorHandler(httpServer, config.logger);
  62506. const options = config.preview;
  62507. const logger = config.logger;
  62508. const server = {
  62509. config,
  62510. middlewares: app,
  62511. httpServer,
  62512. close: createServerCloseFn(httpServer),
  62513. resolvedUrls: null,
  62514. printUrls() {
  62515. if (server.resolvedUrls) {
  62516. printServerUrls(server.resolvedUrls, options.host, logger.info);
  62517. }
  62518. else {
  62519. throw new Error('cannot print server URLs before server is listening.');
  62520. }
  62521. },
  62522. bindCLIShortcuts(options) {
  62523. bindCLIShortcuts(server, options);
  62524. },
  62525. };
  62526. // apply server hooks from plugins
  62527. const postHooks = [];
  62528. for (const hook of config.getSortedPluginHooks('configurePreviewServer')) {
  62529. postHooks.push(await hook(server));
  62530. }
  62531. // cors
  62532. const { cors } = config.preview;
  62533. if (cors !== false) {
  62534. app.use(corsMiddleware(typeof cors === 'boolean' ? {} : cors));
  62535. }
  62536. // proxy
  62537. const { proxy } = config.preview;
  62538. if (proxy) {
  62539. app.use(proxyMiddleware(httpServer, proxy, config));
  62540. }
  62541. app.use(compression());
  62542. // base
  62543. if (config.base !== '/') {
  62544. app.use(baseMiddleware(config.rawBase, false));
  62545. }
  62546. // static assets
  62547. const headers = config.preview.headers;
  62548. const viteAssetMiddleware = (...args) => sirv(distDir, {
  62549. etag: true,
  62550. dev: true,
  62551. extensions: [],
  62552. ignores: false,
  62553. setHeaders(res) {
  62554. if (headers) {
  62555. for (const name in headers) {
  62556. res.setHeader(name, headers[name]);
  62557. }
  62558. }
  62559. },
  62560. shouldServe(filePath) {
  62561. return shouldServeFile(filePath, distDir);
  62562. },
  62563. })(...args);
  62564. app.use(viteAssetMiddleware);
  62565. // html fallback
  62566. if (config.appType === 'spa' || config.appType === 'mpa') {
  62567. app.use(htmlFallbackMiddleware(distDir, config.appType === 'spa'));
  62568. }
  62569. // apply post server hooks from plugins
  62570. postHooks.forEach((fn) => fn && fn());
  62571. if (config.appType === 'spa' || config.appType === 'mpa') {
  62572. // transform index.html
  62573. app.use(indexHtmlMiddleware(distDir, server));
  62574. // handle 404s
  62575. app.use(notFoundMiddleware());
  62576. }
  62577. const hostname = await resolveHostname(options.host);
  62578. const port = options.port ?? DEFAULT_PREVIEW_PORT;
  62579. await httpServerStart(httpServer, {
  62580. port,
  62581. strictPort: options.strictPort,
  62582. host: hostname.host,
  62583. logger,
  62584. });
  62585. server.resolvedUrls = await resolveServerUrls(httpServer, config.preview, config);
  62586. if (options.open) {
  62587. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  62588. if (url) {
  62589. const path = typeof options.open === 'string' ? new URL(options.open, url).href : url;
  62590. openBrowser(path, true, logger);
  62591. }
  62592. }
  62593. return server;
  62594. }
  62595. var preview$1 = {
  62596. __proto__: null,
  62597. preview: preview,
  62598. resolvePreviewOptions: resolvePreviewOptions
  62599. };
  62600. function resolveSSROptions(ssr, preserveSymlinks) {
  62601. ssr ??= {};
  62602. const optimizeDeps = ssr.optimizeDeps ?? {};
  62603. const target = 'node';
  62604. return {
  62605. target,
  62606. ...ssr,
  62607. optimizeDeps: {
  62608. ...optimizeDeps,
  62609. noDiscovery: true,
  62610. esbuildOptions: {
  62611. preserveSymlinks,
  62612. ...optimizeDeps.esbuildOptions,
  62613. },
  62614. },
  62615. };
  62616. }
  62617. const debug = createDebugger('vite:config');
  62618. const promisifiedRealpath = promisify$4(fs$l.realpath);
  62619. function defineConfig(config) {
  62620. return config;
  62621. }
  62622. /**
  62623. * Check and warn if `path` includes characters that don't work well in Vite,
  62624. * such as `#` and `?`.
  62625. */
  62626. function checkBadCharactersInPath(path, logger) {
  62627. const badChars = [];
  62628. if (path.includes('#')) {
  62629. badChars.push('#');
  62630. }
  62631. if (path.includes('?')) {
  62632. badChars.push('?');
  62633. }
  62634. if (badChars.length > 0) {
  62635. const charString = badChars.map((c) => `"${c}"`).join(' and ');
  62636. const inflectedChars = badChars.length > 1 ? 'characters' : 'character';
  62637. logger.warn(colors$1.yellow(`The project root contains the ${charString} ${inflectedChars} (${colors$1.cyan(path)}), which may not work when running Vite. Consider renaming the directory to remove the characters.`));
  62638. }
  62639. }
  62640. async function resolveConfig(inlineConfig, command, defaultMode = 'development', defaultNodeEnv = 'development', isPreview = false) {
  62641. let config = inlineConfig;
  62642. let configFileDependencies = [];
  62643. let mode = inlineConfig.mode || defaultMode;
  62644. const isNodeEnvSet = !!process.env.NODE_ENV;
  62645. const packageCache = new Map();
  62646. // some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
  62647. // production-specific behavior, so set it early on
  62648. if (!isNodeEnvSet) {
  62649. process.env.NODE_ENV = defaultNodeEnv;
  62650. }
  62651. const configEnv = {
  62652. mode,
  62653. command,
  62654. isSsrBuild: command === 'build' && !!config.build?.ssr,
  62655. isPreview,
  62656. };
  62657. let { configFile } = config;
  62658. if (configFile !== false) {
  62659. const loadResult = await loadConfigFromFile(configEnv, configFile, config.root, config.logLevel, config.customLogger);
  62660. if (loadResult) {
  62661. config = mergeConfig(loadResult.config, config);
  62662. configFile = loadResult.path;
  62663. configFileDependencies = loadResult.dependencies;
  62664. }
  62665. }
  62666. // user config may provide an alternative mode. But --mode has a higher priority
  62667. mode = inlineConfig.mode || config.mode || mode;
  62668. configEnv.mode = mode;
  62669. const filterPlugin = (p) => {
  62670. if (!p) {
  62671. return false;
  62672. }
  62673. else if (!p.apply) {
  62674. return true;
  62675. }
  62676. else if (typeof p.apply === 'function') {
  62677. return p.apply({ ...config, mode }, configEnv);
  62678. }
  62679. else {
  62680. return p.apply === command;
  62681. }
  62682. };
  62683. // resolve plugins
  62684. const rawUserPlugins = (await asyncFlatten(config.plugins || [])).filter(filterPlugin);
  62685. const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawUserPlugins);
  62686. // run config hooks
  62687. const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins];
  62688. config = await runConfigHook(config, userPlugins, configEnv);
  62689. // Define logger
  62690. const logger = createLogger(config.logLevel, {
  62691. allowClearScreen: config.clearScreen,
  62692. customLogger: config.customLogger,
  62693. });
  62694. // resolve root
  62695. const resolvedRoot = normalizePath$3(config.root ? path$o.resolve(config.root) : process.cwd());
  62696. checkBadCharactersInPath(resolvedRoot, logger);
  62697. const clientAlias = [
  62698. {
  62699. find: /^\/?@vite\/env/,
  62700. replacement: path$o.posix.join(FS_PREFIX, normalizePath$3(ENV_ENTRY)),
  62701. },
  62702. {
  62703. find: /^\/?@vite\/client/,
  62704. replacement: path$o.posix.join(FS_PREFIX, normalizePath$3(CLIENT_ENTRY)),
  62705. },
  62706. ];
  62707. // resolve alias with internal client alias
  62708. const resolvedAlias = normalizeAlias(mergeAlias(clientAlias, config.resolve?.alias || []));
  62709. const resolveOptions = {
  62710. mainFields: config.resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
  62711. conditions: config.resolve?.conditions ?? [],
  62712. extensions: config.resolve?.extensions ?? DEFAULT_EXTENSIONS,
  62713. dedupe: config.resolve?.dedupe ?? [],
  62714. preserveSymlinks: config.resolve?.preserveSymlinks ?? false,
  62715. alias: resolvedAlias,
  62716. };
  62717. if (
  62718. // @ts-expect-error removed field
  62719. config.resolve?.browserField === false &&
  62720. resolveOptions.mainFields.includes('browser')) {
  62721. logger.warn(colors$1.yellow(`\`resolve.browserField\` is set to false, but the option is removed in favour of ` +
  62722. `the 'browser' string in \`resolve.mainFields\`. You may want to update \`resolve.mainFields\` ` +
  62723. `to remove the 'browser' string and preserve the previous browser behaviour.`));
  62724. }
  62725. // load .env files
  62726. const envDir = config.envDir
  62727. ? normalizePath$3(path$o.resolve(resolvedRoot, config.envDir))
  62728. : resolvedRoot;
  62729. const userEnv = inlineConfig.envFile !== false &&
  62730. loadEnv(mode, envDir, resolveEnvPrefix(config));
  62731. // Note it is possible for user to have a custom mode, e.g. `staging` where
  62732. // development-like behavior is expected. This is indicated by NODE_ENV=development
  62733. // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
  62734. const userNodeEnv = process.env.VITE_USER_NODE_ENV;
  62735. if (!isNodeEnvSet && userNodeEnv) {
  62736. if (userNodeEnv === 'development') {
  62737. process.env.NODE_ENV = 'development';
  62738. }
  62739. else {
  62740. // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
  62741. logger.warn(`NODE_ENV=${userNodeEnv} is not supported in the .env file. ` +
  62742. `Only NODE_ENV=development is supported to create a development build of your project. ` +
  62743. `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`);
  62744. }
  62745. }
  62746. const isProduction = process.env.NODE_ENV === 'production';
  62747. // resolve public base url
  62748. const isBuild = command === 'build';
  62749. const relativeBaseShortcut = config.base === '' || config.base === './';
  62750. // During dev, we ignore relative base and fallback to '/'
  62751. // For the SSR build, relative base isn't possible by means
  62752. // of import.meta.url.
  62753. const resolvedBase = relativeBaseShortcut
  62754. ? !isBuild || config.build?.ssr
  62755. ? '/'
  62756. : './'
  62757. : resolveBaseUrl(config.base, isBuild, logger) ?? '/';
  62758. const resolvedBuildOptions = resolveBuildOptions(config.build, logger, resolvedRoot);
  62759. // resolve cache directory
  62760. const pkgDir = findNearestPackageData(resolvedRoot, packageCache)?.dir;
  62761. const cacheDir = normalizePath$3(config.cacheDir
  62762. ? path$o.resolve(resolvedRoot, config.cacheDir)
  62763. : pkgDir
  62764. ? path$o.join(pkgDir, `node_modules/.vite`)
  62765. : path$o.join(resolvedRoot, `.vite`));
  62766. const assetsFilter = config.assetsInclude &&
  62767. (!Array.isArray(config.assetsInclude) || config.assetsInclude.length)
  62768. ? createFilter(config.assetsInclude)
  62769. : () => false;
  62770. // create an internal resolver to be used in special scenarios, e.g.
  62771. // optimizer & handling css @imports
  62772. const createResolver = (options) => {
  62773. let aliasContainer;
  62774. let resolverContainer;
  62775. return async (id, importer, aliasOnly, ssr) => {
  62776. let container;
  62777. if (aliasOnly) {
  62778. container =
  62779. aliasContainer ||
  62780. (aliasContainer = await createPluginContainer({
  62781. ...resolved,
  62782. plugins: [alias$1({ entries: resolved.resolve.alias })],
  62783. }));
  62784. }
  62785. else {
  62786. container =
  62787. resolverContainer ||
  62788. (resolverContainer = await createPluginContainer({
  62789. ...resolved,
  62790. plugins: [
  62791. alias$1({ entries: resolved.resolve.alias }),
  62792. resolvePlugin({
  62793. ...resolved.resolve,
  62794. root: resolvedRoot,
  62795. isProduction,
  62796. isBuild: command === 'build',
  62797. ssrConfig: resolved.ssr,
  62798. asSrc: true,
  62799. preferRelative: false,
  62800. tryIndex: true,
  62801. ...options,
  62802. idOnly: true,
  62803. fsUtils: getFsUtils(resolved),
  62804. }),
  62805. ],
  62806. }));
  62807. }
  62808. return (await container.resolveId(id, importer, {
  62809. ssr,
  62810. scan: options?.scan,
  62811. }))?.id;
  62812. };
  62813. };
  62814. const { publicDir } = config;
  62815. const resolvedPublicDir = publicDir !== false && publicDir !== ''
  62816. ? normalizePath$3(path$o.resolve(resolvedRoot, typeof publicDir === 'string' ? publicDir : 'public'))
  62817. : '';
  62818. const server = resolveServerOptions(resolvedRoot, config.server, logger);
  62819. const ssr = resolveSSROptions(config.ssr, resolveOptions.preserveSymlinks);
  62820. const optimizeDeps = config.optimizeDeps || {};
  62821. const BASE_URL = resolvedBase;
  62822. let resolved;
  62823. let createUserWorkerPlugins = config.worker?.plugins;
  62824. if (Array.isArray(createUserWorkerPlugins)) {
  62825. // @ts-expect-error backward compatibility
  62826. createUserWorkerPlugins = () => config.worker?.plugins;
  62827. logger.warn(colors$1.yellow(`worker.plugins is now a function that returns an array of plugins. ` +
  62828. `Please update your Vite config accordingly.\n`));
  62829. }
  62830. const createWorkerPlugins = async function (bundleChain) {
  62831. // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
  62832. // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
  62833. // So we need to separate the worker plugin from the plugin that vite needs to run.
  62834. const rawWorkerUserPlugins = (await asyncFlatten(createUserWorkerPlugins?.() || [])).filter(filterPlugin);
  62835. // resolve worker
  62836. let workerConfig = mergeConfig({}, config);
  62837. const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = sortUserPlugins(rawWorkerUserPlugins);
  62838. // run config hooks
  62839. const workerUserPlugins = [
  62840. ...workerPrePlugins,
  62841. ...workerNormalPlugins,
  62842. ...workerPostPlugins,
  62843. ];
  62844. workerConfig = await runConfigHook(workerConfig, workerUserPlugins, configEnv);
  62845. const workerResolved = {
  62846. ...workerConfig,
  62847. ...resolved,
  62848. isWorker: true,
  62849. mainConfig: resolved,
  62850. bundleChain,
  62851. };
  62852. const resolvedWorkerPlugins = await resolvePlugins(workerResolved, workerPrePlugins, workerNormalPlugins, workerPostPlugins);
  62853. // run configResolved hooks
  62854. await Promise.all(createPluginHookUtils(resolvedWorkerPlugins)
  62855. .getSortedPluginHooks('configResolved')
  62856. .map((hook) => hook(workerResolved)));
  62857. return resolvedWorkerPlugins;
  62858. };
  62859. const resolvedWorkerOptions = {
  62860. format: config.worker?.format || 'iife',
  62861. plugins: createWorkerPlugins,
  62862. rollupOptions: config.worker?.rollupOptions || {},
  62863. };
  62864. resolved = {
  62865. configFile: configFile ? normalizePath$3(configFile) : undefined,
  62866. configFileDependencies: configFileDependencies.map((name) => normalizePath$3(path$o.resolve(name))),
  62867. inlineConfig,
  62868. root: resolvedRoot,
  62869. base: withTrailingSlash(resolvedBase),
  62870. rawBase: resolvedBase,
  62871. resolve: resolveOptions,
  62872. publicDir: resolvedPublicDir,
  62873. cacheDir,
  62874. command,
  62875. mode,
  62876. ssr,
  62877. isWorker: false,
  62878. mainConfig: null,
  62879. bundleChain: [],
  62880. isProduction,
  62881. plugins: userPlugins,
  62882. css: resolveCSSOptions(config.css),
  62883. esbuild: config.esbuild === false
  62884. ? false
  62885. : {
  62886. jsxDev: !isProduction,
  62887. ...config.esbuild,
  62888. },
  62889. server,
  62890. build: resolvedBuildOptions,
  62891. preview: resolvePreviewOptions(config.preview, server),
  62892. envDir,
  62893. env: {
  62894. ...userEnv,
  62895. BASE_URL,
  62896. MODE: mode,
  62897. DEV: !isProduction,
  62898. PROD: isProduction,
  62899. },
  62900. assetsInclude(file) {
  62901. return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file);
  62902. },
  62903. logger,
  62904. packageCache,
  62905. createResolver,
  62906. optimizeDeps: {
  62907. holdUntilCrawlEnd: true,
  62908. ...optimizeDeps,
  62909. esbuildOptions: {
  62910. preserveSymlinks: resolveOptions.preserveSymlinks,
  62911. ...optimizeDeps.esbuildOptions,
  62912. },
  62913. },
  62914. worker: resolvedWorkerOptions,
  62915. appType: config.appType ?? 'spa',
  62916. experimental: {
  62917. importGlobRestoreExtension: false,
  62918. hmrPartialAccept: false,
  62919. ...config.experimental,
  62920. },
  62921. getSortedPlugins: undefined,
  62922. getSortedPluginHooks: undefined,
  62923. };
  62924. resolved = {
  62925. ...config,
  62926. ...resolved,
  62927. };
  62928. resolved.plugins = await resolvePlugins(resolved, prePlugins, normalPlugins, postPlugins);
  62929. Object.assign(resolved, createPluginHookUtils(resolved.plugins));
  62930. // call configResolved hooks
  62931. await Promise.all(resolved
  62932. .getSortedPluginHooks('configResolved')
  62933. .map((hook) => hook(resolved)));
  62934. optimizeDepsDisabledBackwardCompatibility(resolved, resolved.optimizeDeps);
  62935. optimizeDepsDisabledBackwardCompatibility(resolved, resolved.ssr.optimizeDeps, 'ssr.');
  62936. debug?.(`using resolved config: %O`, {
  62937. ...resolved,
  62938. plugins: resolved.plugins.map((p) => p.name),
  62939. worker: {
  62940. ...resolved.worker,
  62941. plugins: `() => plugins`,
  62942. },
  62943. });
  62944. // validate config
  62945. if (config.build?.terserOptions &&
  62946. config.build.minify &&
  62947. config.build.minify !== 'terser') {
  62948. logger.warn(colors$1.yellow(`build.terserOptions is specified but build.minify is not set to use Terser. ` +
  62949. `Note Vite now defaults to use esbuild for minification. If you still ` +
  62950. `prefer Terser, set build.minify to "terser".`));
  62951. }
  62952. // Check if all assetFileNames have the same reference.
  62953. // If not, display a warn for user.
  62954. const outputOption = config.build?.rollupOptions?.output ?? [];
  62955. // Use isArray to narrow its type to array
  62956. if (Array.isArray(outputOption)) {
  62957. const assetFileNamesList = outputOption.map((output) => output.assetFileNames);
  62958. if (assetFileNamesList.length > 1) {
  62959. const firstAssetFileNames = assetFileNamesList[0];
  62960. const hasDifferentReference = assetFileNamesList.some((assetFileNames) => assetFileNames !== firstAssetFileNames);
  62961. if (hasDifferentReference) {
  62962. resolved.logger.warn(colors$1.yellow(`
  62963. assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
  62964. `));
  62965. }
  62966. }
  62967. }
  62968. // Warn about removal of experimental features
  62969. if (
  62970. // @ts-expect-error Option removed
  62971. config.legacy?.buildSsrCjsExternalHeuristics ||
  62972. // @ts-expect-error Option removed
  62973. config.ssr?.format === 'cjs') {
  62974. resolved.logger.warn(colors$1.yellow(`
  62975. (!) Experimental legacy.buildSsrCjsExternalHeuristics and ssr.format were be removed in Vite 5.
  62976. The only SSR Output format is ESM. Find more information at https://github.com/vitejs/vite/discussions/13816.
  62977. `));
  62978. }
  62979. const resolvedBuildOutDir = normalizePath$3(path$o.resolve(resolved.root, resolved.build.outDir));
  62980. if (isParentDirectory(resolvedBuildOutDir, resolved.root) ||
  62981. resolvedBuildOutDir === resolved.root) {
  62982. resolved.logger.warn(colors$1.yellow(`
  62983. (!) build.outDir must not be the same directory of root or a parent directory of root as this could cause Vite to overwriting source files with build outputs.
  62984. `));
  62985. }
  62986. return resolved;
  62987. }
  62988. /**
  62989. * Resolve base url. Note that some users use Vite to build for non-web targets like
  62990. * electron or expects to deploy
  62991. */
  62992. function resolveBaseUrl(base = '/', isBuild, logger) {
  62993. if (base[0] === '.') {
  62994. logger.warn(colors$1.yellow(colors$1.bold(`(!) invalid "base" option: "${base}". The value can only be an absolute ` +
  62995. `URL, "./", or an empty string.`)));
  62996. return '/';
  62997. }
  62998. // external URL flag
  62999. const isExternal = isExternalUrl(base);
  63000. // no leading slash warn
  63001. if (!isExternal && base[0] !== '/') {
  63002. logger.warn(colors$1.yellow(colors$1.bold(`(!) "base" option should start with a slash.`)));
  63003. }
  63004. // parse base when command is serve or base is not External URL
  63005. if (!isBuild || !isExternal) {
  63006. base = new URL(base, 'http://vitejs.dev').pathname;
  63007. // ensure leading slash
  63008. if (base[0] !== '/') {
  63009. base = '/' + base;
  63010. }
  63011. }
  63012. return base;
  63013. }
  63014. function sortUserPlugins(plugins) {
  63015. const prePlugins = [];
  63016. const postPlugins = [];
  63017. const normalPlugins = [];
  63018. if (plugins) {
  63019. plugins.flat().forEach((p) => {
  63020. if (p.enforce === 'pre')
  63021. prePlugins.push(p);
  63022. else if (p.enforce === 'post')
  63023. postPlugins.push(p);
  63024. else
  63025. normalPlugins.push(p);
  63026. });
  63027. }
  63028. return [prePlugins, normalPlugins, postPlugins];
  63029. }
  63030. async function loadConfigFromFile(configEnv, configFile, configRoot = process.cwd(), logLevel, customLogger) {
  63031. const start = performance.now();
  63032. const getTime = () => `${(performance.now() - start).toFixed(2)}ms`;
  63033. let resolvedPath;
  63034. if (configFile) {
  63035. // explicit config path is always resolved from cwd
  63036. resolvedPath = path$o.resolve(configFile);
  63037. }
  63038. else {
  63039. // implicit config file loaded from inline root (if present)
  63040. // otherwise from cwd
  63041. for (const filename of DEFAULT_CONFIG_FILES) {
  63042. const filePath = path$o.resolve(configRoot, filename);
  63043. if (!fs$l.existsSync(filePath))
  63044. continue;
  63045. resolvedPath = filePath;
  63046. break;
  63047. }
  63048. }
  63049. if (!resolvedPath) {
  63050. debug?.('no config file found.');
  63051. return null;
  63052. }
  63053. const isESM = isFilePathESM(resolvedPath);
  63054. try {
  63055. const bundled = await bundleConfigFile(resolvedPath, isESM);
  63056. const userConfig = await loadConfigFromBundledFile(resolvedPath, bundled.code, isESM);
  63057. debug?.(`bundled config file loaded in ${getTime()}`);
  63058. const config = await (typeof userConfig === 'function'
  63059. ? userConfig(configEnv)
  63060. : userConfig);
  63061. if (!isObject$1(config)) {
  63062. throw new Error(`config must export or return an object.`);
  63063. }
  63064. return {
  63065. path: normalizePath$3(resolvedPath),
  63066. config,
  63067. dependencies: bundled.dependencies,
  63068. };
  63069. }
  63070. catch (e) {
  63071. createLogger(logLevel, { customLogger }).error(colors$1.red(`failed to load config from ${resolvedPath}`), {
  63072. error: e,
  63073. });
  63074. throw e;
  63075. }
  63076. }
  63077. async function bundleConfigFile(fileName, isESM) {
  63078. const dirnameVarName = '__vite_injected_original_dirname';
  63079. const filenameVarName = '__vite_injected_original_filename';
  63080. const importMetaUrlVarName = '__vite_injected_original_import_meta_url';
  63081. const result = await build$3({
  63082. absWorkingDir: process.cwd(),
  63083. entryPoints: [fileName],
  63084. write: false,
  63085. target: ['node18'],
  63086. platform: 'node',
  63087. bundle: true,
  63088. format: isESM ? 'esm' : 'cjs',
  63089. mainFields: ['main'],
  63090. sourcemap: 'inline',
  63091. metafile: true,
  63092. define: {
  63093. __dirname: dirnameVarName,
  63094. __filename: filenameVarName,
  63095. 'import.meta.url': importMetaUrlVarName,
  63096. 'import.meta.dirname': dirnameVarName,
  63097. 'import.meta.filename': filenameVarName,
  63098. },
  63099. plugins: [
  63100. {
  63101. name: 'externalize-deps',
  63102. setup(build) {
  63103. const packageCache = new Map();
  63104. const resolveByViteResolver = (id, importer, isRequire) => {
  63105. return tryNodeResolve(id, importer, {
  63106. root: path$o.dirname(fileName),
  63107. isBuild: true,
  63108. isProduction: true,
  63109. preferRelative: false,
  63110. tryIndex: true,
  63111. mainFields: [],
  63112. conditions: [],
  63113. overrideConditions: ['node'],
  63114. dedupe: [],
  63115. extensions: DEFAULT_EXTENSIONS,
  63116. preserveSymlinks: false,
  63117. packageCache,
  63118. isRequire,
  63119. }, false)?.id;
  63120. };
  63121. // externalize bare imports
  63122. build.onResolve({ filter: /^[^.].*/ }, async ({ path: id, importer, kind }) => {
  63123. if (kind === 'entry-point' ||
  63124. path$o.isAbsolute(id) ||
  63125. isNodeBuiltin(id)) {
  63126. return;
  63127. }
  63128. // With the `isNodeBuiltin` check above, this check captures if the builtin is a
  63129. // non-node built-in, which esbuild doesn't know how to handle. In that case, we
  63130. // externalize it so the non-node runtime handles it instead.
  63131. if (isBuiltin(id)) {
  63132. return { external: true };
  63133. }
  63134. const isImport = isESM || kind === 'dynamic-import';
  63135. let idFsPath;
  63136. try {
  63137. idFsPath = resolveByViteResolver(id, importer, !isImport);
  63138. }
  63139. catch (e) {
  63140. if (!isImport) {
  63141. let canResolveWithImport = false;
  63142. try {
  63143. canResolveWithImport = !!resolveByViteResolver(id, importer, false);
  63144. }
  63145. catch { }
  63146. if (canResolveWithImport) {
  63147. throw new Error(`Failed to resolve ${JSON.stringify(id)}. This package is ESM only but it was tried to load by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`);
  63148. }
  63149. }
  63150. throw e;
  63151. }
  63152. if (idFsPath && isImport) {
  63153. idFsPath = pathToFileURL(idFsPath).href;
  63154. }
  63155. if (idFsPath &&
  63156. !isImport &&
  63157. isFilePathESM(idFsPath, packageCache)) {
  63158. throw new Error(`${JSON.stringify(id)} resolved to an ESM file. ESM file cannot be loaded by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`);
  63159. }
  63160. return {
  63161. path: idFsPath,
  63162. external: true,
  63163. };
  63164. });
  63165. },
  63166. },
  63167. {
  63168. name: 'inject-file-scope-variables',
  63169. setup(build) {
  63170. build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
  63171. const contents = await fsp.readFile(args.path, 'utf-8');
  63172. const injectValues = `const ${dirnameVarName} = ${JSON.stringify(path$o.dirname(args.path))};` +
  63173. `const ${filenameVarName} = ${JSON.stringify(args.path)};` +
  63174. `const ${importMetaUrlVarName} = ${JSON.stringify(pathToFileURL(args.path).href)};`;
  63175. return {
  63176. loader: args.path.endsWith('ts') ? 'ts' : 'js',
  63177. contents: injectValues + contents,
  63178. };
  63179. });
  63180. },
  63181. },
  63182. ],
  63183. });
  63184. const { text } = result.outputFiles[0];
  63185. return {
  63186. code: text,
  63187. dependencies: result.metafile ? Object.keys(result.metafile.inputs) : [],
  63188. };
  63189. }
  63190. const _require = createRequire$1(import.meta.url);
  63191. async function loadConfigFromBundledFile(fileName, bundledCode, isESM) {
  63192. // for esm, before we can register loaders without requiring users to run node
  63193. // with --experimental-loader themselves, we have to do a hack here:
  63194. // write it to disk, load it with native Node ESM, then delete the file.
  63195. if (isESM) {
  63196. const fileBase = `${fileName}.timestamp-${Date.now()}-${Math.random()
  63197. .toString(16)
  63198. .slice(2)}`;
  63199. const fileNameTmp = `${fileBase}.mjs`;
  63200. const fileUrl = `${pathToFileURL(fileBase)}.mjs`;
  63201. await fsp.writeFile(fileNameTmp, bundledCode);
  63202. try {
  63203. return (await import(fileUrl)).default;
  63204. }
  63205. finally {
  63206. fs$l.unlink(fileNameTmp, () => { }); // Ignore errors
  63207. }
  63208. }
  63209. // for cjs, we can register a custom loader via `_require.extensions`
  63210. else {
  63211. const extension = path$o.extname(fileName);
  63212. // We don't use fsp.realpath() here because it has the same behaviour as
  63213. // fs.realpath.native. On some Windows systems, it returns uppercase volume
  63214. // letters (e.g. "C:\") while the Node.js loader uses lowercase volume letters.
  63215. // See https://github.com/vitejs/vite/issues/12923
  63216. const realFileName = await promisifiedRealpath(fileName);
  63217. const loaderExt = extension in _require.extensions ? extension : '.js';
  63218. const defaultLoader = _require.extensions[loaderExt];
  63219. _require.extensions[loaderExt] = (module, filename) => {
  63220. if (filename === realFileName) {
  63221. module._compile(bundledCode, filename);
  63222. }
  63223. else {
  63224. defaultLoader(module, filename);
  63225. }
  63226. };
  63227. // clear cache in case of server restart
  63228. delete _require.cache[_require.resolve(fileName)];
  63229. const raw = _require(fileName);
  63230. _require.extensions[loaderExt] = defaultLoader;
  63231. return raw.__esModule ? raw.default : raw;
  63232. }
  63233. }
  63234. async function runConfigHook(config, plugins, configEnv) {
  63235. let conf = config;
  63236. for (const p of getSortedPluginsByHook('config', plugins)) {
  63237. const hook = p.config;
  63238. const handler = getHookHandler(hook);
  63239. if (handler) {
  63240. const res = await handler(conf, configEnv);
  63241. if (res) {
  63242. conf = mergeConfig(conf, res);
  63243. }
  63244. }
  63245. }
  63246. return conf;
  63247. }
  63248. function getDepOptimizationConfig(config, ssr) {
  63249. return ssr ? config.ssr.optimizeDeps : config.optimizeDeps;
  63250. }
  63251. function isDepsOptimizerEnabled(config, ssr) {
  63252. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  63253. return !(optimizeDeps.noDiscovery && !optimizeDeps.include?.length);
  63254. }
  63255. function optimizeDepsDisabledBackwardCompatibility(resolved, optimizeDeps, optimizeDepsPath = '') {
  63256. const optimizeDepsDisabled = optimizeDeps.disabled;
  63257. if (optimizeDepsDisabled !== undefined) {
  63258. if (optimizeDepsDisabled === true || optimizeDepsDisabled === 'dev') {
  63259. const commonjsOptionsInclude = resolved.build?.commonjsOptions?.include;
  63260. const commonjsPluginDisabled = Array.isArray(commonjsOptionsInclude) &&
  63261. commonjsOptionsInclude.length === 0;
  63262. optimizeDeps.noDiscovery = true;
  63263. optimizeDeps.include = undefined;
  63264. if (commonjsPluginDisabled) {
  63265. resolved.build.commonjsOptions.include = undefined;
  63266. }
  63267. resolved.logger.warn(colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  63268. To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
  63269. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  63270. ${commonjsPluginDisabled
  63271. ? 'Empty config.build.commonjsOptions.include will be ignored to support CJS during build. This config should also be removed.'
  63272. : ''}
  63273. `));
  63274. }
  63275. else if (optimizeDepsDisabled === false ||
  63276. optimizeDepsDisabled === 'build') {
  63277. resolved.logger.warn(colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  63278. Setting it to ${optimizeDepsDisabled} now has no effect.
  63279. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  63280. `));
  63281. }
  63282. }
  63283. }
  63284. export { colors$1 as A, getDefaultExportFromCjs as B, commonjsGlobal as C, index$1 as D, index as E, build$1 as F, preview$1 as G, arraify as a, build as b, createServer as c, defineConfig as d, preprocessCSS as e, formatPostcssSourceMap as f, buildErrorMessage as g, fetchModule as h, isInNodeModules$1 as i, mergeAlias as j, createFilter as k, loadConfigFromFile as l, mergeConfig as m, normalizePath$3 as n, optimizeDeps as o, preview as p, rollupVersion as q, resolveConfig as r, sortUserPlugins as s, transformWithEsbuild as t, send as u, createLogger as v, searchForWorkspaceRoot as w, isFileServingAllowed as x, loadEnv as y, resolveEnvPrefix as z };