diff --git a/src/domain/webScriptExportPlan.test.ts b/src/domain/webScriptExportPlan.test.ts index ca0cf3d20f..5f2c591ce1 100644 --- a/src/domain/webScriptExportPlan.test.ts +++ b/src/domain/webScriptExportPlan.test.ts @@ -83,6 +83,31 @@ describe("captured script path projection", () => { }, ); + it("groups every descendant and duplicate without isolating a lexical neighbor", () => { + const planned = planWebScriptExport( + [ + "main.js/one/child.js", + "main.js-neighbor.js", + "main.js/two/child.js", + "main.js", + "main.js", + "main.jsx/child.js", + ].map((part, index) => source(`https://a.test/app/${part}`, index + 1)), + ); + expect( + planned.map(({ record }) => + record.content.state === "exported" ? record.content.layout : null, + ), + ).toEqual([ + "isolated", + "url-path", + "isolated", + "isolated", + "isolated", + "url-path", + ]); + }); + it.each([ "", "https://a.test/main.js?v=2", diff --git a/src/domain/webScriptExportPlan.ts b/src/domain/webScriptExportPlan.ts index 9b34ce3b20..31fcaa04e5 100644 --- a/src/domain/webScriptExportPlan.ts +++ b/src/domain/webScriptExportPlan.ts @@ -107,30 +107,32 @@ const candidatePath = (script: CapturedWebScript): CandidatePath => { }; }; -// Register every exact prefix so a repeated path, or a file that is also a -// directory prefix, isolates every affected source. Case-distinct spellings -// stay distinct until the destination filesystem reports a collision. +// Sort file paths with a separator that precedes every portable segment +// character. Repetitions and descendants then follow their file prefix +// contiguously, without allocating a string for every directory prefix. const conflictingPaths = ( candidates: readonly CandidatePath[], ): Set => { - const prefixes = new Map(); - candidates.forEach(({ path }, index) => { - if (path === undefined) return; - const parts = path.split("/"); - parts.forEach((_, position) => { - const spelling = parts.slice(0, position + 1).join("/"); - const entry = prefixes.get(spelling) ?? { indices: [], files: [] }; - entry.indices.push(index); - if (position === parts.length - 1) entry.files.push(index); - prefixes.set(spelling, entry); - }); - }); + const sorted = candidates.flatMap(({ path }, index) => + path === undefined ? [] : [{ key: path.replaceAll("/", "\0"), index }], + ); + sorted.sort((left, right) => + left.key < right.key ? -1 : left.key > right.key ? 1 : 0, + ); const conflicts = new Set(); - for (const entry of prefixes.values()) + let root: (typeof sorted)[number] | undefined; + for (const candidate of sorted) { if ( - entry.files.length > 1 || - (entry.files.length > 0 && entry.indices.length > entry.files.length) - ) - for (const index of entry.indices) conflicts.add(index); + root !== undefined && + (candidate.key === root.key || + (candidate.key.charCodeAt(root.key.length) === 0 && + candidate.key.startsWith(root.key))) + ) { + conflicts.add(root.index); + conflicts.add(candidate.index); + } else { + root = candidate; + } + } return conflicts; };