@devup-ui/vite-plugin
Version:
Zero Config, Zero FOUC, Zero Runtime, CSS in JS Preprocessor
132 lines (131 loc) • 3.29 kB
JavaScript
import { existsSync as f, mkdirSync as $, readFileSync as j, writeFileSync as d } from "node:fs";
import { writeFile as _ } from "node:fs/promises";
import { resolve as l, join as h } from "node:path";
import { setDebug as E, getDefaultTheme as v, codeExtract as k, getCss as x, registerTheme as C, getThemeInterface as O } from "@devup-ui/wasm";
import { normalizePath as y } from "vite";
function D(e) {
var i;
if (f(e.interfacePath) || $(e.interfacePath), f(e.devupPath)) {
C(
(i = JSON.parse(j(e.devupPath, "utf-8"))) == null ? void 0 : i.theme
);
const s = O(
e.package,
"DevupThemeColors",
"DevupThemeTypography",
"DevupTheme"
);
s && d(h(e.interfacePath, "theme.d.ts"), s, {
encoding: "utf-8"
});
}
f(e.cssFile) || d(e.cssFile, "", {
encoding: "utf-8"
}), d(h(e.interfacePath, ".gitignore"), "*", {
encoding: "utf-8"
});
}
let c = "";
function z({
package: e = "@devup-ui/react",
devupPath: i = "devup.json",
interfacePath: s = "df",
cssFile: u = l(s, "devup-ui.css"),
extractCss: a = !0,
debug: w = !1,
include: m = []
} = {}) {
E(w);
try {
D({
package: e,
cssFile: u,
devupPath: i,
interfacePath: s
});
} catch (t) {
console.error(t);
}
const g = v();
return g && JSON.stringify(g), {
name: "devup-ui",
config() {
const t = v(), r = {};
t && (r["process.env.DEVUP_UI_DEFAULT_THEME"] = JSON.stringify(t));
const n = {
server: {
watch: {
ignored: [`!${i}`]
}
},
define: r,
optimizeDeps: {
exclude: m
}
};
return a && (n.build = {
rollupOptions: {
output: {
manualChunks(o) {
if (o.startsWith("devup-ui.css"))
return "devup-ui.css";
}
}
}
}), n;
},
apply() {
return !0;
},
watchChange(t) {
if (l(t) === l(i) && f(i))
try {
D({
package: e,
cssFile: u,
devupPath: i,
interfacePath: s
});
} catch (r) {
console.error(r);
}
},
resolveId(t) {
if (y(t) === y(u))
return `devup-ui.css?t=${Date.now().toString() + c.length}`;
},
load(t) {
if (t.split("?")[0] === "devup-ui.css")
return c = x();
},
enforce: "pre",
async transform(t, r) {
if (!a) return;
const n = r.split("?")[0];
if ((m.length ? new RegExp(
`node_modules(?!(${m.map((S) => `.*${S}`).join("|").replaceAll("/", "[\\/\\\\_]")})([\\/\\\\.]|$))`
).test(n) : r.includes("node_modules")) || !/\.(tsx|ts|js|mjs|jsx)$/i.test(n)) return;
const {
code: o,
css: p,
map: T
} = k(n, t, e, u);
return p && c.length < p.length && (c = p, await _(u, `/* ${r} ${Date.now()} */`, {
encoding: "utf-8"
})), {
code: o,
map: T
};
},
async generateBundle(t, r) {
if (!a) return;
const n = Object.keys(r).find(
(o) => o.includes("devup-ui") && o.endsWith(".css")
);
n && "source" in r[n] && (r[n].source = c);
}
};
}
export {
z as DevupUI
};