Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js-Vite: Streamline Next.js dir option #28995

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using import path from 'path' for consistency with other imports


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
Loading