compiler-ssr.cjs.js 45 KB

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