版博士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-4PQK4PHQ.js 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/core/constants.ts
  2. var SETUP_COMPONENT_ID_SUFFIX = "-setup-component-";
  3. var SETUP_COMPONENT_ID_REGEX = /-setup-component-(\d+).vue$/;
  4. var SETUP_COMPONENT_SUB_MODULE = /-setup-component-(\d+).vue.*/;
  5. var SETUP_COMPONENT_TYPE = "SetupFC";
  6. // src/core/sub-module.ts
  7. function isSubModule(id) {
  8. return SETUP_COMPONENT_SUB_MODULE.test(id);
  9. }
  10. function getMainModule(subModule, root) {
  11. return root + subModule.replace(SETUP_COMPONENT_SUB_MODULE, "");
  12. }
  13. // src/core/index.ts
  14. var _common = require('@vue-macros/common');
  15. function scanSetupComponent(code, id) {
  16. let program;
  17. try {
  18. program = _common.babelParse.call(void 0, code, _common.getLang.call(void 0, id));
  19. } catch (e) {
  20. return void 0;
  21. }
  22. const components = [];
  23. const imports = [];
  24. let scope = _common.attachScopes.call(void 0, program, "scope");
  25. _common.walkAST.call(void 0, program, {
  26. enter(node) {
  27. var _a;
  28. if (node.scope)
  29. scope = node.scope;
  30. const scopes = getScopeDecls(scope);
  31. if (_common.isCallOf.call(void 0, node, _common.DEFINE_SETUP_COMPONENT)) {
  32. components.push({
  33. fn: node,
  34. decl: node.arguments[0],
  35. scopes
  36. });
  37. } else if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && ((_a = node.id.typeAnnotation) == null ? void 0 : _a.type) === "TSTypeAnnotation" && node.id.typeAnnotation.typeAnnotation.type === "TSTypeReference" && node.id.typeAnnotation.typeAnnotation.typeName.type === "Identifier" && node.id.typeAnnotation.typeAnnotation.typeName.name === SETUP_COMPONENT_TYPE && node.init) {
  38. components.push({
  39. decl: node.init,
  40. scopes
  41. });
  42. } else if (node.type === "ImportDeclaration") {
  43. imports.push(code.slice(node.start, node.end));
  44. }
  45. },
  46. leave(node) {
  47. if (node.scope)
  48. scope = scope.parent;
  49. }
  50. });
  51. const ctxComponents = components.map(
  52. ({ decl, fn, scopes }) => {
  53. if (!["FunctionExpression", "ArrowFunctionExpression"].includes(decl.type))
  54. throw new SyntaxError(
  55. `${_common.DEFINE_SETUP_COMPONENT}: invalid setup component definition`
  56. );
  57. const body = decl == null ? void 0 : decl.body;
  58. let bodyStart = body.start;
  59. let bodyEnd = body.end;
  60. if (body.type === "BlockStatement") {
  61. bodyStart++;
  62. bodyEnd--;
  63. }
  64. return {
  65. code: code.slice(decl.start, decl.end),
  66. body: code.slice(bodyStart, bodyEnd),
  67. node: fn || decl,
  68. scopes
  69. };
  70. }
  71. );
  72. return {
  73. components: ctxComponents,
  74. imports
  75. };
  76. }
  77. function transformSetupComponent(code, _id, ctx) {
  78. const id = _common.normalizePath.call(void 0, _id);
  79. const s = new (0, _common.MagicString)(code);
  80. const fileContext = scanSetupComponent(code, id);
  81. if (!fileContext)
  82. return;
  83. const { components } = fileContext;
  84. ctx[id] = fileContext;
  85. for (const [i, { node, scopes }] of components.entries()) {
  86. const importName = `${_common.HELPER_PREFIX}setupComponent_${i}`;
  87. s.overwrite(
  88. node.start,
  89. node.end,
  90. `${importName}(() => ({ ${scopes.join(", ")} }))`
  91. );
  92. s.prepend(
  93. `import ${importName} from '${id}${SETUP_COMPONENT_ID_SUFFIX}${i}.vue'
  94. `
  95. );
  96. }
  97. return _common.getTransformResult.call(void 0, s, id);
  98. }
  99. function loadSetupComponent(virtualId, ctx, root) {
  100. var _a;
  101. const index = +(_nullishCoalesce(((_a = SETUP_COMPONENT_ID_REGEX.exec(virtualId)) == null ? void 0 : _a[1]), () => ( -1)));
  102. const id = virtualId.replace(SETUP_COMPONENT_ID_REGEX, "");
  103. const { components, imports } = ctx[id] || ctx[root + id] || {};
  104. const component = components[index];
  105. if (!component)
  106. return;
  107. const { body, scopes } = component;
  108. const lang = _common.getLang.call(void 0, id);
  109. const s = new (0, _common.MagicString)(body);
  110. const program = _common.babelParse.call(void 0, body, lang, {
  111. allowReturnOutsideFunction: true,
  112. allowImportExportEverywhere: true
  113. });
  114. for (const stmt of program.body) {
  115. if (stmt.type !== "ReturnStatement" || !stmt.argument)
  116. continue;
  117. s.overwriteNode(stmt, `defineRender(${s.sliceNode(stmt.argument)});`);
  118. }
  119. const rootVars = Object.keys(
  120. _common.attachScopes.call(void 0, program, "scope").declarations
  121. );
  122. s.prepend(
  123. `const { ${scopes.filter((name) => !rootVars.includes(name)).join(", ")} } = ${_common.HELPER_PREFIX}ctx();
  124. `
  125. );
  126. for (const i of imports)
  127. s.prepend(`${i}
  128. `);
  129. s.prepend(`<script setup${lang ? ` lang="${lang}"` : ""}>
  130. `);
  131. s.append(`</script>`);
  132. return s.toString();
  133. }
  134. async function hotUpdateSetupComponent({ file, modules, read }, ctx) {
  135. const getSubModule = (module2) => {
  136. const importedModules = Array.from(module2.importedModules);
  137. if (importedModules.length === 0)
  138. return [];
  139. return importedModules.filter(({ id }) => id && isSubModule(id)).flatMap((module3) => [module3, ...getSubModule(module3)]);
  140. };
  141. const module = modules.find((mod) => mod.file === file);
  142. if (!(module == null ? void 0 : module.id))
  143. return;
  144. const affectedModules = getSubModule(module);
  145. const normalizedId = _common.normalizePath.call(void 0, file);
  146. const nodeContexts = scanSetupComponent(await read(), normalizedId);
  147. if (nodeContexts)
  148. ctx[normalizedId] = nodeContexts;
  149. return [...modules, ...affectedModules];
  150. }
  151. function transformPost(code, _id) {
  152. const s = new (0, _common.MagicString)(code);
  153. const id = _common.normalizePath.call(void 0, _id);
  154. if (id.endsWith(".vue")) {
  155. return transformMainEntry();
  156. } else if (id.includes("type=script")) {
  157. return transformScript();
  158. }
  159. function transformMainEntry() {
  160. const program = _common.babelParse.call(void 0, code, "js");
  161. _common.walkAST.call(void 0, program, {
  162. enter(node, parent) {
  163. var _a;
  164. if (node.type === "ExportDefaultDeclaration" && node.declaration) {
  165. const exportDefault = node.declaration;
  166. s.prependLeft(
  167. _nullishCoalesce(((_a = exportDefault.leadingComments) == null ? void 0 : _a[0].start), () => ( exportDefault.start)),
  168. "(ctx) => "
  169. );
  170. } else if (node.type === "Identifier" && node.name === "_sfc_main" && (parent.type === "CallExpression" && parent.callee.type === "Identifier" && parent.callee.name === "_export_sfc" && node.name === "_sfc_main" || parent.type === "ExportDefaultDeclaration")) {
  171. s.appendLeft(node.end, "(ctx)");
  172. }
  173. }
  174. });
  175. return _common.getTransformResult.call(void 0, s, id);
  176. }
  177. function transformScript() {
  178. const program = _common.babelParse.call(void 0, code, _common.getLang.call(void 0, id));
  179. _common.walkAST.call(void 0, program, {
  180. enter(node) {
  181. var _a;
  182. if (node.type === "ExportDefaultDeclaration" && node.declaration) {
  183. const exportDefault = node.declaration;
  184. s.prependLeft(
  185. _nullishCoalesce(((_a = exportDefault.leadingComments) == null ? void 0 : _a[0].start), () => ( exportDefault.start)),
  186. `(${_common.HELPER_PREFIX}ctx) => `
  187. );
  188. }
  189. }
  190. });
  191. return _common.getTransformResult.call(void 0, s, id);
  192. }
  193. }
  194. function getScopeDecls(scope) {
  195. const scopes = /* @__PURE__ */ new Set();
  196. do {
  197. if (!(scope == null ? void 0 : scope.declarations))
  198. continue;
  199. Object.keys(scope.declarations).forEach((name) => scopes.add(name));
  200. } while (scope = scope == null ? void 0 : scope.parent);
  201. return Array.from(scopes);
  202. }
  203. exports.SETUP_COMPONENT_ID_SUFFIX = SETUP_COMPONENT_ID_SUFFIX; exports.SETUP_COMPONENT_ID_REGEX = SETUP_COMPONENT_ID_REGEX; exports.SETUP_COMPONENT_SUB_MODULE = SETUP_COMPONENT_SUB_MODULE; exports.SETUP_COMPONENT_TYPE = SETUP_COMPONENT_TYPE; exports.isSubModule = isSubModule; exports.getMainModule = getMainModule; exports.scanSetupComponent = scanSetupComponent; exports.transformSetupComponent = transformSetupComponent; exports.loadSetupComponent = loadSetupComponent; exports.hotUpdateSetupComponent = hotUpdateSetupComponent; exports.transformPost = transformPost; exports.getScopeDecls = getScopeDecls;