:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import type { StorybookConfig } from '@storybook/react-vite'
import tailwindcss from '@tailwindcss/vite'
import { fileURLToPath } from 'node:url'
import * as path from 'path'
import { mergeConfig, type Rollup } from 'vite'
import { zodJitlessPlugin } from '../../../frontend/plugins/vite-zod-jitless-plugin.ts'
import { frontendResolvePlugin } from './plugins/vite-frontend-resolve-plugin.ts'
import { moduleGraphPlugin } from './plugins/vite-module-graph-plugin.ts'
import { sqlRawPlugin } from './plugins/vite-sql-raw-plugin.ts'
// Storybook 10 loads the config as a native ES module, where `__dirname` is
// not defined — derive it from the module URL instead.
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// Repo root = three levels up from this file (common/storybook/.storybook/main.ts).
const REPO_ROOT = path.resolve(__dirname, '..', '..', '..')
const FRONTEND = path.resolve(REPO_ROOT, 'frontend')
// Keep a single copy of these in the monorepo — duplicate react/kea instances
// break hooks and kea's context. Aliased to the app's copy and deduped.
const SINGLETON_PACKAGES = [
'react',
'react-dom',
'@base-ui/react',
'kea',
'kea-router',
'kea-forms',
'kea-loaders',
'kea-localstorage',
'kea-subscriptions',
'kea-waitfor',
'kea-window-values',
'@testing-library/dom',
]
const STORY_FILE = /\.stories\.(js|jsx|ts|tsx)$/
const createStoriesPathFor = (storyPath: string): string =>
STORY_FILE.test(storyPath) ? `../../../${storyPath}` : `../../../${storyPath}/**/*.stories.@(js|jsx|ts|tsx)`
const DEFAULT_STORY_ROOTS = [
'frontend/src',
'products/**/frontend',
'products/**/mcp/apps',
'services/mcp/src/ui-apps',
'packages/quill/packages/charts/src',
]
// Comma-separated directories or story files, relative to the repo root.
const storyRootsOverride = (process.env.STORYBOOK_STORIES ?? '')
.split(',')
.map((storyRoot) => storyRoot.trim().replace(/\/+$/, ''))
.filter(Boolean)
const config: StorybookConfig = {
stories: (storyRootsOverride.length > 0 ? storyRootsOverride : DEFAULT_STORY_ROOTS).map(createStoriesPathFor),
addons: ['@storybook/addon-docs', '@storybook/addon-links', '@storybook/addon-a11y'],
staticDirs: [
'public',
{ from: '../../../frontend/public', to: '/static' },
// Web workers the app loads by fixed URL from /static (Monaco, HogQL parser). Django
// serves them from frontend/dist; here `pnpm build:workers` builds them into .workers.
// Without them, every `new Worker(...)` 404s, and the resulting error event fails any
// story whose play function is still running when it fires.
{ from: '../.workers', to: '/static' },
{ from: '../../../frontend/node_modules/@posthog/hedgehog-mode/assets', to: '/static/hedgehog-mode' },
// EmojiPickerPanel loads frimousse's emoji data from here. The app build copies the same files into dist.
{ from: '../../../frontend/node_modules/emojibase-data/en', to: '/static/emoji/en' },
// The replay player mounts rrweb into this document rather than the app's, so a recorded
// page is judged against its policy instead of ours. Django renders the same file; serving
// it here keeps stories on the real path rather than a Storybook-only stand-in.
{ from: '../../../posthog/templates/replay_player_frame', to: '/replay_player_frame' },
],
framework: {
name: '@storybook/react-vite',
options: {},
},
// Automated runs skip the highlight feature, which scans every element after each DOM change.
previewHead: (head) =>
`${head}<script>if (navigator.webdriver) { window.__STORYBOOK_HIGHLIGHT_INITIALIZED = true }</script>`,
viteFinal: (viteConfig) =>
mergeConfig(viteConfig, {
plugins: [
frontendResolvePlugin(REPO_ROOT),
tailwindcss(),
sqlRawPlugin(),
moduleGraphPlugin(REPO_ROOT),
zodJitlessPlugin(),
],
resolve: {
dedupe: SINGLETON_PACKAGES,
alias: {
// react-shadow (the toolbar's shadow-DOM renderer) imports react-dom/server.
// The `react-dom` singleton alias below is a prefix match, so it would rewrite
// this subpath to a bare path and resolve the `default` (node) export —
// server.node.js extends stream.Readable, which is undefined in the browser
// build and throws at module eval. Pin the browser build, ahead of that alias.
'react-dom/server': path.resolve(FRONTEND, 'node_modules', 'react-dom', 'server.browser.js'),
// The app's runtime deps live in frontend/node_modules, not under
// common/storybook. Webpack reached them via resolve.modules; Vite has no
// equivalent, so point the ones imported by bundled app/quill code there.
...Object.fromEntries(
SINGLETON_PACKAGES.map((pkg) => [pkg, path.resolve(FRONTEND, 'node_modules', pkg)])
),
'~': path.resolve(FRONTEND, 'src'),
lib: path.resolve(FRONTEND, 'src', 'lib'),
scenes: path.resolve(FRONTEND, 'src', 'scenes'),
queries: path.resolve(FRONTEND, 'src', 'queries'),
layout: path.resolve(FRONTEND, 'src', 'layout'),
taxonomy: path.resolve(FRONTEND, 'src', 'taxonomy'),
models: path.resolve(FRONTEND, 'src', 'models'),
mocks: path.resolve(FRONTEND, 'src', 'mocks'),
exporter: path.resolve(FRONTEND, 'src', 'exporter'),
types: path.resolve(FRONTEND, 'src', 'types.ts'),
public: path.resolve(FRONTEND, 'public'),
products: path.resolve(REPO_ROOT, 'products'),
'@common': path.resolve(REPO_ROOT, 'common'),
'@posthog/lemon-ui': path.resolve(FRONTEND, '@posthog', 'lemon-ui', 'src'),
'@posthog/mcp-ui': path.resolve(REPO_ROOT, 'services', 'mcp', 'src', 'ui-apps', 'lib'),
'@posthog/shared-onboarding': path.resolve(REPO_ROOT, 'docs', 'onboarding'),
'@posthog/quill': path.resolve(REPO_ROOT, 'packages', 'quill', 'packages', 'quill', 'src'),
'@posthog/quill-charts': path.resolve(REPO_ROOT, 'packages', 'quill', 'packages', 'charts', 'src'),
'@posthog/agent-contracts': path.resolve(
REPO_ROOT,
'packages',
'agent',
'packages',
'agent-contracts',
'src'
),
},
},
define: {
global: 'globalThis',
},
optimizeDeps: {
include: ['buffer'],
// @posthog/brand's png stubs locate their image via new URL(..., import.meta.url);
// prebundling into .vite/deps would break that relative resolution. The package is
// pure ESM, so serving it unbundled is safe, and Vite's build handles the URL
// pattern natively.
exclude: ['@posthog/brand'],
rolldownOptions: {
plugins: [zodJitlessPlugin()],
},
},
build: {
// The preload helper's cost grows with the square of the chunk count.
modulePreload: false,
rollupOptions: {
onwarn(warning: Rollup.RollupLog, defaultHandler: (warning: Rollup.RollupLog) => void) {
// "use client" directives from RSC-oriented deps (framer-motion, radix)
// are meaningless here, and barrel-file re-export cycles are endemic to
// the app's index.ts files. Together they bury real warnings in ~1000
// lines of noise per CI build.
if (
warning.code === 'MODULE_LEVEL_DIRECTIVE' ||
warning.code === 'CYCLIC_CROSS_CHUNK_REEXPORT'
) {
return
}
defaultHandler(warning)
},
},
},
}),
docs: {
autodocs: 'tag',
},
}
export default config