Skip to content

Commit aa5f2ed

Browse files
committed
fix(cli): an absolute media path keeps its fragment and query, and the resolver is split
1 parent 2d05052 commit aa5f2ed

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎packages/cli/src/utils/publishProject.absolutePath.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ describe("a media file referenced by its filesystem path", () => {
4444
expect(index).toContain('src="media/inside.mp3"');
4545
});
4646

47+
it("keeps its #fragment and ?query after the archive path", () => {
48+
const inside = stage(
49+
(_ext, project) =>
50+
`<html><audio id="a" src="${join(project, "media/inside.mp3")}#t=1"></audio></html>`,
51+
);
52+
expect(inside.index).toContain('src="media/inside.mp3#t=1"');
53+
const outside = stage(
54+
(ext) => `<html><audio id="a" src="${join(ext, "song.mp3")}?v=2"></audio></html>`,
55+
);
56+
expect(outside.index).toMatch(/src="_ext\/[^"]*song\.mp3\?v=2"/);
57+
});
58+
4759
it("that does not exist is left as written, like any root-relative URL", () => {
4860
const { index } = stage(() => `<html><img src="/assets/none.png"></html>`);
4961
expect(index).toContain('src="/assets/none.png"');

‎packages/cli/src/utils/publishProject.ts‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,27 +443,40 @@ const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[
443443
const relativeToProject = (ctx: ExternalAssetContext, absPath: string) =>
444444
encodeUrlPath(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
445445

446+
function archiveTargetOf(
447+
ctx: ExternalAssetContext,
448+
absPath: string,
449+
rawPath: string,
450+
options: { requireOnDisk: boolean; filesystemPath: boolean },
451+
): string | null | undefined {
452+
if (isPathInside(absPath, ctx.absProjectDir)) {
453+
includeReferencedProjectFile(ctx, absPath, rawPath, options.requireOnDisk);
454+
return options.filesystemPath ? relativeToProject(ctx, absPath) : null;
455+
}
456+
try {
457+
if (!existsSync(absPath) || !statSync(absPath).isFile()) return undefined;
458+
} catch {
459+
return undefined;
460+
}
461+
return encodeUrlPath(addExternalAsset(ctx, absPath));
462+
}
463+
446464
function tryResolveExternal(
447465
ctx: ExternalAssetContext,
448466
rawPath: string,
449467
referrerAbsDir: string,
450468
requireOnDisk = false,
451469
): string | null {
452-
// A leading "/" is a filesystem path to the local render, and the cloud render host refuses one.
453470
const filesystemPath = rawPath.startsWith("/") && !rawPath.startsWith("//");
454471
if (isNonRelativeUrl(rawPath) && !filesystemPath) return null;
455-
for (const path of decodeUrlPathVariants(rawPath)) {
456-
const absPath = resolve(referrerAbsDir, path);
457-
if (isPathInside(absPath, ctx.absProjectDir)) {
458-
includeReferencedProjectFile(ctx, absPath, rawPath, requireOnDisk);
459-
return filesystemPath ? relativeToProject(ctx, absPath) : null;
460-
}
461-
try {
462-
if (!existsSync(absPath) || !statSync(absPath).isFile()) continue;
463-
} catch {
464-
continue;
465-
}
466-
return encodeUrlPath(addExternalAsset(ctx, absPath));
472+
const cut = filesystemPath ? rawPath.search(/[?#]/) : -1;
473+
const [path, suffix] = cut < 0 ? [rawPath, ""] : [rawPath.slice(0, cut), rawPath.slice(cut)];
474+
for (const variant of decodeUrlPathVariants(path)) {
475+
const target = archiveTargetOf(ctx, resolve(referrerAbsDir, variant), rawPath, {
476+
requireOnDisk,
477+
filesystemPath,
478+
});
479+
if (target !== undefined) return target && target + suffix;
467480
}
468481
return null;
469482
}

0 commit comments

Comments
 (0)