Spaces:
Sleeping
Sleeping
Download web/build.mjs from brianbellx/codehygiene: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/build.mjs
- Command line
-
hf download hf://spaces/brianbellx/codehygiene/web/build.mjs
-
curl -L -o build.mjs https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/build.mjs
4.24 kB
| import { createHash } from "node:crypto"; | |
| import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises"; | |
| import { existsSync } from "node:fs"; | |
| import path from "node:path"; | |
| import { fileURLToPath } from "node:url"; | |
| import * as esbuild from "esbuild"; | |
| const webDir = path.dirname(fileURLToPath(import.meta.url)); | |
| const srcDir = path.join(webDir, "src"); | |
| const distDir = path.join(webDir, "dist"); | |
| const vendorDir = path.join(webDir, "vendor"); | |
| const repoRoot = [path.resolve(webDir, "../.."), path.resolve(webDir, "../../..")] | |
| .find((dir) => existsSync(path.join(dir, "compile.py"))) ?? path.resolve(webDir, "../../.."); | |
| const SHARED_FILES = ["engine.js", "presets.json", "weights.npz"]; | |
| const watch = process.argv.includes("--watch"); | |
| function sharedPaths() { | |
| if (SHARED_FILES.every((name) => existsSync(path.join(vendorDir, name)))) { | |
| return { | |
| engine: path.join(vendorDir, "engine.js"), | |
| presets: path.join(vendorDir, "presets.json"), | |
| weights: path.join(vendorDir, "weights.npz"), | |
| }; | |
| } | |
| return { | |
| engine: path.join(repoRoot, "codehygiene", "engine.js"), | |
| presets: path.resolve(webDir, "../presets.json"), | |
| weights: path.join(repoRoot, "codehygiene", "weights.npz"), | |
| }; | |
| } | |
| function sharedFilesPlugin(shared) { | |
| const targets = { | |
| "shared:engine": shared.engine, | |
| "shared:presets": shared.presets, | |
| }; | |
| return { | |
| name: "shared-files", | |
| setup(build) { | |
| build.onResolve({ filter: /^shared:/ }, (args) => { | |
| const target = targets[args.path]; | |
| return target ? { path: target } : { errors: [{ text: `Unknown shared file ${args.path}` }] }; | |
| }); | |
| }, | |
| }; | |
| } | |
| const PAGES = { "board.html": "leaderboard", "privacy.html": "privacy" }; | |
| const HASHED_ASSETS = [ | |
| "app.js", | |
| "board.js", | |
| "score-worker.js", | |
| "styles.css", | |
| "board.css", | |
| "privacy.css", | |
| "weights.npz", | |
| ]; | |
| const ASSET_TAG_RE = /(<(?:link|script)\b[^>]*?\b(?:href|src)=")((?:\.\.\/)?[a-z0-9-]+\.(?:css|js))(")/gi; | |
| async function computeAssetVersion() { | |
| const hash = createHash("sha256"); | |
| for (const name of HASHED_ASSETS) { | |
| const filePath = path.join(distDir, name); | |
| if (existsSync(filePath)) { | |
| hash.update(name); | |
| hash.update(await readFile(filePath)); | |
| } | |
| } | |
| return hash.digest("hex").slice(0, 10); | |
| } | |
| async function writeVersionedHtml(srcHtmlPath, destHtmlPath, version) { | |
| const rawHtml = await readFile(srcHtmlPath, "utf8"); | |
| const versioned = rawHtml.replace(ASSET_TAG_RE, `$1$2?v=${version}$3`); | |
| await writeFile(destHtmlPath, versioned, "utf8"); | |
| } | |
| async function copyStatic(shared) { | |
| await copyFile(shared.weights, path.join(distDir, "weights.npz")); | |
| const version = await computeAssetVersion(); | |
| await writeVersionedHtml(path.join(srcDir, "index.html"), path.join(distDir, "index.html"), version); | |
| for (const [page, folder] of Object.entries(PAGES)) { | |
| await mkdir(path.join(distDir, folder), { recursive: true }); | |
| await writeVersionedHtml(path.join(srcDir, page), path.join(distDir, folder, "index.html"), version); | |
| } | |
| } | |
| const copyStaticPlugin = (shared) => ({ | |
| name: "copy-static", | |
| setup(build) { | |
| build.onEnd(async (result) => { | |
| if (result.errors.length === 0) { | |
| await copyStatic(shared); | |
| } | |
| }); | |
| }, | |
| }); | |
| async function main() { | |
| const shared = sharedPaths(); | |
| await rm(distDir, { recursive: true, force: true }); | |
| await mkdir(distDir, { recursive: true }); | |
| const context = await esbuild.context({ | |
| entryPoints: { | |
| app: path.join(srcDir, "main.js"), | |
| board: path.join(srcDir, "board.js"), | |
| "score-worker": path.join(srcDir, "score-worker.js"), | |
| styles: path.join(srcDir, "styles.css"), | |
| privacy: path.join(srcDir, "privacy.css"), | |
| }, | |
| outdir: distDir, | |
| entryNames: "[name]", | |
| assetNames: "fonts/[name]-[hash]", | |
| bundle: true, | |
| format: "esm", | |
| target: ["es2022", "chrome123", "safari17.5", "firefox120"], | |
| minify: !watch, | |
| sourcemap: watch, | |
| legalComments: "none", | |
| loader: { ".woff2": "file" }, | |
| logLevel: "info", | |
| plugins: [sharedFilesPlugin(shared), copyStaticPlugin(shared)], | |
| }); | |
| if (watch) { | |
| await context.watch(); | |
| return; | |
| } | |
| await context.rebuild(); | |
| await context.dispose(); | |
| } | |
| await main(); | |