compiler-core.cjs.js 161 KB

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