@devup-ui/webpack-plugin
Version:
Zero Config, Zero FOUC, Zero Runtime, CSS in JS Preprocessor
136 lines (135 loc) • 4.42 kB
JavaScript
import { existsSync as t, readFileSync as a, writeFileSync as r, mkdirSync as h } from "node:fs";
import { stat as M, writeFile as k } from "node:fs/promises";
import { createRequire as c } from "node:module";
import { resolve as u, join as o } from "node:path";
import { registerTheme as n, getThemeInterface as x, getCss as f, setDebug as y, setPrefix as j, importSheet as d, importClassMap as m, importFileMap as F, getDefaultTheme as S } from "@devup-ui/wasm";
class I {
options;
sheetFile;
classMapFile;
fileMapFile;
constructor({
package: i = "@devup-ui/react",
devupFile: e = "devup.json",
distDir: s = "df",
cssDir: l = u(s, "devup-ui"),
watch: p = !1,
debug: D = !1,
include: g = [],
singleCss: v = !1,
prefix: w
} = {}) {
this.options = {
package: i,
cssDir: l,
devupFile: e,
distDir: s,
watch: p,
debug: D,
include: g,
singleCss: v,
prefix: w
}, this.sheetFile = o(this.options.distDir, "sheet.json"), this.classMapFile = o(this.options.distDir, "classMap.json"), this.fileMapFile = o(this.options.distDir, "fileMap.json");
}
writeDataFiles() {
try {
const i = t(this.options.devupFile) ? a(this.options.devupFile, "utf-8") : void 0;
if (i) {
n(JSON.parse(i)?.theme ?? {});
const e = x(
this.options.package,
"CustomColors",
"DevupThemeTypography",
"DevupTheme"
);
e && r(
o(this.options.distDir, "theme.d.ts"),
e,
{
encoding: "utf-8"
}
);
} else
n({});
} catch (i) {
console.error(i), n({});
}
t(this.options.cssDir) || h(this.options.cssDir, { recursive: !0 }), this.options.watch && r(
o(this.options.cssDir, "devup-ui.css"),
f(null, !1)
);
}
apply(i) {
y(this.options.debug), this.options.prefix && j(this.options.prefix);
const e = t(this.options.devupFile);
if (t(this.options.distDir) || h(this.options.distDir, { recursive: !0 }), r(o(this.options.distDir, ".gitignore"), "*", "utf-8"), this.options.watch)
try {
t(this.sheetFile) && d(JSON.parse(a(this.sheetFile, "utf-8"))), t(this.classMapFile) && m(JSON.parse(a(this.classMapFile, "utf-8"))), t(this.fileMapFile) && F(JSON.parse(a(this.fileMapFile, "utf-8")));
} catch (s) {
console.error(s), d({}), m({}), F({});
}
if (this.writeDataFiles(), this.options.watch) {
let s = null;
i.hooks.watchRun.tapPromise("DevupUIWebpackPlugin", async () => {
if (e) {
const p = (await M(this.options.devupFile)).mtimeMs;
s && s !== p && this.writeDataFiles(), s = p;
}
});
}
e && i.hooks.afterCompile.tap("DevupUIWebpackPlugin", (s) => {
s.fileDependencies.add(u(this.options.devupFile));
}), i.options.plugins.push(
new i.webpack.DefinePlugin({
"process.env.DEVUP_UI_DEFAULT_THEME": JSON.stringify(S())
})
), this.options.watch || i.hooks.done.tapPromise("DevupUIWebpackPlugin", async (s) => {
s.hasErrors() || await k(
o(this.options.cssDir, "devup-ui.css"),
f(null, !1),
"utf-8"
);
}), i.options.module.rules.push(
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
exclude: new RegExp(
`(node_modules(?!.*(${["@devup-ui", ...this.options.include].join("|").replaceAll("/", "[\\/\\\\_]")})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`
),
enforce: "pre",
use: [
{
loader: c(import.meta.url).resolve(
"@devup-ui/webpack-plugin/loader"
),
options: {
package: this.options.package,
cssDir: this.options.cssDir,
sheetFile: this.sheetFile,
classMapFile: this.classMapFile,
fileMapFile: this.fileMapFile,
watch: this.options.watch,
singleCss: this.options.singleCss
}
}
]
},
{
test: this.options.cssDir,
enforce: "pre",
use: [
{
loader: c(import.meta.url).resolve(
"@devup-ui/webpack-plugin/css-loader"
),
options: {
watch: this.options.watch
}
}
]
}
);
}
}
export {
I as DevupUIWebpackPlugin
};