server-renderer.esm-bundler.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. import { createVNode, ssrContextKey, warn as warn$1, Fragment, Static, Comment, Text, mergeProps, ssrUtils, createApp, initDirectivesForSSR } from 'vue';
  2. import { makeMap, isOn, isSVGTag, propsToAttrMap, isBooleanAttr, includeBooleanAttr, isSSRSafeAttrName, escapeHtml, normalizeClass, isString, normalizeStyle, stringifyStyle, isArray, toDisplayString, isFunction, getGlobalThis, isObject, looseEqual, looseIndexOf, isPromise, NOOP, escapeHtmlComment, isVoidTag } from '@vue/shared';
  3. export { includeBooleanAttr as ssrIncludeBooleanAttr } from '@vue/shared';
  4. const shouldIgnoreProp = makeMap(
  5. `,key,ref,innerHTML,textContent,ref_key,ref_for`
  6. );
  7. function ssrRenderAttrs(props, tag) {
  8. let ret = "";
  9. for (const key in props) {
  10. if (shouldIgnoreProp(key) || isOn(key) || tag === "textarea" && key === "value") {
  11. continue;
  12. }
  13. const value = props[key];
  14. if (key === "class") {
  15. ret += ` class="${ssrRenderClass(value)}"`;
  16. } else if (key === "style") {
  17. ret += ` style="${ssrRenderStyle(value)}"`;
  18. } else {
  19. ret += ssrRenderDynamicAttr(key, value, tag);
  20. }
  21. }
  22. return ret;
  23. }
  24. function ssrRenderDynamicAttr(key, value, tag) {
  25. if (!isRenderableValue(value)) {
  26. return ``;
  27. }
  28. const attrKey = tag && (tag.indexOf("-") > 0 || isSVGTag(tag)) ? key : propsToAttrMap[key] || key.toLowerCase();
  29. if (isBooleanAttr(attrKey)) {
  30. return includeBooleanAttr(value) ? ` ${attrKey}` : ``;
  31. } else if (isSSRSafeAttrName(attrKey)) {
  32. return value === "" ? ` ${attrKey}` : ` ${attrKey}="${escapeHtml(value)}"`;
  33. } else {
  34. console.warn(
  35. `[@vue/server-renderer] Skipped rendering unsafe attribute name: ${attrKey}`
  36. );
  37. return ``;
  38. }
  39. }
  40. function ssrRenderAttr(key, value) {
  41. if (!isRenderableValue(value)) {
  42. return ``;
  43. }
  44. return ` ${key}="${escapeHtml(value)}"`;
  45. }
  46. function isRenderableValue(value) {
  47. if (value == null) {
  48. return false;
  49. }
  50. const type = typeof value;
  51. return type === "string" || type === "number" || type === "boolean";
  52. }
  53. function ssrRenderClass(raw) {
  54. return escapeHtml(normalizeClass(raw));
  55. }
  56. function ssrRenderStyle(raw) {
  57. if (!raw) {
  58. return "";
  59. }
  60. if (isString(raw)) {
  61. return escapeHtml(raw);
  62. }
  63. const styles = normalizeStyle(raw);
  64. return escapeHtml(stringifyStyle(styles));
  65. }
  66. function ssrRenderComponent(comp, props = null, children = null, parentComponent = null, slotScopeId) {
  67. return renderComponentVNode(
  68. createVNode(comp, props, children),
  69. parentComponent,
  70. slotScopeId
  71. );
  72. }
  73. function ssrRenderSlot(slots, slotName, slotProps, fallbackRenderFn, push, parentComponent, slotScopeId) {
  74. push(`<!--[-->`);
  75. ssrRenderSlotInner(
  76. slots,
  77. slotName,
  78. slotProps,
  79. fallbackRenderFn,
  80. push,
  81. parentComponent,
  82. slotScopeId
  83. );
  84. push(`<!--]-->`);
  85. }
  86. function ssrRenderSlotInner(slots, slotName, slotProps, fallbackRenderFn, push, parentComponent, slotScopeId, transition) {
  87. const slotFn = slots[slotName];
  88. if (slotFn) {
  89. const slotBuffer = [];
  90. const bufferedPush = (item) => {
  91. slotBuffer.push(item);
  92. };
  93. const ret = slotFn(
  94. slotProps,
  95. bufferedPush,
  96. parentComponent,
  97. slotScopeId ? " " + slotScopeId : ""
  98. );
  99. if (isArray(ret)) {
  100. renderVNodeChildren(push, ret, parentComponent, slotScopeId);
  101. } else {
  102. let isEmptySlot = true;
  103. if (transition) {
  104. isEmptySlot = false;
  105. } else {
  106. for (let i = 0; i < slotBuffer.length; i++) {
  107. if (!isComment(slotBuffer[i])) {
  108. isEmptySlot = false;
  109. break;
  110. }
  111. }
  112. }
  113. if (isEmptySlot) {
  114. if (fallbackRenderFn) {
  115. fallbackRenderFn();
  116. }
  117. } else {
  118. for (let i = 0; i < slotBuffer.length; i++) {
  119. push(slotBuffer[i]);
  120. }
  121. }
  122. }
  123. } else if (fallbackRenderFn) {
  124. fallbackRenderFn();
  125. }
  126. }
  127. const commentTestRE = /^<!--.*-->$/s;
  128. const commentRE = /<!--[^]*?-->/gm;
  129. function isComment(item) {
  130. if (typeof item !== "string" || !commentTestRE.test(item))
  131. return false;
  132. if (item.length <= 8)
  133. return true;
  134. return !item.replace(commentRE, "").trim();
  135. }
  136. function ssrRenderTeleport(parentPush, contentRenderFn, target, disabled, parentComponent) {
  137. parentPush("<!--teleport start-->");
  138. const context = parentComponent.appContext.provides[ssrContextKey];
  139. const teleportBuffers = context.__teleportBuffers || (context.__teleportBuffers = {});
  140. const targetBuffer = teleportBuffers[target] || (teleportBuffers[target] = []);
  141. const bufferIndex = targetBuffer.length;
  142. let teleportContent;
  143. if (disabled) {
  144. contentRenderFn(parentPush);
  145. teleportContent = `<!--teleport anchor-->`;
  146. } else {
  147. const { getBuffer, push } = createBuffer();
  148. contentRenderFn(push);
  149. push(`<!--teleport anchor-->`);
  150. teleportContent = getBuffer();
  151. }
  152. targetBuffer.splice(bufferIndex, 0, teleportContent);
  153. parentPush("<!--teleport end-->");
  154. }
  155. function ssrInterpolate(value) {
  156. return escapeHtml(toDisplayString(value));
  157. }
  158. Symbol(!!(process.env.NODE_ENV !== "production") ? "iterate" : "");
  159. Symbol(!!(process.env.NODE_ENV !== "production") ? "Map key iterate" : "");
  160. let shouldTrack = true;
  161. const trackStack = [];
  162. function pauseTracking() {
  163. trackStack.push(shouldTrack);
  164. shouldTrack = false;
  165. }
  166. function resetTracking() {
  167. const last = trackStack.pop();
  168. shouldTrack = last === void 0 ? true : last;
  169. }
  170. function toRaw(observed) {
  171. const raw = observed && observed["__v_raw"];
  172. return raw ? toRaw(raw) : observed;
  173. }
  174. function isRef(r) {
  175. return !!(r && r.__v_isRef === true);
  176. }
  177. const stack = [];
  178. function pushWarningContext(vnode) {
  179. stack.push(vnode);
  180. }
  181. function popWarningContext() {
  182. stack.pop();
  183. }
  184. function warn(msg, ...args) {
  185. if (!!!(process.env.NODE_ENV !== "production"))
  186. return;
  187. pauseTracking();
  188. const instance = stack.length ? stack[stack.length - 1].component : null;
  189. const appWarnHandler = instance && instance.appContext.config.warnHandler;
  190. const trace = getComponentTrace();
  191. if (appWarnHandler) {
  192. callWithErrorHandling(
  193. appWarnHandler,
  194. instance,
  195. 11,
  196. [
  197. msg + args.join(""),
  198. instance && instance.proxy,
  199. trace.map(
  200. ({ vnode }) => `at <${formatComponentName(instance, vnode.type)}>`
  201. ).join("\n"),
  202. trace
  203. ]
  204. );
  205. } else {
  206. const warnArgs = [`[Vue warn]: ${msg}`, ...args];
  207. if (trace.length && // avoid spamming console during tests
  208. true) {
  209. warnArgs.push(`
  210. `, ...formatTrace(trace));
  211. }
  212. console.warn(...warnArgs);
  213. }
  214. resetTracking();
  215. }
  216. function getComponentTrace() {
  217. let currentVNode = stack[stack.length - 1];
  218. if (!currentVNode) {
  219. return [];
  220. }
  221. const normalizedStack = [];
  222. while (currentVNode) {
  223. const last = normalizedStack[0];
  224. if (last && last.vnode === currentVNode) {
  225. last.recurseCount++;
  226. } else {
  227. normalizedStack.push({
  228. vnode: currentVNode,
  229. recurseCount: 0
  230. });
  231. }
  232. const parentInstance = currentVNode.component && currentVNode.component.parent;
  233. currentVNode = parentInstance && parentInstance.vnode;
  234. }
  235. return normalizedStack;
  236. }
  237. function formatTrace(trace) {
  238. const logs = [];
  239. trace.forEach((entry, i) => {
  240. logs.push(...i === 0 ? [] : [`
  241. `], ...formatTraceEntry(entry));
  242. });
  243. return logs;
  244. }
  245. function formatTraceEntry({ vnode, recurseCount }) {
  246. const postfix = recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``;
  247. const isRoot = vnode.component ? vnode.component.parent == null : false;
  248. const open = ` at <${formatComponentName(
  249. vnode.component,
  250. vnode.type,
  251. isRoot
  252. )}`;
  253. const close = `>` + postfix;
  254. return vnode.props ? [open, ...formatProps(vnode.props), close] : [open + close];
  255. }
  256. function formatProps(props) {
  257. const res = [];
  258. const keys = Object.keys(props);
  259. keys.slice(0, 3).forEach((key) => {
  260. res.push(...formatProp(key, props[key]));
  261. });
  262. if (keys.length > 3) {
  263. res.push(` ...`);
  264. }
  265. return res;
  266. }
  267. function formatProp(key, value, raw) {
  268. if (isString(value)) {
  269. value = JSON.stringify(value);
  270. return raw ? value : [`${key}=${value}`];
  271. } else if (typeof value === "number" || typeof value === "boolean" || value == null) {
  272. return raw ? value : [`${key}=${value}`];
  273. } else if (isRef(value)) {
  274. value = formatProp(key, toRaw(value.value), true);
  275. return raw ? value : [`${key}=Ref<`, value, `>`];
  276. } else if (isFunction(value)) {
  277. return [`${key}=fn${value.name ? `<${value.name}>` : ``}`];
  278. } else {
  279. value = toRaw(value);
  280. return raw ? value : [`${key}=`, value];
  281. }
  282. }
  283. const ErrorTypeStrings = {
  284. ["sp"]: "serverPrefetch hook",
  285. ["bc"]: "beforeCreate hook",
  286. ["c"]: "created hook",
  287. ["bm"]: "beforeMount hook",
  288. ["m"]: "mounted hook",
  289. ["bu"]: "beforeUpdate hook",
  290. ["u"]: "updated",
  291. ["bum"]: "beforeUnmount hook",
  292. ["um"]: "unmounted hook",
  293. ["a"]: "activated hook",
  294. ["da"]: "deactivated hook",
  295. ["ec"]: "errorCaptured hook",
  296. ["rtc"]: "renderTracked hook",
  297. ["rtg"]: "renderTriggered hook",
  298. [0]: "setup function",
  299. [1]: "render function",
  300. [2]: "watcher getter",
  301. [3]: "watcher callback",
  302. [4]: "watcher cleanup function",
  303. [5]: "native event handler",
  304. [6]: "component event handler",
  305. [7]: "vnode hook",
  306. [8]: "directive hook",
  307. [9]: "transition hook",
  308. [10]: "app errorHandler",
  309. [11]: "app warnHandler",
  310. [12]: "ref function",
  311. [13]: "async component loader",
  312. [14]: "scheduler flush. This is likely a Vue internals bug. Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/core"
  313. };
  314. function callWithErrorHandling(fn, instance, type, args) {
  315. let res;
  316. try {
  317. res = args ? fn(...args) : fn();
  318. } catch (err) {
  319. handleError(err, instance, type);
  320. }
  321. return res;
  322. }
  323. function handleError(err, instance, type, throwInDev = true) {
  324. const contextVNode = instance ? instance.vnode : null;
  325. if (instance) {
  326. let cur = instance.parent;
  327. const exposedInstance = instance.proxy;
  328. const errorInfo = !!(process.env.NODE_ENV !== "production") ? ErrorTypeStrings[type] : type;
  329. while (cur) {
  330. const errorCapturedHooks = cur.ec;
  331. if (errorCapturedHooks) {
  332. for (let i = 0; i < errorCapturedHooks.length; i++) {
  333. if (errorCapturedHooks[i](err, exposedInstance, errorInfo) === false) {
  334. return;
  335. }
  336. }
  337. }
  338. cur = cur.parent;
  339. }
  340. const appErrorHandler = instance.appContext.config.errorHandler;
  341. if (appErrorHandler) {
  342. callWithErrorHandling(
  343. appErrorHandler,
  344. null,
  345. 10,
  346. [err, exposedInstance, errorInfo]
  347. );
  348. return;
  349. }
  350. }
  351. logError(err, type, contextVNode, throwInDev);
  352. }
  353. function logError(err, type, contextVNode, throwInDev = true) {
  354. if (!!(process.env.NODE_ENV !== "production")) {
  355. const info = ErrorTypeStrings[type];
  356. if (contextVNode) {
  357. pushWarningContext(contextVNode);
  358. }
  359. warn(`Unhandled error${info ? ` during execution of ${info}` : ``}`);
  360. if (contextVNode) {
  361. popWarningContext();
  362. }
  363. if (throwInDev) {
  364. throw err;
  365. } else {
  366. console.error(err);
  367. }
  368. } else {
  369. console.error(err);
  370. }
  371. }
  372. let globalCurrentInstanceSetters;
  373. let settersKey = "__VUE_INSTANCE_SETTERS__";
  374. {
  375. if (!(globalCurrentInstanceSetters = getGlobalThis()[settersKey])) {
  376. globalCurrentInstanceSetters = getGlobalThis()[settersKey] = [];
  377. }
  378. globalCurrentInstanceSetters.push((i) => i);
  379. }
  380. const classifyRE = /(?:^|[-_])(\w)/g;
  381. const classify = (str) => str.replace(classifyRE, (c) => c.toUpperCase()).replace(/[-_]/g, "");
  382. function getComponentName(Component, includeInferred = true) {
  383. return isFunction(Component) ? Component.displayName || Component.name : Component.name || includeInferred && Component.__name;
  384. }
  385. function formatComponentName(instance, Component, isRoot = false) {
  386. let name = getComponentName(Component);
  387. if (!name && Component.__file) {
  388. const match = Component.__file.match(/([^/\\]+)\.\w+$/);
  389. if (match) {
  390. name = match[1];
  391. }
  392. }
  393. if (!name && instance && instance.parent) {
  394. const inferFromRegistry = (registry) => {
  395. for (const key in registry) {
  396. if (registry[key] === Component) {
  397. return key;
  398. }
  399. }
  400. };
  401. name = inferFromRegistry(
  402. instance.components || instance.parent.type.components
  403. ) || inferFromRegistry(instance.appContext.components);
  404. }
  405. return name ? classify(name) : isRoot ? `App` : `Anonymous`;
  406. }
  407. function ssrRenderList(source, renderItem) {
  408. if (isArray(source) || isString(source)) {
  409. for (let i = 0, l = source.length; i < l; i++) {
  410. renderItem(source[i], i);
  411. }
  412. } else if (typeof source === "number") {
  413. if (!!(process.env.NODE_ENV !== "production") && !Number.isInteger(source)) {
  414. warn(`The v-for range expect an integer value but got ${source}.`);
  415. return;
  416. }
  417. for (let i = 0; i < source; i++) {
  418. renderItem(i + 1, i);
  419. }
  420. } else if (isObject(source)) {
  421. if (source[Symbol.iterator]) {
  422. const arr = Array.from(source);
  423. for (let i = 0, l = arr.length; i < l; i++) {
  424. renderItem(arr[i], i);
  425. }
  426. } else {
  427. const keys = Object.keys(source);
  428. for (let i = 0, l = keys.length; i < l; i++) {
  429. const key = keys[i];
  430. renderItem(source[key], key, i);
  431. }
  432. }
  433. }
  434. }
  435. async function ssrRenderSuspense(push, { default: renderContent }) {
  436. if (renderContent) {
  437. renderContent();
  438. } else {
  439. push(`<!---->`);
  440. }
  441. }
  442. function ssrGetDirectiveProps(instance, dir, value, arg, modifiers = {}) {
  443. if (typeof dir !== "function" && dir.getSSRProps) {
  444. return dir.getSSRProps(
  445. {
  446. dir,
  447. instance,
  448. value,
  449. oldValue: void 0,
  450. arg,
  451. modifiers
  452. },
  453. null
  454. ) || {};
  455. }
  456. return {};
  457. }
  458. const ssrLooseEqual = looseEqual;
  459. function ssrLooseContain(arr, value) {
  460. return looseIndexOf(arr, value) > -1;
  461. }
  462. function ssrRenderDynamicModel(type, model, value) {
  463. switch (type) {
  464. case "radio":
  465. return looseEqual(model, value) ? " checked" : "";
  466. case "checkbox":
  467. return (isArray(model) ? ssrLooseContain(model, value) : model) ? " checked" : "";
  468. default:
  469. return ssrRenderAttr("value", model);
  470. }
  471. }
  472. function ssrGetDynamicModelProps(existingProps = {}, model) {
  473. const { type, value } = existingProps;
  474. switch (type) {
  475. case "radio":
  476. return looseEqual(model, value) ? { checked: true } : null;
  477. case "checkbox":
  478. return (isArray(model) ? ssrLooseContain(model, value) : model) ? { checked: true } : null;
  479. default:
  480. return { value: model };
  481. }
  482. }
  483. function ssrCompile(template, instance) {
  484. {
  485. throw new Error(
  486. `On-the-fly template compilation is not supported in the ESM build of @vue/server-renderer. All templates must be pre-compiled into render functions.`
  487. );
  488. }
  489. }
  490. const {
  491. createComponentInstance,
  492. setCurrentRenderingInstance,
  493. setupComponent,
  494. renderComponentRoot,
  495. normalizeVNode
  496. } = ssrUtils;
  497. function createBuffer() {
  498. let appendable = false;
  499. const buffer = [];
  500. return {
  501. getBuffer() {
  502. return buffer;
  503. },
  504. push(item) {
  505. const isStringItem = isString(item);
  506. if (appendable && isStringItem) {
  507. buffer[buffer.length - 1] += item;
  508. } else {
  509. buffer.push(item);
  510. }
  511. appendable = isStringItem;
  512. if (isPromise(item) || isArray(item) && item.hasAsync) {
  513. buffer.hasAsync = true;
  514. }
  515. }
  516. };
  517. }
  518. function renderComponentVNode(vnode, parentComponent = null, slotScopeId) {
  519. const instance = createComponentInstance(vnode, parentComponent, null);
  520. const res = setupComponent(
  521. instance,
  522. true
  523. /* isSSR */
  524. );
  525. const hasAsyncSetup = isPromise(res);
  526. const prefetches = instance.sp;
  527. if (hasAsyncSetup || prefetches) {
  528. let p = hasAsyncSetup ? res : Promise.resolve();
  529. if (prefetches) {
  530. p = p.then(
  531. () => Promise.all(prefetches.map((prefetch) => prefetch.call(instance.proxy)))
  532. ).catch(() => {
  533. });
  534. }
  535. return p.then(() => renderComponentSubTree(instance, slotScopeId));
  536. } else {
  537. return renderComponentSubTree(instance, slotScopeId);
  538. }
  539. }
  540. function renderComponentSubTree(instance, slotScopeId) {
  541. const comp = instance.type;
  542. const { getBuffer, push } = createBuffer();
  543. if (isFunction(comp)) {
  544. let root = renderComponentRoot(instance);
  545. if (!comp.props) {
  546. for (const key in instance.attrs) {
  547. if (key.startsWith(`data-v-`)) {
  548. (root.props || (root.props = {}))[key] = ``;
  549. }
  550. }
  551. }
  552. renderVNode(push, instance.subTree = root, instance, slotScopeId);
  553. } else {
  554. if ((!instance.render || instance.render === NOOP) && !instance.ssrRender && !comp.ssrRender && isString(comp.template)) {
  555. comp.ssrRender = ssrCompile(comp.template);
  556. }
  557. for (const e of instance.scope.effects) {
  558. if (e.computed)
  559. e.computed._cacheable = true;
  560. }
  561. const ssrRender = instance.ssrRender || comp.ssrRender;
  562. if (ssrRender) {
  563. let attrs = instance.inheritAttrs !== false ? instance.attrs : void 0;
  564. let hasCloned = false;
  565. let cur = instance;
  566. while (true) {
  567. const scopeId = cur.vnode.scopeId;
  568. if (scopeId) {
  569. if (!hasCloned) {
  570. attrs = { ...attrs };
  571. hasCloned = true;
  572. }
  573. attrs[scopeId] = "";
  574. }
  575. const parent = cur.parent;
  576. if (parent && parent.subTree && parent.subTree === cur.vnode) {
  577. cur = parent;
  578. } else {
  579. break;
  580. }
  581. }
  582. if (slotScopeId) {
  583. if (!hasCloned)
  584. attrs = { ...attrs };
  585. attrs[slotScopeId.trim()] = "";
  586. }
  587. const prev = setCurrentRenderingInstance(instance);
  588. try {
  589. ssrRender(
  590. instance.proxy,
  591. push,
  592. instance,
  593. attrs,
  594. // compiler-optimized bindings
  595. instance.props,
  596. instance.setupState,
  597. instance.data,
  598. instance.ctx
  599. );
  600. } finally {
  601. setCurrentRenderingInstance(prev);
  602. }
  603. } else if (instance.render && instance.render !== NOOP) {
  604. renderVNode(
  605. push,
  606. instance.subTree = renderComponentRoot(instance),
  607. instance,
  608. slotScopeId
  609. );
  610. } else {
  611. const componentName = comp.name || comp.__file || `<Anonymous>`;
  612. warn$1(`Component ${componentName} is missing template or render function.`);
  613. push(`<!---->`);
  614. }
  615. }
  616. return getBuffer();
  617. }
  618. function renderVNode(push, vnode, parentComponent, slotScopeId) {
  619. const { type, shapeFlag, children } = vnode;
  620. switch (type) {
  621. case Text:
  622. push(escapeHtml(children));
  623. break;
  624. case Comment:
  625. push(
  626. children ? `<!--${escapeHtmlComment(children)}-->` : `<!---->`
  627. );
  628. break;
  629. case Static:
  630. push(children);
  631. break;
  632. case Fragment:
  633. if (vnode.slotScopeIds) {
  634. slotScopeId = (slotScopeId ? slotScopeId + " " : "") + vnode.slotScopeIds.join(" ");
  635. }
  636. push(`<!--[-->`);
  637. renderVNodeChildren(
  638. push,
  639. children,
  640. parentComponent,
  641. slotScopeId
  642. );
  643. push(`<!--]-->`);
  644. break;
  645. default:
  646. if (shapeFlag & 1) {
  647. renderElementVNode(push, vnode, parentComponent, slotScopeId);
  648. } else if (shapeFlag & 6) {
  649. push(renderComponentVNode(vnode, parentComponent, slotScopeId));
  650. } else if (shapeFlag & 64) {
  651. renderTeleportVNode(push, vnode, parentComponent, slotScopeId);
  652. } else if (shapeFlag & 128) {
  653. renderVNode(push, vnode.ssContent, parentComponent, slotScopeId);
  654. } else {
  655. warn$1(
  656. "[@vue/server-renderer] Invalid VNode type:",
  657. type,
  658. `(${typeof type})`
  659. );
  660. }
  661. }
  662. }
  663. function renderVNodeChildren(push, children, parentComponent, slotScopeId) {
  664. for (let i = 0; i < children.length; i++) {
  665. renderVNode(push, normalizeVNode(children[i]), parentComponent, slotScopeId);
  666. }
  667. }
  668. function renderElementVNode(push, vnode, parentComponent, slotScopeId) {
  669. const tag = vnode.type;
  670. let { props, children, shapeFlag, scopeId, dirs } = vnode;
  671. let openTag = `<${tag}`;
  672. if (dirs) {
  673. props = applySSRDirectives(vnode, props, dirs);
  674. }
  675. if (props) {
  676. openTag += ssrRenderAttrs(props, tag);
  677. }
  678. if (scopeId) {
  679. openTag += ` ${scopeId}`;
  680. }
  681. let curParent = parentComponent;
  682. let curVnode = vnode;
  683. while (curParent && curVnode === curParent.subTree) {
  684. curVnode = curParent.vnode;
  685. if (curVnode.scopeId) {
  686. openTag += ` ${curVnode.scopeId}`;
  687. }
  688. curParent = curParent.parent;
  689. }
  690. if (slotScopeId) {
  691. openTag += ` ${slotScopeId}`;
  692. }
  693. push(openTag + `>`);
  694. if (!isVoidTag(tag)) {
  695. let hasChildrenOverride = false;
  696. if (props) {
  697. if (props.innerHTML) {
  698. hasChildrenOverride = true;
  699. push(props.innerHTML);
  700. } else if (props.textContent) {
  701. hasChildrenOverride = true;
  702. push(escapeHtml(props.textContent));
  703. } else if (tag === "textarea" && props.value) {
  704. hasChildrenOverride = true;
  705. push(escapeHtml(props.value));
  706. }
  707. }
  708. if (!hasChildrenOverride) {
  709. if (shapeFlag & 8) {
  710. push(escapeHtml(children));
  711. } else if (shapeFlag & 16) {
  712. renderVNodeChildren(
  713. push,
  714. children,
  715. parentComponent,
  716. slotScopeId
  717. );
  718. }
  719. }
  720. push(`</${tag}>`);
  721. }
  722. }
  723. function applySSRDirectives(vnode, rawProps, dirs) {
  724. const toMerge = [];
  725. for (let i = 0; i < dirs.length; i++) {
  726. const binding = dirs[i];
  727. const {
  728. dir: { getSSRProps }
  729. } = binding;
  730. if (getSSRProps) {
  731. const props = getSSRProps(binding, vnode);
  732. if (props)
  733. toMerge.push(props);
  734. }
  735. }
  736. return mergeProps(rawProps || {}, ...toMerge);
  737. }
  738. function renderTeleportVNode(push, vnode, parentComponent, slotScopeId) {
  739. const target = vnode.props && vnode.props.to;
  740. const disabled = vnode.props && vnode.props.disabled;
  741. if (!target) {
  742. if (!disabled) {
  743. warn$1(`[@vue/server-renderer] Teleport is missing target prop.`);
  744. }
  745. return [];
  746. }
  747. if (!isString(target)) {
  748. warn$1(
  749. `[@vue/server-renderer] Teleport target must be a query selector string.`
  750. );
  751. return [];
  752. }
  753. ssrRenderTeleport(
  754. push,
  755. (push2) => {
  756. renderVNodeChildren(
  757. push2,
  758. vnode.children,
  759. parentComponent,
  760. slotScopeId
  761. );
  762. },
  763. target,
  764. disabled || disabled === "",
  765. parentComponent
  766. );
  767. }
  768. const { isVNode: isVNode$1 } = ssrUtils;
  769. async function unrollBuffer$1(buffer) {
  770. if (buffer.hasAsync) {
  771. let ret = "";
  772. for (let i = 0; i < buffer.length; i++) {
  773. let item = buffer[i];
  774. if (isPromise(item)) {
  775. item = await item;
  776. }
  777. if (isString(item)) {
  778. ret += item;
  779. } else {
  780. ret += await unrollBuffer$1(item);
  781. }
  782. }
  783. return ret;
  784. } else {
  785. return unrollBufferSync$1(buffer);
  786. }
  787. }
  788. function unrollBufferSync$1(buffer) {
  789. let ret = "";
  790. for (let i = 0; i < buffer.length; i++) {
  791. let item = buffer[i];
  792. if (isString(item)) {
  793. ret += item;
  794. } else {
  795. ret += unrollBufferSync$1(item);
  796. }
  797. }
  798. return ret;
  799. }
  800. async function renderToString(input, context = {}) {
  801. if (isVNode$1(input)) {
  802. return renderToString(createApp({ render: () => input }), context);
  803. }
  804. const vnode = createVNode(input._component, input._props);
  805. vnode.appContext = input._context;
  806. input.provide(ssrContextKey, context);
  807. const buffer = await renderComponentVNode(vnode);
  808. const result = await unrollBuffer$1(buffer);
  809. await resolveTeleports(context);
  810. if (context.__watcherHandles) {
  811. for (const unwatch of context.__watcherHandles) {
  812. unwatch();
  813. }
  814. }
  815. return result;
  816. }
  817. async function resolveTeleports(context) {
  818. if (context.__teleportBuffers) {
  819. context.teleports = context.teleports || {};
  820. for (const key in context.__teleportBuffers) {
  821. context.teleports[key] = await unrollBuffer$1(
  822. await Promise.all([context.__teleportBuffers[key]])
  823. );
  824. }
  825. }
  826. }
  827. const { isVNode } = ssrUtils;
  828. async function unrollBuffer(buffer, stream) {
  829. if (buffer.hasAsync) {
  830. for (let i = 0; i < buffer.length; i++) {
  831. let item = buffer[i];
  832. if (isPromise(item)) {
  833. item = await item;
  834. }
  835. if (isString(item)) {
  836. stream.push(item);
  837. } else {
  838. await unrollBuffer(item, stream);
  839. }
  840. }
  841. } else {
  842. unrollBufferSync(buffer, stream);
  843. }
  844. }
  845. function unrollBufferSync(buffer, stream) {
  846. for (let i = 0; i < buffer.length; i++) {
  847. let item = buffer[i];
  848. if (isString(item)) {
  849. stream.push(item);
  850. } else {
  851. unrollBufferSync(item, stream);
  852. }
  853. }
  854. }
  855. function renderToSimpleStream(input, context, stream) {
  856. if (isVNode(input)) {
  857. return renderToSimpleStream(
  858. createApp({ render: () => input }),
  859. context,
  860. stream
  861. );
  862. }
  863. const vnode = createVNode(input._component, input._props);
  864. vnode.appContext = input._context;
  865. input.provide(ssrContextKey, context);
  866. Promise.resolve(renderComponentVNode(vnode)).then((buffer) => unrollBuffer(buffer, stream)).then(() => resolveTeleports(context)).then(() => {
  867. if (context.__watcherHandles) {
  868. for (const unwatch of context.__watcherHandles) {
  869. unwatch();
  870. }
  871. }
  872. }).then(() => stream.push(null)).catch((error) => {
  873. stream.destroy(error);
  874. });
  875. return stream;
  876. }
  877. function renderToStream(input, context = {}) {
  878. console.warn(
  879. `[@vue/server-renderer] renderToStream is deprecated - use renderToNodeStream instead.`
  880. );
  881. return renderToNodeStream(input, context);
  882. }
  883. function renderToNodeStream(input, context = {}) {
  884. {
  885. throw new Error(
  886. `ESM build of renderToStream() does not support renderToNodeStream(). Use pipeToNodeWritable() with an existing Node.js Writable stream instance instead.`
  887. );
  888. }
  889. }
  890. function pipeToNodeWritable(input, context = {}, writable) {
  891. renderToSimpleStream(input, context, {
  892. push(content) {
  893. if (content != null) {
  894. writable.write(content);
  895. } else {
  896. writable.end();
  897. }
  898. },
  899. destroy(err) {
  900. writable.destroy(err);
  901. }
  902. });
  903. }
  904. function renderToWebStream(input, context = {}) {
  905. if (typeof ReadableStream !== "function") {
  906. throw new Error(
  907. `ReadableStream constructor is not available in the global scope. If the target environment does support web streams, consider using pipeToWebWritable() with an existing WritableStream instance instead.`
  908. );
  909. }
  910. const encoder = new TextEncoder();
  911. let cancelled = false;
  912. return new ReadableStream({
  913. start(controller) {
  914. renderToSimpleStream(input, context, {
  915. push(content) {
  916. if (cancelled)
  917. return;
  918. if (content != null) {
  919. controller.enqueue(encoder.encode(content));
  920. } else {
  921. controller.close();
  922. }
  923. },
  924. destroy(err) {
  925. controller.error(err);
  926. }
  927. });
  928. },
  929. cancel() {
  930. cancelled = true;
  931. }
  932. });
  933. }
  934. function pipeToWebWritable(input, context = {}, writable) {
  935. const writer = writable.getWriter();
  936. const encoder = new TextEncoder();
  937. let hasReady = false;
  938. try {
  939. hasReady = isPromise(writer.ready);
  940. } catch (e) {
  941. }
  942. renderToSimpleStream(input, context, {
  943. async push(content) {
  944. if (hasReady) {
  945. await writer.ready;
  946. }
  947. if (content != null) {
  948. return writer.write(encoder.encode(content));
  949. } else {
  950. return writer.close();
  951. }
  952. },
  953. destroy(err) {
  954. console.log(err);
  955. writer.close();
  956. }
  957. });
  958. }
  959. initDirectivesForSSR();
  960. export { pipeToNodeWritable, pipeToWebWritable, renderToNodeStream, renderToSimpleStream, renderToStream, renderToString, renderToWebStream, ssrGetDirectiveProps, ssrGetDynamicModelProps, ssrInterpolate, ssrLooseContain, ssrLooseEqual, ssrRenderAttr, ssrRenderAttrs, ssrRenderClass, ssrRenderComponent, ssrRenderDynamicAttr, ssrRenderDynamicModel, ssrRenderList, ssrRenderSlot, ssrRenderSlotInner, ssrRenderStyle, ssrRenderSuspense, ssrRenderTeleport, renderVNode as ssrRenderVNode };