Skip to content

Commit

Permalink
Fix types in presets
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 25, 2024
1 parent 82b63d5 commit ec359a4
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 80 deletions.
5 changes: 3 additions & 2 deletions code/builders/builder-webpack5/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import type {
Options,
BuilderResult as BuilderResultBase,
StorybookConfig,
TypescriptOptions as WebpackTypescriptOptions,
} from '@storybook/core-webpack';

import type ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

type TypeScriptOptionsBase = Required<StorybookConfig>['typescript'];
type TypeScriptOptionsBase = Partial<WebpackTypescriptOptions>;

/**
* Options for TypeScript usage within Storybook.
Expand All @@ -19,7 +20,7 @@ export interface TypescriptOptions extends TypeScriptOptionsBase {
checkOptions?: ConstructorParameters<typeof ForkTsCheckerWebpackPlugin>[0];
}

export interface StorybookConfigWebpack extends Pick<StorybookConfig, 'webpack' | 'webpackFinal'> {
export interface StorybookConfigWebpack extends Omit<StorybookConfig, 'webpack' | 'webpackFinal'> {
/**
* Modify or return a custom Webpack config after the Storybook's default configuration
* has run (mostly used by addons).
Expand Down
4 changes: 2 additions & 2 deletions code/frameworks/angular/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entries
return annotations;
};

export const core: PresetProperty<'core', StorybookConfig> = async (config, options) => {
export const core: PresetProperty<'core'> = async (config, options) => {
const framework = await options.presets.apply('framework');

return {
Expand All @@ -34,7 +34,7 @@ export const core: PresetProperty<'core', StorybookConfig> = async (config, opti
};
};

export const typescript: PresetProperty<'typescript', StorybookConfig> = async (config) => {
export const typescript: PresetProperty<'typescript'> = async (config) => {
return {
...config,
skipCompiler: true,
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/ember/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (baseConfig,
};
};

export const core: PresetProperty<'core', StorybookConfig> = async (config, options) => {
export const core: PresetProperty<'core'> = async (config, options) => {
const framework = await options.presets.apply('framework');

return {
Expand Down
3 changes: 1 addition & 2 deletions code/frameworks/html-vite/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { PresetProperty } from '@storybook/types';
import { dirname, join } from 'path';
import type { StorybookConfig } from './types';

function getAbsolutePath<I extends string>(value: I): I {
return dirname(require.resolve(join(value, 'package.json'))) as any;
}

export const core: PresetProperty<'core', StorybookConfig> = {
export const core: PresetProperty<'core'> = {
builder: getAbsolutePath('@storybook/builder-vite'),
renderer: getAbsolutePath('@storybook/html'),
};
28 changes: 0 additions & 28 deletions code/frameworks/nextjs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,6 @@ export const addons: PresetProperty<'addons'> = [
dirname(require.resolve(join('@storybook/preset-react-webpack', 'package.json'))),
];

const defaultFrameworkOptions: FrameworkOptions = {};

export const frameworkOptions: PresetProperty<'framework'> = async (_, options) => {
const config = await options.presets.apply('framework');

if (typeof config === 'string') {
return {
name: config,
options: defaultFrameworkOptions,
};
}
if (typeof config === 'undefined') {
return {
name: require.resolve('@storybook/nextjs') as '@storybook/nextjs',
options: defaultFrameworkOptions,
};
}

return {
name: config.name,
options: {
...defaultFrameworkOptions,
...config.options,
},
};
};

export const core: PresetProperty<'core'> = async (config, options) => {
const framework = await options.presets.apply('framework');

Expand Down Expand Up @@ -136,7 +109,6 @@ export const babel: PresetProperty<'babel'> = async (baseConfig: TransformOption
};

export const webpackFinal: StorybookConfig['webpackFinal'] = async (baseConfig, options) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const frameworkOptions = await options.presets.apply<{ options: FrameworkOptions }>(
'frameworkOptions'
);
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/preact-vite/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { StorybookConfig } from './types';
const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const core: PresetProperty<'core', StorybookConfig> = {
export const core: PresetProperty<'core'> = {
builder: getAbsolutePath('@storybook/builder-vite'),
renderer: getAbsolutePath('@storybook/preact'),
};
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/react-vite/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { StorybookConfig } from './types';
const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const core: PresetProperty<'core', StorybookConfig> = {
export const core: PresetProperty<'core'> = {
builder: getAbsolutePath('@storybook/builder-vite'),
renderer: getAbsolutePath('@storybook/react'),
};
Expand Down
40 changes: 4 additions & 36 deletions code/frameworks/react-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, join } from 'path';
import type { PresetProperty, Options } from '@storybook/types';
import type { FrameworkOptions, StorybookConfig } from './types';
import type { PresetProperty } from '@storybook/types';
import type { StorybookConfig } from './types';

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;
Expand All @@ -9,46 +9,14 @@ export const addons: PresetProperty<'addons'> = [
getAbsolutePath('@storybook/preset-react-webpack'),
];

const defaultFrameworkOptions: FrameworkOptions = {
legacyRootApi: true,
};

export const frameworkOptions = async (
_: never,
options: Options
): Promise<StorybookConfig['framework']> => {
const config = await options.presets.apply<StorybookConfig['framework']>('framework');

if (typeof config === 'string') {
return {
name: config,
options: defaultFrameworkOptions,
};
}
if (typeof config === 'undefined') {
return {
name: getAbsolutePath('@storybook/react-webpack5'),
options: defaultFrameworkOptions,
};
}

return {
name: config.name,
options: {
...defaultFrameworkOptions,
...config.options,
},
};
};

export const core: PresetProperty<'core'> = async (config, options) => {
const framework = await options.presets.apply('framework');
const presetFramework = await options.presets.apply('framework');

return {
...config,
builder: {
name: getAbsolutePath('@storybook/builder-webpack5'),
options: typeof framework === 'string' ? {} : framework.options.builder || {},
options: typeof presetFramework === 'string' ? {} : presetFramework.options.builder || {},
},
renderer: getAbsolutePath('@storybook/react'),
};
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/svelte-vite/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { svelteDocgen } from './plugins/svelte-docgen';
const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const core: PresetProperty<'core', StorybookConfig> = {
export const core: PresetProperty<'core'> = {
builder: getAbsolutePath('@storybook/builder-vite'),
renderer: getAbsolutePath('@storybook/svelte'),
};
Expand Down
5 changes: 1 addition & 4 deletions code/frameworks/vue3-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { dirname, join } from 'path';
import type { PresetProperty } from '@storybook/types';
import type { StorybookConfig } from './types';

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const addons: PresetProperty<'addons', StorybookConfig> = [
getAbsolutePath('@storybook/preset-vue3-webpack'),
];
export const addons: PresetProperty<'addons'> = [getAbsolutePath('@storybook/preset-vue3-webpack')];

export const core: PresetProperty<'core'> = async (config, options) => {
const framework = await options.presets.apply('framework');
Expand Down
3 changes: 1 addition & 2 deletions code/frameworks/web-components-vite/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { PresetProperty } from '@storybook/types';
import { dirname, join } from 'path';
import type { StorybookConfig } from './types';

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const core: PresetProperty<'core', StorybookConfig> = {
export const core: PresetProperty<'core'> = {
builder: getAbsolutePath('@storybook/builder-vite'),
renderer: getAbsolutePath('@storybook/web-components'),
};

0 comments on commit ec359a4

Please sign in to comment.