compiler-core.cjs.prod.js 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var shared = require('@vue/shared');
  4. var parser = require('@babel/parser');
  5. var sourceMapJs = require('source-map-js');
  6. var estreeWalker = require('estree-walker');
  7. function defaultOnError(error) {
  8. throw error;
  9. }
  10. function defaultOnWarn(msg) {
  11. }
  12. function createCompilerError(code, loc, messages, additionalMessage) {
  13. const msg = (messages || errorMessages)[code] + (additionalMessage || ``) ;
  14. const error = new SyntaxError(String(msg));
  15. error.code = code;
  16. error.loc = loc;
  17. return error;
  18. }
  19. const errorMessages = {
  20. // parse errors
  21. [0]: "Illegal comment.",
  22. [1]: "CDATA section is allowed only in XML context.",
  23. [2]: "Duplicate attribute.",
  24. [3]: "End tag cannot have attributes.",
  25. [4]: "Illegal '/' in tags.",
  26. [5]: "Unexpected EOF in tag.",
  27. [6]: "Unexpected EOF in CDATA section.",
  28. [7]: "Unexpected EOF in comment.",
  29. [8]: "Unexpected EOF in script.",
  30. [9]: "Unexpected EOF in tag.",
  31. [10]: "Incorrectly closed comment.",
  32. [11]: "Incorrectly opened comment.",
  33. [12]: "Illegal tag name. Use '&lt;' to print '<'.",
  34. [13]: "Attribute value was expected.",
  35. [14]: "End tag name was expected.",
  36. [15]: "Whitespace was expected.",
  37. [16]: "Unexpected '<!--' in comment.",
  38. [17]: `Attribute name cannot contain U+0022 ("), U+0027 ('), and U+003C (<).`,
  39. [18]: "Unquoted attribute value cannot contain U+0022 (\"), U+0027 ('), U+003C (<), U+003D (=), and U+0060 (`).",
  40. [19]: "Attribute name cannot start with '='.",
  41. [21]: "'<?' is allowed only in XML context.",
  42. [20]: `Unexpected null character.`,
  43. [22]: "Illegal '/' in tags.",
  44. // Vue-specific parse errors
  45. [23]: "Invalid end tag.",
  46. [24]: "Element is missing end tag.",
  47. [25]: "Interpolation end sign was not found.",
  48. [27]: "End bracket for dynamic directive argument was not found. Note that dynamic directive argument cannot contain spaces.",
  49. [26]: "Legal directive name was expected.",
  50. // transform errors
  51. [28]: `v-if/v-else-if is missing expression.`,
  52. [29]: `v-if/else branches must use unique keys.`,
  53. [30]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
  54. [31]: `v-for is missing expression.`,
  55. [32]: `v-for has invalid expression.`,
  56. [33]: `<template v-for> key should be placed on the <template> tag.`,
  57. [34]: `v-bind is missing expression.`,
  58. [35]: `v-on is missing expression.`,
  59. [36]: `Unexpected custom directive on <slot> outlet.`,
  60. [37]: `Mixed v-slot usage on both the component and nested <template>. When there are multiple named slots, all slots should use <template> syntax to avoid scope ambiguity.`,
  61. [38]: `Duplicate slot names found. `,
  62. [39]: `Extraneous children found when component already has explicitly named default slot. These children will be ignored.`,
  63. [40]: `v-slot can only be used on components or <template> tags.`,
  64. [41]: `v-model is missing expression.`,
  65. [42]: `v-model value must be a valid JavaScript member expression.`,
  66. [43]: `v-model cannot be used on v-for or v-slot scope variables because they are not writable.`,
  67. [44]: `v-model cannot be used on a prop, because local prop bindings are not writable.
  68. Use a v-bind binding combined with a v-on listener that emits update:x event instead.`,
  69. [45]: `Error parsing JavaScript expression: `,
  70. [46]: `<KeepAlive> expects exactly one child component.`,
  71. // generic errors
  72. [47]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
  73. [48]: `ES module mode is not supported in this build of compiler.`,
  74. [49]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
  75. [50]: `"scopeId" option is only supported in module mode.`,
  76. // deprecations
  77. [51]: `@vnode-* hooks in templates are deprecated. Use the vue: prefix instead. For example, @vnode-mounted should be changed to @vue:mounted. @vnode-* hooks support will be removed in 3.4.`,
  78. [52]: `v-is="component-name" has been deprecated. Use is="vue:component-name" instead. v-is support will be removed in 3.4.`,
  79. // just to fulfill types
  80. [53]: ``
  81. };
  82. const FRAGMENT = Symbol(``);
  83. const TELEPORT = Symbol(``);
  84. const SUSPENSE = Symbol(``);
  85. const KEEP_ALIVE = Symbol(``);
  86. const BASE_TRANSITION = Symbol(``);
  87. const OPEN_BLOCK = Symbol(``);
  88. const CREATE_BLOCK = Symbol(``);
  89. const CREATE_ELEMENT_BLOCK = Symbol(``);
  90. const CREATE_VNODE = Symbol(``);
  91. const CREATE_ELEMENT_VNODE = Symbol(``);
  92. const CREATE_COMMENT = Symbol(``);
  93. const CREATE_TEXT = Symbol(``);
  94. const CREATE_STATIC = Symbol(``);
  95. const RESOLVE_COMPONENT = Symbol(``);
  96. const RESOLVE_DYNAMIC_COMPONENT = Symbol(
  97. ``
  98. );
  99. const RESOLVE_DIRECTIVE = Symbol(``);
  100. const RESOLVE_FILTER = Symbol(``);
  101. const WITH_DIRECTIVES = Symbol(``);
  102. const RENDER_LIST = Symbol(``);
  103. const RENDER_SLOT = Symbol(``);
  104. const CREATE_SLOTS = Symbol(``);
  105. const TO_DISPLAY_STRING = Symbol(``);
  106. const MERGE_PROPS = Symbol(``);
  107. const NORMALIZE_CLASS = Symbol(``);
  108. const NORMALIZE_STYLE = Symbol(``);
  109. const NORMALIZE_PROPS = Symbol(``);
  110. const GUARD_REACTIVE_PROPS = Symbol(``);
  111. const TO_HANDLERS = Symbol(``);
  112. const CAMELIZE = Symbol(``);
  113. const CAPITALIZE = Symbol(``);
  114. const TO_HANDLER_KEY = Symbol(``);
  115. const SET_BLOCK_TRACKING = Symbol(``);
  116. const PUSH_SCOPE_ID = Symbol(``);
  117. const POP_SCOPE_ID = Symbol(``);
  118. const WITH_CTX = Symbol(``);
  119. const UNREF = Symbol(``);
  120. const IS_REF = Symbol(``);
  121. const WITH_MEMO = Symbol(``);
  122. const IS_MEMO_SAME = Symbol(``);
  123. const helperNameMap = {
  124. [FRAGMENT]: `Fragment`,
  125. [TELEPORT]: `Teleport`,
  126. [SUSPENSE]: `Suspense`,
  127. [KEEP_ALIVE]: `KeepAlive`,
  128. [BASE_TRANSITION]: `BaseTransition`,
  129. [OPEN_BLOCK]: `openBlock`,
  130. [CREATE_BLOCK]: `createBlock`,
  131. [CREATE_ELEMENT_BLOCK]: `createElementBlock`,
  132. [CREATE_VNODE]: `createVNode`,
  133. [CREATE_ELEMENT_VNODE]: `createElementVNode`,
  134. [CREATE_COMMENT]: `createCommentVNode`,
  135. [CREATE_TEXT]: `createTextVNode`,
  136. [CREATE_STATIC]: `createStaticVNode`,
  137. [RESOLVE_COMPONENT]: `resolveComponent`,
  138. [RESOLVE_DYNAMIC_COMPONENT]: `resolveDynamicComponent`,
  139. [RESOLVE_DIRECTIVE]: `resolveDirective`,
  140. [RESOLVE_FILTER]: `resolveFilter`,
  141. [WITH_DIRECTIVES]: `withDirectives`,
  142. [RENDER_LIST]: `renderList`,
  143. [RENDER_SLOT]: `renderSlot`,
  144. [CREATE_SLOTS]: `createSlots`,
  145. [TO_DISPLAY_STRING]: `toDisplayString`,
  146. [MERGE_PROPS]: `mergeProps`,
  147. [NORMALIZE_CLASS]: `normalizeClass`,
  148. [NORMALIZE_STYLE]: `normalizeStyle`,
  149. [NORMALIZE_PROPS]: `normalizeProps`,
  150. [GUARD_REACTIVE_PROPS]: `guardReactiveProps`,
  151. [TO_HANDLERS]: `toHandlers`,
  152. [CAMELIZE]: `camelize`,
  153. [CAPITALIZE]: `capitalize`,
  154. [TO_HANDLER_KEY]: `toHandlerKey`,
  155. [SET_BLOCK_TRACKING]: `setBlockTracking`,
  156. [PUSH_SCOPE_ID]: `pushScopeId`,
  157. [POP_SCOPE_ID]: `popScopeId`,
  158. [WITH_CTX]: `withCtx`,
  159. [UNREF]: `unref`,
  160. [IS_REF]: `isRef`,
  161. [WITH_MEMO]: `withMemo`,
  162. [IS_MEMO_SAME]: `isMemoSame`
  163. };
  164. function registerRuntimeHelpers(helpers) {
  165. Object.getOwnPropertySymbols(helpers).forEach((s) => {
  166. helperNameMap[s] = helpers[s];
  167. });
  168. }
  169. const locStub = {
  170. source: "",
  171. start: { line: 1, column: 1, offset: 0 },
  172. end: { line: 1, column: 1, offset: 0 }
  173. };
  174. function createRoot(children, loc = locStub) {
  175. return {
  176. type: 0,
  177. children,
  178. helpers: /* @__PURE__ */ new Set(),
  179. components: [],
  180. directives: [],
  181. hoists: [],
  182. imports: [],
  183. cached: 0,
  184. temps: 0,
  185. codegenNode: void 0,
  186. loc
  187. };
  188. }
  189. function createVNodeCall(context, tag, props, children, patchFlag, dynamicProps, directives, isBlock = false, disableTracking = false, isComponent = false, loc = locStub) {
  190. if (context) {
  191. if (isBlock) {
  192. context.helper(OPEN_BLOCK);
  193. context.helper(getVNodeBlockHelper(context.inSSR, isComponent));
  194. } else {
  195. context.helper(getVNodeHelper(context.inSSR, isComponent));
  196. }
  197. if (directives) {
  198. context.helper(WITH_DIRECTIVES);
  199. }
  200. }
  201. return {
  202. type: 13,
  203. tag,
  204. props,
  205. children,
  206. patchFlag,
  207. dynamicProps,
  208. directives,
  209. isBlock,
  210. disableTracking,
  211. isComponent,
  212. loc
  213. };
  214. }
  215. function createArrayExpression(elements, loc = locStub) {
  216. return {
  217. type: 17,
  218. loc,
  219. elements
  220. };
  221. }
  222. function createObjectExpression(properties, loc = locStub) {
  223. return {
  224. type: 15,
  225. loc,
  226. properties
  227. };
  228. }
  229. function createObjectProperty(key, value) {
  230. return {
  231. type: 16,
  232. loc: locStub,
  233. key: shared.isString(key) ? createSimpleExpression(key, true) : key,
  234. value
  235. };
  236. }
  237. function createSimpleExpression(content, isStatic = false, loc = locStub, constType = 0) {
  238. return {
  239. type: 4,
  240. loc,
  241. content,
  242. isStatic,
  243. constType: isStatic ? 3 : constType
  244. };
  245. }
  246. function createInterpolation(content, loc) {
  247. return {
  248. type: 5,
  249. loc,
  250. content: shared.isString(content) ? createSimpleExpression(content, false, loc) : content
  251. };
  252. }
  253. function createCompoundExpression(children, loc = locStub) {
  254. return {
  255. type: 8,
  256. loc,
  257. children
  258. };
  259. }
  260. function createCallExpression(callee, args = [], loc = locStub) {
  261. return {
  262. type: 14,
  263. loc,
  264. callee,
  265. arguments: args
  266. };
  267. }
  268. function createFunctionExpression(params, returns = void 0, newline = false, isSlot = false, loc = locStub) {
  269. return {
  270. type: 18,
  271. params,
  272. returns,
  273. newline,
  274. isSlot,
  275. loc
  276. };
  277. }
  278. function createConditionalExpression(test, consequent, alternate, newline = true) {
  279. return {
  280. type: 19,
  281. test,
  282. consequent,
  283. alternate,
  284. newline,
  285. loc: locStub
  286. };
  287. }
  288. function createCacheExpression(index, value, isVNode = false) {
  289. return {
  290. type: 20,
  291. index,
  292. value,
  293. isVNode,
  294. loc: locStub
  295. };
  296. }
  297. function createBlockStatement(body) {
  298. return {
  299. type: 21,
  300. body,
  301. loc: locStub
  302. };
  303. }
  304. function createTemplateLiteral(elements) {
  305. return {
  306. type: 22,
  307. elements,
  308. loc: locStub
  309. };
  310. }
  311. function createIfStatement(test, consequent, alternate) {
  312. return {
  313. type: 23,
  314. test,
  315. consequent,
  316. alternate,
  317. loc: locStub
  318. };
  319. }
  320. function createAssignmentExpression(left, right) {
  321. return {
  322. type: 24,
  323. left,
  324. right,
  325. loc: locStub
  326. };
  327. }
  328. function createSequenceExpression(expressions) {
  329. return {
  330. type: 25,
  331. expressions,
  332. loc: locStub
  333. };
  334. }
  335. function createReturnStatement(returns) {
  336. return {
  337. type: 26,
  338. returns,
  339. loc: locStub
  340. };
  341. }
  342. function getVNodeHelper(ssr, isComponent) {
  343. return ssr || isComponent ? CREATE_VNODE : CREATE_ELEMENT_VNODE;
  344. }
  345. function getVNodeBlockHelper(ssr, isComponent) {
  346. return ssr || isComponent ? CREATE_BLOCK : CREATE_ELEMENT_BLOCK;
  347. }
  348. function convertToBlock(node, { helper, removeHelper, inSSR }) {
  349. if (!node.isBlock) {
  350. node.isBlock = true;
  351. removeHelper(getVNodeHelper(inSSR, node.isComponent));
  352. helper(OPEN_BLOCK);
  353. helper(getVNodeBlockHelper(inSSR, node.isComponent));
  354. }
  355. }
  356. const isStaticExp = (p) => p.type === 4 && p.isStatic;
  357. const isBuiltInType = (tag, expected) => tag === expected || tag === shared.hyphenate(expected);
  358. function isCoreComponent(tag) {
  359. if (isBuiltInType(tag, "Teleport")) {
  360. return TELEPORT;
  361. } else if (isBuiltInType(tag, "Suspense")) {
  362. return SUSPENSE;
  363. } else if (isBuiltInType(tag, "KeepAlive")) {
  364. return KEEP_ALIVE;
  365. } else if (isBuiltInType(tag, "BaseTransition")) {
  366. return BASE_TRANSITION;
  367. }
  368. }
  369. const nonIdentifierRE = /^\d|[^\$\w]/;
  370. const isSimpleIdentifier = (name) => !nonIdentifierRE.test(name);
  371. const validFirstIdentCharRE = /[A-Za-z_$\xA0-\uFFFF]/;
  372. const validIdentCharRE = /[\.\?\w$\xA0-\uFFFF]/;
  373. const whitespaceRE = /\s+[.[]\s*|\s*[.[]\s+/g;
  374. const isMemberExpressionBrowser = (path) => {
  375. path = path.trim().replace(whitespaceRE, (s) => s.trim());
  376. let state = 0 /* inMemberExp */;
  377. let stateStack = [];
  378. let currentOpenBracketCount = 0;
  379. let currentOpenParensCount = 0;
  380. let currentStringType = null;
  381. for (let i = 0; i < path.length; i++) {
  382. const char = path.charAt(i);
  383. switch (state) {
  384. case 0 /* inMemberExp */:
  385. if (char === "[") {
  386. stateStack.push(state);
  387. state = 1 /* inBrackets */;
  388. currentOpenBracketCount++;
  389. } else if (char === "(") {
  390. stateStack.push(state);
  391. state = 2 /* inParens */;
  392. currentOpenParensCount++;
  393. } else if (!(i === 0 ? validFirstIdentCharRE : validIdentCharRE).test(char)) {
  394. return false;
  395. }
  396. break;
  397. case 1 /* inBrackets */:
  398. if (char === `'` || char === `"` || char === "`") {
  399. stateStack.push(state);
  400. state = 3 /* inString */;
  401. currentStringType = char;
  402. } else if (char === `[`) {
  403. currentOpenBracketCount++;
  404. } else if (char === `]`) {
  405. if (!--currentOpenBracketCount) {
  406. state = stateStack.pop();
  407. }
  408. }
  409. break;
  410. case 2 /* inParens */:
  411. if (char === `'` || char === `"` || char === "`") {
  412. stateStack.push(state);
  413. state = 3 /* inString */;
  414. currentStringType = char;
  415. } else if (char === `(`) {
  416. currentOpenParensCount++;
  417. } else if (char === `)`) {
  418. if (i === path.length - 1) {
  419. return false;
  420. }
  421. if (!--currentOpenParensCount) {
  422. state = stateStack.pop();
  423. }
  424. }
  425. break;
  426. case 3 /* inString */:
  427. if (char === currentStringType) {
  428. state = stateStack.pop();
  429. currentStringType = null;
  430. }
  431. break;
  432. }
  433. }
  434. return !currentOpenBracketCount && !currentOpenParensCount;
  435. };
  436. const isMemberExpressionNode = (path, context) => {
  437. try {
  438. let ret = parser.parseExpression(path, {
  439. plugins: context.expressionPlugins
  440. });
  441. if (ret.type === "TSAsExpression" || ret.type === "TSTypeAssertion") {
  442. ret = ret.expression;
  443. }
  444. return ret.type === "MemberExpression" || ret.type === "OptionalMemberExpression" || ret.type === "Identifier";
  445. } catch (e) {
  446. return false;
  447. }
  448. };
  449. const isMemberExpression = isMemberExpressionNode;
  450. function getInnerRange(loc, offset, length) {
  451. const source = loc.source.slice(offset, offset + length);
  452. const newLoc = {
  453. source,
  454. start: advancePositionWithClone(loc.start, loc.source, offset),
  455. end: loc.end
  456. };
  457. if (length != null) {
  458. newLoc.end = advancePositionWithClone(
  459. loc.start,
  460. loc.source,
  461. offset + length
  462. );
  463. }
  464. return newLoc;
  465. }
  466. function advancePositionWithClone(pos, source, numberOfCharacters = source.length) {
  467. return advancePositionWithMutation(
  468. shared.extend({}, pos),
  469. source,
  470. numberOfCharacters
  471. );
  472. }
  473. function advancePositionWithMutation(pos, source, numberOfCharacters = source.length) {
  474. let linesCount = 0;
  475. let lastNewLinePos = -1;
  476. for (let i = 0; i < numberOfCharacters; i++) {
  477. if (source.charCodeAt(i) === 10) {
  478. linesCount++;
  479. lastNewLinePos = i;
  480. }
  481. }
  482. pos.offset += numberOfCharacters;
  483. pos.line += linesCount;
  484. pos.column = lastNewLinePos === -1 ? pos.column + numberOfCharacters : numberOfCharacters - lastNewLinePos;
  485. return pos;
  486. }
  487. function assert(condition, msg) {
  488. if (!condition) {
  489. throw new Error(msg || `unexpected compiler condition`);
  490. }
  491. }
  492. function findDir(node, name, allowEmpty = false) {
  493. for (let i = 0; i < node.props.length; i++) {
  494. const p = node.props[i];
  495. if (p.type === 7 && (allowEmpty || p.exp) && (shared.isString(name) ? p.name === name : name.test(p.name))) {
  496. return p;
  497. }
  498. }
  499. }
  500. function findProp(node, name, dynamicOnly = false, allowEmpty = false) {
  501. for (let i = 0; i < node.props.length; i++) {
  502. const p = node.props[i];
  503. if (p.type === 6) {
  504. if (dynamicOnly)
  505. continue;
  506. if (p.name === name && (p.value || allowEmpty)) {
  507. return p;
  508. }
  509. } else if (p.name === "bind" && (p.exp || allowEmpty) && isStaticArgOf(p.arg, name)) {
  510. return p;
  511. }
  512. }
  513. }
  514. function isStaticArgOf(arg, name) {
  515. return !!(arg && isStaticExp(arg) && arg.content === name);
  516. }
  517. function hasDynamicKeyVBind(node) {
  518. return node.props.some(
  519. (p) => p.type === 7 && p.name === "bind" && (!p.arg || // v-bind="obj"
  520. p.arg.type !== 4 || // v-bind:[_ctx.foo]
  521. !p.arg.isStatic)
  522. // v-bind:[foo]
  523. );
  524. }
  525. function isText$1(node) {
  526. return node.type === 5 || node.type === 2;
  527. }
  528. function isVSlot(p) {
  529. return p.type === 7 && p.name === "slot";
  530. }
  531. function isTemplateNode(node) {
  532. return node.type === 1 && node.tagType === 3;
  533. }
  534. function isSlotOutlet(node) {
  535. return node.type === 1 && node.tagType === 2;
  536. }
  537. const propsHelperSet = /* @__PURE__ */ new Set([NORMALIZE_PROPS, GUARD_REACTIVE_PROPS]);
  538. function getUnnormalizedProps(props, callPath = []) {
  539. if (props && !shared.isString(props) && props.type === 14) {
  540. const callee = props.callee;
  541. if (!shared.isString(callee) && propsHelperSet.has(callee)) {
  542. return getUnnormalizedProps(
  543. props.arguments[0],
  544. callPath.concat(props)
  545. );
  546. }
  547. }
  548. return [props, callPath];
  549. }
  550. function injectProp(node, prop, context) {
  551. let propsWithInjection;
  552. let props = node.type === 13 ? node.props : node.arguments[2];
  553. let callPath = [];
  554. let parentCall;
  555. if (props && !shared.isString(props) && props.type === 14) {
  556. const ret = getUnnormalizedProps(props);
  557. props = ret[0];
  558. callPath = ret[1];
  559. parentCall = callPath[callPath.length - 1];
  560. }
  561. if (props == null || shared.isString(props)) {
  562. propsWithInjection = createObjectExpression([prop]);
  563. } else if (props.type === 14) {
  564. const first = props.arguments[0];
  565. if (!shared.isString(first) && first.type === 15) {
  566. if (!hasProp(prop, first)) {
  567. first.properties.unshift(prop);
  568. }
  569. } else {
  570. if (props.callee === TO_HANDLERS) {
  571. propsWithInjection = createCallExpression(context.helper(MERGE_PROPS), [
  572. createObjectExpression([prop]),
  573. props
  574. ]);
  575. } else {
  576. props.arguments.unshift(createObjectExpression([prop]));
  577. }
  578. }
  579. !propsWithInjection && (propsWithInjection = props);
  580. } else if (props.type === 15) {
  581. if (!hasProp(prop, props)) {
  582. props.properties.unshift(prop);
  583. }
  584. propsWithInjection = props;
  585. } else {
  586. propsWithInjection = createCallExpression(context.helper(MERGE_PROPS), [
  587. createObjectExpression([prop]),
  588. props
  589. ]);
  590. if (parentCall && parentCall.callee === GUARD_REACTIVE_PROPS) {
  591. parentCall = callPath[callPath.length - 2];
  592. }
  593. }
  594. if (node.type === 13) {
  595. if (parentCall) {
  596. parentCall.arguments[0] = propsWithInjection;
  597. } else {
  598. node.props = propsWithInjection;
  599. }
  600. } else {
  601. if (parentCall) {
  602. parentCall.arguments[0] = propsWithInjection;
  603. } else {
  604. node.arguments[2] = propsWithInjection;
  605. }
  606. }
  607. }
  608. function hasProp(prop, props) {
  609. let result = false;
  610. if (prop.key.type === 4) {
  611. const propKeyName = prop.key.content;
  612. result = props.properties.some(
  613. (p) => p.key.type === 4 && p.key.content === propKeyName
  614. );
  615. }
  616. return result;
  617. }
  618. function toValidAssetId(name, type) {
  619. return `_${type}_${name.replace(/[^\w]/g, (searchValue, replaceValue) => {
  620. return searchValue === "-" ? "_" : name.charCodeAt(replaceValue).toString();
  621. })}`;
  622. }
  623. function hasScopeRef(node, ids) {
  624. if (!node || Object.keys(ids).length === 0) {
  625. return false;
  626. }
  627. switch (node.type) {
  628. case 1:
  629. for (let i = 0; i < node.props.length; i++) {
  630. const p = node.props[i];
  631. if (p.type === 7 && (hasScopeRef(p.arg, ids) || hasScopeRef(p.exp, ids))) {
  632. return true;
  633. }
  634. }
  635. return node.children.some((c) => hasScopeRef(c, ids));
  636. case 11:
  637. if (hasScopeRef(node.source, ids)) {
  638. return true;
  639. }
  640. return node.children.some((c) => hasScopeRef(c, ids));
  641. case 9:
  642. return node.branches.some((b) => hasScopeRef(b, ids));
  643. case 10:
  644. if (hasScopeRef(node.condition, ids)) {
  645. return true;
  646. }
  647. return node.children.some((c) => hasScopeRef(c, ids));
  648. case 4:
  649. return !node.isStatic && isSimpleIdentifier(node.content) && !!ids[node.content];
  650. case 8:
  651. return node.children.some((c) => shared.isObject(c) && hasScopeRef(c, ids));
  652. case 5:
  653. case 12:
  654. return hasScopeRef(node.content, ids);
  655. case 2:
  656. case 3:
  657. return false;
  658. default:
  659. return false;
  660. }
  661. }
  662. function getMemoedVNodeCall(node) {
  663. if (node.type === 14 && node.callee === WITH_MEMO) {
  664. return node.arguments[1].returns;
  665. } else {
  666. return node;
  667. }
  668. }
  669. const deprecationData = {
  670. ["COMPILER_IS_ON_ELEMENT"]: {
  671. message: `Platform-native elements with "is" prop will no longer be treated as components in Vue 3 unless the "is" value is explicitly prefixed with "vue:".`,
  672. link: `https://v3-migration.vuejs.org/breaking-changes/custom-elements-interop.html`
  673. },
  674. ["COMPILER_V_BIND_SYNC"]: {
  675. message: (key) => `.sync modifier for v-bind has been removed. Use v-model with argument instead. \`v-bind:${key}.sync\` should be changed to \`v-model:${key}\`.`,
  676. link: `https://v3-migration.vuejs.org/breaking-changes/v-model.html`
  677. },
  678. ["COMPILER_V_BIND_PROP"]: {
  679. message: `.prop modifier for v-bind has been removed and no longer necessary. Vue 3 will automatically set a binding as DOM property when appropriate.`
  680. },
  681. ["COMPILER_V_BIND_OBJECT_ORDER"]: {
  682. message: `v-bind="obj" usage is now order sensitive and behaves like JavaScript object spread: it will now overwrite an existing non-mergeable attribute that appears before v-bind in the case of conflict. To retain 2.x behavior, move v-bind to make it the first attribute. You can also suppress this warning if the usage is intended.`,
  683. link: `https://v3-migration.vuejs.org/breaking-changes/v-bind.html`
  684. },
  685. ["COMPILER_V_ON_NATIVE"]: {
  686. message: `.native modifier for v-on has been removed as is no longer necessary.`,
  687. link: `https://v3-migration.vuejs.org/breaking-changes/v-on-native-modifier-removed.html`
  688. },
  689. ["COMPILER_V_IF_V_FOR_PRECEDENCE"]: {
  690. message: `v-if / v-for precedence when used on the same element has changed in Vue 3: v-if now takes higher precedence and will no longer have access to v-for scope variables. It is best to avoid the ambiguity with <template> tags or use a computed property that filters v-for data source.`,
  691. link: `https://v3-migration.vuejs.org/breaking-changes/v-if-v-for.html`
  692. },
  693. ["COMPILER_NATIVE_TEMPLATE"]: {
  694. message: `<template> with no special directives will render as a native template element instead of its inner content in Vue 3.`
  695. },
  696. ["COMPILER_INLINE_TEMPLATE"]: {
  697. message: `"inline-template" has been removed in Vue 3.`,
  698. link: `https://v3-migration.vuejs.org/breaking-changes/inline-template-attribute.html`
  699. },
  700. ["COMPILER_FILTER"]: {
  701. message: `filters have been removed in Vue 3. The "|" symbol will be treated as native JavaScript bitwise OR operator. Use method calls or computed properties instead.`,
  702. link: `https://v3-migration.vuejs.org/breaking-changes/filters.html`
  703. }
  704. };
  705. function getCompatValue(key, context) {
  706. const config = context.options ? context.options.compatConfig : context.compatConfig;
  707. const value = config && config[key];
  708. if (key === "MODE") {
  709. return value || 3;
  710. } else {
  711. return value;
  712. }
  713. }
  714. function isCompatEnabled(key, context) {
  715. const mode = getCompatValue("MODE", context);
  716. const value = getCompatValue(key, context);
  717. return mode === 3 ? value === true : value !== false;
  718. }
  719. function checkCompatEnabled(key, context, loc, ...args) {
  720. const enabled = isCompatEnabled(key, context);
  721. return enabled;
  722. }
  723. function warnDeprecation(key, context, loc, ...args) {
  724. const val = getCompatValue(key, context);
  725. if (val === "suppress-warning") {
  726. return;
  727. }
  728. const { message, link } = deprecationData[key];
  729. const msg = `(deprecation ${key}) ${typeof message === "function" ? message(...args) : message}${link ? `
  730. Details: ${link}` : ``}`;
  731. const err = new SyntaxError(msg);
  732. err.code = key;
  733. if (loc)
  734. err.loc = loc;
  735. context.onWarn(err);
  736. }
  737. const decodeRE = /&(gt|lt|amp|apos|quot);/g;
  738. const decodeMap = {
  739. gt: ">",
  740. lt: "<",
  741. amp: "&",
  742. apos: "'",
  743. quot: '"'
  744. };
  745. const defaultParserOptions = {
  746. delimiters: [`{{`, `}}`],
  747. getNamespace: () => 0,
  748. getTextMode: () => 0,
  749. isVoidTag: shared.NO,
  750. isPreTag: shared.NO,
  751. isCustomElement: shared.NO,
  752. decodeEntities: (rawText) => rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
  753. onError: defaultOnError,
  754. onWarn: defaultOnWarn,
  755. comments: false
  756. };
  757. function baseParse(content, options = {}) {
  758. const context = createParserContext(content, options);
  759. const start = getCursor(context);
  760. return createRoot(
  761. parseChildren(context, 0, []),
  762. getSelection(context, start)
  763. );
  764. }
  765. function createParserContext(content, rawOptions) {
  766. const options = shared.extend({}, defaultParserOptions);
  767. let key;
  768. for (key in rawOptions) {
  769. options[key] = rawOptions[key] === void 0 ? defaultParserOptions[key] : rawOptions[key];
  770. }
  771. return {
  772. options,
  773. column: 1,
  774. line: 1,
  775. offset: 0,
  776. originalSource: content,
  777. source: content,
  778. inPre: false,
  779. inVPre: false,
  780. onWarn: options.onWarn
  781. };
  782. }
  783. function parseChildren(context, mode, ancestors) {
  784. const parent = last(ancestors);
  785. const ns = parent ? parent.ns : 0;
  786. const nodes = [];
  787. while (!isEnd(context, mode, ancestors)) {
  788. const s = context.source;
  789. let node = void 0;
  790. if (mode === 0 || mode === 1) {
  791. if (!context.inVPre && startsWith(s, context.options.delimiters[0])) {
  792. node = parseInterpolation(context, mode);
  793. } else if (mode === 0 && s[0] === "<") {
  794. if (s.length === 1) {
  795. emitError(context, 5, 1);
  796. } else if (s[1] === "!") {
  797. if (startsWith(s, "<!--")) {
  798. node = parseComment(context);
  799. } else if (startsWith(s, "<!DOCTYPE")) {
  800. node = parseBogusComment(context);
  801. } else if (startsWith(s, "<![CDATA[")) {
  802. if (ns !== 0) {
  803. node = parseCDATA(context, ancestors);
  804. } else {
  805. emitError(context, 1);
  806. node = parseBogusComment(context);
  807. }
  808. } else {
  809. emitError(context, 11);
  810. node = parseBogusComment(context);
  811. }
  812. } else if (s[1] === "/") {
  813. if (s.length === 2) {
  814. emitError(context, 5, 2);
  815. } else if (s[2] === ">") {
  816. emitError(context, 14, 2);
  817. advanceBy(context, 3);
  818. continue;
  819. } else if (/[a-z]/i.test(s[2])) {
  820. emitError(context, 23);
  821. parseTag(context, TagType.End, parent);
  822. continue;
  823. } else {
  824. emitError(
  825. context,
  826. 12,
  827. 2
  828. );
  829. node = parseBogusComment(context);
  830. }
  831. } else if (/[a-z]/i.test(s[1])) {
  832. node = parseElement(context, ancestors);
  833. if (isCompatEnabled(
  834. "COMPILER_NATIVE_TEMPLATE",
  835. context
  836. ) && node && node.tag === "template" && !node.props.some(
  837. (p) => p.type === 7 && isSpecialTemplateDirective(p.name)
  838. )) {
  839. node = node.children;
  840. }
  841. } else if (s[1] === "?") {
  842. emitError(
  843. context,
  844. 21,
  845. 1
  846. );
  847. node = parseBogusComment(context);
  848. } else {
  849. emitError(context, 12, 1);
  850. }
  851. }
  852. }
  853. if (!node) {
  854. node = parseText(context, mode);
  855. }
  856. if (shared.isArray(node)) {
  857. for (let i = 0; i < node.length; i++) {
  858. pushNode(nodes, node[i]);
  859. }
  860. } else {
  861. pushNode(nodes, node);
  862. }
  863. }
  864. let removedWhitespace = false;
  865. if (mode !== 2 && mode !== 1) {
  866. const shouldCondense = context.options.whitespace !== "preserve";
  867. for (let i = 0; i < nodes.length; i++) {
  868. const node = nodes[i];
  869. if (node.type === 2) {
  870. if (!context.inPre) {
  871. if (!/[^\t\r\n\f ]/.test(node.content)) {
  872. const prev = nodes[i - 1];
  873. const next = nodes[i + 1];
  874. if (!prev || !next || shouldCondense && (prev.type === 3 && next.type === 3 || prev.type === 3 && next.type === 1 || prev.type === 1 && next.type === 3 || prev.type === 1 && next.type === 1 && /[\r\n]/.test(node.content))) {
  875. removedWhitespace = true;
  876. nodes[i] = null;
  877. } else {
  878. node.content = " ";
  879. }
  880. } else if (shouldCondense) {
  881. node.content = node.content.replace(/[\t\r\n\f ]+/g, " ");
  882. }
  883. } else {
  884. node.content = node.content.replace(/\r\n/g, "\n");
  885. }
  886. } else if (node.type === 3 && !context.options.comments) {
  887. removedWhitespace = true;
  888. nodes[i] = null;
  889. }
  890. }
  891. if (context.inPre && parent && context.options.isPreTag(parent.tag)) {
  892. const first = nodes[0];
  893. if (first && first.type === 2) {
  894. first.content = first.content.replace(/^\r?\n/, "");
  895. }
  896. }
  897. }
  898. return removedWhitespace ? nodes.filter(Boolean) : nodes;
  899. }
  900. function pushNode(nodes, node) {
  901. if (node.type === 2) {
  902. const prev = last(nodes);
  903. if (prev && prev.type === 2 && prev.loc.end.offset === node.loc.start.offset) {
  904. prev.content += node.content;
  905. prev.loc.end = node.loc.end;
  906. prev.loc.source += node.loc.source;
  907. return;
  908. }
  909. }
  910. nodes.push(node);
  911. }
  912. function parseCDATA(context, ancestors) {
  913. advanceBy(context, 9);
  914. const nodes = parseChildren(context, 3, ancestors);
  915. if (context.source.length === 0) {
  916. emitError(context, 6);
  917. } else {
  918. advanceBy(context, 3);
  919. }
  920. return nodes;
  921. }
  922. function parseComment(context) {
  923. const start = getCursor(context);
  924. let content;
  925. const match = /--(\!)?>/.exec(context.source);
  926. if (!match) {
  927. content = context.source.slice(4);
  928. advanceBy(context, context.source.length);
  929. emitError(context, 7);
  930. } else {
  931. if (match.index <= 3) {
  932. emitError(context, 0);
  933. }
  934. if (match[1]) {
  935. emitError(context, 10);
  936. }
  937. content = context.source.slice(4, match.index);
  938. const s = context.source.slice(0, match.index);
  939. let prevIndex = 1, nestedIndex = 0;
  940. while ((nestedIndex = s.indexOf("<!--", prevIndex)) !== -1) {
  941. advanceBy(context, nestedIndex - prevIndex + 1);
  942. if (nestedIndex + 4 < s.length) {
  943. emitError(context, 16);
  944. }
  945. prevIndex = nestedIndex + 1;
  946. }
  947. advanceBy(context, match.index + match[0].length - prevIndex + 1);
  948. }
  949. return {
  950. type: 3,
  951. content,
  952. loc: getSelection(context, start)
  953. };
  954. }
  955. function parseBogusComment(context) {
  956. const start = getCursor(context);
  957. const contentStart = context.source[1] === "?" ? 1 : 2;
  958. let content;
  959. const closeIndex = context.source.indexOf(">");
  960. if (closeIndex === -1) {
  961. content = context.source.slice(contentStart);
  962. advanceBy(context, context.source.length);
  963. } else {
  964. content = context.source.slice(contentStart, closeIndex);
  965. advanceBy(context, closeIndex + 1);
  966. }
  967. return {
  968. type: 3,
  969. content,
  970. loc: getSelection(context, start)
  971. };
  972. }
  973. function parseElement(context, ancestors) {
  974. const wasInPre = context.inPre;
  975. const wasInVPre = context.inVPre;
  976. const parent = last(ancestors);
  977. const element = parseTag(context, TagType.Start, parent);
  978. const isPreBoundary = context.inPre && !wasInPre;
  979. const isVPreBoundary = context.inVPre && !wasInVPre;
  980. if (element.isSelfClosing || context.options.isVoidTag(element.tag)) {
  981. if (isPreBoundary) {
  982. context.inPre = false;
  983. }
  984. if (isVPreBoundary) {
  985. context.inVPre = false;
  986. }
  987. return element;
  988. }
  989. ancestors.push(element);
  990. const mode = context.options.getTextMode(element, parent);
  991. const children = parseChildren(context, mode, ancestors);
  992. ancestors.pop();
  993. {
  994. const inlineTemplateProp = element.props.find(
  995. (p) => p.type === 6 && p.name === "inline-template"
  996. );
  997. if (inlineTemplateProp && checkCompatEnabled(
  998. "COMPILER_INLINE_TEMPLATE",
  999. context,
  1000. inlineTemplateProp.loc
  1001. )) {
  1002. const loc = getSelection(context, element.loc.end);
  1003. inlineTemplateProp.value = {
  1004. type: 2,
  1005. content: loc.source,
  1006. loc
  1007. };
  1008. }
  1009. }
  1010. element.children = children;
  1011. if (startsWithEndTagOpen(context.source, element.tag)) {
  1012. parseTag(context, TagType.End, parent);
  1013. } else {
  1014. emitError(context, 24, 0, element.loc.start);
  1015. if (context.source.length === 0 && element.tag.toLowerCase() === "script") {
  1016. const first = children[0];
  1017. if (first && startsWith(first.loc.source, "<!--")) {
  1018. emitError(context, 8);
  1019. }
  1020. }
  1021. }
  1022. element.loc = getSelection(context, element.loc.start);
  1023. if (isPreBoundary) {
  1024. context.inPre = false;
  1025. }
  1026. if (isVPreBoundary) {
  1027. context.inVPre = false;
  1028. }
  1029. return element;
  1030. }
  1031. var TagType = /* @__PURE__ */ ((TagType2) => {
  1032. TagType2[TagType2["Start"] = 0] = "Start";
  1033. TagType2[TagType2["End"] = 1] = "End";
  1034. return TagType2;
  1035. })(TagType || {});
  1036. const isSpecialTemplateDirective = /* @__PURE__ */ shared.makeMap(
  1037. `if,else,else-if,for,slot`
  1038. );
  1039. function parseTag(context, type, parent) {
  1040. const start = getCursor(context);
  1041. const match = /^<\/?([a-z][^\t\r\n\f />]*)/i.exec(context.source);
  1042. const tag = match[1];
  1043. const ns = context.options.getNamespace(tag, parent);
  1044. advanceBy(context, match[0].length);
  1045. advanceSpaces(context);
  1046. const cursor = getCursor(context);
  1047. const currentSource = context.source;
  1048. if (context.options.isPreTag(tag)) {
  1049. context.inPre = true;
  1050. }
  1051. let props = parseAttributes(context, type);
  1052. if (type === 0 /* Start */ && !context.inVPre && props.some((p) => p.type === 7 && p.name === "pre")) {
  1053. context.inVPre = true;
  1054. shared.extend(context, cursor);
  1055. context.source = currentSource;
  1056. props = parseAttributes(context, type).filter((p) => p.name !== "v-pre");
  1057. }
  1058. let isSelfClosing = false;
  1059. if (context.source.length === 0) {
  1060. emitError(context, 9);
  1061. } else {
  1062. isSelfClosing = startsWith(context.source, "/>");
  1063. if (type === 1 /* End */ && isSelfClosing) {
  1064. emitError(context, 4);
  1065. }
  1066. advanceBy(context, isSelfClosing ? 2 : 1);
  1067. }
  1068. if (type === 1 /* End */) {
  1069. return;
  1070. }
  1071. let tagType = 0;
  1072. if (!context.inVPre) {
  1073. if (tag === "slot") {
  1074. tagType = 2;
  1075. } else if (tag === "template") {
  1076. if (props.some(
  1077. (p) => p.type === 7 && isSpecialTemplateDirective(p.name)
  1078. )) {
  1079. tagType = 3;
  1080. }
  1081. } else if (isComponent(tag, props, context)) {
  1082. tagType = 1;
  1083. }
  1084. }
  1085. return {
  1086. type: 1,
  1087. ns,
  1088. tag,
  1089. tagType,
  1090. props,
  1091. isSelfClosing,
  1092. children: [],
  1093. loc: getSelection(context, start),
  1094. codegenNode: void 0
  1095. // to be created during transform phase
  1096. };
  1097. }
  1098. function isComponent(tag, props, context) {
  1099. const options = context.options;
  1100. if (options.isCustomElement(tag)) {
  1101. return false;
  1102. }
  1103. if (tag === "component" || /^[A-Z]/.test(tag) || isCoreComponent(tag) || options.isBuiltInComponent && options.isBuiltInComponent(tag) || options.isNativeTag && !options.isNativeTag(tag)) {
  1104. return true;
  1105. }
  1106. for (let i = 0; i < props.length; i++) {
  1107. const p = props[i];
  1108. if (p.type === 6) {
  1109. if (p.name === "is" && p.value) {
  1110. if (p.value.content.startsWith("vue:")) {
  1111. return true;
  1112. } else if (checkCompatEnabled(
  1113. "COMPILER_IS_ON_ELEMENT",
  1114. context,
  1115. p.loc
  1116. )) {
  1117. return true;
  1118. }
  1119. }
  1120. } else {
  1121. if (p.name === "is") {
  1122. return true;
  1123. } else if (
  1124. // :is on plain element - only treat as component in compat mode
  1125. p.name === "bind" && isStaticArgOf(p.arg, "is") && true && checkCompatEnabled(
  1126. "COMPILER_IS_ON_ELEMENT",
  1127. context,
  1128. p.loc
  1129. )
  1130. ) {
  1131. return true;
  1132. }
  1133. }
  1134. }
  1135. }
  1136. function parseAttributes(context, type) {
  1137. const props = [];
  1138. const attributeNames = /* @__PURE__ */ new Set();
  1139. while (context.source.length > 0 && !startsWith(context.source, ">") && !startsWith(context.source, "/>")) {
  1140. if (startsWith(context.source, "/")) {
  1141. emitError(context, 22);
  1142. advanceBy(context, 1);
  1143. advanceSpaces(context);
  1144. continue;
  1145. }
  1146. if (type === 1 /* End */) {
  1147. emitError(context, 3);
  1148. }
  1149. const attr = parseAttribute(context, attributeNames);
  1150. if (attr.type === 6 && attr.value && attr.name === "class") {
  1151. attr.value.content = attr.value.content.replace(/\s+/g, " ").trim();
  1152. }
  1153. if (type === 0 /* Start */) {
  1154. props.push(attr);
  1155. }
  1156. if (/^[^\t\r\n\f />]/.test(context.source)) {
  1157. emitError(context, 15);
  1158. }
  1159. advanceSpaces(context);
  1160. }
  1161. return props;
  1162. }
  1163. function parseAttribute(context, nameSet) {
  1164. var _a;
  1165. const start = getCursor(context);
  1166. const match = /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(context.source);
  1167. const name = match[0];
  1168. if (nameSet.has(name)) {
  1169. emitError(context, 2);
  1170. }
  1171. nameSet.add(name);
  1172. if (name[0] === "=") {
  1173. emitError(context, 19);
  1174. }
  1175. {
  1176. const pattern = /["'<]/g;
  1177. let m;
  1178. while (m = pattern.exec(name)) {
  1179. emitError(
  1180. context,
  1181. 17,
  1182. m.index
  1183. );
  1184. }
  1185. }
  1186. advanceBy(context, name.length);
  1187. let value = void 0;
  1188. if (/^[\t\r\n\f ]*=/.test(context.source)) {
  1189. advanceSpaces(context);
  1190. advanceBy(context, 1);
  1191. advanceSpaces(context);
  1192. value = parseAttributeValue(context);
  1193. if (!value) {
  1194. emitError(context, 13);
  1195. }
  1196. }
  1197. const loc = getSelection(context, start);
  1198. if (!context.inVPre && /^(v-[A-Za-z0-9-]|:|\.|@|#)/.test(name)) {
  1199. const match2 = /(?:^v-([a-z0-9-]+))?(?:(?::|^\.|^@|^#)(\[[^\]]+\]|[^\.]+))?(.+)?$/i.exec(
  1200. name
  1201. );
  1202. let isPropShorthand = startsWith(name, ".");
  1203. let dirName = match2[1] || (isPropShorthand || startsWith(name, ":") ? "bind" : startsWith(name, "@") ? "on" : "slot");
  1204. let arg;
  1205. if (match2[2]) {
  1206. const isSlot = dirName === "slot";
  1207. const startOffset = name.lastIndexOf(
  1208. match2[2],
  1209. name.length - (((_a = match2[3]) == null ? void 0 : _a.length) || 0)
  1210. );
  1211. const loc2 = getSelection(
  1212. context,
  1213. getNewPosition(context, start, startOffset),
  1214. getNewPosition(
  1215. context,
  1216. start,
  1217. startOffset + match2[2].length + (isSlot && match2[3] || "").length
  1218. )
  1219. );
  1220. let content = match2[2];
  1221. let isStatic = true;
  1222. if (content.startsWith("[")) {
  1223. isStatic = false;
  1224. if (!content.endsWith("]")) {
  1225. emitError(
  1226. context,
  1227. 27
  1228. );
  1229. content = content.slice(1);
  1230. } else {
  1231. content = content.slice(1, content.length - 1);
  1232. }
  1233. } else if (isSlot) {
  1234. content += match2[3] || "";
  1235. }
  1236. arg = {
  1237. type: 4,
  1238. content,
  1239. isStatic,
  1240. constType: isStatic ? 3 : 0,
  1241. loc: loc2
  1242. };
  1243. }
  1244. if (value && value.isQuoted) {
  1245. const valueLoc = value.loc;
  1246. valueLoc.start.offset++;
  1247. valueLoc.start.column++;
  1248. valueLoc.end = advancePositionWithClone(valueLoc.start, value.content);
  1249. valueLoc.source = valueLoc.source.slice(1, -1);
  1250. }
  1251. const modifiers = match2[3] ? match2[3].slice(1).split(".") : [];
  1252. if (isPropShorthand)
  1253. modifiers.push("prop");
  1254. if (dirName === "bind" && arg) {
  1255. if (modifiers.includes("sync") && checkCompatEnabled(
  1256. "COMPILER_V_BIND_SYNC",
  1257. context,
  1258. loc,
  1259. arg.loc.source
  1260. )) {
  1261. dirName = "model";
  1262. modifiers.splice(modifiers.indexOf("sync"), 1);
  1263. }
  1264. }
  1265. return {
  1266. type: 7,
  1267. name: dirName,
  1268. exp: value && {
  1269. type: 4,
  1270. content: value.content,
  1271. isStatic: false,
  1272. // Treat as non-constant by default. This can be potentially set to
  1273. // other values by `transformExpression` to make it eligible for hoisting.
  1274. constType: 0,
  1275. loc: value.loc
  1276. },
  1277. arg,
  1278. modifiers,
  1279. loc
  1280. };
  1281. }
  1282. if (!context.inVPre && startsWith(name, "v-")) {
  1283. emitError(context, 26);
  1284. }
  1285. return {
  1286. type: 6,
  1287. name,
  1288. value: value && {
  1289. type: 2,
  1290. content: value.content,
  1291. loc: value.loc
  1292. },
  1293. loc
  1294. };
  1295. }
  1296. function parseAttributeValue(context) {
  1297. const start = getCursor(context);
  1298. let content;
  1299. const quote = context.source[0];
  1300. const isQuoted = quote === `"` || quote === `'`;
  1301. if (isQuoted) {
  1302. advanceBy(context, 1);
  1303. const endIndex = context.source.indexOf(quote);
  1304. if (endIndex === -1) {
  1305. content = parseTextData(
  1306. context,
  1307. context.source.length,
  1308. 4
  1309. );
  1310. } else {
  1311. content = parseTextData(context, endIndex, 4);
  1312. advanceBy(context, 1);
  1313. }
  1314. } else {
  1315. const match = /^[^\t\r\n\f >]+/.exec(context.source);
  1316. if (!match) {
  1317. return void 0;
  1318. }
  1319. const unexpectedChars = /["'<=`]/g;
  1320. let m;
  1321. while (m = unexpectedChars.exec(match[0])) {
  1322. emitError(
  1323. context,
  1324. 18,
  1325. m.index
  1326. );
  1327. }
  1328. content = parseTextData(context, match[0].length, 4);
  1329. }
  1330. return { content, isQuoted, loc: getSelection(context, start) };
  1331. }
  1332. function parseInterpolation(context, mode) {
  1333. const [open, close] = context.options.delimiters;
  1334. const closeIndex = context.source.indexOf(close, open.length);
  1335. if (closeIndex === -1) {
  1336. emitError(context, 25);
  1337. return void 0;
  1338. }
  1339. const start = getCursor(context);
  1340. advanceBy(context, open.length);
  1341. const innerStart = getCursor(context);
  1342. const innerEnd = getCursor(context);
  1343. const rawContentLength = closeIndex - open.length;
  1344. const rawContent = context.source.slice(0, rawContentLength);
  1345. const preTrimContent = parseTextData(context, rawContentLength, mode);
  1346. const content = preTrimContent.trim();
  1347. const startOffset = preTrimContent.indexOf(content);
  1348. if (startOffset > 0) {
  1349. advancePositionWithMutation(innerStart, rawContent, startOffset);
  1350. }
  1351. const endOffset = rawContentLength - (preTrimContent.length - content.length - startOffset);
  1352. advancePositionWithMutation(innerEnd, rawContent, endOffset);
  1353. advanceBy(context, close.length);
  1354. return {
  1355. type: 5,
  1356. content: {
  1357. type: 4,
  1358. isStatic: false,
  1359. // Set `isConstant` to false by default and will decide in transformExpression
  1360. constType: 0,
  1361. content,
  1362. loc: getSelection(context, innerStart, innerEnd)
  1363. },
  1364. loc: getSelection(context, start)
  1365. };
  1366. }
  1367. function parseText(context, mode) {
  1368. const endTokens = mode === 3 ? ["]]>"] : ["<", context.options.delimiters[0]];
  1369. let endIndex = context.source.length;
  1370. for (let i = 0; i < endTokens.length; i++) {
  1371. const index = context.source.indexOf(endTokens[i], 1);
  1372. if (index !== -1 && endIndex > index) {
  1373. endIndex = index;
  1374. }
  1375. }
  1376. const start = getCursor(context);
  1377. const content = parseTextData(context, endIndex, mode);
  1378. return {
  1379. type: 2,
  1380. content,
  1381. loc: getSelection(context, start)
  1382. };
  1383. }
  1384. function parseTextData(context, length, mode) {
  1385. const rawText = context.source.slice(0, length);
  1386. advanceBy(context, length);
  1387. if (mode === 2 || mode === 3 || !rawText.includes("&")) {
  1388. return rawText;
  1389. } else {
  1390. return context.options.decodeEntities(
  1391. rawText,
  1392. mode === 4
  1393. );
  1394. }
  1395. }
  1396. function getCursor(context) {
  1397. const { column, line, offset } = context;
  1398. return { column, line, offset };
  1399. }
  1400. function getSelection(context, start, end) {
  1401. end = end || getCursor(context);
  1402. return {
  1403. start,
  1404. end,
  1405. source: context.originalSource.slice(start.offset, end.offset)
  1406. };
  1407. }
  1408. function last(xs) {
  1409. return xs[xs.length - 1];
  1410. }
  1411. function startsWith(source, searchString) {
  1412. return source.startsWith(searchString);
  1413. }
  1414. function advanceBy(context, numberOfCharacters) {
  1415. const { source } = context;
  1416. advancePositionWithMutation(context, source, numberOfCharacters);
  1417. context.source = source.slice(numberOfCharacters);
  1418. }
  1419. function advanceSpaces(context) {
  1420. const match = /^[\t\r\n\f ]+/.exec(context.source);
  1421. if (match) {
  1422. advanceBy(context, match[0].length);
  1423. }
  1424. }
  1425. function getNewPosition(context, start, numberOfCharacters) {
  1426. return advancePositionWithClone(
  1427. start,
  1428. context.originalSource.slice(start.offset, numberOfCharacters),
  1429. numberOfCharacters
  1430. );
  1431. }
  1432. function emitError(context, code, offset, loc = getCursor(context)) {
  1433. if (offset) {
  1434. loc.offset += offset;
  1435. loc.column += offset;
  1436. }
  1437. context.options.onError(
  1438. createCompilerError(code, {
  1439. start: loc,
  1440. end: loc,
  1441. source: ""
  1442. })
  1443. );
  1444. }
  1445. function isEnd(context, mode, ancestors) {
  1446. const s = context.source;
  1447. switch (mode) {
  1448. case 0:
  1449. if (startsWith(s, "</")) {
  1450. for (let i = ancestors.length - 1; i >= 0; --i) {
  1451. if (startsWithEndTagOpen(s, ancestors[i].tag)) {
  1452. return true;
  1453. }
  1454. }
  1455. }
  1456. break;
  1457. case 1:
  1458. case 2: {
  1459. const parent = last(ancestors);
  1460. if (parent && startsWithEndTagOpen(s, parent.tag)) {
  1461. return true;
  1462. }
  1463. break;
  1464. }
  1465. case 3:
  1466. if (startsWith(s, "]]>")) {
  1467. return true;
  1468. }
  1469. break;
  1470. }
  1471. return !s;
  1472. }
  1473. function startsWithEndTagOpen(source, tag) {
  1474. return startsWith(source, "</") && source.slice(2, 2 + tag.length).toLowerCase() === tag.toLowerCase() && /[\t\r\n\f />]/.test(source[2 + tag.length] || ">");
  1475. }
  1476. function hoistStatic(root, context) {
  1477. walk(
  1478. root,
  1479. context,
  1480. // Root node is unfortunately non-hoistable due to potential parent
  1481. // fallthrough attributes.
  1482. isSingleElementRoot(root, root.children[0])
  1483. );
  1484. }
  1485. function isSingleElementRoot(root, child) {
  1486. const { children } = root;
  1487. return children.length === 1 && child.type === 1 && !isSlotOutlet(child);
  1488. }
  1489. function walk(node, context, doNotHoistNode = false) {
  1490. const { children } = node;
  1491. const originalCount = children.length;
  1492. let hoistedCount = 0;
  1493. for (let i = 0; i < children.length; i++) {
  1494. const child = children[i];
  1495. if (child.type === 1 && child.tagType === 0) {
  1496. const constantType = doNotHoistNode ? 0 : getConstantType(child, context);
  1497. if (constantType > 0) {
  1498. if (constantType >= 2) {
  1499. child.codegenNode.patchFlag = -1 + (``);
  1500. child.codegenNode = context.hoist(child.codegenNode);
  1501. hoistedCount++;
  1502. continue;
  1503. }
  1504. } else {
  1505. const codegenNode = child.codegenNode;
  1506. if (codegenNode.type === 13) {
  1507. const flag = getPatchFlag(codegenNode);
  1508. if ((!flag || flag === 512 || flag === 1) && getGeneratedPropsConstantType(child, context) >= 2) {
  1509. const props = getNodeProps(child);
  1510. if (props) {
  1511. codegenNode.props = context.hoist(props);
  1512. }
  1513. }
  1514. if (codegenNode.dynamicProps) {
  1515. codegenNode.dynamicProps = context.hoist(codegenNode.dynamicProps);
  1516. }
  1517. }
  1518. }
  1519. }
  1520. if (child.type === 1) {
  1521. const isComponent = child.tagType === 1;
  1522. if (isComponent) {
  1523. context.scopes.vSlot++;
  1524. }
  1525. walk(child, context);
  1526. if (isComponent) {
  1527. context.scopes.vSlot--;
  1528. }
  1529. } else if (child.type === 11) {
  1530. walk(child, context, child.children.length === 1);
  1531. } else if (child.type === 9) {
  1532. for (let i2 = 0; i2 < child.branches.length; i2++) {
  1533. walk(
  1534. child.branches[i2],
  1535. context,
  1536. child.branches[i2].children.length === 1
  1537. );
  1538. }
  1539. }
  1540. }
  1541. if (hoistedCount && context.transformHoist) {
  1542. context.transformHoist(children, context, node);
  1543. }
  1544. if (hoistedCount && hoistedCount === originalCount && node.type === 1 && node.tagType === 0 && node.codegenNode && node.codegenNode.type === 13 && shared.isArray(node.codegenNode.children)) {
  1545. node.codegenNode.children = context.hoist(
  1546. createArrayExpression(node.codegenNode.children)
  1547. );
  1548. }
  1549. }
  1550. function getConstantType(node, context) {
  1551. const { constantCache } = context;
  1552. switch (node.type) {
  1553. case 1:
  1554. if (node.tagType !== 0) {
  1555. return 0;
  1556. }
  1557. const cached = constantCache.get(node);
  1558. if (cached !== void 0) {
  1559. return cached;
  1560. }
  1561. const codegenNode = node.codegenNode;
  1562. if (codegenNode.type !== 13) {
  1563. return 0;
  1564. }
  1565. if (codegenNode.isBlock && node.tag !== "svg" && node.tag !== "foreignObject") {
  1566. return 0;
  1567. }
  1568. const flag = getPatchFlag(codegenNode);
  1569. if (!flag) {
  1570. let returnType2 = 3;
  1571. const generatedPropsType = getGeneratedPropsConstantType(node, context);
  1572. if (generatedPropsType === 0) {
  1573. constantCache.set(node, 0);
  1574. return 0;
  1575. }
  1576. if (generatedPropsType < returnType2) {
  1577. returnType2 = generatedPropsType;
  1578. }
  1579. for (let i = 0; i < node.children.length; i++) {
  1580. const childType = getConstantType(node.children[i], context);
  1581. if (childType === 0) {
  1582. constantCache.set(node, 0);
  1583. return 0;
  1584. }
  1585. if (childType < returnType2) {
  1586. returnType2 = childType;
  1587. }
  1588. }
  1589. if (returnType2 > 1) {
  1590. for (let i = 0; i < node.props.length; i++) {
  1591. const p = node.props[i];
  1592. if (p.type === 7 && p.name === "bind" && p.exp) {
  1593. const expType = getConstantType(p.exp, context);
  1594. if (expType === 0) {
  1595. constantCache.set(node, 0);
  1596. return 0;
  1597. }
  1598. if (expType < returnType2) {
  1599. returnType2 = expType;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. if (codegenNode.isBlock) {
  1605. for (let i = 0; i < node.props.length; i++) {
  1606. const p = node.props[i];
  1607. if (p.type === 7) {
  1608. constantCache.set(node, 0);
  1609. return 0;
  1610. }
  1611. }
  1612. context.removeHelper(OPEN_BLOCK);
  1613. context.removeHelper(
  1614. getVNodeBlockHelper(context.inSSR, codegenNode.isComponent)
  1615. );
  1616. codegenNode.isBlock = false;
  1617. context.helper(getVNodeHelper(context.inSSR, codegenNode.isComponent));
  1618. }
  1619. constantCache.set(node, returnType2);
  1620. return returnType2;
  1621. } else {
  1622. constantCache.set(node, 0);
  1623. return 0;
  1624. }
  1625. case 2:
  1626. case 3:
  1627. return 3;
  1628. case 9:
  1629. case 11:
  1630. case 10:
  1631. return 0;
  1632. case 5:
  1633. case 12:
  1634. return getConstantType(node.content, context);
  1635. case 4:
  1636. return node.constType;
  1637. case 8:
  1638. let returnType = 3;
  1639. for (let i = 0; i < node.children.length; i++) {
  1640. const child = node.children[i];
  1641. if (shared.isString(child) || shared.isSymbol(child)) {
  1642. continue;
  1643. }
  1644. const childType = getConstantType(child, context);
  1645. if (childType === 0) {
  1646. return 0;
  1647. } else if (childType < returnType) {
  1648. returnType = childType;
  1649. }
  1650. }
  1651. return returnType;
  1652. default:
  1653. return 0;
  1654. }
  1655. }
  1656. const allowHoistedHelperSet = /* @__PURE__ */ new Set([
  1657. NORMALIZE_CLASS,
  1658. NORMALIZE_STYLE,
  1659. NORMALIZE_PROPS,
  1660. GUARD_REACTIVE_PROPS
  1661. ]);
  1662. function getConstantTypeOfHelperCall(value, context) {
  1663. if (value.type === 14 && !shared.isString(value.callee) && allowHoistedHelperSet.has(value.callee)) {
  1664. const arg = value.arguments[0];
  1665. if (arg.type === 4) {
  1666. return getConstantType(arg, context);
  1667. } else if (arg.type === 14) {
  1668. return getConstantTypeOfHelperCall(arg, context);
  1669. }
  1670. }
  1671. return 0;
  1672. }
  1673. function getGeneratedPropsConstantType(node, context) {
  1674. let returnType = 3;
  1675. const props = getNodeProps(node);
  1676. if (props && props.type === 15) {
  1677. const { properties } = props;
  1678. for (let i = 0; i < properties.length; i++) {
  1679. const { key, value } = properties[i];
  1680. const keyType = getConstantType(key, context);
  1681. if (keyType === 0) {
  1682. return keyType;
  1683. }
  1684. if (keyType < returnType) {
  1685. returnType = keyType;
  1686. }
  1687. let valueType;
  1688. if (value.type === 4) {
  1689. valueType = getConstantType(value, context);
  1690. } else if (value.type === 14) {
  1691. valueType = getConstantTypeOfHelperCall(value, context);
  1692. } else {
  1693. valueType = 0;
  1694. }
  1695. if (valueType === 0) {
  1696. return valueType;
  1697. }
  1698. if (valueType < returnType) {
  1699. returnType = valueType;
  1700. }
  1701. }
  1702. }
  1703. return returnType;
  1704. }
  1705. function getNodeProps(node) {
  1706. const codegenNode = node.codegenNode;
  1707. if (codegenNode.type === 13) {
  1708. return codegenNode.props;
  1709. }
  1710. }
  1711. function getPatchFlag(node) {
  1712. const flag = node.patchFlag;
  1713. return flag ? parseInt(flag, 10) : void 0;
  1714. }
  1715. function createTransformContext(root, {
  1716. filename = "",
  1717. prefixIdentifiers = false,
  1718. hoistStatic: hoistStatic2 = false,
  1719. cacheHandlers = false,
  1720. nodeTransforms = [],
  1721. directiveTransforms = {},
  1722. transformHoist = null,
  1723. isBuiltInComponent = shared.NOOP,
  1724. isCustomElement = shared.NOOP,
  1725. expressionPlugins = [],
  1726. scopeId = null,
  1727. slotted = true,
  1728. ssr = false,
  1729. inSSR = false,
  1730. ssrCssVars = ``,
  1731. bindingMetadata = shared.EMPTY_OBJ,
  1732. inline = false,
  1733. isTS = false,
  1734. onError = defaultOnError,
  1735. onWarn = defaultOnWarn,
  1736. compatConfig
  1737. }) {
  1738. const nameMatch = filename.replace(/\?.*$/, "").match(/([^/\\]+)\.\w+$/);
  1739. const context = {
  1740. // options
  1741. selfName: nameMatch && shared.capitalize(shared.camelize(nameMatch[1])),
  1742. prefixIdentifiers,
  1743. hoistStatic: hoistStatic2,
  1744. cacheHandlers,
  1745. nodeTransforms,
  1746. directiveTransforms,
  1747. transformHoist,
  1748. isBuiltInComponent,
  1749. isCustomElement,
  1750. expressionPlugins,
  1751. scopeId,
  1752. slotted,
  1753. ssr,
  1754. inSSR,
  1755. ssrCssVars,
  1756. bindingMetadata,
  1757. inline,
  1758. isTS,
  1759. onError,
  1760. onWarn,
  1761. compatConfig,
  1762. // state
  1763. root,
  1764. helpers: /* @__PURE__ */ new Map(),
  1765. components: /* @__PURE__ */ new Set(),
  1766. directives: /* @__PURE__ */ new Set(),
  1767. hoists: [],
  1768. imports: [],
  1769. constantCache: /* @__PURE__ */ new Map(),
  1770. temps: 0,
  1771. cached: 0,
  1772. identifiers: /* @__PURE__ */ Object.create(null),
  1773. scopes: {
  1774. vFor: 0,
  1775. vSlot: 0,
  1776. vPre: 0,
  1777. vOnce: 0
  1778. },
  1779. parent: null,
  1780. currentNode: root,
  1781. childIndex: 0,
  1782. inVOnce: false,
  1783. // methods
  1784. helper(name) {
  1785. const count = context.helpers.get(name) || 0;
  1786. context.helpers.set(name, count + 1);
  1787. return name;
  1788. },
  1789. removeHelper(name) {
  1790. const count = context.helpers.get(name);
  1791. if (count) {
  1792. const currentCount = count - 1;
  1793. if (!currentCount) {
  1794. context.helpers.delete(name);
  1795. } else {
  1796. context.helpers.set(name, currentCount);
  1797. }
  1798. }
  1799. },
  1800. helperString(name) {
  1801. return `_${helperNameMap[context.helper(name)]}`;
  1802. },
  1803. replaceNode(node) {
  1804. context.parent.children[context.childIndex] = context.currentNode = node;
  1805. },
  1806. removeNode(node) {
  1807. const list = context.parent.children;
  1808. const removalIndex = node ? list.indexOf(node) : context.currentNode ? context.childIndex : -1;
  1809. if (!node || node === context.currentNode) {
  1810. context.currentNode = null;
  1811. context.onNodeRemoved();
  1812. } else {
  1813. if (context.childIndex > removalIndex) {
  1814. context.childIndex--;
  1815. context.onNodeRemoved();
  1816. }
  1817. }
  1818. context.parent.children.splice(removalIndex, 1);
  1819. },
  1820. onNodeRemoved: () => {
  1821. },
  1822. addIdentifiers(exp) {
  1823. {
  1824. if (shared.isString(exp)) {
  1825. addId(exp);
  1826. } else if (exp.identifiers) {
  1827. exp.identifiers.forEach(addId);
  1828. } else if (exp.type === 4) {
  1829. addId(exp.content);
  1830. }
  1831. }
  1832. },
  1833. removeIdentifiers(exp) {
  1834. {
  1835. if (shared.isString(exp)) {
  1836. removeId(exp);
  1837. } else if (exp.identifiers) {
  1838. exp.identifiers.forEach(removeId);
  1839. } else if (exp.type === 4) {
  1840. removeId(exp.content);
  1841. }
  1842. }
  1843. },
  1844. hoist(exp) {
  1845. if (shared.isString(exp))
  1846. exp = createSimpleExpression(exp);
  1847. context.hoists.push(exp);
  1848. const identifier = createSimpleExpression(
  1849. `_hoisted_${context.hoists.length}`,
  1850. false,
  1851. exp.loc,
  1852. 2
  1853. );
  1854. identifier.hoisted = exp;
  1855. return identifier;
  1856. },
  1857. cache(exp, isVNode = false) {
  1858. return createCacheExpression(context.cached++, exp, isVNode);
  1859. }
  1860. };
  1861. {
  1862. context.filters = /* @__PURE__ */ new Set();
  1863. }
  1864. function addId(id) {
  1865. const { identifiers } = context;
  1866. if (identifiers[id] === void 0) {
  1867. identifiers[id] = 0;
  1868. }
  1869. identifiers[id]++;
  1870. }
  1871. function removeId(id) {
  1872. context.identifiers[id]--;
  1873. }
  1874. return context;
  1875. }
  1876. function transform(root, options) {
  1877. const context = createTransformContext(root, options);
  1878. traverseNode(root, context);
  1879. if (options.hoistStatic) {
  1880. hoistStatic(root, context);
  1881. }
  1882. if (!options.ssr) {
  1883. createRootCodegen(root, context);
  1884. }
  1885. root.helpers = /* @__PURE__ */ new Set([...context.helpers.keys()]);
  1886. root.components = [...context.components];
  1887. root.directives = [...context.directives];
  1888. root.imports = context.imports;
  1889. root.hoists = context.hoists;
  1890. root.temps = context.temps;
  1891. root.cached = context.cached;
  1892. {
  1893. root.filters = [...context.filters];
  1894. }
  1895. }
  1896. function createRootCodegen(root, context) {
  1897. const { helper } = context;
  1898. const { children } = root;
  1899. if (children.length === 1) {
  1900. const child = children[0];
  1901. if (isSingleElementRoot(root, child) && child.codegenNode) {
  1902. const codegenNode = child.codegenNode;
  1903. if (codegenNode.type === 13) {
  1904. convertToBlock(codegenNode, context);
  1905. }
  1906. root.codegenNode = codegenNode;
  1907. } else {
  1908. root.codegenNode = child;
  1909. }
  1910. } else if (children.length > 1) {
  1911. let patchFlag = 64;
  1912. shared.PatchFlagNames[64];
  1913. root.codegenNode = createVNodeCall(
  1914. context,
  1915. helper(FRAGMENT),
  1916. void 0,
  1917. root.children,
  1918. patchFlag + (``),
  1919. void 0,
  1920. void 0,
  1921. true,
  1922. void 0,
  1923. false
  1924. /* isComponent */
  1925. );
  1926. } else ;
  1927. }
  1928. function traverseChildren(parent, context) {
  1929. let i = 0;
  1930. const nodeRemoved = () => {
  1931. i--;
  1932. };
  1933. for (; i < parent.children.length; i++) {
  1934. const child = parent.children[i];
  1935. if (shared.isString(child))
  1936. continue;
  1937. context.parent = parent;
  1938. context.childIndex = i;
  1939. context.onNodeRemoved = nodeRemoved;
  1940. traverseNode(child, context);
  1941. }
  1942. }
  1943. function traverseNode(node, context) {
  1944. context.currentNode = node;
  1945. const { nodeTransforms } = context;
  1946. const exitFns = [];
  1947. for (let i2 = 0; i2 < nodeTransforms.length; i2++) {
  1948. const onExit = nodeTransforms[i2](node, context);
  1949. if (onExit) {
  1950. if (shared.isArray(onExit)) {
  1951. exitFns.push(...onExit);
  1952. } else {
  1953. exitFns.push(onExit);
  1954. }
  1955. }
  1956. if (!context.currentNode) {
  1957. return;
  1958. } else {
  1959. node = context.currentNode;
  1960. }
  1961. }
  1962. switch (node.type) {
  1963. case 3:
  1964. if (!context.ssr) {
  1965. context.helper(CREATE_COMMENT);
  1966. }
  1967. break;
  1968. case 5:
  1969. if (!context.ssr) {
  1970. context.helper(TO_DISPLAY_STRING);
  1971. }
  1972. break;
  1973. case 9:
  1974. for (let i2 = 0; i2 < node.branches.length; i2++) {
  1975. traverseNode(node.branches[i2], context);
  1976. }
  1977. break;
  1978. case 10:
  1979. case 11:
  1980. case 1:
  1981. case 0:
  1982. traverseChildren(node, context);
  1983. break;
  1984. }
  1985. context.currentNode = node;
  1986. let i = exitFns.length;
  1987. while (i--) {
  1988. exitFns[i]();
  1989. }
  1990. }
  1991. function createStructuralDirectiveTransform(name, fn) {
  1992. const matches = shared.isString(name) ? (n) => n === name : (n) => name.test(n);
  1993. return (node, context) => {
  1994. if (node.type === 1) {
  1995. const { props } = node;
  1996. if (node.tagType === 3 && props.some(isVSlot)) {
  1997. return;
  1998. }
  1999. const exitFns = [];
  2000. for (let i = 0; i < props.length; i++) {
  2001. const prop = props[i];
  2002. if (prop.type === 7 && matches(prop.name)) {
  2003. props.splice(i, 1);
  2004. i--;
  2005. const onExit = fn(node, prop, context);
  2006. if (onExit)
  2007. exitFns.push(onExit);
  2008. }
  2009. }
  2010. return exitFns;
  2011. }
  2012. };
  2013. }
  2014. const PURE_ANNOTATION = `/*#__PURE__*/`;
  2015. const aliasHelper = (s) => `${helperNameMap[s]}: _${helperNameMap[s]}`;
  2016. function createCodegenContext(ast, {
  2017. mode = "function",
  2018. prefixIdentifiers = mode === "module",
  2019. sourceMap = false,
  2020. filename = `template.vue.html`,
  2021. scopeId = null,
  2022. optimizeImports = false,
  2023. runtimeGlobalName = `Vue`,
  2024. runtimeModuleName = `vue`,
  2025. ssrRuntimeModuleName = "vue/server-renderer",
  2026. ssr = false,
  2027. isTS = false,
  2028. inSSR = false
  2029. }) {
  2030. const context = {
  2031. mode,
  2032. prefixIdentifiers,
  2033. sourceMap,
  2034. filename,
  2035. scopeId,
  2036. optimizeImports,
  2037. runtimeGlobalName,
  2038. runtimeModuleName,
  2039. ssrRuntimeModuleName,
  2040. ssr,
  2041. isTS,
  2042. inSSR,
  2043. source: ast.loc.source,
  2044. code: ``,
  2045. column: 1,
  2046. line: 1,
  2047. offset: 0,
  2048. indentLevel: 0,
  2049. pure: false,
  2050. map: void 0,
  2051. helper(key) {
  2052. return `_${helperNameMap[key]}`;
  2053. },
  2054. push(code, node) {
  2055. context.code += code;
  2056. if (context.map) {
  2057. if (node) {
  2058. let name;
  2059. if (node.type === 4 && !node.isStatic) {
  2060. const content = node.content.replace(/^_ctx\./, "");
  2061. if (content !== node.content && isSimpleIdentifier(content)) {
  2062. name = content;
  2063. }
  2064. }
  2065. addMapping(node.loc.start, name);
  2066. }
  2067. advancePositionWithMutation(context, code);
  2068. if (node && node.loc !== locStub) {
  2069. addMapping(node.loc.end);
  2070. }
  2071. }
  2072. },
  2073. indent() {
  2074. newline(++context.indentLevel);
  2075. },
  2076. deindent(withoutNewLine = false) {
  2077. if (withoutNewLine) {
  2078. --context.indentLevel;
  2079. } else {
  2080. newline(--context.indentLevel);
  2081. }
  2082. },
  2083. newline() {
  2084. newline(context.indentLevel);
  2085. }
  2086. };
  2087. function newline(n) {
  2088. context.push("\n" + ` `.repeat(n));
  2089. }
  2090. function addMapping(loc, name) {
  2091. context.map.addMapping({
  2092. name,
  2093. source: context.filename,
  2094. original: {
  2095. line: loc.line,
  2096. column: loc.column - 1
  2097. // source-map column is 0 based
  2098. },
  2099. generated: {
  2100. line: context.line,
  2101. column: context.column - 1
  2102. }
  2103. });
  2104. }
  2105. if (sourceMap) {
  2106. context.map = new sourceMapJs.SourceMapGenerator();
  2107. context.map.setSourceContent(filename, context.source);
  2108. }
  2109. return context;
  2110. }
  2111. function generate(ast, options = {}) {
  2112. const context = createCodegenContext(ast, options);
  2113. if (options.onContextCreated)
  2114. options.onContextCreated(context);
  2115. const {
  2116. mode,
  2117. push,
  2118. prefixIdentifiers,
  2119. indent,
  2120. deindent,
  2121. newline,
  2122. scopeId,
  2123. ssr
  2124. } = context;
  2125. const helpers = Array.from(ast.helpers);
  2126. const hasHelpers = helpers.length > 0;
  2127. const useWithBlock = !prefixIdentifiers && mode !== "module";
  2128. const genScopeId = scopeId != null && mode === "module";
  2129. const isSetupInlined = !!options.inline;
  2130. const preambleContext = isSetupInlined ? createCodegenContext(ast, options) : context;
  2131. if (mode === "module") {
  2132. genModulePreamble(ast, preambleContext, genScopeId, isSetupInlined);
  2133. } else {
  2134. genFunctionPreamble(ast, preambleContext);
  2135. }
  2136. const functionName = ssr ? `ssrRender` : `render`;
  2137. const args = ssr ? ["_ctx", "_push", "_parent", "_attrs"] : ["_ctx", "_cache"];
  2138. if (options.bindingMetadata && !options.inline) {
  2139. args.push("$props", "$setup", "$data", "$options");
  2140. }
  2141. const signature = options.isTS ? args.map((arg) => `${arg}: any`).join(",") : args.join(", ");
  2142. if (isSetupInlined) {
  2143. push(`(${signature}) => {`);
  2144. } else {
  2145. push(`function ${functionName}(${signature}) {`);
  2146. }
  2147. indent();
  2148. if (useWithBlock) {
  2149. push(`with (_ctx) {`);
  2150. indent();
  2151. if (hasHelpers) {
  2152. push(`const { ${helpers.map(aliasHelper).join(", ")} } = _Vue`);
  2153. push(`
  2154. `);
  2155. newline();
  2156. }
  2157. }
  2158. if (ast.components.length) {
  2159. genAssets(ast.components, "component", context);
  2160. if (ast.directives.length || ast.temps > 0) {
  2161. newline();
  2162. }
  2163. }
  2164. if (ast.directives.length) {
  2165. genAssets(ast.directives, "directive", context);
  2166. if (ast.temps > 0) {
  2167. newline();
  2168. }
  2169. }
  2170. if (ast.filters && ast.filters.length) {
  2171. newline();
  2172. genAssets(ast.filters, "filter", context);
  2173. newline();
  2174. }
  2175. if (ast.temps > 0) {
  2176. push(`let `);
  2177. for (let i = 0; i < ast.temps; i++) {
  2178. push(`${i > 0 ? `, ` : ``}_temp${i}`);
  2179. }
  2180. }
  2181. if (ast.components.length || ast.directives.length || ast.temps) {
  2182. push(`
  2183. `);
  2184. newline();
  2185. }
  2186. if (!ssr) {
  2187. push(`return `);
  2188. }
  2189. if (ast.codegenNode) {
  2190. genNode(ast.codegenNode, context);
  2191. } else {
  2192. push(`null`);
  2193. }
  2194. if (useWithBlock) {
  2195. deindent();
  2196. push(`}`);
  2197. }
  2198. deindent();
  2199. push(`}`);
  2200. return {
  2201. ast,
  2202. code: context.code,
  2203. preamble: isSetupInlined ? preambleContext.code : ``,
  2204. // SourceMapGenerator does have toJSON() method but it's not in the types
  2205. map: context.map ? context.map.toJSON() : void 0
  2206. };
  2207. }
  2208. function genFunctionPreamble(ast, context) {
  2209. const {
  2210. ssr,
  2211. prefixIdentifiers,
  2212. push,
  2213. newline,
  2214. runtimeModuleName,
  2215. runtimeGlobalName,
  2216. ssrRuntimeModuleName
  2217. } = context;
  2218. const VueBinding = ssr ? `require(${JSON.stringify(runtimeModuleName)})` : runtimeGlobalName;
  2219. const helpers = Array.from(ast.helpers);
  2220. if (helpers.length > 0) {
  2221. if (prefixIdentifiers) {
  2222. push(`const { ${helpers.map(aliasHelper).join(", ")} } = ${VueBinding}
  2223. `);
  2224. } else {
  2225. push(`const _Vue = ${VueBinding}
  2226. `);
  2227. if (ast.hoists.length) {
  2228. const staticHelpers = [
  2229. CREATE_VNODE,
  2230. CREATE_ELEMENT_VNODE,
  2231. CREATE_COMMENT,
  2232. CREATE_TEXT,
  2233. CREATE_STATIC
  2234. ].filter((helper) => helpers.includes(helper)).map(aliasHelper).join(", ");
  2235. push(`const { ${staticHelpers} } = _Vue
  2236. `);
  2237. }
  2238. }
  2239. }
  2240. if (ast.ssrHelpers && ast.ssrHelpers.length) {
  2241. push(
  2242. `const { ${ast.ssrHelpers.map(aliasHelper).join(", ")} } = require("${ssrRuntimeModuleName}")
  2243. `
  2244. );
  2245. }
  2246. genHoists(ast.hoists, context);
  2247. newline();
  2248. push(`return `);
  2249. }
  2250. function genModulePreamble(ast, context, genScopeId, inline) {
  2251. const {
  2252. push,
  2253. newline,
  2254. optimizeImports,
  2255. runtimeModuleName,
  2256. ssrRuntimeModuleName
  2257. } = context;
  2258. if (genScopeId && ast.hoists.length) {
  2259. ast.helpers.add(PUSH_SCOPE_ID);
  2260. ast.helpers.add(POP_SCOPE_ID);
  2261. }
  2262. if (ast.helpers.size) {
  2263. const helpers = Array.from(ast.helpers);
  2264. if (optimizeImports) {
  2265. push(
  2266. `import { ${helpers.map((s) => helperNameMap[s]).join(", ")} } from ${JSON.stringify(runtimeModuleName)}
  2267. `
  2268. );
  2269. push(
  2270. `
  2271. // Binding optimization for webpack code-split
  2272. const ${helpers.map((s) => `_${helperNameMap[s]} = ${helperNameMap[s]}`).join(", ")}
  2273. `
  2274. );
  2275. } else {
  2276. push(
  2277. `import { ${helpers.map((s) => `${helperNameMap[s]} as _${helperNameMap[s]}`).join(", ")} } from ${JSON.stringify(runtimeModuleName)}
  2278. `
  2279. );
  2280. }
  2281. }
  2282. if (ast.ssrHelpers && ast.ssrHelpers.length) {
  2283. push(
  2284. `import { ${ast.ssrHelpers.map((s) => `${helperNameMap[s]} as _${helperNameMap[s]}`).join(", ")} } from "${ssrRuntimeModuleName}"
  2285. `
  2286. );
  2287. }
  2288. if (ast.imports.length) {
  2289. genImports(ast.imports, context);
  2290. newline();
  2291. }
  2292. genHoists(ast.hoists, context);
  2293. newline();
  2294. if (!inline) {
  2295. push(`export `);
  2296. }
  2297. }
  2298. function genAssets(assets, type, { helper, push, newline, isTS }) {
  2299. const resolver = helper(
  2300. type === "filter" ? RESOLVE_FILTER : type === "component" ? RESOLVE_COMPONENT : RESOLVE_DIRECTIVE
  2301. );
  2302. for (let i = 0; i < assets.length; i++) {
  2303. let id = assets[i];
  2304. const maybeSelfReference = id.endsWith("__self");
  2305. if (maybeSelfReference) {
  2306. id = id.slice(0, -6);
  2307. }
  2308. push(
  2309. `const ${toValidAssetId(id, type)} = ${resolver}(${JSON.stringify(id)}${maybeSelfReference ? `, true` : ``})${isTS ? `!` : ``}`
  2310. );
  2311. if (i < assets.length - 1) {
  2312. newline();
  2313. }
  2314. }
  2315. }
  2316. function genHoists(hoists, context) {
  2317. if (!hoists.length) {
  2318. return;
  2319. }
  2320. context.pure = true;
  2321. const { push, newline, helper, scopeId, mode } = context;
  2322. const genScopeId = scopeId != null && mode !== "function";
  2323. newline();
  2324. if (genScopeId) {
  2325. push(
  2326. `const _withScopeId = n => (${helper(
  2327. PUSH_SCOPE_ID
  2328. )}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`
  2329. );
  2330. newline();
  2331. }
  2332. for (let i = 0; i < hoists.length; i++) {
  2333. const exp = hoists[i];
  2334. if (exp) {
  2335. const needScopeIdWrapper = genScopeId && exp.type === 13;
  2336. push(
  2337. `const _hoisted_${i + 1} = ${needScopeIdWrapper ? `${PURE_ANNOTATION} _withScopeId(() => ` : ``}`
  2338. );
  2339. genNode(exp, context);
  2340. if (needScopeIdWrapper) {
  2341. push(`)`);
  2342. }
  2343. newline();
  2344. }
  2345. }
  2346. context.pure = false;
  2347. }
  2348. function genImports(importsOptions, context) {
  2349. if (!importsOptions.length) {
  2350. return;
  2351. }
  2352. importsOptions.forEach((imports) => {
  2353. context.push(`import `);
  2354. genNode(imports.exp, context);
  2355. context.push(` from '${imports.path}'`);
  2356. context.newline();
  2357. });
  2358. }
  2359. function isText(n) {
  2360. return shared.isString(n) || n.type === 4 || n.type === 2 || n.type === 5 || n.type === 8;
  2361. }
  2362. function genNodeListAsArray(nodes, context) {
  2363. const multilines = nodes.length > 3 || nodes.some((n) => shared.isArray(n) || !isText(n));
  2364. context.push(`[`);
  2365. multilines && context.indent();
  2366. genNodeList(nodes, context, multilines);
  2367. multilines && context.deindent();
  2368. context.push(`]`);
  2369. }
  2370. function genNodeList(nodes, context, multilines = false, comma = true) {
  2371. const { push, newline } = context;
  2372. for (let i = 0; i < nodes.length; i++) {
  2373. const node = nodes[i];
  2374. if (shared.isString(node)) {
  2375. push(node);
  2376. } else if (shared.isArray(node)) {
  2377. genNodeListAsArray(node, context);
  2378. } else {
  2379. genNode(node, context);
  2380. }
  2381. if (i < nodes.length - 1) {
  2382. if (multilines) {
  2383. comma && push(",");
  2384. newline();
  2385. } else {
  2386. comma && push(", ");
  2387. }
  2388. }
  2389. }
  2390. }
  2391. function genNode(node, context) {
  2392. if (shared.isString(node)) {
  2393. context.push(node);
  2394. return;
  2395. }
  2396. if (shared.isSymbol(node)) {
  2397. context.push(context.helper(node));
  2398. return;
  2399. }
  2400. switch (node.type) {
  2401. case 1:
  2402. case 9:
  2403. case 11:
  2404. genNode(node.codegenNode, context);
  2405. break;
  2406. case 2:
  2407. genText(node, context);
  2408. break;
  2409. case 4:
  2410. genExpression(node, context);
  2411. break;
  2412. case 5:
  2413. genInterpolation(node, context);
  2414. break;
  2415. case 12:
  2416. genNode(node.codegenNode, context);
  2417. break;
  2418. case 8:
  2419. genCompoundExpression(node, context);
  2420. break;
  2421. case 3:
  2422. genComment(node, context);
  2423. break;
  2424. case 13:
  2425. genVNodeCall(node, context);
  2426. break;
  2427. case 14:
  2428. genCallExpression(node, context);
  2429. break;
  2430. case 15:
  2431. genObjectExpression(node, context);
  2432. break;
  2433. case 17:
  2434. genArrayExpression(node, context);
  2435. break;
  2436. case 18:
  2437. genFunctionExpression(node, context);
  2438. break;
  2439. case 19:
  2440. genConditionalExpression(node, context);
  2441. break;
  2442. case 20:
  2443. genCacheExpression(node, context);
  2444. break;
  2445. case 21:
  2446. genNodeList(node.body, context, true, false);
  2447. break;
  2448. case 22:
  2449. genTemplateLiteral(node, context);
  2450. break;
  2451. case 23:
  2452. genIfStatement(node, context);
  2453. break;
  2454. case 24:
  2455. genAssignmentExpression(node, context);
  2456. break;
  2457. case 25:
  2458. genSequenceExpression(node, context);
  2459. break;
  2460. case 26:
  2461. genReturnStatement(node, context);
  2462. break;
  2463. }
  2464. }
  2465. function genText(node, context) {
  2466. context.push(JSON.stringify(node.content), node);
  2467. }
  2468. function genExpression(node, context) {
  2469. const { content, isStatic } = node;
  2470. context.push(isStatic ? JSON.stringify(content) : content, node);
  2471. }
  2472. function genInterpolation(node, context) {
  2473. const { push, helper, pure } = context;
  2474. if (pure)
  2475. push(PURE_ANNOTATION);
  2476. push(`${helper(TO_DISPLAY_STRING)}(`);
  2477. genNode(node.content, context);
  2478. push(`)`);
  2479. }
  2480. function genCompoundExpression(node, context) {
  2481. for (let i = 0; i < node.children.length; i++) {
  2482. const child = node.children[i];
  2483. if (shared.isString(child)) {
  2484. context.push(child);
  2485. } else {
  2486. genNode(child, context);
  2487. }
  2488. }
  2489. }
  2490. function genExpressionAsPropertyKey(node, context) {
  2491. const { push } = context;
  2492. if (node.type === 8) {
  2493. push(`[`);
  2494. genCompoundExpression(node, context);
  2495. push(`]`);
  2496. } else if (node.isStatic) {
  2497. const text = isSimpleIdentifier(node.content) ? node.content : JSON.stringify(node.content);
  2498. push(text, node);
  2499. } else {
  2500. push(`[${node.content}]`, node);
  2501. }
  2502. }
  2503. function genComment(node, context) {
  2504. const { push, helper, pure } = context;
  2505. if (pure) {
  2506. push(PURE_ANNOTATION);
  2507. }
  2508. push(`${helper(CREATE_COMMENT)}(${JSON.stringify(node.content)})`, node);
  2509. }
  2510. function genVNodeCall(node, context) {
  2511. const { push, helper, pure } = context;
  2512. const {
  2513. tag,
  2514. props,
  2515. children,
  2516. patchFlag,
  2517. dynamicProps,
  2518. directives,
  2519. isBlock,
  2520. disableTracking,
  2521. isComponent
  2522. } = node;
  2523. if (directives) {
  2524. push(helper(WITH_DIRECTIVES) + `(`);
  2525. }
  2526. if (isBlock) {
  2527. push(`(${helper(OPEN_BLOCK)}(${disableTracking ? `true` : ``}), `);
  2528. }
  2529. if (pure) {
  2530. push(PURE_ANNOTATION);
  2531. }
  2532. const callHelper = isBlock ? getVNodeBlockHelper(context.inSSR, isComponent) : getVNodeHelper(context.inSSR, isComponent);
  2533. push(helper(callHelper) + `(`, node);
  2534. genNodeList(
  2535. genNullableArgs([tag, props, children, patchFlag, dynamicProps]),
  2536. context
  2537. );
  2538. push(`)`);
  2539. if (isBlock) {
  2540. push(`)`);
  2541. }
  2542. if (directives) {
  2543. push(`, `);
  2544. genNode(directives, context);
  2545. push(`)`);
  2546. }
  2547. }
  2548. function genNullableArgs(args) {
  2549. let i = args.length;
  2550. while (i--) {
  2551. if (args[i] != null)
  2552. break;
  2553. }
  2554. return args.slice(0, i + 1).map((arg) => arg || `null`);
  2555. }
  2556. function genCallExpression(node, context) {
  2557. const { push, helper, pure } = context;
  2558. const callee = shared.isString(node.callee) ? node.callee : helper(node.callee);
  2559. if (pure) {
  2560. push(PURE_ANNOTATION);
  2561. }
  2562. push(callee + `(`, node);
  2563. genNodeList(node.arguments, context);
  2564. push(`)`);
  2565. }
  2566. function genObjectExpression(node, context) {
  2567. const { push, indent, deindent, newline } = context;
  2568. const { properties } = node;
  2569. if (!properties.length) {
  2570. push(`{}`, node);
  2571. return;
  2572. }
  2573. const multilines = properties.length > 1 || properties.some((p) => p.value.type !== 4);
  2574. push(multilines ? `{` : `{ `);
  2575. multilines && indent();
  2576. for (let i = 0; i < properties.length; i++) {
  2577. const { key, value } = properties[i];
  2578. genExpressionAsPropertyKey(key, context);
  2579. push(`: `);
  2580. genNode(value, context);
  2581. if (i < properties.length - 1) {
  2582. push(`,`);
  2583. newline();
  2584. }
  2585. }
  2586. multilines && deindent();
  2587. push(multilines ? `}` : ` }`);
  2588. }
  2589. function genArrayExpression(node, context) {
  2590. genNodeListAsArray(node.elements, context);
  2591. }
  2592. function genFunctionExpression(node, context) {
  2593. const { push, indent, deindent } = context;
  2594. const { params, returns, body, newline, isSlot } = node;
  2595. if (isSlot) {
  2596. push(`_${helperNameMap[WITH_CTX]}(`);
  2597. }
  2598. push(`(`, node);
  2599. if (shared.isArray(params)) {
  2600. genNodeList(params, context);
  2601. } else if (params) {
  2602. genNode(params, context);
  2603. }
  2604. push(`) => `);
  2605. if (newline || body) {
  2606. push(`{`);
  2607. indent();
  2608. }
  2609. if (returns) {
  2610. if (newline) {
  2611. push(`return `);
  2612. }
  2613. if (shared.isArray(returns)) {
  2614. genNodeListAsArray(returns, context);
  2615. } else {
  2616. genNode(returns, context);
  2617. }
  2618. } else if (body) {
  2619. genNode(body, context);
  2620. }
  2621. if (newline || body) {
  2622. deindent();
  2623. push(`}`);
  2624. }
  2625. if (isSlot) {
  2626. if (node.isNonScopedSlot) {
  2627. push(`, undefined, true`);
  2628. }
  2629. push(`)`);
  2630. }
  2631. }
  2632. function genConditionalExpression(node, context) {
  2633. const { test, consequent, alternate, newline: needNewline } = node;
  2634. const { push, indent, deindent, newline } = context;
  2635. if (test.type === 4) {
  2636. const needsParens = !isSimpleIdentifier(test.content);
  2637. needsParens && push(`(`);
  2638. genExpression(test, context);
  2639. needsParens && push(`)`);
  2640. } else {
  2641. push(`(`);
  2642. genNode(test, context);
  2643. push(`)`);
  2644. }
  2645. needNewline && indent();
  2646. context.indentLevel++;
  2647. needNewline || push(` `);
  2648. push(`? `);
  2649. genNode(consequent, context);
  2650. context.indentLevel--;
  2651. needNewline && newline();
  2652. needNewline || push(` `);
  2653. push(`: `);
  2654. const isNested = alternate.type === 19;
  2655. if (!isNested) {
  2656. context.indentLevel++;
  2657. }
  2658. genNode(alternate, context);
  2659. if (!isNested) {
  2660. context.indentLevel--;
  2661. }
  2662. needNewline && deindent(
  2663. true
  2664. /* without newline */
  2665. );
  2666. }
  2667. function genCacheExpression(node, context) {
  2668. const { push, helper, indent, deindent, newline } = context;
  2669. push(`_cache[${node.index}] || (`);
  2670. if (node.isVNode) {
  2671. indent();
  2672. push(`${helper(SET_BLOCK_TRACKING)}(-1),`);
  2673. newline();
  2674. }
  2675. push(`_cache[${node.index}] = `);
  2676. genNode(node.value, context);
  2677. if (node.isVNode) {
  2678. push(`,`);
  2679. newline();
  2680. push(`${helper(SET_BLOCK_TRACKING)}(1),`);
  2681. newline();
  2682. push(`_cache[${node.index}]`);
  2683. deindent();
  2684. }
  2685. push(`)`);
  2686. }
  2687. function genTemplateLiteral(node, context) {
  2688. const { push, indent, deindent } = context;
  2689. push("`");
  2690. const l = node.elements.length;
  2691. const multilines = l > 3;
  2692. for (let i = 0; i < l; i++) {
  2693. const e = node.elements[i];
  2694. if (shared.isString(e)) {
  2695. push(e.replace(/(`|\$|\\)/g, "\\$1"));
  2696. } else {
  2697. push("${");
  2698. if (multilines)
  2699. indent();
  2700. genNode(e, context);
  2701. if (multilines)
  2702. deindent();
  2703. push("}");
  2704. }
  2705. }
  2706. push("`");
  2707. }
  2708. function genIfStatement(node, context) {
  2709. const { push, indent, deindent } = context;
  2710. const { test, consequent, alternate } = node;
  2711. push(`if (`);
  2712. genNode(test, context);
  2713. push(`) {`);
  2714. indent();
  2715. genNode(consequent, context);
  2716. deindent();
  2717. push(`}`);
  2718. if (alternate) {
  2719. push(` else `);
  2720. if (alternate.type === 23) {
  2721. genIfStatement(alternate, context);
  2722. } else {
  2723. push(`{`);
  2724. indent();
  2725. genNode(alternate, context);
  2726. deindent();
  2727. push(`}`);
  2728. }
  2729. }
  2730. }
  2731. function genAssignmentExpression(node, context) {
  2732. genNode(node.left, context);
  2733. context.push(` = `);
  2734. genNode(node.right, context);
  2735. }
  2736. function genSequenceExpression(node, context) {
  2737. context.push(`(`);
  2738. genNodeList(node.expressions, context);
  2739. context.push(`)`);
  2740. }
  2741. function genReturnStatement({ returns }, context) {
  2742. context.push(`return `);
  2743. if (shared.isArray(returns)) {
  2744. genNodeListAsArray(returns, context);
  2745. } else {
  2746. genNode(returns, context);
  2747. }
  2748. }
  2749. function walkIdentifiers(root, onIdentifier, includeAll = false, parentStack = [], knownIds = /* @__PURE__ */ Object.create(null)) {
  2750. const rootExp = root.type === "Program" && root.body[0].type === "ExpressionStatement" && root.body[0].expression;
  2751. estreeWalker.walk(root, {
  2752. enter(node, parent) {
  2753. parent && parentStack.push(parent);
  2754. if (parent && parent.type.startsWith("TS") && !TS_NODE_TYPES.includes(parent.type)) {
  2755. return this.skip();
  2756. }
  2757. if (node.type === "Identifier") {
  2758. const isLocal = !!knownIds[node.name];
  2759. const isRefed = isReferencedIdentifier(node, parent, parentStack);
  2760. if (includeAll || isRefed && !isLocal) {
  2761. onIdentifier(node, parent, parentStack, isRefed, isLocal);
  2762. }
  2763. } else if (node.type === "ObjectProperty" && parent.type === "ObjectPattern") {
  2764. node.inPattern = true;
  2765. } else if (isFunctionType(node)) {
  2766. walkFunctionParams(node, (id) => markScopeIdentifier(node, id, knownIds));
  2767. } else if (node.type === "BlockStatement") {
  2768. walkBlockDeclarations(
  2769. node,
  2770. (id) => markScopeIdentifier(node, id, knownIds)
  2771. );
  2772. }
  2773. },
  2774. leave(node, parent) {
  2775. parent && parentStack.pop();
  2776. if (node !== rootExp && node.scopeIds) {
  2777. for (const id of node.scopeIds) {
  2778. knownIds[id]--;
  2779. if (knownIds[id] === 0) {
  2780. delete knownIds[id];
  2781. }
  2782. }
  2783. }
  2784. }
  2785. });
  2786. }
  2787. function isReferencedIdentifier(id, parent, parentStack) {
  2788. if (!parent) {
  2789. return true;
  2790. }
  2791. if (id.name === "arguments") {
  2792. return false;
  2793. }
  2794. if (isReferenced(id, parent)) {
  2795. return true;
  2796. }
  2797. switch (parent.type) {
  2798. case "AssignmentExpression":
  2799. case "AssignmentPattern":
  2800. return true;
  2801. case "ObjectPattern":
  2802. case "ArrayPattern":
  2803. return isInDestructureAssignment(parent, parentStack);
  2804. }
  2805. return false;
  2806. }
  2807. function isInDestructureAssignment(parent, parentStack) {
  2808. if (parent && (parent.type === "ObjectProperty" || parent.type === "ArrayPattern")) {
  2809. let i = parentStack.length;
  2810. while (i--) {
  2811. const p = parentStack[i];
  2812. if (p.type === "AssignmentExpression") {
  2813. return true;
  2814. } else if (p.type !== "ObjectProperty" && !p.type.endsWith("Pattern")) {
  2815. break;
  2816. }
  2817. }
  2818. }
  2819. return false;
  2820. }
  2821. function walkFunctionParams(node, onIdent) {
  2822. for (const p of node.params) {
  2823. for (const id of extractIdentifiers(p)) {
  2824. onIdent(id);
  2825. }
  2826. }
  2827. }
  2828. function walkBlockDeclarations(block, onIdent) {
  2829. for (const stmt of block.body) {
  2830. if (stmt.type === "VariableDeclaration") {
  2831. if (stmt.declare)
  2832. continue;
  2833. for (const decl of stmt.declarations) {
  2834. for (const id of extractIdentifiers(decl.id)) {
  2835. onIdent(id);
  2836. }
  2837. }
  2838. } else if (stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") {
  2839. if (stmt.declare || !stmt.id)
  2840. continue;
  2841. onIdent(stmt.id);
  2842. }
  2843. }
  2844. }
  2845. function extractIdentifiers(param, nodes = []) {
  2846. switch (param.type) {
  2847. case "Identifier":
  2848. nodes.push(param);
  2849. break;
  2850. case "MemberExpression":
  2851. let object = param;
  2852. while (object.type === "MemberExpression") {
  2853. object = object.object;
  2854. }
  2855. nodes.push(object);
  2856. break;
  2857. case "ObjectPattern":
  2858. for (const prop of param.properties) {
  2859. if (prop.type === "RestElement") {
  2860. extractIdentifiers(prop.argument, nodes);
  2861. } else {
  2862. extractIdentifiers(prop.value, nodes);
  2863. }
  2864. }
  2865. break;
  2866. case "ArrayPattern":
  2867. param.elements.forEach((element) => {
  2868. if (element)
  2869. extractIdentifiers(element, nodes);
  2870. });
  2871. break;
  2872. case "RestElement":
  2873. extractIdentifiers(param.argument, nodes);
  2874. break;
  2875. case "AssignmentPattern":
  2876. extractIdentifiers(param.left, nodes);
  2877. break;
  2878. }
  2879. return nodes;
  2880. }
  2881. function markScopeIdentifier(node, child, knownIds) {
  2882. const { name } = child;
  2883. if (node.scopeIds && node.scopeIds.has(name)) {
  2884. return;
  2885. }
  2886. if (name in knownIds) {
  2887. knownIds[name]++;
  2888. } else {
  2889. knownIds[name] = 1;
  2890. }
  2891. (node.scopeIds || (node.scopeIds = /* @__PURE__ */ new Set())).add(name);
  2892. }
  2893. const isFunctionType = (node) => {
  2894. return /Function(?:Expression|Declaration)$|Method$/.test(node.type);
  2895. };
  2896. const isStaticProperty = (node) => node && (node.type === "ObjectProperty" || node.type === "ObjectMethod") && !node.computed;
  2897. const isStaticPropertyKey = (node, parent) => isStaticProperty(parent) && parent.key === node;
  2898. function isReferenced(node, parent, grandparent) {
  2899. switch (parent.type) {
  2900. case "MemberExpression":
  2901. case "OptionalMemberExpression":
  2902. if (parent.property === node) {
  2903. return !!parent.computed;
  2904. }
  2905. return parent.object === node;
  2906. case "JSXMemberExpression":
  2907. return parent.object === node;
  2908. case "VariableDeclarator":
  2909. return parent.init === node;
  2910. case "ArrowFunctionExpression":
  2911. return parent.body === node;
  2912. case "PrivateName":
  2913. return false;
  2914. case "ClassMethod":
  2915. case "ClassPrivateMethod":
  2916. case "ObjectMethod":
  2917. if (parent.key === node) {
  2918. return !!parent.computed;
  2919. }
  2920. return false;
  2921. case "ObjectProperty":
  2922. if (parent.key === node) {
  2923. return !!parent.computed;
  2924. }
  2925. return !grandparent || grandparent.type !== "ObjectPattern";
  2926. case "ClassProperty":
  2927. if (parent.key === node) {
  2928. return !!parent.computed;
  2929. }
  2930. return true;
  2931. case "ClassPrivateProperty":
  2932. return parent.key !== node;
  2933. case "ClassDeclaration":
  2934. case "ClassExpression":
  2935. return parent.superClass === node;
  2936. case "AssignmentExpression":
  2937. return parent.right === node;
  2938. case "AssignmentPattern":
  2939. return parent.right === node;
  2940. case "LabeledStatement":
  2941. return false;
  2942. case "CatchClause":
  2943. return false;
  2944. case "RestElement":
  2945. return false;
  2946. case "BreakStatement":
  2947. case "ContinueStatement":
  2948. return false;
  2949. case "FunctionDeclaration":
  2950. case "FunctionExpression":
  2951. return false;
  2952. case "ExportNamespaceSpecifier":
  2953. case "ExportDefaultSpecifier":
  2954. return false;
  2955. case "ExportSpecifier":
  2956. if (grandparent == null ? void 0 : grandparent.source) {
  2957. return false;
  2958. }
  2959. return parent.local === node;
  2960. case "ImportDefaultSpecifier":
  2961. case "ImportNamespaceSpecifier":
  2962. case "ImportSpecifier":
  2963. return false;
  2964. case "ImportAttribute":
  2965. return false;
  2966. case "JSXAttribute":
  2967. return false;
  2968. case "ObjectPattern":
  2969. case "ArrayPattern":
  2970. return false;
  2971. case "MetaProperty":
  2972. return false;
  2973. case "ObjectTypeProperty":
  2974. return parent.key !== node;
  2975. case "TSEnumMember":
  2976. return parent.id !== node;
  2977. case "TSPropertySignature":
  2978. if (parent.key === node) {
  2979. return !!parent.computed;
  2980. }
  2981. return true;
  2982. }
  2983. return true;
  2984. }
  2985. const TS_NODE_TYPES = [
  2986. "TSAsExpression",
  2987. // foo as number
  2988. "TSTypeAssertion",
  2989. // (<number>foo)
  2990. "TSNonNullExpression",
  2991. // foo!
  2992. "TSInstantiationExpression",
  2993. // foo<string>
  2994. "TSSatisfiesExpression"
  2995. // foo satisfies T
  2996. ];
  2997. const isLiteralWhitelisted = /* @__PURE__ */ shared.makeMap("true,false,null,this");
  2998. const constantBailRE = /\w\s*\(|\.[^\d]/;
  2999. const transformExpression = (node, context) => {
  3000. if (node.type === 5) {
  3001. node.content = processExpression(
  3002. node.content,
  3003. context
  3004. );
  3005. } else if (node.type === 1) {
  3006. for (let i = 0; i < node.props.length; i++) {
  3007. const dir = node.props[i];
  3008. if (dir.type === 7 && dir.name !== "for") {
  3009. const exp = dir.exp;
  3010. const arg = dir.arg;
  3011. if (exp && exp.type === 4 && !(dir.name === "on" && arg)) {
  3012. dir.exp = processExpression(
  3013. exp,
  3014. context,
  3015. // slot args must be processed as function params
  3016. dir.name === "slot"
  3017. );
  3018. }
  3019. if (arg && arg.type === 4 && !arg.isStatic) {
  3020. dir.arg = processExpression(arg, context);
  3021. }
  3022. }
  3023. }
  3024. }
  3025. };
  3026. function processExpression(node, context, asParams = false, asRawStatements = false, localVars = Object.create(context.identifiers)) {
  3027. if (!context.prefixIdentifiers || !node.content.trim()) {
  3028. return node;
  3029. }
  3030. const { inline, bindingMetadata } = context;
  3031. const rewriteIdentifier = (raw, parent, id) => {
  3032. const type = shared.hasOwn(bindingMetadata, raw) && bindingMetadata[raw];
  3033. if (inline) {
  3034. const isAssignmentLVal = parent && parent.type === "AssignmentExpression" && parent.left === id;
  3035. const isUpdateArg = parent && parent.type === "UpdateExpression" && parent.argument === id;
  3036. const isDestructureAssignment = parent && isInDestructureAssignment(parent, parentStack);
  3037. if (isConst(type) || type === "setup-reactive-const" || localVars[raw]) {
  3038. return raw;
  3039. } else if (type === "setup-ref") {
  3040. return `${raw}.value`;
  3041. } else if (type === "setup-maybe-ref") {
  3042. return isAssignmentLVal || isUpdateArg || isDestructureAssignment ? `${raw}.value` : `${context.helperString(UNREF)}(${raw})`;
  3043. } else if (type === "setup-let") {
  3044. if (isAssignmentLVal) {
  3045. const { right: rVal, operator } = parent;
  3046. const rExp = rawExp.slice(rVal.start - 1, rVal.end - 1);
  3047. const rExpString = stringifyExpression(
  3048. processExpression(
  3049. createSimpleExpression(rExp, false),
  3050. context,
  3051. false,
  3052. false,
  3053. knownIds
  3054. )
  3055. );
  3056. return `${context.helperString(IS_REF)}(${raw})${context.isTS ? ` //@ts-ignore
  3057. ` : ``} ? ${raw}.value ${operator} ${rExpString} : ${raw}`;
  3058. } else if (isUpdateArg) {
  3059. id.start = parent.start;
  3060. id.end = parent.end;
  3061. const { prefix: isPrefix, operator } = parent;
  3062. const prefix = isPrefix ? operator : ``;
  3063. const postfix = isPrefix ? `` : operator;
  3064. return `${context.helperString(IS_REF)}(${raw})${context.isTS ? ` //@ts-ignore
  3065. ` : ``} ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`;
  3066. } else if (isDestructureAssignment) {
  3067. return raw;
  3068. } else {
  3069. return `${context.helperString(UNREF)}(${raw})`;
  3070. }
  3071. } else if (type === "props") {
  3072. return shared.genPropsAccessExp(raw);
  3073. } else if (type === "props-aliased") {
  3074. return shared.genPropsAccessExp(bindingMetadata.__propsAliases[raw]);
  3075. }
  3076. } else {
  3077. if (type && type.startsWith("setup") || type === "literal-const") {
  3078. return `$setup.${raw}`;
  3079. } else if (type === "props-aliased") {
  3080. return `$props['${bindingMetadata.__propsAliases[raw]}']`;
  3081. } else if (type) {
  3082. return `$${type}.${raw}`;
  3083. }
  3084. }
  3085. return `_ctx.${raw}`;
  3086. };
  3087. const rawExp = node.content;
  3088. const bailConstant = constantBailRE.test(rawExp);
  3089. if (isSimpleIdentifier(rawExp)) {
  3090. const isScopeVarReference = context.identifiers[rawExp];
  3091. const isAllowedGlobal = shared.isGloballyWhitelisted(rawExp);
  3092. const isLiteral = isLiteralWhitelisted(rawExp);
  3093. if (!asParams && !isScopeVarReference && !isAllowedGlobal && !isLiteral) {
  3094. if (isConst(bindingMetadata[node.content])) {
  3095. node.constType = 1;
  3096. }
  3097. node.content = rewriteIdentifier(rawExp);
  3098. } else if (!isScopeVarReference) {
  3099. if (isLiteral) {
  3100. node.constType = 3;
  3101. } else {
  3102. node.constType = 2;
  3103. }
  3104. }
  3105. return node;
  3106. }
  3107. let ast;
  3108. const source = asRawStatements ? ` ${rawExp} ` : `(${rawExp})${asParams ? `=>{}` : ``}`;
  3109. try {
  3110. ast = parser.parse(source, {
  3111. plugins: context.expressionPlugins
  3112. }).program;
  3113. } catch (e) {
  3114. context.onError(
  3115. createCompilerError(
  3116. 45,
  3117. node.loc,
  3118. void 0,
  3119. e.message
  3120. )
  3121. );
  3122. return node;
  3123. }
  3124. const ids = [];
  3125. const parentStack = [];
  3126. const knownIds = Object.create(context.identifiers);
  3127. walkIdentifiers(
  3128. ast,
  3129. (node2, parent, _, isReferenced, isLocal) => {
  3130. if (isStaticPropertyKey(node2, parent)) {
  3131. return;
  3132. }
  3133. if (node2.name.startsWith("_filter_")) {
  3134. return;
  3135. }
  3136. const needPrefix = isReferenced && canPrefix(node2);
  3137. if (needPrefix && !isLocal) {
  3138. if (isStaticProperty(parent) && parent.shorthand) {
  3139. node2.prefix = `${node2.name}: `;
  3140. }
  3141. node2.name = rewriteIdentifier(node2.name, parent, node2);
  3142. ids.push(node2);
  3143. } else {
  3144. if (!(needPrefix && isLocal) && !bailConstant) {
  3145. node2.isConstant = true;
  3146. }
  3147. ids.push(node2);
  3148. }
  3149. },
  3150. true,
  3151. // invoke on ALL identifiers
  3152. parentStack,
  3153. knownIds
  3154. );
  3155. const children = [];
  3156. ids.sort((a, b) => a.start - b.start);
  3157. ids.forEach((id, i) => {
  3158. const start = id.start - 1;
  3159. const end = id.end - 1;
  3160. const last = ids[i - 1];
  3161. const leadingText = rawExp.slice(last ? last.end - 1 : 0, start);
  3162. if (leadingText.length || id.prefix) {
  3163. children.push(leadingText + (id.prefix || ``));
  3164. }
  3165. const source2 = rawExp.slice(start, end);
  3166. children.push(
  3167. createSimpleExpression(
  3168. id.name,
  3169. false,
  3170. {
  3171. source: source2,
  3172. start: advancePositionWithClone(node.loc.start, source2, start),
  3173. end: advancePositionWithClone(node.loc.start, source2, end)
  3174. },
  3175. id.isConstant ? 3 : 0
  3176. )
  3177. );
  3178. if (i === ids.length - 1 && end < rawExp.length) {
  3179. children.push(rawExp.slice(end));
  3180. }
  3181. });
  3182. let ret;
  3183. if (children.length) {
  3184. ret = createCompoundExpression(children, node.loc);
  3185. } else {
  3186. ret = node;
  3187. ret.constType = bailConstant ? 0 : 3;
  3188. }
  3189. ret.identifiers = Object.keys(knownIds);
  3190. return ret;
  3191. }
  3192. function canPrefix(id) {
  3193. if (shared.isGloballyWhitelisted(id.name)) {
  3194. return false;
  3195. }
  3196. if (id.name === "require") {
  3197. return false;
  3198. }
  3199. return true;
  3200. }
  3201. function stringifyExpression(exp) {
  3202. if (shared.isString(exp)) {
  3203. return exp;
  3204. } else if (exp.type === 4) {
  3205. return exp.content;
  3206. } else {
  3207. return exp.children.map(stringifyExpression).join("");
  3208. }
  3209. }
  3210. function isConst(type) {
  3211. return type === "setup-const" || type === "literal-const";
  3212. }
  3213. const transformIf = createStructuralDirectiveTransform(
  3214. /^(if|else|else-if)$/,
  3215. (node, dir, context) => {
  3216. return processIf(node, dir, context, (ifNode, branch, isRoot) => {
  3217. const siblings = context.parent.children;
  3218. let i = siblings.indexOf(ifNode);
  3219. let key = 0;
  3220. while (i-- >= 0) {
  3221. const sibling = siblings[i];
  3222. if (sibling && sibling.type === 9) {
  3223. key += sibling.branches.length;
  3224. }
  3225. }
  3226. return () => {
  3227. if (isRoot) {
  3228. ifNode.codegenNode = createCodegenNodeForBranch(
  3229. branch,
  3230. key,
  3231. context
  3232. );
  3233. } else {
  3234. const parentCondition = getParentCondition(ifNode.codegenNode);
  3235. parentCondition.alternate = createCodegenNodeForBranch(
  3236. branch,
  3237. key + ifNode.branches.length - 1,
  3238. context
  3239. );
  3240. }
  3241. };
  3242. });
  3243. }
  3244. );
  3245. function processIf(node, dir, context, processCodegen) {
  3246. if (dir.name !== "else" && (!dir.exp || !dir.exp.content.trim())) {
  3247. const loc = dir.exp ? dir.exp.loc : node.loc;
  3248. context.onError(
  3249. createCompilerError(28, dir.loc)
  3250. );
  3251. dir.exp = createSimpleExpression(`true`, false, loc);
  3252. }
  3253. if (context.prefixIdentifiers && dir.exp) {
  3254. dir.exp = processExpression(dir.exp, context);
  3255. }
  3256. if (dir.name === "if") {
  3257. const branch = createIfBranch(node, dir);
  3258. const ifNode = {
  3259. type: 9,
  3260. loc: node.loc,
  3261. branches: [branch]
  3262. };
  3263. context.replaceNode(ifNode);
  3264. if (processCodegen) {
  3265. return processCodegen(ifNode, branch, true);
  3266. }
  3267. } else {
  3268. const siblings = context.parent.children;
  3269. let i = siblings.indexOf(node);
  3270. while (i-- >= -1) {
  3271. const sibling = siblings[i];
  3272. if (sibling && sibling.type === 3) {
  3273. context.removeNode(sibling);
  3274. continue;
  3275. }
  3276. if (sibling && sibling.type === 2 && !sibling.content.trim().length) {
  3277. context.removeNode(sibling);
  3278. continue;
  3279. }
  3280. if (sibling && sibling.type === 9) {
  3281. if (dir.name === "else-if" && sibling.branches[sibling.branches.length - 1].condition === void 0) {
  3282. context.onError(
  3283. createCompilerError(30, node.loc)
  3284. );
  3285. }
  3286. context.removeNode();
  3287. const branch = createIfBranch(node, dir);
  3288. {
  3289. const key = branch.userKey;
  3290. if (key) {
  3291. sibling.branches.forEach(({ userKey }) => {
  3292. if (isSameKey(userKey, key)) {
  3293. context.onError(
  3294. createCompilerError(
  3295. 29,
  3296. branch.userKey.loc
  3297. )
  3298. );
  3299. }
  3300. });
  3301. }
  3302. }
  3303. sibling.branches.push(branch);
  3304. const onExit = processCodegen && processCodegen(sibling, branch, false);
  3305. traverseNode(branch, context);
  3306. if (onExit)
  3307. onExit();
  3308. context.currentNode = null;
  3309. } else {
  3310. context.onError(
  3311. createCompilerError(30, node.loc)
  3312. );
  3313. }
  3314. break;
  3315. }
  3316. }
  3317. }
  3318. function createIfBranch(node, dir) {
  3319. const isTemplateIf = node.tagType === 3;
  3320. return {
  3321. type: 10,
  3322. loc: node.loc,
  3323. condition: dir.name === "else" ? void 0 : dir.exp,
  3324. children: isTemplateIf && !findDir(node, "for") ? node.children : [node],
  3325. userKey: findProp(node, `key`),
  3326. isTemplateIf
  3327. };
  3328. }
  3329. function createCodegenNodeForBranch(branch, keyIndex, context) {
  3330. if (branch.condition) {
  3331. return createConditionalExpression(
  3332. branch.condition,
  3333. createChildrenCodegenNode(branch, keyIndex, context),
  3334. // make sure to pass in asBlock: true so that the comment node call
  3335. // closes the current block.
  3336. createCallExpression(context.helper(CREATE_COMMENT), [
  3337. '""',
  3338. "true"
  3339. ])
  3340. );
  3341. } else {
  3342. return createChildrenCodegenNode(branch, keyIndex, context);
  3343. }
  3344. }
  3345. function createChildrenCodegenNode(branch, keyIndex, context) {
  3346. const { helper } = context;
  3347. const keyProperty = createObjectProperty(
  3348. `key`,
  3349. createSimpleExpression(
  3350. `${keyIndex}`,
  3351. false,
  3352. locStub,
  3353. 2
  3354. )
  3355. );
  3356. const { children } = branch;
  3357. const firstChild = children[0];
  3358. const needFragmentWrapper = children.length !== 1 || firstChild.type !== 1;
  3359. if (needFragmentWrapper) {
  3360. if (children.length === 1 && firstChild.type === 11) {
  3361. const vnodeCall = firstChild.codegenNode;
  3362. injectProp(vnodeCall, keyProperty, context);
  3363. return vnodeCall;
  3364. } else {
  3365. let patchFlag = 64;
  3366. shared.PatchFlagNames[64];
  3367. return createVNodeCall(
  3368. context,
  3369. helper(FRAGMENT),
  3370. createObjectExpression([keyProperty]),
  3371. children,
  3372. patchFlag + (``),
  3373. void 0,
  3374. void 0,
  3375. true,
  3376. false,
  3377. false,
  3378. branch.loc
  3379. );
  3380. }
  3381. } else {
  3382. const ret = firstChild.codegenNode;
  3383. const vnodeCall = getMemoedVNodeCall(ret);
  3384. if (vnodeCall.type === 13) {
  3385. convertToBlock(vnodeCall, context);
  3386. }
  3387. injectProp(vnodeCall, keyProperty, context);
  3388. return ret;
  3389. }
  3390. }
  3391. function isSameKey(a, b) {
  3392. if (!a || a.type !== b.type) {
  3393. return false;
  3394. }
  3395. if (a.type === 6) {
  3396. if (a.value.content !== b.value.content) {
  3397. return false;
  3398. }
  3399. } else {
  3400. const exp = a.exp;
  3401. const branchExp = b.exp;
  3402. if (exp.type !== branchExp.type) {
  3403. return false;
  3404. }
  3405. if (exp.type !== 4 || exp.isStatic !== branchExp.isStatic || exp.content !== branchExp.content) {
  3406. return false;
  3407. }
  3408. }
  3409. return true;
  3410. }
  3411. function getParentCondition(node) {
  3412. while (true) {
  3413. if (node.type === 19) {
  3414. if (node.alternate.type === 19) {
  3415. node = node.alternate;
  3416. } else {
  3417. return node;
  3418. }
  3419. } else if (node.type === 20) {
  3420. node = node.value;
  3421. }
  3422. }
  3423. }
  3424. const transformFor = createStructuralDirectiveTransform(
  3425. "for",
  3426. (node, dir, context) => {
  3427. const { helper, removeHelper } = context;
  3428. return processFor(node, dir, context, (forNode) => {
  3429. const renderExp = createCallExpression(helper(RENDER_LIST), [
  3430. forNode.source
  3431. ]);
  3432. const isTemplate = isTemplateNode(node);
  3433. const memo = findDir(node, "memo");
  3434. const keyProp = findProp(node, `key`);
  3435. const keyExp = keyProp && (keyProp.type === 6 ? createSimpleExpression(keyProp.value.content, true) : keyProp.exp);
  3436. const keyProperty = keyProp ? createObjectProperty(`key`, keyExp) : null;
  3437. if (isTemplate) {
  3438. if (memo) {
  3439. memo.exp = processExpression(
  3440. memo.exp,
  3441. context
  3442. );
  3443. }
  3444. if (keyProperty && keyProp.type !== 6) {
  3445. keyProperty.value = processExpression(
  3446. keyProperty.value,
  3447. context
  3448. );
  3449. }
  3450. }
  3451. const isStableFragment = forNode.source.type === 4 && forNode.source.constType > 0;
  3452. const fragmentFlag = isStableFragment ? 64 : keyProp ? 128 : 256;
  3453. forNode.codegenNode = createVNodeCall(
  3454. context,
  3455. helper(FRAGMENT),
  3456. void 0,
  3457. renderExp,
  3458. fragmentFlag + (``),
  3459. void 0,
  3460. void 0,
  3461. true,
  3462. !isStableFragment,
  3463. false,
  3464. node.loc
  3465. );
  3466. return () => {
  3467. let childBlock;
  3468. const { children } = forNode;
  3469. if (isTemplate) {
  3470. node.children.some((c) => {
  3471. if (c.type === 1) {
  3472. const key = findProp(c, "key");
  3473. if (key) {
  3474. context.onError(
  3475. createCompilerError(
  3476. 33,
  3477. key.loc
  3478. )
  3479. );
  3480. return true;
  3481. }
  3482. }
  3483. });
  3484. }
  3485. const needFragmentWrapper = children.length !== 1 || children[0].type !== 1;
  3486. const slotOutlet = isSlotOutlet(node) ? node : isTemplate && node.children.length === 1 && isSlotOutlet(node.children[0]) ? node.children[0] : null;
  3487. if (slotOutlet) {
  3488. childBlock = slotOutlet.codegenNode;
  3489. if (isTemplate && keyProperty) {
  3490. injectProp(childBlock, keyProperty, context);
  3491. }
  3492. } else if (needFragmentWrapper) {
  3493. childBlock = createVNodeCall(
  3494. context,
  3495. helper(FRAGMENT),
  3496. keyProperty ? createObjectExpression([keyProperty]) : void 0,
  3497. node.children,
  3498. 64 + (``),
  3499. void 0,
  3500. void 0,
  3501. true,
  3502. void 0,
  3503. false
  3504. /* isComponent */
  3505. );
  3506. } else {
  3507. childBlock = children[0].codegenNode;
  3508. if (isTemplate && keyProperty) {
  3509. injectProp(childBlock, keyProperty, context);
  3510. }
  3511. if (childBlock.isBlock !== !isStableFragment) {
  3512. if (childBlock.isBlock) {
  3513. removeHelper(OPEN_BLOCK);
  3514. removeHelper(
  3515. getVNodeBlockHelper(context.inSSR, childBlock.isComponent)
  3516. );
  3517. } else {
  3518. removeHelper(
  3519. getVNodeHelper(context.inSSR, childBlock.isComponent)
  3520. );
  3521. }
  3522. }
  3523. childBlock.isBlock = !isStableFragment;
  3524. if (childBlock.isBlock) {
  3525. helper(OPEN_BLOCK);
  3526. helper(getVNodeBlockHelper(context.inSSR, childBlock.isComponent));
  3527. } else {
  3528. helper(getVNodeHelper(context.inSSR, childBlock.isComponent));
  3529. }
  3530. }
  3531. if (memo) {
  3532. const loop = createFunctionExpression(
  3533. createForLoopParams(forNode.parseResult, [
  3534. createSimpleExpression(`_cached`)
  3535. ])
  3536. );
  3537. loop.body = createBlockStatement([
  3538. createCompoundExpression([`const _memo = (`, memo.exp, `)`]),
  3539. createCompoundExpression([
  3540. `if (_cached`,
  3541. ...keyExp ? [` && _cached.key === `, keyExp] : [],
  3542. ` && ${context.helperString(
  3543. IS_MEMO_SAME
  3544. )}(_cached, _memo)) return _cached`
  3545. ]),
  3546. createCompoundExpression([`const _item = `, childBlock]),
  3547. createSimpleExpression(`_item.memo = _memo`),
  3548. createSimpleExpression(`return _item`)
  3549. ]);
  3550. renderExp.arguments.push(
  3551. loop,
  3552. createSimpleExpression(`_cache`),
  3553. createSimpleExpression(String(context.cached++))
  3554. );
  3555. } else {
  3556. renderExp.arguments.push(
  3557. createFunctionExpression(
  3558. createForLoopParams(forNode.parseResult),
  3559. childBlock,
  3560. true
  3561. /* force newline */
  3562. )
  3563. );
  3564. }
  3565. };
  3566. });
  3567. }
  3568. );
  3569. function processFor(node, dir, context, processCodegen) {
  3570. if (!dir.exp) {
  3571. context.onError(
  3572. createCompilerError(31, dir.loc)
  3573. );
  3574. return;
  3575. }
  3576. const parseResult = parseForExpression(
  3577. // can only be simple expression because vFor transform is applied
  3578. // before expression transform.
  3579. dir.exp,
  3580. context
  3581. );
  3582. if (!parseResult) {
  3583. context.onError(
  3584. createCompilerError(32, dir.loc)
  3585. );
  3586. return;
  3587. }
  3588. const { addIdentifiers, removeIdentifiers, scopes } = context;
  3589. const { source, value, key, index } = parseResult;
  3590. const forNode = {
  3591. type: 11,
  3592. loc: dir.loc,
  3593. source,
  3594. valueAlias: value,
  3595. keyAlias: key,
  3596. objectIndexAlias: index,
  3597. parseResult,
  3598. children: isTemplateNode(node) ? node.children : [node]
  3599. };
  3600. context.replaceNode(forNode);
  3601. scopes.vFor++;
  3602. if (context.prefixIdentifiers) {
  3603. value && addIdentifiers(value);
  3604. key && addIdentifiers(key);
  3605. index && addIdentifiers(index);
  3606. }
  3607. const onExit = processCodegen && processCodegen(forNode);
  3608. return () => {
  3609. scopes.vFor--;
  3610. if (context.prefixIdentifiers) {
  3611. value && removeIdentifiers(value);
  3612. key && removeIdentifiers(key);
  3613. index && removeIdentifiers(index);
  3614. }
  3615. if (onExit)
  3616. onExit();
  3617. };
  3618. }
  3619. const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/;
  3620. const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/;
  3621. const stripParensRE = /^\(|\)$/g;
  3622. function parseForExpression(input, context) {
  3623. const loc = input.loc;
  3624. const exp = input.content;
  3625. const inMatch = exp.match(forAliasRE);
  3626. if (!inMatch)
  3627. return;
  3628. const [, LHS, RHS] = inMatch;
  3629. const result = {
  3630. source: createAliasExpression(
  3631. loc,
  3632. RHS.trim(),
  3633. exp.indexOf(RHS, LHS.length)
  3634. ),
  3635. value: void 0,
  3636. key: void 0,
  3637. index: void 0
  3638. };
  3639. if (context.prefixIdentifiers) {
  3640. result.source = processExpression(
  3641. result.source,
  3642. context
  3643. );
  3644. }
  3645. let valueContent = LHS.trim().replace(stripParensRE, "").trim();
  3646. const trimmedOffset = LHS.indexOf(valueContent);
  3647. const iteratorMatch = valueContent.match(forIteratorRE);
  3648. if (iteratorMatch) {
  3649. valueContent = valueContent.replace(forIteratorRE, "").trim();
  3650. const keyContent = iteratorMatch[1].trim();
  3651. let keyOffset;
  3652. if (keyContent) {
  3653. keyOffset = exp.indexOf(keyContent, trimmedOffset + valueContent.length);
  3654. result.key = createAliasExpression(loc, keyContent, keyOffset);
  3655. if (context.prefixIdentifiers) {
  3656. result.key = processExpression(result.key, context, true);
  3657. }
  3658. }
  3659. if (iteratorMatch[2]) {
  3660. const indexContent = iteratorMatch[2].trim();
  3661. if (indexContent) {
  3662. result.index = createAliasExpression(
  3663. loc,
  3664. indexContent,
  3665. exp.indexOf(
  3666. indexContent,
  3667. result.key ? keyOffset + keyContent.length : trimmedOffset + valueContent.length
  3668. )
  3669. );
  3670. if (context.prefixIdentifiers) {
  3671. result.index = processExpression(result.index, context, true);
  3672. }
  3673. }
  3674. }
  3675. }
  3676. if (valueContent) {
  3677. result.value = createAliasExpression(loc, valueContent, trimmedOffset);
  3678. if (context.prefixIdentifiers) {
  3679. result.value = processExpression(result.value, context, true);
  3680. }
  3681. }
  3682. return result;
  3683. }
  3684. function createAliasExpression(range, content, offset) {
  3685. return createSimpleExpression(
  3686. content,
  3687. false,
  3688. getInnerRange(range, offset, content.length)
  3689. );
  3690. }
  3691. function createForLoopParams({ value, key, index }, memoArgs = []) {
  3692. return createParamsList([value, key, index, ...memoArgs]);
  3693. }
  3694. function createParamsList(args) {
  3695. let i = args.length;
  3696. while (i--) {
  3697. if (args[i])
  3698. break;
  3699. }
  3700. return args.slice(0, i + 1).map((arg, i2) => arg || createSimpleExpression(`_`.repeat(i2 + 1), false));
  3701. }
  3702. const defaultFallback = createSimpleExpression(`undefined`, false);
  3703. const trackSlotScopes = (node, context) => {
  3704. if (node.type === 1 && (node.tagType === 1 || node.tagType === 3)) {
  3705. const vSlot = findDir(node, "slot");
  3706. if (vSlot) {
  3707. const slotProps = vSlot.exp;
  3708. if (context.prefixIdentifiers) {
  3709. slotProps && context.addIdentifiers(slotProps);
  3710. }
  3711. context.scopes.vSlot++;
  3712. return () => {
  3713. if (context.prefixIdentifiers) {
  3714. slotProps && context.removeIdentifiers(slotProps);
  3715. }
  3716. context.scopes.vSlot--;
  3717. };
  3718. }
  3719. }
  3720. };
  3721. const trackVForSlotScopes = (node, context) => {
  3722. let vFor;
  3723. if (isTemplateNode(node) && node.props.some(isVSlot) && (vFor = findDir(node, "for"))) {
  3724. const result = vFor.parseResult = parseForExpression(
  3725. vFor.exp,
  3726. context
  3727. );
  3728. if (result) {
  3729. const { value, key, index } = result;
  3730. const { addIdentifiers, removeIdentifiers } = context;
  3731. value && addIdentifiers(value);
  3732. key && addIdentifiers(key);
  3733. index && addIdentifiers(index);
  3734. return () => {
  3735. value && removeIdentifiers(value);
  3736. key && removeIdentifiers(key);
  3737. index && removeIdentifiers(index);
  3738. };
  3739. }
  3740. }
  3741. };
  3742. const buildClientSlotFn = (props, children, loc) => createFunctionExpression(
  3743. props,
  3744. children,
  3745. false,
  3746. true,
  3747. children.length ? children[0].loc : loc
  3748. );
  3749. function buildSlots(node, context, buildSlotFn = buildClientSlotFn) {
  3750. context.helper(WITH_CTX);
  3751. const { children, loc } = node;
  3752. const slotsProperties = [];
  3753. const dynamicSlots = [];
  3754. let hasDynamicSlots = context.scopes.vSlot > 0 || context.scopes.vFor > 0;
  3755. if (!context.ssr && context.prefixIdentifiers) {
  3756. hasDynamicSlots = hasScopeRef(node, context.identifiers);
  3757. }
  3758. const onComponentSlot = findDir(node, "slot", true);
  3759. if (onComponentSlot) {
  3760. const { arg, exp } = onComponentSlot;
  3761. if (arg && !isStaticExp(arg)) {
  3762. hasDynamicSlots = true;
  3763. }
  3764. slotsProperties.push(
  3765. createObjectProperty(
  3766. arg || createSimpleExpression("default", true),
  3767. buildSlotFn(exp, children, loc)
  3768. )
  3769. );
  3770. }
  3771. let hasTemplateSlots = false;
  3772. let hasNamedDefaultSlot = false;
  3773. const implicitDefaultChildren = [];
  3774. const seenSlotNames = /* @__PURE__ */ new Set();
  3775. let conditionalBranchIndex = 0;
  3776. for (let i = 0; i < children.length; i++) {
  3777. const slotElement = children[i];
  3778. let slotDir;
  3779. if (!isTemplateNode(slotElement) || !(slotDir = findDir(slotElement, "slot", true))) {
  3780. if (slotElement.type !== 3) {
  3781. implicitDefaultChildren.push(slotElement);
  3782. }
  3783. continue;
  3784. }
  3785. if (onComponentSlot) {
  3786. context.onError(
  3787. createCompilerError(37, slotDir.loc)
  3788. );
  3789. break;
  3790. }
  3791. hasTemplateSlots = true;
  3792. const { children: slotChildren, loc: slotLoc } = slotElement;
  3793. const {
  3794. arg: slotName = createSimpleExpression(`default`, true),
  3795. exp: slotProps,
  3796. loc: dirLoc
  3797. } = slotDir;
  3798. let staticSlotName;
  3799. if (isStaticExp(slotName)) {
  3800. staticSlotName = slotName ? slotName.content : `default`;
  3801. } else {
  3802. hasDynamicSlots = true;
  3803. }
  3804. const slotFunction = buildSlotFn(slotProps, slotChildren, slotLoc);
  3805. let vIf;
  3806. let vElse;
  3807. let vFor;
  3808. if (vIf = findDir(slotElement, "if")) {
  3809. hasDynamicSlots = true;
  3810. dynamicSlots.push(
  3811. createConditionalExpression(
  3812. vIf.exp,
  3813. buildDynamicSlot(slotName, slotFunction, conditionalBranchIndex++),
  3814. defaultFallback
  3815. )
  3816. );
  3817. } else if (vElse = findDir(
  3818. slotElement,
  3819. /^else(-if)?$/,
  3820. true
  3821. /* allowEmpty */
  3822. )) {
  3823. let j = i;
  3824. let prev;
  3825. while (j--) {
  3826. prev = children[j];
  3827. if (prev.type !== 3) {
  3828. break;
  3829. }
  3830. }
  3831. if (prev && isTemplateNode(prev) && findDir(prev, "if")) {
  3832. children.splice(i, 1);
  3833. i--;
  3834. let conditional = dynamicSlots[dynamicSlots.length - 1];
  3835. while (conditional.alternate.type === 19) {
  3836. conditional = conditional.alternate;
  3837. }
  3838. conditional.alternate = vElse.exp ? createConditionalExpression(
  3839. vElse.exp,
  3840. buildDynamicSlot(
  3841. slotName,
  3842. slotFunction,
  3843. conditionalBranchIndex++
  3844. ),
  3845. defaultFallback
  3846. ) : buildDynamicSlot(slotName, slotFunction, conditionalBranchIndex++);
  3847. } else {
  3848. context.onError(
  3849. createCompilerError(30, vElse.loc)
  3850. );
  3851. }
  3852. } else if (vFor = findDir(slotElement, "for")) {
  3853. hasDynamicSlots = true;
  3854. const parseResult = vFor.parseResult || parseForExpression(vFor.exp, context);
  3855. if (parseResult) {
  3856. dynamicSlots.push(
  3857. createCallExpression(context.helper(RENDER_LIST), [
  3858. parseResult.source,
  3859. createFunctionExpression(
  3860. createForLoopParams(parseResult),
  3861. buildDynamicSlot(slotName, slotFunction),
  3862. true
  3863. /* force newline */
  3864. )
  3865. ])
  3866. );
  3867. } else {
  3868. context.onError(
  3869. createCompilerError(32, vFor.loc)
  3870. );
  3871. }
  3872. } else {
  3873. if (staticSlotName) {
  3874. if (seenSlotNames.has(staticSlotName)) {
  3875. context.onError(
  3876. createCompilerError(
  3877. 38,
  3878. dirLoc
  3879. )
  3880. );
  3881. continue;
  3882. }
  3883. seenSlotNames.add(staticSlotName);
  3884. if (staticSlotName === "default") {
  3885. hasNamedDefaultSlot = true;
  3886. }
  3887. }
  3888. slotsProperties.push(createObjectProperty(slotName, slotFunction));
  3889. }
  3890. }
  3891. if (!onComponentSlot) {
  3892. const buildDefaultSlotProperty = (props, children2) => {
  3893. const fn = buildSlotFn(props, children2, loc);
  3894. if (context.compatConfig) {
  3895. fn.isNonScopedSlot = true;
  3896. }
  3897. return createObjectProperty(`default`, fn);
  3898. };
  3899. if (!hasTemplateSlots) {
  3900. slotsProperties.push(buildDefaultSlotProperty(void 0, children));
  3901. } else if (implicitDefaultChildren.length && // #3766
  3902. // with whitespace: 'preserve', whitespaces between slots will end up in
  3903. // implicitDefaultChildren. Ignore if all implicit children are whitespaces.
  3904. implicitDefaultChildren.some((node2) => isNonWhitespaceContent(node2))) {
  3905. if (hasNamedDefaultSlot) {
  3906. context.onError(
  3907. createCompilerError(
  3908. 39,
  3909. implicitDefaultChildren[0].loc
  3910. )
  3911. );
  3912. } else {
  3913. slotsProperties.push(
  3914. buildDefaultSlotProperty(void 0, implicitDefaultChildren)
  3915. );
  3916. }
  3917. }
  3918. }
  3919. const slotFlag = hasDynamicSlots ? 2 : hasForwardedSlots(node.children) ? 3 : 1;
  3920. let slots = createObjectExpression(
  3921. slotsProperties.concat(
  3922. createObjectProperty(
  3923. `_`,
  3924. // 2 = compiled but dynamic = can skip normalization, but must run diff
  3925. // 1 = compiled and static = can skip normalization AND diff as optimized
  3926. createSimpleExpression(
  3927. slotFlag + (``),
  3928. false
  3929. )
  3930. )
  3931. ),
  3932. loc
  3933. );
  3934. if (dynamicSlots.length) {
  3935. slots = createCallExpression(context.helper(CREATE_SLOTS), [
  3936. slots,
  3937. createArrayExpression(dynamicSlots)
  3938. ]);
  3939. }
  3940. return {
  3941. slots,
  3942. hasDynamicSlots
  3943. };
  3944. }
  3945. function buildDynamicSlot(name, fn, index) {
  3946. const props = [
  3947. createObjectProperty(`name`, name),
  3948. createObjectProperty(`fn`, fn)
  3949. ];
  3950. if (index != null) {
  3951. props.push(
  3952. createObjectProperty(`key`, createSimpleExpression(String(index), true))
  3953. );
  3954. }
  3955. return createObjectExpression(props);
  3956. }
  3957. function hasForwardedSlots(children) {
  3958. for (let i = 0; i < children.length; i++) {
  3959. const child = children[i];
  3960. switch (child.type) {
  3961. case 1:
  3962. if (child.tagType === 2 || hasForwardedSlots(child.children)) {
  3963. return true;
  3964. }
  3965. break;
  3966. case 9:
  3967. if (hasForwardedSlots(child.branches))
  3968. return true;
  3969. break;
  3970. case 10:
  3971. case 11:
  3972. if (hasForwardedSlots(child.children))
  3973. return true;
  3974. break;
  3975. }
  3976. }
  3977. return false;
  3978. }
  3979. function isNonWhitespaceContent(node) {
  3980. if (node.type !== 2 && node.type !== 12)
  3981. return true;
  3982. return node.type === 2 ? !!node.content.trim() : isNonWhitespaceContent(node.content);
  3983. }
  3984. const directiveImportMap = /* @__PURE__ */ new WeakMap();
  3985. const transformElement = (node, context) => {
  3986. return function postTransformElement() {
  3987. node = context.currentNode;
  3988. if (!(node.type === 1 && (node.tagType === 0 || node.tagType === 1))) {
  3989. return;
  3990. }
  3991. const { tag, props } = node;
  3992. const isComponent = node.tagType === 1;
  3993. let vnodeTag = isComponent ? resolveComponentType(node, context) : `"${tag}"`;
  3994. const isDynamicComponent = shared.isObject(vnodeTag) && vnodeTag.callee === RESOLVE_DYNAMIC_COMPONENT;
  3995. let vnodeProps;
  3996. let vnodeChildren;
  3997. let vnodePatchFlag;
  3998. let patchFlag = 0;
  3999. let vnodeDynamicProps;
  4000. let dynamicPropNames;
  4001. let vnodeDirectives;
  4002. let shouldUseBlock = (
  4003. // dynamic component may resolve to plain elements
  4004. isDynamicComponent || vnodeTag === TELEPORT || vnodeTag === SUSPENSE || !isComponent && // <svg> and <foreignObject> must be forced into blocks so that block
  4005. // updates inside get proper isSVG flag at runtime. (#639, #643)
  4006. // This is technically web-specific, but splitting the logic out of core
  4007. // leads to too much unnecessary complexity.
  4008. (tag === "svg" || tag === "foreignObject")
  4009. );
  4010. if (props.length > 0) {
  4011. const propsBuildResult = buildProps(
  4012. node,
  4013. context,
  4014. void 0,
  4015. isComponent,
  4016. isDynamicComponent
  4017. );
  4018. vnodeProps = propsBuildResult.props;
  4019. patchFlag = propsBuildResult.patchFlag;
  4020. dynamicPropNames = propsBuildResult.dynamicPropNames;
  4021. const directives = propsBuildResult.directives;
  4022. vnodeDirectives = directives && directives.length ? createArrayExpression(
  4023. directives.map((dir) => buildDirectiveArgs(dir, context))
  4024. ) : void 0;
  4025. if (propsBuildResult.shouldUseBlock) {
  4026. shouldUseBlock = true;
  4027. }
  4028. }
  4029. if (node.children.length > 0) {
  4030. if (vnodeTag === KEEP_ALIVE) {
  4031. shouldUseBlock = true;
  4032. patchFlag |= 1024;
  4033. }
  4034. const shouldBuildAsSlots = isComponent && // Teleport is not a real component and has dedicated runtime handling
  4035. vnodeTag !== TELEPORT && // explained above.
  4036. vnodeTag !== KEEP_ALIVE;
  4037. if (shouldBuildAsSlots) {
  4038. const { slots, hasDynamicSlots } = buildSlots(node, context);
  4039. vnodeChildren = slots;
  4040. if (hasDynamicSlots) {
  4041. patchFlag |= 1024;
  4042. }
  4043. } else if (node.children.length === 1 && vnodeTag !== TELEPORT) {
  4044. const child = node.children[0];
  4045. const type = child.type;
  4046. const hasDynamicTextChild = type === 5 || type === 8;
  4047. if (hasDynamicTextChild && getConstantType(child, context) === 0) {
  4048. patchFlag |= 1;
  4049. }
  4050. if (hasDynamicTextChild || type === 2) {
  4051. vnodeChildren = child;
  4052. } else {
  4053. vnodeChildren = node.children;
  4054. }
  4055. } else {
  4056. vnodeChildren = node.children;
  4057. }
  4058. }
  4059. if (patchFlag !== 0) {
  4060. {
  4061. vnodePatchFlag = String(patchFlag);
  4062. }
  4063. if (dynamicPropNames && dynamicPropNames.length) {
  4064. vnodeDynamicProps = stringifyDynamicPropNames(dynamicPropNames);
  4065. }
  4066. }
  4067. node.codegenNode = createVNodeCall(
  4068. context,
  4069. vnodeTag,
  4070. vnodeProps,
  4071. vnodeChildren,
  4072. vnodePatchFlag,
  4073. vnodeDynamicProps,
  4074. vnodeDirectives,
  4075. !!shouldUseBlock,
  4076. false,
  4077. isComponent,
  4078. node.loc
  4079. );
  4080. };
  4081. };
  4082. function resolveComponentType(node, context, ssr = false) {
  4083. let { tag } = node;
  4084. const isExplicitDynamic = isComponentTag(tag);
  4085. const isProp = findProp(node, "is");
  4086. if (isProp) {
  4087. if (isExplicitDynamic || isCompatEnabled(
  4088. "COMPILER_IS_ON_ELEMENT",
  4089. context
  4090. )) {
  4091. const exp = isProp.type === 6 ? isProp.value && createSimpleExpression(isProp.value.content, true) : isProp.exp;
  4092. if (exp) {
  4093. return createCallExpression(context.helper(RESOLVE_DYNAMIC_COMPONENT), [
  4094. exp
  4095. ]);
  4096. }
  4097. } else if (isProp.type === 6 && isProp.value.content.startsWith("vue:")) {
  4098. tag = isProp.value.content.slice(4);
  4099. }
  4100. }
  4101. const isDir = !isExplicitDynamic && findDir(node, "is");
  4102. if (isDir && isDir.exp) {
  4103. return createCallExpression(context.helper(RESOLVE_DYNAMIC_COMPONENT), [
  4104. isDir.exp
  4105. ]);
  4106. }
  4107. const builtIn = isCoreComponent(tag) || context.isBuiltInComponent(tag);
  4108. if (builtIn) {
  4109. if (!ssr)
  4110. context.helper(builtIn);
  4111. return builtIn;
  4112. }
  4113. {
  4114. const fromSetup = resolveSetupReference(tag, context);
  4115. if (fromSetup) {
  4116. return fromSetup;
  4117. }
  4118. const dotIndex = tag.indexOf(".");
  4119. if (dotIndex > 0) {
  4120. const ns = resolveSetupReference(tag.slice(0, dotIndex), context);
  4121. if (ns) {
  4122. return ns + tag.slice(dotIndex);
  4123. }
  4124. }
  4125. }
  4126. if (context.selfName && shared.capitalize(shared.camelize(tag)) === context.selfName) {
  4127. context.helper(RESOLVE_COMPONENT);
  4128. context.components.add(tag + `__self`);
  4129. return toValidAssetId(tag, `component`);
  4130. }
  4131. context.helper(RESOLVE_COMPONENT);
  4132. context.components.add(tag);
  4133. return toValidAssetId(tag, `component`);
  4134. }
  4135. function resolveSetupReference(name, context) {
  4136. const bindings = context.bindingMetadata;
  4137. if (!bindings || bindings.__isScriptSetup === false) {
  4138. return;
  4139. }
  4140. const camelName = shared.camelize(name);
  4141. const PascalName = shared.capitalize(camelName);
  4142. const checkType = (type) => {
  4143. if (bindings[name] === type) {
  4144. return name;
  4145. }
  4146. if (bindings[camelName] === type) {
  4147. return camelName;
  4148. }
  4149. if (bindings[PascalName] === type) {
  4150. return PascalName;
  4151. }
  4152. };
  4153. const fromConst = checkType("setup-const") || checkType("setup-reactive-const") || checkType("literal-const");
  4154. if (fromConst) {
  4155. return context.inline ? (
  4156. // in inline mode, const setup bindings (e.g. imports) can be used as-is
  4157. fromConst
  4158. ) : `$setup[${JSON.stringify(fromConst)}]`;
  4159. }
  4160. const fromMaybeRef = checkType("setup-let") || checkType("setup-ref") || checkType("setup-maybe-ref");
  4161. if (fromMaybeRef) {
  4162. return context.inline ? (
  4163. // setup scope bindings that may be refs need to be unrefed
  4164. `${context.helperString(UNREF)}(${fromMaybeRef})`
  4165. ) : `$setup[${JSON.stringify(fromMaybeRef)}]`;
  4166. }
  4167. }
  4168. function buildProps(node, context, props = node.props, isComponent, isDynamicComponent, ssr = false) {
  4169. const { tag, loc: elementLoc, children } = node;
  4170. let properties = [];
  4171. const mergeArgs = [];
  4172. const runtimeDirectives = [];
  4173. const hasChildren = children.length > 0;
  4174. let shouldUseBlock = false;
  4175. let patchFlag = 0;
  4176. let hasRef = false;
  4177. let hasClassBinding = false;
  4178. let hasStyleBinding = false;
  4179. let hasHydrationEventBinding = false;
  4180. let hasDynamicKeys = false;
  4181. let hasVnodeHook = false;
  4182. const dynamicPropNames = [];
  4183. const pushMergeArg = (arg) => {
  4184. if (properties.length) {
  4185. mergeArgs.push(
  4186. createObjectExpression(dedupeProperties(properties), elementLoc)
  4187. );
  4188. properties = [];
  4189. }
  4190. if (arg)
  4191. mergeArgs.push(arg);
  4192. };
  4193. const analyzePatchFlag = ({ key, value }) => {
  4194. if (isStaticExp(key)) {
  4195. const name = key.content;
  4196. const isEventHandler = shared.isOn(name);
  4197. if (isEventHandler && (!isComponent || isDynamicComponent) && // omit the flag for click handlers because hydration gives click
  4198. // dedicated fast path.
  4199. name.toLowerCase() !== "onclick" && // omit v-model handlers
  4200. name !== "onUpdate:modelValue" && // omit onVnodeXXX hooks
  4201. !shared.isReservedProp(name)) {
  4202. hasHydrationEventBinding = true;
  4203. }
  4204. if (isEventHandler && shared.isReservedProp(name)) {
  4205. hasVnodeHook = true;
  4206. }
  4207. if (value.type === 20 || (value.type === 4 || value.type === 8) && getConstantType(value, context) > 0) {
  4208. return;
  4209. }
  4210. if (name === "ref") {
  4211. hasRef = true;
  4212. } else if (name === "class") {
  4213. hasClassBinding = true;
  4214. } else if (name === "style") {
  4215. hasStyleBinding = true;
  4216. } else if (name !== "key" && !dynamicPropNames.includes(name)) {
  4217. dynamicPropNames.push(name);
  4218. }
  4219. if (isComponent && (name === "class" || name === "style") && !dynamicPropNames.includes(name)) {
  4220. dynamicPropNames.push(name);
  4221. }
  4222. } else {
  4223. hasDynamicKeys = true;
  4224. }
  4225. };
  4226. for (let i = 0; i < props.length; i++) {
  4227. const prop = props[i];
  4228. if (prop.type === 6) {
  4229. const { loc, name, value } = prop;
  4230. let isStatic = true;
  4231. if (name === "ref") {
  4232. hasRef = true;
  4233. if (context.scopes.vFor > 0) {
  4234. properties.push(
  4235. createObjectProperty(
  4236. createSimpleExpression("ref_for", true),
  4237. createSimpleExpression("true")
  4238. )
  4239. );
  4240. }
  4241. if (value && context.inline) {
  4242. const binding = context.bindingMetadata[value.content];
  4243. if (binding === "setup-let" || binding === "setup-ref" || binding === "setup-maybe-ref") {
  4244. isStatic = false;
  4245. properties.push(
  4246. createObjectProperty(
  4247. createSimpleExpression("ref_key", true),
  4248. createSimpleExpression(value.content, true, value.loc)
  4249. )
  4250. );
  4251. }
  4252. }
  4253. }
  4254. if (name === "is" && (isComponentTag(tag) || value && value.content.startsWith("vue:") || isCompatEnabled(
  4255. "COMPILER_IS_ON_ELEMENT",
  4256. context
  4257. ))) {
  4258. continue;
  4259. }
  4260. properties.push(
  4261. createObjectProperty(
  4262. createSimpleExpression(
  4263. name,
  4264. true,
  4265. getInnerRange(loc, 0, name.length)
  4266. ),
  4267. createSimpleExpression(
  4268. value ? value.content : "",
  4269. isStatic,
  4270. value ? value.loc : loc
  4271. )
  4272. )
  4273. );
  4274. } else {
  4275. const { name, arg, exp, loc } = prop;
  4276. const isVBind = name === "bind";
  4277. const isVOn = name === "on";
  4278. if (name === "slot") {
  4279. if (!isComponent) {
  4280. context.onError(
  4281. createCompilerError(40, loc)
  4282. );
  4283. }
  4284. continue;
  4285. }
  4286. if (name === "once" || name === "memo") {
  4287. continue;
  4288. }
  4289. if (name === "is" || isVBind && isStaticArgOf(arg, "is") && (isComponentTag(tag) || isCompatEnabled(
  4290. "COMPILER_IS_ON_ELEMENT",
  4291. context
  4292. ))) {
  4293. continue;
  4294. }
  4295. if (isVOn && ssr) {
  4296. continue;
  4297. }
  4298. if (
  4299. // #938: elements with dynamic keys should be forced into blocks
  4300. isVBind && isStaticArgOf(arg, "key") || // inline before-update hooks need to force block so that it is invoked
  4301. // before children
  4302. isVOn && hasChildren && isStaticArgOf(arg, "vue:before-update")
  4303. ) {
  4304. shouldUseBlock = true;
  4305. }
  4306. if (isVBind && isStaticArgOf(arg, "ref") && context.scopes.vFor > 0) {
  4307. properties.push(
  4308. createObjectProperty(
  4309. createSimpleExpression("ref_for", true),
  4310. createSimpleExpression("true")
  4311. )
  4312. );
  4313. }
  4314. if (!arg && (isVBind || isVOn)) {
  4315. hasDynamicKeys = true;
  4316. if (exp) {
  4317. if (isVBind) {
  4318. pushMergeArg();
  4319. {
  4320. if (isCompatEnabled(
  4321. "COMPILER_V_BIND_OBJECT_ORDER",
  4322. context
  4323. )) {
  4324. mergeArgs.unshift(exp);
  4325. continue;
  4326. }
  4327. }
  4328. mergeArgs.push(exp);
  4329. } else {
  4330. pushMergeArg({
  4331. type: 14,
  4332. loc,
  4333. callee: context.helper(TO_HANDLERS),
  4334. arguments: isComponent ? [exp] : [exp, `true`]
  4335. });
  4336. }
  4337. } else {
  4338. context.onError(
  4339. createCompilerError(
  4340. isVBind ? 34 : 35,
  4341. loc
  4342. )
  4343. );
  4344. }
  4345. continue;
  4346. }
  4347. const directiveTransform = context.directiveTransforms[name];
  4348. if (directiveTransform) {
  4349. const { props: props2, needRuntime } = directiveTransform(prop, node, context);
  4350. !ssr && props2.forEach(analyzePatchFlag);
  4351. if (isVOn && arg && !isStaticExp(arg)) {
  4352. pushMergeArg(createObjectExpression(props2, elementLoc));
  4353. } else {
  4354. properties.push(...props2);
  4355. }
  4356. if (needRuntime) {
  4357. runtimeDirectives.push(prop);
  4358. if (shared.isSymbol(needRuntime)) {
  4359. directiveImportMap.set(prop, needRuntime);
  4360. }
  4361. }
  4362. } else if (!shared.isBuiltInDirective(name)) {
  4363. runtimeDirectives.push(prop);
  4364. if (hasChildren) {
  4365. shouldUseBlock = true;
  4366. }
  4367. }
  4368. }
  4369. }
  4370. let propsExpression = void 0;
  4371. if (mergeArgs.length) {
  4372. pushMergeArg();
  4373. if (mergeArgs.length > 1) {
  4374. propsExpression = createCallExpression(
  4375. context.helper(MERGE_PROPS),
  4376. mergeArgs,
  4377. elementLoc
  4378. );
  4379. } else {
  4380. propsExpression = mergeArgs[0];
  4381. }
  4382. } else if (properties.length) {
  4383. propsExpression = createObjectExpression(
  4384. dedupeProperties(properties),
  4385. elementLoc
  4386. );
  4387. }
  4388. if (hasDynamicKeys) {
  4389. patchFlag |= 16;
  4390. } else {
  4391. if (hasClassBinding && !isComponent) {
  4392. patchFlag |= 2;
  4393. }
  4394. if (hasStyleBinding && !isComponent) {
  4395. patchFlag |= 4;
  4396. }
  4397. if (dynamicPropNames.length) {
  4398. patchFlag |= 8;
  4399. }
  4400. if (hasHydrationEventBinding) {
  4401. patchFlag |= 32;
  4402. }
  4403. }
  4404. if (!shouldUseBlock && (patchFlag === 0 || patchFlag === 32) && (hasRef || hasVnodeHook || runtimeDirectives.length > 0)) {
  4405. patchFlag |= 512;
  4406. }
  4407. if (!context.inSSR && propsExpression) {
  4408. switch (propsExpression.type) {
  4409. case 15:
  4410. let classKeyIndex = -1;
  4411. let styleKeyIndex = -1;
  4412. let hasDynamicKey = false;
  4413. for (let i = 0; i < propsExpression.properties.length; i++) {
  4414. const key = propsExpression.properties[i].key;
  4415. if (isStaticExp(key)) {
  4416. if (key.content === "class") {
  4417. classKeyIndex = i;
  4418. } else if (key.content === "style") {
  4419. styleKeyIndex = i;
  4420. }
  4421. } else if (!key.isHandlerKey) {
  4422. hasDynamicKey = true;
  4423. }
  4424. }
  4425. const classProp = propsExpression.properties[classKeyIndex];
  4426. const styleProp = propsExpression.properties[styleKeyIndex];
  4427. if (!hasDynamicKey) {
  4428. if (classProp && !isStaticExp(classProp.value)) {
  4429. classProp.value = createCallExpression(
  4430. context.helper(NORMALIZE_CLASS),
  4431. [classProp.value]
  4432. );
  4433. }
  4434. if (styleProp && // the static style is compiled into an object,
  4435. // so use `hasStyleBinding` to ensure that it is a dynamic style binding
  4436. (hasStyleBinding || styleProp.value.type === 4 && styleProp.value.content.trim()[0] === `[` || // v-bind:style and style both exist,
  4437. // v-bind:style with static literal object
  4438. styleProp.value.type === 17)) {
  4439. styleProp.value = createCallExpression(
  4440. context.helper(NORMALIZE_STYLE),
  4441. [styleProp.value]
  4442. );
  4443. }
  4444. } else {
  4445. propsExpression = createCallExpression(
  4446. context.helper(NORMALIZE_PROPS),
  4447. [propsExpression]
  4448. );
  4449. }
  4450. break;
  4451. case 14:
  4452. break;
  4453. default:
  4454. propsExpression = createCallExpression(
  4455. context.helper(NORMALIZE_PROPS),
  4456. [
  4457. createCallExpression(context.helper(GUARD_REACTIVE_PROPS), [
  4458. propsExpression
  4459. ])
  4460. ]
  4461. );
  4462. break;
  4463. }
  4464. }
  4465. return {
  4466. props: propsExpression,
  4467. directives: runtimeDirectives,
  4468. patchFlag,
  4469. dynamicPropNames,
  4470. shouldUseBlock
  4471. };
  4472. }
  4473. function dedupeProperties(properties) {
  4474. const knownProps = /* @__PURE__ */ new Map();
  4475. const deduped = [];
  4476. for (let i = 0; i < properties.length; i++) {
  4477. const prop = properties[i];
  4478. if (prop.key.type === 8 || !prop.key.isStatic) {
  4479. deduped.push(prop);
  4480. continue;
  4481. }
  4482. const name = prop.key.content;
  4483. const existing = knownProps.get(name);
  4484. if (existing) {
  4485. if (name === "style" || name === "class" || shared.isOn(name)) {
  4486. mergeAsArray(existing, prop);
  4487. }
  4488. } else {
  4489. knownProps.set(name, prop);
  4490. deduped.push(prop);
  4491. }
  4492. }
  4493. return deduped;
  4494. }
  4495. function mergeAsArray(existing, incoming) {
  4496. if (existing.value.type === 17) {
  4497. existing.value.elements.push(incoming.value);
  4498. } else {
  4499. existing.value = createArrayExpression(
  4500. [existing.value, incoming.value],
  4501. existing.loc
  4502. );
  4503. }
  4504. }
  4505. function buildDirectiveArgs(dir, context) {
  4506. const dirArgs = [];
  4507. const runtime = directiveImportMap.get(dir);
  4508. if (runtime) {
  4509. dirArgs.push(context.helperString(runtime));
  4510. } else {
  4511. const fromSetup = resolveSetupReference("v-" + dir.name, context);
  4512. if (fromSetup) {
  4513. dirArgs.push(fromSetup);
  4514. } else {
  4515. context.helper(RESOLVE_DIRECTIVE);
  4516. context.directives.add(dir.name);
  4517. dirArgs.push(toValidAssetId(dir.name, `directive`));
  4518. }
  4519. }
  4520. const { loc } = dir;
  4521. if (dir.exp)
  4522. dirArgs.push(dir.exp);
  4523. if (dir.arg) {
  4524. if (!dir.exp) {
  4525. dirArgs.push(`void 0`);
  4526. }
  4527. dirArgs.push(dir.arg);
  4528. }
  4529. if (Object.keys(dir.modifiers).length) {
  4530. if (!dir.arg) {
  4531. if (!dir.exp) {
  4532. dirArgs.push(`void 0`);
  4533. }
  4534. dirArgs.push(`void 0`);
  4535. }
  4536. const trueExpression = createSimpleExpression(`true`, false, loc);
  4537. dirArgs.push(
  4538. createObjectExpression(
  4539. dir.modifiers.map(
  4540. (modifier) => createObjectProperty(modifier, trueExpression)
  4541. ),
  4542. loc
  4543. )
  4544. );
  4545. }
  4546. return createArrayExpression(dirArgs, dir.loc);
  4547. }
  4548. function stringifyDynamicPropNames(props) {
  4549. let propsNamesString = `[`;
  4550. for (let i = 0, l = props.length; i < l; i++) {
  4551. propsNamesString += JSON.stringify(props[i]);
  4552. if (i < l - 1)
  4553. propsNamesString += ", ";
  4554. }
  4555. return propsNamesString + `]`;
  4556. }
  4557. function isComponentTag(tag) {
  4558. return tag === "component" || tag === "Component";
  4559. }
  4560. const transformSlotOutlet = (node, context) => {
  4561. if (isSlotOutlet(node)) {
  4562. const { children, loc } = node;
  4563. const { slotName, slotProps } = processSlotOutlet(node, context);
  4564. const slotArgs = [
  4565. context.prefixIdentifiers ? `_ctx.$slots` : `$slots`,
  4566. slotName,
  4567. "{}",
  4568. "undefined",
  4569. "true"
  4570. ];
  4571. let expectedLen = 2;
  4572. if (slotProps) {
  4573. slotArgs[2] = slotProps;
  4574. expectedLen = 3;
  4575. }
  4576. if (children.length) {
  4577. slotArgs[3] = createFunctionExpression([], children, false, false, loc);
  4578. expectedLen = 4;
  4579. }
  4580. if (context.scopeId && !context.slotted) {
  4581. expectedLen = 5;
  4582. }
  4583. slotArgs.splice(expectedLen);
  4584. node.codegenNode = createCallExpression(
  4585. context.helper(RENDER_SLOT),
  4586. slotArgs,
  4587. loc
  4588. );
  4589. }
  4590. };
  4591. function processSlotOutlet(node, context) {
  4592. let slotName = `"default"`;
  4593. let slotProps = void 0;
  4594. const nonNameProps = [];
  4595. for (let i = 0; i < node.props.length; i++) {
  4596. const p = node.props[i];
  4597. if (p.type === 6) {
  4598. if (p.value) {
  4599. if (p.name === "name") {
  4600. slotName = JSON.stringify(p.value.content);
  4601. } else {
  4602. p.name = shared.camelize(p.name);
  4603. nonNameProps.push(p);
  4604. }
  4605. }
  4606. } else {
  4607. if (p.name === "bind" && isStaticArgOf(p.arg, "name")) {
  4608. if (p.exp)
  4609. slotName = p.exp;
  4610. } else {
  4611. if (p.name === "bind" && p.arg && isStaticExp(p.arg)) {
  4612. p.arg.content = shared.camelize(p.arg.content);
  4613. }
  4614. nonNameProps.push(p);
  4615. }
  4616. }
  4617. }
  4618. if (nonNameProps.length > 0) {
  4619. const { props, directives } = buildProps(
  4620. node,
  4621. context,
  4622. nonNameProps,
  4623. false,
  4624. false
  4625. );
  4626. slotProps = props;
  4627. if (directives.length) {
  4628. context.onError(
  4629. createCompilerError(
  4630. 36,
  4631. directives[0].loc
  4632. )
  4633. );
  4634. }
  4635. }
  4636. return {
  4637. slotName,
  4638. slotProps
  4639. };
  4640. }
  4641. const fnExpRE = /^\s*([\w$_]+|(async\s*)?\([^)]*?\))\s*(:[^=]+)?=>|^\s*(async\s+)?function(?:\s+[\w$]+)?\s*\(/;
  4642. const transformOn = (dir, node, context, augmentor) => {
  4643. const { loc, modifiers, arg } = dir;
  4644. if (!dir.exp && !modifiers.length) {
  4645. context.onError(createCompilerError(35, loc));
  4646. }
  4647. let eventName;
  4648. if (arg.type === 4) {
  4649. if (arg.isStatic) {
  4650. let rawName = arg.content;
  4651. if (rawName.startsWith("vue:")) {
  4652. rawName = `vnode-${rawName.slice(4)}`;
  4653. }
  4654. const eventString = node.tagType !== 0 || rawName.startsWith("vnode") || !/[A-Z]/.test(rawName) ? (
  4655. // for non-element and vnode lifecycle event listeners, auto convert
  4656. // it to camelCase. See issue #2249
  4657. shared.toHandlerKey(shared.camelize(rawName))
  4658. ) : (
  4659. // preserve case for plain element listeners that have uppercase
  4660. // letters, as these may be custom elements' custom events
  4661. `on:${rawName}`
  4662. );
  4663. eventName = createSimpleExpression(eventString, true, arg.loc);
  4664. } else {
  4665. eventName = createCompoundExpression([
  4666. `${context.helperString(TO_HANDLER_KEY)}(`,
  4667. arg,
  4668. `)`
  4669. ]);
  4670. }
  4671. } else {
  4672. eventName = arg;
  4673. eventName.children.unshift(`${context.helperString(TO_HANDLER_KEY)}(`);
  4674. eventName.children.push(`)`);
  4675. }
  4676. let exp = dir.exp;
  4677. if (exp && !exp.content.trim()) {
  4678. exp = void 0;
  4679. }
  4680. let shouldCache = context.cacheHandlers && !exp && !context.inVOnce;
  4681. if (exp) {
  4682. const isMemberExp = isMemberExpression(exp.content, context);
  4683. const isInlineStatement = !(isMemberExp || fnExpRE.test(exp.content));
  4684. const hasMultipleStatements = exp.content.includes(`;`);
  4685. if (context.prefixIdentifiers) {
  4686. isInlineStatement && context.addIdentifiers(`$event`);
  4687. exp = dir.exp = processExpression(
  4688. exp,
  4689. context,
  4690. false,
  4691. hasMultipleStatements
  4692. );
  4693. isInlineStatement && context.removeIdentifiers(`$event`);
  4694. shouldCache = context.cacheHandlers && // unnecessary to cache inside v-once
  4695. !context.inVOnce && // runtime constants don't need to be cached
  4696. // (this is analyzed by compileScript in SFC <script setup>)
  4697. !(exp.type === 4 && exp.constType > 0) && // #1541 bail if this is a member exp handler passed to a component -
  4698. // we need to use the original function to preserve arity,
  4699. // e.g. <transition> relies on checking cb.length to determine
  4700. // transition end handling. Inline function is ok since its arity
  4701. // is preserved even when cached.
  4702. !(isMemberExp && node.tagType === 1) && // bail if the function references closure variables (v-for, v-slot)
  4703. // it must be passed fresh to avoid stale values.
  4704. !hasScopeRef(exp, context.identifiers);
  4705. if (shouldCache && isMemberExp) {
  4706. if (exp.type === 4) {
  4707. exp.content = `${exp.content} && ${exp.content}(...args)`;
  4708. } else {
  4709. exp.children = [...exp.children, ` && `, ...exp.children, `(...args)`];
  4710. }
  4711. }
  4712. }
  4713. if (isInlineStatement || shouldCache && isMemberExp) {
  4714. exp = createCompoundExpression([
  4715. `${isInlineStatement ? context.isTS ? `($event: any)` : `$event` : `${context.isTS ? `
  4716. //@ts-ignore
  4717. ` : ``}(...args)`} => ${hasMultipleStatements ? `{` : `(`}`,
  4718. exp,
  4719. hasMultipleStatements ? `}` : `)`
  4720. ]);
  4721. }
  4722. }
  4723. let ret = {
  4724. props: [
  4725. createObjectProperty(
  4726. eventName,
  4727. exp || createSimpleExpression(`() => {}`, false, loc)
  4728. )
  4729. ]
  4730. };
  4731. if (augmentor) {
  4732. ret = augmentor(ret);
  4733. }
  4734. if (shouldCache) {
  4735. ret.props[0].value = context.cache(ret.props[0].value);
  4736. }
  4737. ret.props.forEach((p) => p.key.isHandlerKey = true);
  4738. return ret;
  4739. };
  4740. const transformBind = (dir, _node, context) => {
  4741. const { exp, modifiers, loc } = dir;
  4742. const arg = dir.arg;
  4743. if (arg.type !== 4) {
  4744. arg.children.unshift(`(`);
  4745. arg.children.push(`) || ""`);
  4746. } else if (!arg.isStatic) {
  4747. arg.content = `${arg.content} || ""`;
  4748. }
  4749. if (modifiers.includes("camel")) {
  4750. if (arg.type === 4) {
  4751. if (arg.isStatic) {
  4752. arg.content = shared.camelize(arg.content);
  4753. } else {
  4754. arg.content = `${context.helperString(CAMELIZE)}(${arg.content})`;
  4755. }
  4756. } else {
  4757. arg.children.unshift(`${context.helperString(CAMELIZE)}(`);
  4758. arg.children.push(`)`);
  4759. }
  4760. }
  4761. if (!context.inSSR) {
  4762. if (modifiers.includes("prop")) {
  4763. injectPrefix(arg, ".");
  4764. }
  4765. if (modifiers.includes("attr")) {
  4766. injectPrefix(arg, "^");
  4767. }
  4768. }
  4769. if (!exp || exp.type === 4 && !exp.content.trim()) {
  4770. context.onError(createCompilerError(34, loc));
  4771. return {
  4772. props: [createObjectProperty(arg, createSimpleExpression("", true, loc))]
  4773. };
  4774. }
  4775. return {
  4776. props: [createObjectProperty(arg, exp)]
  4777. };
  4778. };
  4779. const injectPrefix = (arg, prefix) => {
  4780. if (arg.type === 4) {
  4781. if (arg.isStatic) {
  4782. arg.content = prefix + arg.content;
  4783. } else {
  4784. arg.content = `\`${prefix}\${${arg.content}}\``;
  4785. }
  4786. } else {
  4787. arg.children.unshift(`'${prefix}' + (`);
  4788. arg.children.push(`)`);
  4789. }
  4790. };
  4791. const transformText = (node, context) => {
  4792. if (node.type === 0 || node.type === 1 || node.type === 11 || node.type === 10) {
  4793. return () => {
  4794. const children = node.children;
  4795. let currentContainer = void 0;
  4796. let hasText = false;
  4797. for (let i = 0; i < children.length; i++) {
  4798. const child = children[i];
  4799. if (isText$1(child)) {
  4800. hasText = true;
  4801. for (let j = i + 1; j < children.length; j++) {
  4802. const next = children[j];
  4803. if (isText$1(next)) {
  4804. if (!currentContainer) {
  4805. currentContainer = children[i] = createCompoundExpression(
  4806. [child],
  4807. child.loc
  4808. );
  4809. }
  4810. currentContainer.children.push(` + `, next);
  4811. children.splice(j, 1);
  4812. j--;
  4813. } else {
  4814. currentContainer = void 0;
  4815. break;
  4816. }
  4817. }
  4818. }
  4819. }
  4820. if (!hasText || // if this is a plain element with a single text child, leave it
  4821. // as-is since the runtime has dedicated fast path for this by directly
  4822. // setting textContent of the element.
  4823. // for component root it's always normalized anyway.
  4824. children.length === 1 && (node.type === 0 || node.type === 1 && node.tagType === 0 && // #3756
  4825. // custom directives can potentially add DOM elements arbitrarily,
  4826. // we need to avoid setting textContent of the element at runtime
  4827. // to avoid accidentally overwriting the DOM elements added
  4828. // by the user through custom directives.
  4829. !node.props.find(
  4830. (p) => p.type === 7 && !context.directiveTransforms[p.name]
  4831. ) && // in compat mode, <template> tags with no special directives
  4832. // will be rendered as a fragment so its children must be
  4833. // converted into vnodes.
  4834. !(node.tag === "template"))) {
  4835. return;
  4836. }
  4837. for (let i = 0; i < children.length; i++) {
  4838. const child = children[i];
  4839. if (isText$1(child) || child.type === 8) {
  4840. const callArgs = [];
  4841. if (child.type !== 2 || child.content !== " ") {
  4842. callArgs.push(child);
  4843. }
  4844. if (!context.ssr && getConstantType(child, context) === 0) {
  4845. callArgs.push(
  4846. 1 + (``)
  4847. );
  4848. }
  4849. children[i] = {
  4850. type: 12,
  4851. content: child,
  4852. loc: child.loc,
  4853. codegenNode: createCallExpression(
  4854. context.helper(CREATE_TEXT),
  4855. callArgs
  4856. )
  4857. };
  4858. }
  4859. }
  4860. };
  4861. }
  4862. };
  4863. const seen$1 = /* @__PURE__ */ new WeakSet();
  4864. const transformOnce = (node, context) => {
  4865. if (node.type === 1 && findDir(node, "once", true)) {
  4866. if (seen$1.has(node) || context.inVOnce || context.inSSR) {
  4867. return;
  4868. }
  4869. seen$1.add(node);
  4870. context.inVOnce = true;
  4871. context.helper(SET_BLOCK_TRACKING);
  4872. return () => {
  4873. context.inVOnce = false;
  4874. const cur = context.currentNode;
  4875. if (cur.codegenNode) {
  4876. cur.codegenNode = context.cache(
  4877. cur.codegenNode,
  4878. true
  4879. /* isVNode */
  4880. );
  4881. }
  4882. };
  4883. }
  4884. };
  4885. const transformModel = (dir, node, context) => {
  4886. const { exp, arg } = dir;
  4887. if (!exp) {
  4888. context.onError(
  4889. createCompilerError(41, dir.loc)
  4890. );
  4891. return createTransformProps();
  4892. }
  4893. const rawExp = exp.loc.source;
  4894. const expString = exp.type === 4 ? exp.content : rawExp;
  4895. const bindingType = context.bindingMetadata[rawExp];
  4896. if (bindingType === "props" || bindingType === "props-aliased") {
  4897. context.onError(createCompilerError(44, exp.loc));
  4898. return createTransformProps();
  4899. }
  4900. const maybeRef = context.inline && (bindingType === "setup-let" || bindingType === "setup-ref" || bindingType === "setup-maybe-ref");
  4901. if (!expString.trim() || !isMemberExpression(expString, context) && !maybeRef) {
  4902. context.onError(
  4903. createCompilerError(42, exp.loc)
  4904. );
  4905. return createTransformProps();
  4906. }
  4907. if (context.prefixIdentifiers && isSimpleIdentifier(expString) && context.identifiers[expString]) {
  4908. context.onError(
  4909. createCompilerError(43, exp.loc)
  4910. );
  4911. return createTransformProps();
  4912. }
  4913. const propName = arg ? arg : createSimpleExpression("modelValue", true);
  4914. const eventName = arg ? isStaticExp(arg) ? `onUpdate:${shared.camelize(arg.content)}` : createCompoundExpression(['"onUpdate:" + ', arg]) : `onUpdate:modelValue`;
  4915. let assignmentExp;
  4916. const eventArg = context.isTS ? `($event: any)` : `$event`;
  4917. if (maybeRef) {
  4918. if (bindingType === "setup-ref") {
  4919. assignmentExp = createCompoundExpression([
  4920. `${eventArg} => ((`,
  4921. createSimpleExpression(rawExp, false, exp.loc),
  4922. `).value = $event)`
  4923. ]);
  4924. } else {
  4925. const altAssignment = bindingType === "setup-let" ? `${rawExp} = $event` : `null`;
  4926. assignmentExp = createCompoundExpression([
  4927. `${eventArg} => (${context.helperString(IS_REF)}(${rawExp}) ? (`,
  4928. createSimpleExpression(rawExp, false, exp.loc),
  4929. `).value = $event : ${altAssignment})`
  4930. ]);
  4931. }
  4932. } else {
  4933. assignmentExp = createCompoundExpression([
  4934. `${eventArg} => ((`,
  4935. exp,
  4936. `) = $event)`
  4937. ]);
  4938. }
  4939. const props = [
  4940. // modelValue: foo
  4941. createObjectProperty(propName, dir.exp),
  4942. // "onUpdate:modelValue": $event => (foo = $event)
  4943. createObjectProperty(eventName, assignmentExp)
  4944. ];
  4945. if (context.prefixIdentifiers && !context.inVOnce && context.cacheHandlers && !hasScopeRef(exp, context.identifiers)) {
  4946. props[1].value = context.cache(props[1].value);
  4947. }
  4948. if (dir.modifiers.length && node.tagType === 1) {
  4949. const modifiers = dir.modifiers.map((m) => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`).join(`, `);
  4950. const modifiersKey = arg ? isStaticExp(arg) ? `${arg.content}Modifiers` : createCompoundExpression([arg, ' + "Modifiers"']) : `modelModifiers`;
  4951. props.push(
  4952. createObjectProperty(
  4953. modifiersKey,
  4954. createSimpleExpression(
  4955. `{ ${modifiers} }`,
  4956. false,
  4957. dir.loc,
  4958. 2
  4959. )
  4960. )
  4961. );
  4962. }
  4963. return createTransformProps(props);
  4964. };
  4965. function createTransformProps(props = []) {
  4966. return { props };
  4967. }
  4968. const validDivisionCharRE = /[\w).+\-_$\]]/;
  4969. const transformFilter = (node, context) => {
  4970. if (!isCompatEnabled("COMPILER_FILTER", context)) {
  4971. return;
  4972. }
  4973. if (node.type === 5) {
  4974. rewriteFilter(node.content, context);
  4975. }
  4976. if (node.type === 1) {
  4977. node.props.forEach((prop) => {
  4978. if (prop.type === 7 && prop.name !== "for" && prop.exp) {
  4979. rewriteFilter(prop.exp, context);
  4980. }
  4981. });
  4982. }
  4983. };
  4984. function rewriteFilter(node, context) {
  4985. if (node.type === 4) {
  4986. parseFilter(node, context);
  4987. } else {
  4988. for (let i = 0; i < node.children.length; i++) {
  4989. const child = node.children[i];
  4990. if (typeof child !== "object")
  4991. continue;
  4992. if (child.type === 4) {
  4993. parseFilter(child, context);
  4994. } else if (child.type === 8) {
  4995. rewriteFilter(node, context);
  4996. } else if (child.type === 5) {
  4997. rewriteFilter(child.content, context);
  4998. }
  4999. }
  5000. }
  5001. }
  5002. function parseFilter(node, context) {
  5003. const exp = node.content;
  5004. let inSingle = false;
  5005. let inDouble = false;
  5006. let inTemplateString = false;
  5007. let inRegex = false;
  5008. let curly = 0;
  5009. let square = 0;
  5010. let paren = 0;
  5011. let lastFilterIndex = 0;
  5012. let c, prev, i, expression, filters = [];
  5013. for (i = 0; i < exp.length; i++) {
  5014. prev = c;
  5015. c = exp.charCodeAt(i);
  5016. if (inSingle) {
  5017. if (c === 39 && prev !== 92)
  5018. inSingle = false;
  5019. } else if (inDouble) {
  5020. if (c === 34 && prev !== 92)
  5021. inDouble = false;
  5022. } else if (inTemplateString) {
  5023. if (c === 96 && prev !== 92)
  5024. inTemplateString = false;
  5025. } else if (inRegex) {
  5026. if (c === 47 && prev !== 92)
  5027. inRegex = false;
  5028. } else if (c === 124 && // pipe
  5029. exp.charCodeAt(i + 1) !== 124 && exp.charCodeAt(i - 1) !== 124 && !curly && !square && !paren) {
  5030. if (expression === void 0) {
  5031. lastFilterIndex = i + 1;
  5032. expression = exp.slice(0, i).trim();
  5033. } else {
  5034. pushFilter();
  5035. }
  5036. } else {
  5037. switch (c) {
  5038. case 34:
  5039. inDouble = true;
  5040. break;
  5041. case 39:
  5042. inSingle = true;
  5043. break;
  5044. case 96:
  5045. inTemplateString = true;
  5046. break;
  5047. case 40:
  5048. paren++;
  5049. break;
  5050. case 41:
  5051. paren--;
  5052. break;
  5053. case 91:
  5054. square++;
  5055. break;
  5056. case 93:
  5057. square--;
  5058. break;
  5059. case 123:
  5060. curly++;
  5061. break;
  5062. case 125:
  5063. curly--;
  5064. break;
  5065. }
  5066. if (c === 47) {
  5067. let j = i - 1;
  5068. let p;
  5069. for (; j >= 0; j--) {
  5070. p = exp.charAt(j);
  5071. if (p !== " ")
  5072. break;
  5073. }
  5074. if (!p || !validDivisionCharRE.test(p)) {
  5075. inRegex = true;
  5076. }
  5077. }
  5078. }
  5079. }
  5080. if (expression === void 0) {
  5081. expression = exp.slice(0, i).trim();
  5082. } else if (lastFilterIndex !== 0) {
  5083. pushFilter();
  5084. }
  5085. function pushFilter() {
  5086. filters.push(exp.slice(lastFilterIndex, i).trim());
  5087. lastFilterIndex = i + 1;
  5088. }
  5089. if (filters.length) {
  5090. for (i = 0; i < filters.length; i++) {
  5091. expression = wrapFilter(expression, filters[i], context);
  5092. }
  5093. node.content = expression;
  5094. }
  5095. }
  5096. function wrapFilter(exp, filter, context) {
  5097. context.helper(RESOLVE_FILTER);
  5098. const i = filter.indexOf("(");
  5099. if (i < 0) {
  5100. context.filters.add(filter);
  5101. return `${toValidAssetId(filter, "filter")}(${exp})`;
  5102. } else {
  5103. const name = filter.slice(0, i);
  5104. const args = filter.slice(i + 1);
  5105. context.filters.add(name);
  5106. return `${toValidAssetId(name, "filter")}(${exp}${args !== ")" ? "," + args : args}`;
  5107. }
  5108. }
  5109. const seen = /* @__PURE__ */ new WeakSet();
  5110. const transformMemo = (node, context) => {
  5111. if (node.type === 1) {
  5112. const dir = findDir(node, "memo");
  5113. if (!dir || seen.has(node)) {
  5114. return;
  5115. }
  5116. seen.add(node);
  5117. return () => {
  5118. const codegenNode = node.codegenNode || context.currentNode.codegenNode;
  5119. if (codegenNode && codegenNode.type === 13) {
  5120. if (node.tagType !== 1) {
  5121. convertToBlock(codegenNode, context);
  5122. }
  5123. node.codegenNode = createCallExpression(context.helper(WITH_MEMO), [
  5124. dir.exp,
  5125. createFunctionExpression(void 0, codegenNode),
  5126. `_cache`,
  5127. String(context.cached++)
  5128. ]);
  5129. }
  5130. };
  5131. }
  5132. };
  5133. function getBaseTransformPreset(prefixIdentifiers) {
  5134. return [
  5135. [
  5136. transformOnce,
  5137. transformIf,
  5138. transformMemo,
  5139. transformFor,
  5140. ...[transformFilter] ,
  5141. ...prefixIdentifiers ? [
  5142. // order is important
  5143. trackVForSlotScopes,
  5144. transformExpression
  5145. ] : [],
  5146. transformSlotOutlet,
  5147. transformElement,
  5148. trackSlotScopes,
  5149. transformText
  5150. ],
  5151. {
  5152. on: transformOn,
  5153. bind: transformBind,
  5154. model: transformModel
  5155. }
  5156. ];
  5157. }
  5158. function baseCompile(template, options = {}) {
  5159. const onError = options.onError || defaultOnError;
  5160. const isModuleMode = options.mode === "module";
  5161. const prefixIdentifiers = options.prefixIdentifiers === true || isModuleMode;
  5162. if (!prefixIdentifiers && options.cacheHandlers) {
  5163. onError(createCompilerError(49));
  5164. }
  5165. if (options.scopeId && !isModuleMode) {
  5166. onError(createCompilerError(50));
  5167. }
  5168. const ast = shared.isString(template) ? baseParse(template, options) : template;
  5169. const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(prefixIdentifiers);
  5170. if (options.isTS) {
  5171. const { expressionPlugins } = options;
  5172. if (!expressionPlugins || !expressionPlugins.includes("typescript")) {
  5173. options.expressionPlugins = [...expressionPlugins || [], "typescript"];
  5174. }
  5175. }
  5176. transform(
  5177. ast,
  5178. shared.extend({}, options, {
  5179. prefixIdentifiers,
  5180. nodeTransforms: [
  5181. ...nodeTransforms,
  5182. ...options.nodeTransforms || []
  5183. // user transforms
  5184. ],
  5185. directiveTransforms: shared.extend(
  5186. {},
  5187. directiveTransforms,
  5188. options.directiveTransforms || {}
  5189. // user transforms
  5190. )
  5191. })
  5192. );
  5193. return generate(
  5194. ast,
  5195. shared.extend({}, options, {
  5196. prefixIdentifiers
  5197. })
  5198. );
  5199. }
  5200. const noopDirectiveTransform = () => ({ props: [] });
  5201. exports.generateCodeFrame = shared.generateCodeFrame;
  5202. exports.BASE_TRANSITION = BASE_TRANSITION;
  5203. exports.CAMELIZE = CAMELIZE;
  5204. exports.CAPITALIZE = CAPITALIZE;
  5205. exports.CREATE_BLOCK = CREATE_BLOCK;
  5206. exports.CREATE_COMMENT = CREATE_COMMENT;
  5207. exports.CREATE_ELEMENT_BLOCK = CREATE_ELEMENT_BLOCK;
  5208. exports.CREATE_ELEMENT_VNODE = CREATE_ELEMENT_VNODE;
  5209. exports.CREATE_SLOTS = CREATE_SLOTS;
  5210. exports.CREATE_STATIC = CREATE_STATIC;
  5211. exports.CREATE_TEXT = CREATE_TEXT;
  5212. exports.CREATE_VNODE = CREATE_VNODE;
  5213. exports.FRAGMENT = FRAGMENT;
  5214. exports.GUARD_REACTIVE_PROPS = GUARD_REACTIVE_PROPS;
  5215. exports.IS_MEMO_SAME = IS_MEMO_SAME;
  5216. exports.IS_REF = IS_REF;
  5217. exports.KEEP_ALIVE = KEEP_ALIVE;
  5218. exports.MERGE_PROPS = MERGE_PROPS;
  5219. exports.NORMALIZE_CLASS = NORMALIZE_CLASS;
  5220. exports.NORMALIZE_PROPS = NORMALIZE_PROPS;
  5221. exports.NORMALIZE_STYLE = NORMALIZE_STYLE;
  5222. exports.OPEN_BLOCK = OPEN_BLOCK;
  5223. exports.POP_SCOPE_ID = POP_SCOPE_ID;
  5224. exports.PUSH_SCOPE_ID = PUSH_SCOPE_ID;
  5225. exports.RENDER_LIST = RENDER_LIST;
  5226. exports.RENDER_SLOT = RENDER_SLOT;
  5227. exports.RESOLVE_COMPONENT = RESOLVE_COMPONENT;
  5228. exports.RESOLVE_DIRECTIVE = RESOLVE_DIRECTIVE;
  5229. exports.RESOLVE_DYNAMIC_COMPONENT = RESOLVE_DYNAMIC_COMPONENT;
  5230. exports.RESOLVE_FILTER = RESOLVE_FILTER;
  5231. exports.SET_BLOCK_TRACKING = SET_BLOCK_TRACKING;
  5232. exports.SUSPENSE = SUSPENSE;
  5233. exports.TELEPORT = TELEPORT;
  5234. exports.TO_DISPLAY_STRING = TO_DISPLAY_STRING;
  5235. exports.TO_HANDLERS = TO_HANDLERS;
  5236. exports.TO_HANDLER_KEY = TO_HANDLER_KEY;
  5237. exports.TS_NODE_TYPES = TS_NODE_TYPES;
  5238. exports.UNREF = UNREF;
  5239. exports.WITH_CTX = WITH_CTX;
  5240. exports.WITH_DIRECTIVES = WITH_DIRECTIVES;
  5241. exports.WITH_MEMO = WITH_MEMO;
  5242. exports.advancePositionWithClone = advancePositionWithClone;
  5243. exports.advancePositionWithMutation = advancePositionWithMutation;
  5244. exports.assert = assert;
  5245. exports.baseCompile = baseCompile;
  5246. exports.baseParse = baseParse;
  5247. exports.buildDirectiveArgs = buildDirectiveArgs;
  5248. exports.buildProps = buildProps;
  5249. exports.buildSlots = buildSlots;
  5250. exports.checkCompatEnabled = checkCompatEnabled;
  5251. exports.convertToBlock = convertToBlock;
  5252. exports.createArrayExpression = createArrayExpression;
  5253. exports.createAssignmentExpression = createAssignmentExpression;
  5254. exports.createBlockStatement = createBlockStatement;
  5255. exports.createCacheExpression = createCacheExpression;
  5256. exports.createCallExpression = createCallExpression;
  5257. exports.createCompilerError = createCompilerError;
  5258. exports.createCompoundExpression = createCompoundExpression;
  5259. exports.createConditionalExpression = createConditionalExpression;
  5260. exports.createForLoopParams = createForLoopParams;
  5261. exports.createFunctionExpression = createFunctionExpression;
  5262. exports.createIfStatement = createIfStatement;
  5263. exports.createInterpolation = createInterpolation;
  5264. exports.createObjectExpression = createObjectExpression;
  5265. exports.createObjectProperty = createObjectProperty;
  5266. exports.createReturnStatement = createReturnStatement;
  5267. exports.createRoot = createRoot;
  5268. exports.createSequenceExpression = createSequenceExpression;
  5269. exports.createSimpleExpression = createSimpleExpression;
  5270. exports.createStructuralDirectiveTransform = createStructuralDirectiveTransform;
  5271. exports.createTemplateLiteral = createTemplateLiteral;
  5272. exports.createTransformContext = createTransformContext;
  5273. exports.createVNodeCall = createVNodeCall;
  5274. exports.extractIdentifiers = extractIdentifiers;
  5275. exports.findDir = findDir;
  5276. exports.findProp = findProp;
  5277. exports.generate = generate;
  5278. exports.getBaseTransformPreset = getBaseTransformPreset;
  5279. exports.getConstantType = getConstantType;
  5280. exports.getInnerRange = getInnerRange;
  5281. exports.getMemoedVNodeCall = getMemoedVNodeCall;
  5282. exports.getVNodeBlockHelper = getVNodeBlockHelper;
  5283. exports.getVNodeHelper = getVNodeHelper;
  5284. exports.hasDynamicKeyVBind = hasDynamicKeyVBind;
  5285. exports.hasScopeRef = hasScopeRef;
  5286. exports.helperNameMap = helperNameMap;
  5287. exports.injectProp = injectProp;
  5288. exports.isBuiltInType = isBuiltInType;
  5289. exports.isCoreComponent = isCoreComponent;
  5290. exports.isFunctionType = isFunctionType;
  5291. exports.isInDestructureAssignment = isInDestructureAssignment;
  5292. exports.isMemberExpression = isMemberExpression;
  5293. exports.isMemberExpressionBrowser = isMemberExpressionBrowser;
  5294. exports.isMemberExpressionNode = isMemberExpressionNode;
  5295. exports.isReferencedIdentifier = isReferencedIdentifier;
  5296. exports.isSimpleIdentifier = isSimpleIdentifier;
  5297. exports.isSlotOutlet = isSlotOutlet;
  5298. exports.isStaticArgOf = isStaticArgOf;
  5299. exports.isStaticExp = isStaticExp;
  5300. exports.isStaticProperty = isStaticProperty;
  5301. exports.isStaticPropertyKey = isStaticPropertyKey;
  5302. exports.isTemplateNode = isTemplateNode;
  5303. exports.isText = isText$1;
  5304. exports.isVSlot = isVSlot;
  5305. exports.locStub = locStub;
  5306. exports.noopDirectiveTransform = noopDirectiveTransform;
  5307. exports.processExpression = processExpression;
  5308. exports.processFor = processFor;
  5309. exports.processIf = processIf;
  5310. exports.processSlotOutlet = processSlotOutlet;
  5311. exports.registerRuntimeHelpers = registerRuntimeHelpers;
  5312. exports.resolveComponentType = resolveComponentType;
  5313. exports.stringifyExpression = stringifyExpression;
  5314. exports.toValidAssetId = toValidAssetId;
  5315. exports.trackSlotScopes = trackSlotScopes;
  5316. exports.trackVForSlotScopes = trackVForSlotScopes;
  5317. exports.transform = transform;
  5318. exports.transformBind = transformBind;
  5319. exports.transformElement = transformElement;
  5320. exports.transformExpression = transformExpression;
  5321. exports.transformModel = transformModel;
  5322. exports.transformOn = transformOn;
  5323. exports.traverseNode = traverseNode;
  5324. exports.walkBlockDeclarations = walkBlockDeclarations;
  5325. exports.walkFunctionParams = walkFunctionParams;
  5326. exports.walkIdentifiers = walkIdentifiers;
  5327. exports.warnDeprecation = warnDeprecation;