import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises" import path from "node:path" import { fileURLToPath } from "node:url" import { execFileSync } from "node:child_process" import { existsSync } from "node:fs" import { build } from "vite" const root = fileURLToPath(new URL("../", import.meta.url)) const name = process.argv[2] if (!["ui", "center-graphic"].includes(name)) throw new Error("Expected ui or center-graphic") const packageRoot = path.join(root, "packages", name) const output = path.join(packageRoot, "dist") const manifest = JSON.parse(await readFile(path.join(packageRoot, "package.json"), "utf8")) const externals = Object.keys({ ...manifest.dependencies, ...manifest.peerDependencies }) // Only this package's generated output is removed. Library builds never copy root public assets. await rm(output, { recursive: true, force: true }) await build({ configFile: false, root: packageRoot, publicDir: false, build: { target: "es2022", outDir: output, minify: false, lib: { entry: name === "ui" ? { index: path.join(packageRoot, "src/index.ts"), "theme/index": path.join(packageRoot, "src/theme/index.ts") } : { index: path.join(packageRoot, "src/index.ts"), reference: path.join(packageRoot, "src/reference.ts") }, formats: ["es"], fileName: (_format, entry) => `${entry}.js`, cssFileName: "style", }, rolldownOptions: { external: id => externals.some(dep => id === dep || id.startsWith(`${dep}/`)), onwarn(warning, warn) { // The UI component entrypoint retains the directive below; source directives are redundant. if (name === "ui" && warning.code === "MODULE_LEVEL_DIRECTIVE") return warn(warning) }, }, }, }) execFileSync(process.execPath, [path.join(root, "node_modules/typescript/bin/tsc"), "-p", path.join(packageRoot, "tsconfig.build.json")], { stdio: "inherit" }) // Declarations describe APIs, not stylesheet loading; explicit .js specifiers also support NodeNext. async function finishDeclarations(directory) { for (const entry of await readdir(directory, { withFileTypes: true })) { const file = path.join(directory, entry.name) if (entry.isDirectory()) await finishDeclarations(file) else if (entry.name.endsWith(".d.ts")) { const text = (await readFile(file, "utf8")) .replace(/^import\s+["'][^"']+\.css["'];?\s*$/gm, "") .replace(/(from\s+["']|import\(["'])(\.\.?\/[^"']+)(["'])/g, (_match, before, specifier, after) => { const target = path.resolve(directory, specifier) const resolved = existsSync(`${target}.d.ts`) ? specifier : existsSync(path.join(target, "index.d.ts")) ? `${specifier}/index` : specifier return `${before}${resolved.endsWith(".js") ? resolved : `${resolved}.js`}${after}` }) await writeFile(file, text) } } } await finishDeclarations(output) if (name === "ui") { const entry = path.join(output, "index.js") await writeFile(entry, `"use client";\n${await readFile(entry, "utf8")}`) await mkdir(path.join(output, "theme"), { recursive: true }) for (const entry of await readdir(path.join(packageRoot, "src/theme"))) { if (entry.endsWith(".css")) await cp(path.join(packageRoot, "src/theme", entry), path.join(output, "theme", entry)) } await mkdir(path.join(output, "vendor"), { recursive: true }) // Preserve the exact upstream CSS rather than shipping its CLI dependency graph to consumers. await cp(path.join(root, "node_modules/shadcn/dist/tailwind.css"), path.join(output, "vendor/shadcn-tailwind.css")) await cp(path.join(root, "node_modules/shadcn/LICENSE.md"), path.join(output, "vendor/shadcn-LICENSE.md")) const tailwind = (await readFile(path.join(packageRoot, "src/tailwind.css"), "utf8")) .replace('"shadcn/tailwind.css"', '"./vendor/shadcn-tailwind.css"') // The compiled folder is self-contained. Scanning it also avoids the // Windows file-source path case that drops index.js utility candidates. .replace(/@source "\.\/components";\r?\n@source "\.\/patterns";/, '@source "./";') await writeFile(path.join(output, "tailwind.css"), tailwind) await mkdir(path.join(output, "fonts"), { recursive: true }) for (const file of ["NotoSansKR-variable.ttf", "NotoSansKR-OFL.txt"]) { await cp(path.join(root, "public/fonts", file), path.join(output, "fonts", file)) } const theme = (await readFile(path.join(packageRoot, "src/theme.css"), "utf8")) .replace('/fonts/NotoSansKR-variable.ttf', './fonts/NotoSansKR-variable.ttf') await writeFile(path.join(output, "theme.css"), theme) await cp(path.join(packageRoot, "src/patterns/dashboard-status/dashboard-status.css"), path.join(output, "dashboard-status.css")) } else { // Preserve CENTER's existing automatic component CSS behavior for both public entrypoints. for (const entry of ["index.js", "reference.js"]) { const file = path.join(output, entry) await writeFile(file, `import "./style.css";\n${await readFile(file, "utf8")}`) } }