compiler-ssr.cjs.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /**
  2. * @vue/compiler-ssr v3.5.18
  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 vText = compilerDom.findDir(node, "text");
  357. if (!vText) {
  358. const tempId = `_temp${context.temps++}`;
  359. propsExp.arguments = [
  360. compilerDom.createAssignmentExpression(
  361. compilerDom.createSimpleExpression(tempId, false),
  362. mergedProps
  363. )
  364. ];
  365. rawChildrenMap.set(
  366. node,
  367. compilerDom.createConditionalExpression(
  368. compilerDom.createSimpleExpression(`"textContent" in ${tempId}`, false),
  369. compilerDom.createCallExpression(context.helper(SSR_INTERPOLATE), [
  370. compilerDom.createSimpleExpression(`${tempId}.textContent`, false)
  371. ]),
  372. compilerDom.createSimpleExpression(`${tempId}.innerHTML ?? ''`, false),
  373. false
  374. )
  375. );
  376. }
  377. }
  378. if (needTagForRuntime) {
  379. propsExp.arguments.push(`"${node.tag}"`);
  380. }
  381. openTag.push(propsExp);
  382. }
  383. }
  384. let dynamicClassBinding = void 0;
  385. let staticClassBinding = void 0;
  386. let dynamicStyleBinding = void 0;
  387. for (let i = 0; i < node.props.length; i++) {
  388. const prop = node.props[i];
  389. if (node.tag === "input" && isTrueFalseValue(prop)) {
  390. continue;
  391. }
  392. if (prop.type === 7) {
  393. if (prop.name === "html" && prop.exp) {
  394. rawChildrenMap.set(
  395. node,
  396. compilerDom.createCompoundExpression([`(`, prop.exp, `) ?? ''`])
  397. );
  398. } else if (prop.name === "text" && prop.exp) {
  399. node.children = [compilerDom.createInterpolation(prop.exp, prop.loc)];
  400. } else if (prop.name === "slot") {
  401. context.onError(
  402. compilerDom.createCompilerError(40, prop.loc)
  403. );
  404. } else if (isTextareaWithValue(node, prop) && prop.exp) {
  405. if (!needMergeProps) {
  406. node.children = [compilerDom.createInterpolation(prop.exp, prop.loc)];
  407. }
  408. } else if (!needMergeProps && prop.name !== "on") {
  409. const directiveTransform = context.directiveTransforms[prop.name];
  410. if (directiveTransform) {
  411. const { props, ssrTagParts } = directiveTransform(
  412. prop,
  413. node,
  414. context
  415. );
  416. if (ssrTagParts) {
  417. openTag.push(...ssrTagParts);
  418. }
  419. for (let j = 0; j < props.length; j++) {
  420. const { key, value } = props[j];
  421. if (compilerDom.isStaticExp(key)) {
  422. let attrName = key.content;
  423. if (attrName === "key" || attrName === "ref") {
  424. continue;
  425. }
  426. if (attrName === "class") {
  427. openTag.push(
  428. ` class="`,
  429. dynamicClassBinding = compilerDom.createCallExpression(
  430. context.helper(SSR_RENDER_CLASS),
  431. [value]
  432. ),
  433. `"`
  434. );
  435. } else if (attrName === "style") {
  436. if (dynamicStyleBinding) {
  437. mergeCall(dynamicStyleBinding, value);
  438. } else {
  439. openTag.push(
  440. ` style="`,
  441. dynamicStyleBinding = compilerDom.createCallExpression(
  442. context.helper(SSR_RENDER_STYLE),
  443. [value]
  444. ),
  445. `"`
  446. );
  447. }
  448. } else {
  449. attrName = node.tag.indexOf("-") > 0 ? attrName : shared.propsToAttrMap[attrName] || attrName.toLowerCase();
  450. if (shared.isBooleanAttr(attrName)) {
  451. openTag.push(
  452. compilerDom.createConditionalExpression(
  453. compilerDom.createCallExpression(
  454. context.helper(SSR_INCLUDE_BOOLEAN_ATTR),
  455. [value]
  456. ),
  457. compilerDom.createSimpleExpression(" " + attrName, true),
  458. compilerDom.createSimpleExpression("", true),
  459. false
  460. )
  461. );
  462. } else if (shared.isSSRSafeAttrName(attrName)) {
  463. openTag.push(
  464. compilerDom.createCallExpression(context.helper(SSR_RENDER_ATTR), [
  465. key,
  466. value
  467. ])
  468. );
  469. } else {
  470. context.onError(
  471. createSSRCompilerError(
  472. 65,
  473. key.loc
  474. )
  475. );
  476. }
  477. }
  478. } else {
  479. const args = [key, value];
  480. if (needTagForRuntime) {
  481. args.push(`"${node.tag}"`);
  482. }
  483. openTag.push(
  484. compilerDom.createCallExpression(
  485. context.helper(SSR_RENDER_DYNAMIC_ATTR),
  486. args
  487. )
  488. );
  489. }
  490. }
  491. }
  492. }
  493. } else {
  494. const name = prop.name;
  495. if (node.tag === "textarea" && name === "value" && prop.value) {
  496. rawChildrenMap.set(node, shared.escapeHtml(prop.value.content));
  497. } else if (!needMergeProps) {
  498. if (name === "key" || name === "ref") {
  499. continue;
  500. }
  501. if (name === "class" && prop.value) {
  502. staticClassBinding = JSON.stringify(prop.value.content);
  503. }
  504. openTag.push(
  505. ` ${prop.name}` + (prop.value ? `="${shared.escapeHtml(prop.value.content)}"` : ``)
  506. );
  507. }
  508. }
  509. }
  510. if (dynamicClassBinding && staticClassBinding) {
  511. mergeCall(dynamicClassBinding, staticClassBinding);
  512. removeStaticBinding(openTag, "class");
  513. }
  514. if (context.scopeId) {
  515. openTag.push(` ${context.scopeId}`);
  516. }
  517. node.ssrCodegenNode = compilerDom.createTemplateLiteral(openTag);
  518. };
  519. };
  520. function buildSSRProps(props, directives, context) {
  521. let mergePropsArgs = [];
  522. if (props) {
  523. if (props.type === 14) {
  524. mergePropsArgs = props.arguments;
  525. } else {
  526. mergePropsArgs.push(props);
  527. }
  528. }
  529. if (directives.length) {
  530. for (const dir of directives) {
  531. mergePropsArgs.push(
  532. compilerDom.createCallExpression(context.helper(SSR_GET_DIRECTIVE_PROPS), [
  533. `_ctx`,
  534. ...compilerDom.buildDirectiveArgs(dir, context).elements
  535. ])
  536. );
  537. }
  538. }
  539. return mergePropsArgs.length > 1 ? compilerDom.createCallExpression(context.helper(compilerDom.MERGE_PROPS), mergePropsArgs) : mergePropsArgs[0];
  540. }
  541. function isTrueFalseValue(prop) {
  542. if (prop.type === 7) {
  543. return prop.name === "bind" && prop.arg && compilerDom.isStaticExp(prop.arg) && (prop.arg.content === "true-value" || prop.arg.content === "false-value");
  544. } else {
  545. return prop.name === "true-value" || prop.name === "false-value";
  546. }
  547. }
  548. function isTextareaWithValue(node, prop) {
  549. return !!(node.tag === "textarea" && prop.name === "bind" && compilerDom.isStaticArgOf(prop.arg, "value"));
  550. }
  551. function mergeCall(call, arg) {
  552. const existing = call.arguments[0];
  553. if (existing.type === 17) {
  554. existing.elements.push(arg);
  555. } else {
  556. call.arguments[0] = compilerDom.createArrayExpression([existing, arg]);
  557. }
  558. }
  559. function removeStaticBinding(tag, binding) {
  560. const regExp = new RegExp(`^ ${binding}=".+"$`);
  561. const i = tag.findIndex((e) => typeof e === "string" && regExp.test(e));
  562. if (i > -1) {
  563. tag.splice(i, 1);
  564. }
  565. }
  566. function findVModel(node) {
  567. return node.props.find(
  568. (p) => p.type === 7 && p.name === "model" && p.exp
  569. );
  570. }
  571. function ssrProcessElement(node, context) {
  572. const isVoidTag = context.options.isVoidTag || shared.NO;
  573. const elementsToAdd = node.ssrCodegenNode.elements;
  574. for (let j = 0; j < elementsToAdd.length; j++) {
  575. context.pushStringPart(elementsToAdd[j]);
  576. }
  577. if (context.withSlotScopeId) {
  578. context.pushStringPart(compilerDom.createSimpleExpression(`_scopeId`, false));
  579. }
  580. context.pushStringPart(`>`);
  581. const rawChildren = rawChildrenMap.get(node);
  582. if (rawChildren) {
  583. context.pushStringPart(rawChildren);
  584. } else if (node.children.length) {
  585. processChildren(node, context);
  586. }
  587. if (!isVoidTag(node.tag)) {
  588. context.pushStringPart(`</${node.tag}>`);
  589. }
  590. }
  591. const wipMap$2 = /* @__PURE__ */ new WeakMap();
  592. function ssrTransformTransitionGroup(node, context) {
  593. return () => {
  594. const tag = compilerDom.findProp(node, "tag");
  595. if (tag) {
  596. const otherProps = node.props.filter((p) => p !== tag);
  597. const { props, directives } = compilerDom.buildProps(
  598. node,
  599. context,
  600. otherProps,
  601. true,
  602. false,
  603. true
  604. );
  605. let propsExp = null;
  606. if (props || directives.length) {
  607. propsExp = compilerDom.createCallExpression(context.helper(SSR_RENDER_ATTRS), [
  608. buildSSRProps(props, directives, context)
  609. ]);
  610. }
  611. wipMap$2.set(node, {
  612. tag,
  613. propsExp,
  614. scopeId: context.scopeId || null
  615. });
  616. }
  617. };
  618. }
  619. function ssrProcessTransitionGroup(node, context) {
  620. const entry = wipMap$2.get(node);
  621. if (entry) {
  622. const { tag, propsExp, scopeId } = entry;
  623. if (tag.type === 7) {
  624. context.pushStringPart(`<`);
  625. context.pushStringPart(tag.exp);
  626. if (propsExp) {
  627. context.pushStringPart(propsExp);
  628. }
  629. if (scopeId) {
  630. context.pushStringPart(` ${scopeId}`);
  631. }
  632. context.pushStringPart(`>`);
  633. processChildren(
  634. node,
  635. context,
  636. false,
  637. /**
  638. * TransitionGroup has the special runtime behavior of flattening and
  639. * concatenating all children into a single fragment (in order for them to
  640. * be patched using the same key map) so we need to account for that here
  641. * by disabling nested fragment wrappers from being generated.
  642. */
  643. true,
  644. /**
  645. * TransitionGroup filters out comment children at runtime and thus
  646. * doesn't expect comments to be present during hydration. We need to
  647. * account for that by disabling the empty comment that is otherwise
  648. * rendered for a falsy v-if that has no v-else specified. (#6715)
  649. */
  650. true
  651. );
  652. context.pushStringPart(`</`);
  653. context.pushStringPart(tag.exp);
  654. context.pushStringPart(`>`);
  655. } else {
  656. context.pushStringPart(`<${tag.value.content}`);
  657. if (propsExp) {
  658. context.pushStringPart(propsExp);
  659. }
  660. if (scopeId) {
  661. context.pushStringPart(` ${scopeId}`);
  662. }
  663. context.pushStringPart(`>`);
  664. processChildren(node, context, false, true, true);
  665. context.pushStringPart(`</${tag.value.content}>`);
  666. }
  667. } else {
  668. processChildren(node, context, true, true, true);
  669. }
  670. }
  671. const wipMap$1 = /* @__PURE__ */ new WeakMap();
  672. function ssrTransformTransition(node, context) {
  673. return () => {
  674. const appear = compilerDom.findProp(node, "appear", false, true);
  675. wipMap$1.set(node, !!appear);
  676. };
  677. }
  678. function ssrProcessTransition(node, context) {
  679. node.children = node.children.filter((c) => c.type !== 3);
  680. const appear = wipMap$1.get(node);
  681. if (appear) {
  682. context.pushStringPart(`<template>`);
  683. processChildren(node, context, false, true);
  684. context.pushStringPart(`</template>`);
  685. } else {
  686. processChildren(node, context, false, true);
  687. }
  688. }
  689. const wipMap = /* @__PURE__ */ new WeakMap();
  690. const WIP_SLOT = Symbol();
  691. const componentTypeMap = /* @__PURE__ */ new WeakMap();
  692. const ssrTransformComponent = (node, context) => {
  693. if (node.type !== 1 || node.tagType !== 1) {
  694. return;
  695. }
  696. const component = compilerDom.resolveComponentType(
  697. node,
  698. context,
  699. true
  700. /* ssr */
  701. );
  702. const isDynamicComponent = shared.isObject(component) && component.callee === compilerDom.RESOLVE_DYNAMIC_COMPONENT;
  703. componentTypeMap.set(node, component);
  704. if (shared.isSymbol(component)) {
  705. if (component === compilerDom.SUSPENSE) {
  706. return ssrTransformSuspense(node, context);
  707. } else if (component === compilerDom.TRANSITION_GROUP) {
  708. return ssrTransformTransitionGroup(node, context);
  709. } else if (component === compilerDom.TRANSITION) {
  710. return ssrTransformTransition(node);
  711. }
  712. return;
  713. }
  714. const vnodeBranches = [];
  715. const clonedNode = clone(node);
  716. return function ssrPostTransformComponent() {
  717. if (clonedNode.children.length) {
  718. compilerDom.buildSlots(clonedNode, context, (props, vFor, children) => {
  719. vnodeBranches.push(
  720. createVNodeSlotBranch(props, vFor, children, context)
  721. );
  722. return compilerDom.createFunctionExpression(void 0);
  723. });
  724. }
  725. let propsExp = `null`;
  726. if (node.props.length) {
  727. const { props, directives } = compilerDom.buildProps(
  728. node,
  729. context,
  730. void 0,
  731. true,
  732. isDynamicComponent
  733. );
  734. if (props || directives.length) {
  735. propsExp = buildSSRProps(props, directives, context);
  736. }
  737. }
  738. const wipEntries = [];
  739. wipMap.set(node, wipEntries);
  740. const buildSSRSlotFn = (props, _vForExp, children, loc) => {
  741. const param0 = props && compilerDom.stringifyExpression(props) || `_`;
  742. const fn = compilerDom.createFunctionExpression(
  743. [param0, `_push`, `_parent`, `_scopeId`],
  744. void 0,
  745. // no return, assign body later
  746. true,
  747. // newline
  748. true,
  749. // isSlot
  750. loc
  751. );
  752. wipEntries.push({
  753. type: WIP_SLOT,
  754. fn,
  755. children,
  756. // also collect the corresponding vnode branch built earlier
  757. vnodeBranch: vnodeBranches[wipEntries.length]
  758. });
  759. return fn;
  760. };
  761. const slots = node.children.length ? compilerDom.buildSlots(node, context, buildSSRSlotFn).slots : `null`;
  762. if (typeof component !== "string") {
  763. node.ssrCodegenNode = compilerDom.createCallExpression(
  764. context.helper(SSR_RENDER_VNODE),
  765. [
  766. `_push`,
  767. compilerDom.createCallExpression(context.helper(compilerDom.CREATE_VNODE), [
  768. component,
  769. propsExp,
  770. slots
  771. ]),
  772. `_parent`
  773. ]
  774. );
  775. } else {
  776. node.ssrCodegenNode = compilerDom.createCallExpression(
  777. context.helper(SSR_RENDER_COMPONENT),
  778. [component, propsExp, slots, `_parent`]
  779. );
  780. }
  781. };
  782. };
  783. function ssrProcessComponent(node, context, parent) {
  784. const component = componentTypeMap.get(node);
  785. if (!node.ssrCodegenNode) {
  786. if (component === compilerDom.TELEPORT) {
  787. return ssrProcessTeleport(node, context);
  788. } else if (component === compilerDom.SUSPENSE) {
  789. return ssrProcessSuspense(node, context);
  790. } else if (component === compilerDom.TRANSITION_GROUP) {
  791. return ssrProcessTransitionGroup(node, context);
  792. } else {
  793. if (parent.type === WIP_SLOT) {
  794. context.pushStringPart(``);
  795. }
  796. if (component === compilerDom.TRANSITION) {
  797. return ssrProcessTransition(node, context);
  798. }
  799. processChildren(node, context);
  800. }
  801. } else {
  802. const wipEntries = wipMap.get(node) || [];
  803. for (let i = 0; i < wipEntries.length; i++) {
  804. const { fn, vnodeBranch } = wipEntries[i];
  805. fn.body = compilerDom.createIfStatement(
  806. compilerDom.createSimpleExpression(`_push`, false),
  807. processChildrenAsStatement(
  808. wipEntries[i],
  809. context,
  810. false,
  811. true
  812. ),
  813. vnodeBranch
  814. );
  815. }
  816. if (context.withSlotScopeId) {
  817. node.ssrCodegenNode.arguments.push(`_scopeId`);
  818. }
  819. if (typeof component === "string") {
  820. context.pushStatement(
  821. compilerDom.createCallExpression(`_push`, [node.ssrCodegenNode])
  822. );
  823. } else {
  824. context.pushStatement(node.ssrCodegenNode);
  825. }
  826. }
  827. }
  828. const rawOptionsMap = /* @__PURE__ */ new WeakMap();
  829. const [baseNodeTransforms, baseDirectiveTransforms] = compilerDom.getBaseTransformPreset(true);
  830. const vnodeNodeTransforms = [...baseNodeTransforms, ...compilerDom.DOMNodeTransforms];
  831. const vnodeDirectiveTransforms = {
  832. ...baseDirectiveTransforms,
  833. ...compilerDom.DOMDirectiveTransforms
  834. };
  835. function createVNodeSlotBranch(slotProps, vFor, children, parentContext) {
  836. const rawOptions = rawOptionsMap.get(parentContext.root);
  837. const subOptions = {
  838. ...rawOptions,
  839. // overwrite with vnode-based transforms
  840. nodeTransforms: [
  841. ...vnodeNodeTransforms,
  842. ...rawOptions.nodeTransforms || []
  843. ],
  844. directiveTransforms: {
  845. ...vnodeDirectiveTransforms,
  846. ...rawOptions.directiveTransforms || {}
  847. }
  848. };
  849. const wrapperProps = [];
  850. if (slotProps) {
  851. wrapperProps.push({
  852. type: 7,
  853. name: "slot",
  854. exp: slotProps,
  855. arg: void 0,
  856. modifiers: [],
  857. loc: compilerDom.locStub
  858. });
  859. }
  860. if (vFor) {
  861. wrapperProps.push(shared.extend({}, vFor));
  862. }
  863. const wrapperNode = {
  864. type: 1,
  865. ns: 0,
  866. tag: "template",
  867. tagType: 3,
  868. props: wrapperProps,
  869. children,
  870. loc: compilerDom.locStub,
  871. codegenNode: void 0
  872. };
  873. subTransform(wrapperNode, subOptions, parentContext);
  874. return compilerDom.createReturnStatement(children);
  875. }
  876. function subTransform(node, options, parentContext) {
  877. const childRoot = compilerDom.createRoot([node]);
  878. const childContext = compilerDom.createTransformContext(childRoot, options);
  879. childContext.ssr = false;
  880. childContext.scopes = { ...parentContext.scopes };
  881. childContext.identifiers = { ...parentContext.identifiers };
  882. childContext.imports = parentContext.imports;
  883. compilerDom.traverseNode(childRoot, childContext);
  884. ["helpers", "components", "directives"].forEach((key) => {
  885. childContext[key].forEach((value, helperKey) => {
  886. if (key === "helpers") {
  887. const parentCount = parentContext.helpers.get(helperKey);
  888. if (parentCount === void 0) {
  889. parentContext.helpers.set(helperKey, value);
  890. } else {
  891. parentContext.helpers.set(helperKey, value + parentCount);
  892. }
  893. } else {
  894. parentContext[key].add(value);
  895. }
  896. });
  897. });
  898. }
  899. function clone(v) {
  900. if (shared.isArray(v)) {
  901. return v.map(clone);
  902. } else if (shared.isPlainObject(v)) {
  903. const res = {};
  904. for (const key in v) {
  905. res[key] = clone(v[key]);
  906. }
  907. return res;
  908. } else {
  909. return v;
  910. }
  911. }
  912. function ssrCodegenTransform(ast, options) {
  913. const context = createSSRTransformContext(ast, options);
  914. if (options.ssrCssVars) {
  915. const cssContext = compilerDom.createTransformContext(compilerDom.createRoot([]), options);
  916. const varsExp = compilerDom.processExpression(
  917. compilerDom.createSimpleExpression(options.ssrCssVars, false),
  918. cssContext
  919. );
  920. context.body.push(
  921. compilerDom.createCompoundExpression([`const _cssVars = { style: `, varsExp, `}`])
  922. );
  923. Array.from(cssContext.helpers.keys()).forEach((helper) => {
  924. ast.helpers.add(helper);
  925. });
  926. }
  927. const isFragment = ast.children.length > 1 && ast.children.some((c) => !compilerDom.isText(c));
  928. processChildren(ast, context, isFragment);
  929. ast.codegenNode = compilerDom.createBlockStatement(context.body);
  930. ast.ssrHelpers = Array.from(
  931. /* @__PURE__ */ new Set([
  932. ...Array.from(ast.helpers).filter((h) => h in ssrHelpers),
  933. ...context.helpers
  934. ])
  935. );
  936. ast.helpers = new Set(Array.from(ast.helpers).filter((h) => !(h in ssrHelpers)));
  937. }
  938. function createSSRTransformContext(root, options, helpers = /* @__PURE__ */ new Set(), withSlotScopeId = false) {
  939. const body = [];
  940. let currentString = null;
  941. return {
  942. root,
  943. options,
  944. body,
  945. helpers,
  946. withSlotScopeId,
  947. onError: options.onError || ((e) => {
  948. throw e;
  949. }),
  950. helper(name) {
  951. helpers.add(name);
  952. return name;
  953. },
  954. pushStringPart(part) {
  955. if (!currentString) {
  956. const currentCall = compilerDom.createCallExpression(`_push`);
  957. body.push(currentCall);
  958. currentString = compilerDom.createTemplateLiteral([]);
  959. currentCall.arguments.push(currentString);
  960. }
  961. const bufferedElements = currentString.elements;
  962. const lastItem = bufferedElements[bufferedElements.length - 1];
  963. if (shared.isString(part) && shared.isString(lastItem)) {
  964. bufferedElements[bufferedElements.length - 1] += part;
  965. } else {
  966. bufferedElements.push(part);
  967. }
  968. },
  969. pushStatement(statement) {
  970. currentString = null;
  971. body.push(statement);
  972. }
  973. };
  974. }
  975. function createChildContext(parent, withSlotScopeId = parent.withSlotScopeId) {
  976. return createSSRTransformContext(
  977. parent.root,
  978. parent.options,
  979. parent.helpers,
  980. withSlotScopeId
  981. );
  982. }
  983. function processChildren(parent, context, asFragment = false, disableNestedFragments = false, disableComment = false) {
  984. if (asFragment) {
  985. context.pushStringPart(`<!--[-->`);
  986. }
  987. const { children } = parent;
  988. for (let i = 0; i < children.length; i++) {
  989. const child = children[i];
  990. switch (child.type) {
  991. case 1:
  992. switch (child.tagType) {
  993. case 0:
  994. ssrProcessElement(child, context);
  995. break;
  996. case 1:
  997. ssrProcessComponent(child, context, parent);
  998. break;
  999. case 2:
  1000. ssrProcessSlotOutlet(child, context);
  1001. break;
  1002. case 3:
  1003. break;
  1004. default:
  1005. context.onError(
  1006. createSSRCompilerError(
  1007. 67,
  1008. child.loc
  1009. )
  1010. );
  1011. const exhaustiveCheck2 = child;
  1012. return exhaustiveCheck2;
  1013. }
  1014. break;
  1015. case 2:
  1016. context.pushStringPart(shared.escapeHtml(child.content));
  1017. break;
  1018. case 3:
  1019. if (!disableComment) {
  1020. context.pushStringPart(`<!--${child.content}-->`);
  1021. }
  1022. break;
  1023. case 5:
  1024. context.pushStringPart(
  1025. compilerDom.createCallExpression(context.helper(SSR_INTERPOLATE), [
  1026. child.content
  1027. ])
  1028. );
  1029. break;
  1030. case 9:
  1031. ssrProcessIf(child, context, disableNestedFragments, disableComment);
  1032. break;
  1033. case 11:
  1034. ssrProcessFor(child, context, disableNestedFragments);
  1035. break;
  1036. case 10:
  1037. break;
  1038. case 12:
  1039. case 8:
  1040. break;
  1041. default:
  1042. context.onError(
  1043. createSSRCompilerError(
  1044. 67,
  1045. child.loc
  1046. )
  1047. );
  1048. const exhaustiveCheck = child;
  1049. return exhaustiveCheck;
  1050. }
  1051. }
  1052. if (asFragment) {
  1053. context.pushStringPart(`<!--]-->`);
  1054. }
  1055. }
  1056. function processChildrenAsStatement(parent, parentContext, asFragment = false, withSlotScopeId = parentContext.withSlotScopeId) {
  1057. const childContext = createChildContext(parentContext, withSlotScopeId);
  1058. processChildren(parent, childContext, asFragment);
  1059. return compilerDom.createBlockStatement(childContext.body);
  1060. }
  1061. const ssrTransformModel = (dir, node, context) => {
  1062. const model = dir.exp;
  1063. function checkDuplicatedValue() {
  1064. const value = compilerDom.findProp(node, "value");
  1065. if (value) {
  1066. context.onError(
  1067. compilerDom.createDOMCompilerError(
  1068. 60,
  1069. value.loc
  1070. )
  1071. );
  1072. }
  1073. }
  1074. const processSelectChildren = (children) => {
  1075. children.forEach((child) => {
  1076. if (child.type === 1) {
  1077. processOption(child);
  1078. } else if (child.type === 11) {
  1079. processSelectChildren(child.children);
  1080. } else if (child.type === 9) {
  1081. child.branches.forEach((b) => processSelectChildren(b.children));
  1082. }
  1083. });
  1084. };
  1085. function processOption(plainNode) {
  1086. if (plainNode.tag === "option") {
  1087. if (plainNode.props.findIndex((p) => p.name === "selected") === -1) {
  1088. const value = findValueBinding(plainNode);
  1089. plainNode.ssrCodegenNode.elements.push(
  1090. compilerDom.createConditionalExpression(
  1091. compilerDom.createCallExpression(context.helper(SSR_INCLUDE_BOOLEAN_ATTR), [
  1092. compilerDom.createConditionalExpression(
  1093. compilerDom.createCallExpression(`Array.isArray`, [model]),
  1094. compilerDom.createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
  1095. model,
  1096. value
  1097. ]),
  1098. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1099. model,
  1100. value
  1101. ])
  1102. )
  1103. ]),
  1104. compilerDom.createSimpleExpression(" selected", true),
  1105. compilerDom.createSimpleExpression("", true),
  1106. false
  1107. )
  1108. );
  1109. }
  1110. } else if (plainNode.tag === "optgroup") {
  1111. processSelectChildren(plainNode.children);
  1112. }
  1113. }
  1114. if (node.tagType === 0) {
  1115. const res = { props: [] };
  1116. const defaultProps = [
  1117. // default value binding for text type inputs
  1118. compilerDom.createObjectProperty(`value`, model)
  1119. ];
  1120. if (node.tag === "input") {
  1121. const type = compilerDom.findProp(node, "type");
  1122. if (type) {
  1123. const value = findValueBinding(node);
  1124. if (type.type === 7) {
  1125. res.ssrTagParts = [
  1126. compilerDom.createCallExpression(context.helper(SSR_RENDER_DYNAMIC_MODEL), [
  1127. type.exp,
  1128. model,
  1129. value
  1130. ])
  1131. ];
  1132. } else if (type.value) {
  1133. switch (type.value.content) {
  1134. case "radio":
  1135. res.props = [
  1136. compilerDom.createObjectProperty(
  1137. `checked`,
  1138. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1139. model,
  1140. value
  1141. ])
  1142. )
  1143. ];
  1144. break;
  1145. case "checkbox":
  1146. const trueValueBinding = compilerDom.findProp(node, "true-value");
  1147. if (trueValueBinding) {
  1148. const trueValue = trueValueBinding.type === 6 ? JSON.stringify(trueValueBinding.value.content) : trueValueBinding.exp;
  1149. res.props = [
  1150. compilerDom.createObjectProperty(
  1151. `checked`,
  1152. compilerDom.createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
  1153. model,
  1154. trueValue
  1155. ])
  1156. )
  1157. ];
  1158. } else {
  1159. res.props = [
  1160. compilerDom.createObjectProperty(
  1161. `checked`,
  1162. compilerDom.createConditionalExpression(
  1163. compilerDom.createCallExpression(`Array.isArray`, [model]),
  1164. compilerDom.createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
  1165. model,
  1166. value
  1167. ]),
  1168. model
  1169. )
  1170. )
  1171. ];
  1172. }
  1173. break;
  1174. case "file":
  1175. context.onError(
  1176. compilerDom.createDOMCompilerError(
  1177. 59,
  1178. dir.loc
  1179. )
  1180. );
  1181. break;
  1182. default:
  1183. checkDuplicatedValue();
  1184. res.props = defaultProps;
  1185. break;
  1186. }
  1187. }
  1188. } else if (compilerDom.hasDynamicKeyVBind(node)) ; else {
  1189. checkDuplicatedValue();
  1190. res.props = defaultProps;
  1191. }
  1192. } else if (node.tag === "textarea") {
  1193. checkDuplicatedValue();
  1194. node.children = [compilerDom.createInterpolation(model, model.loc)];
  1195. } else if (node.tag === "select") {
  1196. processSelectChildren(node.children);
  1197. } else {
  1198. context.onError(
  1199. compilerDom.createDOMCompilerError(
  1200. 57,
  1201. dir.loc
  1202. )
  1203. );
  1204. }
  1205. return res;
  1206. } else {
  1207. return compilerDom.transformModel(dir, node, context);
  1208. }
  1209. };
  1210. function findValueBinding(node) {
  1211. const valueBinding = compilerDom.findProp(node, "value");
  1212. return valueBinding ? valueBinding.type === 7 ? valueBinding.exp : compilerDom.createSimpleExpression(valueBinding.value.content, true) : compilerDom.createSimpleExpression(`null`, false);
  1213. }
  1214. const ssrTransformShow = (dir, node, context) => {
  1215. if (!dir.exp) {
  1216. context.onError(
  1217. compilerDom.createDOMCompilerError(61)
  1218. );
  1219. }
  1220. return {
  1221. props: [
  1222. compilerDom.createObjectProperty(
  1223. `style`,
  1224. compilerDom.createConditionalExpression(
  1225. dir.exp,
  1226. compilerDom.createSimpleExpression(`null`, false),
  1227. compilerDom.createObjectExpression([
  1228. compilerDom.createObjectProperty(
  1229. `display`,
  1230. compilerDom.createSimpleExpression(`none`, true)
  1231. )
  1232. ]),
  1233. false
  1234. )
  1235. )
  1236. ]
  1237. };
  1238. };
  1239. const filterChild = (node) => node.children.filter((n) => n.type !== 3);
  1240. const hasSingleChild = (node) => filterChild(node).length === 1;
  1241. const ssrInjectFallthroughAttrs = (node, context) => {
  1242. if (node.type === 0) {
  1243. context.identifiers._attrs = 1;
  1244. }
  1245. if (node.type === 1 && node.tagType === 1 && (node.tag === "transition" || node.tag === "Transition" || node.tag === "KeepAlive" || node.tag === "keep-alive")) {
  1246. const rootChildren = filterChild(context.root);
  1247. if (rootChildren.length === 1 && rootChildren[0] === node) {
  1248. if (hasSingleChild(node)) {
  1249. injectFallthroughAttrs(node.children[0]);
  1250. }
  1251. return;
  1252. }
  1253. }
  1254. const parent = context.parent;
  1255. if (!parent || parent.type !== 0) {
  1256. return;
  1257. }
  1258. if (node.type === 10 && hasSingleChild(node)) {
  1259. let hasEncounteredIf = false;
  1260. for (const c of filterChild(parent)) {
  1261. if (c.type === 9 || c.type === 1 && compilerDom.findDir(c, "if")) {
  1262. if (hasEncounteredIf) return;
  1263. hasEncounteredIf = true;
  1264. } else if (
  1265. // node before v-if
  1266. !hasEncounteredIf || // non else nodes
  1267. !(c.type === 1 && compilerDom.findDir(c, /else/, true))
  1268. ) {
  1269. return;
  1270. }
  1271. }
  1272. injectFallthroughAttrs(node.children[0]);
  1273. } else if (hasSingleChild(parent)) {
  1274. injectFallthroughAttrs(node);
  1275. }
  1276. };
  1277. function injectFallthroughAttrs(node) {
  1278. if (node.type === 1 && (node.tagType === 0 || node.tagType === 1) && !compilerDom.findDir(node, "for")) {
  1279. node.props.push({
  1280. type: 7,
  1281. name: "bind",
  1282. arg: void 0,
  1283. exp: compilerDom.createSimpleExpression(`_attrs`, false),
  1284. modifiers: [],
  1285. loc: compilerDom.locStub
  1286. });
  1287. }
  1288. }
  1289. const ssrInjectCssVars = (node, context) => {
  1290. if (!context.ssrCssVars) {
  1291. return;
  1292. }
  1293. if (node.type === 0) {
  1294. context.identifiers._cssVars = 1;
  1295. }
  1296. const parent = context.parent;
  1297. if (!parent || parent.type !== 0) {
  1298. return;
  1299. }
  1300. if (node.type === 10) {
  1301. for (const child of node.children) {
  1302. injectCssVars(child);
  1303. }
  1304. } else {
  1305. injectCssVars(node);
  1306. }
  1307. };
  1308. function injectCssVars(node) {
  1309. if (node.type === 1 && (node.tagType === 0 || node.tagType === 1) && !compilerDom.findDir(node, "for")) {
  1310. if (node.tag === "suspense" || node.tag === "Suspense") {
  1311. for (const child of node.children) {
  1312. if (child.type === 1 && child.tagType === 3) {
  1313. child.children.forEach(injectCssVars);
  1314. } else {
  1315. injectCssVars(child);
  1316. }
  1317. }
  1318. } else {
  1319. node.props.push({
  1320. type: 7,
  1321. name: "bind",
  1322. arg: void 0,
  1323. exp: compilerDom.createSimpleExpression(`_cssVars`, false),
  1324. modifiers: [],
  1325. loc: compilerDom.locStub
  1326. });
  1327. }
  1328. }
  1329. }
  1330. function compile(source, options = {}) {
  1331. options = {
  1332. ...options,
  1333. ...compilerDom.parserOptions,
  1334. ssr: true,
  1335. inSSR: true,
  1336. scopeId: options.mode === "function" ? null : options.scopeId,
  1337. // always prefix since compiler-ssr doesn't have size concern
  1338. prefixIdentifiers: true,
  1339. // disable optimizations that are unnecessary for ssr
  1340. cacheHandlers: false,
  1341. hoistStatic: false
  1342. };
  1343. const ast = typeof source === "string" ? compilerDom.baseParse(source, options) : source;
  1344. rawOptionsMap.set(ast, options);
  1345. compilerDom.transform(ast, {
  1346. ...options,
  1347. hoistStatic: false,
  1348. nodeTransforms: [
  1349. ssrTransformIf,
  1350. ssrTransformFor,
  1351. compilerDom.trackVForSlotScopes,
  1352. compilerDom.transformExpression,
  1353. ssrTransformSlotOutlet,
  1354. ssrInjectFallthroughAttrs,
  1355. ssrInjectCssVars,
  1356. ssrTransformElement,
  1357. ssrTransformComponent,
  1358. compilerDom.trackSlotScopes,
  1359. compilerDom.transformStyle,
  1360. ...options.nodeTransforms || []
  1361. // user transforms
  1362. ],
  1363. directiveTransforms: {
  1364. // reusing core v-bind
  1365. bind: compilerDom.transformBind,
  1366. on: compilerDom.transformOn,
  1367. // model and show have dedicated SSR handling
  1368. model: ssrTransformModel,
  1369. show: ssrTransformShow,
  1370. // the following are ignored during SSR
  1371. // on: noopDirectiveTransform,
  1372. cloak: compilerDom.noopDirectiveTransform,
  1373. once: compilerDom.noopDirectiveTransform,
  1374. memo: compilerDom.noopDirectiveTransform,
  1375. ...options.directiveTransforms || {}
  1376. // user transforms
  1377. }
  1378. });
  1379. ssrCodegenTransform(ast, options);
  1380. return compilerDom.generate(ast, options);
  1381. }
  1382. exports.compile = compile;