compiler-ssr.cjs.js 43 KB

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