Files
hao-ai-lab-FastVideo/docs/js/build-cookbook-assets.mjs
T

48 lines
1.9 KiB
JavaScript

/** Bundle the cookbook API and pinned Ajv validator for the local demo and other browser consumers. */
import { build } from "esbuild";
import { readFile, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const root = dirname(dirname(fileURLToPath(import.meta.url)));
const result = await build({
absWorkingDir: root,
entryPoints: ["js/cookbook-config.mjs"],
outfile: "assets/cookbook-config/cookbook-config.js",
bundle: true,
platform: "browser",
format: "iife",
globalName: "FastVideoConfigCookbook",
target: ["es2020"],
minify: true,
metafile: true,
legalComments: "inline",
banner: { js: "/*! Generated by docs/js/build-cookbook-assets.mjs. Licenses: cookbook-config.LICENSE.txt */" },
footer: { js: 'if (typeof module !== "undefined" && module.exports) module.exports = FastVideoConfigCookbook;' },
});
// Preserve the notices of the packages whose code is actually included in the bundle.
const packages = new Set();
for (const input of Object.keys(result.metafile.inputs)) {
const parts = input.split("node_modules/").at(-1).split("/");
if (input.includes("node_modules/")) {
packages.add(parts[0].startsWith("@") ? parts.slice(0, 2).join("/") : parts[0]);
}
}
const notices = [];
for (const name of [...packages].sort()) {
let license;
for (const filename of ["LICENSE", "LICENSE.md", "LICENSE.txt", "LICENSE-MIT"]) {
try {
license = await readFile(join(root, "node_modules", name, filename), "utf8");
break;
} catch (error) {
if (error.code !== "ENOENT") throw error;
}
}
if (!license) throw new Error(`Missing license for bundled package ${name}`);
notices.push(`${name}\n${"=".repeat(name.length)}\n${license}`);
}
await writeFile(join(root, "assets/cookbook-config/cookbook-config.LICENSE.txt"), notices.join("\n\n"));
console.log("Built assets/cookbook-config/cookbook-config.js and third-party license notices.");