|
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/index.ts
-
-
-
-
-
-
-
- var _common = require('@vue-macros/common');
- function transformExportProps(code, id) {
- var _a;
- const { scriptSetup, getSetupAst } = _common.parseSFC.call(void 0, code, id);
- if (!scriptSetup)
- return;
- const offset = scriptSetup.loc.start.offset;
- const s = new (0, _common.MagicString)(code);
- const props = {};
- let hasDefineProps = false;
- function walkVariableDeclarator(decl) {
- if (decl.id.type !== "Identifier") {
- throw new Error("Only support identifier in export props");
- } else if (decl.init && (_common.isCallOf.call(void 0, decl.init, _common.DEFINE_PROPS) || _common.isCallOf.call(void 0, decl.init, _common.WITH_DEFAULTS))) {
- hasDefineProps = true;
- return;
- }
- const name = decl.id.name;
- const type = decl.id.typeAnnotation ? s.sliceNode(decl.id.typeAnnotation, { offset }) : ": any";
- const defaultValue = decl.init ? s.sliceNode(decl.init, { offset }) : void 0;
- props[name] = { type, defaultValue };
- }
- const setupAst = getSetupAst();
- for (const stmt of setupAst.body) {
- if (stmt.type === "ExportNamedDeclaration" && ((_a = stmt.declaration) == null ? void 0 : _a.type) === "VariableDeclaration") {
- for (const decl of stmt.declaration.declarations) {
- walkVariableDeclarator(decl);
- }
- s.removeNode(stmt, { offset });
- } else if (_common.isCallOf.call(void 0, stmt, _common.DEFINE_PROPS) || _common.isCallOf.call(void 0, stmt, _common.WITH_DEFAULTS)) {
- hasDefineProps = true;
- }
- }
- if (Object.keys(props).length === 0)
- return;
- else if (hasDefineProps)
- throw new Error("Don't support export props mixed with defineProps");
- let codegen = "";
- let destructureString = "";
- for (const [name, { type, defaultValue }] of Object.entries(props)) {
- codegen += ` ${name}${defaultValue ? "?" : ""}${type},
- `;
- destructureString += ` ${name}${defaultValue ? ` = ${defaultValue}` : ""},`;
- }
- codegen = `const {${destructureString} } = defineProps<{
- ${codegen}}>()`;
- s.prependLeft(offset, `
- ${codegen}`);
- return _common.getTransformResult.call(void 0, s, id);
- }
-
-
-
- exports.transformExportProps = transformExportProps;
|