Skip to content

Commit 309fdd9

Browse files
committed
fix(scripts): silence diffs viewer side-effect warning
1 parent 1188aa3 commit 309fdd9

3 files changed

Lines changed: 99 additions & 11 deletions

File tree

scripts/build-diffs-viewer-runtime.mjs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { build } from "esbuild";
77

88
const modulePath = fileURLToPath(import.meta.url);
99
const repoRoot = path.resolve(path.dirname(modulePath), "..");
10+
const pierreDiffsEmptySideEffectNamespace = "openclaw-diffs-empty-side-effect";
11+
const pierreDiffsEmptySideEffectPath = "pierre-diffs-parse-decorations-side-effect";
1012

1113
const targets = {
1214
curated: {
@@ -20,6 +22,39 @@ const targets = {
2022
},
2123
};
2224

25+
function toPosixPath(value) {
26+
return String(value ?? "").replaceAll("\\", "/");
27+
}
28+
29+
export function createPierreDiffsSideEffectImportPlugin() {
30+
return {
31+
name: "openclaw-diffs-pierre-side-effect-imports",
32+
setup(buildContext) {
33+
buildContext.onResolve({ filter: /^diff$/ }, (args) => {
34+
const importer = toPosixPath(args.importer);
35+
if (!importer.endsWith("/@pierre/diffs/dist/utils/parseDiffDecorations.js")) {
36+
return undefined;
37+
}
38+
return {
39+
path: pierreDiffsEmptySideEffectPath,
40+
namespace: pierreDiffsEmptySideEffectNamespace,
41+
sideEffects: true,
42+
};
43+
});
44+
buildContext.onLoad(
45+
{
46+
filter: /^pierre-diffs-parse-decorations-side-effect$/,
47+
namespace: pierreDiffsEmptySideEffectNamespace,
48+
},
49+
() => ({
50+
contents: "export {};\n",
51+
loader: "js",
52+
}),
53+
);
54+
},
55+
};
56+
}
57+
2358
export async function buildDiffsViewerRuntime(targetName) {
2459
const target = targets[targetName];
2560
if (!target) {
@@ -44,18 +79,21 @@ export async function buildDiffsViewerRuntime(targetName) {
4479
legalComments: "none",
4580
outfile: outputPath,
4681
write: false,
47-
plugins: target.shikiAlias
48-
? [
49-
{
50-
name: "openclaw-diffs-curated-shiki",
51-
setup(buildContext) {
52-
buildContext.onResolve({ filter: /^shiki$/ }, () => ({
53-
path: path.join(repoRoot, target.shikiAlias),
54-
}));
82+
plugins: [
83+
createPierreDiffsSideEffectImportPlugin(),
84+
...(target.shikiAlias
85+
? [
86+
{
87+
name: "openclaw-diffs-curated-shiki",
88+
setup(buildContext) {
89+
buildContext.onResolve({ filter: /^shiki$/ }, () => ({
90+
path: path.join(repoRoot, target.shikiAlias),
91+
}));
92+
},
5593
},
56-
},
57-
]
58-
: [],
94+
]
95+
: []),
96+
],
5997
});
6098

6199
const outputFile = result.outputFiles?.[0];

scripts/test-projects.test-support.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ const TOOLING_SOURCE_TEST_TARGETS = new Map([
423423
["scripts/test-projects.test-support.mjs", ["test/scripts/test-projects.test.ts"]],
424424
["scripts/bundled-plugin-assets.mjs", ["test/scripts/bundled-plugin-assets.test.ts"]],
425425
["scripts/bundle-a2ui.mjs", ["test/scripts/bundled-plugin-assets.test.ts"]],
426+
["scripts/build-diffs-viewer-runtime.mjs", ["test/scripts/build-diffs-viewer-runtime.test.ts"]],
426427
["extensions/canvas/scripts/bundle-a2ui.mjs", ["extensions/canvas/scripts/bundle-a2ui.test.ts"]],
427428
["extensions/canvas/scripts/copy-a2ui.mjs", ["extensions/canvas/scripts/copy-a2ui.test.ts"]],
428429
]);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from "vitest";
2+
import { createPierreDiffsSideEffectImportPlugin } from "../../scripts/build-diffs-viewer-runtime.mjs";
3+
4+
type ResolveCallback = (args: { importer: string; path: string }) => unknown;
5+
type LoadCallback = () => unknown;
6+
7+
function collectPluginCallbacks() {
8+
const resolveCallbacks: ResolveCallback[] = [];
9+
const loadCallbacks: LoadCallback[] = [];
10+
const plugin = createPierreDiffsSideEffectImportPlugin();
11+
plugin.setup({
12+
onResolve(_options: unknown, callback: ResolveCallback) {
13+
resolveCallbacks.push(callback);
14+
},
15+
onLoad(_options: unknown, callback: LoadCallback) {
16+
loadCallbacks.push(callback);
17+
},
18+
});
19+
return { loadCallbacks, resolveCallbacks };
20+
}
21+
22+
describe("build diffs viewer runtime", () => {
23+
it("replaces Pierre Diffs' empty side-effect import without touching real diff imports", () => {
24+
const { loadCallbacks, resolveCallbacks } = collectPluginCallbacks();
25+
expect(resolveCallbacks).toHaveLength(1);
26+
expect(loadCallbacks).toHaveLength(1);
27+
28+
expect(
29+
resolveCallbacks[0]({
30+
path: "diff",
31+
importer: "/repo/node_modules/@pierre/diffs/dist/utils/parseDiffDecorations.js",
32+
}),
33+
).toEqual({
34+
path: "pierre-diffs-parse-decorations-side-effect",
35+
namespace: "openclaw-diffs-empty-side-effect",
36+
sideEffects: true,
37+
});
38+
expect(
39+
resolveCallbacks[0]({
40+
path: "diff",
41+
importer: "/repo/node_modules/@pierre/diffs/dist/utils/renderDiffWithHighlighter.js",
42+
}),
43+
).toBeUndefined();
44+
expect(loadCallbacks[0]()).toEqual({
45+
contents: "export {};\n",
46+
loader: "js",
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)