Files
wysite/scripts/generate_site_sourcemaps.mjs

134 lines
3.2 KiB
JavaScript

#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
const ROOT = "/Users/yuanjiantsui/dev/05-work/site";
const OUTPUT_ROOT = path.join(ROOT, "output", "sourcemaps");
const JOBS = [
{
name: "hc-pos-js",
roots: [path.join(ROOT, "hc-pos.sqygj.cn", "static", "js")],
exts: new Set([".js"])
},
{
name: "hc-pos-css",
roots: [path.join(ROOT, "hc-pos.sqygj.cn", "static", "css")],
exts: new Set([".css"])
},
{
name: "hc-etms-js",
roots: [path.join(ROOT, "hc-etms.sqygj.cn", "static", "js")],
exts: new Set([".js"])
},
{
name: "hc-etms-css",
roots: [path.join(ROOT, "hc-etms.sqygj.cn", "static", "css")],
exts: new Set([".css"])
},
{
name: "runtime-js",
roots: [
path.join(ROOT, "__mirror", "runtime", "hc-pos-dashboard", "runtime-bootstrap.js"),
path.join(ROOT, "__mirror", "runtime", "hc-etms-dashboard", "runtime-bootstrap.js")
],
exts: new Set([".js"])
}
];
function walkFiles(targetPath, exts, bucket) {
const stat = fs.statSync(targetPath);
if (stat.isFile()) {
if (exts.has(path.extname(targetPath))) {
bucket.push(targetPath);
}
return;
}
for (const entry of fs.readdirSync(targetPath, { withFileTypes: true })) {
const entryPath = path.join(targetPath, entry.name);
if (entry.isDirectory()) {
walkFiles(entryPath, exts, bucket);
continue;
}
if (entry.isFile() && exts.has(path.extname(entry.name))) {
bucket.push(entryPath);
}
}
}
function collectFiles(roots, exts) {
const files = [];
for (const root of roots) {
if (!fs.existsSync(root)) {
continue;
}
walkFiles(root, exts, files);
}
return files.sort();
}
function ensureDir(dir) {
fs.mkdirSync(dir, { recursive: true });
}
function buildFile(inputPath) {
const relativePath = path.relative(ROOT, inputPath);
const outputPath = path.join(OUTPUT_ROOT, relativePath);
ensureDir(path.dirname(outputPath));
const args = [
"--yes",
"esbuild",
inputPath,
`--outfile=${outputPath}`,
"--sourcemap=external",
"--sources-content=true",
"--charset=utf8",
"--log-level=error"
];
const result = spawnSync("npx", args, {
cwd: ROOT,
encoding: "utf8"
});
if (result.status !== 0) {
throw new Error(`esbuild 失败: ${relativePath}\n${result.stderr || result.stdout}`);
}
return {
input: relativePath,
output: path.relative(ROOT, outputPath),
map: path.relative(ROOT, `${outputPath}.map`)
};
}
function main() {
ensureDir(OUTPUT_ROOT);
const manifest = {
generated_at: new Date().toISOString(),
root: ROOT,
output_root: path.relative(ROOT, OUTPUT_ROOT),
jobs: []
};
for (const job of JOBS) {
const files = collectFiles(job.roots, job.exts);
const outputs = files.map((file) => buildFile(file));
manifest.jobs.push({
name: job.name,
count: outputs.length,
outputs
});
console.log(`${job.name}: ${outputs.length} 个文件`);
}
const manifestPath = path.join(OUTPUT_ROOT, "manifest.json");
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(`manifest: ${manifestPath}`);
}
main();