版博士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-SPMTHNBU.mjs 5.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // src/core/constants.ts
  2. import { HELPER_PREFIX } from "@vue-macros/common";
  3. var PROPS_VARIABLE_NAME = `${HELPER_PREFIX}props`;
  4. var EMIT_VARIABLE_NAME = `${HELPER_PREFIX}emit`;
  5. // src/core/define-emit.ts
  6. import { DEFINE_EMIT, isCallOf, walkAST } from "@vue-macros/common";
  7. function mountEmits(emits) {
  8. const isAllWithoutOptions = emits.every(({ validator }) => !validator);
  9. if (isAllWithoutOptions) {
  10. return `[${emits.map(({ name }) => JSON.stringify(name)).join(", ")}]`;
  11. }
  12. return `{
  13. ${emits.map(
  14. ({ name, validator }) => `${JSON.stringify(name)}: ${validator || `null`}`
  15. ).join(",\n ")}
  16. }`;
  17. }
  18. function transformDefineEmit({ setupAst, offset, s }) {
  19. const emits = [];
  20. walkAST(setupAst, {
  21. enter(node, parent) {
  22. if (isCallOf(node, DEFINE_EMIT)) {
  23. const [name, validator] = node.arguments;
  24. let emitName;
  25. if (!name) {
  26. if (parent.type !== "VariableDeclarator" || parent.id.type !== "Identifier") {
  27. throw new Error(
  28. `A variable must be used to receive the return value of ${DEFINE_EMIT}.`
  29. );
  30. }
  31. emitName = parent.id.name;
  32. } else if (name.type !== "StringLiteral") {
  33. throw new Error(
  34. `The first argument of ${DEFINE_EMIT} must be a string literal.`
  35. );
  36. } else {
  37. emitName = name.value;
  38. }
  39. emits.push({
  40. name: emitName,
  41. validator: validator ? s.sliceNode(validator, { offset }) : void 0
  42. });
  43. s.overwriteNode(
  44. node,
  45. `(...args) => ${EMIT_VARIABLE_NAME}(${JSON.stringify(
  46. emitName
  47. )}, ...args)`,
  48. { offset }
  49. );
  50. }
  51. }
  52. });
  53. if (emits.length > 0) {
  54. s.prependLeft(
  55. offset,
  56. `
  57. const ${EMIT_VARIABLE_NAME} = defineEmits(${mountEmits(emits)})
  58. `
  59. );
  60. }
  61. }
  62. // src/core/define-prop.ts
  63. import {
  64. DEFINE_PROP,
  65. HELPER_PREFIX as HELPER_PREFIX2,
  66. importHelperFn,
  67. isCallOf as isCallOf2,
  68. walkAST as walkAST2
  69. } from "@vue-macros/common";
  70. import {
  71. inferRuntimeType,
  72. resolveTSReferencedType,
  73. toRuntimeTypeString
  74. } from "@vue-macros/api";
  75. async function mountProps({ id, scriptSetup, setupAst, isProduction }, props) {
  76. const isAllWithoutOptions = props.every(
  77. ({ definition, typeParameter }) => !definition && !typeParameter
  78. );
  79. if (isAllWithoutOptions) {
  80. return `[${props.map(({ name }) => JSON.stringify(name)).join(", ")}]`;
  81. }
  82. let propsString = "{\n";
  83. for (const { name, definition, typeParameter } of props) {
  84. propsString += ` ${JSON.stringify(name)}: `;
  85. let propDef;
  86. if (typeParameter && !isProduction) {
  87. const resolved = await resolveTSReferencedType({
  88. scope: {
  89. filePath: id,
  90. content: scriptSetup.content,
  91. ast: setupAst.body
  92. },
  93. type: typeParameter
  94. });
  95. if (resolved)
  96. propDef = `{ type: ${toRuntimeTypeString(
  97. await inferRuntimeType(resolved)
  98. )} }`;
  99. }
  100. if (definition) {
  101. if (propDef) {
  102. propDef = `{ ...${propDef}, ...${definition} }`;
  103. } else {
  104. propDef = definition;
  105. }
  106. }
  107. propsString += `${propDef || "null"},
  108. `;
  109. }
  110. propsString += "}";
  111. return propsString;
  112. }
  113. async function transformDefineProp(options) {
  114. const { setupAst, offset, s } = options;
  115. const props = [];
  116. walkAST2(setupAst, {
  117. enter(node, parent) {
  118. var _a;
  119. if (isCallOf2(node, DEFINE_PROP)) {
  120. const [name, definition] = node.arguments;
  121. let propName;
  122. if (!name) {
  123. if (parent.type !== "VariableDeclarator" || parent.id.type !== "Identifier") {
  124. throw new Error(
  125. `A variable must be used to receive the return value of ${DEFINE_PROP}.`
  126. );
  127. }
  128. propName = parent.id.name;
  129. } else if (name.type !== "StringLiteral") {
  130. throw new Error(
  131. `The first argument of ${DEFINE_PROP} must be a string literal.`
  132. );
  133. } else {
  134. propName = name.value;
  135. }
  136. props.push({
  137. name: propName,
  138. definition: definition ? s.sliceNode(definition, { offset }) : void 0,
  139. typeParameter: (_a = node.typeParameters) == null ? void 0 : _a.params[0]
  140. });
  141. s.overwriteNode(
  142. node,
  143. `${HELPER_PREFIX2}computed(() => ${PROPS_VARIABLE_NAME}[${JSON.stringify(
  144. propName
  145. )}])`,
  146. { offset }
  147. );
  148. }
  149. }
  150. });
  151. if (props.length > 0) {
  152. importHelperFn(s, offset, "computed", "vue");
  153. s.prependLeft(
  154. offset,
  155. `
  156. const ${PROPS_VARIABLE_NAME} = defineProps(${await mountProps(
  157. options,
  158. props
  159. )})
  160. `
  161. );
  162. }
  163. }
  164. // src/core/index.ts
  165. import {
  166. DEFINE_EMIT as DEFINE_EMIT2,
  167. DEFINE_PROP as DEFINE_PROP2,
  168. MagicString,
  169. getTransformResult,
  170. parseSFC
  171. } from "@vue-macros/common";
  172. async function transformDefineSingle(code, id, isProduction) {
  173. const { scriptSetup, getSetupAst } = parseSFC(code, id);
  174. if (!scriptSetup)
  175. return;
  176. const offset = scriptSetup.loc.start.offset;
  177. const s = new MagicString(code);
  178. const setupAst = getSetupAst();
  179. const options = {
  180. id,
  181. s,
  182. offset,
  183. scriptSetup,
  184. setupAst,
  185. isProduction
  186. };
  187. if (code.includes(DEFINE_PROP2)) {
  188. await transformDefineProp(options);
  189. }
  190. if (code.includes(DEFINE_EMIT2)) {
  191. await transformDefineEmit(options);
  192. }
  193. return getTransformResult(s, id);
  194. }
  195. export {
  196. PROPS_VARIABLE_NAME,
  197. EMIT_VARIABLE_NAME,
  198. transformDefineEmit,
  199. transformDefineProp,
  200. transformDefineSingle
  201. };