@devup-ui/webpack-plugin
Version:
Zero Config, Zero FOUC, Zero Runtime, CSS in JS Preprocessor
136 lines (135 loc) • 4.13 kB
JavaScript
var w = Object.defineProperty;
var D = (i, e, t) => e in i ? w(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var d = (i, e, t) => D(i, typeof e != "symbol" ? e + "" : e, t);
import { readFileSync as u, writeFileSync as p, existsSync as a, mkdirSync as k, stat as P } from "node:fs";
import { createRequire as g } from "node:module";
import { resolve as m, join as c } from "node:path";
import { registerTheme as F, getThemeInterface as y, setDebug as T, importSheet as b, importClassMap as S, getDefaultTheme as j, getCss as x } from "@devup-ui/wasm";
class C {
constructor({
package: e = "@devup-ui/react",
devupPath: t = "devup.json",
interfacePath: o = "df",
cssFile: n = m(o, "devup-ui.css"),
watch: s = !1,
debug: r = !1,
include: h = []
} = {}) {
d(this, "options");
this.options = {
package: e,
cssFile: n,
devupPath: t,
interfacePath: o,
watch: s,
debug: r,
include: h
};
}
writeDataFiles() {
var t;
F(
(t = JSON.parse(u(this.options.devupPath, "utf-8"))) == null ? void 0 : t.theme
);
const e = y(
this.options.package,
"DevupThemeColors",
"DevupThemeTypography",
"DevupTheme"
);
e && p(
c(this.options.interfacePath, "theme.d.ts"),
e,
{
encoding: "utf-8"
}
), this.options.watch && p(this.options.cssFile, `/* ${Date.now()} */`, {
encoding: "utf-8"
});
}
apply(e) {
T(this.options.debug);
const t = a(this.options.devupPath);
a(this.options.interfacePath) || k(this.options.interfacePath), p(c(this.options.interfacePath, ".gitignore"), "*", {
encoding: "utf-8"
});
const o = c(this.options.interfacePath, "sheet.json"), n = c(this.options.interfacePath, "classMap.json");
if (this.options.watch) {
try {
a(o) && b(JSON.parse(u(o, "utf-8"))), a(n) && S(JSON.parse(u(n, "utf-8")));
} catch (r) {
console.error(r);
}
let s = null;
e.hooks.watchRun.tapAsync(
"DevupUIWebpackPlugin",
(r, h) => {
t && P(this.options.devupPath, (l, v) => {
if (l) {
console.error(`Error checking ${this.options.devupPath}:`, l);
return;
}
const f = v.mtimeMs;
s && s !== f && this.writeDataFiles(), s = f;
}), h();
}
);
}
if (t) {
try {
this.writeDataFiles();
} catch (s) {
console.error(s);
}
e.hooks.afterCompile.tap("DevupUIWebpackPlugin", (s) => {
s.fileDependencies.add(m(this.options.devupPath));
});
}
a(this.options.cssFile) || p(this.options.cssFile, "", { encoding: "utf-8" }), e.options.plugins.push(
new e.webpack.DefinePlugin({
"process.env.DEVUP_UI_DEFAULT_THEME": JSON.stringify(j())
})
), this.options.watch || e.hooks.done.tap("DevupUIWebpackPlugin", (s) => {
s.hasErrors() || p(this.options.cssFile, x(), { encoding: "utf-8" });
}), e.options.module.rules.push(
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
exclude: new RegExp(
this.options.include.length ? `node_modules(?!.*(${this.options.include.join("|").replaceAll("/", "[\\/\\\\]")})([\\/\\\\]|$))` : "node_modules"
),
enforce: "pre",
use: [
{
loader: g(import.meta.url).resolve(
"@devup-ui/webpack-plugin/loader"
),
options: {
package: this.options.package,
cssFile: this.options.cssFile,
sheetFile: o,
classMapFile: n,
watch: this.options.watch
}
}
]
},
{
test: this.options.cssFile,
enforce: "pre",
use: [
{
loader: g(import.meta.url).resolve(
"@devup-ui/webpack-plugin/css-loader"
),
options: {
watch: this.options.watch
}
}
]
}
);
}
}
export {
C as DevupUIWebpackPlugin
};