compiler-core.cjs.prod.js 156 KB

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