From dbcb31c702a9019713c16c6688e9f5d3047ba46c Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Wed, 28 Aug 2024 15:52:41 +0200 Subject: [PATCH] Next.js-Vite: Streamline Next.js dir option --- .../experimental-nextjs-vite/src/preset.ts | 16 +++++++--------- .../experimental-nextjs-vite/src/types.ts | 8 ++------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/code/frameworks/experimental-nextjs-vite/src/preset.ts b/code/frameworks/experimental-nextjs-vite/src/preset.ts index af5deabf2b87..6af7f9a877f0 100644 --- a/code/frameworks/experimental-nextjs-vite/src/preset.ts +++ b/code/frameworks/experimental-nextjs-vite/src/preset.ts @@ -1,4 +1,6 @@ // https://storybook.js.org/docs/react/addons/writing-presets +import path from 'node:path'; + import type { PresetProperty } from 'storybook/internal/types'; import type { StorybookConfigVite } from '@storybook/builder-vite'; @@ -7,7 +9,7 @@ import { dirname, join } from 'path'; // @ts-expect-error - tsconfig settings have to be moduleResolution=Bundler and module=Preserve import vitePluginStorybookNextjs from 'vite-plugin-storybook-nextjs'; -import type { StorybookConfig } from './types'; +import type { FrameworkOptions } from './types'; export const core: PresetProperty<'core'> = async (config, options) => { const framework = await options.presets.apply('framework'); @@ -34,14 +36,10 @@ export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = export const viteFinal: StorybookConfigVite['viteFinal'] = async (config, options) => { config.plugins = config.plugins || []; - const framework = (await options.presets.apply( - 'framework', - {}, - options - )) as StorybookConfig['framework']; - - const nextAppDir = typeof framework !== 'string' ? framework.options.nextAppDir : undefined; - config.plugins.push(vitePluginStorybookNextjs({ dir: nextAppDir })); + const { nextConfigPath } = await options.presets.apply('frameworkOptions'); + + const nextDir = nextConfigPath ? path.dirname(nextConfigPath) : undefined; + config.plugins.push(vitePluginStorybookNextjs({ dir: nextDir })); return config; }; diff --git a/code/frameworks/experimental-nextjs-vite/src/types.ts b/code/frameworks/experimental-nextjs-vite/src/types.ts index cb8ebb737b02..8de91a4430d9 100644 --- a/code/frameworks/experimental-nextjs-vite/src/types.ts +++ b/code/frameworks/experimental-nextjs-vite/src/types.ts @@ -9,12 +9,8 @@ type FrameworkName = CompatibleString<'@storybook/experimental-nextjs-vite'>; type BuilderName = CompatibleString<'@storybook/builder-vite'>; export type FrameworkOptions = { - /** - * The directory where the Next.js app is located. - * - * @default process.cwd() - */ - nextAppDir?: string; + /** The path to the Next.js configuration file. */ + nextConfigPath?: string; builder?: BuilderOptions; };