import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import fs from "fs";
import path from "path";
import { glob } from "glob";
function getPages() {
const pages = glob.sync("src/apps/*/main.tsx");
console.log("Found pages:", pages);
return pages;
}
function generateHtml() {
const templatePath = path.resolve("base.html");
if (!fs.existsSync(templatePath)) {
console.error("base.html not found:", templatePath);
return;
}
const template = fs.readFileSync(templatePath, "utf-8");
const pages = getPages();
pages.forEach((entry) => {
const name = path.basename(path.dirname(entry));
const outPath = path.resolve(`${name}.html`);
const html = template
// .replace(/{{TITLE}}/g, name)
.replace(/{{TITLE}}/g, "SHOPINZEN WORDPRESS PLUGIN")
.replace(/{{ENTRY}}/g, ``);
fs.writeFileSync(outPath, html);
// console.log("Generated:", outPath);
});
}
generateHtml();
export function loadWpEnvFile() {
const externalRoot = path.resolve(__dirname, "../");
const externalEnvFile = path.join(externalRoot, ".env");
if (!fs.existsSync(externalEnvFile)) {
throw new Error(`External env file not found at ${externalEnvFile}`);
}
const content = fs.readFileSync(externalEnvFile, "utf-8");
const env: Record = {};
content.split("\n").forEach((line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) return;
const [key, ...rest] = trimmed.split("=");
let value = rest.join("=").trim();
// Remove surrounding single or double quotes
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
const finalKey = key.startsWith("VITE_")
? key.trim()
: `VITE_${key.trim()}`;
env[finalKey] = value;
});
const localEnvFile = path.join(process.cwd(), ".env");
let localContent: string[] = [];
if (fs.existsSync(localEnvFile)) {
localContent = fs.readFileSync(localEnvFile, "utf-8").split("\n");
}
const localEnv: Record = {};
localContent.forEach((line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) return;
const [key, ...rest] = trimmed.split("=");
let value = rest.join("=").trim();
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
localEnv[key] = value;
});
// Merge external over local
const mergedEnv = { ...localEnv, ...env };
// Write back to local .env
const newContent = Object.entries(mergedEnv)
.map(([key, value]) => `${key}=${value}`)
.join("\n");
fs.writeFileSync(localEnvFile, newContent, "utf-8");
return mergedEnv;
}
export default defineConfig(({ mode }) => {
loadWpEnvFile();
const env = { ...loadEnv(mode, process.cwd(), "") };
console.log("mode", mode);
const base =
(env.WP_SITE_BASE_PATH || "") +
(mode == "production"
? "/wp-content/plugins/shopinzen-integrator/frontend/dist"
: "");
console.log('base', base)
return {
resolve: {
alias: {
"@src": path.resolve(__dirname, "src"),
},
},
base,
plugins: [react(), tailwindcss()],
build: {
outDir: "dist",
emptyOutDir: true,
rollupOptions: {
input: Object.fromEntries(
getPages().map((entry) => {
const name = path.basename(path.dirname(entry));
return [name, path.resolve(__dirname, `${name}.html`)];
}),
),
output: {
entryFileNames: "assets/[name].js",
chunkFileNames: "assets/[name].js",
assetFileNames: "assets/[name].[ext]",
manualChunks(id) {
if (id.includes("node_modules")) {
return "vendor";
}
},
},
},
},
};
});