912 lines
129 KiB
JavaScript
912 lines
129 KiB
JavaScript
|
import { createUnplugin } from 'unplugin';
|
||
|
import { createFilter } from '@rollup/pluginutils';
|
||
|
import MagicString from 'magic-string';
|
||
|
import { shouldTransform as shouldTransform$1, transformAST, transform as transform$1 } from '@vue/reactivity-transform';
|
||
|
import { partition, notNullish, uniq } from '@antfu/utils';
|
||
|
import { baseParse } from '@vue/compiler-core';
|
||
|
import { parserOptions } from '@vue/compiler-dom';
|
||
|
import { capitalize, camelize } from '@vue/shared';
|
||
|
import * as babel from '@babel/core';
|
||
|
import { parse, parseExpression } from '@babel/parser';
|
||
|
import g from '@babel/generator';
|
||
|
import * as babel_traverse from '@babel/traverse';
|
||
|
|
||
|
var _a, _b, _c;
|
||
|
const t = (babel.default || babel).types;
|
||
|
const generate = g.default || g;
|
||
|
const traverse = (_c = (_b = (_a = babel_traverse == null ? void 0 : babel_traverse.default) == null ? void 0 : _a.default) != null ? _b : babel_traverse == null ? void 0 : babel_traverse.default) != null ? _c : babel_traverse;
|
||
|
|
||
|
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) => capitalize(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(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(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(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 = baseParse(code, parserOptions).children.flatMap(
|
||
|
(x) => x.type === NodeTypes.ELEMENT && x.tagType === ElementTypes.ELEMENT ? [x] : []
|
||
|
);
|
||
|
const templateNode = elementChildren.find((x) => x.tag === "template");
|
||
|
const [scriptSetupChildNodes, scriptChildNodes] = 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 = 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(
|
||
|
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";
|
||
|
}
|
||
|
) ? baseParse(
|
||
|
(await import('pug')).compile(
|
||
|
templateNode.children.map((x) => x.loc.source).join(""),
|
||
|
{
|
||
|
filename: id
|
||
|
}
|
||
|
)(),
|
||
|
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(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), 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] = partition(
|
||
|
body,
|
||
|
(n) => isAsyncImport(n) || t.isImportDeclaration(n) || t.isExportNamedDeclaration(n) || n.type.startsWith("TS")
|
||
|
);
|
||
|
const declarationArray = uniq([
|
||
|
...getIdentifierDeclarations(hoisted),
|
||
|
...getIdentifierDeclarations(setupBody),
|
||
|
...getIdentifierDeclarations(script.ast.body)
|
||
|
]).filter(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(notNullish);
|
||
|
const directiveDeclaration = Array.from(template.directives).map((directive) => {
|
||
|
const identifier = declarationArray.find(
|
||
|
(declaration) => declaration === `v${capitalize(directive)}`
|
||
|
);
|
||
|
if (identifier === void 0)
|
||
|
return void 0;
|
||
|
return { identifier, directive };
|
||
|
}).filter(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 (shouldTransform$1(script.content)) {
|
||
|
const s = new MagicString(script.content);
|
||
|
const { importedHelpers: imports } = transformAST(script.ast, s);
|
||
|
Array.from(imports).forEach((helper) => importedHelpers.add(helper));
|
||
|
script.content = s.toString();
|
||
|
script.ast = 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) && shouldTransform$1(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 && shouldTransform$1(input)) {
|
||
|
return transform$1(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 = createUnplugin((options = {}) => {
|
||
|
const filter = 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;
|
||
|
|
||
|
export { esbuild as default };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ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