Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(docsite): render Recharts in the playground (#6263)

Ernestd6fd8d45deb1
diff --git a/apps/docsite/scripts/generate-scope.mjs b/apps/docsite/scripts/generate-scope.mjs
index b2f49d8e4f..bab57d3aa2 100644
--- a/apps/docsite/scripts/generate-scope.mjs
+++ b/apps/docsite/scripts/generate-scope.mjs
@@ -329,3 +329,4 @@ console.log(` ${SCOPE_THEMES.length} themes`);
console.log(
` lucide-react icons + ${HEROICON_VARIANTS.length} heroicon variants`,
);
+console.log(' recharts');
diff --git a/apps/docsite/src/__tests__/playground-scope.test.ts b/apps/docsite/src/__tests__/playground-scope.test.ts
index 2b8802e5fa..c056387a40 100644
--- a/apps/docsite/src/__tests__/playground-scope.test.ts
+++ b/apps/docsite/src/__tests__/playground-scope.test.ts
@@ -37,6 +37,39 @@ const CORE_PKG_PATH = path.resolve(
'../../../../packages/core/package.json',
);
+const TEMPLATE_PAGES_DIR = path.resolve(
+ __dirname,
+ '../../../../packages/cli/assets/templates/pages',
+);
+
+/** Mirrors the runner's asset list — those are stubbed, not scoped. */
+const ASSET_RE =
+ /\.(png|jpe?g|gif|svg|webp|ico|bmp|css|scss|less|sass|woff2?|ttf|eot|otf|mp4|webm|ogg|mp3|wav)$/i;
+
+/**
+ * Real import statements only. Anchored to the start of a line and required to
+ * end there, so the `from '...'` inside a code sample a template renders as a
+ * string — documentation-technical has one — is not mistaken for a dependency.
+ * The `}` branch catches the closing line of a multi-line import.
+ */
+const IMPORT_RE = /^\s*(?:import\b[^'"]*?|\}\s*)from\s+'([^']+)';?\s*$/gm;
+
+/**
+ * Top-level keys of the generated `scope` object. Quoted for anything with a
+ * slash or dash, bare where the name is a valid identifier. Lowercased to
+ * match how the runner builds `scopeLookup`.
+ */
+function getScopeKeys(content: string): Set<string> {
+ const keys = new Set<string>();
+ for (const match of content.matchAll(/^ {2}'([^']+)':/gm)) {
+ keys.add(match[1].toLowerCase());
+ }
+ for (const match of content.matchAll(/^ {2}([A-Za-z][\w-]*):/gm)) {
+ keys.add(match[1].toLowerCase());
+ }
+ return keys;
+}
+
/**
* Same skip pattern as generate-scope.mjs — non-component exports like
* CSS files, utils, hooks, theme, etc.
@@ -189,4 +222,43 @@ describe('playground-scope', () => {
expect(scopeContent).toContain("'next/image':");
expect(scopeContent).toContain("React.createElement('img', props)");
});
+
+ // ── What the templates actually import ─────────────────────────────────
+
+ it('resolves every bare import in the page templates', () => {
+ // The assertions above are an allowlist, and an allowlist only covers what
+ // someone remembered to add. Recharts was missing from the scope for as
+ // long as the dashboard templates existed, and nothing here noticed,
+ // because nothing asked the templates what they need. The runner's
+ // fallback makes that silent: an unresolved module yields a proxy whose
+ // every export is `() => null`, so the charts vanished while the legends
+ // around them kept painting. Ask the templates instead.
+ const specifiers = new Set<string>();
+ for (const dir of fs.readdirSync(TEMPLATE_PAGES_DIR)) {
+ const page = path.join(TEMPLATE_PAGES_DIR, dir, 'page.tsx');
+ if (!fs.existsSync(page)) {
+ continue;
+ }
+ const source = fs.readFileSync(page, 'utf-8');
+ for (const match of source.matchAll(IMPORT_RE)) {
+ const id = match[1];
+ // Relative paths resolve within the template, and assets are stubbed
+ // by the runner on purpose.
+ if (id.startsWith('.') || ASSET_RE.test(id)) {
+ continue;
+ }
+ specifiers.add(id);
+ }
+ }
+
+ expect(specifiers.size).toBeGreaterThan(5);
+ // The runner lowercases both sides when resolving, so '@astryxdesign/
+ // core/Theme' finds the 'theme' entry. Match that here or the test
+ // reports gaps the playground does not actually have.
+ const keys = getScopeKeys(scopeContent);
+ const missing = [...specifiers]
+ .filter(id => !keys.has(id.toLowerCase()))
+ .sort();
+ expect(missing).toEqual([]);
+ });
});
diff --git a/apps/docsite/src/app/playground/preview/runner.ts b/apps/docsite/src/app/playground/preview/runner.ts
index 196e21d901..489aad4f83 100644
--- a/apps/docsite/src/app/playground/preview/runner.ts
+++ b/apps/docsite/src/app/playground/preview/runner.ts
@@ -36,6 +36,29 @@ for (const [key, value] of Object.entries(scope)) {
scopeLookup.set(key.toLowerCase(), value as Record<string, unknown>);
}
+const warnedModules = new Set<string>();
+
+/**
+ * The placeholder proxy below is deliberately forgiving — user code in the
+ * playground may import anything, and a hard failure on every stray import
+ * would make the editor unusable. The cost is that a module missing from the
+ * generated scope is indistinguishable from one that renders nothing: Recharts
+ * went unnoticed this way, with every chart in the dashboard templates
+ * resolving to `() => null` while the legends around it painted normally. Say
+ * so once per module, so the next gap is one console line rather than a
+ * silently empty preview.
+ */
+function warnUnresolved(id: string): void {
+ if (warnedModules.has(id)) {
+ return;
+ }
+ warnedModules.add(id);
+ console.warn(
+ `[playground] "${id}" is not in the preview scope — its exports will ` +
+ `render nothing. Add it in apps/docsite/scripts/generate-scope.mjs.`,
+ );
+}
+
/** A CommonJS-style require resolving against the preview scope. */
function makeRequire(): (id: string) => unknown {
return (id: string) => {
@@ -49,6 +72,7 @@ function makeRequire(): (id: string) => unknown {
? mod
: {...mod, __esModule: true};
}
+ warnUnresolved(id);
// Unknown module — return placeholders that render nothing.
return new Proxy(
{__esModule: true},