Skip to content

Commit 092a6a7

Browse files
author
DevBot
committed
fix(adapter-vite): customElements.define(tagName) counts as tagName usage in the scanner
Routes registering via the platform primitive (www, the reference starter) were misread as orphaned tagName exports and drew the #960 migration note on every build. The usage detector now recognizes both defineElement and customElements.define, with a pin test.
1 parent a2a77c2 commit 092a6a7

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

packages/adapter-vite/__tests__/route-scanner-tagname.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ export default definePage({
189189
}
190190
});
191191

192+
Deno.test('scanRoutes treats customElements.define(tagName) as usage (no orphan note)', async () => {
193+
const dir = await Deno.makeTempDir({ prefix: 'oe-scan-tagname-' });
194+
try {
195+
const routesDir = join(dir, 'routes');
196+
await Deno.mkdir(routesDir, { recursive: true });
197+
// The www site and some starters register via the platform primitive
198+
// instead of defineElement — that is still a use of the export.
199+
await Deno.writeTextFile(
200+
join(routesDir, 'home.tsx'),
201+
`import { definePage } from '@openelement/app';
202+
export const tagName = 'page-home';
203+
class HomePage extends HTMLElement {}
204+
customElements.define(tagName, HomePage);
205+
export default definePage({ render() { return <page-home />; } });
206+
`,
207+
);
208+
209+
const messages = await captureInfo(async () => {
210+
await scanRoutes(routesDir);
211+
});
212+
const notes = messages.filter((m) => m.includes('ignored for registration'));
213+
assertEquals(notes, [], 'customElements.define(tagName) must count as usage');
214+
} finally {
215+
await Deno.remove(dir, { recursive: true }).catch(() => {});
216+
}
217+
});
218+
192219
Deno.test('scanRoutes notes an orphaned tagName export on a definePage route once', async () => {
193220
const dir = await Deno.makeTempDir({ prefix: 'oe-scan-decouple-' });
194221
try {

packages/adapter-vite/src/internal/ssg/route-scanner.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,14 @@ function escapeRegExp(value: string): string {
211211
* an embedded code sample merely suppresses the note (the safe direction).
212212
*/
213213
function routeUsesTagName(source: string, tagName: string): boolean {
214-
if (/\bdefineElement\s*\(\s*tagName\b/.test(source)) return true;
215-
if (new RegExp(`\\bdefineElement\\s*\\(\\s*['"\`]${escapeRegExp(tagName)}['"\`]`).test(source)) {
214+
// defineElement(tagName, …) or customElements.define(tagName, …) — the
215+
// www site and some starters register via the platform primitive.
216+
if (/\b(?:defineElement|customElements\.define)\s*\(\s*tagName\b/.test(source)) return true;
217+
if (
218+
new RegExp(
219+
`\\b(?:defineElement|customElements\\.define)\\s*\\(\\s*['"\`]${escapeRegExp(tagName)}['"\`]`,
220+
).test(source)
221+
) {
216222
return true;
217223
}
218224
return new RegExp(`</?${escapeRegExp(tagName)}(?=[\\s/>])`).test(source);

0 commit comments

Comments
 (0)