compiler-ssr.cjs.js 46 KB

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