'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( "