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