"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => vueJsxHmrPlugin }); module.exports = __toCommonJS(src_exports); var import_node_crypto = require("crypto"); var import_babel_plugin_jsx = __toESM(require("@vue/babel-plugin-jsx"), 1); function interopDefault(m) { return m.default || m; } var jsx = interopDefault(import_babel_plugin_jsx.default); function vueJsxHmrPlugin({ types: t }) { let declaredComponents = []; let hotComponents = []; let hasDefault = false; const visitor = { VariableDeclaration(path) { const names = parseComponentDecls(path.node); if (names.length) { declaredComponents.push(...names); } }, ExportNamedDeclaration(path) { if (path.node.declaration && path.node.declaration.type === "VariableDeclaration") { hotComponents.push( ...parseComponentDecls(path.node.declaration).map((name) => ({ local: name, id: getHash(path.hub.file.opts.filename + name) })) ); } else if (path.node.specifiers.length) { for (const spec of path.node.specifiers) { if (spec.type === "ExportSpecifier" && spec.exported.type === "Identifier") { const matched = declaredComponents.find( (name) => name === spec.local.name ); if (matched) { hotComponents.push({ local: spec.local.name, id: getHash( path.hub.file.opts.filename + spec.exported.name ) }); } } } } }, ExportDefaultDeclaration(path) { if (path.node.declaration.type === "Identifier") { const _name = path.node.declaration.name; const matched = declaredComponents.find((name) => name === _name); if (matched) { hotComponents.push({ local: _name, id: getHash(path.hub.file.opts.filename + "default") }); } } else if (isDefineComponentCall(path.node.declaration)) { hasDefault = true; hotComponents.push({ local: "__default__", id: getHash(path.hub.file.opts.filename + "default") }); } } }; return { inherits: interopDefault(jsx), pre() { declaredComponents = []; hotComponents = []; hasDefault = false; }, visitor, post(file) { if (hotComponents.length) { const code = []; const callbackCode = []; for (const { local, id } of hotComponents) { code.push( t.expressionStatement( t.assignmentExpression( "=", t.memberExpression( t.identifier(local), t.identifier("__hmrId") ), t.stringLiteral(id) ) ), t.expressionStatement( t.callExpression( t.memberExpression( t.identifier("__VUE_HMR_RUNTIME__"), t.identifier("createRecord") ), [t.stringLiteral(id), t.identifier(local)] ) ) ); callbackCode.push( t.expressionStatement( t.callExpression( t.memberExpression( t.identifier("__VUE_HMR_RUNTIME__"), t.identifier("reload") ), [t.stringLiteral(id), t.identifier(local)] ) ) ); } const programPath = file.path; if (hasDefault) { programPath.traverse({ ExportDefaultDeclaration(path) { if (path.node.declaration.type === "CallExpression" && t.isIdentifier(path.node.declaration.callee) && path.node.declaration.callee.name === "defineComponent") { const defaultDecl = t.variableDeclaration("const", [ t.variableDeclarator( t.identifier("__default__"), path.node.declaration ) ]); path.replaceWith(defaultDecl); programPath.pushContainer("body", [ t.exportDefaultDeclaration(t.identifier("__default__")) ]); } } }); } const hotReloadCode = [ ...code, t.expressionStatement(t.stringLiteral("/* hot reload */")), t.ifStatement( t.memberExpression(t.identifier("module"), t.identifier("hot")), t.blockStatement([ t.expressionStatement( t.callExpression( t.memberExpression( t.memberExpression( t.identifier("module"), t.identifier("hot") ), t.identifier("accept") ), [] ) ), ...callbackCode ]) ) ]; programPath.pushContainer("body", hotReloadCode); } } }; } function parseComponentDecls(node) { const names = []; for (const decl of node.declarations) { if (decl.id.type === "Identifier" && isDefineComponentCall(decl.init)) { names.push(decl.id.name); } } return names; } function isDefineComponentCall(node) { return !!(node && node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "defineComponent"); } function getHash(text) { return (0, import_node_crypto.createHash)("sha256").update(text).digest("hex").substring(0, 8); }