UNPKG

@chenzoho/ldt-vite-plugin

Version:

ldt-vite-plugin

155 lines (154 loc) 4.14 kB
import { theme as m } from "ant-design-vue"; import { namespace as a } from "@chenzoho/ldt-vue-config"; const i = "_app.theme.css"; async function E(n = { default: "#1677ff" }) { const r = []; let t = !1; const { darkAlgorithm: e, defaultAlgorithm: o, defaultSeed: c } = m; return { name: "vite-dynamic-css-var", async configResolved(s) { t = !s.env.DEV; const l = t ? "" : `\r `; Object.entries(n).forEach(([f, u]) => { c.colorPrimary = u; const g = e(c), v = o(c), d = f == "default" ? ":root" : `.${f}`; r.push(p(d, l, v)), r.push(p(`${d} .dark`, l, g)); }); }, transformIndexHtml(s) { return t ? { html: s, tags: [ { tag: "link", attrs: { rel: "stylesheet", type: "text/css", href: `/${i}` }, injectTo: "head-prepend" } ] } : { html: s, tags: [ { tag: "style", attrs: { type: "text/css", "data-vite-dev-id": "vite-dynamic-css-var" }, injectTo: "head-prepend", children: r.join(`\r `) } ] }; }, async generateBundle() { try { this.emitFile({ type: "asset", fileName: i, source: r.join(`\r `) }), console.log(`[${a}] - theme variables have been injected into ${i}`); } catch (s) { console.log(`[${a}] - an error occurred while theme variables injected `), console.error(`${s instanceof Error ? s.message : String(s)}`); } } }; } function h(n) { let r = n.replace(/[A-Z]/g, function(t) { return "-" + t.toLowerCase(); }); return r.slice(0, 1) === "-" && (r = r.slice(1)), r; } function p(n, r, t) { const e = Object.entries(t).map(([o, c]) => `--${h(o)}: ${c};`).join(r); return `${n} { ${e} }`; } const O = (n = void 0, r = {}) => { const t = { namespace: a, // 将params参数添加到merge对象中 ...r }; return n && (t.hack = `true;@import (reference) '${n}';`), console.log(`[${a}] - less variables have been injected`), { // 允许使用js javascriptEnabled: !0, // 全局less变量 globalVars: { ...t } }; }, y = "VITE_GLOB_"; function b({ namespace: n, config: r, prefix: t = y }) { try { const e = new RegExp(`^(${t})`); Object.keys(r).forEach((s) => { e.test(s) || Reflect.deleteProperty(r, s); }); const o = `window.${n}`, c = `${o}=${JSON.stringify(r)}; Object.freeze(${o}); Object.defineProperty(window, "${n}", { configurable: false, writable: false, });`.replace(/\n\s*/g, ""); return console.log(`[${n}] - env variables have been injected`), c; } catch (e) { return console.error(`[${n}] - an error occurred while env variables injected:`), console.error(`${e instanceof Error ? e.message : String(e)}`), ""; } } function w(n) { const r = {}; for (const t of Object.keys(n)) { let e = n[t].replace(/\\n/g, ` `); if (e = e === "true" ? !0 : e === "false" ? !1 : e, t === "VITE_PORT" && (e = Number(e)), t === "VITE_PROXY" && e) try { e = JSON.parse(e.replace(/'/g, '"')); } catch { e = ""; } r[t] = e, typeof e == "string" ? process.env[t] = e : typeof e == "object" && (process.env[t] = JSON.stringify(e)); } return r; } async function _(n) { const r = []; return { name: "vite-dynamic-env-var", async configResolved(t) { const e = b({ namespace: a, config: n }) ?? ""; r.push(e); }, transformIndexHtml(t) { return { html: t, tags: [ { tag: "script", attrs: { type: "text/javascript", "vite-dev-id": "vite-dynamic-env-var" }, injectTo: "head-prepend", children: r.join(`\r `) } ] }; } }; } export { _ as createAppEnvPlugin, E as createAppThemePlugin, O as createViteLess, w as wrapperEnv }; //# sourceMappingURL=index.mjs.map