"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/plugins/vite.ts var vite_exports = {}; __export(vite_exports, { vitePluginVersionMark: () => vitePluginVersionMark }); module.exports = __toCommonJS(vite_exports); var import_promises = require("fs/promises"); var import_node_path = require("path"); // src/plugins/core/index.ts var import_child_process = __toESM(require("child_process"), 1); var execCommand = (command) => { const { exec } = import_child_process.default; return new Promise((resolve2, reject) => { exec(command, (error, stdout) => { if (error) { reject(error); } else { const output = stdout.toString()?.replace("\n", ""); resolve2(output); } }); }); }; var analyticOptions = async (options) => { const { name = process.env["npm_package_name"], version = process.env["npm_package_version"], command = void 0, ifShortSHA = false, ifGitSHA = false, ifMeta = true, ifLog = true, ifGlobal = true, ifExport = false, outputFile } = options; const finalCommand = command ?? (ifShortSHA ? "git rev-parse --short HEAD" : ifGitSHA ? "git rev-parse HEAD" : void 0); const printVersion = finalCommand ? await execCommand(finalCommand) : version; const printName = `${name?.replace(/((?!\w).)/g, "_")?.toLocaleUpperCase?.()}_VERSION`; const printInfo = `${printName}: ${printVersion}`; const fileList = (() => { switch (typeof outputFile) { case "function": { const res = outputFile(printVersion); return Array.isArray(res) ? res : [res]; } case "boolean": return outputFile ? [{ path: ".well-known/version", content: printVersion }] : []; default: return []; } })(); return { ifMeta, ifLog, ifGlobal, ifExport, fileList, printVersion, printName, printInfo }; }; // src/plugins/vite.ts var vitePluginVersionMark = (options = {}) => { let versionMarkConfig; const getVersionMarkConfig = async () => { if (!versionMarkConfig) versionMarkConfig = await analyticOptions(options); return versionMarkConfig; }; let outDir; return { name: "vite-plugin-version-mark", async config() { const { ifGlobal, printName, printVersion } = await getVersionMarkConfig(); if (ifGlobal) { const keyName = `__${printName}__`; return { define: { [keyName]: JSON.stringify(printVersion) } }; } }, async renderChunk(code, chunk) { if (chunk.isEntry) { const { ifExport, printName, printVersion } = await getVersionMarkConfig(); let modifiedCode = code; if (ifExport) modifiedCode += ` export const ${printName} = '${printVersion}';`; return { code: modifiedCode, map: null }; } }, async transformIndexHtml() { const { ifLog, ifMeta, ifGlobal, printName, printVersion, printInfo } = await getVersionMarkConfig(); const els = []; if (ifMeta) { els.push({ tag: "meta", injectTo: "head-prepend", attrs: { name: "application-name", content: printInfo } }); } if (ifLog) { els.push({ tag: "script", injectTo: "body", children: `console.log("${printInfo}")` }); } if (ifGlobal) { els.push({ tag: "script", injectTo: "body", children: `__${printName}__ = "${printVersion}"` }); } return els; }, configResolved(config) { outDir = config.build.outDir; }, async closeBundle() { const { fileList } = await getVersionMarkConfig(); if (!fileList.length) return; await Promise.all(fileList.map(async ({ path, content = "" }) => { try { const dir = (0, import_node_path.dirname)(path); await (0, import_promises.mkdir)((0, import_node_path.resolve)(outDir, dir), { recursive: true }); const outputFilePath = (0, import_node_path.resolve)(outDir, path); await (0, import_promises.writeFile)(outputFilePath, content); this.info(`Generate version file in ${outputFilePath}`); } catch (error) { this.error(`Failed to generate version file at ${path}: ${error.message}`); } })); } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { vitePluginVersionMark }); //# sourceMappingURL=index.cjs.map