49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Build script: copies entire site/ to dist/, then inlines critical CSS in dist/index.html.
|
|
* Uses Critters (no headless browser) so the build runs in any environment.
|
|
* Dockerfile copies only dist/ — single source of truth for the built site.
|
|
*/
|
|
|
|
import Critters from "critters";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const ROOT = path.join(__dirname);
|
|
const SITE = path.join(ROOT, "site");
|
|
const DIST = path.join(ROOT, "dist");
|
|
|
|
async function main() {
|
|
// Copy entire site structure to dist
|
|
if (fs.existsSync(DIST)) {
|
|
fs.rmSync(DIST, { recursive: true });
|
|
}
|
|
fs.cpSync(SITE, DIST, { recursive: true });
|
|
console.log("✓ Copied site/ → dist/");
|
|
|
|
// Inline critical CSS in dist/index.html (Critters reads/writes relative to path)
|
|
const indexPath = path.join(DIST, "index.html");
|
|
let html = fs.readFileSync(indexPath, "utf8");
|
|
|
|
const critters = new Critters({
|
|
path: DIST,
|
|
preload: "media",
|
|
noscriptFallback: true,
|
|
pruneSource: false,
|
|
logLevel: "warn",
|
|
});
|
|
|
|
html = await critters.process(html);
|
|
fs.writeFileSync(indexPath, html, "utf8");
|
|
console.log("✓ Critical CSS inlined → dist/index.html");
|
|
|
|
console.log("Build complete. Output: dist/");
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|