58 lines
2.4 KiB
JavaScript
58 lines
2.4 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)
|