c73818a3873f96487d5697149add0ee3fc2ca530da9cb7c4629299a365b5116f22a661b8350b73def38bda2dc5f3cade0be14113fcfeb5be59d4f05ec1ecea 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/index.ts
  20. var index_exports = {};
  21. __export(index_exports, {
  22. default: () => index_default
  23. });
  24. module.exports = __toCommonJS(index_exports);
  25. var import_parser = require("@babel/parser");
  26. var import_compiler_sfc = require("@vue/compiler-sfc");
  27. var import_code_frame = require("@babel/code-frame");
  28. var import_helper_module_imports = require("@babel/helper-module-imports");
  29. var import_helper_plugin_utils = require("@babel/helper-plugin-utils");
  30. var index_default = (0, import_helper_plugin_utils.declare)(({ types: t }, options) => {
  31. let ctx;
  32. let helpers;
  33. return {
  34. name: "babel-plugin-resolve-type",
  35. pre(file) {
  36. const filename = file.opts.filename || "unknown.js";
  37. helpers = /* @__PURE__ */ new Set();
  38. ctx = {
  39. filename,
  40. source: file.code,
  41. options,
  42. ast: file.ast.program.body,
  43. isCE: false,
  44. error(msg, node) {
  45. throw new Error(
  46. `[@vue/babel-plugin-resolve-type] ${msg}
  47. ${filename}
  48. ${(0, import_code_frame.codeFrameColumns)(
  49. file.code,
  50. {
  51. start: {
  52. line: node.loc.start.line,
  53. column: node.loc.start.column + 1
  54. },
  55. end: {
  56. line: node.loc.end.line,
  57. column: node.loc.end.column + 1
  58. }
  59. }
  60. )}`
  61. );
  62. },
  63. helper(key) {
  64. helpers.add(key);
  65. return `_${key}`;
  66. },
  67. getString(node) {
  68. return file.code.slice(node.start, node.end);
  69. },
  70. propsTypeDecl: void 0,
  71. propsRuntimeDefaults: void 0,
  72. propsDestructuredBindings: {},
  73. emitsTypeDecl: void 0
  74. };
  75. },
  76. visitor: {
  77. CallExpression(path) {
  78. if (!ctx) {
  79. throw new Error(
  80. "[@vue/babel-plugin-resolve-type] context is not loaded."
  81. );
  82. }
  83. const { node } = path;
  84. if (!t.isIdentifier(node.callee, { name: "defineComponent" })) return;
  85. if (!checkDefineComponent(path)) return;
  86. const comp = node.arguments[0];
  87. if (!comp || !t.isFunction(comp)) return;
  88. let options2 = node.arguments[1];
  89. if (!options2) {
  90. options2 = t.objectExpression([]);
  91. node.arguments.push(options2);
  92. }
  93. node.arguments[1] = processProps(comp, options2) || options2;
  94. node.arguments[1] = processEmits(comp, node.arguments[1]) || options2;
  95. },
  96. VariableDeclarator(path) {
  97. inferComponentName(path);
  98. }
  99. },
  100. post(file) {
  101. for (const helper of helpers) {
  102. (0, import_helper_module_imports.addNamed)(file.path, `_${helper}`, "vue");
  103. }
  104. }
  105. };
  106. function inferComponentName(path) {
  107. var _a;
  108. const id = path.get("id");
  109. const init = path.get("init");
  110. if (!id || !id.isIdentifier() || !init || !init.isCallExpression()) return;
  111. if (!((_a = init.get("callee")) == null ? void 0 : _a.isIdentifier({ name: "defineComponent" }))) return;
  112. if (!checkDefineComponent(init)) return;
  113. const nameProperty = t.objectProperty(
  114. t.identifier("name"),
  115. t.stringLiteral(id.node.name)
  116. );
  117. const { arguments: args } = init.node;
  118. if (args.length === 0) return;
  119. if (args.length === 1) {
  120. init.node.arguments.push(t.objectExpression([]));
  121. }
  122. args[1] = addProperty(t, args[1], nameProperty);
  123. }
  124. function processProps(comp, options2) {
  125. const props = comp.params[0];
  126. if (!props) return;
  127. if (props.type === "AssignmentPattern") {
  128. ctx.propsTypeDecl = getTypeAnnotation(props.left);
  129. ctx.propsRuntimeDefaults = props.right;
  130. } else {
  131. ctx.propsTypeDecl = getTypeAnnotation(props);
  132. }
  133. if (!ctx.propsTypeDecl) return;
  134. const runtimeProps = (0, import_compiler_sfc.extractRuntimeProps)(ctx);
  135. if (!runtimeProps) {
  136. return;
  137. }
  138. const ast = (0, import_parser.parseExpression)(runtimeProps);
  139. return addProperty(
  140. t,
  141. options2,
  142. t.objectProperty(t.identifier("props"), ast)
  143. );
  144. }
  145. function processEmits(comp, options2) {
  146. var _a;
  147. const setupCtx = comp.params[1] && getTypeAnnotation(comp.params[1]);
  148. if (!setupCtx || !t.isTSTypeReference(setupCtx) || !t.isIdentifier(setupCtx.typeName, { name: "SetupContext" }))
  149. return;
  150. const emitType = (_a = setupCtx.typeParameters) == null ? void 0 : _a.params[0];
  151. if (!emitType) return;
  152. ctx.emitsTypeDecl = emitType;
  153. const runtimeEmits = (0, import_compiler_sfc.extractRuntimeEmits)(ctx);
  154. const ast = t.arrayExpression(
  155. Array.from(runtimeEmits).map((e) => t.stringLiteral(e))
  156. );
  157. return addProperty(
  158. t,
  159. options2,
  160. t.objectProperty(t.identifier("emits"), ast)
  161. );
  162. }
  163. });
  164. function getTypeAnnotation(node) {
  165. if ("typeAnnotation" in node && node.typeAnnotation && node.typeAnnotation.type === "TSTypeAnnotation") {
  166. return node.typeAnnotation.typeAnnotation;
  167. }
  168. }
  169. function checkDefineComponent(path) {
  170. var _a;
  171. const defineCompImport = (_a = path.scope.getBinding("defineComponent")) == null ? void 0 : _a.path.parent;
  172. if (!defineCompImport) return true;
  173. return defineCompImport.type === "ImportDeclaration" && /^@?vue(\/|$)/.test(defineCompImport.source.value);
  174. }
  175. function addProperty(t, object, property) {
  176. if (t.isObjectExpression(object)) {
  177. object.properties.unshift(property);
  178. } else if (t.isExpression(object)) {
  179. return t.objectExpression([property, t.spreadElement(object)]);
  180. }
  181. return object;
  182. }