diff --git a/packages/cli/src/utils/publishProject.absolutePath.test.ts b/packages/cli/src/utils/publishProject.absolutePath.test.ts
new file mode 100644
index 0000000000..04b286c032
--- /dev/null
+++ b/packages/cli/src/utils/publishProject.absolutePath.test.ts
@@ -0,0 +1,63 @@
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import { stagePublishFiles } from "./publishProject.js";
+
+const dirs: string[] = [];
+const temp = () => {
+ const dir = mkdtempSync(join(tmpdir(), "hf-abs-"));
+ dirs.push(dir);
+ return dir;
+};
+afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })));
+
+/** A project whose index.html is `html(external, project)`, beside a folder holding song.mp3. */
+function stage(html: (external: string, project: string) => string) {
+ const project = temp();
+ const external = temp();
+ writeFileSync(join(external, "song.mp3"), "audio");
+ mkdirSync(join(project, "media"));
+ writeFileSync(join(project, "media", "inside.mp3"), "audio");
+ writeFileSync(join(project, "index.html"), html(external, project));
+ const files = stagePublishFiles(project, Infinity);
+ const index = files.get("index.html")!.toString("utf-8");
+ return { files, index, external };
+}
+
+describe("a media file referenced by its filesystem path", () => {
+ it("outside the project is archived under _ext and the reference points at it", () => {
+ const { files, index, external } = stage(
+ (ext) => ``,
+ );
+ expect([...files.keys()].filter((key) => key.startsWith("_ext/"))).toHaveLength(1);
+ expect(index).toMatch(/src="_ext\/[^"]*song\.mp3"/);
+ expect(index).not.toContain(external);
+ });
+
+ it("inside the project is rewritten to its path in the archive", () => {
+ const { files, index } = stage(
+ (_ext, project) =>
+ ``,
+ );
+ expect(files.has("media/inside.mp3")).toBe(true);
+ expect(index).toContain('src="media/inside.mp3"');
+ });
+
+ it("keeps its #fragment and ?query after the archive path", () => {
+ const inside = stage(
+ (_ext, project) =>
+ ``,
+ );
+ expect(inside.index).toContain('src="media/inside.mp3#t=1"');
+ const outside = stage(
+ (ext) => ``,
+ );
+ expect(outside.index).toMatch(/src="_ext\/[^"]*song\.mp3\?v=2"/);
+ });
+
+ it("that does not exist is left as written, like any root-relative URL", () => {
+ const { index } = stage(() => `
`);
+ expect(index).toContain('src="/assets/none.png"');
+ });
+});
diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts
index 78fd43088b..cfe8f0f007 100644
--- a/packages/cli/src/utils/publishProject.ts
+++ b/packages/cli/src/utils/publishProject.ts
@@ -1,5 +1,5 @@
import { envProxyEnabled } from "@hyperframes/core/env-proxy";
-import { basename, dirname, join, posix, relative, resolve } from "node:path";
+import { basename, dirname, isAbsolute, join, posix, relative, resolve } from "node:path";
import {
closeSync,
existsSync,
@@ -440,25 +440,43 @@ function includeReferencedProjectFile(
/** References with no fallback: a missing file here cannot render. */
const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[poster]"]);
+const relativeToProject = (ctx: ExternalAssetContext, absPath: string) =>
+ encodeUrlPath(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
+
+function archiveTargetOf(
+ ctx: ExternalAssetContext,
+ absPath: string,
+ rawPath: string,
+ options: { requireOnDisk: boolean; filesystemPath: boolean },
+): string | null | undefined {
+ if (isPathInside(absPath, ctx.absProjectDir)) {
+ includeReferencedProjectFile(ctx, absPath, rawPath, options.requireOnDisk);
+ return options.filesystemPath ? relativeToProject(ctx, absPath) : null;
+ }
+ try {
+ if (!existsSync(absPath) || !statSync(absPath).isFile()) return undefined;
+ } catch {
+ return undefined;
+ }
+ return encodeUrlPath(addExternalAsset(ctx, absPath));
+}
+
function tryResolveExternal(
ctx: ExternalAssetContext,
rawPath: string,
referrerAbsDir: string,
requireOnDisk = false,
): string | null {
- if (isNonRelativeUrl(rawPath)) return null;
- for (const path of decodeUrlPathVariants(rawPath)) {
- const absPath = resolve(referrerAbsDir, path);
- if (isPathInside(absPath, ctx.absProjectDir)) {
- includeReferencedProjectFile(ctx, absPath, rawPath, requireOnDisk);
- return null;
- }
- try {
- if (!existsSync(absPath) || !statSync(absPath).isFile()) continue;
- } catch {
- continue;
- }
- return encodeUrlPath(addExternalAsset(ctx, absPath));
+ const filesystemPath = isAbsolute(rawPath) && !rawPath.startsWith("//");
+ if (isNonRelativeUrl(rawPath) && !filesystemPath) return null;
+ const cut = filesystemPath ? rawPath.search(/[?#]/) : -1;
+ const [path, suffix] = cut < 0 ? [rawPath, ""] : [rawPath.slice(0, cut), rawPath.slice(cut)];
+ for (const variant of decodeUrlPathVariants(path)) {
+ const target = archiveTargetOf(ctx, resolve(referrerAbsDir, variant), rawPath, {
+ requireOnDisk,
+ filesystemPath,
+ });
+ if (target !== undefined) return target && target + suffix;
}
return null;
}