Skip to content

Commit d00a248

Browse files
skovhusclaude
andcommitted
fix: address review comments for markerFile
- Merge sidecar map entries in transform.ts when multiple files write to the same shared marker path (instead of overwriting) - Thread markerFile through adapterWithLogging in run.ts so the public runTransform API passes it to the per-file transform - Preserve existing markers in --only regeneration mode by seeding from the existing sidecar file - Extract mergeMarkerContent helper to DRY up the merge logic Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 13c8cd1 commit d00a248

4 files changed

Lines changed: 45 additions & 21 deletions

File tree

scripts/regenerate-test-case-outputs.mts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ async function listFixtureNames(): Promise<Array<{ name: string; ext: string }>>
8181
.sort((a, b) => a.name.localeCompare(b.name));
8282
}
8383

84+
/** Merge marker declarations from `incoming` into `base`, appending only new exports. */
85+
function mergeMarkerContent(base: string, incoming: string): string {
86+
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
87+
const newMarkers = [...incoming.matchAll(markerLineRe)].map((m) => m[0]);
88+
const markersToAdd = newMarkers.filter((line) => !base.includes(line));
89+
if (markersToAdd.length === 0) {
90+
return base;
91+
}
92+
const trailingNewline = base.endsWith("\n") ? "" : "\n";
93+
return base + trailingNewline + markersToAdd.join("\n") + "\n";
94+
}
95+
8496
// Accumulate all sidecar files across fixtures for merging (shared marker files)
8597
const allSidecarFiles = new Map<string, string>();
8698

@@ -113,21 +125,7 @@ async function updateFixture(name: string, ext: string) {
113125
// Accumulate sidecar files for merging after all fixtures are processed
114126
for (const [sidecarPath, content] of sidecarFiles) {
115127
const existing = allSidecarFiles.get(sidecarPath);
116-
if (existing) {
117-
// Merge: extract new marker lines and append any that don't already exist
118-
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
119-
const newMarkers = [...content.matchAll(markerLineRe)].map((m) => m[0]);
120-
const markersToAdd = newMarkers.filter((line) => !existing.includes(line));
121-
if (markersToAdd.length > 0) {
122-
const trailingNewline = existing.endsWith("\n") ? "" : "\n";
123-
allSidecarFiles.set(
124-
sidecarPath,
125-
existing + trailingNewline + markersToAdd.join("\n") + "\n",
126-
);
127-
}
128-
} else {
129-
allSidecarFiles.set(sidecarPath, content);
130-
}
128+
allSidecarFiles.set(sidecarPath, existing ? mergeMarkerContent(existing, content) : content);
131129
}
132130

133131
return outputPath;
@@ -159,7 +157,17 @@ for (const { name, ext } of targetFixtures) {
159157
await updateFixture(name, ext);
160158
}
161159

162-
// Write accumulated sidecar files (merged across all fixtures)
160+
// Write accumulated sidecar files (merged across all fixtures).
161+
// When running --only, seed from existing sidecar files so markers from untouched fixtures are preserved.
163162
for (const [sidecarPath, content] of allSidecarFiles) {
164-
await writeFile(sidecarPath, content, "utf-8");
163+
let merged = content;
164+
if (only) {
165+
try {
166+
const existing = await readFile(sidecarPath, "utf-8");
167+
merged = mergeMarkerContent(content, existing);
168+
} catch {
169+
// File doesn't exist yet, write fresh content
170+
}
171+
}
172+
await writeFile(sidecarPath, merged, "utf-8");
165173
}

src/run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ export async function runTransform(options: RunTransformOptions): Promise<RunTra
428428
? resolveBaseComponentWithLogging
429429
: undefined,
430430
resolveThemeCall: resolvedAdapter.resolveThemeCall,
431+
markerFile: resolvedAdapter.markerFile,
431432
};
432433

433434
// Path to the transform module.

src/transform.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,22 @@ export default function transform(file: FileInfo, api: API, options: Options): s
7070
const sidecarPath =
7171
result.sidecarFilePath ??
7272
join(dirname(file.path), `${basename(file.path).replace(/\.\w+$/, "")}.stylex.ts`);
73-
sidecarFiles.set(sidecarPath, result.sidecarContent);
73+
// Merge with existing content when multiple files write to the same sidecar path
74+
const existing = sidecarFiles.get(sidecarPath);
75+
if (existing) {
76+
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
77+
const newMarkers = [...result.sidecarContent.matchAll(markerLineRe)].map((m) => m[0]);
78+
const markersToAdd = newMarkers.filter((line) => !existing.includes(line));
79+
if (markersToAdd.length > 0) {
80+
const trailingNewline = existing.endsWith("\n") ? "" : "\n";
81+
sidecarFiles.set(
82+
sidecarPath,
83+
existing + trailingNewline + markersToAdd.join("\n") + "\n",
84+
);
85+
}
86+
} else {
87+
sidecarFiles.set(sidecarPath, result.sidecarContent);
88+
}
7489
}
7590
}
7691

test-cases/markers.stylex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as stylex from "@stylexjs/stylex";
22

3+
export const ThingMarker = stylex.defineMarker();
4+
export const ThingThemedMarker = stylex.defineMarker();
5+
export const RowMarker = stylex.defineMarker();
36
export const WrapperMarker = stylex.defineMarker();
47
export const LinkMarker = stylex.defineMarker();
58
export const StyledCollapseButtonMarker = stylex.defineMarker();
69
export const IconButtonMarker = stylex.defineMarker();
710
export const CrossFileLinkMarker = stylex.defineMarker();
8-
export const ThingMarker = stylex.defineMarker();
911
export const IconMarker = stylex.defineMarker();
10-
export const ThingThemedMarker = stylex.defineMarker();
11-
export const RowMarker = stylex.defineMarker();

0 commit comments

Comments
 (0)