Skip to content

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

TypeScript

fix(storybook): load the Vite plugin from source (#5133)

AKdebb82b90336
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index be11335a1f..85936031a4 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -92,9 +92,6 @@ cd astryx
# Install dependencies
pnpm install
-# Build core package first (required for Storybook)
-pnpm -F @astryxdesign/core build
-
# Start Storybook for component development
cd apps/storybook
pnpm dev
@@ -102,19 +99,9 @@ pnpm dev
### Running Storybook
-Storybook loads pre-built packages from `dist/` folders, so you need to build packages before running Storybook.
-
-**First time setup:**
-
-```bash
-# Build all packages
-pnpm build
-
-# Or build just core
-pnpm -F @astryxdesign/core build
-```
-
-**Start Storybook:**
+Storybook resolves every workspace package to its `src/` directory and compiles
+it itself, so a fresh clone needs no build step first — `pnpm install` then
+`pnpm dev` is enough.
```bash
cd apps/storybook
@@ -127,16 +114,8 @@ Storybook will open at http://localhost:6006 with:
- **Mode switcher** - Toggle between Light and Dark modes
- **Component stories** - Interactive component examples
-**If you make changes to `@astryxdesign/core`:**
-
-```bash
-# Rebuild core package
-pnpm -F @astryxdesign/core build
-
-# Restart Storybook to see changes
-cd apps/storybook
-pnpm dev
-```
+**If you make changes to `@astryxdesign/core`:** nothing extra. The dev server
+serves the edited source, so the story updates on save — no rebuild, no restart.
### Running the Doc Site
diff --git a/apps/storybook/.storybook/main.test.ts b/apps/storybook/.storybook/main.test.ts
index 5425eddfb2..eccce93147 100644
--- a/apps/storybook/.storybook/main.test.ts
+++ b/apps/storybook/.storybook/main.test.ts
@@ -2,17 +2,23 @@
/**
* @file main.test.ts
- * @description Guards the workspace source-alias invariant in Storybook's
- * Vite config. Every `@astryxdesign/*` dependency of this app must be
- * aliased to package source in `.storybook/main.ts`. Without an alias,
- * Vite resolves the workspace link through the package's export map,
- * which points at `dist/` build output that does not exist in a fresh
- * worktree — `storybook dev` then fails its dependency scan (#5092:
- * `@astryxdesign/richtext`).
+ * @description Guards two things that both keep `storybook dev` working from
+ * a cold clone, at two different resolution stages.
+ *
+ * 1. Vite's module graph: every `@astryxdesign/*` dependency of this app
+ * must be aliased to package source in `.storybook/main.ts`. Without an
+ * alias, Vite resolves the workspace link through the package's export
+ * map, which points at `dist/` build output that does not exist in a
+ * fresh worktree — `storybook dev` then fails its dependency scan
+ * (#5092: `@astryxdesign/richtext`).
+ * 2. Storybook's own config loader: `main.ts` is evaluated by Node's ESM
+ * resolver before any alias from it is in play, so the specifiers
+ * `main.ts` itself imports must also resolve unbuilt (#5128:
+ * `@astryxdesign/build/vite` → `dist/vite.mjs`).
*/
import {describe, it, expect} from 'vitest';
-import {readFileSync} from 'node:fs';
+import {existsSync, readFileSync} from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
@@ -31,6 +37,15 @@ const workspaceDeps = Object.keys(pkg.dependencies ?? {}).filter(name =>
name.startsWith('@astryxdesign/'),
);
+/**
+ * Specifiers `main.ts` imports at runtime. `import type` statements are
+ * erased before Node ever sees them, so they are excluded — only the
+ * specifiers the config loader actually has to resolve count.
+ */
+const runtimeImports = [
+ ...mainTs.matchAll(/^import\s+(?!type\s)(?:[^;]*?from\s+)?'([^']+)';/gm),
+].map(match => match[1]);
+
describe('storybook main.ts workspace source aliases', () => {
it('sees the @astryxdesign workspace dependencies', () => {
expect(workspaceDeps).not.toHaveLength(0);
@@ -43,3 +58,56 @@ describe('storybook main.ts workspace source aliases', () => {
expect(viteAliasBlock).toContain(`'${dep}':`);
});
});
+
+describe('storybook main.ts config-loader imports (#5128)', () => {
+ it('sees the runtime imports of main.ts', () => {
+ expect(runtimeImports).not.toHaveLength(0);
+ });
+
+ it('imports no workspace package by bare specifier', () => {
+ // A bare `@astryxdesign/*` specifier here goes through that package's
+ // export map, and every workspace export map points at `dist/`. The
+ // Storybook config loader resolves it with plain Node ESM, so no Vite
+ // alias can rescue it — the only cold-clone-safe form is a path into
+ // the package's own source.
+ expect(
+ runtimeImports.filter(spec => spec.startsWith('@astryxdesign/')),
+ ).toEqual([]);
+ });
+
+ it('reaches the Vite plugin through source that exists unbuilt', () => {
+ const spec = runtimeImports.find(s => s.includes('packages/build'));
+ expect(spec, 'main.ts should import the Astryx Vite plugin').toBeDefined();
+
+ const resolved = path.resolve(__dirname, spec as string);
+ expect(resolved).toContain(path.join('packages', 'build', 'src'));
+ expect(existsSync(resolved)).toBe(true);
+ });
+
+ it('evaluates and returns a Vite config carrying the Astryx plugins', async () => {
+ // The behavioral half: on a worktree without `packages/build/dist` this
+ // import is what fails today. It is green on an already-built checkout
+ // either way, which is why the structural assertions above carry the
+ // permanent guard.
+ const {default: config} = (await import('./main.ts')) as {
+ default: {
+ viteFinal: (
+ config: Record<string, unknown>,
+ options: unknown,
+ ) => Promise<{
+ plugins: {name?: string}[];
+ resolve: {alias: Record<string, string>};
+ }>;
+ };
+ };
+
+ const viteConfig = await config.viteFinal({}, {});
+
+ expect(viteConfig.plugins.map(plugin => plugin?.name)).toContain(
+ 'astryx-css-layer-order',
+ );
+ expect(viteConfig.resolve.alias['@astryxdesign/core']).toMatch(
+ /packages[\\/]core[\\/]src$/,
+ );
+ });
+});
diff --git a/apps/storybook/.storybook/main.ts b/apps/storybook/.storybook/main.ts
index 4d9054fb9b..9bad56c5ec 100644
--- a/apps/storybook/.storybook/main.ts
+++ b/apps/storybook/.storybook/main.ts
@@ -1,7 +1,13 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import type {StorybookConfig} from '@storybook/react-vite';
-import {astryxStylex} from '@astryxdesign/build/vite';
+// Source, not the `@astryxdesign/build/vite` export: Storybook evaluates this
+// file with Node's ESM resolver before any alias below it applies, and that
+// export map points at `dist/vite.mjs`, which does not exist until
+// `@astryxdesign/build` is built. Importing the source keeps `pnpm install &&
+// pnpm dev` working from a cold clone (#5128) and matches what this app's
+// tsconfig already resolves for typecheck.
+import {astryxStylex} from '../../../packages/build/src/vite.ts';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
diff --git a/apps/storybook/tsconfig.json b/apps/storybook/tsconfig.json
index 3f94101b02..ec8ee4a950 100644
--- a/apps/storybook/tsconfig.json
+++ b/apps/storybook/tsconfig.json
@@ -1,6 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
+ // `.storybook/main.ts` imports the Astryx Vite plugin from package source
+ // by path, because Storybook's config loader cannot resolve the package's
+ // dist-only `./vite` export in an unbuilt worktree (#5128).
+ "allowImportingTsExtensions": true,
"paths": {
"@astryxdesign/core": [
"../../packages/core/src/index.ts"
@@ -26,9 +30,6 @@
"@astryxdesign/richtext/*": [
"../../packages/richtext/src/*/index.ts",
"../../packages/richtext/src/*"
- ],
- "@astryxdesign/build/vite": [
- "../../packages/build/src/vite.ts"
]
}
},