版博士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.
 
 
 
 

70 lines
2.3 KiB

  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/index.ts
  2. var _common = require('@vue-macros/common');
  3. var _api = require('@vue-macros/api');
  4. async function transformBetterDefine(code, id, isProduction) {
  5. const s = new (0, _common.MagicString)(code);
  6. const sfc = _common.parseSFC.call(void 0, code, id);
  7. if (!sfc.scriptSetup)
  8. return;
  9. const offset = sfc.scriptSetup.loc.start.offset;
  10. const result = await _api.analyzeSFC.call(void 0, s, sfc);
  11. if (result.props) {
  12. await processProps(result.props);
  13. }
  14. if (result.emits) {
  15. processEmits(result.emits);
  16. }
  17. return _common.getTransformResult.call(void 0, s, id);
  18. async function processProps(props) {
  19. const runtimeDefs = await props.getRuntimeDefinitions();
  20. const runtimeDecls = `{
  21. ${Object.entries(runtimeDefs).map(([key, { type, required, default: defaultDecl }]) => {
  22. const escapedKey = JSON.stringify(key);
  23. let defaultString = "";
  24. if (defaultDecl) {
  25. defaultString = defaultDecl("default");
  26. }
  27. if (!isProduction) {
  28. return `${escapedKey}: { type: ${_api.toRuntimeTypeString.call(void 0,
  29. type
  30. )}, required: ${required}, ${defaultString} }`;
  31. } else if (type.some((el) => el === "Boolean" || el === "Function")) {
  32. return `${escapedKey}: { type: ${_api.toRuntimeTypeString.call(void 0,
  33. type
  34. )}, ${defaultString} }`;
  35. } else {
  36. return `${escapedKey}: ${defaultString ? `{ ${defaultString} }` : "null"}`;
  37. }
  38. }).join(",\n ")}
  39. }`;
  40. let decl = runtimeDecls;
  41. if (props.withDefaultsAst && !props.defaults) {
  42. decl = `${_common.HELPER_PREFIX}mergeDefaults(${decl}, ${s.sliceNode(
  43. props.withDefaultsAst.arguments[1],
  44. { offset }
  45. )})`;
  46. _common.importHelperFn.call(void 0, s, offset, "mergeDefaults", "vue");
  47. }
  48. decl = `defineProps(${decl})`;
  49. s.overwriteNode(props.withDefaultsAst || props.definePropsAst, decl, {
  50. offset
  51. });
  52. }
  53. function processEmits(emits) {
  54. const runtimeDecls = `[${Object.keys(emits.definitions).map((name) => JSON.stringify(name)).join(", ")}]`;
  55. s.overwriteNode(emits.defineEmitsAst, `defineEmits(${runtimeDecls})`, {
  56. offset
  57. });
  58. }
  59. }
  60. exports.transformBetterDefine = transformBetterDefine;