Initial Commit
This commit is contained in:
93
scripts/build.js
Normal file
93
scripts/build.js
Normal file
@@ -0,0 +1,93 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname, "..");
|
||||
const DIST_DIR = path.join(ROOT_DIR, "dist");
|
||||
const BASE_CSS_DIR = path.join(ROOT_DIR, "css", "base");
|
||||
const THEMES_DIR = path.join(ROOT_DIR, "css", "themes");
|
||||
const JS_DIR = path.join(ROOT_DIR, "js");
|
||||
|
||||
const BASE_CSS_FILES = ["base.css", "alert.css", "buttons.css"];
|
||||
const JS_FILES = ["constants.js", "dom.js", "y2k-alert.js", "index.js"];
|
||||
|
||||
function ensureDir(dirPath) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
|
||||
function readFiles(folder, files) {
|
||||
return files
|
||||
.map((file) => fs.readFileSync(path.join(folder, file), "utf8"))
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
function minifyCss(css) {
|
||||
return css
|
||||
.replace(/\/\*[\s\S]*?\*\//g, "")
|
||||
.replace(/\s+/g, " ")
|
||||
.replace(/\s*([{}:;,])\s*/g, "$1")
|
||||
.replace(/;}/g, "}")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function minifyJs(js) {
|
||||
return js
|
||||
.replace(/\/\*[\s\S]*?\*\//g, "")
|
||||
.replace(/^\s*\/\/.*$/gm, "")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function buildCss() {
|
||||
const baseCss = readFiles(BASE_CSS_DIR, BASE_CSS_FILES);
|
||||
const themeFolders = fs
|
||||
.readdirSync(THEMES_DIR, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => entry.name)
|
||||
.sort();
|
||||
|
||||
themeFolders.forEach((themeName) => {
|
||||
const themePath = path.join(THEMES_DIR, themeName);
|
||||
const themeFiles = fs
|
||||
.readdirSync(themePath)
|
||||
.filter((file) => file.endsWith(".css"))
|
||||
.sort();
|
||||
|
||||
const themeCss = readFiles(themePath, themeFiles);
|
||||
const output = `${baseCss}\n\n${themeCss}`;
|
||||
|
||||
const distFile = path.join(DIST_DIR, `y2k-alerts-${themeName}.css`);
|
||||
const distMinFile = path.join(DIST_DIR, `y2k-alerts-${themeName}.min.css`);
|
||||
|
||||
fs.writeFileSync(distFile, output, "utf8");
|
||||
fs.writeFileSync(distMinFile, minifyCss(output), "utf8");
|
||||
});
|
||||
|
||||
const genericFile = path.join(DIST_DIR, "y2k-alerts-generic.css");
|
||||
const genericMinFile = path.join(DIST_DIR, "y2k-alerts-generic.min.css");
|
||||
|
||||
if (fs.existsSync(genericFile)) {
|
||||
fs.copyFileSync(genericFile, path.join(DIST_DIR, "y2k-alerts.css"));
|
||||
}
|
||||
|
||||
if (fs.existsSync(genericMinFile)) {
|
||||
fs.copyFileSync(genericMinFile, path.join(DIST_DIR, "y2k-alerts.min.css"));
|
||||
}
|
||||
}
|
||||
|
||||
function buildJs() {
|
||||
const jsBundle = readFiles(JS_DIR, JS_FILES);
|
||||
const distFile = path.join(DIST_DIR, "y2k-alerts.js");
|
||||
const distMinFile = path.join(DIST_DIR, "y2k-alerts.min.js");
|
||||
|
||||
fs.writeFileSync(distFile, jsBundle, "utf8");
|
||||
fs.writeFileSync(distMinFile, minifyJs(jsBundle), "utf8");
|
||||
}
|
||||
|
||||
function build() {
|
||||
ensureDir(DIST_DIR);
|
||||
buildCss();
|
||||
buildJs();
|
||||
console.log("Built y2k-alerts assets in dist/");
|
||||
}
|
||||
|
||||
build();
|
||||
Reference in New Issue
Block a user