compiler-core.esm-bundler.js 140 KB

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