index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _util = require("./util");
  5. function defaultTemplate({
  6. template
  7. }, opts, {
  8. imports,
  9. interfaces,
  10. componentName,
  11. props,
  12. jsx,
  13. exports
  14. }) {
  15. const plugins = ['jsx'];
  16. if (opts.typescript) {
  17. plugins.push('typescript');
  18. }
  19. const typeScriptTpl = template.smart({
  20. plugins
  21. });
  22. return typeScriptTpl.ast`${imports}
  23. ${interfaces}
  24. function ${componentName}(${props}) {
  25. return ${jsx};
  26. }
  27. ${exports}
  28. `;
  29. }
  30. const plugin = (api, opts) => ({
  31. visitor: {
  32. Program(path) {
  33. const {
  34. types: t
  35. } = api;
  36. const template = opts.template || defaultTemplate;
  37. const body = template(api, opts, {
  38. componentName: t.identifier(opts.state.componentName),
  39. interfaces: (0, _util.getInterface)(api, opts),
  40. props: (0, _util.getProps)(api, opts),
  41. imports: (0, _util.getImport)(api, opts),
  42. exports: (0, _util.getExport)(api, opts),
  43. jsx: path.node.body[0].expression
  44. });
  45. if (Array.isArray(body)) {
  46. path.node.body = body;
  47. } else {
  48. path.node.body = [body];
  49. }
  50. path.replaceWith(path.node);
  51. }
  52. }
  53. });
  54. var _default = plugin;
  55. exports.default = _default;