Files
HMDesign/packages/sw/build.mjs
shkim4andClaude Opus 5.5 55179b4577 feat(sw): integrated improvement 0.2.0 (popup content, header open, tool buttons)
SW package 0.2.0 integrated improvement (Task A, steps 1-8). All new props are
optional; existing SW Stories are pixel-identical to the previous build (28/28).

- Build reproducibility: dist text normalized to LF, dist/build-info.json
  (commit, dirty, tool versions, no timestamp), packages/sw/.gitattributes
  (eol=lf). LF checkouts give identical dist and tarball.
- SWHeader: documentOpenPlacement ("menu" default | "header": Open right of
  the logo in the existing document-button look, reusing onDocumentOpen and
  documentOpenLabel, no duplicate Open in the dropdown), leading slot,
  hiddenControls sub-items documentNew / documentOpen / layerFreeze /
  layerLock. Open moves into More on measured overflow (with hysteresis).
- showNew / showOpen (SWHeaderDocumentTitle), showFreeze / showLock (layer
  menu, field, property window list).
- SWToolButton (text / pill / document) and SWToolGroup (radio selection),
  reusing the existing toggle, pill and file-name looks.
- SWPopup / SWPopupSection / SWPopupRow on the shared property window frame;
  SWHeaderPropertyPanel gains children, title, autoFocus and optional
  selection / properties / groups. Escape stack for several popups.
- Field dropdowns inside a popup use the shared fixed overlay
  (useSWFixedDropdownPosition), so the scrolling body no longer clips them.
- AI guide, examples E/F, README, Storybook (SW/Header/Options, SW/Tool,
  SW/Popup) and verification record docs/sw/sw-integrated-improvement-2026-10-19.md.
- Regression scripts rescoped for the intended changes (unused-removal 81/81,
  overview 52/54 with the two pre-existing historical assertions).

Known limitation (unresolved): tools / leading content (incl. SWToolGroup)
does not move into More. With a wide tool bar the configured fields shrink
instead; in Example F the document name and layer field reach 0px at 390px.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 14:49:19 +09:00

88 lines
4.3 KiB
JavaScript

// 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")