1+ import type { StorybookConfig } from "@storybook/nextjs-vite" ;
2+
3+ import { dirname , join , relative } from "path"
4+
5+ import { fileURLToPath } from "url"
6+ import { existsSync } from "fs"
7+
8+ /**
9+ * This function is used to resolve the absolute path of a package.
10+ * It is needed in projects that use Yarn PnP or are set up within a monorepo.
11+ */
12+ function getAbsolutePath ( value : string ) : any {
13+ return dirname ( fileURLToPath ( import . meta. resolve ( `${ value } /package.json` ) ) )
14+ }
15+
16+ /**
17+ * Find the git repository root by walking up the directory tree
18+ */
19+ function findGitRoot ( startPath : string ) : string | null {
20+ let currentPath = startPath ;
21+
22+ while ( currentPath !== dirname ( currentPath ) ) {
23+ if ( existsSync ( join ( currentPath , '.git' ) ) ) {
24+ return currentPath ;
25+ }
26+ currentPath = dirname ( currentPath ) ;
27+ }
28+
29+ return null ;
30+ }
31+ const config : StorybookConfig = {
32+ "stories" : [
33+ "../src/**/*.mdx" ,
34+ "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"
35+ ] ,
36+ "addons" : [
37+ getAbsolutePath ( '@chromatic-com/storybook' ) ,
38+ getAbsolutePath ( '@storybook/addon-docs' ) ,
39+ getAbsolutePath ( '@storybook/addon-onboarding' ) ,
40+ getAbsolutePath ( "@storybook/addon-a11y" ) ,
41+ getAbsolutePath ( "@storybook/addon-vitest" )
42+ ] ,
43+ "framework" : {
44+ "name" : getAbsolutePath ( "@storybook/nextjs-vite" ) ,
45+ "options" : { }
46+ } ,
47+ "staticDirs" : [
48+ "../public"
49+ ] ,
50+ async viteFinal ( config ) {
51+ const { mergeConfig } = await import ( 'vite' ) ;
52+
53+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
54+ const storybookDir = join ( __dirname , '..' ) ;
55+ const gitRoot = findGitRoot ( storybookDir ) ;
56+ const storybookLocation = gitRoot ? relative ( gitRoot , storybookDir ) : '' ;
57+
58+ return mergeConfig ( config , {
59+ define : {
60+ 'process.env' : '{}' ,
61+ 'process' : '{"env": {}}' ,
62+ } ,
63+ resolve : {
64+ alias : {
65+ '@/utils/supabase/client' : fileURLToPath (
66+ new URL ( './mocks/supabase-client.ts' , import . meta. url )
67+ ) ,
68+ '@/trpc/react' : fileURLToPath (
69+ new URL ( './mocks/trpc-react.tsx' , import . meta. url )
70+ ) ,
71+ '~/trpc/react' : fileURLToPath (
72+ new URL ( './mocks/trpc-react.tsx' , import . meta. url )
73+ ) ,
74+ } ,
75+ } ,
76+ plugins : [
77+ {
78+ name : 'onbook-metadata' ,
79+ configureServer ( server ) {
80+ // Serve metadata in dev mode
81+ server . middlewares . use ( ( req , res , next ) => {
82+ if ( req . url === '/onbook-metadata.json' ) {
83+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
84+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
85+ res . end ( JSON . stringify ( { storybookLocation } ) ) ;
86+ return ;
87+ }
88+ next ( ) ;
89+ } ) ;
90+ } ,
91+ configurePreviewServer ( server ) {
92+ // Serve metadata in preview/build mode (for Chromatic)
93+ server . middlewares . use ( ( req , res , next ) => {
94+ if ( req . url === '/onbook-metadata.json' ) {
95+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
96+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
97+ res . end ( JSON . stringify ( { storybookLocation } ) ) ;
98+ return ;
99+ }
100+ next ( ) ;
101+ } ) ;
102+ } ,
103+ } ,
104+ ] ,
105+ } ) ;
106+ } ,
107+ } ;
108+ export default config ;
0 commit comments