compiler-ssr.cjs.js 45 KB

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