mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-09 03:28:02 +08:00
* fix(diffs): share SSR preloads and repair language-pack hydration Render viewer and file documents from a single @pierre/diffs SSR preload per file (mode=both previously ran the full diff+highlight pipeline twice; 651ms -> 303ms on an 8-file patch), apply the file-mode font bump as a document-level override, and keep hydration payloads variant-faithful. Fix the language-pack runtime downgrading pack-only languages to plain text at hydration by defining a per-target build flag and forwarding it to payload normalization. Also: case-insensitive language hints, identical before/after short-circuit with details.changed, patch input failures classified as tool input errors, canonical config values now win over deprecated aliases, hash-pinned viewer runtime served immutable, truthful browser-vs-render errors, timing-safe artifact token compare, unref idle browser timer. * docs(changelog): link diffs rendering entry to PR * test(diffs): narrow manifest validation results before value access * test(tooling): allowlist diffs viewer-client define suppression
132 lines
3.7 KiB
JavaScript
132 lines
3.7 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
// Builds browser runtime bundles for the diffs viewer assets.
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { build } from "esbuild";
|
|
|
|
const modulePath = fileURLToPath(import.meta.url);
|
|
const repoRoot = path.resolve(path.dirname(modulePath), "..");
|
|
const pierreDiffsEmptySideEffectNamespace = "openclaw-diffs-empty-side-effect";
|
|
const pierreDiffsEmptySideEffectPath = "pierre-diffs-parse-decorations-side-effect";
|
|
|
|
const targets = {
|
|
curated: {
|
|
entry: "extensions/diffs/src/viewer-client.ts",
|
|
output: "extensions/diffs/assets/viewer-runtime.js",
|
|
shikiAlias: "scripts/diffs-shiki-curated.ts",
|
|
languagePackAvailable: false,
|
|
},
|
|
full: {
|
|
entry: "extensions/diffs/src/viewer-client.ts",
|
|
output: "extensions/diffs-language-pack/assets/viewer-runtime.js",
|
|
languagePackAvailable: true,
|
|
},
|
|
};
|
|
|
|
function toPosixPath(value) {
|
|
return String(value ?? "").replaceAll("\\", "/");
|
|
}
|
|
|
|
/**
|
|
* Creates the esbuild plugin that neutralizes Pierre diffs' browser side-effect import.
|
|
*/
|
|
export function createPierreDiffsSideEffectImportPlugin() {
|
|
return {
|
|
name: "openclaw-diffs-pierre-side-effect-imports",
|
|
setup(buildContext) {
|
|
buildContext.onResolve({ filter: /^diff$/ }, (args) => {
|
|
const importer = toPosixPath(args.importer);
|
|
if (!importer.endsWith("/@pierre/diffs/dist/utils/parseDiffDecorations.js")) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
path: pierreDiffsEmptySideEffectPath,
|
|
namespace: pierreDiffsEmptySideEffectNamespace,
|
|
sideEffects: true,
|
|
};
|
|
});
|
|
buildContext.onLoad(
|
|
{
|
|
filter: /^pierre-diffs-parse-decorations-side-effect$/,
|
|
namespace: pierreDiffsEmptySideEffectNamespace,
|
|
},
|
|
() => ({
|
|
contents: "export {};\n",
|
|
loader: "js",
|
|
}),
|
|
);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Builds one configured diffs viewer runtime target.
|
|
*/
|
|
export async function buildDiffsViewerRuntime(targetName) {
|
|
const target = targets[targetName];
|
|
if (!target) {
|
|
throw new Error(
|
|
`Usage: node scripts/build-diffs-viewer-runtime.mjs ${Object.keys(targets).join("|")}`,
|
|
);
|
|
}
|
|
|
|
const outputPath = path.join(repoRoot, target.output);
|
|
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
|
|
|
const result = await build({
|
|
entryPoints: [path.join(repoRoot, target.entry)],
|
|
bundle: true,
|
|
platform: "browser",
|
|
target: "es2020",
|
|
format: "esm",
|
|
minify: true,
|
|
define: {
|
|
__OPENCLAW_DIFFS_LANGUAGE_PACK__: String(target.languagePackAvailable),
|
|
NaN: "Number.NaN",
|
|
},
|
|
legalComments: "none",
|
|
outfile: outputPath,
|
|
write: false,
|
|
plugins: [
|
|
createPierreDiffsSideEffectImportPlugin(),
|
|
...(target.shikiAlias
|
|
? [
|
|
{
|
|
name: "openclaw-diffs-curated-shiki",
|
|
setup(buildContext) {
|
|
buildContext.onResolve({ filter: /^shiki$/ }, () => ({
|
|
path: path.join(repoRoot, target.shikiAlias),
|
|
}));
|
|
},
|
|
},
|
|
]
|
|
: []),
|
|
],
|
|
});
|
|
|
|
const outputFile = result.outputFiles?.[0];
|
|
if (!outputFile) {
|
|
throw new Error(`esbuild did not produce ${target.output}`);
|
|
}
|
|
|
|
const runtime = outputFile.text.replace(/[ \t]+$/gm, "");
|
|
let previousRuntime = null;
|
|
try {
|
|
previousRuntime = await fs.readFile(outputPath, "utf8");
|
|
} catch (error) {
|
|
if (error?.code !== "ENOENT") {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
if (previousRuntime !== runtime) {
|
|
await fs.writeFile(outputPath, runtime);
|
|
}
|
|
}
|
|
|
|
if (process.argv[1] === modulePath) {
|
|
await buildDiffsViewerRuntime(process.argv[2]);
|
|
}
|