Files
HMDesign/scripts/build-package.mjs
T
shkim4 dc9258599c feat(chart): add @hm-design/chart package with Bar, Line, Donut, Pie, Radar, Area and Radial charts
- new workspace package packages/chart (shadcn/ui Chart + Recharts, HM Theme tokens), examples entry with sample data
- Storybook Chart group (src/chart), consumer example (examples/chart), build:chart / build:chart-example scripts
- README rewritten as the AI usage guide (install, CSS wiring, chart selection, data rules, verification)
- docs: package count/list, Submodule guide (chart workspace, React dedupe), shared AI_GUIDE row, MAINTAINING, story inventory
2026-11-06 15:52:47 +09:00

110 lines
5.8 KiB
JavaScript

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", "chart"].includes(name)) throw new Error("Expected ui, center-graphic or chart")
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") }
: name === "chart"
? { index: path.join(packageRoot, "src/index.ts"), examples: path.join(packageRoot, "src/examples/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" || name === "chart") && 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 if (name === "chart") {
// The chart stylesheet loads with the component entry. Tailwind utilities used by the shadcn Chart markup
// are generated in the consumer app, so ship the same @source entry as @hm-design/ui/tailwind.css.
// Both entries share one chunk, so each loads the stylesheet and keeps the client directive Rolldown drops.
for (const entry of ["index.js", "examples.js"]) {
const file = path.join(output, entry)
await writeFile(file, `"use client";\nimport "./style.css";\n${await readFile(file, "utf8")}`)
}
await cp(path.join(packageRoot, "src/tailwind.css"), path.join(output, "tailwind.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")}`)
}
}