From 23553316a73b481f3566e5251300c5c6a374434e Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 11 Aug 2020 07:23:53 +0800 Subject: [PATCH 1/2] Document preview entries --- MIGRATION.md | 2 +- docs/api/writing-presets.md | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index d846ebc1972c..f400233f5678 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -728,7 +728,7 @@ addons.setConfig({ The `addParameters` and `addDecorator` APIs to add global decorators and parameters, exported by the various frameworks (e.g. `@storybook/react`) and `@storybook/client` are now deprecated. -Instead, use `export const parameters = {};` and `export const decorators = [];` in your `.storybook/preview.js`. Addon authors similarly should use such an export in a `previewEntry` file. +Instead, use `export const parameters = {};` and `export const decorators = [];` in your `.storybook/preview.js`. Addon authors similarly should use such an export in a preview entry file (see [Preview entries](https://github.com/storybookjs/storybook/blob/next/docs/api/writing-presets.md#preview-entries)). #### Deprecated clearDecorators diff --git a/docs/api/writing-presets.md b/docs/api/writing-presets.md index 61eef304857a..b21df09f7255 100644 --- a/docs/api/writing-presets.md +++ b/docs/api/writing-presets.md @@ -2,7 +2,6 @@ title: 'Writing your own Storybook Preset' --- - [Storybook presets](./presets.md) are grouped collections of `babel`, `webpack`, and `addons` configurations that support specific use cases in Storybook, such as typescript or MDX support. This doc covers the [presets API](#presets-api) and how to use the presets mechanism for [advanced configuration](#advanced-configuration). @@ -45,7 +44,6 @@ All functions take a [Babel configuration object](https://babeljs.io/docs/en/con For example, Storybook's Mihtril support uses plugins internally and here's how it configures babel: ```ts - export function babelDefault(config: TransformOptions) { return { ...config, @@ -104,6 +102,35 @@ module.exports = { }; ``` +### Preview entries + +The addon config `config` allows you to add extra preview configuration from within a preset, for example to add parameters or decorators from an addon. + +For example, the Backgrounds preset contains the following code: + +```ts +// preset.ts +export function config(entry: any[] = []) { + return [...entry, require.resolve('./defaultParameters')]; +} +``` + +Which in turn invokes: + +```ts +// defaultParameters.ts +export const parameters = { + backgrounds: { + values: [ + { name: 'light', value: '#F8F8F8' }, + { name: 'dark', value: '#333333' }, + ], + }, +}; +``` + +This is equivalent to exporting the `backgrounds` parameter manually in `main.js`. + ### Addons For users, the name `managerEntries` might be a bit too technical, so instead both users and preset-authors can simply use the property: `addons`: @@ -184,7 +211,6 @@ and extract the configuration to a new file `./storybook/my-preset.js`: ```js // .storybook/my-preset.js - module.exports = { managerWebpack: async (config, options) => { // update config here @@ -203,4 +229,4 @@ module.exports = { }; ``` -Place your `my-preset.js` file wherever you want, if you want to share it far and wide you'll want to make it its own package. \ No newline at end of file +Place your `my-preset.js` file wherever you want, if you want to share it far and wide you'll want to make it its own package. From 313a8b758e9ccb797e0f4790ce51f00675a605d7 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 11 Aug 2020 10:24:31 +0800 Subject: [PATCH 2/2] TS => js --- docs/api/writing-presets.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/api/writing-presets.md b/docs/api/writing-presets.md index b21df09f7255..94432354ef92 100644 --- a/docs/api/writing-presets.md +++ b/docs/api/writing-presets.md @@ -108,17 +108,17 @@ The addon config `config` allows you to add extra preview configuration from wit For example, the Backgrounds preset contains the following code: -```ts -// preset.ts -export function config(entry: any[] = []) { +```js +// preset.js +export function config(entry = []) { return [...entry, require.resolve('./defaultParameters')]; } ``` Which in turn invokes: -```ts -// defaultParameters.ts +```js +// defaultParameters.js export const parameters = { backgrounds: { values: [