Skip to content

Commit

Permalink
Next.js-Vite: Streamline Next.js dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Aug 28, 2024
1 parent a400b02 commit dbcb31c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
16 changes: 7 additions & 9 deletions code/frameworks/experimental-nextjs-vite/src/preset.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand All @@ -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>('frameworkOptions');

const nextDir = nextConfigPath ? path.dirname(nextConfigPath) : undefined;
config.plugins.push(vitePluginStorybookNextjs({ dir: nextDir }));

return config;
};
8 changes: 2 additions & 6 deletions code/frameworks/experimental-nextjs-vite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit dbcb31c

Please sign in to comment.