jiuyiUniapp/jiuyi2/node_modules/unplugin-vue2-script-setup/dist/esbuild.js

936 lines
130 KiB
JavaScript
Raw Normal View History

2024-12-18 15:46:27 +08:00
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var unplugin$1 = require('unplugin');
var pluginutils = require('@rollup/pluginutils');
var MagicString = require('magic-string');
var reactivityTransform = require('@vue/reactivity-transform');
var utils = require('@antfu/utils');
var compilerCore = require('@vue/compiler-core');
var compilerDom = require('@vue/compiler-dom');
var shared = require('@vue/shared');
var babel = require('@babel/core');
var parser = require('@babel/parser');
var g = require('@babel/generator');
var babel_traverse = require('@babel/traverse');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
var babel_traverse__namespace = /*#__PURE__*/_interopNamespaceDefault(babel_traverse);
var _a, _b, _c;
const t = (babel__namespace.default || babel__namespace).types;
const generate = g.default || g;
const traverse = (_c = (_b = (_a = babel_traverse__namespace == null ? void 0 : babel_traverse__namespace.default) == null ? void 0 : _a.default) != null ? _b : babel_traverse__namespace == null ? void 0 : babel_traverse__namespace.default) != null ? _c : babel_traverse__namespace;
function getIdentifierDeclarations(nodes) {
let result;
let programScopeUid;
traverse(t.file(t.program(nodes)), {
Program(path) {
result = new Set(Object.keys(path.scope.bindings));
programScopeUid = path.scope.uid;
},
// FIXME: babel bug, temporary add TSEnumDeclaration and TSModuleDeclaration logic
TSEnumDeclaration(path) {
if (path.scope.uid === programScopeUid)
result.add(path.node.id.name);
},
TSModuleDeclaration(path) {
if (path.scope.uid === programScopeUid) {
const id = path.node.id;
if (id.type === "Identifier")
result.add(id.name);
}
}
});
return Array.from(result);
}
function getFileGlobals(result) {
let globals;
let programScopeUid;
traverse(result, {
Program(path) {
globals = new Set(Object.keys(path.scope.globals));
programScopeUid = path.scope.uid;
},
// FIXME: babel bug, temporary add TSEnumDeclaration and TSModuleDeclaration logic
TSEnumDeclaration(path) {
if (path.scope.uid === programScopeUid)
globals.delete(path.node.id.name);
},
TSModuleDeclaration(path) {
if (path.scope.uid === programScopeUid) {
const id = path.node.id;
if (id.type === "Identifier")
globals.delete(id.name);
}
}
});
return Array.from(globals);
}
const pascalize = (str) => shared.capitalize(shared.camelize(str));
function exhaustiveCheckReturnUndefined(_param) {
return void 0;
}
var NodeTypes;
((NodeTypes2) => {
NodeTypes2.ROOT = 0;
NodeTypes2.ELEMENT = 1;
NodeTypes2.TEXT = 2;
NodeTypes2.COMMENT = 3;
NodeTypes2.SIMPLE_EXPRESSION = 4;
NodeTypes2.INTERPOLATION = 5;
NodeTypes2.ATTRIBUTE = 6;
NodeTypes2.DIRECTIVE = 7;
NodeTypes2.COMPOUND_EXPRESSION = 8;
NodeTypes2.IF = 9;
NodeTypes2.IF_BRANCH = 10;
NodeTypes2.FOR = 11;
NodeTypes2.TEXT_CALL = 12;
NodeTypes2.VNODE_CALL = 13;
NodeTypes2.JS_CALL_EXPRESSION = 14;
NodeTypes2.JS_OBJECT_EXPRESSION = 15;
NodeTypes2.JS_PROPERTY = 16;
NodeTypes2.JS_ARRAY_EXPRESSION = 17;
NodeTypes2.JS_FUNCTION_EXPRESSION = 18;
NodeTypes2.JS_CONDITIONAL_EXPRESSION = 19;
NodeTypes2.JS_CACHE_EXPRESSION = 20;
NodeTypes2.JS_BLOCK_STATEMENT = 21;
NodeTypes2.JS_TEMPLATE_LITERAL = 22;
NodeTypes2.JS_IF_STATEMENT = 23;
NodeTypes2.JS_ASSIGNMENT_EXPRESSION = 24;
NodeTypes2.JS_SEQUENCE_EXPRESSION = 25;
NodeTypes2.JS_RETURN_STATEMENT = 26;
})(NodeTypes || (NodeTypes = {}));
var ElementTypes;
((ElementTypes2) => {
ElementTypes2.ELEMENT = 0;
ElementTypes2.COMPONENT = 1;
ElementTypes2.SLOT = 2;
ElementTypes2.TEMPLATE = 3;
})(ElementTypes || (ElementTypes = {}));
const BUILD_IN_DIRECTIVES = /* @__PURE__ */ new Set([
"if",
"else",
"else-if",
"for",
"once",
"model",
"on",
"bind",
"slot",
"slot-scope",
"key",
"ref",
"text",
"html",
"show",
"pre",
"cloak"
// 'el',
// 'ref',
]);
function getComponents(node) {
var _a;
const current = node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.COMPONENT ? [node.tag] : node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.ELEMENT ? [node.tag] : [];
const children = node.type === NodeTypes.IF ? node.branches : node.type === NodeTypes.ELEMENT || node.type === NodeTypes.IF_BRANCH || node.type === NodeTypes.FOR ? node.children : node.type === NodeTypes.TEXT || node.type === NodeTypes.COMMENT || node.type === NodeTypes.COMPOUND_EXPRESSION || node.type === NodeTypes.TEXT_CALL || node.type === NodeTypes.INTERPOLATION ? [] : (_a = exhaustiveCheckReturnUndefined()) != null ? _a : [];
return [...current, ...children.flatMap(getComponents)];
}
function getDirectiveNames(node) {
if (node.type === NodeTypes.ELEMENT) {
const directives = node.props.flatMap(
(x) => x.type === NodeTypes.DIRECTIVE ? [x.name] : []
);
return [...directives, ...node.children.flatMap(getDirectiveNames)];
} else if (node.type === NodeTypes.IF) {
return node.branches.flatMap(getDirectiveNames);
} else if (node.type === NodeTypes.IF_BRANCH || node.type === NodeTypes.FOR) {
return node.children.flatMap(getDirectiveNames);
} else if (node.type === NodeTypes.INTERPOLATION || node.type === NodeTypes.COMPOUND_EXPRESSION || node.type === NodeTypes.TEXT || node.type === NodeTypes.COMMENT || node.type === NodeTypes.TEXT_CALL) {
return [];
} else {
return [];
}
}
function getFreeVariablesForText(input) {
const inputWithPrefix = input.trimStart()[0] === "{" ? `(${input})` : input;
return getFileGlobals(parser.parse(inputWithPrefix));
}
function getFreeVariablesForPropsNode(node) {
if (node.type === NodeTypes.DIRECTIVE) {
const arg = node.arg === void 0 ? [] : getFreeVariablesForNode(node.arg);
const exp = node.exp === void 0 ? [] : getFreeVariablesForNode(node.exp);
return [...arg, ...exp];
}
return [];
}
function getFreeVariablesForNode(node) {
var _a, _b, _c;
if (node.type === NodeTypes.SIMPLE_EXPRESSION) {
return node.isStatic ? [] : getFreeVariablesForText(node.content);
} else if (node.type === NodeTypes.COMPOUND_EXPRESSION) {
return node.children.flatMap(
(x) => typeof x !== "object" ? [] : getFreeVariablesForNode(x)
);
} else if (node.type === NodeTypes.INTERPOLATION) {
return getFreeVariablesForNode(node.content);
} else if (node.type === NodeTypes.ELEMENT) {
const children = node.children.flatMap(getFreeVariablesForNode);
const directiveProps = node.props.flatMap(
(x) => x.type === NodeTypes.DIRECTIVE ? [x] : []
);
const attributeProps = node.props.flatMap(
(x) => x.type === NodeTypes.ATTRIBUTE ? [x] : []
);
const refNode = attributeProps.find(
(node2) => node2.name === "ref" && node2.value !== void 0
);
const refIdentifier = (_a = refNode == null ? void 0 : refNode.value) == null ? void 0 : _a.content;
const vSlotNode = directiveProps.find((node2) => node2.name === "slot");
const vSlotArgIdentifiers = (vSlotNode == null ? void 0 : vSlotNode.arg) === void 0 ? [] : getFreeVariablesForNode(vSlotNode.arg);
const vSlotExpVariableShadowingIdentifiers = [];
const vForNode = directiveProps.find((node2) => node2.name === "for");
const vForIdentifiers = ((_b = vForNode == null ? void 0 : vForNode.exp) == null ? void 0 : _b.type) === NodeTypes.SIMPLE_EXPRESSION ? getFreeVariablesForText(
vForNode.exp.content.replace(/^.*\s(?:in|of)\s/, "")
) : [];
const vForExpVariableShadowingIdentifiers = [];
const props = directiveProps.filter(({ name }) => name !== "slot" && name !== "for").flatMap(getFreeVariablesForPropsNode);
const shadowingIdentifiers = /* @__PURE__ */ new Set([
...vSlotExpVariableShadowingIdentifiers,
...vForExpVariableShadowingIdentifiers
]);
return [
...vSlotArgIdentifiers,
refIdentifier,
...vForIdentifiers,
...[...children, ...props].filter((x) => !shadowingIdentifiers.has(x))
].filter(utils.notNullish);
} else if (node.type === NodeTypes.FOR) {
return [node.source, ...node.children].flatMap(getFreeVariablesForNode);
} else if (node.type === NodeTypes.IF) {
return ((_c = node.branches) != null ? _c : []).flatMap(getFreeVariablesForNode);
} else if (node.type === NodeTypes.IF_BRANCH) {
return [node.condition, ...node.children].filter(utils.notNullish).flatMap(getFreeVariablesForNode);
} else if (node.type === NodeTypes.TEXT || node.type === NodeTypes.COMMENT || node.type === NodeTypes.TEXT_CALL) {
return [];
} else {
return [];
}
}
function findReferencesForSFC(templateNode) {
var _a, _b, _c;
const components = (_a = templateNode == null ? void 0 : templateNode.children.flatMap(getComponents)) != null ? _a : [];
const directives = (_b = templateNode == null ? void 0 : templateNode.children.flatMap(getDirectiveNames)) != null ? _b : [];
const identifiers = (_c = templateNode == null ? void 0 : templateNode.children.flatMap(getFreeVariablesForNode)) != null ? _c : [];
return {
components,
directives,
identifiers
};
}
function getBabelParserOptions(lang) {
lang = lang || "js";
const pluginsDict = {
js: [],
ts: ["typescript"],
jsx: ["jsx"],
tsx: ["jsx", "typescript"]
};
const plugins = pluginsDict[lang];
if (plugins === void 0)
throw new SyntaxError(`Unsupported script language: ${lang}`);
return {
sourceType: "module",
plugins
};
}
async function parseSFC(code, id, options) {
var _a, _b, _c;
const elementChildren = compilerCore.baseParse(code, compilerDom.parserOptions).children.flatMap(
(x) => x.type === NodeTypes.ELEMENT && x.tagType === ElementTypes.ELEMENT ? [x] : []
);
const templateNode = elementChildren.find((x) => x.tag === "template");
const [scriptSetupChildNodes, scriptChildNodes] = utils.partition(
elementChildren.filter((x) => x.tag === "script"),
(x) => x.props.some((p) => p.type === NodeTypes.ATTRIBUTE && p.name === "setup")
);
const getScriptTagMeta = (n, astTransforms = (x) => x) => {
var _a2, _b2, _c2, _d, _e, _f;
if (n === void 0) {
const content2 = "";
const ast2 = parser.parse(content2, {
sourceType: "module",
plugins: []
}).program;
return {
start: 0,
end: 0,
contentStart: 0,
contentEnd: 0,
content: content2,
attrs: {},
found: false,
ast: ast2
};
}
const attrs = Object.fromEntries(
n.props.flatMap(
(x) => {
var _a3;
return !(x.type === NodeTypes.ATTRIBUTE && typeof ((_a3 = x.value) == null ? void 0 : _a3.content) === "string") ? [] : [[x.name, x.value.content]];
}
)
);
const content = (_b2 = (_a2 = n.children[0]) == null ? void 0 : _a2.loc.source) != null ? _b2 : "";
const contentStart = (_d = (_c2 = n.children[0]) == null ? void 0 : _c2.loc.start.offset) != null ? _d : 0;
const contentEnd = (_f = (_e = n.children[0]) == null ? void 0 : _e.loc.end.offset) != null ? _f : 0;
const ast = astTransforms(
parser.parse(content, getBabelParserOptions(attrs.lang)).program
);
return {
start: n.loc.start.offset,
end: n.loc.end.offset,
contentStart,
contentEnd,
content,
attrs,
found: true,
ast
};
};
const scriptSetup = getScriptTagMeta(
scriptSetupChildNodes[0],
(_a = options == null ? void 0 : options.astTransforms) == null ? void 0 : _a.scriptSetup
);
const script = getScriptTagMeta(
scriptChildNodes[0],
(_b = options == null ? void 0 : options.astTransforms) == null ? void 0 : _b.script
);
if (script.found && scriptSetup.found && scriptSetup.attrs.lang !== script.attrs.lang) {
throw new SyntaxError(
"<script setup> language must be the same as <script>"
);
}
const codeOfTemplate = templateNode == null ? void 0 : templateNode.props.some(
(p) => {
var _a2;
return p.type === NodeTypes.ATTRIBUTE && p.name === "lang" && ((_a2 = p.value) == null ? void 0 : _a2.type) === NodeTypes.TEXT && p.value.content === "pug";
}
) ? compilerCore.baseParse(
(await import('pug')).compile(
templateNode.children.map((x) => x.loc.source).join(""),
{
filename: id
}
)(),
compilerDom.parserOptions
) : templateNode;
const result = codeOfTemplate ? findReferencesForSFC(codeOfTemplate) : void 0;
return {
id,
template: {
components: new Set(result == null ? void 0 : result.components.map(pascalize)),
directives: new Set(
result == null ? void 0 : result.directives.filter((x) => !BUILD_IN_DIRECTIVES.has(x)).map(shared.camelize)
),
identifiers: new Set(result == null ? void 0 : result.identifiers)
},
scriptSetup,
script,
parserOptions: getBabelParserOptions(
(_c = script.attrs.lang) != null ? _c : scriptSetup.attrs.lang
),
extraDeclarations: []
};
}
const DEFINE_PROPS = "defineProps";
const DEFINE_EMITS = "defineEmits";
const DEFINE_EXPOSE = "defineExpose";
const WITH_DEFAULTS = "withDefaults";
function applyMacros(nodes) {
let hasDefinePropsCall = false;
let hasDefineEmitCall = false;
let propsRuntimeDecl;
let propsRuntimeDefaults;
let propsTypeDecl;
let propsTypeDeclRaw;
let emitsRuntimeDecl;
let emitsTypeDecl;
let emitsTypeDeclRaw;
let exposeDecl;
const typeDeclaredProps = {};
const declaredTypes = {};
function error(msg, _node) {
throw new Error(msg);
}
function processDefineProps(node) {
if (!isCallOf(node, DEFINE_PROPS))
return false;
if (hasDefinePropsCall)
error(`duplicate ${DEFINE_PROPS}() call`);
hasDefinePropsCall = true;
propsRuntimeDecl = node.arguments[0];
if (node.typeParameters) {
if (propsRuntimeDecl) {
error(
`${DEFINE_PROPS}() cannot accept both type and non-type arguments at the same time. Use one or the other.`);
}
propsTypeDeclRaw = node.typeParameters.params[0];
propsTypeDecl = resolveQualifiedType(
propsTypeDeclRaw,
(node2) => node2.type === "TSTypeLiteral"
);
if (!propsTypeDecl) {
error(
`type argument passed to ${DEFINE_PROPS}() must be a literal type, or a reference to an interface or literal type.`);
}
}
return true;
}
function processWithDefaults(node) {
if (!isCallOf(node, WITH_DEFAULTS))
return false;
if (processDefineProps(node.arguments[0])) {
if (propsRuntimeDecl) {
error(
`${WITH_DEFAULTS} can only be used with type-based ${DEFINE_PROPS} declaration.`);
}
propsRuntimeDefaults = node.arguments[1];
} else {
error(
`${WITH_DEFAULTS}' first argument must be a ${DEFINE_PROPS} call.`,
node.arguments[0] || node
);
}
return true;
}
function processDefineEmits(node) {
if (!isCallOf(node, DEFINE_EMITS))
return false;
if (hasDefineEmitCall)
error(`duplicate ${DEFINE_EMITS}() call`);
hasDefineEmitCall = true;
emitsRuntimeDecl = node.arguments[0];
if (node.typeParameters) {
if (emitsRuntimeDecl) {
error(
`${DEFINE_EMITS}() cannot accept both type and non-type arguments at the same time. Use one or the other.`);
}
emitsTypeDeclRaw = node.typeParameters.params[0];
emitsTypeDecl = resolveQualifiedType(
emitsTypeDeclRaw,
(node2) => node2.type === "TSFunctionType" || node2.type === "TSTypeLiteral"
);
if (!emitsTypeDecl) {
error(
`type argument passed to ${DEFINE_EMITS}() must be a function type, a literal type with call signatures, or a reference to the above types.`);
}
}
return true;
}
function resolveQualifiedType(node, qualifier) {
if (qualifier(node))
return node;
if (node.type === "TSTypeReference" && node.typeName.type === "Identifier") {
const refName = node.typeName.name;
const isQualifiedType = (node2) => {
if (node2.type === "TSInterfaceDeclaration" && node2.id.name === refName)
return node2.body;
else if (node2.type === "TSTypeAliasDeclaration" && node2.id.name === refName && qualifier(node2.typeAnnotation))
return node2.typeAnnotation;
else if (node2.type === "ExportNamedDeclaration" && node2.declaration)
return isQualifiedType(node2.declaration);
};
for (const node2 of nodes) {
const qualified = isQualifiedType(node2);
if (qualified)
return qualified;
}
}
}
function processDefineExpose(node) {
if (!isCallOf(node, DEFINE_EXPOSE))
return false;
if (exposeDecl)
error(`duplicate ${DEFINE_EXPOSE}() call`);
if (node.arguments.length !== 1)
error(`${DEFINE_EXPOSE}() requires one argument`);
exposeDecl = node.arguments[0];
return true;
}
function genRuntimeProps(props) {
const keys = Object.keys(props);
if (!keys.length)
return void 0;
const hasStaticDefaults = propsRuntimeDefaults && propsRuntimeDefaults.type === "ObjectExpression" && propsRuntimeDefaults.properties.every(
(node) => node.type === "ObjectProperty" && !node.computed
);
return t.objectExpression(
Object.entries(props).map(([key, value]) => {
const prop = hasStaticDefaults ? propsRuntimeDefaults.properties.find((node) => node.key.name === key) : void 0;
if (prop)
value.required = false;
const entries = Object.entries(value).map(
([key2, value2]) => key2 === "type" ? t.objectProperty(t.identifier(key2), typeof value2 === "string" ? t.identifier(value2) : t.arrayExpression(value2.map((i) => t.identifier(i)))) : t.objectProperty(t.identifier(key2), parser.parseExpression(JSON.stringify(value2)))
);
if (prop)
entries.push(t.objectProperty(t.identifier("default"), prop.value));
return t.objectProperty(
t.identifier(key),
t.objectExpression(entries)
);
})
);
}
function getProps() {
if (propsRuntimeDecl)
return propsRuntimeDecl;
if (propsTypeDecl) {
extractRuntimeProps(propsTypeDecl, typeDeclaredProps, declaredTypes);
return genRuntimeProps(typeDeclaredProps);
}
}
function throwIfAwait(node) {
if (node.type === "AwaitExpression")
error("top-level await is not supported in Vue 2");
}
nodes = nodes.map((raw) => {
let node = raw;
if (raw.type === "ExpressionStatement")
node = raw.expression;
if (node.type === "VariableDeclaration" && !node.declare) {
const total = node.declarations.length;
for (let i = 0; i < total; i++) {
const decl = node.declarations[i];
if (decl.init) {
if (processDefineEmits(decl.init))
decl.init = t.memberExpression(t.identifier("__ctx"), t.identifier("emit"));
else if (processDefineProps(decl.init) || processWithDefaults(decl.init))
decl.init = t.identifier("__props");
else
throwIfAwait(decl.init);
}
}
}
if (processWithDefaults(node) || processDefineEmits(node) || processDefineProps(node) || processDefineExpose(node))
return null;
throwIfAwait(node);
return raw;
}).filter(Boolean);
return {
nodes,
props: getProps(),
expose: exposeDecl
};
}
function isCallOf(node, test) {
return !!(node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : test(node.callee.name)));
}
function extractRuntimeProps(node, props, declaredTypes) {
const members = node.type === "TSTypeLiteral" ? node.members : node.body;
for (const m of members) {
if ((m.type === "TSPropertySignature" || m.type === "TSMethodSignature") && m.key.type === "Identifier") {
let type;
if (m.type === "TSMethodSignature") {
type = ["Function"];
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
);
}
props[m.key.name] = {
key: m.key.name,
required: !m.optional,
type: (type == null ? void 0 : type.length) === 1 ? type[0] : type || "null"
};
}
}
}
function inferRuntimeType(node, declaredTypes) {
switch (node.type) {
case "TSStringKeyword":
return ["String"];
case "TSNumberKeyword":
return ["Number"];
case "TSBooleanKeyword":
return ["Boolean"];
case "TSObjectKeyword":
return ["Object"];
case "TSTypeLiteral":
return ["Object"];
case "TSFunctionType":
return ["Function"];
case "TSArrayType":
case "TSTupleType":
return ["Array"];
case "TSLiteralType":
switch (node.literal.type) {
case "StringLiteral":
return ["String"];
case "BooleanLiteral":
return ["Boolean"];
case "NumericLiteral":
case "BigIntLiteral":
return ["Number"];
default:
return ["null"];
}
case "TSTypeReference":
if (node.typeName.type === "Identifier") {
if (declaredTypes[node.typeName.name])
return declaredTypes[node.typeName.name];
switch (node.typeName.name) {
case "Array":
case "Function":
case "Object":
case "Set":
case "Map":
case "WeakSet":
case "WeakMap":
return [node.typeName.name];
case "Record":
case "Partial":
case "Readonly":
case "Pick":
case "Omit":
case "Exclude":
case "Extract":
case "Required":
case "InstanceType":
return ["Object"];
}
}
return ["null"];
case "TSParenthesizedType":
return inferRuntimeType(node.typeAnnotation, declaredTypes);
case "TSUnionType":
return [
...new Set(
[].concat(
...node.types.map((t2) => inferRuntimeType(t2, declaredTypes))
)
)
];
case "TSIntersectionType":
return ["Object"];
default:
return ["null"];
}
}
function isAsyncImport(node) {
if (t.isVariableDeclaration(node)) {
const declaration = node.declarations[0];
return declaration !== void 0 && t.isCallExpression(declaration.init) && t.isIdentifier(declaration.init.callee) && declaration.init.callee.name === "defineAsyncComponent";
}
return false;
}
function transformScriptSetup(sfc, options) {
var _a, _b;
const { scriptSetup, script, template } = sfc;
const { nodes: body, props, expose } = applyMacros(scriptSetup.ast.body);
const [hoisted, setupBody] = utils.partition(
body,
(n) => isAsyncImport(n) || t.isImportDeclaration(n) || t.isExportNamedDeclaration(n) || n.type.startsWith("TS")
);
const declarationArray = utils.uniq([
...getIdentifierDeclarations(hoisted),
...getIdentifierDeclarations(setupBody),
...getIdentifierDeclarations(script.ast.body)
]).filter(utils.notNullish);
const returns = declarationArray.filter((i) => template.identifiers.has(i)).map((i) => {
const id = t.identifier(i);
return t.objectProperty(id, id, false, true);
});
const components = Array.from(template.components).map(
(component) => {
var _a2;
return (_a2 = declarationArray.find((declare) => declare === component)) != null ? _a2 : declarationArray.find((declare) => pascalize(declare) === component);
}
).filter(utils.notNullish);
const directiveDeclaration = Array.from(template.directives).map((directive) => {
const identifier = declarationArray.find(
(declaration) => declaration === `v${shared.capitalize(directive)}`
);
if (identifier === void 0)
return void 0;
return { identifier, directive };
}).filter(utils.notNullish);
const __sfc = t.identifier("__sfc_main");
let hasBody = false;
const bodyNodes = script.ast.body.map((node) => {
if (node.type === "ExportDefaultDeclaration") {
hasBody = true;
return t.variableDeclaration("const", [
t.variableDeclarator(__sfc, node.declaration)
]);
}
return node;
});
let ast = t.program([
...sfc.extraDeclarations,
...hoisted,
...bodyNodes
]);
if (!hasBody) {
ast.body.push(
t.variableDeclaration("const", [
t.variableDeclarator(__sfc, t.objectExpression([]))
])
);
}
if (props) {
hasBody = true;
ast.body.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(__sfc, t.identifier("props")),
props
)
)
);
}
if (body.length) {
hasBody = true;
const returnExpr = expose ? t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("assign")),
[t.objectExpression(returns), expose]
) : t.objectExpression(returns);
const returnStatement = t.returnStatement(returnExpr);
ast.body.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(__sfc, t.identifier("setup")),
t.arrowFunctionExpression(
[t.identifier("__props"), t.identifier("__ctx")],
t.blockStatement([...setupBody, returnStatement])
)
)
)
);
}
if (components.length) {
hasBody = true;
const componentsObject = t.objectExpression(
components.map((i) => {
const id = t.identifier(i);
return t.objectProperty(id, id, false, true);
})
);
ast.body.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(__sfc, t.identifier("components")),
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("assign")),
[
componentsObject,
t.memberExpression(__sfc, t.identifier("components"))
]
)
)
)
);
}
if (directiveDeclaration.length) {
hasBody = true;
const directivesObject = t.objectExpression(
directiveDeclaration.map(
({ directive, identifier }) => t.objectProperty(
t.identifier(directive),
t.identifier(identifier),
false,
false
)
)
);
ast.body.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(__sfc, t.identifier("directives")),
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("assign")),
[
directivesObject,
t.memberExpression(__sfc, t.identifier("directives"))
]
)
)
)
);
}
if (!hasBody && !(options == null ? void 0 : options.astTransforms)) {
return {
ast: null,
code: ""
};
}
ast.body.push(t.exportDefaultDeclaration(__sfc));
ast = ((_b = (_a = options == null ? void 0 : options.astTransforms) == null ? void 0 : _a.post) == null ? void 0 : _b.call(_a, ast, sfc)) || ast;
return {
ast,
code: generate(ast).code
};
}
function transformSfcRefSugar(sfc, options) {
const importedHelpers = /* @__PURE__ */ new Set();
for (const script of [sfc.script, sfc.scriptSetup]) {
if (reactivityTransform.shouldTransform(script.content)) {
const s = new MagicString(script.content);
const { importedHelpers: imports } = reactivityTransform.transformAST(script.ast, s);
Array.from(imports).forEach((helper) => importedHelpers.add(helper));
script.content = s.toString();
script.ast = parser.parse(script.content, sfc.parserOptions).program;
}
}
if (importedHelpers.size) {
sfc.extraDeclarations = [
t.importDeclaration(
Array.from(importedHelpers).map((i) => t.importSpecifier(t.identifier(`_${i}`), t.identifier(i))),
t.stringLiteral(options.importHelpersFrom)
)
];
}
}
function resolveOptions(options = {}) {
return Object.assign(
{},
{
sourceMap: true,
reactivityTransform: false,
importHelpersFrom: "@vue/composition-api",
astTransforms: {}
},
options
);
}
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
const scriptSetupRE = /<script\s(.*\s)?setup(\s.*)?>/;
function shouldTransform(code, id, options) {
if (code.includes("export default __sfc_main"))
return false;
return (options == null ? void 0 : options.reactivityTransform) && reactivityTransform.shouldTransform(code) || scriptSetupRE.test(code);
}
async function transform(input, id, options) {
if (!shouldTransform(input, id, options))
return null;
const resolved = resolveOptions(options);
if (id.endsWith(".vue") || id.includes(".vue?vue"))
return await transformVue(input, id, resolved);
else
return transformNonVue(input, id, resolved);
}
function transformNonVue(input, id, options) {
if (options.reactivityTransform && reactivityTransform.shouldTransform(input)) {
return reactivityTransform.transform(input, {
filename: id,
sourceMap: options.sourceMap,
importHelpersFrom: options.importHelpersFrom
});
}
return null;
}
async function transformVue(input, id, options) {
const s = new MagicString(input);
const sfc = await parseSFC(input, id);
if (options.reactivityTransform)
transformSfcRefSugar(sfc, options);
const { code } = transformScriptSetup(sfc, options);
const attributes = __spreadValues(__spreadValues({}, sfc.script.attrs), sfc.scriptSetup.attrs);
delete attributes.setup;
const attr = Object.entries(attributes).map(([key, value]) => value ? `${key}="${value}"` : key).join(" ");
if (code) {
const block = `<script ${attr}>
${code}
<\/script>`;
s.remove(sfc.script.start, sfc.script.end);
if (sfc.scriptSetup.start !== sfc.scriptSetup.end) {
s.overwrite(
sfc.scriptSetup.start,
sfc.scriptSetup.end,
block
);
} else {
s.prependLeft(0, `${block}
`);
}
}
return {
code: s.toString(),
map: options.sourceMap ? s.generateMap({
source: id,
includeContent: true
}) : null
};
}
const unplugin = unplugin$1.createUnplugin((options = {}) => {
const filter = pluginutils.createFilter(
options.include || (options.reactivityTransform ? [/\.vue$/, /\.vue\?vue/, /\.[jt]sx?$/] : [/\.vue$/, /\.vue\?vue/]),
options.exclude || [/node_modules/, /\.git/, /\.nuxt/]
);
return {
name: "unplugin-vue2-script-setup",
enforce: "pre",
transformInclude(id) {
return filter(id);
},
async transform(code, id) {
try {
return await transform(code, id, options);
} catch (e) {
this.error(e);
}
}
};
});
var esbuild = unplugin.esbuild;
exports.default = esbuild;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXNidWlsZC5qcyIsInNvdXJjZXMiOlsiLi4vc3JjL2NvcmUvYmFiZWwudHMiLCIuLi9zcmMvY29yZS9pZGVudGlmaWVycy50cyIsIi4uL3NyYy9jb3JlL3V0aWxzLnRzIiwiLi4vc3JjL2NvcmUvcGFyc2VTRkMudHMiLCIuLi9zcmMvY29yZS9tYWNyb3MudHMiLCIuLi9zcmMvY29yZS90cmFuc2Zvcm1TY3JpcHRTZXR1cC50cyIsIi4uL3NyYy9jb3JlL3RyYW5zZm9ybVNmY1JlZlN1Z2FyLnRzIiwiLi4vc3JjL2NvcmUvb3B0aW9ucy50cyIsIi4uL3NyYy9jb3JlL3RyYW5zZm9ybS50cyIsIi4uL3NyYy9pbmRleC50cyIsIi4uL3NyYy9lc2J1aWxkLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIGJhYmVsIGZyb20gJ0BiYWJlbC9jb3JlJ1xuaW1wb3J0IHsgcGFyc2UsIHBhcnNlRXhwcmVzc2lvbiB9IGZyb20gJ0BiYWJlbC9wYXJzZXInXG5pbXBvcnQgZyBmcm9tICdAYmFiZWwvZ2VuZXJhdG9yJ1xuaW1wb3J0ICogYXMgYmFiZWxfdHJhdmVyc2UgZnJvbSAnQGJhYmVsL3RyYXZlcnNlJ1xuXG5leHBvcnQgY29uc3QgdDogdHlwZW9mIGJhYmVsWyd0eXBlcyddID0gKChiYWJlbCBhcyBhbnkpLmRlZmF1bHQgfHwgYmFiZWwpLnR5cGVzXG5leHBvcnQgY29uc3QgZ2VuZXJhdGU6IHR5cGVvZiBnID0gKChnIGFzIGFueSkuZGVmYXVsdCB8fCBnKVxuZXhwb3J0IGNvbnN0IHRyYXZlcnNlID0gKChiYWJlbF90cmF2ZXJzZSBhcyBhbnkpPy5kZWZhdWx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