compiler-ssr.cjs.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /**
  2. * @vue/compiler-ssr v3.5.12
  3. * (c) 2018-present Yuxi (Evan) You and Vue contributors
  4. * @license MIT
  5. **/
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var compilerDom = require('@vue/compiler-dom');
  9. var shared = require('@vue/shared');
  10. const SSR_INTERPOLATE = Symbol(`ssrInterpolate`);
  11. const SSR_RENDER_VNODE = Symbol(`ssrRenderVNode`);
  12. const SSR_RENDER_COMPONENT = Symbol(`ssrRenderComponent`);
  13. const SSR_RENDER_SLOT = Symbol(`ssrRenderSlot`);
  14. const SSR_RENDER_SLOT_INNER = Symbol(`ssrRenderSlotInner`);
  15. const SSR_RENDER_CLASS = Symbol(`ssrRenderClass`);
  16. const SSR_RENDER_STYLE = Symbol(`ssrRenderStyle`);
  17. const SSR_RENDER_ATTRS = Symbol(`ssrRenderAttrs`);
  18. const SSR_RENDER_ATTR = Symbol(`ssrRenderAttr`);
  19. const SSR_RENDER_DYNAMIC_ATTR = Symbol(`ssrRenderDynamicAttr`);
  20. const SSR_RENDER_LIST = Symbol(`ssrRenderList`);
  21. const SSR_INCLUDE_BOOLEAN_ATTR = Symbol(
  22. `ssrIncludeBooleanAttr`
  23. );
  24. const SSR_LOOSE_EQUAL = Symbol(`ssrLooseEqual`);
  25. const SSR_LOOSE_CONTAIN = Symbol(`ssrLooseContain`);
  26. const SSR_RENDER_DYNAMIC_MODEL = Symbol(
  27. `ssrRenderDynamicModel`
  28. );
  29. const SSR_GET_DYNAMIC_MODEL_PROPS = Symbol(
  30. `ssrGetDynamicModelProps`
  31. );
  32. const SSR_RENDER_TELEPORT = Symbol(`ssrRenderTeleport`);
  33. const SSR_RENDER_SUSPENSE = Symbol(`ssrRenderSuspense`);
  34. const SSR_GET_DIRECTIVE_PROPS = Symbol(`ssrGetDirectiveProps`);
  35. const ssrHelpers = {
  36. [SSR_INTERPOLATE]: `ssrInterpolate`,
  37. [SSR_RENDER_VNODE]: `ssrRenderVNode`,
  38. [SSR_RENDER_COMPONENT]: `ssrRenderComponent`,
  39. [SSR_RENDER_SLOT]: `ssrRenderSlot`,
  40. [SSR_RENDER_SLOT_INNER]: `ssrRenderSlotInner`,
  41. [SSR_RENDER_CLASS]: `ssrRenderClass`,
  42. [SSR_RENDER_STYLE]: `ssrRenderStyle`,
  43. [SSR_RENDER_ATTRS]: `ssrRenderAttrs`,
  44. [SSR_RENDER_ATTR]: `ssrRenderAttr`,
  45. [SSR_RENDER_DYNAMIC_ATTR]: `ssrRenderDynamicAttr`,
  46. [SSR_RENDER_LIST]: `ssrRenderList`,
  47. [SSR_INCLUDE_BOOLEAN_ATTR]: `ssrIncludeBooleanAttr`,
  48. [SSR_LOOSE_EQUAL]: `ssrLooseEqual`,
  49. [SSR_LOOSE_CONTAIN]: `ssrLooseContain`,
  50. [SSR_RENDER_DYNAMIC_MODEL]: `ssrRenderDynamicModel`,
  51. [SSR_GET_DYNAMIC_MODEL_PROPS]: `ssrGetDynamicModelProps`,
  52. [SSR_RENDER_TELEPORT]: `ssrRenderTeleport`,
  53. [SSR_RENDER_SUSPENSE]: `ssrRenderSuspense`,
  54. [SSR_GET_DIRECTIVE_PROPS]: `ssrGetDirectiveProps`
  55. };
  56. compilerDom.registerRuntimeHelpers(ssrHelpers);
  57. const ssrTransformIf = compilerDom.createStructuralDirectiveTransform(
  58. /^(if|else|else-if)$/,
  59. compilerDom.processIf
  60. );
  61. function ssrProcessIf(node, context, disableNestedFragments = false, disableComment = false) {
  62. const [rootBranch] = node.branches;
  63. const ifStatement = compilerDom.createIfStatement(
  64. rootBranch.condition,
  65. processIfBranch(rootBranch, context, disableNestedFragments)
  66. );
  67. context.pushStatement(ifStatement);
  68. let currentIf = ifStatement;
  69. for (let i = 1; i < node.branches.length; i++) {
  70. const branch = node.branches[i];
  71. const branchBlockStatement = processIfBranch(
  72. branch,
  73. context,
  74. disableNestedFragments
  75. );
  76. if (branch.condition) {
  77. currentIf = currentIf.alternate = compilerDom.createIfStatement(
  78. branch.condition,
  79. branchBlockStatement
  80. );
  81. } else {
  82. currentIf.alternate = branchBlockStatement;
  83. }
  84. }
  85. if (!currentIf.alternate && !disableComment) {
  86. currentIf.alternate = compilerDom.createBlockStatement([
  87. compilerDom.createCallExpression(`_push`, ["`<!---->`"])
  88. ]);
  89. }
  90. }
  91. function processIfBranch(branch, context, disableNestedFragments = false) {
  92. const { children } = branch;
  93. const needFragmentWrapper = !disableNestedFragments && (children.length !== 1 || children[0].type !== 1) && // optimize away nested fragments when the only child is a ForNode
  94. !(children.length === 1 && children[0].type === 11);
  95. return processChildrenAsStatement(branch, context, needFragmentWrapper);
  96. }
  97. const ssrTransformFor = compilerDom.createStructuralDirectiveTransform("for", compilerDom.processFor);
  98. function ssrProcessFor(node, context, disableNestedFragments = false) {
  99. const needFragmentWrapper = !disableNestedFragments && (node.children.length !== 1 || node.children[0].type !== 1);
  100. const renderLoop = compilerDom.createFunctionExpression(
  101. compilerDom.createForLoopParams(node.parseResult)
  102. );
  103. renderLoop.body = processChildrenAsStatement(
  104. node,
  105. context,
  106. needFragmentWrapper
  107. );
  108. if (!disableNestedFragments) {
  109. context.pushStringPart(`<!--[-->`);
  110. }
  111. context.pushStatement(
  112. compilerDom.createCallExpression(context.helper(SSR_RENDER_LIST), [
  113. node.source,
  114. renderLoop
  115. ])
  116. );
  117. if (!disableNestedFragments) {
  118. context.pushStringPart(`<!--]-->`);
  119. }
  120. }
  121. const ssrTransformSlotOutlet = (node, context) => {
  122. if (compilerDom.isSlotOutlet(node)) {
  123. const { slotName, slotProps } = compilerDom.processSlotOutlet(node, context);
  124. const args = [
  125. `_ctx.$slots`,
  126. slotName,
  127. slotProps || `{}`,
  128. // fallback content placeholder. will be replaced in the process phase
  129. `null`,
  130. `_push`,
  131. `_parent`
  132. ];
  133. if (context.scopeId && context.slotted !== false) {
  134. args.push(`"${context.scopeId}-s"`);
  135. }
  136. let method = SSR_RENDER_SLOT;
  137. let parent = context.parent;
  138. if (parent) {
  139. const children = parent.children;
  140. if (parent.type === 10) {
  141. parent = context.grandParent;
  142. }
  143. let componentType;
  144. if (parent.type === 1 && parent.tagType === 1 && ((componentType = compilerDom.resolveComponentType(parent, context, true)) === compilerDom.TRANSITION || componentType === compilerDom.TRANSITION_GROUP) && children.filter((c) => c.type === 1).length === 1) {
  145. method = SSR_RENDER_SLOT_INNER;
  146. if (!(context.scopeId && context.slotted !== false)) {
  147. args.push("null");
  148. }
  149. args.push("true");
  150. }
  151. }
  152. node.ssrCodegenNode = compilerDom.createCallExpression(context.helper(method), args);
  153. }
  154. };
  155. function ssrProcessSlotOutlet(node, context) {
  156. const renderCall = node.ssrCodegenNode;
  157. if (node.children.length) {
  158. const fallbackRenderFn = compilerDom.createFunctionExpression([]);
  159. fallbackRenderFn.body = processChildrenAsStatement(node, context);
  160. renderCall.arguments[3] = fallbackRenderFn;
  161. }
  162. if (context.withSlotScopeId) {
  163. const slotScopeId = renderCall.arguments[6];
  164. renderCall.arguments[6] = slotScopeId ? `${slotScopeId} + _scopeId` : `_scopeId`;
  165. }
  166. context.pushStatement(node.ssrCodegenNode);
  167. }
  168. function createSSRCompilerError(code, loc) {
  169. return compilerDom.createCompilerError(code, loc, SSRErrorMessages);
  170. }
  171. const SSRErrorMessages = {
  172. [65]: `Unsafe attribute name for SSR.`,
  173. [66]: `Missing the 'to' prop on teleport element.`,
  174. [67]: `Invalid AST node during SSR transform.`
  175. };
  176. function ssrProcessTeleport(node, context) {
  177. const targetProp = compilerDom.findProp(node, "to");
  178. if (!targetProp) {
  179. context.onError(
  180. createSSRCompilerError(66, node.loc)
  181. );
  182. return;
  183. }
  184. let target;
  185. if (targetProp.type === 6) {
  186. target = targetProp.value && compilerDom.createSimpleExpression(targetProp.value.content, true);
  187. } else {
  188. target = targetProp.exp;
  189. }
  190. if (!target) {
  191. context.onError(
  192. createSSRCompilerError(
  193. 66,
  194. targetProp.loc
  195. )
  196. );
  197. return;
  198. }
  199. const disabledProp = compilerDom.findProp(
  200. node,
  201. "disabled",
  202. false,
  203. true
  204. /* allow empty */
  205. );
  206. const disabled = disabledProp ? disabledProp.type === 6 ? `true` : disabledProp.exp || `false` : `false`;
  207. const contentRenderFn = compilerDom.createFunctionExpression(
  208. [`_push`],
  209. void 0,
  210. // Body is added later
  211. true,
  212. // newline
  213. false,
  214. // isSlot
  215. node.loc
  216. );
  217. contentRenderFn.body = processChildrenAsStatement(node, context);
  218. context.pushStatement(
  219. compilerDom.createCallExpression(context.helper(SSR_RENDER_TELEPORT), [
  220. `_push`,
  221. contentRenderFn,
  222. target,
  223. disabled,
  224. `_parent`
  225. ])
  226. );
  227. }
  228. const wipMap$3 = /* @__PURE__ */ new WeakMap();
  229. function ssrTransformSuspense(node, context) {
  230. return () => {
  231. if (node.children.length) {
  232. const wipEntry = {
  233. slotsExp: null,
  234. // to be immediately set
  235. wipSlots: []
  236. };
  237. wipMap$3.set(node, wipEntry);
  238. wipEntry.slotsExp = compilerDom.buildSlots(
  239. node,
  240. context,
  241. (_props, _vForExp, children, loc) => {
  242. const fn = compilerDom.createFunctionExpression(
  243. [],
  244. void 0,
  245. // no return, assign body later
  246. true,
  247. // newline
  248. false,
  249. // suspense slots are not treated as normal slots
  250. loc
  251. );
  252. wipEntry.wipSlots.push({
  253. fn,
  254. children
  255. });
  256. return fn;
  257. }
  258. ).slots;
  259. }
  260. };
  261. }
  262. function ssrProcessSuspense(node, context) {
  263. const wipEntry = wipMap$3.get(node);
  264. if (!wipEntry) {
  265. return;
  266. }
  267. const { slotsExp, wipSlots } = wipEntry;
  268. for (let i = 0; i < wipSlots.length; i++) {
  269. const slot = wipSlots[i];
  270. slot.fn.body = processChildrenAsStatement(slot, context);
  271. }
  272. context.pushStatement(
  273. compilerDom.createCallExpression(context.helper(SSR_RENDER_SUSPENSE), [
  274. `_push`,
  275. slotsExp
  276. ])
  277. );
  278. }
  279. const rawChildrenMap = /* @__PURE__ */ new WeakMap();
  280. const ssrTransformElement = (node, context) => {
  281. if (node.type !== 1 || node.tagType !== 0) {
  282. return;
  283. }
  284. return function ssrPostTransformElement() {
  285. const openTag = [`<${node.tag}`];
  286. const needTagForRuntime = node.tag === "textarea" || node.tag.indexOf("-") > 0;
  287. const hasDynamicVBind = compilerDom.hasDynamicKeyVBind(node);
  288. const hasCustomDir = node.props.some(
  289. (p) => p.type === 7 && !shared.isBuiltInDirective(p.name)
  290. );
  291. const needMergeProps = hasDynamicVBind || hasCustomDir;
  292. if (needMergeProps) {
  293. const { props, directives } = compilerDom.buildProps(
  294. node,
  295. context,
  296. node.props,
  297. false,
  298. false,
  299. true
  300. );
  301. if (props || directives.length) {
  302. const mergedProps = buildSSRProps(props, directives, context);
  303. const propsExp = compilerDom.createCallExpression(
  304. context.helper(SSR_RENDER_ATTRS),
  305. [mergedProps]
  306. );
  307. if (node.tag === "textarea") {
  308. const existingText = node.children[0];
  309. if (!existingText || existingText.type !== 5) {
  310. const tempId = `_temp${context.temps++}`;
  311. propsExp.arguments = [
  312. compilerDom.createAssignmentExpression(
  313. compilerDom.createSimpleExpression(tempId, false),
  314. mergedProps
  315. )
  316. ];
  317. rawChildrenMap.set(
  318. node,
  319. compilerDom.createCallExpression(context.helper(SSR_INTERPOLATE), [
  320. compilerDom.createConditionalExpression(
  321. compilerDom.createSimpleExpression(`"value" in ${tempId}`, false),
  322. compilerDom.createSimpleExpression(`${tempId}.value`, false),
  323. compilerDom.createSimpleExpression(
  324. existingText ? existingText.content : ``,
  325. true
  326. ),
  327. false
  328. )
  329. ])
  330. );
  331. }
  332. } else if (node.tag === "input") {
  333. const vModel = findVModel(node);
  334. if (vModel) {
  335. const tempId = `_temp${context.temps++}`;
  336. const tempExp = compilerDom.createSimpleExpression(tempId, false);
  337. propsExp.arguments = [
  338. compilerDom.createSequenceExpression([
  339. compilerDom.createAssignmentExpression(tempExp, mergedProps),
  340. compilerDom.createCallExpression(context.helper(compilerDom.MERGE_PROPS), [
  341. tempExp,
  342. compilerDom.createCallExpression(
  343. context.helper(SSR_GET_DYNAMIC_MODEL_PROPS),
  344. [
  345. tempExp,
  346. // existing props
  347. vModel.exp
  348. // model
  349. ]
  350. )
  351. ])
  352. ])
  353. ];
  354. }
  355. } else if (directives.length && !node.children.length) {
  356. const tempId = `_temp${context.temps++}`;
  357. propsExp.arguments = [
  358. compilerDom.createAssignmentExpression(
  359. compilerDom.createSimpleExpression(tempId, false),
  360. mergedProps
  361. )
  362. ];
  363. rawChildrenMap.set(
  364. node,
  365. compilerDom.createConditionalExpression(
  366. compilerDom.createSimpleExpression(`"textContent" in ${tempId}`, false),
  367. compilerDom.createCallExpression(context.helper(SSR_INTERPOLATE), [
  368. compilerDom.createSimpleExpression(`${tempId}.textContent`, false)
  369. ]),
  370. compilerDom.createSimpleExpression(`${tempId}.innerHTML ?? ''`, false),
  371. false
  372. )
  373. );
  374. }
  375. if (needTagForRuntime) {
  376. propsExp.arguments.push(`"${node.tag}"`);
  377. }
  378. openTag.push(propsExp);
  379. }
  380. }
  381. let dynamicClassBinding = void 0;
  382. let staticClassBinding = void 0;
  383. let dynamicStyleBinding = void 0;
  384. for (let i = 0; i < node.props.length; i++) {
  385. const prop = node.props[i];
  386. if (node.tag === "input" && isTrueFalseValue(prop)) {
  387. continue;
  388. }
  389. if (prop.type === 7) {
  390. if (prop.name === "html" && prop.exp) {
  391. rawChildrenMap.set(
  392. node,
  393. compilerDom.createCompoundExpression([`(`, prop.exp, `) ?? ''`])
  394. );
  395. } else if (prop.name === "text" && prop.exp) {
  396. node.children = [compilerDom.createInterpolation(prop.exp, prop.loc)];
  397. } else if (prop.name === "slot") {
  398. context.onError(
  399. compilerDom.createCompilerError(40, prop.loc)
  400. );
  401. } else if (isTextareaWithValue(node, prop) && prop.exp) {
  402. if (!needMergeProps) {
  403. node.children = [compilerDom.createInterpolation(prop.exp, prop.loc)];
  404. }
  405. } else if (!needMergeProps && prop.name !== "on") {
  406. const directiveTransform = context.directiveTransforms[prop.name];
  407. if (directiveTransform) {
  408. const { props, ssrTagParts } = directiveTransform(
  409. prop,
  410. node,
  411. context
  412. );
  413. if (ssrTagParts) {
  414. openTag.push(...ssrTagParts);
  415. }
  416. for (let j = 0; j < props.length; j++) {
  417. const { key, value } = props[j];
  418. if (compilerDom.isStaticExp(key)) {
  419. let attrName = key.content;
  420. if (attrName === "key" || attrName === "ref") {
  421. continue;
  422. }
  423. if (attrName === "class") {
  424. openTag.push(
  425. ` class="`,
  426. dynamicClassBinding = compilerDom.createCallExpression(
  427. context.helper(SSR_RENDER_CLASS),
  428. [value]
  429. ),
  430. `"`
  431. );
  432. } else if (attrName === "style") {
  433. if (dynamicStyleBinding) {
  434. mergeCall(dynamicStyleBinding, value);
  435. } else {
  436. openTag.push(
  437. ` style="`,
  438. dynamicStyleBinding = compilerDom.createCallExpression(
  439. context.helper(SSR_RENDER_STYLE),
  440. [value]
  441. ),
  442. `"`
  443. );
  444. }
  445. } else {
  446. attrName = node.tag.indexOf("-") > 0 ? attrName : shared.propsToAttrMap[attrName] || attrName.toLowerCase();
  447. if (shared.isBooleanAttr(attrName)) {
  448. openTag.push(
  449. compilerDom.createConditionalExpression(
  450. compilerDom.createCallExpression(
  451. context.helper(SSR_INCLUDE_BOOLEAN_ATTR),
  452. [value]
  453. ),
  454. compilerDom.createSimpleExpression(" " + attrName, true),
  455. compilerDom.createSimpleExpression("", true),
  456. false
  457. )
  458. );
  459. } else if (shared.isSSRSafeAttrName(attrName)) {
  460. openTag.push(
  461. compilerDom.createCallExpression(context.helper(SSR_RENDER_ATTR), [
  462. key,
  463. value
  464. ])
  465. );
  466. } else {
  467. context.onError(
  468. createSSRCompilerError(
  469. 65,
  470. key.loc
  471. )
  472. );
  473. }
  474. }
  475. } else {
  476. const args = [key, value];
  477. if (needTagForRuntime) {
  478. args.push(`"${node.tag}"`);
  479. }
  480. openTag.push(
  481. compilerDom.createCallExpression(
  482. context.helper(SSR_RENDER_DYNAMIC_ATTR),
  483. args
  484. )
  485. );
  486. }
  487. }
  488. }
  489. }
  490. } else {
  491. const name = prop.name;
  492. if (node.tag === "textarea" && name === "value" && prop.value) {
  493. rawChildrenMap.set(node, shared.escapeHtml(prop.value.content));
  494. } else if (!needMergeProps) {
  495. if (name === "key" || name === "ref") {
  496. continue;
  497. }
  498. if (name === "class" && prop.value) {
  499. staticClassBinding = JSON.stringify(prop.value.content);
  500. }
  501. openTag.push(
  502. ` ${prop.name}` + (prop.value ? `="${shared.escapeHtml(prop.value.content)}"` : ``)
  503. );
  504. }
  505. }
  506. }
  507. if (dynamicClassBinding && staticClassBinding) {
  508. mergeCall(dynamicClassBinding, staticClassBinding);
  509. removeStaticBinding(openTag, "class");
  510. }
  511. if (context.scopeId) {
  512. openTag.push(` ${context.scopeId}`);
  513. }
  514. node.ssrCodegenNode = compilerDom.createTemplateLiteral(openTag);
  515. };
  516. };
  517. function buildSSRProps(props, directives, context) {
  518. let mergePropsArgs = [];
  519. if (props) {
  520. if (props.type === 14) {
  521. mergePropsArgs = props.arguments;
  522. } else {
  523. mergePropsArgs.push(props);
  524. }
  525. }
  526. if (directives.length) {
  527. for (const dir of directives) {
  528. mergePropsArgs.push(
  529. compilerDom.createCallExpression(context.helper(SSR_GET_DIRECTIVE_PROPS), [
  530. `_ctx`,
  531. ...compilerDom.buildDirectiveArgs(dir, context).elements
  532. ])
  533. );
  534. }
  535. }
  536. return mergePropsArgs.length > 1 ? compilerDom.createCallExpression(context.helper(compilerDom.MERGE_PROPS), mergePropsArgs) : mergePropsArgs[0];
  537. }
  538. function isTrueFalseValue(prop) {
  539. if (prop.type === 7) {
  540. return prop.name === "bind" && prop.arg && compilerDom.isStaticExp(prop.arg) && (prop.arg.content === "true-value" || prop.arg.content === "false-value");
  541. } else {
  542. return prop.name === "true-value" || prop.name === "false-value";
  543. }
  544. }
  545. function isTextareaWithValue(node, prop) {
  546. return !!(node.tag === "textarea" && prop.name === "bind" && compilerDom.isStaticArgOf(prop.arg, "value"));
  547. }
  548. function mergeCall(call, arg) {
  549. const existing = call.arguments[0];
  550. if (existing.type === 17) {
  551. existing.elements.push(arg);
  552. } else {
  553. call.arguments[0] = compilerDom.createArrayExpression([existing, arg]);
  554. }
  555. }
  556. function removeStaticBinding(tag, binding) {
  557. const regExp = new RegExp(`^ ${binding}=".+"$`);
  558. const i = tag.findIndex((e) => typeof e === "string" && regExp.test(e));
  559. if (i > -1) {
  560. tag.splice(i, 1);
  561. }
  562. }
  563. function findVModel(node) {
  564. return node.props.find(
  565. (p) => p.type === 7 && p.name === "model" && p.exp
  566. );
  567. }
  568. function ssrProcessElement(node, context) {
  569. const isVoidTag = context.options.isVoidTag || shared.NO;
  570. const elementsToAdd = node.ssrCodegenNode.elements;
  571. for (let j = 0; j < elementsToAdd.length; j++) {
  572. context.pushStringPart(elementsToAdd[j]);
  573. }
  574. if (context.withSlotScopeId) {
  575. context.pushStringPart(compilerDom.createSimpleExpression(`_scopeId`, false));
  576. }
  577. context.pushStringPart(`>`);
  578. const rawChildren = rawChildrenMap.get(node);
  579. if (rawChildren) {
  580. context.pushStringPart(rawChildren);
  581. } else if (node.children.length) {
  582. processChildren(node, context);
  583. }
  584. if (!isVoidTag(node.tag)) {
  585. context.pushStringPart(`</${node.tag}>`);
  586. }
  587. }
  588. const wipMap$2 = /* @__PURE__ */ new WeakMap();
  589. function ssrTransformTransitionGroup(node, context) {
  590. return () => {
  591. const tag = compilerDom.findProp(node, "tag");
  592. if (tag) {
  593. const otherProps = node.props.filter((p) => p !== tag);
  594. const { props, directives } = compilerDom.buildProps(
  595. node,
  596. context,
  597. otherProps,
  598. true,
  599. false,
  600. true
  601. );
  602. let propsExp = null;
  603. if (props || directives.length) {
  604. propsExp = compilerDom.createCallExpression(context.helper(SSR_RENDER_ATTRS), [
  605. buildSSRProps(props, directives, context)
  606. ]);
  607. }
  608. wipMap$2.set(node, {
  609. tag,
  610. propsExp,
  611. scopeId: context.scopeId || null
  612. });
  613. }
  614. };
  615. }
  616. function ssrProcessTransitionGroup(node, context) {
  617. const entry = wipMap$2.get(node);
  618. if (entry) {
  619. const { tag, propsExp, scopeId } = entry;
  620. if (tag.type === 7) {
  621. context.pushStringPart(`<`);
  622. context.pushStringPart(tag.exp);
  623. if (propsExp) {
  624. context.pushStringPart(propsExp);
  625. }
  626. if (scopeId) {
  627. context.pushStringPart(` ${scopeId}`);
  628. }
  629. context.pushStringPart(`>`);
  630. processChildren(
  631. node,
  632. context,
  633. false,
  634. /**
  635. * TransitionGroup has the special runtime behavior of flattening and
  636. * concatenating all children into a single fragment (in order for them to
  637. * be patched using the same key map) so we need to account for that here
  638. * by disabling nested fragment wrappers from being generated.
  639. */
  640. true,
  641. /**
  642. * TransitionGroup filters out comment children at runtime and thus
  643. * doesn't expect comments to be present during hydration. We need to
  644. * account for that by disabling the empty comment that is otherwise
  645. * rendered for a falsy v-if that has no v-else specified. (#6715)
  646. */
  647. true
  648. );
  649. context.pushStringPart(`</`);
  650. context.pushStringPart(tag.exp);
  651. context.pushStringPart(`>`);
  652. } else {
  653. context.pushStringPart(`<${tag.value.content}`);
  654. if (propsExp) {
  655. context.pushStringPart(propsExp);
  656. }
  657. if (scopeId) {
  658. context.pushStringPart(` ${scopeId}`);
  659. }
  660. context.pushStringPart(`>`);
  661. processChildren(node, context, false, true, true);
  662. context.pushStringPart(`</${tag.value.content}>`);
  663. }
  664. } else {
  665. processChildren(node, context, true, true, true);
  666. }
  667. }
  668. const wipMap$1 = /* @__PURE__ */ new WeakMap();
  669. function ssrTransformTransition(node, context) {
  670. return () => {
  671. const appear = compilerDom.findProp(node, "appear", false, true);
  672. wipMap$1.set(node, !!appear);
  673. };
  674. }
  675. function ssrProcessTransition(node, context) {
  676. node.children = node.children.filter((c) => c.type !== 3);
  677. const appear = wipMap$1.get(node);
  678. if (appear) {
  679. context.pushStringPart(`<template>`);
  680. processChildren(node, context, false, true);
  681. context.pushStringPart(`</template>`);
  682. } else {
  683. processChildren(node, context, false, true);
  684. }
  685. }
  686. const wipMap = /* @__PURE__ */ new WeakMap();
  687. const WIP_SLOT = Symbol();
  688. const componentTypeMap = /* @__PURE__ */ new WeakMap();
  689. const ssrTransformComponent = (node, context) => {
  690. if (node.type !== 1 || node.tagType !== 1) {
  691. return;
  692. }
  693. const component = compilerDom.resolveComponentType(
  694. node,
  695. context,
  696. true
  697. /* ssr */
  698. );
  699. const isDynamicComponent = shared.isObject(component) && component.callee === compilerDom.RESOLVE_DYNAMIC_COMPONENT;
  700. componentTypeMap.set(node, component);
  701. if (shared.isSymbol(component)) {
  702. if (component === compilerDom.SUSPENSE) {
  703. return ssrTransformSuspense(node, context);
  704. } else if (component === compilerDom.TRANSITION_GROUP) {
  705. return ssrTransformTransitionGroup(node, context);
  706. } else if (component === compilerDom.TRANSITION) {
  707. return ssrTransformTransition(node);
  708. }
  709. return;
  710. }
  711. const vnodeBranches = [];
  712. const clonedNode = clone(node);
  713. return function ssrPostTransformComponent() {
  714. if (clonedNode.children.length) {
  715. compilerDom.buildSlots(clonedNode, context, (props, vFor, children) => {
  716. vnodeBranches.push(
  717. createVNodeSlotBranch(props, vFor, children, context)
  718. );
  719. return compilerDom.createFunctionExpression(void 0);
  720. });
  721. }
  722. let propsExp = `null`;
  723. if (node.props.length) {
  724. const { props, directives } = compilerDom.buildProps(
  725. node,
  726. context,
  727. void 0,
  728. true,
  729. isDynamicComponent
  730. );
  731. if (props || directives.length) {
  732. propsExp = buildSSRProps(props, directives, context);
  733. }
  734. }
  735. const wipEntries = [];
  736. wipMap.set(node, wipEntries);
  737. const buildSSRSlotFn = (props, _vForExp, children, loc) => {
  738. const param0 = props && compilerDom.stringifyExpression(props) || `_`;
  739. const fn = compilerDom.createFunctionExpression(
  740. [param0, `_push`, `_parent`, `_scopeId`],
  741. void 0,
  742. // no return, assign body later
  743. true,
  744. // newline
  745. true,
  746. // isSlot
  747. loc
  748. );
  749. wipEntries.push({
  750. type: WIP_SLOT,
  751. fn,
  752. children,
  753. // also collect the corresponding vnode branch built earlier
  754. vnodeBranch: vnodeBranches[wipEntries.length]
  755. });
  756. return fn;
  757. };
  758. const slots = node.children.length ? compilerDom.buildSlots(node, context, buildSSRSlotFn).slots : `null`;
  759. if (typeof component !== "string") {
  760. node.ssrCodegenNode = compilerDom.createCallExpression(
  761. context.helper(SSR_RENDER_VNODE),
  762. [
  763. `_push`,
  764. compilerDom.createCallExpression(context.helper(compilerDom.CREATE_VNODE), [
  765. component,
  766. propsExp,
  767. slots
  768. ]),
  769. `_parent`
  770. ]
  771. );
  772. } else {
  773. node.ssrCodegenNode = compilerDom.createCallExpression(
  774. context.helper(SSR_RENDER_COMPONENT),
  775. [component, propsExp, slots, `_parent`]
  776. );
  777. }
  778. };
  779. };
  780. function ssrProcessComponent(node, context, parent) {
  781. const component = componentTypeMap.get(node);
  782. if (!node.ssrCodegenNode) {
  783. if (component === compilerDom.TELEPORT) {
  784. return ssrProcessTeleport(node, context);
  785. } else if (component === compilerDom.SUSPENSE) {
  786. return ssrProcessSuspense(node, context);
  787. } else if (component === compilerDom.TRANSITION_GROUP) {
  788. return ssrProcessTransitionGroup(node, context);
  789. } else {
  790. if (parent.type === WIP_SLOT) {
  791. context.pushStringPart(``);
  792. }
  793. if (component === compilerDom.TRANSITION) {
  794. return ssrProcessTransition(node, context);
  795. }
  796. processChildren(node, context);
  797. }
  798. } else {
  799. const wipEntries = wipMap.get(node) || [];
  800. for (let i = 0; i < wipEntries.length; i++) {
  801. const { fn, vnodeBranch } = wipEntries[i];
  802. fn.body = compilerDom.createIfStatement(
  803. compilerDom.createSimpleExpression(`_push`, false),
  804. processChildrenAsStatement(
  805. wipEntries[i],
  806. context,
  807. false,
  808. true
  809. ),
  810. vnodeBranch
  811. );
  812. }
  813. if (context.withSlotScopeId) {
  814. node.ssrCodegenNode.arguments.push(`_scopeId`);
  815. }
  816. if (typeof component === "string") {
  817. context.pushStatement(
  818. compilerDom.createCallExpression(`_push`, [node.ssrCodegenNode])
  819. );
  820. } else {
  821. context.pushStatement(node.ssrCodegenNode);
  822. }
  823. }
  824. }
  825. const rawOptionsMap = /* @__PURE__ */ new WeakMap();
  826. const [baseNodeTransforms, baseDirectiveTransforms] = compilerDom.getBaseTransformPreset(true);
  827. const vnodeNodeTransforms = [...baseNodeTransforms, ...compilerDom.DOMNodeTransforms];
  828. const vnodeDirectiveTransforms = {
  829. ...baseDirectiveTransforms,
  830. ...compilerDom.DOMDirectiveTransforms
  831. };
  832. function createVNodeSlotBranch(slotProps, vFor, children, parentContext) {
  833. const rawOptions = rawOptionsMap.get(parentContext.root);
  834. const subOptions = {
  835. ...rawOptions,
  836. // overwrite with vnode-based transforms
  837. nodeTransforms: [
  838. ...vnodeNodeTransforms,
  839. ...rawOptions.nodeTransforms || []
  840. ],
  841. directiveTransforms: {
  842. ...vnodeDirectiveTransforms,
  843. ...rawOptions.directiveTransforms || {}
  844. }
  845. };
  846. const wrapperProps = [];
  847. if (slotProps) {
  848. wrapperProps.push({
  849. type: 7,
  850. name: "slot",
  851. exp: slotProps,
  852. arg: void 0,
  853. modifiers: [],
  854. loc: compilerDom.locStub
  855. });
  856. }
  857. if (vFor) {
  858. wrapperProps.push(shared.extend({}, vFor));
  859. }
  860. const wrapperNode = {
  861. type: 1,
  862. ns: 0,
  863. tag: "template",
  864. tagType: 3,
  865. props: wrapperProps,
  866. children,
  867. loc: compilerDom.locStub,
  868. codegenNode: void 0
  869. };
  870. subTransform(wrapperNode, subOptions, parentContext);
  871. return compilerDom.createReturnStatement(children);
  872. }
  873. function subTransform(node, options, parentContext) {
  874. const childRoot = compilerDom.createRoot([node]);
  875. const childContext = compilerDom.createTransformContext(childRoot, options);
  876. childContext.ssr = false;
  877. childContext.scopes = { ...parentContext.scopes };
  878. childContext.identifiers = { ...parentContext.identifiers };
  879. childContext.imports = parentContext.imports;
  880. compilerDom.traverseNode(childRoot, childContext);
  881. ["helpers", "components", "directives"].forEach((key) => {
  882. childContext[key].forEach((value, helperKey) => {
  883. if (key === "helpers") {
  884. const parentCount = parentContext.helpers.get(helperKey);
  885. if (parentCount === void 0) {
  886. parentContext.helpers.set(helperKey, value);
  887. } else {
  888. parentContext.helpers.set(helperKey, value + parentCount);
  889. }
  890. } else {
  891. parentContext[key].add(value);
  892. }
  893. });
  894. });
  895. }
  896. function clone(v) {
  897. if (shared.isArray(v)) {
  898. return v.map(clone);
  899. } else if (shared.isPlainObject(v)) {
  900. const res = {};
  901. for (const key in v) {
  902. res[key] = clone(v[key]);
  903. }
  904. return res;
  905. } else {
  906. return v;
  907. }
  908. }
  909. function ssrCodegenTransform(ast, options) {
  910. const context = createSSRTransformContext(ast, options);
  911. if (options.ssrCssVars) {
  912. const cssContext = compilerDom.createTransformContext(compilerDom.createRoot([]), options);
  913. const varsExp = compilerDom.processExpression(
  914. compilerDom.createSimpleExpression(options.ssrCssVars, false),
  915. cssContext
  916. );
  917. context.body.push(
  918. compilerDom.createCompoundExpression([`const _cssVars = { style: `, varsExp, `}`])
  919. );
  920. Array.from(cssContext.helpers.keys()).forEach((helper) => {
  921. ast.helpers.add(helper);
  922. });
  923. }
  924. const isFragment = ast.children.length > 1 && ast.children.some((c) => !compilerDom.isText(c));
  925. processChildren(ast, context, isFragment);
  926. ast.codegenNode = compilerDom.createBlockStatement(context.body);
  927. ast.ssrHelpers = Array.from(
  928. /* @__PURE__ */ new Set([
  929. ...Array.from(ast.helpers).filter((h) => h in ssrHelpers),
  930. ...context.helpers
  931. ])
  932. );
  933. ast.helpers = new Set(Array.from(ast.helpers).filter((h) => !(h in ssrHelpers)));
  934. }
  935. function createSSRTransformContext(root, options, helpers = /* @__PURE__ */ new Set(), withSlotScopeId = false) {
  936. const body = [];
  937. let currentString = null;
  938. return {
  939. root,
  940. options,
  941. body,
  942. helpers,
  943. withSlotScopeId,
  944. onError: options.onError || ((e) => {
  945. throw e;
  946. }),
  947. helper(name) {
  948. helpers.add(name);
  949. return name;
  950. },
  951. pushStringPart(part) {
  952. if (!currentString) {
  953. const currentCall = compilerDom.createCallExpression(`_push`);
  954. body.push(currentCall);
  955. currentString = compilerDom.createTemplateLiteral([]);
  956. currentCall.arguments.push(currentString);
  957. }
  958. const bufferedElements = currentString.elements;
  959. const lastItem = bufferedElements[bufferedElements.length - 1];
  960. if (shared.isString(part) && shared.isString(lastItem)) {
  961. bufferedElements[bufferedElements.length - 1] += part;
  962. } else {
  963. bufferedElements.push(part);
  964. }
  965. },
  966. pushStatement(statement) {
  967. currentString = null;
  968. body.push(statement);
  969. }
  970. };
  971. }
  972. function createChildContext(parent, withSlotScopeId = parent.withSlotScopeId) {
  973. return createSSRTransformContext(
  974. parent.root,
  975. parent.options,
  976. parent.helpers,
  977. withSlotScopeId
  978. );
  979. }
  980. function processChildren(parent, context, asFragment = false, disableNestedFragments = false, disableComment = false) {
  981. if (asFragment) {
  982. context.pushStringPart(`<!--[-->`);
  983. }
  984. const { children } = parent;
  985. for (let i = 0; i < children.length; i++) {
  986. const child = children[i];
  987. switch (child.type) {
  988. case 1:
  989. switch (child.tagType) {
  990. case 0:
  991. ssrProcessElement(child, context);
  992. break;
  993. case 1:
  994. ssrProcessComponent(child, context, parent);
  995. break;
  996. case 2:
  997. ssrProcessSlotOutlet(child, context);
  998. break;
  999. case 3:
  1000. break;
  1001. default:
  1002. context.onError(
  1003. createSSRCompilerError(
  1004. 67,
  1005. child.loc
  1006. )
  1007. );
  1008. const exhaustiveCheck2 = child;
  1009. return exhaustiveCheck2;
  1010. }
  1011. break;
  1012. case 2:
  1013. context.pushStringPart(shared.escapeHtml(child.content));
  1014. break;
  1015. case 3:
  1016. if (!disableComment) {
  1017. context.pushStringPart(`<!--${child.content}-->`);
  1018. }
  1019. break;
  1020. case 5:
  1021. context.pushStringPart(
  1022. compilerDom.createCallExpression(context.helper(SSR_INTERPOLATE), [
  1023. child.content
  1024. ])
  1025. );
  1026. break;
  1027. case 9:
  1028. ssrProcessIf(child, context, disableNestedFragments, disableComment);
  1029. break;
  1030. case 11:
  1031. ssrProcessFor(child, context, disableNestedFragments);
  1032. break;
  1033. case 10:
  1034. break;
  1035. case 12:
  1036. case 8:
  1037. break;
  1038. default:
  1039. context.onError(
  1040. createSSRCompilerError(
  1041. 67,
  1042. child.loc
  1043. )
  1044. );
  1045. const exhaustiveCheck = child;
  1046. return exhaustiveCheck;
  1047. }
  1048. }
  1049. if (asFragment) {
  1050. context.pushStringPart(`<!--]-->`);
  1051. }
  1052. }
  1053. function processChildrenAsStatement(parent, parentContext, asFragment = false, withSlotScopeId = parentContext.withSlotScopeId) {
  1054. const childContext = createChildContext(parentContext, withSlotScopeId);
  1055. processChildren(parent, childContext, asFragment);
  1056. return compilerDom.createBlockStatement(childContext.body);
  1057. }
  1058. const ssrTransformModel = (dir, node, context) => {
  1059. const model = dir.exp;
  1060. function checkDuplicatedValue() {
  1061. const value = compilerDom.findProp(node, "value");
  1062. if (value) {
  1063. context.onError(
  1064. compilerDom.createDOMCompilerError(
  1065. 60,
  1066. value.loc
  1067. )
  1068. );
  1069. }
  1070. }
  1071. function processOption(plainNode) {
  1072. if (plainNode.tag === "option") {
  1073. if (plainNode.props.findIndex((p) => p.name === "selected") === -1) {
  1074. const value = findValueBinding(plainNode);
  1075. plainNode.ssrCodegenNode.elements.push(
  1076. compilerDom.createConditionalExpression(
  1077. compilerDom.createCallExpression(context.helper(SSR_INCLUDE_BOOLEAN_ATTR), [
  1078. compilerDom.createConditionalExpression(
  1079. compilerDom.createCallExpression(`Array.isArray`, [model]),
  1080. compilerDom.createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
  1081. model,
  1082. value
  1083. ]),
  1084. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1085. model,
  1086. value
  1087. ])
  1088. )
  1089. ]),
  1090. compilerDom.createSimpleExpression(" selected", true),
  1091. compilerDom.createSimpleExpression("", true),
  1092. false
  1093. )
  1094. );
  1095. }
  1096. } else if (plainNode.tag === "optgroup") {
  1097. plainNode.children.forEach(
  1098. (option) => processOption(option)
  1099. );
  1100. }
  1101. }
  1102. if (node.tagType === 0) {
  1103. const res = { props: [] };
  1104. const defaultProps = [
  1105. // default value binding for text type inputs
  1106. compilerDom.createObjectProperty(`value`, model)
  1107. ];
  1108. if (node.tag === "input") {
  1109. const type = compilerDom.findProp(node, "type");
  1110. if (type) {
  1111. const value = findValueBinding(node);
  1112. if (type.type === 7) {
  1113. res.ssrTagParts = [
  1114. compilerDom.createCallExpression(context.helper(SSR_RENDER_DYNAMIC_MODEL), [
  1115. type.exp,
  1116. model,
  1117. value
  1118. ])
  1119. ];
  1120. } else if (type.value) {
  1121. switch (type.value.content) {
  1122. case "radio":
  1123. res.props = [
  1124. compilerDom.createObjectProperty(
  1125. `checked`,
  1126. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1127. model,
  1128. value
  1129. ])
  1130. )
  1131. ];
  1132. break;
  1133. case "checkbox":
  1134. const trueValueBinding = compilerDom.findProp(node, "true-value");
  1135. if (trueValueBinding) {
  1136. const trueValue = trueValueBinding.type === 6 ? JSON.stringify(trueValueBinding.value.content) : trueValueBinding.exp;
  1137. res.props = [
  1138. compilerDom.createObjectProperty(
  1139. `checked`,
  1140. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1141. model,
  1142. trueValue
  1143. ])
  1144. )
  1145. ];
  1146. } else {
  1147. res.props = [
  1148. compilerDom.createObjectProperty(
  1149. `checked`,
  1150. compilerDom.createConditionalExpression(
  1151. compilerDom.createCallExpression(`Array.isArray`, [model]),
  1152. compilerDom.createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
  1153. model,
  1154. value
  1155. ]),
  1156. model
  1157. )
  1158. )
  1159. ];
  1160. }
  1161. break;
  1162. case "file":
  1163. context.onError(
  1164. compilerDom.createDOMCompilerError(
  1165. 59,
  1166. dir.loc
  1167. )
  1168. );
  1169. break;
  1170. default:
  1171. checkDuplicatedValue();
  1172. res.props = defaultProps;
  1173. break;
  1174. }
  1175. }
  1176. } else if (compilerDom.hasDynamicKeyVBind(node)) ; else {
  1177. checkDuplicatedValue();
  1178. res.props = defaultProps;
  1179. }
  1180. } else if (node.tag === "textarea") {
  1181. checkDuplicatedValue();
  1182. node.children = [compilerDom.createInterpolation(model, model.loc)];
  1183. } else if (node.tag === "select") {
  1184. node.children.forEach((child) => {
  1185. if (child.type === 1) {
  1186. processOption(child);
  1187. }
  1188. });
  1189. } else {
  1190. context.onError(
  1191. compilerDom.createDOMCompilerError(
  1192. 57,
  1193. dir.loc
  1194. )
  1195. );
  1196. }
  1197. return res;
  1198. } else {
  1199. return compilerDom.transformModel(dir, node, context);
  1200. }
  1201. };
  1202. function findValueBinding(node) {
  1203. const valueBinding = compilerDom.findProp(node, "value");
  1204. return valueBinding ? valueBinding.type === 7 ? valueBinding.exp : compilerDom.createSimpleExpression(valueBinding.value.content, true) : compilerDom.createSimpleExpression(`null`, false);
  1205. }
  1206. const ssrTransformShow = (dir, node, context) => {
  1207. if (!dir.exp) {
  1208. context.onError(
  1209. compilerDom.createDOMCompilerError(61)
  1210. );
  1211. }
  1212. return {
  1213. props: [
  1214. compilerDom.createObjectProperty(
  1215. `style`,
  1216. compilerDom.createConditionalExpression(
  1217. dir.exp,
  1218. compilerDom.createSimpleExpression(`null`, false),
  1219. compilerDom.createObjectExpression([
  1220. compilerDom.createObjectProperty(
  1221. `display`,
  1222. compilerDom.createSimpleExpression(`none`, true)
  1223. )
  1224. ]),
  1225. false
  1226. )
  1227. )
  1228. ]
  1229. };
  1230. };
  1231. const filterChild = (node) => node.children.filter((n) => n.type !== 3);
  1232. const hasSingleChild = (node) => filterChild(node).length === 1;
  1233. const ssrInjectFallthroughAttrs = (node, context) => {
  1234. if (node.type === 0) {
  1235. context.identifiers._attrs = 1;
  1236. }
  1237. if (node.type === 1 && node.tagType === 1 && (node.tag === "transition" || node.tag === "Transition" || node.tag === "KeepAlive" || node.tag === "keep-alive")) {
  1238. const rootChildren = filterChild(context.root);
  1239. if (rootChildren.length === 1 && rootChildren[0] === node) {
  1240. if (hasSingleChild(node)) {
  1241. injectFallthroughAttrs(node.children[0]);
  1242. }
  1243. return;
  1244. }
  1245. }
  1246. const parent = context.parent;
  1247. if (!parent || parent.type !== 0) {
  1248. return;
  1249. }
  1250. if (node.type === 10 && hasSingleChild(node)) {
  1251. let hasEncounteredIf = false;
  1252. for (const c of filterChild(parent)) {
  1253. if (c.type === 9 || c.type === 1 && compilerDom.findDir(c, "if")) {
  1254. if (hasEncounteredIf) return;
  1255. hasEncounteredIf = true;
  1256. } else if (
  1257. // node before v-if
  1258. !hasEncounteredIf || // non else nodes
  1259. !(c.type === 1 && compilerDom.findDir(c, /else/, true))
  1260. ) {
  1261. return;
  1262. }
  1263. }
  1264. injectFallthroughAttrs(node.children[0]);
  1265. } else if (hasSingleChild(parent)) {
  1266. injectFallthroughAttrs(node);
  1267. }
  1268. };
  1269. function injectFallthroughAttrs(node) {
  1270. if (node.type === 1 && (node.tagType === 0 || node.tagType === 1) && !compilerDom.findDir(node, "for")) {
  1271. node.props.push({
  1272. type: 7,
  1273. name: "bind",
  1274. arg: void 0,
  1275. exp: compilerDom.createSimpleExpression(`_attrs`, false),
  1276. modifiers: [],
  1277. loc: compilerDom.locStub
  1278. });
  1279. }
  1280. }
  1281. const ssrInjectCssVars = (node, context) => {
  1282. if (!context.ssrCssVars) {
  1283. return;
  1284. }
  1285. if (node.type === 0) {
  1286. context.identifiers._cssVars = 1;
  1287. }
  1288. const parent = context.parent;
  1289. if (!parent || parent.type !== 0) {
  1290. return;
  1291. }
  1292. if (node.type === 10) {
  1293. for (const child of node.children) {
  1294. injectCssVars(child);
  1295. }
  1296. } else {
  1297. injectCssVars(node);
  1298. }
  1299. };
  1300. function injectCssVars(node) {
  1301. if (node.type === 1 && (node.tagType === 0 || node.tagType === 1) && !compilerDom.findDir(node, "for")) {
  1302. if (node.tag === "suspense" || node.tag === "Suspense") {
  1303. for (const child of node.children) {
  1304. if (child.type === 1 && child.tagType === 3) {
  1305. child.children.forEach(injectCssVars);
  1306. } else {
  1307. injectCssVars(child);
  1308. }
  1309. }
  1310. } else {
  1311. node.props.push({
  1312. type: 7,
  1313. name: "bind",
  1314. arg: void 0,
  1315. exp: compilerDom.createSimpleExpression(`_cssVars`, false),
  1316. modifiers: [],
  1317. loc: compilerDom.locStub
  1318. });
  1319. }
  1320. }
  1321. }
  1322. function compile(source, options = {}) {
  1323. options = {
  1324. ...options,
  1325. ...compilerDom.parserOptions,
  1326. ssr: true,
  1327. inSSR: true,
  1328. scopeId: options.mode === "function" ? null : options.scopeId,
  1329. // always prefix since compiler-ssr doesn't have size concern
  1330. prefixIdentifiers: true,
  1331. // disable optimizations that are unnecessary for ssr
  1332. cacheHandlers: false,
  1333. hoistStatic: false
  1334. };
  1335. const ast = typeof source === "string" ? compilerDom.baseParse(source, options) : source;
  1336. rawOptionsMap.set(ast, options);
  1337. compilerDom.transform(ast, {
  1338. ...options,
  1339. hoistStatic: false,
  1340. nodeTransforms: [
  1341. ssrTransformIf,
  1342. ssrTransformFor,
  1343. compilerDom.trackVForSlotScopes,
  1344. compilerDom.transformExpression,
  1345. ssrTransformSlotOutlet,
  1346. ssrInjectFallthroughAttrs,
  1347. ssrInjectCssVars,
  1348. ssrTransformElement,
  1349. ssrTransformComponent,
  1350. compilerDom.trackSlotScopes,
  1351. compilerDom.transformStyle,
  1352. ...options.nodeTransforms || []
  1353. // user transforms
  1354. ],
  1355. directiveTransforms: {
  1356. // reusing core v-bind
  1357. bind: compilerDom.transformBind,
  1358. on: compilerDom.transformOn,
  1359. // model and show have dedicated SSR handling
  1360. model: ssrTransformModel,
  1361. show: ssrTransformShow,
  1362. // the following are ignored during SSR
  1363. // on: noopDirectiveTransform,
  1364. cloak: compilerDom.noopDirectiveTransform,
  1365. once: compilerDom.noopDirectiveTransform,
  1366. memo: compilerDom.noopDirectiveTransform,
  1367. ...options.directiveTransforms || {}
  1368. // user transforms
  1369. }
  1370. });
  1371. ssrCodegenTransform(ast, options);
  1372. return compilerDom.generate(ast, options);
  1373. }
  1374. exports.compile = compile;