Skip to content

Commit a127183

Browse files
committed
fix(scripts): track import meta url package deps
1 parent 22f696d commit a127183

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

scripts/lib/package-dist-imports.mjs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function stripSpecifierSuffix(value) {
1111
return value.replace(/[?#].*$/u, "");
1212
}
1313

14+
function hasJavaScriptFileExtension(value) {
15+
return /\.(?:cjs|js|mjs)$/u.test(path.posix.basename(stripSpecifierSuffix(value)));
16+
}
17+
1418
function resolveDistImportPath(importerPath, specifier) {
1519
if (!specifier.startsWith(".")) {
1620
return null;
@@ -46,6 +50,15 @@ function isImportSpecifierContext(source, index) {
4650
);
4751
}
4852

53+
function isImportMetaUrlContext(source, quoteStart, quoteEnd) {
54+
const prefix = source.slice(Math.max(0, quoteStart - 32), quoteStart);
55+
if (!/\bnew\s+URL\s*\(\s*$/u.test(prefix)) {
56+
return false;
57+
}
58+
const suffix = source.slice(quoteEnd + 1, quoteEnd + 96);
59+
return /^\s*,\s*import\.meta\.url\s*,?\s*\)/u.test(suffix);
60+
}
61+
4962
function collectImportSpecifiers(source) {
5063
const specifiers = [];
5164
let inBlockComment = false;
@@ -100,7 +113,10 @@ function collectImportSpecifiers(source) {
100113
}
101114

102115
if (value.startsWith(".")) {
103-
if (isImportSpecifierContext(source, index)) {
116+
const isDistDependency =
117+
isImportSpecifierContext(source, index) ||
118+
(isImportMetaUrlContext(source, index, cursor) && hasJavaScriptFileExtension(value));
119+
if (isDistDependency) {
104120
specifiers.push(value);
105121
}
106122
}

test/scripts/check-openclaw-package-tarball.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,90 @@ describe("check-openclaw-package-tarball", () => {
245245
);
246246
});
247247

248+
it("rejects dist files with missing import.meta.url URL dependencies", () => {
249+
withTarball(
250+
["dist/index.js"],
251+
{ "dist/index.js": 'const worker = new URL("./worker.js", import.meta.url);\n' },
252+
(tarball) => {
253+
const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" });
254+
255+
expect(result.status).not.toBe(0);
256+
expect(result.stderr).toContain("dist/index.js imports missing dist/worker.js");
257+
},
258+
"2026.4.27",
259+
);
260+
});
261+
262+
it("rejects formatted import.meta.url URL dependencies", () => {
263+
withTarball(
264+
["dist/index.js"],
265+
{
266+
"dist/index.js": [
267+
"const worker = new URL(",
268+
' "./worker.js",',
269+
" import.meta.url,",
270+
");",
271+
"",
272+
].join("\n"),
273+
},
274+
(tarball) => {
275+
const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" });
276+
277+
expect(result.status).not.toBe(0);
278+
expect(result.stderr).toContain("dist/index.js imports missing dist/worker.js");
279+
},
280+
"2026.4.27",
281+
);
282+
});
283+
284+
it("rejects import.meta.url URL dependencies omitted from the postinstall inventory", () => {
285+
withTarball(
286+
["dist/index.js"],
287+
{
288+
"dist/index.js": 'const worker = new URL("./worker.js", import.meta.url);\n',
289+
"dist/worker.js": "export {};\n",
290+
},
291+
(tarball) => {
292+
const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" });
293+
294+
expect(result.status).not.toBe(0);
295+
expect(result.stderr).toContain("inventory omits imported dist file dist/worker.js");
296+
},
297+
"2026.4.27",
298+
);
299+
});
300+
301+
it("allows import.meta.url package-root probes", () => {
302+
withTarball(
303+
["dist/index.js"],
304+
{ "dist/index.js": 'const root = new URL("../..", import.meta.url);\n' },
305+
(tarball) => {
306+
const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" });
307+
308+
expect(result.status, result.stderr).toBe(0);
309+
expect(result.stdout).toContain("OpenClaw package tarball integrity passed.");
310+
},
311+
"2026.4.27",
312+
);
313+
});
314+
315+
it("allows import.meta.url source helper probes", () => {
316+
withTarball(
317+
["dist/index.js"],
318+
{
319+
"dist/index.js":
320+
'const shim = new URL("./capability-runtime-vitest-shims/config-runtime.ts", import.meta.url);\n',
321+
},
322+
(tarball) => {
323+
const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" });
324+
325+
expect(result.status, result.stderr).toBe(0);
326+
expect(result.stdout).toContain("OpenClaw package tarball integrity passed.");
327+
},
328+
"2026.4.27",
329+
);
330+
});
331+
248332
it("rejects missing Control UI assets", () => {
249333
withTarball(
250334
["dist/index.js"],

0 commit comments

Comments
 (0)