// Self-contained library build for @hm-design/sw (no shared script, no @hm-design/ui). import { readFile, readdir, rm, writeFile } from "node:fs/promises" import { existsSync } from "node:fs" import path from "node:path" import { fileURLToPath } from "node:url" import { execFileSync } from "node:child_process" import { build } from "vite" const packageRoot = fileURLToPath(new URL("./", import.meta.url)) 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 }) await rm(output, { recursive: true, force: true }) await build({ configFile: false, root: packageRoot, publicDir: false, build: { target: "es2022", outDir: output, minify: false, // Figma SVG assets are inlined as data URIs: the package needs no asset path resolution. assetsInlineLimit: Number.MAX_SAFE_INTEGER, lib: { entry: { index: path.join(packageRoot, "src/index.ts") }, formats: ["es"], fileName: (_format, entry) => `${entry}.js`, cssFileName: "styles", }, rolldownOptions: { external: id => externals.some(dep => id === dep || id.startsWith(`${dep}/`)), }, }, }) execFileSync(process.execPath, [path.join(packageRoot, "../../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) // Reproducibility: the same source and the same locked tools give byte-identical dist on any path or // checkout. Sources are copied as written (styles.css keeps sw.css bytes), so CRLF checkouts // (core.autocrlf=true) would change the output; every emitted text file is normalized to LF. async function normalizeLineEndings(directory) { for (const entry of await readdir(directory, { withFileTypes: true })) { const file = path.join(directory, entry.name) if (entry.isDirectory()) await normalizeLineEndings(file) else if (/\.(js|css|d\.ts|json)$/.test(entry.name)) { const text = await readFile(file, "utf8") if (text.includes("\r")) await writeFile(file, text.replace(/\r\n?/g, "\n")) } } } await normalizeLineEndings(output) // Provenance without timestamps: source commit (SW_BUILD_COMMIT overrides; "unknown" outside git), // whether packages/sw had uncommitted changes, and the locked build tool versions. const git = args => { try { return execFileSync("git", args, { cwd: packageRoot, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() } catch { return null } } const toolVersion = name => { try { return JSON.parse(execFileSync(process.execPath, ["-p", `JSON.stringify(require("${name}/package.json").version)`], { cwd: packageRoot, encoding: "utf8" })) } catch { return null } } const commit = process.env.SW_BUILD_COMMIT ?? git(["rev-parse", "HEAD"]) ?? "unknown" // With SW_BUILD_COMMIT the caller vouches for the source; dirty is then unknown (null), never a guess. const status = process.env.SW_BUILD_COMMIT ? null : git(["status", "--porcelain", "--", "."]) await writeFile(path.join(output, "build-info.json"), JSON.stringify({ name: manifest.name, version: manifest.version, commit, dirty: status === null ? null : status.length > 0, tools: { vite: toolVersion("vite"), rolldown: toolVersion("rolldown"), typescript: toolVersion("typescript") }, }, null, 2) + "\n")