版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chunk-TASLRYRR.js 7.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/utils.ts
  2. function getChildrenLocation(node) {
  3. if (node.children.length > 0) {
  4. const lastChild = node.children[node.children.length - 1];
  5. return [node.children[0].loc.start.offset, lastChild.loc.end.offset];
  6. } else {
  7. return void 0;
  8. }
  9. }
  10. function parseVueRequest(id) {
  11. const [filename, rawQuery] = id.split(`?`, 2);
  12. const query = Object.fromEntries(new URLSearchParams(rawQuery));
  13. if (query.vue != null) {
  14. query.vue = true;
  15. }
  16. if (query.index != null) {
  17. query.index = Number(query.index);
  18. }
  19. if (query.raw != null) {
  20. query.raw = true;
  21. }
  22. if (query.url != null) {
  23. query.url = true;
  24. }
  25. if (query.scoped != null) {
  26. query.scoped = true;
  27. }
  28. return {
  29. filename,
  30. query
  31. };
  32. }
  33. // src/core/constants.ts
  34. var QUERY_NAMED_TEMPLATE = "?vue&type=named-template";
  35. var QUERY_TEMPLATE = "type=template&namedTemplate";
  36. var QUERY_TEMPLATE_MAIN = `${QUERY_TEMPLATE}&mainTemplate`;
  37. var MAIN_TEMPLATE = Symbol();
  38. // src/core/index.ts
  39. var _common = require('@vue-macros/common');
  40. var _compilerdom = require('@vue/compiler-dom');
  41. function transformTemplateIs(s) {
  42. return (node) => {
  43. if (!(node.type === 1 && node.tag === "template"))
  44. return;
  45. const propIs = node.props.find(
  46. (prop) => prop.type === 6 && prop.name === "is"
  47. );
  48. if (!(propIs == null ? void 0 : propIs.value))
  49. return;
  50. const refName = propIs.value.content;
  51. s.overwrite(
  52. node.loc.start.offset,
  53. node.loc.end.offset,
  54. `<component is="named-template-${refName}" />`
  55. );
  56. };
  57. }
  58. function preTransform(code, id, templateContent) {
  59. const root = _compilerdom.parse.call(void 0, code);
  60. const templates = root.children.filter(
  61. (node) => node.type === 1 && node.tag === "template"
  62. );
  63. if (templates.length <= 1)
  64. return;
  65. const s = new (0, _common.MagicString)(code);
  66. for (const node of templates) {
  67. const propName = node.props.find(
  68. (prop) => prop.type === 6 && prop.name === "name"
  69. );
  70. if (!propName) {
  71. preTransformMainTemplate({ s, root, node, id, templateContent });
  72. continue;
  73. } else if (!propName.value) {
  74. continue;
  75. }
  76. const name = propName.value.content;
  77. let template = "";
  78. const templateLoc = getChildrenLocation(node);
  79. if (templateLoc) {
  80. template = s.slice(...templateLoc);
  81. }
  82. if (!templateContent[id])
  83. templateContent[id] = {};
  84. templateContent[id][name] = template;
  85. s.appendLeft(node.loc.start.offset, `<named-template name="${name}">`);
  86. s.appendLeft(node.loc.end.offset, "</named-template>");
  87. }
  88. return _common.getTransformResult.call(void 0, s, id);
  89. }
  90. function preTransformMainTemplate({
  91. s,
  92. root,
  93. node,
  94. id,
  95. templateContent
  96. }) {
  97. const ctx = _compilerdom.createTransformContext.call(void 0, root, {
  98. filename: id,
  99. nodeTransforms: [transformTemplateIs(s)]
  100. });
  101. _compilerdom.traverseNode.call(void 0, node, ctx);
  102. const loc = getChildrenLocation(node);
  103. if (!loc)
  104. return;
  105. if (!templateContent[id])
  106. templateContent[id] = {};
  107. templateContent[id][MAIN_TEMPLATE] = s.slice(...loc);
  108. s.remove(...loc);
  109. const offset = node.loc.start.offset + 1 + node.tag.length;
  110. s.appendLeft(offset, ` src="${`${id}?vue&${QUERY_TEMPLATE_MAIN}`}"`);
  111. }
  112. function postTransform(code, id, customBlocks) {
  113. var _a, _b, _c;
  114. const lang = _common.getLang.call(void 0, id);
  115. const program = _common.babelParse.call(void 0, code, lang);
  116. const { filename } = parseVueRequest(id);
  117. if (!id.includes(QUERY_TEMPLATE_MAIN)) {
  118. postTransformMainEntry(program, filename, customBlocks);
  119. return;
  120. }
  121. const s = new (0, _common.MagicString)(code);
  122. const subTemplates = [];
  123. for (const node of program.body) {
  124. if (node.type === "ExportNamedDeclaration" && ((_a = node.declaration) == null ? void 0 : _a.type) === "FunctionDeclaration" && ((_b = node.declaration.id) == null ? void 0 : _b.name) === "render") {
  125. const params = node.declaration.params;
  126. if (params.length > 0) {
  127. const lastParams = params[node.declaration.params.length - 1];
  128. const loc = [params[0].start, lastParams.end];
  129. const paramsText = s.slice(...loc);
  130. s.overwrite(...loc, "...args");
  131. s.appendLeft(
  132. node.declaration.body.start + 1,
  133. `
  134. let [${paramsText}] = args`
  135. );
  136. }
  137. }
  138. }
  139. _common.walkAST.call(void 0, program, {
  140. enter(node) {
  141. if (_common.isCallOf.call(void 0, node, ["_createVNode", "_createBlock"]) && _common.isCallOf.call(void 0, node.arguments[0], "_resolveDynamicComponent") && node.arguments[0].arguments[0].type === "StringLiteral" && node.arguments[0].arguments[0].value.startsWith("named-template-")) {
  142. subTemplates.push({
  143. vnode: node,
  144. component: node.arguments[0],
  145. name: node.arguments[0].arguments[0].value.replace(
  146. "named-template-",
  147. ""
  148. ),
  149. fnName: node.callee.name
  150. });
  151. }
  152. }
  153. });
  154. if (subTemplates.length === 0)
  155. return;
  156. for (const { vnode, component, name, fnName } of subTemplates) {
  157. const block = (_c = customBlocks[filename]) == null ? void 0 : _c[name];
  158. if (!block)
  159. throw new SyntaxError(`Unknown named template: ${name}`);
  160. const render = `${_common.HELPER_PREFIX}block_${escapeTemplateName(
  161. name
  162. )}.render(...args)`;
  163. if (fnName === "_createVNode") {
  164. s.overwriteNode(vnode, render);
  165. } else if (fnName === "_createBlock") {
  166. _common.importHelperFn.call(void 0, s, 0, "Fragment", "vue");
  167. s.overwriteNode(component, `${_common.HELPER_PREFIX}Fragment`);
  168. const text = `${vnode.arguments[1] ? "" : ", null"}, [${render}]`;
  169. s.appendLeft((vnode.arguments[1] || vnode.arguments[0]).end, text);
  170. }
  171. }
  172. for (const [name, source] of Object.entries(customBlocks[filename])) {
  173. s.prepend(
  174. `import { default as ${_common.HELPER_PREFIX}block_${escapeTemplateName(
  175. name
  176. )} } from ${JSON.stringify(source)}
  177. `
  178. );
  179. }
  180. return _common.getTransformResult.call(void 0, s, id);
  181. }
  182. function postTransformMainEntry(program, id, customBlocks) {
  183. for (const node of program.body) {
  184. if (node.type === "ImportDeclaration" && node.source.value.includes(QUERY_NAMED_TEMPLATE)) {
  185. const { name } = parseVueRequest(node.source.value).query;
  186. if (!customBlocks[id])
  187. customBlocks[id] = {};
  188. customBlocks[id][name] = node.source.value;
  189. }
  190. }
  191. }
  192. function escapeTemplateName(name) {
  193. return name.replace(/-/g, "$DASH");
  194. }
  195. exports.getChildrenLocation = getChildrenLocation; exports.parseVueRequest = parseVueRequest; exports.QUERY_NAMED_TEMPLATE = QUERY_NAMED_TEMPLATE; exports.QUERY_TEMPLATE = QUERY_TEMPLATE; exports.QUERY_TEMPLATE_MAIN = QUERY_TEMPLATE_MAIN; exports.MAIN_TEMPLATE = MAIN_TEMPLATE; exports.transformTemplateIs = transformTemplateIs; exports.preTransform = preTransform; exports.preTransformMainTemplate = preTransformMainTemplate; exports.postTransform = postTransform; exports.postTransformMainEntry = postTransformMainEntry;