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

Core: StorybookConfig stories-field support type async function #21555

Merged
merged 7 commits into from
Nov 27, 2023
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
2 changes: 1 addition & 1 deletion code/addons/actions/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
declare var FEATURES: import('@storybook/types').StorybookConfig['features'];
declare var FEATURES: import('@storybook/types').StorybookConfigRaw['features'];
6 changes: 3 additions & 3 deletions code/addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import remarkSlug from 'remark-slug';
import remarkExternalLinks from 'remark-external-links';
import { dedent } from 'ts-dedent';

import type { DocsOptions, Indexer, Options, StorybookConfig } from '@storybook/types';
import type { DocsOptions, Indexer, Options, PresetProperty } from '@storybook/types';
import type { CsfPluginOptions } from '@storybook/csf-plugin';
import type { JSXOptions, CompileOptions } from '@storybook/mdx2-csf';
import { global } from '@storybook/global';
Expand Down Expand Up @@ -160,7 +160,7 @@ export const createStoriesMdxIndexer = (legacyMdx1?: boolean): Indexer => ({
},
});

const indexers: StorybookConfig['experimental_indexers'] = (existingIndexers) =>
const indexers: PresetProperty<'experimental_indexers'> = (existingIndexers) =>
[createStoriesMdxIndexer(global.FEATURES?.legacyMdx1)].concat(existingIndexers || []);

const docs = (docsOptions: DocsOptions) => {
Expand All @@ -171,7 +171,7 @@ const docs = (docsOptions: DocsOptions) => {
};
};

export const addons: StorybookConfig['addons'] = [
export const addons: PresetProperty<'addons'> = [
require.resolve('@storybook/react-dom-shim/dist/preset'),
];

Expand Down
2 changes: 1 addition & 1 deletion code/addons/docs/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ declare module 'sveltedoc-parser' {
export function parse(options: any): Promise<any>;
}

declare var FEATURES: import('@storybook/types').StorybookConfig['features'];
declare var FEATURES: import('@storybook/types').StorybookConfigRaw['features'];

declare var LOGLEVEL: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent' | undefined;
4 changes: 2 additions & 2 deletions code/builders/builder-manager/src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename } from 'path';
import type { DocsOptions, Options } from '@storybook/types';
import type { Options } from '@storybook/types';
import { getRefs } from '@storybook/core-common';

import { readTemplate } from './template';
Expand All @@ -13,7 +13,7 @@ export const getData = async (options: Options) => {
const features = options.presets.apply<Record<string, string | boolean>>('features');
const logLevel = options.presets.apply<string>('logLevel');
const title = options.presets.apply<string>('title');
const docsOptions = options.presets.apply<DocsOptions>('docs', {});
const docsOptions = options.presets.apply('docs', {});
const template = readTemplate('template.ejs');
const customHead = options.presets.apply<string>('managerHead');

Expand Down
4 changes: 2 additions & 2 deletions code/builders/builder-vite/src/plugins/csf-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Plugin } from 'vite';
import { vite } from '@storybook/csf-plugin';
import type { StorybookConfig, Options } from '@storybook/types';
import type { Options } from '@storybook/types';

export async function csfPlugin(config: Options): Promise<Plugin> {
const { presets } = config;

const addons = await presets.apply<StorybookConfig['addons']>('addons', []);
const addons = await presets.apply('addons', []);
const docsOptions =
// @ts-expect-error - not sure what type to use here
addons.find((a) => [a, a.name].includes('@storybook/addon-docs'))?.options ?? {};
Expand Down
4 changes: 2 additions & 2 deletions code/builders/builder-vite/src/transform-iframe-html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalizeStories } from '@storybook/core-common';
import type { CoreConfig, DocsOptions, Options } from '@storybook/types';
import type { DocsOptions, Options } from '@storybook/types';

export type PreviewHtml = string | undefined;

Expand All @@ -12,7 +12,7 @@ export async function transformIframeHtml(html: string, options: Options) {
const logLevel = await presets.apply('logLevel', undefined);
const docsOptions = await presets.apply<DocsOptions>('docs');

const coreOptions = await presets.apply<CoreConfig>('core');
const coreOptions = await presets.apply('core');
const stories = normalizeStories(await options.presets.apply('stories', [], options), {
configDir: options.configDir,
workingDir: process.cwd(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpackReal from 'webpack';
import { logger } from '@storybook/node-logger';
import type { Options, CoreConfig } from '@storybook/types';
import type { Options } from '@storybook/types';
import type { Configuration } from 'webpack';
import deprecate from 'util-deprecate';
import { dedent } from 'ts-dedent';
Expand All @@ -11,7 +11,7 @@ export async function webpack(config: Configuration, options: Options) {
// @ts-expect-error (Converted from ts-ignore)
const { configDir, configType, presets, webpackConfig } = options;

const coreOptions = await presets.apply<CoreConfig>('core');
const coreOptions = await presets.apply('core');

let defaultConfig = config;
if (!coreOptions?.disableWebpackDefaults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import VirtualModulePlugin from 'webpack-virtual-modules';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import type { TransformOptions as EsbuildOptions } from 'esbuild';
import type { JsMinifyOptions as SwcOptions } from '@swc/core';
import type { Options, CoreConfig, DocsOptions } from '@storybook/types';
import type { Options } from '@storybook/types';
import { globalsNameReferenceMap } from '@storybook/preview/globals';
import {
getBuilderOptions,
Expand Down Expand Up @@ -86,14 +86,14 @@ export default async (
modulesCount = 1000,
build,
] = await Promise.all([
presets.apply<CoreConfig>('core'),
presets.apply('core'),
presets.apply('frameworkOptions'),
presets.apply<Record<string, string>>('env'),
presets.apply('logLevel', undefined),
presets.apply('previewHead'),
presets.apply('previewBody'),
presets.apply<string>('previewMainTemplate'),
presets.apply<DocsOptions>('docs'),
presets.apply('docs'),
presets.apply<string[]>('entries', []),
presets.apply('stories', []),
options.cache?.get('modulesCount').catch(() => {}),
Expand Down
9 changes: 3 additions & 6 deletions code/frameworks/angular/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { StandaloneOptions } from './builders/utils/standalone-options';
const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;

export const addons: PresetProperty<'addons', StorybookConfig> = [
export const addons: PresetProperty<'addons'> = [
require.resolve('./server/framework-preset-angular-cli'),
require.resolve('./server/framework-preset-angular-ivy'),
require.resolve('./server/framework-preset-angular-docs'),
];

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (
entries = [],
options
) => {
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entries = [], options) => {
const annotations = [...entries, require.resolve('./client/config')];

if ((options as any as StandaloneOptions).enableProdMode) {
Expand All @@ -26,7 +23,7 @@ export const previewAnnotations: StorybookConfig['previewAnnotations'] = (
};

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

return {
...config,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StorybookConfig } from '@storybook/types';
import { PresetProperty } from '@storybook/types';
import { hasDocsOrControls } from '@storybook/docs-tools';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = [], options) => {
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = [], options) => {
if (!hasDocsOrControls(options)) return entry;
return [...entry, require.resolve('../client/docs/config')];
};
2 changes: 1 addition & 1 deletion code/frameworks/angular/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare var __STORYBOOK_STORY_STORE__: any;
declare var CHANNEL_OPTIONS: any;
declare var DOCS_OPTIONS: any;

declare var FEATURES: import('@storybook/types').StorybookConfig['features'];
declare var FEATURES: import('@storybook/types').StorybookConfigRaw['features'];

declare var IS_STORYBOOK: any;
declare var LOGLEVEL: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent' | undefined;
Expand Down
4 changes: 2 additions & 2 deletions code/frameworks/ember/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ 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> = [
export const addons: PresetProperty<'addons'> = [
require.resolve('./server/framework-preset-babel-ember'),
require.resolve('./server/framework-preset-ember-docs'),
];

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

return {
...config,
Expand Down
11 changes: 5 additions & 6 deletions code/frameworks/ember/src/server/framework-preset-babel-ember.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TransformOptions } from '@babel/core';
import { precompile } from 'ember-source/dist/ember-template-compiler';
import { findDistEsm } from '@storybook/core-common';
import type { StorybookConfig, Options } from '@storybook/types';
import type { PresetProperty } from '@storybook/types';

let emberOptions: any;

Expand All @@ -14,7 +13,7 @@ function precompileWithPlugins(string: string, options: any) {
return precompile(string, precompileOptions);
}

export function babel(config: TransformOptions, options: Options): TransformOptions {
export const babel: PresetProperty<'babel'> = (config, options) => {
if (options && options.presetsList) {
options.presetsList.forEach((e: any, index: number) => {
if (e.preset && e.preset.emberOptions) {
Expand All @@ -27,7 +26,7 @@ export function babel(config: TransformOptions, options: Options): TransformOpti
});
}

const babelConfigPlugins = config.plugins || [];
const babelConfigPlugins = config?.plugins || [];

const extraPlugins = [
[
Expand All @@ -48,8 +47,8 @@ export function babel(config: TransformOptions, options: Options): TransformOpti
...config,
plugins: [...babelConfigPlugins, ...extraPlugins],
};
}
};

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { StorybookConfig } from '@storybook/types';
import type { PresetProperty } from '@storybook/types';
import { findDistEsm } from '@storybook/core-common';
import { hasDocsOrControls } from '@storybook/docs-tools';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = [], options) => {
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = [], options) => {
if (!hasDocsOrControls(options)) return entry;
return [...entry, findDistEsm(__dirname, 'client/docs/config')];
};
9 changes: 3 additions & 6 deletions code/frameworks/html-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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-html-webpack'),
];
export const addons: PresetProperty<'addons'> = [getAbsolutePath('@storybook/preset-html-webpack')];

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

return {
...config,
Expand Down
21 changes: 9 additions & 12 deletions code/frameworks/nextjs/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://storybook.js.org/docs/react/addons/writing-presets
import { dirname, join } from 'path';
import type { Options, PresetProperty } from '@storybook/types';
import type { ConfigItem, PluginItem, TransformOptions } from '@babel/core';
import type { PresetProperty } from '@storybook/types';
import type { ConfigItem, PluginItem } from '@babel/core';
import { loadPartialConfig } from '@babel/core';
import { getProjectRoot } from '@storybook/core-common';
import { configureConfig } from './config/webpack';
Expand All @@ -19,17 +19,14 @@ import { configureNodePolyfills } from './nodePolyfills/webpack';
import { configureAliasing } from './dependency-map';
import { configureSWCLoader } from './swc/loader';

export const addons: PresetProperty<'addons', StorybookConfig> = [
export const addons: PresetProperty<'addons'> = [
dirname(require.resolve(join('@storybook/preset-react-webpack', 'package.json'))),
];

const defaultFrameworkOptions: FrameworkOptions = {};

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

if (typeof config === 'string') {
return {
Expand All @@ -53,8 +50,8 @@ export const frameworkOptions = async (
};
};

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

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

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => [
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = []) => [
...entry,
join(dirname(require.resolve('@storybook/nextjs/package.json')), 'dist/preview.mjs'),
];

// Not even sb init - automigrate - running dev
// You're using a version of Nextjs prior to v10, which is unsupported by this framework.

export const babel = async (baseConfig: TransformOptions): Promise<TransformOptions> => {
export const babel: PresetProperty<'babel'> = async (baseConfig) => {
const configPartial = loadPartialConfig({
...baseConfig,
filename: `${getProjectRoot()}/__fake__.js`,
Expand Down
7 changes: 3 additions & 4 deletions code/frameworks/preact-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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> = [
export const addons: PresetProperty<'addons'> = [
getAbsolutePath('@storybook/preset-preact-webpack'),
];

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

return {
...config,
Expand Down
6 changes: 3 additions & 3 deletions code/frameworks/react-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { FrameworkOptions, 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> = [
export const addons: PresetProperty<'addons'> = [
getAbsolutePath('@storybook/preset-react-webpack'),
];

Expand Down Expand Up @@ -43,8 +43,8 @@ export const frameworkOptions = async (
};
};

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

return {
...config,
Expand Down
7 changes: 3 additions & 4 deletions code/frameworks/server-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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> = [
export const addons: PresetProperty<'addons'> = [
getAbsolutePath('@storybook/preset-server-webpack'),
];

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

return {
...config,
Expand Down
7 changes: 3 additions & 4 deletions code/frameworks/svelte-webpack5/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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> = [
export const addons: PresetProperty<'addons'> = [
getAbsolutePath('@storybook/preset-svelte-webpack'),
];

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

return {
...config,
Expand Down
Loading